src/share/vm/opto/ifnode.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/opto

src/share/vm/opto/ifnode.cpp

Print this page
rev 8157 : 8078426: mb/jvm/compiler/InterfaceCalls/testAC2 - assert(predicate_proj == 0L) failed: only one predicate entry expected
Summary: split if finds predicates on several incoming paths when unswitched's loops are optimized out
Reviewed-by:
rev 9360 : 8137168: Replace IfNode with a new RangeCheckNode for range checks
Summary: new RangeCheckNode to enable optimization of explicit library level range checks
Reviewed-by:


 289   igvn->register_new_node_with_optimizer( region_c );
 290   igvn->register_new_node_with_optimizer( region_x );
 291   // Prevent the untimely death of phi_x.  Currently he has no uses.  He is
 292   // about to get one.  If this only use goes away, then phi_x will look dead.
 293   // However, he will be picking up some more uses down below.
 294   Node *hook = new Node(4);
 295   hook->init_req(0, phi_x);
 296   hook->init_req(1, phi_c);
 297   phi_x = phase->transform( phi_x );
 298 
 299   // Make the compare
 300   Node *cmp_c = phase->makecon(t);
 301   Node *cmp_x = cmp->clone();
 302   cmp_x->set_req(1,phi_x);
 303   cmp_x->set_req(2,con2);
 304   cmp_x = phase->transform(cmp_x);
 305   // Make the bool
 306   Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test));
 307   Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test));
 308   // Make the IfNode
 309   IfNode *iff_c = new IfNode(region_c,b_c,iff->_prob,iff->_fcnt);


 310   igvn->set_type_bottom(iff_c);
 311   igvn->_worklist.push(iff_c);
 312   hook->init_req(2, iff_c);
 313 
 314   IfNode *iff_x = new IfNode(region_x,b_x,iff->_prob, iff->_fcnt);


 315   igvn->set_type_bottom(iff_x);
 316   igvn->_worklist.push(iff_x);
 317   hook->init_req(3, iff_x);
 318 
 319   // Make the true/false arms
 320   Node *iff_c_t = phase->transform(new IfTrueNode (iff_c));
 321   Node *iff_c_f = phase->transform(new IfFalseNode(iff_c));
 322   if (predicate_c != NULL) {
 323     assert(predicate_x == NULL, "only one predicate entry expected");
 324     // Clone loop predicates to each path
 325     iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);
 326     iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);
 327   }
 328   Node *iff_x_t = phase->transform(new IfTrueNode (iff_x));
 329   Node *iff_x_f = phase->transform(new IfFalseNode(iff_x));
 330   if (predicate_x != NULL) {
 331     assert(predicate_c == NULL, "only one predicate entry expected");
 332     // Clone loop predicates to each path
 333     iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);
 334     iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);


 479     flip_test = 2;
 480   } else if (bn->_test._test != BoolTest::lt) {
 481     return NULL;
 482   }
 483   if (l->is_top())  return NULL;   // Top input means dead test
 484   if (r->Opcode() != Op_LoadRange)  return NULL;
 485 
 486   // We have recognized one of these forms:
 487   //  Flip 1:  If (Bool[<] CmpU(l, LoadRange)) ...
 488   //  Flip 2:  If (Bool[<=] CmpU(LoadRange, l)) ...
 489 
 490   ProjNode* iftrap = proj_out(flip_test == 2 ? true : false);
 491   return iftrap;
 492 }
 493 
 494 
 495 //------------------------------is_range_check---------------------------------
 496 // Return 0 if not a range check.  Return 1 if a range check and set index and
 497 // offset.  Return 2 if we had to negate the test.  Index is NULL if the check
 498 // is versus a constant.
 499 int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) {
 500   int flip_test = 0;
 501   Node* l = NULL;
 502   Node* r = NULL;
 503   ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
 504 
 505   if (iftrap == NULL) {
 506     return 0;
 507   }
 508 
 509   // Make sure it's a real range check by requiring an uncommon trap
 510   // along the OOB path.  Otherwise, it's possible that the user wrote
 511   // something which optimized to look like a range check but behaves
 512   // in some other way.
 513   if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == NULL) {
 514     return 0;
 515   }
 516 
 517   // Look for index+offset form
 518   Node* ind = l;
 519   jint  off = 0;


 707 //           /      unc
 708 //
 709 
 710 // Is the comparison for this If suitable for folding?
 711 bool IfNode::cmpi_folds(PhaseIterGVN* igvn) {
 712   return in(1) != NULL &&
 713     in(1)->is_Bool() &&
 714     in(1)->in(1) != NULL &&
 715     in(1)->in(1)->Opcode() == Op_CmpI &&
 716     in(1)->in(1)->in(2) != NULL &&
 717     in(1)->in(1)->in(2) != igvn->C->top() &&
 718     (in(1)->as_Bool()->_test.is_less() ||
 719      in(1)->as_Bool()->_test.is_greater());
 720 }
 721 
 722 // Is a dominating control suitable for folding with this if?
 723 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
 724   return ctrl != NULL &&
 725     ctrl->is_Proj() &&
 726     ctrl->in(0) != NULL &&
 727     ctrl->in(0)->is_If() &&
 728     ctrl->in(0)->outcnt() == 2 &&
 729     ctrl->in(0)->as_If()->cmpi_folds(igvn) &&
 730     // Must compare same value
 731     ctrl->in(0)->in(1)->in(1)->in(1) != NULL &&
 732     ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
 733 }
 734 
 735 // Do this If and the dominating If share a region?
 736 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
 737   ProjNode* otherproj = proj->other_if_proj();
 738   Node* otherproj_ctrl_use = otherproj->unique_ctrl_out();
 739   RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL;
 740   success = NULL;
 741   fail = NULL;
 742 
 743   if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) {
 744     for (int i = 0; i < 2; i++) {
 745       ProjNode* proj = proj_out(i);
 746       if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
 747         success = proj;


 955     } else if (lo_test == BoolTest::le) {
 956       if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
 957         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 958         cond = BoolTest::ge;
 959       } else {
 960         assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test");
 961         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 962         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 963         cond = BoolTest::ge;
 964       }
 965     }
 966   } else {
 967     const TypeInt* failtype  = filtered_int_type(igvn, n, proj);
 968     if (failtype != NULL) {
 969       const TypeInt* type2 = filtered_int_type(igvn, n, fail);
 970       if (type2 != NULL) {
 971         failtype = failtype->join(type2)->is_int();
 972         if (failtype->_lo > failtype->_hi) {
 973           // previous if determines the result of this if so
 974           // replace Bool with constant
 975           igvn->hash_delete(this);
 976           set_req(1, igvn->intcon(success->_con));
 977           return true;
 978         }
 979       }
 980     }
 981     lo = NULL;
 982     hi = NULL;
 983   }
 984 
 985   if (lo && hi) {
 986     // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
 987     Node* adjusted_val = igvn->transform(new SubINode(n,  lo));
 988     if (adjusted_lim == NULL) {
 989       adjusted_lim = igvn->transform(new SubINode(hi, lo));
 990     }
 991     Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
 992     Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
 993 
 994     igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
 995     set_req(1, newbool);

 996 
 997     return true;
 998   }
 999   return false;
