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 #ifdef ASSERT
539 void do_Assert (Assert* x);
540 #endif
541 };
542
543
544 // Because of a static contained within (for the purpose of iteration
545 // over instructions), it is only valid to have one of these active at
546 // a time
547 class NullCheckEliminator: public ValueVisitor {
548 private:
549 Optimizer* _opt;
550
551 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
552 BlockList* _work_list; // Basic blocks to visit
553
641 // Handlers for relevant instructions
642 // (separated out from NullCheckVisitor for clarity)
643
644 // The basic contract is that these must leave the instruction in
645 // the desired state; must not assume anything about the state of
646 // the instruction. We make multiple passes over some basic blocks
647 // and the last pass is the only one whose result is valid.
648 void handle_AccessField (AccessField* x);
649 void handle_ArrayLength (ArrayLength* x);
650 void handle_LoadIndexed (LoadIndexed* x);
651 void handle_StoreIndexed (StoreIndexed* x);
652 void handle_NullCheck (NullCheck* x);
653 void handle_Invoke (Invoke* x);
654 void handle_NewInstance (NewInstance* x);
655 void handle_NewArray (NewArray* x);
656 void handle_AccessMonitor (AccessMonitor* x);
657 void handle_Intrinsic (Intrinsic* x);
658 void handle_ExceptionObject (ExceptionObject* x);
659 void handle_Phi (Phi* x);
660 void handle_ProfileCall (ProfileCall* x);
661 };
662
663
664 // NEEDS_CLEANUP
665 // There may be other instructions which need to clear the last
666 // explicit null check. Anything across which we can not hoist the
667 // debug information for a NullCheck instruction must clear it. It
668 // might be safer to pattern match "NullCheck ; {AccessField,
669 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
670 // Should test to see performance hit of clearing it for all handlers
671 // with empty bodies below. If it is negligible then we should leave
672 // that in for safety, otherwise should think more about it.
673 void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); }
674 void NullCheckVisitor::do_Local (Local* x) {}
675 void NullCheckVisitor::do_Constant (Constant* x) { /* FIXME: handle object constants */ }
676 void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); }
677 void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); }
678 void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); }
679 void NullCheckVisitor::do_LoadIndexed (LoadIndexed* x) { nce()->handle_LoadIndexed(x); }
680 void NullCheckVisitor::do_StoreIndexed (StoreIndexed* x) { nce()->handle_StoreIndexed(x); }
701 void NullCheckVisitor::do_Goto (Goto* x) {}
702 void NullCheckVisitor::do_If (If* x) {}
703 void NullCheckVisitor::do_IfInstanceOf (IfInstanceOf* x) {}
704 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
705 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
706 void NullCheckVisitor::do_Return (Return* x) {}
707 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
708 void NullCheckVisitor::do_Base (Base* x) {}
709 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
710 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
711 void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
712 void NullCheckVisitor::do_UnsafeGetRaw (UnsafeGetRaw* x) {}
713 void NullCheckVisitor::do_UnsafePutRaw (UnsafePutRaw* x) {}
714 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}
715 void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {}
716 void NullCheckVisitor::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {}
717 void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {}
718 void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
719 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check();
720 nce()->handle_ProfileCall(x); }
721 void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {}
722 void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {}
723 void NullCheckVisitor::do_MemBar (MemBar* x) {}
724 void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
725 #ifdef ASSERT
726 void NullCheckVisitor::do_Assert (Assert* x) {}
727 #endif
728
729 void NullCheckEliminator::visit(Value* p) {
730 assert(*p != NULL, "should not find NULL instructions");
731 if (visitable(*p)) {
732 mark_visited(*p);
733 (*p)->visit(&_visitor);
734 }
735 }
736
737 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
738 ValueSet* state = state_for(block);
739 if (state == NULL) {
740 state = incoming_state->copy();
1123 all_non_null = false;
1124 }
1125 }
1126 }
1127
1128 if (all_non_null) {
1129 // Value is non-null => update Phi
1130 if (PrintNullCheckElimination) {
1131 tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1132 }
1133 x->set_needs_null_check(false);
1134 } else if (set_contains(x)) {
1135 set_remove(x);
1136 }
1137 }
1138
1139 void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
1140 for (int i = 0; i < x->nb_profiled_args(); i++) {
1141 x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
1142 }
1143 }
1144
1145 void Optimizer::eliminate_null_checks() {
1146 ResourceMark rm;
1147
1148 NullCheckEliminator nce(this);
1149
1150 if (PrintNullCheckElimination) {
1151 tty->print_cr("Starting null check elimination for method %s::%s%s",
1152 ir()->method()->holder()->name()->as_utf8(),
1153 ir()->method()->name()->as_utf8(),
1154 ir()->method()->signature()->as_symbol()->as_utf8());
1155 }
1156
1157 // Apply to graph
1158 nce.iterate(ir()->start());
1159
1160 // walk over the graph looking for exception
1161 // handlers and iterate over them as well
1162 int nblocks = BlockBegin::number_of_blocks();
|
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_ProfileReturnType (ProfileReturnType* x);
535 void do_ProfileInvoke (ProfileInvoke* x);
536 void do_RuntimeCall (RuntimeCall* x);
537 void do_MemBar (MemBar* x);
538 void do_RangeCheckPredicate(RangeCheckPredicate* x);
539 #ifdef ASSERT
540 void do_Assert (Assert* x);
541 #endif
542 };
543
544
545 // Because of a static contained within (for the purpose of iteration
546 // over instructions), it is only valid to have one of these active at
547 // a time
548 class NullCheckEliminator: public ValueVisitor {
549 private:
550 Optimizer* _opt;
551
552 ValueSet* _visitable_instructions; // Visit each instruction only once per basic block
553 BlockList* _work_list; // Basic blocks to visit
554
642 // Handlers for relevant instructions
643 // (separated out from NullCheckVisitor for clarity)
644
645 // The basic contract is that these must leave the instruction in
646 // the desired state; must not assume anything about the state of
647 // the instruction. We make multiple passes over some basic blocks
648 // and the last pass is the only one whose result is valid.
649 void handle_AccessField (AccessField* x);
650 void handle_ArrayLength (ArrayLength* x);
651 void handle_LoadIndexed (LoadIndexed* x);
652 void handle_StoreIndexed (StoreIndexed* x);
653 void handle_NullCheck (NullCheck* x);
654 void handle_Invoke (Invoke* x);
655 void handle_NewInstance (NewInstance* x);
656 void handle_NewArray (NewArray* x);
657 void handle_AccessMonitor (AccessMonitor* x);
658 void handle_Intrinsic (Intrinsic* x);
659 void handle_ExceptionObject (ExceptionObject* x);
660 void handle_Phi (Phi* x);
661 void handle_ProfileCall (ProfileCall* x);
662 void handle_ProfileReturnType (ProfileReturnType* x);
663 };
664
665
666 // NEEDS_CLEANUP
667 // There may be other instructions which need to clear the last
668 // explicit null check. Anything across which we can not hoist the
669 // debug information for a NullCheck instruction must clear it. It
670 // might be safer to pattern match "NullCheck ; {AccessField,
671 // ArrayLength, LoadIndexed}" but it is more easily structured this way.
672 // Should test to see performance hit of clearing it for all handlers
673 // with empty bodies below. If it is negligible then we should leave
674 // that in for safety, otherwise should think more about it.
675 void NullCheckVisitor::do_Phi (Phi* x) { nce()->handle_Phi(x); }
676 void NullCheckVisitor::do_Local (Local* x) {}
677 void NullCheckVisitor::do_Constant (Constant* x) { /* FIXME: handle object constants */ }
678 void NullCheckVisitor::do_LoadField (LoadField* x) { nce()->handle_AccessField(x); }
679 void NullCheckVisitor::do_StoreField (StoreField* x) { nce()->handle_AccessField(x); }
680 void NullCheckVisitor::do_ArrayLength (ArrayLength* x) { nce()->handle_ArrayLength(x); }
681 void NullCheckVisitor::do_LoadIndexed (LoadIndexed* x) { nce()->handle_LoadIndexed(x); }
682 void NullCheckVisitor::do_StoreIndexed (StoreIndexed* x) { nce()->handle_StoreIndexed(x); }
703 void NullCheckVisitor::do_Goto (Goto* x) {}
704 void NullCheckVisitor::do_If (If* x) {}
705 void NullCheckVisitor::do_IfInstanceOf (IfInstanceOf* x) {}
706 void NullCheckVisitor::do_TableSwitch (TableSwitch* x) {}
707 void NullCheckVisitor::do_LookupSwitch (LookupSwitch* x) {}
708 void NullCheckVisitor::do_Return (Return* x) {}
709 void NullCheckVisitor::do_Throw (Throw* x) { nce()->clear_last_explicit_null_check(); }
710 void NullCheckVisitor::do_Base (Base* x) {}
711 void NullCheckVisitor::do_OsrEntry (OsrEntry* x) {}
712 void NullCheckVisitor::do_ExceptionObject(ExceptionObject* x) { nce()->handle_ExceptionObject(x); }
713 void NullCheckVisitor::do_RoundFP (RoundFP* x) {}
714 void NullCheckVisitor::do_UnsafeGetRaw (UnsafeGetRaw* x) {}
715 void NullCheckVisitor::do_UnsafePutRaw (UnsafePutRaw* x) {}
716 void NullCheckVisitor::do_UnsafeGetObject(UnsafeGetObject* x) {}
717 void NullCheckVisitor::do_UnsafePutObject(UnsafePutObject* x) {}
718 void NullCheckVisitor::do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) {}
719 void NullCheckVisitor::do_UnsafePrefetchRead (UnsafePrefetchRead* x) {}
720 void NullCheckVisitor::do_UnsafePrefetchWrite(UnsafePrefetchWrite* x) {}
721 void NullCheckVisitor::do_ProfileCall (ProfileCall* x) { nce()->clear_last_explicit_null_check();
722 nce()->handle_ProfileCall(x); }
723 void NullCheckVisitor::do_ProfileReturnType (ProfileReturnType* x) { nce()->handle_ProfileReturnType(x); }
724 void NullCheckVisitor::do_ProfileInvoke (ProfileInvoke* x) {}
725 void NullCheckVisitor::do_RuntimeCall (RuntimeCall* x) {}
726 void NullCheckVisitor::do_MemBar (MemBar* x) {}
727 void NullCheckVisitor::do_RangeCheckPredicate(RangeCheckPredicate* x) {}
728 #ifdef ASSERT
729 void NullCheckVisitor::do_Assert (Assert* x) {}
730 #endif
731
732 void NullCheckEliminator::visit(Value* p) {
733 assert(*p != NULL, "should not find NULL instructions");
734 if (visitable(*p)) {
735 mark_visited(*p);
736 (*p)->visit(&_visitor);
737 }
738 }
739
740 bool NullCheckEliminator::merge_state_for(BlockBegin* block, ValueSet* incoming_state) {
741 ValueSet* state = state_for(block);
742 if (state == NULL) {
743 state = incoming_state->copy();
1126 all_non_null = false;
1127 }
1128 }
1129 }
1130
1131 if (all_non_null) {
1132 // Value is non-null => update Phi
1133 if (PrintNullCheckElimination) {
1134 tty->print_cr("Eliminated Phi %d's null check for phifun because all inputs are non-null", x->id());
1135 }
1136 x->set_needs_null_check(false);
1137 } else if (set_contains(x)) {
1138 set_remove(x);
1139 }
1140 }
1141
1142 void NullCheckEliminator::handle_ProfileCall(ProfileCall* x) {
1143 for (int i = 0; i < x->nb_profiled_args(); i++) {
1144 x->set_arg_needs_null_check(i, !set_contains(x->profiled_arg_at(i)));
1145 }
1146 }
1147
1148 void NullCheckEliminator::handle_ProfileReturnType(ProfileReturnType* x) {
1149 x->set_needs_null_check(!set_contains(x->ret()));
1150 }
1151
1152 void Optimizer::eliminate_null_checks() {
1153 ResourceMark rm;
1154
1155 NullCheckEliminator nce(this);
1156
1157 if (PrintNullCheckElimination) {
1158 tty->print_cr("Starting null check elimination for method %s::%s%s",
1159 ir()->method()->holder()->name()->as_utf8(),
1160 ir()->method()->name()->as_utf8(),
1161 ir()->method()->signature()->as_symbol()->as_utf8());
1162 }
1163
1164 // Apply to graph
1165 nce.iterate(ir()->start());
1166
1167 // walk over the graph looking for exception
1168 // handlers and iterate over them as well
1169 int nblocks = BlockBegin::number_of_blocks();
|