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