1000 }
1001 
1002 // Merge the branches that trap for this If and the dominating If into
1003 // a single region that branches to the uncommon trap for the
1004 // dominating If
1005 void IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {



1006   ProjNode* otherproj = proj->other_if_proj();
1007 
1008   CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
1009   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
1010 
1011   if (unc != dom_unc) {
1012     Node* r = new RegionNode(3);
1013 
1014     r->set_req(1, otherproj);
1015     r->set_req(2, success);
1016     r = igvn->transform(r);
1017     assert(r->is_Region(), "can't go away");
1018 
1019     // Make both If trap at the state of the first If: once the CmpI
1020     // nodes are merged, if we trap we don't know which of the CmpI
1021     // nodes would have caused the trap so we have to restart
1022     // execution at the first one
1023     igvn->replace_input_of(dom_unc, 0, r);
1024     igvn->replace_input_of(unc, 0, igvn->C->top());
1025   }
1026   int trap_request = dom_unc->uncommon_trap_request();
1027   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1028   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
1029 
1030   int flip_test = 0;
1031   Node* l = NULL;
1032   Node* r = NULL;
1033 
1034   if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) {
1035     // If this looks like a range check, change the trap to
1036     // Reason_range_check so the compiler recognizes it as a range
1037     // check and applies the corresponding optimizations
1038     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1039 
1040     improve_address_types(l, r, fail, igvn);


1041   } else if (unc != dom_unc) {
1042     // If we trap we won't know what CmpI would have caused the trap
1043     // so use a special trap reason to mark this pair of CmpI nodes as
1044     // bad candidate for folding. On recompilation we won't fold them
1045     // and we may trap again but this time we'll know what branch
1046     // traps
1047     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1048   }
1049   igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));

1050 }
1051 
1052 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1053 // of a rangecheck on index i, on 64 bit the compares may be followed
1054 // by memory accesses using i as index. In that case, the CmpU tells
1055 // us something about the values taken by i that can help the compiler
1056 // (see Compile::conv_I2X_index())
1057 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1058 #ifdef _LP64
1059   ResourceMark rm;
1060   Node_Stack stack(2);
1061 
1062   assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1063   const TypeInt* array_size = igvn->type(r)->is_int();
1064 
1065   stack.push(l, 0);
1066 
1067   while(stack.size() > 0) {
1068     Node* n = stack.node();
1069     uint start = stack.index();


1223 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1224   if (Opcode() != Op_If) return NULL;
1225 
1226   if (cmpi_folds(igvn)) {
1227     Node* ctrl = in(0);
1228     if (is_ctrl_folds(ctrl, igvn) &&
1229         ctrl->outcnt() == 1) {
1230       // A integer comparison immediately dominated by another integer
1231       // comparison
1232       ProjNode* success = NULL;
1233       ProjNode* fail = NULL;
1234       ProjNode* dom_cmp = ctrl->as_Proj();
1235       if (has_shared_region(dom_cmp, success, fail) &&
1236           // Next call modifies graph so must be last
1237           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1238         return this;
1239       }
1240       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1241           // Next call modifies graph so must be last
1242           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1243         merge_uncommon_traps(dom_cmp, success, fail, igvn);
1244         return this;
1245       }
1246       return NULL;
1247     } else if (ctrl->in(0) != NULL &&
1248                ctrl->in(0)->in(0) != NULL) {
1249       ProjNode* success = NULL;
1250       ProjNode* fail = NULL;
1251       Node* dom = ctrl->in(0)->in(0);
1252       ProjNode* dom_cmp = dom->isa_Proj();
1253       ProjNode* other_cmp = ctrl->isa_Proj();
1254 
1255       // Check if it's an integer comparison dominated by another
1256       // integer comparison with another test in between
1257       if (is_ctrl_folds(dom, igvn) &&
1258           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1259           is_side_effect_free_test(other_cmp, igvn) &&
1260           // Next call modifies graph so must be last
1261           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1262         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1263         merge_uncommon_traps(dom_cmp, success, fail, igvn);
1264         return this;
1265       }
1266     }
1267   }
1268   return NULL;
1269 }
1270 
1271 //------------------------------remove_useless_bool----------------------------
1272 // Check for people making a useless boolean: things like
1273 // if( (x < y ? true : false) ) { ... }
1274 // Replace with if( x < y ) { ... }
1275 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1276   Node *i1 = iff->in(1);
1277   if( !i1->is_Bool() ) return NULL;
1278   BoolNode *bol = i1->as_Bool();
1279 
1280   Node *cmp = bol->in(1);
1281   if( cmp->Opcode() != Op_CmpI ) return NULL;
1282 
1283   // Must be comparing against a bool
1284   const Type *cmp2_t = phase->type( cmp->in(2) );


1325   }
1326   if( true_path == 2 ) {
1327     flip = 1-flip;
1328   }
1329 
1330   Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1331   assert(new_bol != iff->in(1), "must make progress");
1332   iff->set_req(1, new_bol);
1333   // Intervening diamond probably goes dead
1334   phase->C->set_major_progress();
1335   return iff;
1336 }
1337 
1338 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1339 
1340 struct RangeCheck {
1341   Node* ctl;
1342   jint off;
1343 };
1344 
1345 //------------------------------Ideal------------------------------------------
1346 // Return a node which is more "ideal" than the current node.  Strip out
1347 // control copies
1348 Node *IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1349   if (remove_dead_region(phase, can_reshape))  return this;
1350   // No Def-Use info?
1351   if (!can_reshape)  return NULL;
1352   PhaseIterGVN *igvn = phase->is_IterGVN();
1353 
1354   // Don't bother trying to transform a dead if
1355   if (in(0)->is_top())  return NULL;
1356   // Don't bother trying to transform an if with a dead test
1357   if (in(1)->is_top())  return NULL;
1358   // Another variation of a dead test
1359   if (in(1)->is_Con())  return NULL;
1360   // Another variation of a dead if
1361   if (outcnt() < 2)  return NULL;
1362 
1363   // Canonicalize the test.
1364   Node* idt_if = idealize_test(phase, this);
1365   if (idt_if != NULL)  return idt_if;
1366 
1367   // Try to split the IF

1368   Node *s = split_if(this, igvn);
1369   if (s != NULL)  return s;
1370 












