@@ -1031,63 +1031,57 @@ bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* f
10311031 const TypeInt* type2 = filtered_int_type (igvn, n, fail);
10321032 if (type2 != nullptr ) {
10331033 failtype = failtype->join (type2)->is_int ();
1034- if (failtype->_lo > failtype-> _hi ) {
1034+ if (failtype->empty () ) {
10351035 // previous if determines the result of this if so
10361036 // replace Bool with constant
10371037 igvn->replace_input_of (this , 1 , igvn->intcon (success->_con ));
10381038 return true ;
10391039 }
10401040 }
10411041 }
1042- lo = nullptr ;
1043- hi = nullptr ;
1042+ return false ;
1043+ }
1044+
1045+ assert (lo != nullptr && hi != nullptr , " sanity" );
1046+ Node* hook = new Node (lo); // Add a use to lo to prevent him from dying
1047+ // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
1048+ Node* adjusted_val = igvn->transform (new SubINode (n, lo));
1049+ if (adjusted_lim == nullptr ) {
1050+ adjusted_lim = igvn->transform (new SubINode (hi, lo));
10441051 }
1052+ hook->destruct (igvn);
10451053
1046- if (lo && hi) {
1047- Node* hook = new Node (1 );
1048- hook->init_req (0 , lo); // Add a use to lo to prevent him from dying
1049- // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
1050- Node* adjusted_val = igvn->transform (new SubINode (n, lo));
1051- if (adjusted_lim == nullptr ) {
1052- adjusted_lim = igvn->transform (new SubINode (hi, lo));
1054+ if (igvn->type (adjusted_lim)->is_int ()->_lo < 0 &&
1055+ !igvn->C ->post_loop_opts_phase ()) {
1056+ // If range check elimination applies to this comparison, it includes code to protect from overflows that may
1057+ // cause the main loop to be skipped entirely. Delay this transformation.
1058+ // Example:
1059+ // for (int i = 0; i < limit; i++) {
1060+ // if (i < max_jint && i > min_jint) {...
1061+ // }
1062+ // Comparisons folded as:
1063+ // i - min_jint - 1 <u -2
1064+ // when RC applies, main loop limit becomes:
1065+ // min(limit, max(-2 + min_jint + 1, min_jint))
1066+ // = min(limit, min_jint)
1067+ // = min_jint
1068+ if (adjusted_val->outcnt () == 0 ) {
1069+ igvn->remove_dead_node (adjusted_val);
10531070 }
1054- hook->destruct (igvn);
1055-
1056- int lo = igvn->type (adjusted_lim)->is_int ()->_lo ;
1057- if (lo < 0 ) {
1058- // If range check elimination applies to this comparison, it includes code to protect from overflows that may
1059- // cause the main loop to be skipped entirely. Delay this transformation.
1060- // Example:
1061- // for (int i = 0; i < limit; i++) {
1062- // if (i < max_jint && i > min_jint) {...
1063- // }
1064- // Comparisons folded as:
1065- // i - min_jint - 1 <u -2
1066- // when RC applies, main loop limit becomes:
1067- // min(limit, max(-2 + min_jint + 1, min_jint))
1068- // = min(limit, min_jint)
1069- // = min_jint
1070- if (!igvn->C ->post_loop_opts_phase ()) {
1071- if (adjusted_val->outcnt () == 0 ) {
1072- igvn->remove_dead_node (adjusted_val);
1073- }
1074- if (adjusted_lim->outcnt () == 0 ) {
1075- igvn->remove_dead_node (adjusted_lim);
1076- }
1077- igvn->C ->record_for_post_loop_opts_igvn (this );
1078- return false ;
1079- }
1071+ if (adjusted_lim->outcnt () == 0 ) {
1072+ igvn->remove_dead_node (adjusted_lim);
10801073 }
1074+ igvn->C ->record_for_post_loop_opts_igvn (this );
1075+ return false ;
1076+ }
10811077
1082- Node* newcmp = igvn->transform (new CmpUNode (adjusted_val, adjusted_lim));
1083- Node* newbool = igvn->transform (new BoolNode (newcmp, cond));
1078+ Node* newcmp = igvn->transform (new CmpUNode (adjusted_val, adjusted_lim));
1079+ Node* newbool = igvn->transform (new BoolNode (newcmp, cond));
10841080
1085- igvn->replace_input_of (dom_iff, 1 , igvn->intcon (proj->_con ));
1086- igvn->replace_input_of (this , 1 , newbool);
1081+ igvn->replace_input_of (dom_iff, 1 , igvn->intcon (proj->_con ));
1082+ igvn->replace_input_of (this , 1 , newbool);
10871083
1088- return true ;
1089- }
1090- return false ;
1084+ return true ;
10911085}
10921086
10931087// Merge the branches that trap for this If and the dominating If into
0 commit comments