src/share/vm/c1/c1_Optimizer.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
child 9148
03e0bbd8e9dd
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "c1/c1_Canonicalizer.hpp"
aoqi@0 27 #include "c1/c1_Optimizer.hpp"
aoqi@0 28 #include "c1/c1_ValueMap.hpp"
aoqi@0 29 #include "c1/c1_ValueSet.hpp"
aoqi@0 30 #include "c1/c1_ValueStack.hpp"
aoqi@0 31 #include "utilities/bitMap.inline.hpp"
aoqi@0 32 #include "compiler/compileLog.hpp"
aoqi@0 33
aoqi@0 34 define_array(ValueSetArray, ValueSet*);
aoqi@0 35 define_stack(ValueSetList, ValueSetArray);
aoqi@0 36
aoqi@0 37
aoqi@0 38 Optimizer::Optimizer(IR* ir) {
aoqi@0 39 assert(ir->is_valid(), "IR must be valid");
aoqi@0 40 _ir = ir;
aoqi@0 41 }
aoqi@0 42
aoqi@0 43 class CE_Eliminator: public BlockClosure {
aoqi@0 44 private:
aoqi@0 45 IR* _hir;
aoqi@0 46 int _cee_count; // the number of CEs successfully eliminated
aoqi@0 47 int _ifop_count; // the number of IfOps successfully simplified
aoqi@0 48 int _has_substitution;
aoqi@0 49
aoqi@0 50 public:
aoqi@0 51 CE_Eliminator(IR* hir) : _cee_count(0), _ifop_count(0), _hir(hir) {
aoqi@0 52 _has_substitution = false;
aoqi@0 53 _hir->iterate_preorder(this);
aoqi@0 54 if (_has_substitution) {
aoqi@0 55 // substituted some ifops/phis, so resolve the substitution
aoqi@0 56 SubstitutionResolver sr(_hir);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 CompileLog* log = _hir->compilation()->log();
aoqi@0 60 if (log != NULL)
aoqi@0 61 log->set_context("optimize name='cee'");
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 ~CE_Eliminator() {
aoqi@0 65 CompileLog* log = _hir->compilation()->log();
aoqi@0 66 if (log != NULL)
aoqi@0 67 log->clear_context(); // skip marker if nothing was printed
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 int cee_count() const { return _cee_count; }
aoqi@0 71 int ifop_count() const { return _ifop_count; }
aoqi@0 72
aoqi@0 73 void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) {
aoqi@0 74 int e = sux->number_of_exception_handlers();
aoqi@0 75 for (int i = 0; i < e; i++) {
aoqi@0 76 BlockBegin* xhandler = sux->exception_handler_at(i);
aoqi@0 77 block->add_exception_handler(xhandler);
aoqi@0 78
aoqi@0 79 assert(xhandler->is_predecessor(sux), "missing predecessor");
aoqi@0 80 if (sux->number_of_preds() == 0) {
aoqi@0 81 // sux is disconnected from graph so disconnect from exception handlers
aoqi@0 82 xhandler->remove_predecessor(sux);
aoqi@0 83 }
aoqi@0 84 if (!xhandler->is_predecessor(block)) {
aoqi@0 85 xhandler->add_predecessor(block);
aoqi@0 86 }
aoqi@0 87 }
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 virtual void block_do(BlockBegin* block);
aoqi@0 91
aoqi@0 92 private:
aoqi@0 93 Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval);
aoqi@0 94 };
aoqi@0 95
aoqi@0 96 void CE_Eliminator::block_do(BlockBegin* block) {
aoqi@0 97 // 1) find conditional expression
aoqi@0 98 // check if block ends with an If
aoqi@0 99 If* if_ = block->end()->as_If();
aoqi@0 100 if (if_ == NULL) return;
aoqi@0 101
aoqi@0 102 // check if If works on int or object types
aoqi@0 103 // (we cannot handle If's working on long, float or doubles yet,
aoqi@0 104 // since IfOp doesn't support them - these If's show up if cmp
aoqi@0 105 // operations followed by If's are eliminated)
aoqi@0 106 ValueType* if_type = if_->x()->type();
aoqi@0 107 if (!if_type->is_int() && !if_type->is_object()) return;
aoqi@0 108
aoqi@0 109 BlockBegin* t_block = if_->tsux();
aoqi@0 110 BlockBegin* f_block = if_->fsux();
aoqi@0 111 Instruction* t_cur = t_block->next();
aoqi@0 112 Instruction* f_cur = f_block->next();
aoqi@0 113
aoqi@0 114 // one Constant may be present between BlockBegin and BlockEnd
aoqi@0 115 Value t_const = NULL;
aoqi@0 116 Value f_const = NULL;
aoqi@0 117 if (t_cur->as_Constant() != NULL && !t_cur->can_trap()) {
aoqi@0 118 t_const = t_cur;
aoqi@0 119 t_cur = t_cur->next();
aoqi@0 120 }
aoqi@0 121 if (f_cur->as_Constant() != NULL && !f_cur->can_trap()) {
aoqi@0 122 f_const = f_cur;
aoqi@0 123 f_cur = f_cur->next();
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 // check if both branches end with a goto
aoqi@0 127 Goto* t_goto = t_cur->as_Goto();
aoqi@0 128 if (t_goto == NULL) return;
aoqi@0 129 Goto* f_goto = f_cur->as_Goto();
aoqi@0 130 if (f_goto == NULL) return;
aoqi@0 131
aoqi@0 132 // check if both gotos merge into the same block
aoqi@0 133 BlockBegin* sux = t_goto->default_sux();
aoqi@0 134 if (sux != f_goto->default_sux()) return;
aoqi@0 135
aoqi@0 136 // check if at least one word was pushed on sux_state
aoqi@0 137 // inlining depths must match
aoqi@0 138 ValueStack* if_state = if_->state();
aoqi@0 139 ValueStack* sux_state = sux->state();
aoqi@0 140 if (if_state->scope()->level() > sux_state->scope()->level()) {
aoqi@0 141 while (sux_state->scope() != if_state->scope()) {
aoqi@0 142 if_state = if_state->caller_state();
aoqi@0 143 assert(if_state != NULL, "states do not match up");
aoqi@0 144 }
aoqi@0 145 } else if (if_state->scope()->level() < sux_state->scope()->level()) {
aoqi@0 146 while (sux_state->scope() != if_state->scope()) {
aoqi@0 147 sux_state = sux_state->caller_state();
aoqi@0 148 assert(sux_state != NULL, "states do not match up");
aoqi@0 149 }
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 if (sux_state->stack_size() <= if_state->stack_size()) return;
aoqi@0 153
aoqi@0 154 // check if phi function is present at end of successor stack and that
aoqi@0 155 // only this phi was pushed on the stack
aoqi@0 156 Value sux_phi = sux_state->stack_at(if_state->stack_size());
aoqi@0 157 if (sux_phi == NULL || sux_phi->as_Phi() == NULL || sux_phi->as_Phi()->block() != sux) return;
aoqi@0 158 if (sux_phi->type()->size() != sux_state->stack_size() - if_state->stack_size()) return;
aoqi@0 159
aoqi@0 160 // get the values that were pushed in the true- and false-branch
aoqi@0 161 Value t_value = t_goto->state()->stack_at(if_state->stack_size());
aoqi@0 162 Value f_value = f_goto->state()->stack_at(if_state->stack_size());
aoqi@0 163
aoqi@0 164 // backend does not support floats
aoqi@0 165 assert(t_value->type()->base() == f_value->type()->base(), "incompatible types");
aoqi@0 166 if (t_value->type()->is_float_kind()) return;
aoqi@0 167
aoqi@0 168 // check that successor has no other phi functions but sux_phi
aoqi@0 169 // this can happen when t_block or f_block contained additonal stores to local variables
aoqi@0 170 // that are no longer represented by explicit instructions
aoqi@0 171 for_each_phi_fun(sux, phi,
aoqi@0 172 if (phi != sux_phi) return;
aoqi@0 173 );
aoqi@0 174 // true and false blocks can't have phis
aoqi@0 175 for_each_phi_fun(t_block, phi, return; );
aoqi@0 176 for_each_phi_fun(f_block, phi, return; );
aoqi@0 177
aoqi@0 178 // 2) substitute conditional expression
aoqi@0 179 // with an IfOp followed by a Goto
aoqi@0 180 // cut if_ away and get node before
aoqi@0 181 Instruction* cur_end = if_->prev();
aoqi@0 182
aoqi@0 183 // append constants of true- and false-block if necessary
aoqi@0 184 // clone constants because original block must not be destroyed
aoqi@0 185 assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
aoqi@0 186 if (t_value == t_const) {
aoqi@0 187 t_value = new Constant(t_const->type());
aoqi@0 188 NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
aoqi@0 189 cur_end = cur_end->set_next(t_value);
aoqi@0 190 }
aoqi@0 191 if (f_value == f_const) {
aoqi@0 192 f_value = new Constant(f_const->type());
aoqi@0 193 NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
aoqi@0 194 cur_end = cur_end->set_next(f_value);
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value);
aoqi@0 198 assert(result != NULL, "make_ifop must return a non-null instruction");
aoqi@0 199 if (!result->is_linked() && result->can_be_linked()) {
aoqi@0 200 NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
aoqi@0 201 cur_end = cur_end->set_next(result);
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 // append Goto to successor
aoqi@0 205 ValueStack* state_before = if_->state_before();
aoqi@0 206 Goto* goto_ = new Goto(sux, state_before, if_->is_safepoint() || t_goto->is_safepoint() || f_goto->is_safepoint());
aoqi@0 207
aoqi@0 208 // prepare state for Goto
aoqi@0 209 ValueStack* goto_state = if_state;
aoqi@0 210 goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
aoqi@0 211 goto_state->push(result->type(), result);
aoqi@0 212 assert(goto_state->is_same(sux_state), "states must match now");
aoqi@0 213 goto_->set_state(goto_state);
aoqi@0 214
aoqi@0 215 cur_end = cur_end->set_next(goto_, goto_state->bci());
aoqi@0 216
aoqi@0 217 // Adjust control flow graph
aoqi@0 218 BlockBegin::disconnect_edge(block, t_block);
aoqi@0 219 BlockBegin::disconnect_edge(block, f_block);
aoqi@0 220 if (t_block->number_of_preds() == 0) {
aoqi@0 221 BlockBegin::disconnect_edge(t_block, sux);
aoqi@0 222 }
aoqi@0 223 adjust_exception_edges(block, t_block);
aoqi@0 224 if (f_block->number_of_preds() == 0) {
aoqi@0 225 BlockBegin::disconnect_edge(f_block, sux);
aoqi@0 226 }
aoqi@0 227 adjust_exception_edges(block, f_block);
aoqi@0 228
aoqi@0 229 // update block end
aoqi@0 230 block->set_end(goto_);
aoqi@0 231
aoqi@0 232 // substitute the phi if possible
aoqi@0 233 if (sux_phi->as_Phi()->operand_count() == 1) {
aoqi@0 234 assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
aoqi@0 235 sux_phi->set_subst(result);
aoqi@0 236 _has_substitution = true;
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 // 3) successfully eliminated a conditional expression
aoqi@0 240 _cee_count++;
aoqi@0 241 if (PrintCEE) {
aoqi@0 242 tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id());
aoqi@0 243 tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 _hir->verify();
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) {
aoqi@0 250 if (!OptimizeIfOps) {
aoqi@0 251 return new IfOp(x, cond, y, tval, fval);
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 tval = tval->subst();
aoqi@0 255 fval = fval->subst();
aoqi@0 256 if (tval == fval) {
aoqi@0 257 _ifop_count++;
aoqi@0 258 return tval;
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 x = x->subst();
aoqi@0 262 y = y->subst();
aoqi@0 263
aoqi@0 264 Constant* y_const = y->as_Constant();
aoqi@0 265 if (y_const != NULL) {
aoqi@0 266 IfOp* x_ifop = x->as_IfOp();
aoqi@0 267 if (x_ifop != NULL) { // x is an ifop, y is a constant
aoqi@0 268 Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
aoqi@0 269 Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
aoqi@0 270
aoqi@0 271 if (x_tval_const != NULL && x_fval_const != NULL) {
aoqi@0 272 Instruction::Condition x_ifop_cond = x_ifop->cond();
aoqi@0 273
aoqi@0 274 Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
aoqi@0 275 Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
aoqi@0 276
aoqi@0 277 // not_comparable here is a valid return in case we're comparing unloaded oop constants
aoqi@0 278 if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
aoqi@0 279 Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
aoqi@0 280 Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
aoqi@0 281
aoqi@0 282 _ifop_count++;
aoqi@0 283 if (new_tval == new_fval) {
aoqi@0 284 return new_tval;
aoqi@0 285 } else {
aoqi@0 286 return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval);
aoqi@0 287 }
aoqi@0 288 }
aoqi@0 289 }
aoqi@0 290 } else {
aoqi@0 291 Constant* x_const = x->as_Constant();
aoqi@0 292 if (x_const != NULL) { // x and y are constants
aoqi@0 293 Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
aoqi@0 294 // not_comparable here is a valid return in case we're comparing unloaded oop constants
aoqi@0 295 if (x_compare_res != Constant::not_comparable) {
aoqi@0 296 _ifop_count++;
aoqi@0 297 return x_compare_res == Constant::cond_true ? tval : fval;
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301 }
aoqi@0 302 return new IfOp(x, cond, y, tval, fval);
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 void Optimizer::eliminate_conditional_expressions() {
aoqi@0 306 // find conditional expressions & replace them with IfOps
aoqi@0 307 CE_Eliminator ce(ir());
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 class BlockMerger: public BlockClosure {
aoqi@0 311 private:
aoqi@0 312 IR* _hir;
aoqi@0 313 int _merge_count; // the number of block pairs successfully merged
aoqi@0 314
aoqi@0 315 public:
aoqi@0 316 BlockMerger(IR* hir)
aoqi@0 317 : _hir(hir)
aoqi@0 318 , _merge_count(0)
aoqi@0 319 {
aoqi@0 320 _hir->iterate_preorder(this);
aoqi@0 321 CompileLog* log = _hir->compilation()->log();
aoqi@0 322 if (log != NULL)
aoqi@0 323 log->set_context("optimize name='eliminate_blocks'");
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 ~BlockMerger() {
aoqi@0 327 CompileLog* log = _hir->compilation()->log();
aoqi@0 328 if (log != NULL)
aoqi@0 329 log->clear_context(); // skip marker if nothing was printed
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 bool try_merge(BlockBegin* block) {
aoqi@0 333 BlockEnd* end = block->end();
aoqi@0 334 if (end->as_Goto() != NULL) {
aoqi@0 335 assert(end->number_of_sux() == 1, "end must have exactly one successor");
aoqi@0 336 // Note: It would be sufficient to check for the number of successors (= 1)
aoqi@0 337 // in order to decide if this block can be merged potentially. That
aoqi@0 338 // would then also include switch statements w/ only a default case.
aoqi@0 339 // However, in that case we would need to make sure the switch tag
aoqi@0 340 // expression is executed if it can produce observable side effects.
aoqi@0 341 // We should probably have the canonicalizer simplifying such switch
aoqi@0 342 // statements and then we are sure we don't miss these merge opportunities
aoqi@0 343 // here (was bug - gri 7/7/99).
aoqi@0 344 BlockBegin* sux = end->default_sux();
aoqi@0 345 if (sux->number_of_preds() == 1 && !sux->is_entry_block() && !end->is_safepoint()) {
aoqi@0 346 // merge the two blocks
aoqi@0 347
aoqi@0 348 #ifdef ASSERT
aoqi@0 349 // verify that state at the end of block and at the beginning of sux are equal
aoqi@0 350 // no phi functions must be present at beginning of sux
aoqi@0 351 ValueStack* sux_state = sux->state();
aoqi@0 352 ValueStack* end_state = end->state();
aoqi@0 353
aoqi@0 354 assert(end_state->scope() == sux_state->scope(), "scopes must match");
aoqi@0 355 assert(end_state->stack_size() == sux_state->stack_size(), "stack not equal");
aoqi@0 356 assert(end_state->locals_size() == sux_state->locals_size(), "locals not equal");
aoqi@0 357
aoqi@0 358 int index;
aoqi@0 359 Value sux_value;
aoqi@0 360 for_each_stack_value(sux_state, index, sux_value) {
aoqi@0 361 assert(sux_value == end_state->stack_at(index), "stack not equal");
aoqi@0 362 }
aoqi@0 363 for_each_local_value(sux_state, index, sux_value) {
aoqi@0 364 assert(sux_value == end_state->local_at(index), "locals not equal");
aoqi@0 365 }
aoqi@0 366 assert(sux_state->caller_state() == end_state->caller_state(), "caller not equal");
aoqi@0 367 #endif
aoqi@0 368
aoqi@0 369 // find instruction before end & append first instruction of sux block
aoqi@0 370 Instruction* prev = end->prev();
aoqi@0 371 Instruction* next = sux->next();
aoqi@0 372 assert(prev->as_BlockEnd() == NULL, "must not be a BlockEnd");
aoqi@0 373 prev->set_next(next);
aoqi@0 374 prev->fixup_block_pointers();
aoqi@0 375 sux->disconnect_from_graph();
aoqi@0 376 block->set_end(sux->end());
aoqi@0 377 // add exception handlers of deleted block, if any
aoqi@0 378 for (int k = 0; k < sux->number_of_exception_handlers(); k++) {
aoqi@0 379 BlockBegin* xhandler = sux->exception_handler_at(k);
aoqi@0 380 block->add_exception_handler(xhandler);
aoqi@0 381
aoqi@0 382 // also substitute predecessor of exception handler
aoqi@0 383 assert(xhandler->is_predecessor(sux), "missing predecessor");
aoqi@0 384 xhandler->remove_predecessor(sux);
aoqi@0 385 if (!xhandler->is_predecessor(block)) {
aoqi@0 386 xhandler->add_predecessor(block);
aoqi@0 387 }
aoqi@0 388 }
aoqi@0 389
aoqi@0 390 // debugging output
aoqi@0 391 _merge_count++;
aoqi@0 392 if (PrintBlockElimination) {
aoqi@0 393 tty->print_cr("%d. merged B%d & B%d (stack size = %d)",
aoqi@0 394 _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size());
aoqi@0 395 }
aoqi@0 396
aoqi@0 397 _hir->verify();
aoqi@0 398
aoqi@0 399 If* if_ = block->end()->as_If();
aoqi@0 400 if (if_) {
aoqi@0 401 IfOp* ifop = if_->x()->as_IfOp();
aoqi@0 402 Constant* con = if_->y()->as_Constant();
aoqi@0 403 bool swapped = false;
aoqi@0 404 if (!con || !ifop) {
aoqi@0 405 ifop = if_->y()->as_IfOp();
aoqi@0 406 con = if_->x()->as_Constant();
aoqi@0 407 swapped = true;
aoqi@0 408 }
aoqi@0 409 if (con && ifop) {
aoqi@0 410 Constant* tval = ifop->tval()->as_Constant();
aoqi@0 411 Constant* fval = ifop->fval()->as_Constant();
aoqi@0 412 if (tval && fval) {
aoqi@0 413 // Find the instruction before if_, starting with ifop.
aoqi@0 414 // When if_ and ifop are not in the same block, prev
aoqi@0 415 // becomes NULL In such (rare) cases it is not
aoqi@0 416 // profitable to perform the optimization.
aoqi@0 417 Value prev = ifop;
aoqi@0 418 while (prev != NULL && prev->next() != if_) {
aoqi@0 419 prev = prev->next();
aoqi@0 420 }
aoqi@0 421
aoqi@0 422 if (prev != NULL) {
aoqi@0 423 Instruction::Condition cond = if_->cond();
aoqi@0 424 BlockBegin* tsux = if_->tsux();
aoqi@0 425 BlockBegin* fsux = if_->fsux();
aoqi@0 426 if (swapped) {
aoqi@0 427 cond = Instruction::mirror(cond);
aoqi@0 428 }
aoqi@0 429
aoqi@0 430 BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
aoqi@0 431 BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
aoqi@0 432 if (tblock != fblock && !if_->is_safepoint()) {
aoqi@0 433 If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
aoqi@0 434 tblock, fblock, if_->state_before(), if_->is_safepoint());
aoqi@0 435 newif->set_state(if_->state()->copy());
aoqi@0 436
aoqi@0 437 assert(prev->next() == if_, "must be guaranteed by above search");
aoqi@0 438 NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
aoqi@0 439 prev->set_next(newif);
aoqi@0 440 block->set_end(newif);
aoqi@0 441
aoqi@0 442 _merge_count++;
aoqi@0 443 if (PrintBlockElimination) {
aoqi@0 444 tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
aoqi@0 445 }
aoqi@0 446
aoqi@0 447 _hir->verify();
aoqi@0 448 }
aoqi@0 449 }
aoqi@0 450 }
aoqi@0 451 }
aoqi@0 452 }
aoqi@0 453
aoqi@0 454 return true;
aoqi@0 455 }
aoqi@0 456 }
aoqi@0 457 return false;
aoqi@0 458 }
aoqi@0 459
aoqi@0 460 virtual void block_do(BlockBegin* block) {
aoqi@0 461 _hir->verify();
aoqi@0 462 // repeat since the same block may merge again
aoqi@0 463 while (try_merge(block)) {
aoqi@0 464 _hir->verify();
aoqi@0 465 }
aoqi@0 466 }
aoqi@0 467 };
aoqi@0 468
aoqi@0 469
aoqi@0 470 void Optimizer::eliminate_blocks() {
aoqi@0 471 // merge blocks if possible
aoqi@0 472 BlockMerger bm(ir());
aoqi@0 473 }
aoqi@0 474
aoqi@0 475
aoqi@0 476 class NullCheckEliminator;
aoqi@0 477 class NullCheckVisitor: public InstructionVisitor {
aoqi@0 478 private:
aoqi@0 479 NullCheckEliminator* _nce;
aoqi@0 480 NullCheckEliminator* nce() { return _nce; }
aoqi@0 481
aoqi@0 482 public:
aoqi@0 483 NullCheckVisitor() {}
aoqi@0 484
aoqi@0 485 void set_eliminator(NullCheckEliminator* nce) { _nce = nce; }
aoqi@0 486
aoqi@0 487 void do_Phi (Phi* x);
aoqi@0 488 void do_Local (Local* x);
aoqi@0 489 void do_Constant (Constant* x);
aoqi@0 490 void do_LoadField (LoadField* x);
aoqi@0 491 void do_StoreField (StoreField* x);
aoqi@0 492 void do_ArrayLength (ArrayLength* x);
aoqi@0 493 void do_LoadIndexed (LoadIndexed* x);
aoqi@0 494 void do_StoreIndexed (StoreIndexed* x);
aoqi@0 495 void do_NegateOp (NegateOp* x);
aoqi@0 496 void do_ArithmeticOp (ArithmeticOp* x);
aoqi@0 497 void do_ShiftOp (ShiftOp* x);
aoqi@0 498 void do_LogicOp (LogicOp* x);
aoqi@0 499 void do_CompareOp (CompareOp* x);
aoqi@0 500 void do_IfOp (IfOp* x);
aoqi@0 501 void do_Convert (Convert* x);
aoqi@0 502 void do_NullCheck (NullCheck* x);
aoqi@0 503 void do_TypeCast (TypeCast* x);
aoqi@0 504 void do_Invoke (Invoke* x);
aoqi@0 505 void do_NewInstance (NewInstance* x);
aoqi@0 506 void do_NewTypeArray (NewTypeArray* x);
aoqi@0 507 void do_NewObjectArray (NewObjectArray* x);
aoqi@0 508 void do_NewMultiArray (NewMultiArray* x);
aoqi@0 509 void do_CheckCast (CheckCast* x);
aoqi@0 510 void do_InstanceOf (InstanceOf* x);
aoqi@0 511 void do_MonitorEnter (MonitorEnter* x);
aoqi@0 512 void do_MonitorExit (MonitorExit* x);
aoqi@0 513 void do_Intrinsic (Intrinsic* x);
aoqi@0 514 void do_BlockBegin (BlockBegin* x);
aoqi@0 515 void do_Goto (Goto* x);
aoqi@0 516 void do_If (If* x);
aoqi@0 517 void do_IfInstanceOf (IfInstanceOf* x);
aoqi@0 518 void do_TableSwitch (TableSwitch* x);
aoqi@0 519 void do_LookupSwitch (LookupSwitch* x);
aoqi@0 520 void do_Return (Return* x);
aoqi@0 521 void do_Throw (Throw* x);
aoqi@0 522 void do_Base (Base* x);
aoqi@0 523 void do_OsrEntry (OsrEntry* x);
aoqi@0 524 void do_ExceptionObject(ExceptionObject* x);
aoqi@0 525 void do_RoundFP (RoundFP* x);
aoqi@0 526 void do_UnsafeGetRaw (UnsafeGetRaw* x);
aoqi@0 527 void do_UnsafePutRaw (UnsafePutRaw* x);
aoqi@0 528 void do_UnsafeGetObject(UnsafeGetObject* x);
aoqi@0 529 void do_UnsafePutObject(UnsafePutObject* x);
aoqi@0 530 void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x);
aoqi@0 531 void do_UnsafePrefetchRead (UnsafePrefetchRead* x);
aoqi@0 532 void do_UnsafePrefetchWrite(UnsafePrefetchWrite* x);
aoqi@0 533 void do_ProfileCall (ProfileCall* x);
aoqi@0 534 void do_ProfileReturnType (ProfileReturnType* x);
aoqi@0 535 void do_ProfileInvoke (ProfileInvoke* x);
aoqi@0 536 void do_RuntimeCall (RuntimeCall* x);
aoqi@0 537 void do_MemBar (MemBar* x);
aoqi@0 538 void do_RangeCheckPredicate(RangeCheckPredicate* x);
aoqi@0 539 #ifdef ASSERT
aoqi@0 540 void do_Assert (Assert* x);
aoqi@0 541 #endif
aoqi@0 542 };
aoqi@0 543
aoqi@0 544
aoqi@0 545 // Because of a static contained within (for the purpose of iteration
aoqi@0 546 // over instructions), it is only valid to have one of these active at
aoqi@0 547 // a time
aoqi@0 548 class NullCheckEliminator: public ValueVisitor {
aoqi@0 549 private:
aoqi@0 550 Optimizer* _opt;
aoqi@0 551
aoqi@0 552 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
aoqi@0 553 BlockList* _work_list; // Basic blocks to visit
aoqi@0 554
aoqi@0 555 bool visitable(Value x) {
aoqi@0 556 assert(_visitable_instructions != NULL, "check");
aoqi@0 557 return _visitable_instructions->contains(x);
aoqi@0 558 }
aoqi@0 559 void mark_visited(Value x) {
aoqi@0 560 assert(_visitable_instructions != NULL, "check");
aoqi@0 561 _visitable_instructions->remove(x);
aoqi@0 562 }
aoqi@0 563 void mark_visitable(Value x) {
aoqi@0 564 assert(_visitable_instructions != NULL, "check");
aoqi@0 565 _visitable_instructions->put(x);
aoqi@0 566 }
aoqi@0 567 void clear_visitable_state() {
aoqi@0 568 assert(_visitable_instructions != NULL, "check");
aoqi@0 569 _visitable_instructions->clear();
aoqi@0 570 }
aoqi@0 571
aoqi@0 572 ValueSet* _set; // current state, propagated to subsequent BlockBegins
aoqi@0 573 ValueSetList _block_states; // BlockBegin null-check states for all processed blocks
aoqi@0 574 NullCheckVisitor _visitor;
aoqi@0 575 NullCheck* _last_explicit_null_check;
aoqi@0 576
aoqi@0 577 bool set_contains(Value x) { assert(_set != NULL, "check"); return _set->contains(x); }
aoqi@0 578 void set_put (Value x) { assert(_set != NULL, "check"); _set->put(x); }
aoqi@0 579 void set_remove (Value x) { assert(_set != NULL, "check"); _set->remove(x); }
aoqi@0 580
aoqi@0 581 BlockList* work_list() { return _work_list; }
aoqi@0 582
aoqi@0 583 void iterate_all();
aoqi@0 584 void iterate_one(BlockBegin* block);
aoqi@0 585
aoqi@0 586 ValueSet* state() { return _set; }
aoqi@0 587 void set_state_from (ValueSet* state) { _set->set_from(state); }
aoqi@0 588 ValueSet* state_for (BlockBegin* block) { return _block_states[block->block_id()]; }
aoqi@0 589 void set_state_for (BlockBegin* block, ValueSet* stack) { _block_states[block->block_id()] = stack; }
aoqi@0 590 // Returns true if caused a change in the block's state.
aoqi@0 591 bool merge_state_for(BlockBegin* block,
aoqi@0 592 ValueSet* incoming_state);
aoqi@0 593
aoqi@0 594 public:
aoqi@0 595 // constructor
aoqi@0 596 NullCheckEliminator(Optimizer* opt)
aoqi@0 597 : _opt(opt)
aoqi@0 598 , _set(new ValueSet())
aoqi@0 599 , _last_explicit_null_check(NULL)
aoqi@0 600 , _block_states(BlockBegin::number_of_blocks(), NULL)
aoqi@0 601 , _work_list(new BlockList()) {
aoqi@0 602 _visitable_instructions = new ValueSet();
aoqi@0 603 _visitor.set_eliminator(this);
aoqi@0 604 CompileLog* log = _opt->ir()->compilation()->log();
aoqi@0 605 if (log != NULL)
aoqi@0 606 log->set_context("optimize name='null_check_elimination'");
aoqi@0 607 }
aoqi@0 608
aoqi@0 609 ~NullCheckEliminator() {
aoqi@0 610 CompileLog* log = _opt->ir()->compilation()->log();
aoqi@0 611 if (log != NULL)
aoqi@0 612 log->clear_context(); // skip marker if nothing was printed
aoqi@0 613 }
aoqi@0 614
aoqi@0 615 Optimizer* opt() { return _opt; }
aoqi@0 616 IR* ir () { return opt()->ir(); }
aoqi@0 617
aoqi@0 618 // Process a graph
aoqi@0 619 void iterate(BlockBegin* root);
aoqi@0 620
aoqi@0 621 void visit(Value* f);
aoqi@0 622
aoqi@0 623 // In some situations (like NullCheck(x); getfield(x)) the debug
aoqi@0 624 // information from the explicit NullCheck can be used to populate
aoqi@0 625 // the getfield, even if the two instructions are in different
aoqi@0 626 // scopes; this allows implicit null checks to be used but the
aoqi@0 627 // correct exception information to be generated. We must clear the
aoqi@0 628 // last-traversed NullCheck when we reach a potentially-exception-
aoqi@0 629 // throwing instruction, as well as in some other cases.
aoqi@0 630 void set_last_explicit_null_check(NullCheck* check) { _last_explicit_null_check = check; }
aoqi@0 631 NullCheck* last_explicit_null_check() { return _last_explicit_null_check; }
aoqi@0 632 Value last_explicit_null_check_obj() { return (_last_explicit_null_check
aoqi@0 633 ? _last_explicit_null_check->obj()
aoqi@0 634 : NULL); }
aoqi@0 635 NullCheck* consume_last_explicit_null_check() {
aoqi@0 636 _last_explicit_null_check->unpin(Instruction::PinExplicitNullCheck);
aoqi@0 637 _last_explicit_null_check->set_can_trap(false);
aoqi@0 638 return _last_explicit_null_check;
aoqi@0 639 }
aoqi@0 640 void clear_last_explicit_null_check() { _last_explicit_null_check = NULL; }
aoqi@0 641
aoqi@0 642 // Handlers for relevant instructions
aoqi@0 643 // (separated out from NullCheckVisitor for clarity)
aoqi@0 644
aoqi@0 645 // The basic contract is that these must leave the instruction in
aoqi@0 646 // the desired state; must not assume anything about the state of
aoqi@0 647 // the instruction. We make multiple passes over some basic blocks
aoqi@0 648 // and the last pass is the only one whose result is valid.
aoqi@0 649 void handle_AccessField (AccessField* x);
aoqi@0 650 void handle_ArrayLength (ArrayLength* x);
aoqi@0 651 void handle_LoadIndexed (LoadIndexed* x);
aoqi@0 652 void handle_StoreIndexed (StoreIndexed* x);
aoqi@0 653 void handle_NullCheck (NullCheck* x);
aoqi@0 654 void handle_Invoke (Invoke* x);
aoqi@0 655 void handle_NewInstance (NewInstance* x);
aoqi@0 656 void handle_NewArray (NewArray* x);
aoqi@0 657 void handle_AccessMonitor (AccessMonitor* x);
aoqi@0 658 void handle_Intrinsic (Intrinsic* x);
aoqi@0 659 void handle_ExceptionObject (ExceptionObject* x);
aoqi@0 660 void handle_Phi (Phi* x);
aoqi@0 661 void handle_ProfileCall (ProfileCall* x);
aoqi@0 662 void handle_ProfileReturnType (ProfileReturnType* x);
aoqi@0 663 };
aoqi@0 664
aoqi@0 665
aoqi@0 666 // NEEDS_CLEANUP
aoqi@0 667 // There may be other instructions which need to clear the last
aoqi@0 668 // explicit null check. Anything across which we can not hoist the
aoqi@0 669 // debug information for a NullCheck instruction must clear it. It
aoqi@0 670 // might be safer to pattern match "NullCheck ; {AccessField,
aoqi@0 671 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
aoqi@0 672 // Should test to see performance hit of clearing it for all handlers
aoqi@0 673 // with empty bodies below. If it is negligible then we should leave
aoqi@0 674 // that in for safety, otherwise should think more about it.
aoqi@0 675 void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); }
aoqi@0 676 void NullCheckVisitor::do_Local (Local* x) {}
aoqi@0 677 void NullCheckVisitor::do_Constant (Constant* x) { /* FIXME: handle object constants */ }
aoqi@0 678 void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); }
aoqi@0 679 void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); }
aoqi@0 680 void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); }
aoqi@0 681 void NullCheckVisitor::do_LoadIndexed (LoadIndexed* x) { nce()->handle_LoadIndexed(x); }
aoqi@0 682 void NullCheckVisitor::do_StoreIndexed (StoreIndexed* x) { nce()->handle_StoreIndexed(x); }
aoqi@0 683 void NullCheckVisitor::do_NegateOp (NegateOp* x) {}
aoqi@0 684 void NullCheckVisitor::do_ArithmeticOp (ArithmeticOp* x) { if (x->can_trap()) nce()->clear_last_explicit_null_check(); }
aoqi@0 685 void NullCheckVisitor::do_ShiftOp (ShiftOp* x) {}
aoqi@0 686 void NullCheckVisitor::do_LogicOp (LogicOp* x) {}
aoqi@0 687 void NullCheckVisitor::do_CompareOp (CompareOp* x) {}
aoqi@0 688 void NullCheckVisitor::do_IfOp (IfOp* x) {}
aoqi@0 689 void NullCheckVisitor::do_Convert (Convert* x) {}
aoqi@0 690 void NullCheckVisitor::do_NullCheck (NullCheck* x) { nce()->handle_NullCheck(x); }
aoqi@0 691 void NullCheckVisitor::do_TypeCast (TypeCast* x) {}
aoqi@0 692 void NullCheckVisitor::do_Invoke (Invoke* x) { nce()->handle_Invoke(x); }
aoqi@0 693 void NullCheckVisitor::do_NewInstance (NewInstance* x) { nce()->handle_NewInstance(x); }
aoqi@0 694 void NullCheckVisitor::do_NewTypeArray (NewTypeArray* x) { nce()->handle_NewArray(x); }
aoqi@0 695 void NullCheckVisitor::do_NewObjectArray (NewObjectArray* x) { nce()->handle_NewArray(x); }
aoqi@0 696 void NullCheckVisitor::do_NewMultiArray (NewMultiArray* x) { nce()->handle_NewArray(x); }
aoqi@0 697 void NullCheckVisitor::do_CheckCast (CheckCast* x) { nce()->clear_last_explicit_null_check(); }
aoqi@0 698 void NullCheckVisitor::do_InstanceOf (InstanceOf* x) {}
aoqi@0 699 void NullCheckVisitor::do_MonitorEnter (MonitorEnter* x) { nce()->handle_AccessMonitor(x); }
aoqi@0 700 void NullCheckVisitor::do_MonitorExit (MonitorExit* x) { nce()->handle_AccessMonitor(x); }
aoqi@0 701 void NullCheckVisitor::do_Intrinsic (Intrinsic* x) { nce()->handle_Intrinsic(x); }
aoqi@0 702 void NullCheckVisitor::do_BlockBegin (BlockBegin* x) {}
aoqi@0 703 void NullCheckVisitor::do_Goto (Goto* x) {}
aoqi@0 704 void NullCheckVisitor::do_If (If* x) {}
aoqi@0 705 void NullCheckVisitor::do_IfInstanceOf (IfInstanceOf* x) {}
aoqi@0 706 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
aoqi@0 707 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
aoqi@0 708 void NullCheckVisitor::do_Return (Return* x) {}
aoqi@0 709 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
aoqi@0 710 void NullCheckVisitor::do_Base (Base* x) {}
aoqi@0 711 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
aoqi@0 712 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
aoqi@0 713 void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
aoqi@0 714 void NullCheckVisitor::do_UnsafeGetRaw (UnsafeGetRaw* x) {}
aoqi@0 715 void NullCheckVisitor::do_UnsafePutRaw (UnsafePutRaw* x) {}
aoqi@0 716 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}
aoqi@0 717 void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {}
aoqi@0 718 void NullCheckVisitor::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {}
aoqi@0 719 void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {}
aoqi@0 720 void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
aoqi@0 721 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check();
aoqi@0 722 nce()->handle_ProfileCall(x); }
aoqi@0 723 void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); }
aoqi@0 724 void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {}
aoqi@0 725 void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {}
aoqi@0 726 void NullCheckVisitor::do_MemBar (MemBar* x) {}
aoqi@0 727 void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
aoqi@0 728 #ifdef ASSERT
aoqi@0 729 void NullCheckVisitor::do_Assert (Assert* x) {}
aoqi@0 730 #endif
aoqi@0 731
aoqi@0 732 void NullCheckEliminator::visit(Value* p) {
aoqi@0 733 assert(*p != NULL, "should not find NULL instructions");
aoqi@0 734 if (visitable(*p)) {
aoqi@0 735 mark_visited(*p);
aoqi@0 736 (*p)->visit(&_visitor);
aoqi@0 737 }
aoqi@0 738 }
aoqi@0 739
aoqi@0 740 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
aoqi@0 741 ValueSet* state = state_for(block);
aoqi@0 742 if (state == NULL) {
aoqi@0 743 state = incoming_state->copy();
aoqi@0 744 set_state_for(block, state);
aoqi@0 745 return true;
aoqi@0 746 } else {
aoqi@0 747 bool changed = state->set_intersect(incoming_state);
aoqi@0 748 if (PrintNullCheckElimination && changed) {
aoqi@0 749 tty->print_cr("Block %d's null check state changed", block->block_id());
aoqi@0 750 }
aoqi@0 751 return changed;
aoqi@0 752 }
aoqi@0 753 }
aoqi@0 754
aoqi@0 755
aoqi@0 756 void NullCheckEliminator::iterate_all() {
aoqi@0 757 while (work_list()->length() > 0) {
aoqi@0 758 iterate_one(work_list()->pop());
aoqi@0 759 }
aoqi@0 760 }
aoqi@0 761
aoqi@0 762
aoqi@0 763 void NullCheckEliminator::iterate_one(BlockBegin* block) {
aoqi@0 764 clear_visitable_state();
aoqi@0 765 // clear out an old explicit null checks
aoqi@0 766 set_last_explicit_null_check(NULL);
aoqi@0 767
aoqi@0 768 if (PrintNullCheckElimination) {
aoqi@0 769 tty->print_cr(" ...iterating block %d in null check elimination for %s::%s%s",
aoqi@0 770 block->block_id(),
aoqi@0 771 ir()->method()->holder()->name()->as_utf8(),
aoqi@0 772 ir()->method()->name()->as_utf8(),
aoqi@0 773 ir()->method()->signature()->as_symbol()->as_utf8());
aoqi@0 774 }
aoqi@0 775
aoqi@0 776 // Create new state if none present (only happens at root)
aoqi@0 777 if (state_for(block) == NULL) {
aoqi@0 778 ValueSet* tmp_state = new ValueSet();
aoqi@0 779 set_state_for(block, tmp_state);
aoqi@0 780 // Initial state is that local 0 (receiver) is non-null for
aoqi@0 781 // non-static methods
aoqi@0 782 ValueStack* stack = block->state();
aoqi@0 783 IRScope* scope = stack->scope();
aoqi@0 784 ciMethod* method = scope->method();
aoqi@0 785 if (!method->is_static()) {
aoqi@0 786 Local* local0 = stack->local_at(0)->as_Local();
aoqi@0 787 assert(local0 != NULL, "must be");
aoqi@0 788 assert(local0->type() == objectType, "invalid type of receiver");
aoqi@0 789
aoqi@0 790 if (local0 != NULL) {
aoqi@0 791 // Local 0 is used in this scope
aoqi@0 792 tmp_state->put(local0);
aoqi@0 793 if (PrintNullCheckElimination) {
aoqi@0 794 tty->print_cr("Local 0 (value %d) proven non-null upon entry", local0->id());
aoqi@0 795 }
aoqi@0 796 }
aoqi@0 797 }
aoqi@0 798 }
aoqi@0 799
aoqi@0 800 // Must copy block's state to avoid mutating it during iteration
aoqi@0 801 // through the block -- otherwise "not-null" states can accidentally
aoqi@0 802 // propagate "up" through the block during processing of backward
aoqi@0 803 // branches and algorithm is incorrect (and does not converge)
aoqi@0 804 set_state_from(state_for(block));
aoqi@0 805
aoqi@0 806 // allow visiting of Phis belonging to this block
aoqi@0 807 for_each_phi_fun(block, phi,
aoqi@0 808 mark_visitable(phi);
aoqi@0 809 );
aoqi@0 810
aoqi@0 811 BlockEnd* e = block->end();
aoqi@0 812 assert(e != NULL, "incomplete graph");
aoqi@0 813 int i;
aoqi@0 814
aoqi@0 815 // Propagate the state before this block into the exception
aoqi@0 816 // handlers. They aren't true successors since we aren't guaranteed
aoqi@0 817 // to execute the whole block before executing them. Also putting
aoqi@0 818 // them on first seems to help reduce the amount of iteration to
aoqi@0 819 // reach a fixed point.
aoqi@0 820 for (i = 0; i < block->number_of_exception_handlers(); i++) {
aoqi@0 821 BlockBegin* next = block->exception_handler_at(i);
aoqi@0 822 if (merge_state_for(next, state())) {
aoqi@0 823 if (!work_list()->contains(next)) {
aoqi@0 824 work_list()->push(next);
aoqi@0 825 }
aoqi@0 826 }
aoqi@0 827 }
aoqi@0 828
aoqi@0 829 // Iterate through block, updating state.
aoqi@0 830 for (Instruction* instr = block; instr != NULL; instr = instr->next()) {
aoqi@0 831 // Mark instructions in this block as visitable as they are seen
aoqi@0 832 // in the instruction list. This keeps the iteration from
aoqi@0 833 // visiting instructions which are references in other blocks or
aoqi@0 834 // visiting instructions more than once.
aoqi@0 835 mark_visitable(instr);
aoqi@0 836 if (instr->is_pinned() || instr->can_trap() || (instr->as_NullCheck() != NULL)) {
aoqi@0 837 mark_visited(instr);
aoqi@0 838 instr->input_values_do(this);
aoqi@0 839 instr->visit(&_visitor);
aoqi@0 840 }
aoqi@0 841 }
aoqi@0 842
aoqi@0 843 // Propagate state to successors if necessary
aoqi@0 844 for (i = 0; i < e->number_of_sux(); i++) {
aoqi@0 845 BlockBegin* next = e->sux_at(i);
aoqi@0 846 if (merge_state_for(next, state())) {
aoqi@0 847 if (!work_list()->contains(next)) {
aoqi@0 848 work_list()->push(next);
aoqi@0 849 }
aoqi@0 850 }
aoqi@0 851 }
aoqi@0 852 }
aoqi@0 853
aoqi@0 854
aoqi@0 855 void NullCheckEliminator::iterate(BlockBegin* block) {
aoqi@0 856 work_list()->push(block);
aoqi@0 857 iterate_all();
aoqi@0 858 }
aoqi@0 859
aoqi@0 860 void NullCheckEliminator::handle_AccessField(AccessField* x) {
aoqi@0 861 if (x->is_static()) {
aoqi@0 862 if (x->as_LoadField() != NULL) {
aoqi@0 863 // If the field is a non-null static final object field (as is
aoqi@0 864 // often the case for sun.misc.Unsafe), put this LoadField into
aoqi@0 865 // the non-null map
aoqi@0 866 ciField* field = x->field();
aoqi@0 867 if (field->is_constant()) {
aoqi@0 868 ciConstant field_val = field->constant_value();
aoqi@0 869 BasicType field_type = field_val.basic_type();
aoqi@0 870 if (field_type == T_OBJECT || field_type == T_ARRAY) {
aoqi@0 871 ciObject* obj_val = field_val.as_object();
aoqi@0 872 if (!obj_val->is_null_object()) {
aoqi@0 873 if (PrintNullCheckElimination) {
aoqi@0 874 tty->print_cr("AccessField %d proven non-null by static final non-null oop check",
aoqi@0 875 x->id());
aoqi@0 876 }
aoqi@0 877 set_put(x);
aoqi@0 878 }
aoqi@0 879 }
aoqi@0 880 }
aoqi@0 881 }
aoqi@0 882 // Be conservative
aoqi@0 883 clear_last_explicit_null_check();
aoqi@0 884 return;
aoqi@0 885 }
aoqi@0 886
aoqi@0 887 Value obj = x->obj();
aoqi@0 888 if (set_contains(obj)) {
aoqi@0 889 // Value is non-null => update AccessField
aoqi@0 890 if (last_explicit_null_check_obj() == obj && !x->needs_patching()) {
aoqi@0 891 x->set_explicit_null_check(consume_last_explicit_null_check());
aoqi@0 892 x->set_needs_null_check(true);
aoqi@0 893 if (PrintNullCheckElimination) {
aoqi@0 894 tty->print_cr("Folded NullCheck %d into AccessField %d's null check for value %d",
aoqi@0 895 x->explicit_null_check()->id(), x->id(), obj->id());
aoqi@0 896 }
aoqi@0 897 } else {
aoqi@0 898 x->set_explicit_null_check(NULL);
aoqi@0 899 x->set_needs_null_check(false);
aoqi@0 900 if (PrintNullCheckElimination) {
aoqi@0 901 tty->print_cr("Eliminated AccessField %d's null check for value %d", x->id(), obj->id());
aoqi@0 902 }
aoqi@0 903 }
aoqi@0 904 } else {
aoqi@0 905 set_put(obj);
aoqi@0 906 if (PrintNullCheckElimination) {
aoqi@0 907 tty->print_cr("AccessField %d of value %d proves value to be non-null", x->id(), obj->id());
aoqi@0 908 }
aoqi@0 909 // Ensure previous passes do not cause wrong state
aoqi@0 910 x->set_needs_null_check(true);
aoqi@0 911 x->set_explicit_null_check(NULL);
aoqi@0 912 }
aoqi@0 913 clear_last_explicit_null_check();
aoqi@0 914 }
aoqi@0 915
aoqi@0 916
aoqi@0 917 void NullCheckEliminator::handle_ArrayLength(ArrayLength* x) {
aoqi@0 918 Value array = x->array();
aoqi@0 919 if (set_contains(array)) {
aoqi@0 920 // Value is non-null => update AccessArray
aoqi@0 921 if (last_explicit_null_check_obj() == array) {
aoqi@0 922 x->set_explicit_null_check(consume_last_explicit_null_check());
aoqi@0 923 x->set_needs_null_check(true);
aoqi@0 924 if (PrintNullCheckElimination) {
aoqi@0 925 tty->print_cr("Folded NullCheck %d into ArrayLength %d's null check for value %d",
aoqi@0 926 x->explicit_null_check()->id(), x->id(), array->id());
aoqi@0 927 }
aoqi@0 928 } else {
aoqi@0 929 x->set_explicit_null_check(NULL);
aoqi@0 930 x->set_needs_null_check(false);
aoqi@0 931 if (PrintNullCheckElimination) {
aoqi@0 932 tty->print_cr("Eliminated ArrayLength %d's null check for value %d", x->id(), array->id());
aoqi@0 933 }
aoqi@0 934 }
aoqi@0 935 } else {
aoqi@0 936 set_put(array);
aoqi@0 937 if (PrintNullCheckElimination) {
aoqi@0 938 tty->print_cr("ArrayLength %d of value %d proves value to be non-null", x->id(), array->id());
aoqi@0 939 }
aoqi@0 940 // Ensure previous passes do not cause wrong state
aoqi@0 941 x->set_needs_null_check(true);
aoqi@0 942 x->set_explicit_null_check(NULL);
aoqi@0 943 }
aoqi@0 944 clear_last_explicit_null_check();
aoqi@0 945 }
aoqi@0 946
aoqi@0 947
aoqi@0 948 void NullCheckEliminator::handle_LoadIndexed(LoadIndexed* x) {
aoqi@0 949 Value array = x->array();
aoqi@0 950 if (set_contains(array)) {
aoqi@0 951 // Value is non-null => update AccessArray
aoqi@0 952 if (last_explicit_null_check_obj() == array) {
aoqi@0 953 x->set_explicit_null_check(consume_last_explicit_null_check());
aoqi@0 954 x->set_needs_null_check(true);
aoqi@0 955 if (PrintNullCheckElimination) {
aoqi@0 956 tty->print_cr("Folded NullCheck %d into LoadIndexed %d's null check for value %d",
aoqi@0 957 x->explicit_null_check()->id(), x->id(), array->id());
aoqi@0 958 }
aoqi@0 959 } else {
aoqi@0 960 x->set_explicit_null_check(NULL);
aoqi@0 961 x->set_needs_null_check(false);
aoqi@0 962 if (PrintNullCheckElimination) {
aoqi@0 963 tty->print_cr("Eliminated LoadIndexed %d's null check for value %d", x->id(), array->id());
aoqi@0 964 }
aoqi@0 965 }
aoqi@0 966 } else {
aoqi@0 967 set_put(array);
aoqi@0 968 if (PrintNullCheckElimination) {
aoqi@0 969 tty->print_cr("LoadIndexed %d of value %d proves value to be non-null", x->id(), array->id());
aoqi@0 970 }
aoqi@0 971 // Ensure previous passes do not cause wrong state
aoqi@0 972 x->set_needs_null_check(true);
aoqi@0 973 x->set_explicit_null_check(NULL);
aoqi@0 974 }
aoqi@0 975 clear_last_explicit_null_check();
aoqi@0 976 }
aoqi@0 977
aoqi@0 978
aoqi@0 979 void NullCheckEliminator::handle_StoreIndexed(StoreIndexed* x) {
aoqi@0 980 Value array = x->array();
aoqi@0 981 if (set_contains(array)) {
aoqi@0 982 // Value is non-null => update AccessArray
aoqi@0 983 if (PrintNullCheckElimination) {
aoqi@0 984 tty->print_cr("Eliminated StoreIndexed %d's null check for value %d", x->id(), array->id());
aoqi@0 985 }
aoqi@0 986 x->set_needs_null_check(false);
aoqi@0 987 } else {
aoqi@0 988 set_put(array);
aoqi@0 989 if (PrintNullCheckElimination) {
aoqi@0 990 tty->print_cr("StoreIndexed %d of value %d proves value to be non-null", x->id(), array->id());
aoqi@0 991 }
aoqi@0 992 // Ensure previous passes do not cause wrong state
aoqi@0 993 x->set_needs_null_check(true);
aoqi@0 994 }
aoqi@0 995 clear_last_explicit_null_check();
aoqi@0 996 }
aoqi@0 997
aoqi@0 998
aoqi@0 999 void NullCheckEliminator::handle_NullCheck(NullCheck* x) {
aoqi@0 1000 Value obj = x->obj();
aoqi@0 1001 if (set_contains(obj)) {
aoqi@0 1002 // Already proven to be non-null => this NullCheck is useless
aoqi@0 1003 if (PrintNullCheckElimination) {
aoqi@0 1004 tty->print_cr("Eliminated NullCheck %d for value %d", x->id(), obj->id());
aoqi@0 1005 }
aoqi@0 1006 // Don't unpin since that may shrink obj's live range and make it unavailable for debug info.
aoqi@0 1007 // The code generator won't emit LIR for a NullCheck that cannot trap.
aoqi@0 1008 x->set_can_trap(false);
aoqi@0 1009 } else {
aoqi@0 1010 // May be null => add to map and set last explicit NullCheck
aoqi@0 1011 x->set_can_trap(true);
aoqi@0 1012 // make sure it's pinned if it can trap
aoqi@0 1013 x->pin(Instruction::PinExplicitNullCheck);
aoqi@0 1014 set_put(obj);
aoqi@0 1015 set_last_explicit_null_check(x);
aoqi@0 1016 if (PrintNullCheckElimination) {
aoqi@0 1017 tty->print_cr("NullCheck %d of value %d proves value to be non-null", x->id(), obj->id());
aoqi@0 1018 }
aoqi@0 1019 }
aoqi@0 1020 }
aoqi@0 1021
aoqi@0 1022
aoqi@0 1023 void NullCheckEliminator::handle_Invoke(Invoke* x) {
aoqi@0 1024 if (!x->has_receiver()) {
aoqi@0 1025 // Be conservative
aoqi@0 1026 clear_last_explicit_null_check();
aoqi@0 1027 return;
aoqi@0 1028 }
aoqi@0 1029
aoqi@0 1030 Value recv = x->receiver();
aoqi@0 1031 if (!set_contains(recv)) {
aoqi@0 1032 set_put(recv);
aoqi@0 1033 if (PrintNullCheckElimination) {
aoqi@0 1034 tty->print_cr("Invoke %d of value %d proves value to be non-null", x->id(), recv->id());
aoqi@0 1035 }
aoqi@0 1036 }
aoqi@0 1037 clear_last_explicit_null_check();
aoqi@0 1038 }
aoqi@0 1039
aoqi@0 1040
aoqi@0 1041 void NullCheckEliminator::handle_NewInstance(NewInstance* x) {
aoqi@0 1042 set_put(x);
aoqi@0 1043 if (PrintNullCheckElimination) {
aoqi@0 1044 tty->print_cr("NewInstance %d is non-null", x->id());
aoqi@0 1045 }
aoqi@0 1046 }
aoqi@0 1047
aoqi@0 1048
aoqi@0 1049 void NullCheckEliminator::handle_NewArray(NewArray* x) {
aoqi@0 1050 set_put(x);
aoqi@0 1051 if (PrintNullCheckElimination) {
aoqi@0 1052 tty->print_cr("NewArray %d is non-null", x->id());
aoqi@0 1053 }
aoqi@0 1054 }
aoqi@0 1055
aoqi@0 1056
aoqi@0 1057 void NullCheckEliminator::handle_ExceptionObject(ExceptionObject* x) {
aoqi@0 1058 set_put(x);
aoqi@0 1059 if (PrintNullCheckElimination) {
aoqi@0 1060 tty->print_cr("ExceptionObject %d is non-null", x->id());
aoqi@0 1061 }
aoqi@0 1062 }
aoqi@0 1063
aoqi@0 1064
aoqi@0 1065 void NullCheckEliminator::handle_AccessMonitor(AccessMonitor* x) {
aoqi@0 1066 Value obj = x->obj();
aoqi@0 1067 if (set_contains(obj)) {
aoqi@0 1068 // Value is non-null => update AccessMonitor
aoqi@0 1069 if (PrintNullCheckElimination) {
aoqi@0 1070 tty->print_cr("Eliminated AccessMonitor %d's null check for value %d", x->id(), obj->id());
aoqi@0 1071 }
aoqi@0 1072 x->set_needs_null_check(false);
aoqi@0 1073 } else {
aoqi@0 1074 set_put(obj);
aoqi@0 1075 if (PrintNullCheckElimination) {
aoqi@0 1076 tty->print_cr("AccessMonitor %d of value %d proves value to be non-null", x->id(), obj->id());
aoqi@0 1077 }
aoqi@0 1078 // Ensure previous passes do not cause wrong state
aoqi@0 1079 x->set_needs_null_check(true);
aoqi@0 1080 }
aoqi@0 1081 clear_last_explicit_null_check();
aoqi@0 1082 }
aoqi@0 1083
aoqi@0 1084
aoqi@0 1085 void NullCheckEliminator::handle_Intrinsic(Intrinsic* x) {
aoqi@0 1086 if (!x->has_receiver()) {
aoqi@0 1087 if (x->id() == vmIntrinsics::_arraycopy) {
aoqi@0 1088 for (int i = 0; i < x->number_of_arguments(); i++) {
aoqi@0 1089 x->set_arg_needs_null_check(i, !set_contains(x->argument_at(i)));
aoqi@0 1090 }
aoqi@0 1091 }
aoqi@0 1092
aoqi@0 1093 // Be conservative
aoqi@0 1094 clear_last_explicit_null_check();
aoqi@0 1095 return;
aoqi@0 1096 }
aoqi@0 1097
aoqi@0 1098 Value recv = x->receiver();
aoqi@0 1099 if (set_contains(recv)) {
aoqi@0 1100 // Value is non-null => update Intrinsic
aoqi@0 1101 if (PrintNullCheckElimination) {
aoqi@0 1102 tty->print_cr("Eliminated Intrinsic %d's null check for value %d", x->id(), recv->id());
aoqi@0 1103 }
aoqi@0 1104 x->set_needs_null_check(false);
aoqi@0 1105 } else {
aoqi@0 1106 set_put(recv);
aoqi@0 1107 if (PrintNullCheckElimination) {
aoqi@0 1108 tty->print_cr("Intrinsic %d of value %d proves value to be non-null", x->id(), recv->id());
aoqi@0 1109 }
aoqi@0 1110 // Ensure previous passes do not cause wrong state
aoqi@0 1111 x->set_needs_null_check(true);
aoqi@0 1112 }
aoqi@0 1113 clear_last_explicit_null_check();
aoqi@0 1114 }
aoqi@0 1115
aoqi@0 1116
aoqi@0 1117 void NullCheckEliminator::handle_Phi(Phi* x) {
aoqi@0 1118 int i;
aoqi@0 1119 bool all_non_null = true;
aoqi@0 1120 if (x->is_illegal()) {
aoqi@0 1121 all_non_null = false;
aoqi@0 1122 } else {
aoqi@0 1123 for (i = 0; i < x->operand_count(); i++) {
aoqi@0 1124 Value input = x->operand_at(i);
aoqi@0 1125 if (!set_contains(input)) {
aoqi@0 1126 all_non_null = false;
aoqi@0 1127 }
aoqi@0 1128 }
aoqi@0 1129 }
aoqi@0 1130
aoqi@0 1131 if (all_non_null) {
aoqi@0 1132 // Value is non-null => update Phi
aoqi@0 1133 if (PrintNullCheckElimination) {
aoqi@0 1134 tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
aoqi@0 1135 }
aoqi@0 1136 x->set_needs_null_check(false);
aoqi@0 1137 } else if (set_contains(x)) {
aoqi@0 1138 set_remove(x);
aoqi@0 1139 }
aoqi@0 1140 }
aoqi@0 1141
aoqi@0 1142 void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
aoqi@0 1143 for (int i = 0; i < x->nb_profiled_args(); i++) {
aoqi@0 1144 x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
aoqi@0 1145 }
aoqi@0 1146 }
aoqi@0 1147
aoqi@0 1148 void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) {
aoqi@0 1149 x->set_needs_null_check(!set_contains(x->ret()));
aoqi@0 1150 }
aoqi@0 1151
aoqi@0 1152 void Optimizer::eliminate_null_checks() {
aoqi@0 1153 ResourceMark rm;
aoqi@0 1154
aoqi@0 1155 NullCheckEliminator nce(this);
aoqi@0 1156
aoqi@0 1157 if (PrintNullCheckElimination) {
aoqi@0 1158 tty->print_cr("Starting null check elimination for method %s::%s%s",
aoqi@0 1159 ir()->method()->holder()->name()->as_utf8(),
aoqi@0 1160 ir()->method()->name()->as_utf8(),
aoqi@0 1161 ir()->method()->signature()->as_symbol()->as_utf8());
aoqi@0 1162 }
aoqi@0 1163
aoqi@0 1164 // Apply to graph
aoqi@0 1165 nce.iterate(ir()->start());
aoqi@0 1166
aoqi@0 1167 // walk over the graph looking for exception
aoqi@0 1168 // handlers and iterate over them as well
aoqi@0 1169 int nblocks = BlockBegin::number_of_blocks();
aoqi@0 1170 BlockList blocks(nblocks);
aoqi@0 1171 boolArray visited_block(nblocks, false);
aoqi@0 1172
aoqi@0 1173 blocks.push(ir()->start());
aoqi@0 1174 visited_block[ir()->start()->block_id()] = true;
aoqi@0 1175 for (int i = 0; i < blocks.length(); i++) {
aoqi@0 1176 BlockBegin* b = blocks[i];
aoqi@0 1177 // exception handlers need to be treated as additional roots
aoqi@0 1178 for (int e = b->number_of_exception_handlers(); e-- > 0; ) {
aoqi@0 1179 BlockBegin* excp = b->exception_handler_at(e);
aoqi@0 1180 int id = excp->block_id();
aoqi@0 1181 if (!visited_block[id]) {
aoqi@0 1182 blocks.push(excp);
aoqi@0 1183 visited_block[id] = true;
aoqi@0 1184 nce.iterate(excp);
aoqi@0 1185 }
aoqi@0 1186 }
aoqi@0 1187 // traverse successors
aoqi@0 1188 BlockEnd *end = b->end();
aoqi@0 1189 for (int s = end->number_of_sux(); s-- > 0; ) {
aoqi@0 1190 BlockBegin* next = end->sux_at(s);
aoqi@0 1191 int id = next->block_id();
aoqi@0 1192 if (!visited_block[id]) {
aoqi@0 1193 blocks.push(next);
aoqi@0 1194 visited_block[id] = true;
aoqi@0 1195 }
aoqi@0 1196 }
aoqi@0 1197 }
aoqi@0 1198
aoqi@0 1199
aoqi@0 1200 if (PrintNullCheckElimination) {
aoqi@0 1201 tty->print_cr("Done with null check elimination for method %s::%s%s",
aoqi@0 1202 ir()->method()->holder()->name()->as_utf8(),
aoqi@0 1203 ir()->method()->name()->as_utf8(),
aoqi@0 1204 ir()->method()->signature()->as_symbol()->as_utf8());
aoqi@0 1205 }
aoqi@0 1206 }

mercurial