1371   // Check for people making a useless boolean: things like
1372   // if( (x < y ? true : false) ) { ... }
1373   // Replace with if( x < y ) { ... }
1374   Node *bol2 = remove_useless_bool(this, phase);
1375   if( bol2 ) return bol2;
1376 
1377   // Setup to scan up the CFG looking for a dominating test
1378   Node *dom = in(0);
1379   Node *prev_dom = this;
1380 
1381   // Check for range-check vs other kinds of tests
1382   Node *index1, *range1;
1383   jint offset1;
1384   int flip1 = is_range_check(range1, index1, offset1);
1385   if( flip1 ) {
1386     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
1387     // so all checks we inspect post-dominate the top-most check we find.
1388     // If we are going to fail the current check and we reach the top check
1389     // then we are guaranteed to fail, so just start interpreting there.
1390     // We 'expand' the top 3 range checks to include all post-dominating
1391     // checks.
1392 
1393     // The top 3 range checks seen
1394     const int NRC =3;
1395     RangeCheck prev_checks[NRC];
1396     int nb_checks = 0;
1397 
1398     // Low and high offsets seen so far
1399     jint off_lo = offset1;
1400     jint off_hi = offset1;
1401 
1402     bool found_immediate_dominator = false;
1403 
1404     // Scan for the top checks and collect range of offsets
1405     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1406       if (dom->Opcode() == Op_If &&  // Not same opcode?
1407           prev_dom->in(0) == dom) { // One path of test does dominate?
1408         if (dom == this) return NULL; // dead loop
1409         // See if this is a range check
1410         Node *index2, *range2;
1411         jint offset2;
1412         int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);
1413         // See if this is a _matching_ range check, checking against
1414         // the same array bounds.
1415         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1416             dom->outcnt() == 2) {
1417           if (nb_checks == 0 && dom->in(1) == in(1)) {
1418             // Found an immediately dominating test at the same offset.
1419             // This kind of back-to-back test can be eliminated locally,
1420             // and there is no need to search further for dominating tests.
1421             assert(offset2 == offset1, "Same test but different offsets");
1422             found_immediate_dominator = true;
1423             break;
1424           }
1425           // Gather expanded bounds
1426           off_lo = MIN2(off_lo,offset2);
1427           off_hi = MAX2(off_hi,offset2);
1428           // Record top NRC range checks
1429           prev_checks[nb_checks%NRC].ctl = prev_dom;
1430           prev_checks[nb_checks%NRC].off = offset2;
1431           nb_checks++;
1432         }
1433       }
1434       prev_dom = dom;
1435       dom = up_one_dom(dom);
1436       if (!dom) break;
1437     }
1438 
1439     if (!found_immediate_dominator) {
1440       // Attempt to widen the dominating range check to cover some later
1441       // ones.  Since range checks "fail" by uncommon-trapping to the
1442       // interpreter, widening a check can make us speculatively enter
1443       // the interpreter.  If we see range-check deopt's, do not widen!
1444       if (!phase->C->allow_range_check_smearing())  return NULL;
1445 
1446       // Didn't find prior covering check, so cannot remove anything.
1447       if (nb_checks == 0) {
1448         return NULL;
1449       }
1450       // Constant indices only need to check the upper bound.
1451       // Non-constant indices must check both low and high.
1452       int chk0 = (nb_checks - 1) % NRC;
1453       if (index1) {
1454         if (nb_checks == 1) {
1455           return NULL;
1456         } else {
1457           // If the top range check's constant is the min or max of
1458           // all constants we widen the next one to cover the whole
1459           // range of constants.
1460           RangeCheck rc0 = prev_checks[chk0];
1461           int chk1 = (nb_checks - 2) % NRC;
1462           RangeCheck rc1 = prev_checks[chk1];
1463           if (rc0.off == off_lo) {
1464             adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1465             prev_dom = rc1.ctl;
1466           } else if (rc0.off == off_hi) {
1467             adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1468             prev_dom = rc1.ctl;
1469           } else {
1470             // If the top test's constant is not the min or max of all
1471             // constants, we need 3 range checks. We must leave the
1472             // top test unchanged because widening it would allow the
1473             // accesses it protects to successfully read/write out of
1474             // bounds.
1475             if (nb_checks == 2) {
1476               return NULL;
1477             }
1478             int chk2 = (nb_checks - 3) % NRC;
1479             RangeCheck rc2 = prev_checks[chk2];
1480             // The top range check a+i covers interval: -a <= i < length-a
1481             // The second range check b+i covers interval: -b <= i < length-b
1482             if (rc1.off <= rc0.off) {
1483               // if b <= a, we change the second range check to:
1484               // -min_of_all_constants <= i < length-min_of_all_constants
1485               // Together top and second range checks now cover:
1486               // -min_of_all_constants <= i < length-a
1487               // which is more restrictive than -b <= i < length-b:
1488               // -b <= -min_of_all_constants <= i < length-a <= length-b
1489               // The third check is then changed to:
1490               // -max_of_all_constants <= i < length-max_of_all_constants
1491               // so 2nd and 3rd checks restrict allowed values of i to:
1492               // -min_of_all_constants <= i < length-max_of_all_constants
1493               adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1494               adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1495             } else {
1496               // if b > a, we change the second range check to:
1497               // -max_of_all_constants <= i < length-max_of_all_constants
1498               // Together top and second range checks now cover:
1499               // -a <= i < length-max_of_all_constants
1500               // which is more restrictive than -b <= i < length-b:
1501               // -b < -a <= i < length-max_of_all_constants <= length-b
1502               // The third check is then changed to:
1503               // -max_of_all_constants <= i < length-max_of_all_constants
1504               // so 2nd and 3rd checks restrict allowed values of i to:
1505               // -min_of_all_constants <= i < length-max_of_all_constants
1506               adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1507               adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1508             }
1509             prev_dom = rc2.ctl;
1510           }
1511         }
1512       } else {
1513         RangeCheck rc0 = prev_checks[chk0];
1514         // 'Widen' the offset of the 1st and only covering check
1515         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1516         // Test is now covered by prior checks, dominate it out
1517         prev_dom = rc0.ctl;
1518       }
1519     }
1520 
1521   } else {                      // Scan for an equivalent test
1522 
1523     Node *cmp;
1524     int dist = 0;               // Cutoff limit for search
1525     int op = Opcode();
1526     if( op == Op_If &&
1527         (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
1528       if( cmp->in(2) != NULL && // make sure cmp is not already dead
1529           cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
1530         dist = 64;              // Limit for null-pointer scans
1531       } else {
1532         dist = 4;               // Do not bother for random pointer tests
1533       }
1534     } else {
1535       dist = 4;                 // Limit for random junky scans
1536     }
1537 
1538     // Normal equivalent-test check.
1539     if( !dom ) return NULL;     // Dead loop?
1540 
1541     Node* result = fold_compares(igvn);
1542     if (result != NULL) {
1543       return result;
1544     }
1545 
1546     // Search up the dominator tree for an If with an identical test
1547     while( dom->Opcode() != op    ||  // Not same opcode?
1548            dom->in(1)    != in(1) ||  // Not same input 1?
1549            (req() == 3 && dom->in(2) != in(2)) || // Not same input 2?
1550            prev_dom->in(0) != dom ) { // One path of test does not dominate?
1551       if( dist < 0 ) return NULL;
1552 
1553       dist--;
1554       prev_dom = dom;
1555       dom = up_one_dom( dom );
1556       if( !dom ) return NULL;
1557     }
1558 
1559     // Check that we did not follow a loop back to ourselves
1560     if( this == dom )
1561       return NULL;

1562 
1563     if( dist > 2 )              // Add to count of NULL checks elided
1564       explicit_null_checks_elided++;
1565 
1566   } // End of Else scan for an equivalent test
1567 
1568   // Hit!  Remove this IF

1569 #ifndef PRODUCT
1570   if( TraceIterativeGVN ) {
1571     tty->print("   Removing IfNode: "); this->dump();
1572   }
1573   if( VerifyOpto && !phase->allow_progress() ) {
1574     // Found an equivalent dominating test,
1575     // we can not guarantee reaching a fix-point for these during iterativeGVN
1576     // since intervening nodes may not change.
1577     return NULL;
1578   }
1579 #endif
1580 
1581   // Replace dominated IfNode
1582   dominated_by( prev_dom, igvn );
1583 
1584   // Must return either the original node (now dead) or a new node
1585   // (Do not return a top here, since that would break the uniqueness of top.)
1586   return new ConINode(TypeInt::ZERO);
1587 }
1588 
1589 //------------------------------dominated_by-----------------------------------
1590 void IfNode::dominated_by( Node *prev_dom, PhaseIterGVN *igvn ) {
1591   igvn->hash_delete(this);      // Remove self to prevent spurious V-N
1592   Node *idom = in(0);
1593   // Need opcode to decide which way 'this' test goes
1594   int prev_op = prev_dom->Opcode();
1595   Node *top = igvn->C->top(); // Shortcut to top
1596 
1597   // Loop predicates may have depending checks which should not
1598   // be skipped. For example, range check predicate has two checks
1599   // for lower and upper bounds.
1600   ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1601   if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL)
1602    prev_dom = idom;
1603 
1604   // Now walk the current IfNode's projections.
1605   // Loop ends when 'this' has no more uses.
1606   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1607     Node *ifp = last_out(i);     // Get IfTrue/IfFalse
1608     igvn->add_users_to_worklist(ifp);
1609     // Check which projection it is and set target.
1610     // Data-target is either the dominating projection of the same type


