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