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