1618     // For each child of an IfTrue/IfFalse projection, reroute.
1619     // Loop ends when projection has no more uses.
1620     for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1621       Node* s = ifp->last_out(j);   // Get child of IfTrue/IfFalse
1622       if( !s->depends_only_on_test() ) {
1623         // Find the control input matching this def-use edge.
1624         // For Regions it may not be in slot 0.
1625         uint l;
1626         for( l = 0; s->in(l) != ifp; l++ ) { }
1627         igvn->replace_input_of(s, l, ctrl_target);
1628       } else {                      // Else, for control producers,
1629         igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1630       }
1631     } // End for each child of a projection
1632 
1633     igvn->remove_dead_node(ifp);
1634   } // End for each IfTrue/IfFalse child of If
1635 
1636   // Kill the IfNode
1637   igvn->remove_dead_node(this);
































1638 }
1639 
1640 //------------------------------Identity---------------------------------------
1641 // If the test is constant & we match, then we are the input Control
1642 Node *IfProjNode::Identity(PhaseTransform *phase) {
1643   // Can only optimize if cannot go the other way
1644   const TypeTuple *t = phase->type(in(0))->is_tuple();
1645   if (t == TypeTuple::IFNEITHER ||
1646       // kill dead branch first otherwise the IfNode's control will
1647       // have 2 control uses (the IfNode that doesn't go away because
1648       // it still has uses and this branch of the
1649       // If). Node::has_special_unique_user() will cause this node to
1650       // be reprocessed once the dead branch is killed.
1651       (always_taken(t) && in(0)->outcnt() == 1)) {
1652     // IfNode control
1653     return in(0)->in(0);
1654   }
1655   // no progress
1656   return this;
1657 }


1709   // whether they are testing a 'gt' or 'lt' condition.  The 'gt' condition
1710   // happens in count-down loops
1711   if (iff->is_CountedLoopEnd())  return NULL;
1712   if (!iff->in(1)->is_Bool())  return NULL; // Happens for partially optimized IF tests
1713   BoolNode *b = iff->in(1)->as_Bool();
1714   BoolTest bt = b->_test;
1715   // Test already in good order?
1716   if( bt.is_canonical() )
1717     return NULL;
1718 
1719   // Flip test to be canonical.  Requires flipping the IfFalse/IfTrue and
1720   // cloning the IfNode.
1721   Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1722   if( !new_b->is_Bool() ) return NULL;
1723   b = new_b->as_Bool();
1724 
1725   PhaseIterGVN *igvn = phase->is_IterGVN();
1726   assert( igvn, "Test is not canonical in parser?" );
1727 
1728   // The IF node never really changes, but it needs to be cloned
1729   iff = new IfNode( iff->in(0), b, 1.0-iff->_prob, iff->_fcnt);


1730 
1731   Node *prior = igvn->hash_find_insert(iff);
1732   if( prior ) {
1733     igvn->remove_dead_node(iff);
1734     iff = (IfNode*)prior;
1735   } else {
1736     // Cannot call transform on it just yet
1737     igvn->set_type_bottom(iff);
1738   }
1739   igvn->_worklist.push(iff);
1740 
1741   // Now handle projections.  Cloning not required.
1742   Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1743   Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1744 
1745   igvn->register_new_node_with_optimizer(new_if_f);
1746   igvn->register_new_node_with_optimizer(new_if_t);
1747   // Flip test, so flip trailing control
1748   igvn->replace_node(old_if_f, new_if_t);
1749   igvn->replace_node(old_if_t, new_if_f);
1750 
1751   // Progress
1752   return iff;




































































































































































