hotspot/src/share/vm/opto/ifnode.cpp
changeset 34164 a9e6034d7707
parent 34151 8e0bebdbc29e
child 34180 f0ec91019db2
child 34177 e19e45b065c6
equal deleted inserted replaced
34163:513739bb6c10 34164:a9e6034d7707
   304   cmp_x = phase->transform(cmp_x);
   304   cmp_x = phase->transform(cmp_x);
   305   // Make the bool
   305   // Make the bool
   306   Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test));
   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));
   307   Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test));
   308   // Make the IfNode
   308   // Make the IfNode
   309   IfNode *iff_c = new IfNode(region_c,b_c,iff->_prob,iff->_fcnt);
   309   IfNode* iff_c = iff->clone()->as_If();
       
   310   iff_c->set_req(0, region_c);
       
   311   iff_c->set_req(1, b_c);
   310   igvn->set_type_bottom(iff_c);
   312   igvn->set_type_bottom(iff_c);
   311   igvn->_worklist.push(iff_c);
   313   igvn->_worklist.push(iff_c);
   312   hook->init_req(2, iff_c);
   314   hook->init_req(2, iff_c);
   313 
   315 
   314   IfNode *iff_x = new IfNode(region_x,b_x,iff->_prob, iff->_fcnt);
   316   IfNode* iff_x = iff->clone()->as_If();
       
   317   iff_x->set_req(0, region_x);
       
   318   iff_x->set_req(1, b_x);
   315   igvn->set_type_bottom(iff_x);
   319   igvn->set_type_bottom(iff_x);
   316   igvn->_worklist.push(iff_x);
   320   igvn->_worklist.push(iff_x);
   317   hook->init_req(3, iff_x);
   321   hook->init_req(3, iff_x);
   318 
   322 
   319   // Make the true/false arms
   323   // Make the true/false arms
   494 
   498 
   495 //------------------------------is_range_check---------------------------------
   499 //------------------------------is_range_check---------------------------------
   496 // Return 0 if not a range check.  Return 1 if a range check and set index and
   500 // 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
   501 // offset.  Return 2 if we had to negate the test.  Index is NULL if the check
   498 // is versus a constant.
   502 // is versus a constant.
   499 int IfNode::is_range_check(Node* &range, Node* &index, jint &offset) {
   503 int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) {
   500   int flip_test = 0;
   504   int flip_test = 0;
   501   Node* l = NULL;
   505   Node* l = NULL;
   502   Node* r = NULL;
   506   Node* r = NULL;
   503   ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
   507   ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
   504 
   508 
   722 // Is a dominating control suitable for folding with this if?
   726 // Is a dominating control suitable for folding with this if?
   723 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
   727 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
   724   return ctrl != NULL &&
   728   return ctrl != NULL &&
   725     ctrl->is_Proj() &&
   729     ctrl->is_Proj() &&
   726     ctrl->in(0) != NULL &&
   730     ctrl->in(0) != NULL &&
   727     ctrl->in(0)->is_If() &&
   731     ctrl->in(0)->Opcode() == Op_If &&
   728     ctrl->in(0)->outcnt() == 2 &&
   732     ctrl->in(0)->outcnt() == 2 &&
   729     ctrl->in(0)->as_If()->cmpi_folds(igvn) &&
   733     ctrl->in(0)->as_If()->cmpi_folds(igvn) &&
   730     // Must compare same value
   734     // Must compare same value
   731     ctrl->in(0)->in(1)->in(1)->in(1) != NULL &&
   735     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);
   736     ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
   970       if (type2 != NULL) {
   974       if (type2 != NULL) {
   971         failtype = failtype->join(type2)->is_int();
   975         failtype = failtype->join(type2)->is_int();
   972         if (failtype->_lo > failtype->_hi) {
   976         if (failtype->_lo > failtype->_hi) {
   973           // previous if determines the result of this if so
   977           // previous if determines the result of this if so
   974           // replace Bool with constant
   978           // replace Bool with constant
   975           igvn->hash_delete(this);
   979           igvn->_worklist.push(in(1));
   976           set_req(1, igvn->intcon(success->_con));
   980           igvn->replace_input_of(this, 1, igvn->intcon(success->_con));
   977           return true;
   981           return true;
   978         }
   982         }
   979       }
   983       }
   980     }
   984     }
   981     lo = NULL;
   985     lo = NULL;
   990     }
   994     }
   991     Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
   995     Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
   992     Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
   996     Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
   993 
   997 
   994     igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
   998     igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
   995     set_req(1, newbool);
   999     igvn->_worklist.push(in(1));
       
  1000     igvn->replace_input_of(this, 1, newbool);
   996 
  1001 
   997     return true;
  1002     return true;
   998   }
  1003   }
   999   return false;
  1004   return false;
  1000 }
  1005 }
  1001 
  1006 
  1002 // Merge the branches that trap for this If and the dominating If into
  1007 // 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
  1008 // a single region that branches to the uncommon trap for the
  1004 // dominating If
  1009 // dominating If
  1005 void IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
  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 
  1006   ProjNode* otherproj = proj->other_if_proj();
  1014   ProjNode* otherproj = proj->other_if_proj();
  1007 
  1015 
  1008   CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
  1016   CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
  1009   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
  1017   CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
  1010 
  1018 
  1036     // Reason_range_check so the compiler recognizes it as a range
  1044     // Reason_range_check so the compiler recognizes it as a range
  1037     // check and applies the corresponding optimizations
  1045     // check and applies the corresponding optimizations
  1038     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
  1046     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
  1039 
  1047 
  1040     improve_address_types(l, r, fail, igvn);
  1048     improve_address_types(l, r, fail, igvn);
       
  1049 
       
  1050     res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt));
  1041   } else if (unc != dom_unc) {
  1051   } else if (unc != dom_unc) {
  1042     // If we trap we won't know what CmpI would have caused the trap
  1052     // 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
  1053     // 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
  1054     // 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
  1055     // and we may trap again but this time we'll know what branch
  1046     // traps
  1056     // traps
  1047     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
  1057     trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
  1048   }
  1058   }
  1049   igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
  1059   igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
       
  1060   return res;
  1050 }
  1061 }
  1051 
  1062 
  1052 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
  1063 // 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
  1064 // 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
  1065 // by memory accesses using i as index. In that case, the CmpU tells
  1238         return this;
  1249         return this;
  1239       }
  1250       }
  1240       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
  1251       if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
  1241           // Next call modifies graph so must be last
  1252           // Next call modifies graph so must be last
  1242           fold_compares_helper(dom_cmp, success, fail, igvn)) {
  1253           fold_compares_helper(dom_cmp, success, fail, igvn)) {
  1243         merge_uncommon_traps(dom_cmp, success, fail, igvn);
  1254         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
  1244         return this;
       
  1245       }
  1255       }
  1246       return NULL;
  1256       return NULL;
  1247     } else if (ctrl->in(0) != NULL &&
  1257     } else if (ctrl->in(0) != NULL &&
  1248                ctrl->in(0)->in(0) != NULL) {
  1258                ctrl->in(0)->in(0) != NULL) {
  1249       ProjNode* success = NULL;
  1259       ProjNode* success = NULL;
  1258           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
  1268           has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
  1259           is_side_effect_free_test(other_cmp, igvn) &&
  1269           is_side_effect_free_test(other_cmp, igvn) &&
  1260           // Next call modifies graph so must be last
  1270           // Next call modifies graph so must be last
  1261           fold_compares_helper(dom_cmp, success, fail, igvn)) {
  1271           fold_compares_helper(dom_cmp, success, fail, igvn)) {
  1262         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
  1272         reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
  1263         merge_uncommon_traps(dom_cmp, success, fail, igvn);
  1273         return merge_uncommon_traps(dom_cmp, success, fail, igvn);
  1264         return this;
       
  1265       }
  1274       }
  1266     }
  1275     }
  1267   }
  1276   }
  1268   return NULL;
  1277   return NULL;
  1269 }
  1278 }
  1340 struct RangeCheck {
  1349 struct RangeCheck {
  1341   Node* ctl;
  1350   Node* ctl;
  1342   jint off;
  1351   jint off;
  1343 };
  1352 };
  1344 
  1353 
  1345 //------------------------------Ideal------------------------------------------
  1354 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
  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;
  1355   if (remove_dead_region(phase, can_reshape))  return this;
  1350   // No Def-Use info?
  1356   // No Def-Use info?
  1351   if (!can_reshape)  return NULL;
  1357   if (!can_reshape)  return NULL;
  1352   PhaseIterGVN *igvn = phase->is_IterGVN();
       
  1353 
  1358 
  1354   // Don't bother trying to transform a dead if
  1359   // Don't bother trying to transform a dead if
  1355   if (in(0)->is_top())  return NULL;
  1360   if (in(0)->is_top())  return NULL;
  1356   // Don't bother trying to transform an if with a dead test
  1361   // Don't bother trying to transform an if with a dead test
  1357   if (in(1)->is_top())  return NULL;
  1362   if (in(1)->is_top())  return NULL;
  1363   // Canonicalize the test.
  1368   // Canonicalize the test.
  1364   Node* idt_if = idealize_test(phase, this);
  1369   Node* idt_if = idealize_test(phase, this);
  1365   if (idt_if != NULL)  return idt_if;
  1370   if (idt_if != NULL)  return idt_if;
  1366 
  1371 
  1367   // Try to split the IF
  1372   // Try to split the IF
       
  1373   PhaseIterGVN *igvn = phase->is_IterGVN();
  1368   Node *s = split_if(this, igvn);
  1374   Node *s = split_if(this, igvn);
  1369   if (s != NULL)  return s;
  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   }
  1370 
  1388 
  1371   // Check for people making a useless boolean: things like
  1389   // Check for people making a useless boolean: things like
  1372   // if( (x < y ? true : false) ) { ... }
  1390   // if( (x < y ? true : false) ) { ... }
  1373   // Replace with if( x < y ) { ... }
  1391   // Replace with if( x < y ) { ... }
  1374   Node *bol2 = remove_useless_bool(this, phase);
  1392   Node *bol2 = remove_useless_bool(this, phase);
  1375   if( bol2 ) return bol2;
  1393   if( bol2 ) return bol2;
  1376 
  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
       
  1463     // or TOP if the dominating projection is of opposite type.
       
  1464     // Data-target will be used as the new control edge for the non-CFG
       
  1465     // nodes like Casts and Loads.
       
  1466     Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
       
  1467     // Control-target is just the If's immediate dominator or TOP.
       
  1468     Node *ctrl_target = (ifp->Opcode() == prev_op) ?     idom : top;
       
  1469 
       
  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) {
  1377   // Setup to scan up the CFG looking for a dominating test
  1497   // Setup to scan up the CFG looking for a dominating test
  1378   Node *dom = in(0);
  1498   Node* dom = in(0);
  1379   Node *prev_dom = this;
  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 }
       
  1542 
       
  1543 #ifndef PRODUCT
       
  1544 //-------------------------------related---------------------------------------
       
  1545 // An IfProjNode's related node set consists of its input (an IfNode) including
       
  1546 // the IfNode's condition, plus all of its outputs at level 1. In compact mode,
       
  1547 // the restrictions for IfNode apply (see IfNode::rel).
       
  1548 void IfProjNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
       
  1549   Node* ifNode = this->in(0);
       
  1550   in_rel->append(ifNode);
       
  1551   if (compact) {
       
  1552     ifNode->collect_nodes(in_rel, 3, false, true);
       
  1553   } else {
       
  1554     ifNode->collect_nodes_in_all_data(in_rel, false);
       
  1555   }
       
  1556   this->collect_nodes(out_rel, -1, false, false);
       
  1557 }
       
  1558 
       
  1559 //------------------------------dump_spec--------------------------------------
       
  1560 void IfNode::dump_spec(outputStream *st) const {
       
  1561   st->print("P=%f, C=%f",_prob,_fcnt);
       
  1562 }
       
  1563 
       
  1564 //-------------------------------related---------------------------------------
       
  1565 // For an IfNode, the set of related output nodes is just the output nodes till
       
  1566 // depth 2, i.e, the IfTrue/IfFalse projection nodes plus the nodes they refer.
       
  1567 // The related input nodes contain no control nodes, but all data nodes
       
  1568 // pertaining to the condition. In compact mode, the input nodes are collected
       
  1569 // up to a depth of 3.
       
  1570 void IfNode::related(GrowableArray <Node *> *in_rel, GrowableArray <Node *> *out_rel, bool compact) const {
       
  1571   if (compact) {
       
  1572     this->collect_nodes(in_rel, 3, false, true);
       
  1573   } else {
       
  1574     this->collect_nodes_in_all_data(in_rel, false);
       
  1575   }
       
  1576   this->collect_nodes(out_rel, -2, false, false);
       
  1577 }
       
  1578 #endif
       
  1579 
       
  1580 //------------------------------idealize_test----------------------------------
       
  1581 // Try to canonicalize tests better.  Peek at the Cmp/Bool/If sequence and
       
  1582 // come up with a canonical sequence.  Bools getting 'eq', 'gt' and 'ge' forms
       
  1583 // converted to 'ne', 'le' and 'lt' forms.  IfTrue/IfFalse get swapped as
       
  1584 // needed.
       
  1585 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
       
  1586   assert(iff->in(0) != NULL, "If must be live");
       
  1587 
       
  1588   if (iff->outcnt() != 2)  return NULL; // Malformed projections.
       
  1589   Node* old_if_f = iff->proj_out(false);
       
  1590   Node* old_if_t = iff->proj_out(true);
       
  1591 
       
  1592   // CountedLoopEnds want the back-control test to be TRUE, irregardless of
       
  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;
  1380 
  1650 
  1381   // Check for range-check vs other kinds of tests
  1651   // Check for range-check vs other kinds of tests
  1382   Node *index1, *range1;
  1652   Node* index1;
       
  1653   Node* range1;
  1383   jint offset1;
  1654   jint offset1;
  1384   int flip1 = is_range_check(range1, index1, offset1);
  1655   int flip1 = is_range_check(range1, index1, offset1);
  1385   if( flip1 ) {
  1656   if (flip1) {
       
  1657     Node* dom = in(0);
  1386     // Try to remove extra range checks.  All 'up_one_dom' gives up at merges
  1658     // 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.
  1659     // 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
  1660     // 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.
  1661     // then we are guaranteed to fail, so just start interpreting there.
  1390     // We 'expand' the top 3 range checks to include all post-dominating
  1662     // We 'expand' the top 3 range checks to include all post-dominating
  1401 
  1673 
  1402     bool found_immediate_dominator = false;
  1674     bool found_immediate_dominator = false;
  1403 
  1675 
  1404     // Scan for the top checks and collect range of offsets
  1676     // Scan for the top checks and collect range of offsets
  1405     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
  1677     for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
  1406       if (dom->Opcode() == Op_If &&  // Not same opcode?
  1678       if (dom->Opcode() == Op_RangeCheck &&  // Not same opcode?
  1407           prev_dom->in(0) == dom) { // One path of test does dominate?
  1679           prev_dom->in(0) == dom) { // One path of test does dominate?
  1408         if (dom == this) return NULL; // dead loop
  1680         if (dom == this) return NULL; // dead loop
  1409         // See if this is a range check
  1681         // See if this is a range check
  1410         Node *index2, *range2;
  1682         Node* index2;
       
  1683         Node* range2;
  1411         jint offset2;
  1684         jint offset2;
  1412         int flip2 = dom->as_If()->is_range_check(range2, index2, offset2);
  1685         int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
  1413         // See if this is a _matching_ range check, checking against
  1686         // See if this is a _matching_ range check, checking against
  1414         // the same array bounds.
  1687         // the same array bounds.
  1415         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
  1688         if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
  1416             dom->outcnt() == 2) {
  1689             dom->outcnt() == 2) {
  1417           if (nb_checks == 0 && dom->in(1) == in(1)) {
  1690           if (nb_checks == 0 && dom->in(1) == in(1)) {
  1515         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
  1788         adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
  1516         // Test is now covered by prior checks, dominate it out
  1789         // Test is now covered by prior checks, dominate it out
  1517         prev_dom = rc0.ctl;
  1790         prev_dom = rc0.ctl;
  1518       }
  1791       }
  1519     }
  1792     }
  1520 
  1793   } else {
  1521   } else {                      // Scan for an equivalent test
  1794     prev_dom = search_identical(4);
  1522 
  1795 
  1523     Node *cmp;
  1796     if (prev_dom == NULL) {
  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;
  1797       return NULL;
  1562 
  1798     }
  1563     if( dist > 2 )              // Add to count of NULL checks elided
  1799   }
  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 
  1800 
  1581   // Replace dominated IfNode
  1801   // Replace dominated IfNode
  1582   dominated_by( prev_dom, igvn );
  1802   return dominated_by(prev_dom, igvn);
  1583 
  1803 }
  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
       
  1611     // or TOP if the dominating projection is of opposite type.
       
  1612     // Data-target will be used as the new control edge for the non-CFG
       
  1613     // nodes like Casts and Loads.
       
  1614     Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
       
  1615     // Control-target is just the If's immediate dominator or TOP.
       
  1616     Node *ctrl_target = (ifp->Opcode() == prev_op) ?     idom : top;
       
  1617 
       
  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 }
       
  1658 
       
  1659 #ifndef PRODUCT
       
  1660 //-------------------------------related---------------------------------------
       
  1661 // An IfProjNode's related node set consists of its input (an IfNode) including
       
  1662 // the IfNode's condition, plus all of its outputs at level 1. In compact mode,
       
  1663 // the restrictions for IfNode apply (see IfNode::rel).
       
  1664 void IfProjNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
       
  1665   Node* ifNode = this->in(0);
       
  1666   in_rel->append(ifNode);
       
  1667   if (compact) {
       
  1668     ifNode->collect_nodes(in_rel, 3, false, true);
       
  1669   } else {
       
  1670     ifNode->collect_nodes_in_all_data(in_rel, false);
       
  1671   }
       
  1672   this->collect_nodes(out_rel, -1, false, false);
       
  1673 }
       
  1674 
       
  1675 //------------------------------dump_spec--------------------------------------
       
  1676 void IfNode::dump_spec(outputStream *st) const {
       
  1677   st->print("P=%f, C=%f",_prob,_fcnt);
       
  1678 }
       
  1679 
       
  1680 //-------------------------------related---------------------------------------
       
  1681 // For an IfNode, the set of related output nodes is just the output nodes till
       
  1682 // depth 2, i.e, the IfTrue/IfFalse projection nodes plus the nodes they refer.
       
  1683 // The related input nodes contain no control nodes, but all data nodes
       
  1684 // pertaining to the condition. In compact mode, the input nodes are collected
       
  1685 // up to a depth of 3.
       
  1686 void IfNode::related(GrowableArray <Node *> *in_rel, GrowableArray <Node *> *out_rel, bool compact) const {
       
  1687   if (compact) {
       
  1688     this->collect_nodes(in_rel, 3, false, true);
       
  1689   } else {
       
  1690     this->collect_nodes_in_all_data(in_rel, false);
       
  1691   }
       
  1692   this->collect_nodes(out_rel, -2, false, false);
       
  1693 }
       
  1694 #endif
       
  1695 
       
  1696 //------------------------------idealize_test----------------------------------
       
  1697 // Try to canonicalize tests better.  Peek at the Cmp/Bool/If sequence and
       
  1698 // come up with a canonical sequence.  Bools getting 'eq', 'gt' and 'ge' forms
       
  1699 // converted to 'ne', 'le' and 'lt' forms.  IfTrue/IfFalse get swapped as
       
  1700 // needed.
       
  1701 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
       
  1702   assert(iff->in(0) != NULL, "If must be live");
       
  1703 
       
  1704   if (iff->outcnt() != 2)  return NULL; // Malformed projections.
       
  1705   Node* old_if_f = iff->proj_out(false);
       
  1706   Node* old_if_t = iff->proj_out(true);
       
  1707 
       
  1708   // CountedLoopEnds want the back-control test to be TRUE, irregardless of
       
  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 }