69 void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) {
70 int e = sux->number_of_exception_handlers();
71 for (int i = 0; i < e; i++) {
72 BlockBegin* xhandler = sux->exception_handler_at(i);
73 block->add_exception_handler(xhandler);
74
75 assert(xhandler->is_predecessor(sux), "missing predecessor");
76 if (sux->number_of_preds() == 0) {
77 // sux is disconnected from graph so disconnect from exception handlers
78 xhandler->remove_predecessor(sux);
79 }
80 if (!xhandler->is_predecessor(block)) {
81 xhandler->add_predecessor(block);
82 }
83 }
84 }
85
86 virtual void block_do(BlockBegin* block);
87
88 private:
89 Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval);
90 };
91
92 void CE_Eliminator::block_do(BlockBegin* block) {
93 // 1) find conditional expression
94 // check if block ends with an If
95 If* if_ = block->end()->as_If();
96 if (if_ == nullptr) return;
97
98 // check if If works on int or object types
99 // (we cannot handle If's working on long, float or doubles yet,
100 // since IfOp doesn't support them - these If's show up if cmp
101 // operations followed by If's are eliminated)
102 ValueType* if_type = if_->x()->type();
103 if (!if_type->is_int() && !if_type->is_object()) return;
104
105 BlockBegin* t_block = if_->tsux();
106 BlockBegin* f_block = if_->fsux();
107 Instruction* t_cur = t_block->next();
108 Instruction* f_cur = f_block->next();
109
198
199 // 2) substitute conditional expression
200 // with an IfOp followed by a Goto
201 // cut if_ away and get node before
202 Instruction* cur_end = if_->prev();
203
204 // append constants of true- and false-block if necessary
205 // clone constants because original block must not be destroyed
206 assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
207 if (t_value == t_const) {
208 t_value = new Constant(t_const->type());
209 NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
210 cur_end = cur_end->set_next(t_value);
211 }
212 if (f_value == f_const) {
213 f_value = new Constant(f_const->type());
214 NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
215 cur_end = cur_end->set_next(f_value);
216 }
217
218 Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value);
219 assert(result != nullptr, "make_ifop must return a non-null instruction");
220 if (!result->is_linked() && result->can_be_linked()) {
221 NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
222 cur_end = cur_end->set_next(result);
223 }
224
225 // append Goto to successor
226 ValueStack* state_before = if_->state_before();
227 Goto* goto_ = new Goto(sux, state_before, is_safepoint);
228
229 // prepare state for Goto
230 ValueStack* goto_state = if_state;
231 goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
232 goto_state->push(result->type(), result);
233 assert(goto_state->is_same(sux_state), "states must match now");
234 goto_->set_state(goto_state);
235
236 cur_end = cur_end->set_next(goto_, goto_state->bci());
237
238 // Adjust control flow graph
253 // substitute the phi if possible
254 if (sux_phi->as_Phi()->operand_count() == 1) {
255 assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
256 sux_phi->set_subst(result);
257 _has_substitution = true;
258 }
259
260 // 3) successfully eliminated a conditional expression
261 _cee_count++;
262 if (PrintCEE) {
263 tty->print_cr("%d. CEE in B%d (B%d B%d)", cee_count(), block->block_id(), t_block->block_id(), f_block->block_id());
264 tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
265 }
266
267 #ifdef DO_DELAYED_VERIFICATION
268 _hir->verify_local(blocks_to_verify_later);
269 #endif // DO_DELAYED_VERIFICATION
270
271 }
272
273 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval) {
274 if (!OptimizeIfOps) {
275 return new IfOp(x, cond, y, tval, fval);
276 }
277
278 tval = tval->subst();
279 fval = fval->subst();
280 if (tval == fval) {
281 _ifop_count++;
282 return tval;
283 }
284
285 x = x->subst();
286 y = y->subst();
287
288 Constant* y_const = y->as_Constant();
289 if (y_const != nullptr) {
290 IfOp* x_ifop = x->as_IfOp();
291 if (x_ifop != nullptr) { // x is an ifop, y is a constant
292 Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
293 Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
294
295 if (x_tval_const != nullptr && x_fval_const != nullptr) {
296 Instruction::Condition x_ifop_cond = x_ifop->cond();
297
298 Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
299 Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
300
301 // not_comparable here is a valid return in case we're comparing unloaded oop constants
302 if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
303 Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
304 Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
305
306 _ifop_count++;
307 if (new_tval == new_fval) {
308 return new_tval;
309 } else {
310 return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval);
311 }
312 }
313 }
314 } else {
315 Constant* x_const = x->as_Constant();
316 if (x_const != nullptr) { // x and y are constants
317 Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
318 // not_comparable here is a valid return in case we're comparing unloaded oop constants
319 if (x_compare_res != Constant::not_comparable) {
320 _ifop_count++;
321 return x_compare_res == Constant::cond_true ? tval : fval;
322 }
323 }
324 }
325 }
326 return new IfOp(x, cond, y, tval, fval);
327 }
328
329 void Optimizer::eliminate_conditional_expressions() {
330 // find conditional expressions & replace them with IfOps
331 CE_Eliminator ce(ir());
332 }
333
334 // This removes others' relation to block, but doesn't empty block's lists
335 static void disconnect_from_graph(BlockBegin* block) {
336 for (int p = 0; p < block->number_of_preds(); p++) {
337 BlockBegin* pred = block->pred_at(p);
338 int idx;
339 while ((idx = pred->end()->find_sux(block)) >= 0) {
340 pred->end()->remove_sux_at(idx);
341 }
342 }
343 for (int s = 0; s < block->number_of_sux(); s++) {
344 block->sux_at(s)->remove_predecessor(block);
345 }
346 }
446 _merge_count++;
447 if (PrintBlockElimination) {
448 tty->print_cr("%d. merged B%d & B%d (stack size = %d)",
449 _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size());
450 }
451
452 #ifdef DO_DELAYED_VERIFICATION
453 _hir->verify_local(blocks_to_verify_later);
454 #endif // DO_DELAYED_VERIFICATION
455
456 If* if_ = block->end()->as_If();
457 if (if_) {
458 IfOp* ifop = if_->x()->as_IfOp();
459 Constant* con = if_->y()->as_Constant();
460 bool swapped = false;
461 if (!con || !ifop) {
462 ifop = if_->y()->as_IfOp();
463 con = if_->x()->as_Constant();
464 swapped = true;
465 }
466 if (con && ifop) {
467 Constant* tval = ifop->tval()->as_Constant();
468 Constant* fval = ifop->fval()->as_Constant();
469 if (tval && fval) {
470 // Find the instruction before if_, starting with ifop.
471 // When if_ and ifop are not in the same block, prev
472 // becomes null In such (rare) cases it is not
473 // profitable to perform the optimization.
474 Value prev = ifop;
475 while (prev != nullptr && prev->next() != if_) {
476 prev = prev->next();
477 }
478
479 if (prev != nullptr) {
480 Instruction::Condition cond = if_->cond();
481 BlockBegin* tsux = if_->tsux();
482 BlockBegin* fsux = if_->fsux();
483 if (swapped) {
484 cond = Instruction::mirror(cond);
485 }
486
487 BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
488 BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
489 if (tblock != fblock && !if_->is_safepoint()) {
490 If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
491 tblock, fblock, if_->state_before(), if_->is_safepoint());
492 newif->set_state(if_->state()->copy());
493
494 assert(prev->next() == if_, "must be guaranteed by above search");
495 NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
496 prev->set_next(newif);
497 block->set_end(newif);
498
499 _merge_count++;
500 if (PrintBlockElimination) {
501 tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
502 }
503
504 #ifdef DO_DELAYED_VERIFICATION
505 _hir->verify_local(blocks_to_verify_later);
506 #endif // DO_DELAYED_VERIFICATION
507 }
508 }
509 }
510 }
511 }
566 void do_InstanceOf (InstanceOf* x);
567 void do_MonitorEnter (MonitorEnter* x);
568 void do_MonitorExit (MonitorExit* x);
569 void do_Intrinsic (Intrinsic* x);
570 void do_BlockBegin (BlockBegin* x);
571 void do_Goto (Goto* x);
572 void do_If (If* x);
573 void do_TableSwitch (TableSwitch* x);
574 void do_LookupSwitch (LookupSwitch* x);
575 void do_Return (Return* x);
576 void do_Throw (Throw* x);
577 void do_Base (Base* x);
578 void do_OsrEntry (OsrEntry* x);
579 void do_ExceptionObject(ExceptionObject* x);
580 void do_RoundFP (RoundFP* x);
581 void do_UnsafeGet (UnsafeGet* x);
582 void do_UnsafePut (UnsafePut* x);
583 void do_UnsafeGetAndSet(UnsafeGetAndSet* x);
584 void do_ProfileCall (ProfileCall* x);
585 void do_ProfileReturnType (ProfileReturnType* x);
586 void do_ProfileInvoke (ProfileInvoke* x);
587 void do_RuntimeCall (RuntimeCall* x);
588 void do_MemBar (MemBar* x);
589 void do_RangeCheckPredicate(RangeCheckPredicate* x);
590 #ifdef ASSERT
591 void do_Assert (Assert* x);
592 #endif
593 };
594
595
596 // Because of a static contained within (for the purpose of iteration
597 // over instructions), it is only valid to have one of these active at
598 // a time
599 class NullCheckEliminator: public ValueVisitor {
600 private:
601 Optimizer* _opt;
602
603 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
604 BlockList* _work_list; // Basic blocks to visit
605
694 // (separated out from NullCheckVisitor for clarity)
695
696 // The basic contract is that these must leave the instruction in
697 // the desired state; must not assume anything about the state of
698 // the instruction. We make multiple passes over some basic blocks
699 // and the last pass is the only one whose result is valid.
700 void handle_AccessField (AccessField* x);
701 void handle_ArrayLength (ArrayLength* x);
702 void handle_LoadIndexed (LoadIndexed* x);
703 void handle_StoreIndexed (StoreIndexed* x);
704 void handle_NullCheck (NullCheck* x);
705 void handle_Invoke (Invoke* x);
706 void handle_NewInstance (NewInstance* x);
707 void handle_NewArray (NewArray* x);
708 void handle_AccessMonitor (AccessMonitor* x);
709 void handle_Intrinsic (Intrinsic* x);
710 void handle_ExceptionObject (ExceptionObject* x);
711 void handle_Phi (Phi* x);
712 void handle_ProfileCall (ProfileCall* x);
713 void handle_ProfileReturnType (ProfileReturnType* x);
714 void handle_Constant (Constant* x);
715 void handle_IfOp (IfOp* x);
716 };
717
718
719 // NEEDS_CLEANUP
720 // There may be other instructions which need to clear the last
721 // explicit null check. Anything across which we can not hoist the
722 // debug information for a NullCheck instruction must clear it. It
723 // might be safer to pattern match "NullCheck ; {AccessField,
724 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
725 // Should test to see performance hit of clearing it for all handlers
726 // with empty bodies below. If it is negligible then we should leave
727 // that in for safety, otherwise should think more about it.
728 void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); }
729 void NullCheckVisitor::do_Local (Local* x) {}
730 void NullCheckVisitor::do_Constant (Constant* x) { nce()->handle_Constant(x); }
731 void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); }
732 void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); }
733 void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); }
753 void NullCheckVisitor::do_MonitorExit (MonitorExit* x) { nce()->handle_AccessMonitor(x); }
754 void NullCheckVisitor::do_Intrinsic (Intrinsic* x) { nce()->handle_Intrinsic(x); }
755 void NullCheckVisitor::do_BlockBegin (BlockBegin* x) {}
756 void NullCheckVisitor::do_Goto (Goto* x) {}
757 void NullCheckVisitor::do_If (If* x) {}
758 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
759 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
760 void NullCheckVisitor::do_Return (Return* x) {}
761 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
762 void NullCheckVisitor::do_Base (Base* x) {}
763 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
764 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
765 void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
766 void NullCheckVisitor::do_UnsafeGet (UnsafeGet* x) {}
767 void NullCheckVisitor::do_UnsafePut (UnsafePut* x) {}
768 void NullCheckVisitor::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {}
769 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check();
770 nce()->handle_ProfileCall(x); }
771 void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); }
772 void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {}
773 void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {}
774 void NullCheckVisitor::do_MemBar (MemBar* x) {}
775 void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
776 #ifdef ASSERT
777 void NullCheckVisitor::do_Assert (Assert* x) {}
778 #endif
779
780 void NullCheckEliminator::visit(Value* p) {
781 assert(*p != nullptr, "should not find null instructions");
782 if (visitable(*p)) {
783 mark_visited(*p);
784 (*p)->visit(&_visitor);
785 }
786 }
787
788 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
789 ValueSet* state = state_for(block);
790 if (state == nullptr) {
791 state = incoming_state->copy();
792 set_state_for(block, state);
1182 // Value is non-null => update Phi
1183 if (PrintNullCheckElimination) {
1184 tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1185 }
1186 x->set_needs_null_check(false);
1187 } else if (set_contains(x)) {
1188 set_remove(x);
1189 }
1190 }
1191
1192 void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
1193 for (int i = 0; i < x->nb_profiled_args(); i++) {
1194 x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
1195 }
1196 }
1197
1198 void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) {
1199 x->set_needs_null_check(!set_contains(x->ret()));
1200 }
1201
1202 void NullCheckEliminator::handle_Constant(Constant *x) {
1203 ObjectType* ot = x->type()->as_ObjectType();
1204 if (ot != nullptr && ot->is_loaded()) {
1205 ObjectConstant* oc = ot->as_ObjectConstant();
1206 if (oc == nullptr || !oc->value()->is_null_object()) {
1207 set_put(x);
1208 if (PrintNullCheckElimination) {
1209 tty->print_cr("Constant %d is non-null", x->id());
1210 }
1211 }
1212 }
1213 }
1214
1215 void NullCheckEliminator::handle_IfOp(IfOp *x) {
1216 if (x->type()->is_object() && set_contains(x->tval()) && set_contains(x->fval())) {
1217 set_put(x);
1218 if (PrintNullCheckElimination) {
1219 tty->print_cr("IfOp %d is non-null", x->id());
1220 }
1221 }
|
69 void adjust_exception_edges(BlockBegin* block, BlockBegin* sux) {
70 int e = sux->number_of_exception_handlers();
71 for (int i = 0; i < e; i++) {
72 BlockBegin* xhandler = sux->exception_handler_at(i);
73 block->add_exception_handler(xhandler);
74
75 assert(xhandler->is_predecessor(sux), "missing predecessor");
76 if (sux->number_of_preds() == 0) {
77 // sux is disconnected from graph so disconnect from exception handlers
78 xhandler->remove_predecessor(sux);
79 }
80 if (!xhandler->is_predecessor(block)) {
81 xhandler->add_predecessor(block);
82 }
83 }
84 }
85
86 virtual void block_do(BlockBegin* block);
87
88 private:
89 Value make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval,
90 ValueStack* state_before, bool substitutability_check);
91 };
92
93 void CE_Eliminator::block_do(BlockBegin* block) {
94 // 1) find conditional expression
95 // check if block ends with an If
96 If* if_ = block->end()->as_If();
97 if (if_ == nullptr) return;
98
99 // check if If works on int or object types
100 // (we cannot handle If's working on long, float or doubles yet,
101 // since IfOp doesn't support them - these If's show up if cmp
102 // operations followed by If's are eliminated)
103 ValueType* if_type = if_->x()->type();
104 if (!if_type->is_int() && !if_type->is_object()) return;
105
106 BlockBegin* t_block = if_->tsux();
107 BlockBegin* f_block = if_->fsux();
108 Instruction* t_cur = t_block->next();
109 Instruction* f_cur = f_block->next();
110
199
200 // 2) substitute conditional expression
201 // with an IfOp followed by a Goto
202 // cut if_ away and get node before
203 Instruction* cur_end = if_->prev();
204
205 // append constants of true- and false-block if necessary
206 // clone constants because original block must not be destroyed
207 assert((t_value != f_const && f_value != t_const) || t_const == f_const, "mismatch");
208 if (t_value == t_const) {
209 t_value = new Constant(t_const->type());
210 NOT_PRODUCT(t_value->set_printable_bci(if_->printable_bci()));
211 cur_end = cur_end->set_next(t_value);
212 }
213 if (f_value == f_const) {
214 f_value = new Constant(f_const->type());
215 NOT_PRODUCT(f_value->set_printable_bci(if_->printable_bci()));
216 cur_end = cur_end->set_next(f_value);
217 }
218
219 Value result = make_ifop(if_->x(), if_->cond(), if_->y(), t_value, f_value,
220 if_->state_before(), if_->substitutability_check());
221 assert(result != nullptr, "make_ifop must return a non-null instruction");
222 if (!result->is_linked() && result->can_be_linked()) {
223 NOT_PRODUCT(result->set_printable_bci(if_->printable_bci()));
224 cur_end = cur_end->set_next(result);
225 }
226
227 // append Goto to successor
228 ValueStack* state_before = if_->state_before();
229 Goto* goto_ = new Goto(sux, state_before, is_safepoint);
230
231 // prepare state for Goto
232 ValueStack* goto_state = if_state;
233 goto_state = goto_state->copy(ValueStack::StateAfter, goto_state->bci());
234 goto_state->push(result->type(), result);
235 assert(goto_state->is_same(sux_state), "states must match now");
236 goto_->set_state(goto_state);
237
238 cur_end = cur_end->set_next(goto_, goto_state->bci());
239
240 // Adjust control flow graph
255 // substitute the phi if possible
256 if (sux_phi->as_Phi()->operand_count() == 1) {
257 assert(sux_phi->as_Phi()->operand_at(0) == result, "screwed up phi");
258 sux_phi->set_subst(result);
259 _has_substitution = true;
260 }
261
262 // 3) successfully eliminated a conditional expression
263 _cee_count++;
264 if (PrintCEE) {
265 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());
266 tty->print_cr("%d. IfOp in B%d", ifop_count(), block->block_id());
267 }
268
269 #ifdef DO_DELAYED_VERIFICATION
270 _hir->verify_local(blocks_to_verify_later);
271 #endif // DO_DELAYED_VERIFICATION
272
273 }
274
275 Value CE_Eliminator::make_ifop(Value x, Instruction::Condition cond, Value y, Value tval, Value fval,
276 ValueStack* state_before, bool substitutability_check) {
277 if (!OptimizeIfOps) {
278 return new IfOp(x, cond, y, tval, fval, state_before, substitutability_check);
279 }
280
281 tval = tval->subst();
282 fval = fval->subst();
283 if (tval == fval) {
284 _ifop_count++;
285 return tval;
286 }
287
288 x = x->subst();
289 y = y->subst();
290
291 Constant* y_const = y->as_Constant();
292 if (y_const != nullptr) {
293 IfOp* x_ifop = x->as_IfOp();
294 if (x_ifop != nullptr) { // x is an ifop, y is a constant
295 Constant* x_tval_const = x_ifop->tval()->subst()->as_Constant();
296 Constant* x_fval_const = x_ifop->fval()->subst()->as_Constant();
297
298 if (x_tval_const != nullptr && x_fval_const != nullptr) {
299 Instruction::Condition x_ifop_cond = x_ifop->cond();
300
301 Constant::CompareResult t_compare_res = x_tval_const->compare(cond, y_const);
302 Constant::CompareResult f_compare_res = x_fval_const->compare(cond, y_const);
303
304 // not_comparable here is a valid return in case we're comparing unloaded oop constants
305 if (t_compare_res != Constant::not_comparable && f_compare_res != Constant::not_comparable) {
306 Value new_tval = t_compare_res == Constant::cond_true ? tval : fval;
307 Value new_fval = f_compare_res == Constant::cond_true ? tval : fval;
308
309 _ifop_count++;
310 if (new_tval == new_fval) {
311 return new_tval;
312 } else {
313 return new IfOp(x_ifop->x(), x_ifop_cond, x_ifop->y(), new_tval, new_fval, state_before, substitutability_check);
314 }
315 }
316 }
317 } else {
318 Constant* x_const = x->as_Constant();
319 if (x_const != nullptr) { // x and y are constants
320 Constant::CompareResult x_compare_res = x_const->compare(cond, y_const);
321 // not_comparable here is a valid return in case we're comparing unloaded oop constants
322 if (x_compare_res != Constant::not_comparable) {
323 _ifop_count++;
324 return x_compare_res == Constant::cond_true ? tval : fval;
325 }
326 }
327 }
328 }
329 return new IfOp(x, cond, y, tval, fval, state_before, substitutability_check);
330 }
331
332 void Optimizer::eliminate_conditional_expressions() {
333 // find conditional expressions & replace them with IfOps
334 CE_Eliminator ce(ir());
335 }
336
337 // This removes others' relation to block, but doesn't empty block's lists
338 static void disconnect_from_graph(BlockBegin* block) {
339 for (int p = 0; p < block->number_of_preds(); p++) {
340 BlockBegin* pred = block->pred_at(p);
341 int idx;
342 while ((idx = pred->end()->find_sux(block)) >= 0) {
343 pred->end()->remove_sux_at(idx);
344 }
345 }
346 for (int s = 0; s < block->number_of_sux(); s++) {
347 block->sux_at(s)->remove_predecessor(block);
348 }
349 }
449 _merge_count++;
450 if (PrintBlockElimination) {
451 tty->print_cr("%d. merged B%d & B%d (stack size = %d)",
452 _merge_count, block->block_id(), sux->block_id(), sux->state()->stack_size());
453 }
454
455 #ifdef DO_DELAYED_VERIFICATION
456 _hir->verify_local(blocks_to_verify_later);
457 #endif // DO_DELAYED_VERIFICATION
458
459 If* if_ = block->end()->as_If();
460 if (if_) {
461 IfOp* ifop = if_->x()->as_IfOp();
462 Constant* con = if_->y()->as_Constant();
463 bool swapped = false;
464 if (!con || !ifop) {
465 ifop = if_->y()->as_IfOp();
466 con = if_->x()->as_Constant();
467 swapped = true;
468 }
469 if (con && ifop && !ifop->substitutability_check()) {
470 Constant* tval = ifop->tval()->as_Constant();
471 Constant* fval = ifop->fval()->as_Constant();
472 if (tval && fval) {
473 // Find the instruction before if_, starting with ifop.
474 // When if_ and ifop are not in the same block, prev
475 // becomes null In such (rare) cases it is not
476 // profitable to perform the optimization.
477 Value prev = ifop;
478 while (prev != nullptr && prev->next() != if_) {
479 prev = prev->next();
480 }
481
482 if (prev != nullptr) {
483 Instruction::Condition cond = if_->cond();
484 BlockBegin* tsux = if_->tsux();
485 BlockBegin* fsux = if_->fsux();
486 if (swapped) {
487 cond = Instruction::mirror(cond);
488 }
489
490 BlockBegin* tblock = tval->compare(cond, con, tsux, fsux);
491 BlockBegin* fblock = fval->compare(cond, con, tsux, fsux);
492 if (tblock != fblock && !if_->is_safepoint()) {
493 If* newif = new If(ifop->x(), ifop->cond(), false, ifop->y(),
494 tblock, fblock, if_->state_before(), if_->is_safepoint(), ifop->substitutability_check());
495 newif->set_state(if_->state()->copy());
496
497 assert(prev->next() == if_, "must be guaranteed by above search");
498 NOT_PRODUCT(newif->set_printable_bci(if_->printable_bci()));
499 prev->set_next(newif);
500 block->set_end(newif);
501
502 _merge_count++;
503 if (PrintBlockElimination) {
504 tty->print_cr("%d. replaced If and IfOp at end of B%d with single If", _merge_count, block->block_id());
505 }
506
507 #ifdef DO_DELAYED_VERIFICATION
508 _hir->verify_local(blocks_to_verify_later);
509 #endif // DO_DELAYED_VERIFICATION
510 }
511 }
512 }
513 }
514 }
569 void do_InstanceOf (InstanceOf* x);
570 void do_MonitorEnter (MonitorEnter* x);
571 void do_MonitorExit (MonitorExit* x);
572 void do_Intrinsic (Intrinsic* x);
573 void do_BlockBegin (BlockBegin* x);
574 void do_Goto (Goto* x);
575 void do_If (If* x);
576 void do_TableSwitch (TableSwitch* x);
577 void do_LookupSwitch (LookupSwitch* x);
578 void do_Return (Return* x);
579 void do_Throw (Throw* x);
580 void do_Base (Base* x);
581 void do_OsrEntry (OsrEntry* x);
582 void do_ExceptionObject(ExceptionObject* x);
583 void do_RoundFP (RoundFP* x);
584 void do_UnsafeGet (UnsafeGet* x);
585 void do_UnsafePut (UnsafePut* x);
586 void do_UnsafeGetAndSet(UnsafeGetAndSet* x);
587 void do_ProfileCall (ProfileCall* x);
588 void do_ProfileReturnType (ProfileReturnType* x);
589 void do_ProfileACmpTypes(ProfileACmpTypes* x);
590 void do_ProfileInvoke (ProfileInvoke* x);
591 void do_RuntimeCall (RuntimeCall* x);
592 void do_MemBar (MemBar* x);
593 void do_RangeCheckPredicate(RangeCheckPredicate* x);
594 #ifdef ASSERT
595 void do_Assert (Assert* x);
596 #endif
597 };
598
599
600 // Because of a static contained within (for the purpose of iteration
601 // over instructions), it is only valid to have one of these active at
602 // a time
603 class NullCheckEliminator: public ValueVisitor {
604 private:
605 Optimizer* _opt;
606
607 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
608 BlockList* _work_list; // Basic blocks to visit
609
698 // (separated out from NullCheckVisitor for clarity)
699
700 // The basic contract is that these must leave the instruction in
701 // the desired state; must not assume anything about the state of
702 // the instruction. We make multiple passes over some basic blocks
703 // and the last pass is the only one whose result is valid.
704 void handle_AccessField (AccessField* x);
705 void handle_ArrayLength (ArrayLength* x);
706 void handle_LoadIndexed (LoadIndexed* x);
707 void handle_StoreIndexed (StoreIndexed* x);
708 void handle_NullCheck (NullCheck* x);
709 void handle_Invoke (Invoke* x);
710 void handle_NewInstance (NewInstance* x);
711 void handle_NewArray (NewArray* x);
712 void handle_AccessMonitor (AccessMonitor* x);
713 void handle_Intrinsic (Intrinsic* x);
714 void handle_ExceptionObject (ExceptionObject* x);
715 void handle_Phi (Phi* x);
716 void handle_ProfileCall (ProfileCall* x);
717 void handle_ProfileReturnType (ProfileReturnType* x);
718 void handle_ProfileACmpTypes(ProfileACmpTypes* x);
719 void handle_Constant (Constant* x);
720 void handle_IfOp (IfOp* x);
721 };
722
723
724 // NEEDS_CLEANUP
725 // There may be other instructions which need to clear the last
726 // explicit null check. Anything across which we can not hoist the
727 // debug information for a NullCheck instruction must clear it. It
728 // might be safer to pattern match "NullCheck ; {AccessField,
729 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
730 // Should test to see performance hit of clearing it for all handlers
731 // with empty bodies below. If it is negligible then we should leave
732 // that in for safety, otherwise should think more about it.
733 void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); }
734 void NullCheckVisitor::do_Local (Local* x) {}
735 void NullCheckVisitor::do_Constant (Constant* x) { nce()->handle_Constant(x); }
736 void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); }
737 void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); }
738 void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); }
758 void NullCheckVisitor::do_MonitorExit (MonitorExit* x) { nce()->handle_AccessMonitor(x); }
759 void NullCheckVisitor::do_Intrinsic (Intrinsic* x) { nce()->handle_Intrinsic(x); }
760 void NullCheckVisitor::do_BlockBegin (BlockBegin* x) {}
761 void NullCheckVisitor::do_Goto (Goto* x) {}
762 void NullCheckVisitor::do_If (If* x) {}
763 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
764 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
765 void NullCheckVisitor::do_Return (Return* x) {}
766 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
767 void NullCheckVisitor::do_Base (Base* x) {}
768 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
769 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
770 void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
771 void NullCheckVisitor::do_UnsafeGet (UnsafeGet* x) {}
772 void NullCheckVisitor::do_UnsafePut (UnsafePut* x) {}
773 void NullCheckVisitor::do_UnsafeGetAndSet(UnsafeGetAndSet* x) {}
774 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check();
775 nce()->handle_ProfileCall(x); }
776 void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); }
777 void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {}
778 void NullCheckVisitor::do_ProfileACmpTypes(ProfileACmpTypes* x) { nce()->handle_ProfileACmpTypes(x); }
779 void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {}
780 void NullCheckVisitor::do_MemBar (MemBar* x) {}
781 void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
782 #ifdef ASSERT
783 void NullCheckVisitor::do_Assert (Assert* x) {}
784 #endif
785
786 void NullCheckEliminator::visit(Value* p) {
787 assert(*p != nullptr, "should not find null instructions");
788 if (visitable(*p)) {
789 mark_visited(*p);
790 (*p)->visit(&_visitor);
791 }
792 }
793
794 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
795 ValueSet* state = state_for(block);
796 if (state == nullptr) {
797 state = incoming_state->copy();
798 set_state_for(block, state);
1188 // Value is non-null => update Phi
1189 if (PrintNullCheckElimination) {
1190 tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1191 }
1192 x->set_needs_null_check(false);
1193 } else if (set_contains(x)) {
1194 set_remove(x);
1195 }
1196 }
1197
1198 void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
1199 for (int i = 0; i < x->nb_profiled_args(); i++) {
1200 x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
1201 }
1202 }
1203
1204 void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) {
1205 x->set_needs_null_check(!set_contains(x->ret()));
1206 }
1207
1208 void NullCheckEliminator::handle_ProfileACmpTypes(ProfileACmpTypes* x) {
1209 x->set_left_maybe_null(!set_contains(x->left()));
1210 x->set_right_maybe_null(!set_contains(x->right()));
1211 }
1212
1213 void NullCheckEliminator::handle_Constant(Constant *x) {
1214 ObjectType* ot = x->type()->as_ObjectType();
1215 if (ot != nullptr && ot->is_loaded()) {
1216 ObjectConstant* oc = ot->as_ObjectConstant();
1217 if (oc == nullptr || !oc->value()->is_null_object()) {
1218 set_put(x);
1219 if (PrintNullCheckElimination) {
1220 tty->print_cr("Constant %d is non-null", x->id());
1221 }
1222 }
1223 }
1224 }
1225
1226 void NullCheckEliminator::handle_IfOp(IfOp *x) {
1227 if (x->type()->is_object() && set_contains(x->tval()) && set_contains(x->fval())) {
1228 set_put(x);
1229 if (PrintNullCheckElimination) {
1230 tty->print_cr("IfOp %d is non-null", x->id());
1231 }
1232 }
|