1753 }


 289   igvn->register_new_node_with_optimizer( region_c );
 290   igvn->register_new_node_with_optimizer( region_x );
 291   // Prevent the untimely death of phi_x.  Currently he has no uses.  He is
 292   // about to get one.  If this only use goes away, then phi_x will look dead.
 293   // However, he will be picking up some more uses down below.
 294   Node *hook = new Node(4);
 295   hook->init_req(0, phi_x);
 296   hook->init_req(1, phi_c);
 297   phi_x = phase->transform( phi_x );
 298 
 299   // Make the compare
 300   Node *cmp_c = phase->makecon(t);
 301   Node *cmp_x = cmp->clone();
 302   cmp_x->set_req(1,phi_x);
 303   cmp_x->set_req(2,con2);
 304   cmp_x = phase->transform(cmp_x);
 305   // Make the bool
 306   Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test));
 307   Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test));
 308   // Make the IfNode
 309   IfNode* iff_c = iff->clone()->as_If();
 310   iff_c->set_req(0, region_c);
 311   iff_c->set_req(1, b_c);
 312   igvn->set_type_bottom(iff_c);
 313   igvn->_worklist.push(iff_c);
 314   hook->init_req(2, iff_c);
 315 
 316   IfNode* iff_x = iff->clone()->as_If();
 317   iff_x->set_req(0, region_x);
 318   iff_x->set_req(1, b_x);
 319   igvn->set_type_bottom(iff_x);
 320   igvn->_worklist.push(iff_x);
 321   hook->init_req(3, iff_x);
 322 
 323   // Make the true/false arms
 324   Node *iff_c_t = phase->transform(new IfTrueNode (iff_c));
 325   Node *iff_c_f = phase->transform(new IfFalseNode(iff_c));
 326   if (predicate_c != NULL) {
 327     assert(predicate_x == NULL, "only one predicate entry expected");
 328     // Clone loop predicates to each path
 329     iff_c_t = igvn->clone_loop_predicates(predicate_c, iff_c_t, !counted_loop);
 330     iff_c_f = igvn->clone_loop_predicates(predicate_c, iff_c_f, !counted_loop);
 331   }
 332   Node *iff_x_t = phase->transform(new IfTrueNode (iff_x));
 333   Node *iff_x_f = phase->transform(new IfFalseNode(iff_x));
 334   if (predicate_x != NULL) {
 335     assert(predicate_c == NULL, "only one predicate entry expected");
 336     // Clone loop predicates to each path
 337     iff_x_t = igvn->clone_loop_predicates(predicate_x, iff_x_t, !counted_loop);
 338     iff_x_f = igvn->clone_loop_predicates(predicate_x, iff_x_f, !counted_loop);


 483     flip_test = 2;
 484   } else if (bn->_test._test != BoolTest::lt) {
 485     return NULL;
 486   }
 487   if (l->is_top())  return NULL;   // Top input means dead test
 488   if (r->Opcode() != Op_LoadRange)  return NULL;
 489 
 490   // We have recognized one of these forms:
 491   //  Flip 1:  If (Bool[<] CmpU(l, LoadRange)) ...
 492   //  Flip 2:  If (Bool[<=] CmpU(LoadRange, l)) ...
 493 
 494   ProjNode* iftrap = proj_out(flip_test == 2 ? true : false);
 495   return iftrap;
 496 }
 497 
 498 
 499 //------------------------------is_range_check---------------------------------
 500 // Return 0 if not a range check.  Return 1 if a range check and set index and
 501 // offset.  Return 2 if we had to negate the test.  Index is NULL if the check
 502 // is versus a constant.
 503 int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) {
 504   int flip_test = 0;
 505   Node* l = NULL;
 506   Node* r = NULL;
 507   ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
 508 
 509   if (iftrap == NULL) {
 510     return 0;
 511   }
 512 
 513   // Make sure it's a real range check by requiring an uncommon trap
 514   // along the OOB path.  Otherwise, it's possible that the user wrote
 515   // something which optimized to look like a range check but behaves
 516   // in some other way.
 517   if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == NULL) {
 518     return 0;
 519   }
 520 
 521   // Look for index+offset form
 522   Node* ind = l;
 523   jint  off = 0;


 711 //           /      unc
 712 //
 713 
 714 // Is the comparison for this If suitable for folding?
 715 bool IfNode::cmpi_folds(PhaseIterGVN* igvn) {
 716   return in(1) != NULL &&
 717     in(1)->is_Bool() &&
 718     in(1)->in(1) != NULL &&
 719     in(1)->in(1)->Opcode() == Op_CmpI &&
 720     in(1)->in(1)->in(2) != NULL &&
 721     in(1)->in(1)->in(2) != igvn->C->top() &&
 722     (in(1)->as_Bool()->_test.is_less() ||
 723      in(1)->as_Bool()->_test.is_greater());
 724 }
 725 
 726 // Is a dominating control suitable for folding with this if?
 727 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
 728   return ctrl != NULL &&
 729     ctrl->is_Proj() &&
 730     ctrl->in(0) != NULL &&
 731     ctrl->in(0)->Opcode() == Op_If &&
 732     ctrl->in(0)->outcnt() == 2 &&
 733     ctrl->in(0)->as_If()->cmpi_folds(igvn) &&
 734     // Must compare same value
 735     ctrl->in(0)->in(1)->in(1)->in(1) != NULL &&
 736     ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
 737 }
 738 
 739 // Do this If and the dominating If share a region?
 740 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
 741   ProjNode* otherproj = proj->other_if_proj();
 742   Node* otherproj_ctrl_use = otherproj->unique_ctrl_out();
 743   RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL;
 744   success = NULL;
 745   fail = NULL;
 746 
 747   if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) {
 748     for (int i = 0; i < 2; i++) {
 749       ProjNode* proj = proj_out(i);
 750       if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
 751         success = proj;


 959     } else if (lo_test == BoolTest::le) {
 960       if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
 961         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 962         cond = BoolTest::ge;
 963       } else {
 964         assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test");
 965         adjusted_lim = igvn->transform(new SubINode(hi, lo));
 966         lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
 967         cond = BoolTest::ge;
 968       }
 969     }
 970   } else {
 971     const TypeInt* failtype  = filtered_int_type(igvn, n, proj);
 972     if (failtype != NULL) {
 973       const TypeInt* type2 = filtered_int_type(igvn, n, fail);
 974       if (type2 != NULL) {
 975         failtype = failtype->join(type2)->is_int();
 976         if (failtype->_lo > failtype->_hi) {
 977           // previous if determines the result of this if so
 978           // replace Bool with constant
 979           igvn->_worklist.push(in(1));
 980           igvn->replace_input_of(this, 1, igvn->intcon(success->_con));
 981           return true;
 982         }
 983       }
 984     }
 985     lo = NULL;
 986     hi = NULL;
 987   }
 988 
 989   if (lo && hi) {
 990     // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
 991     Node* adjusted_val = igvn->transform(new SubINode(n,  lo));
 992     if (adjusted_lim == NULL) {
 993       adjusted_lim = igvn->transform(new SubINode(hi, lo));
 994     }
 995     Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
 996     Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
 997 
 998     igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
 999     igvn->_worklist.push(in(1));
1000     igvn->replace_input_of(this, 1, newbool);
1001 
1002     return true;
1003   }
1004   return false;
1005 }
1006 
1007 // Merge the branches that trap for this If and the dominating If into
1008 // a single region that branches to the uncommon trap for the
1009 // dominating If
1010 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
1011   Node* res = this;
1012   assert(success->in(0) == this, "bad projection");
1013 
1014   ProjNode* otherproj = proj->other_if_proj();
1015 
1016   CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
1017   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
1018 
1019   if (unc != dom_unc) {
1020     Node* r = new RegionNode(3);
1021 
1022     r->set_req(1, otherproj);
1023     r->set_req(2, success);
1024     r = igvn->transform(r);
1025     assert(r->is_Region(), "can't go away");
1026 
1027     // Make both If trap at the state of the first If: once the CmpI
1028     // nodes are merged, if we trap we don't know which of the CmpI
1029     // nodes would have caused the trap so we have to restart
1030     // execution at the first one
1031     igvn->replace_input_of(dom_unc, 0, r);
1032     igvn->replace_input_of(unc, 0, igvn->C->top());
1033   }
1034   int trap_request = dom_unc->uncommon_trap_request();
1035   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1036   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
1037 
1038   int flip_test = 0;
1039   Node* l = NULL;
1040   Node* r = NULL;
1041 
1042   if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) {
1043     // If this looks like a range check, change the trap to
1044     // Reason_range_check so the compiler recognizes it as a range
1045     // check and applies the corresponding optimizations
1046     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1047 
1048     improve_address_types(l, r, fail, igvn);
1049     
1050     res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt));
1051   } else if (unc != dom_unc) {
1052     // If we trap we won't know what CmpI would have caused the trap
1053     // so use a special trap reason to mark this pair of CmpI nodes as
1054     // bad candidate for folding. On recompilation we won't fold them
1055     // and we may trap again but this time we'll know what branch
1056     // traps
1057     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1058   }
1059   igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
1060   return res;
1061 }
1062 
1063 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1064 // of a rangecheck on index i, on 64 bit the compares may be followed
1065 // by memory accesses using i as index. In that case, the CmpU tells
1066 // us something about the values taken by i that can help the compiler
1067 // (see Compile::conv_I2X_index())
1068 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1069 #ifdef _LP64
1070   ResourceMark rm;
1071   Node_Stack stack(2);
1072 
1073   assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1074   const TypeInt* array_size = igvn->type(r)->is_int();
1075 
1076   stack.push(l, 0);
1077 
1078   while(stack.size() > 0) {
1079     Node* n = stack.node();
1080     uint start = stack.index();


1234 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1235   if (Opcode() != Op_If) return NULL;
1236 
1237   if (cmpi_folds(igvn)) {
1238     Node* ctrl = in(0);
1239     if (is_ctrl_folds(ctrl, igvn) &&
1240         ctrl->outcnt() == 1) {
1241       // A integer comparison immediately dominated by another integer
1242       // comparison
1243       ProjNode* success = NULL;
1244       ProjNode* fail = NULL;
1245       ProjNode* dom_cmp = ctrl->as_Proj();
1246       if (has_shared_region(dom_cmp, success, fail) &&
1247           // Next call modifies graph so must be last
1248           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1249         return this;
1250       }
1251       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1252           // Next call modifies graph so must be last
1253           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1254         return merge_uncommon_traps(dom_cmp, success, fail, igvn);

1255       }
1256       return NULL;
1257     } else if (ctrl->in(0) != NULL &&
1258                ctrl->in(0)->in(0) != NULL) {
1259       ProjNode* success = NULL;
1260       ProjNode* fail = NULL;
1261       Node* dom = ctrl->in(0)->in(0);
1262       ProjNode* dom_cmp = dom->isa_Proj();
1263       ProjNode* other_cmp = ctrl->isa_Proj();
1264 
1265       // Check if it's an integer comparison dominated by another
1266       // integer comparison with another test in between
1267       if (is_ctrl_folds(dom, igvn) &&
1268           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1269           is_side_effect_free_test(other_cmp, igvn) &&
1270           // Next call modifies graph so must be last
1271           fold_compares_helper(dom_cmp, success, fail, igvn)) {
1272         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1273         return merge_uncommon_traps(dom_cmp, success, fail, igvn);

1274       }
1275     }
1276   }
1277   return NULL;
1278 }
1279 
1280 //------------------------------remove_useless_bool----------------------------
1281 // Check for people making a useless boolean: things like
1282 // if( (x < y ? true : false) ) { ... }
1283 // Replace with if( x < y ) { ... }
1284 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1285   Node *i1 = iff->in(1);
1286   if( !i1->is_Bool() ) return NULL;
1287   BoolNode *bol = i1->as_Bool();
1288 
1289   Node *cmp = bol->in(1);
1290   if( cmp->Opcode() != Op_CmpI ) return NULL;
1291 
1292   // Must be comparing against a bool
1293   const Type *cmp2_t = phase->type( cmp->in(2) );


