src/share/vm/c1/c1_Optimizer.cpp

Tue, 09 Oct 2012 10:09:34 -0700

author
mikael
date
Tue, 09 Oct 2012 10:09:34 -0700
changeset 4153
b9a9ed0f8eeb
parent 4106
7eca5de9e0b6
child 4164
d804e148cff8
permissions
-rw-r--r--

7197424: update copyright year to match last edit in jdk8 hotspot repository
Summary: Update copyright year to 2012 for relevant files
Reviewed-by: dholmes, coleenp

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

mercurial