800 // Insert loop predicates for null checks and range checks
801 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) {
802 if (!UseLoopPredicate) return false;
803
804 if (!loop->_head->is_Loop()) {
805 // Could be a simple region when irreducible loops are present.
806 return false;
807 }
808 LoopNode* head = loop->_head->as_Loop();
809
810 if (head->unique_ctrl_out()->Opcode() == Op_NeverBranch) {
811 // do nothing for infinite loops
812 return false;
813 }
814
815 CountedLoopNode *cl = NULL;
816 if (head->is_CountedLoop()) {
817 cl = head->as_CountedLoop();
818 // do nothing for iteration-splitted loops
819 if (!cl->is_normal_loop()) return false;
820 }
821
822 Node* entry = head->in(LoopNode::EntryControl);
823 ProjNode *predicate_proj = NULL;
824 // Loop limit check predicate should be near the loop.
825 if (LoopLimitCheck) {
826 predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
827 if (predicate_proj != NULL)
828 entry = predicate_proj->in(0)->in(0);
829 }
830
831 predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
832 if (!predicate_proj) {
833 #ifndef PRODUCT
834 if (TraceLoopPredicate) {
835 tty->print("missing predicate:");
836 loop->dump_head();
837 head->dump(1);
838 }
839 #endif
941 ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate);
942 assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate");
943 Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0);
944
945 // Perform cloning to keep Invariance state correct since the
946 // late schedule will place invariant things in the loop.
947 rng = invar.clone(rng, ctrl);
948 if (offset && offset != zero) {
949 assert(invar.is_invariant(offset), "offset must be loop invariant");
950 offset = invar.clone(offset, ctrl);
951 }
952
953 // Test the lower bound
954 Node* lower_bound_bol = rc_predicate(loop, ctrl, scale, offset, init, limit, stride, rng, false);
955 IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If();
956 _igvn.hash_delete(lower_bound_iff);
957 lower_bound_iff->set_req(1, lower_bound_bol);
958 if (TraceLoopPredicate) tty->print_cr("lower bound check if: %d", lower_bound_iff->_idx);
959
960 // Test the upper bound
961 Node* upper_bound_bol = rc_predicate(loop, ctrl, scale, offset, init, limit, stride, rng, true);
962 IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If();
963 _igvn.hash_delete(upper_bound_iff);
964 upper_bound_iff->set_req(1, upper_bound_bol);
965 if (TraceLoopPredicate) tty->print_cr("upper bound check if: %d", lower_bound_iff->_idx);
966
967 // Fall through into rest of the clean up code which will move
968 // any dependent nodes onto the upper bound test.
969 new_predicate_proj = upper_bound_proj;
970
971 #ifndef PRODUCT
972 if (TraceLoopOpts && !TraceLoopPredicate) {
973 tty->print("Predicate RC ");
974 loop->dump_head();
975 }
976 #endif
977 } else {
978 // Loop variant check (for example, range check in non-counted loop)
979 // with uncommon trap.
980 continue;
981 }
|
800 // Insert loop predicates for null checks and range checks
801 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) {
802 if (!UseLoopPredicate) return false;
803
804 if (!loop->_head->is_Loop()) {
805 // Could be a simple region when irreducible loops are present.
806 return false;
807 }
808 LoopNode* head = loop->_head->as_Loop();
809
810 if (head->unique_ctrl_out()->Opcode() == Op_NeverBranch) {
811 // do nothing for infinite loops
812 return false;
813 }
814
815 CountedLoopNode *cl = NULL;
816 if (head->is_CountedLoop()) {
817 cl = head->as_CountedLoop();
818 // do nothing for iteration-splitted loops
819 if (!cl->is_normal_loop()) return false;
820 // Avoid RCE if Counted loop's test is '!='.
821 BoolTest::mask bt = cl->loopexit()->test_trip();
822 if (bt != BoolTest::lt && bt != BoolTest::gt)
823 cl = NULL;
824 }
825
826 Node* entry = head->in(LoopNode::EntryControl);
827 ProjNode *predicate_proj = NULL;
828 // Loop limit check predicate should be near the loop.
829 if (LoopLimitCheck) {
830 predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
831 if (predicate_proj != NULL)
832 entry = predicate_proj->in(0)->in(0);
833 }
834
835 predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
836 if (!predicate_proj) {
837 #ifndef PRODUCT
838 if (TraceLoopPredicate) {
839 tty->print("missing predicate:");
840 loop->dump_head();
841 head->dump(1);
842 }
843 #endif
945 ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate);
946 assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate");
947 Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0);
948
949 // Perform cloning to keep Invariance state correct since the
950 // late schedule will place invariant things in the loop.
951 rng = invar.clone(rng, ctrl);
952 if (offset && offset != zero) {
953 assert(invar.is_invariant(offset), "offset must be loop invariant");
954 offset = invar.clone(offset, ctrl);
955 }
956
957 // Test the lower bound
958 Node* lower_bound_bol = rc_predicate(loop, ctrl, scale, offset, init, limit, stride, rng, false);
959 IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If();
960 _igvn.hash_delete(lower_bound_iff);
961 lower_bound_iff->set_req(1, lower_bound_bol);
962 if (TraceLoopPredicate) tty->print_cr("lower bound check if: %d", lower_bound_iff->_idx);
963
964 // Test the upper bound
965 Node* upper_bound_bol = rc_predicate(loop, lower_bound_proj, scale, offset, init, limit, stride, rng, true);
966 IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If();
967 _igvn.hash_delete(upper_bound_iff);
968 upper_bound_iff->set_req(1, upper_bound_bol);
969 if (TraceLoopPredicate) tty->print_cr("upper bound check if: %d", lower_bound_iff->_idx);
970
971 // Fall through into rest of the clean up code which will move
972 // any dependent nodes onto the upper bound test.
973 new_predicate_proj = upper_bound_proj;
974
975 #ifndef PRODUCT
976 if (TraceLoopOpts && !TraceLoopPredicate) {
977 tty->print("Predicate RC ");
978 loop->dump_head();
979 }
980 #endif
981 } else {
982 // Loop variant check (for example, range check in non-counted loop)
983 // with uncommon trap.
984 continue;
985 }
|