1334   }
1335   if( true_path == 2 ) {
1336     flip = 1-flip;
1337   }
1338 
1339   Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1340   assert(new_bol != iff->in(1), "must make progress");
1341   iff->set_req(1, new_bol);
1342   // Intervening diamond probably goes dead
1343   phase->C->set_major_progress();
1344   return iff;
1345 }
1346 
1347 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1348 
1349 struct RangeCheck {
1350   Node* ctl;
1351   jint off;
1352 };
1353 
1354 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {



1355   if (remove_dead_region(phase, can_reshape))  return this;
1356   // No Def-Use info?
1357   if (!can_reshape)  return NULL;

1358 
1359   // Don't bother trying to transform a dead if
1360   if (in(0)->is_top())  return NULL;
1361   // Don't bother trying to transform an if with a dead test
1362   if (in(1)->is_top())  return NULL;
1363   // Another variation of a dead test
1364   if (in(1)->is_Con())  return NULL;
1365   // Another variation of a dead if
1366   if (outcnt() < 2)  return NULL;
1367 
1368   // Canonicalize the test.
1369   Node* idt_if = idealize_test(phase, this);
1370   if (idt_if != NULL)  return idt_if;
1371 
1372   // Try to split the IF
1373   PhaseIterGVN *igvn = phase->is_IterGVN();
1374   Node *s = split_if(this, igvn);
1375   if (s != NULL)  return s;
1376 
1377   return NodeSentinel;
1378 }
1379 
1380 //------------------------------Ideal------------------------------------------
1381 // Return a node which is more "ideal" than the current node.  Strip out
1382 // control copies
1383 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1384   Node* res = Ideal_common(phase, can_reshape);
1385   if (res != NodeSentinel) {
1386     return res;
1387   }
1388 
1389   // Check for people making a useless boolean: things like
1390   // if( (x < y ? true : false) ) { ... }
1391   // Replace with if( x < y ) { ... }
1392   Node *bol2 = remove_useless_bool(this, phase);
1393   if( bol2 ) return bol2;
1394 
1395   if (in(0) == NULL) return NULL;     // Dead loop?



































































