src/share/vm/c1/c1_Optimizer.cpp

Mon, 09 Mar 2009 13:28:46 -0700

author
xdono
date
Mon, 09 Mar 2009 13:28:46 -0700
changeset 1014
0fbdb4381b99
parent 922
80206b8a9128
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6814575: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 03/09
Reviewed-by: katleman, tbell, ohair

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

mercurial