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