1396 
1397   PhaseIterGVN *igvn = phase->is_IterGVN();
1398   Node* result = fold_compares(igvn);
1399   if (result != NULL) {
1400     return result;





































































1401   }
1402 
1403   // Scan for an equivalent test

1404   Node *cmp;
1405   int dist = 0;               // Cutoff limit for search
1406   int op = Opcode();
1407   if( op == Op_If &&
1408       (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
1409     if( cmp->in(2) != NULL && // make sure cmp is not already dead
1410         cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
1411       dist = 64;              // Limit for null-pointer scans
1412     } else {
1413       dist = 4;               // Do not bother for random pointer tests
1414     }
1415   } else {
1416     dist = 4;                 // Limit for random junky scans
1417   }
1418 
1419   Node* prev_dom = search_identical(dist);

1420 
1421   if (prev_dom == NULL) {



















1422     return NULL;
1423   }
1424 
1425   // Replace dominated IfNode
1426   return dominated_by(prev_dom, igvn);
1427 }

1428 
1429 //------------------------------dominated_by-----------------------------------
1430 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN *igvn) {
1431 #ifndef PRODUCT
1432   if (TraceIterativeGVN) {
1433     tty->print("   Removing IfNode: "); this->dump();
1434   }
1435   if (VerifyOpto && !igvn->allow_progress()) {
1436     // Found an equivalent dominating test,
1437     // we can not guarantee reaching a fix-point for these during iterativeGVN
1438     // since intervening nodes may not change.
1439     return NULL;
1440   }
1441 #endif
1442 










1443   igvn->hash_delete(this);      // Remove self to prevent spurious V-N
1444   Node *idom = in(0);
1445   // Need opcode to decide which way 'this' test goes
1446   int prev_op = prev_dom->Opcode();
1447   Node *top = igvn->C->top(); // Shortcut to top
1448 
1449   // Loop predicates may have depending checks which should not
1450   // be skipped. For example, range check predicate has two checks
1451   // for lower and upper bounds.
1452   ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1453   if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL)
1454    prev_dom = idom;
1455 
1456   // Now walk the current IfNode's projections.
1457   // Loop ends when 'this' has no more uses.
1458   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1459     Node *ifp = last_out(i);     // Get IfTrue/IfFalse
1460     igvn->add_users_to_worklist(ifp);
1461     // Check which projection it is and set target.
1462     // Data-target is either the dominating projection of the same type


1470     // For each child of an IfTrue/IfFalse projection, reroute.
1471     // Loop ends when projection has no more uses.
1472     for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1473       Node* s = ifp->last_out(j);   // Get child of IfTrue/IfFalse
1474       if( !s->depends_only_on_test() ) {
1475         // Find the control input matching this def-use edge.
1476         // For Regions it may not be in slot 0.
1477         uint l;
1478         for( l = 0; s->in(l) != ifp; l++ ) { }
1479         igvn->replace_input_of(s, l, ctrl_target);
1480       } else {                      // Else, for control producers,
1481         igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1482       }
1483     } // End for each child of a projection
1484 
1485     igvn->remove_dead_node(ifp);
1486   } // End for each IfTrue/IfFalse child of If
1487 
1488   // Kill the IfNode
1489   igvn->remove_dead_node(this);
1490 
1491   // Must return either the original node (now dead) or a new node
1492   // (Do not return a top here, since that would break the uniqueness of top.)
1493   return new ConINode(TypeInt::ZERO);
1494 }
1495 
1496 Node* IfNode::search_identical(int dist) {
1497   // Setup to scan up the CFG looking for a dominating test
1498   Node* dom = in(0);
1499   Node* prev_dom = this;
1500   int op = Opcode();
1501   // Search up the dominator tree for an If with an identical test
1502   while( dom->Opcode() != op    ||  // Not same opcode?
1503          dom->in(1)    != in(1) ||  // Not same input 1?
1504          (req() == 3 && dom->in(2) != in(2)) || // Not same input 2?
1505          prev_dom->in(0) != dom ) { // One path of test does not dominate?
1506     if( dist < 0 ) return NULL;
1507 
1508     dist--;
1509     prev_dom = dom;
1510     dom = up_one_dom( dom );
1511     if( !dom ) return NULL;
1512   }
1513 
1514   // Check that we did not follow a loop back to ourselves
1515   if( this == dom )
1516     return NULL;
1517 
1518   if( dist > 2 )              // Add to count of NULL checks elided
1519     explicit_null_checks_elided++;
1520   
1521   return prev_dom;
1522 }
1523 
1524 //------------------------------Identity---------------------------------------
1525 // If the test is constant & we match, then we are the input Control
1526 Node *IfProjNode::Identity(PhaseTransform *phase) {
1527   // Can only optimize if cannot go the other way
1528   const TypeTuple *t = phase->type(in(0))->is_tuple();
1529   if (t == TypeTuple::IFNEITHER ||
1530       // kill dead branch first otherwise the IfNode's control will
1531       // have 2 control uses (the IfNode that doesn't go away because
1532       // it still has uses and this branch of the
1533       // If). Node::has_special_unique_user() will cause this node to
1534       // be reprocessed once the dead branch is killed.
1535       (always_taken(t) && in(0)->outcnt() == 1)) {
1536     // IfNode control
1537     return in(0)->in(0);
1538   }
1539   // no progress
1540   return this;
1541 }


1593   // whether they are testing a 'gt' or 'lt' condition.  The 'gt' condition
1594   // happens in count-down loops
1595   if (iff->is_CountedLoopEnd())  return NULL;
1596   if (!iff->in(1)->is_Bool())  return NULL; // Happens for partially optimized IF tests
1597   BoolNode *b = iff->in(1)->as_Bool();
1598   BoolTest bt = b->_test;
1599   // Test already in good order?
1600   if( bt.is_canonical() )
1601     return NULL;
1602 
1603   // Flip test to be canonical.  Requires flipping the IfFalse/IfTrue and
1604   // cloning the IfNode.
1605   Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1606   if( !new_b->is_Bool() ) return NULL;
1607   b = new_b->as_Bool();
1608 
1609   PhaseIterGVN *igvn = phase->is_IterGVN();
1610   assert( igvn, "Test is not canonical in parser?" );
1611 
1612   // The IF node never really changes, but it needs to be cloned
1613   iff = iff->clone()->as_If();
1614   iff->set_req(1, b);
1615   iff->_prob = 1.0-iff->_prob;
1616 
1617   Node *prior = igvn->hash_find_insert(iff);
1618   if( prior ) {
1619     igvn->remove_dead_node(iff);
1620     iff = (IfNode*)prior;
1621   } else {
1622     // Cannot call transform on it just yet
1623     igvn->set_type_bottom(iff);
1624   }
1625   igvn->_worklist.push(iff);
1626 
1627   // Now handle projections.  Cloning not required.
1628   Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1629   Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1630 
1631   igvn->register_new_node_with_optimizer(new_if_f);
1632   igvn->register_new_node_with_optimizer(new_if_t);
1633   // Flip test, so flip trailing control
1634   igvn->replace_node(old_if_f, new_if_t);
1635   igvn->replace_node(old_if_t, new_if_f);
1636 
1637   // Progress
1638   return iff;
1639 }
1640 
1641 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1642   Node* res = Ideal_common(phase, can_reshape);
1643   if (res != NodeSentinel) {
1644     return res;
1645   }
1646 
1647   PhaseIterGVN *igvn = phase->is_IterGVN();
1648   // Setup to scan up the CFG looking for a dominating test
1649   Node* prev_dom = this;
1650 
1651   // Check for range-check vs other kinds of tests
1652   Node* index1;
1653   Node* range1;
1654   jint offset1;
1655   int flip1 = is_range_check(range1, index1, offset1);
1656   if (flip1) {
1657     Node* dom = in(0);
1658     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
1659     // so all checks we inspect post-dominate the top-most check we find.
1660     // If we are going to fail the current check and we reach the top check
1661     // then we are guaranteed to fail, so just start interpreting there.
1662     // We 'expand' the top 3 range checks to include all post-dominating
1663     // checks.
1664 
1665     // The top 3 range checks seen
1666     const int NRC =3;
1667     RangeCheck prev_checks[NRC];
1668     int nb_checks = 0;
1669 
1670     // Low and high offsets seen so far
1671     jint off_lo = offset1;
1672     jint off_hi = offset1;
1673 
1674     bool found_immediate_dominator = false;
1675 
1676     // Scan for the top checks and collect range of offsets
1677     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1678       if (dom->Opcode() == Op_RangeCheck &&  // Not same opcode?
1679           prev_dom->in(0) == dom) { // One path of test does dominate?
1680         if (dom == this) return NULL; // dead loop
1681         // See if this is a range check
1682         Node* index2;
1683         Node* range2;
1684         jint offset2;
1685         int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
1686         // See if this is a _matching_ range check, checking against
1687         // the same array bounds.
1688         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1689             dom->outcnt() == 2) {
1690           if (nb_checks == 0 && dom->in(1) == in(1)) {
1691             // Found an immediately dominating test at the same offset.
1692             // This kind of back-to-back test can be eliminated locally,
1693             // and there is no need to search further for dominating tests.
1694             assert(offset2 == offset1, "Same test but different offsets");
1695             found_immediate_dominator = true;
1696             break;
1697           }
1698           // Gather expanded bounds
1699           off_lo = MIN2(off_lo,offset2);
1700           off_hi = MAX2(off_hi,offset2);
1701           // Record top NRC range checks
1702           prev_checks[nb_checks%NRC].ctl = prev_dom;
1703           prev_checks[nb_checks%NRC].off = offset2;
1704           nb_checks++;
1705         }
1706       }
1707       prev_dom = dom;
1708       dom = up_one_dom(dom);
1709       if (!dom) break;
1710     }
1711 
1712     if (!found_immediate_dominator) {
1713       // Attempt to widen the dominating range check to cover some later
1714       // ones.  Since range checks "fail" by uncommon-trapping to the
1715       // interpreter, widening a check can make us speculatively enter
1716       // the interpreter.  If we see range-check deopt's, do not widen!
1717       if (!phase->C->allow_range_check_smearing())  return NULL;
1718 
1719       // Didn't find prior covering check, so cannot remove anything.
1720       if (nb_checks == 0) {
1721         return NULL;
1722       }
1723       // Constant indices only need to check the upper bound.
1724       // Non-constant indices must check both low and high.
1725       int chk0 = (nb_checks - 1) % NRC;
1726       if (index1) {
1727         if (nb_checks == 1) {
1728           return NULL;
1729         } else {
1730           // If the top range check's constant is the min or max of
1731           // all constants we widen the next one to cover the whole
1732           // range of constants.
1733           RangeCheck rc0 = prev_checks[chk0];
1734           int chk1 = (nb_checks - 2) % NRC;
1735           RangeCheck rc1 = prev_checks[chk1];
1736           if (rc0.off == off_lo) {
1737             adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1738             prev_dom = rc1.ctl;
1739           } else if (rc0.off == off_hi) {
1740             adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1741             prev_dom = rc1.ctl;
1742           } else {
1743             // If the top test's constant is not the min or max of all
1744             // constants, we need 3 range checks. We must leave the
1745             // top test unchanged because widening it would allow the
1746             // accesses it protects to successfully read/write out of
1747             // bounds.
1748             if (nb_checks == 2) {
1749               return NULL;
1750             }
1751             int chk2 = (nb_checks - 3) % NRC;
1752             RangeCheck rc2 = prev_checks[chk2];
1753             // The top range check a+i covers interval: -a <= i < length-a
1754             // The second range check b+i covers interval: -b <= i < length-b
1755             if (rc1.off <= rc0.off) {
1756               // if b <= a, we change the second range check to:
1757               // -min_of_all_constants <= i < length-min_of_all_constants
1758               // Together top and second range checks now cover:
1759               // -min_of_all_constants <= i < length-a
1760               // which is more restrictive than -b <= i < length-b:
1761               // -b <= -min_of_all_constants <= i < length-a <= length-b
1762               // The third check is then changed to:
1763               // -max_of_all_constants <= i < length-max_of_all_constants
1764               // so 2nd and 3rd checks restrict allowed values of i to:
1765               // -min_of_all_constants <= i < length-max_of_all_constants
1766               adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1767               adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1768             } else {
1769               // if b > a, we change the second range check to:
1770               // -max_of_all_constants <= i < length-max_of_all_constants
1771               // Together top and second range checks now cover:
1772               // -a <= i < length-max_of_all_constants
1773               // which is more restrictive than -b <= i < length-b:
1774               // -b < -a <= i < length-max_of_all_constants <= length-b
1775               // The third check is then changed to:
1776               // -max_of_all_constants <= i < length-max_of_all_constants
1777               // so 2nd and 3rd checks restrict allowed values of i to:
1778               // -min_of_all_constants <= i < length-max_of_all_constants
1779               adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1780               adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1781             }
1782             prev_dom = rc2.ctl;
1783           }
1784         }
1785       } else {
1786         RangeCheck rc0 = prev_checks[chk0];
1787         // 'Widen' the offset of the 1st and only covering check
1788         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1789         // Test is now covered by prior checks, dominate it out
1790         prev_dom = rc0.ctl;
1791       }
1792     }
1793   } else {
1794     prev_dom = search_identical(4);
1795 
1796     if (prev_dom == NULL) {
1797       return NULL;
1798     }
1799   }
1800 
1801   // Replace dominated IfNode
1802   return dominated_by(prev_dom, igvn);
1803 }
src/share/vm/opto/ifnode.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File