< prev index next >

src/hotspot/share/opto/cfgnode.cpp

Print this page

  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "gc/shared/barrierSet.hpp"
  26 #include "gc/shared/c2/barrierSetC2.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/objArrayKlass.hpp"
  30 #include "opto/addnode.hpp"
  31 #include "opto/castnode.hpp"
  32 #include "opto/cfgnode.hpp"
  33 #include "opto/connode.hpp"
  34 #include "opto/convertnode.hpp"

  35 #include "opto/loopnode.hpp"
  36 #include "opto/machnode.hpp"
  37 #include "opto/movenode.hpp"
  38 #include "opto/mulnode.hpp"
  39 #include "opto/narrowptrnode.hpp"
  40 #include "opto/phaseX.hpp"
  41 #include "opto/regalloc.hpp"
  42 #include "opto/regmask.hpp"
  43 #include "opto/runtime.hpp"
  44 #include "opto/subnode.hpp"
  45 #include "opto/vectornode.hpp"
  46 #include "utilities/vmError.hpp"
  47 
  48 // Portions of code courtesy of Clifford Click
  49 
  50 // Optimization - Graph Style
  51 
  52 //=============================================================================
  53 //------------------------------Value------------------------------------------
  54 // Compute the type of the RegionNode.

 503   if (left_path == nullptr || right_path == nullptr) {
 504     return false;
 505   }
 506   Node* diamond_if = left_path->in(0);
 507   if (diamond_if == nullptr || !diamond_if->is_If() || diamond_if != right_path->in(0)) {
 508     // Not an IfNode merging a diamond or TOP.
 509     return false;
 510   }
 511 
 512   // Check for a proper bool/cmp
 513   const Node* bol = diamond_if->in(1);
 514   if (!bol->is_Bool()) {
 515     return false;
 516   }
 517   const Node* cmp = bol->in(1);
 518   if (!cmp->is_Cmp()) {
 519     return false;
 520   }
 521   return true;
 522 }

 523 //------------------------------Ideal------------------------------------------
 524 // Return a node which is more "ideal" than the current node.  Must preserve
 525 // the CFG, but we can still strip out dead paths.
 526 Node *RegionNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 527   if( !can_reshape && !in(0) ) return nullptr;     // Already degraded to a Copy
 528   assert(!in(0) || !in(0)->is_Root(), "not a specially hidden merge");
 529 
 530   // Check for RegionNode with no Phi users and both inputs come from either
 531   // arm of the same IF.  If found, then the control-flow split is useless.
 532   bool has_phis = false;
 533   if (can_reshape) {            // Need DU info to check for Phi users
 534     try_clean_mem_phis(phase->is_IterGVN());
 535     has_phis = (has_phi() != nullptr);       // Cache result
 536 
 537     if (!has_phis) {            // No Phi users?  Nothing merging?
 538       for (uint i = 1; i < req()-1; i++) {
 539         Node *if1 = in(i);
 540         if( !if1 ) continue;
 541         Node *iff = if1->in(0);
 542         if( !iff || !iff->is_If() ) continue;

 949   if (iff1 == iff2) {
 950     igvn->add_users_to_worklist(iff1); // Make sure dead if is eliminated
 951     igvn->replace_input_of(region, idx1, iff1->in(0));
 952     igvn->replace_input_of(region, idx2, igvn->C->top());
 953     return (region == this); // Remove useless if (both projections map to the same control/value)
 954   }
 955   BoolNode* bol1 = iff1->in(1)->isa_Bool();
 956   BoolNode* bol2 = iff2->in(1)->isa_Bool();
 957   if (bol1 == nullptr || bol2 == nullptr) {
 958     return false; // No bool inputs found
 959   }
 960   Node* cmp1 = bol1->in(1);
 961   Node* cmp2 = bol2->in(1);
 962   bool commute = false;
 963   if (!cmp1->is_Cmp() || !cmp2->is_Cmp()) {
 964     return false; // No comparison
 965   } else if (cmp1->Opcode() == Op_CmpF || cmp1->Opcode() == Op_CmpD ||
 966              cmp2->Opcode() == Op_CmpF || cmp2->Opcode() == Op_CmpD ||
 967              cmp1->Opcode() == Op_CmpP || cmp1->Opcode() == Op_CmpN ||
 968              cmp2->Opcode() == Op_CmpP || cmp2->Opcode() == Op_CmpN ||
 969              cmp1->is_SubTypeCheck() || cmp2->is_SubTypeCheck()) {

 970     // Floats and pointers don't exactly obey trichotomy. To be on the safe side, don't transform their tests.
 971     // SubTypeCheck is not commutative
 972     return false;
 973   } else if (cmp1 != cmp2) {
 974     if (cmp1->in(1) == cmp2->in(2) &&
 975         cmp1->in(2) == cmp2->in(1)) {
 976       commute = true; // Same but swapped inputs, commute the test
 977     } else {
 978       return false; // Ifs are not comparing the same values
 979     }
 980   }
 981   proj1 = proj1->other_if_proj();
 982   proj2 = proj2->other_if_proj();
 983   if (!((proj1->unique_ctrl_out_or_null() == iff2 &&
 984          proj2->unique_ctrl_out_or_null() == this) ||
 985         (proj2->unique_ctrl_out_or_null() == iff1 &&
 986          proj1->unique_ctrl_out_or_null() == this))) {
 987     return false; // Ifs are not connected through other projs
 988   }
 989   // Found 'iff -> proj -> iff -> proj -> this' shape where all other projs are merged

1028     st->print("#reducible ");
1029     break;
1030   case RegionNode::LoopStatus::NeverIrreducibleEntry:
1031     break; // nothing
1032   }
1033 }
1034 #endif
1035 
1036 // Find the one non-null required input.  RegionNode only
1037 Node *Node::nonnull_req() const {
1038   assert( is_Region(), "" );
1039   for( uint i = 1; i < _cnt; i++ )
1040     if( in(i) )
1041       return in(i);
1042   ShouldNotReachHere();
1043   return nullptr;
1044 }
1045 
1046 
1047 //=============================================================================
1048 // note that these functions assume that the _adr_type field is flattened
1049 uint PhiNode::hash() const {
1050   const Type* at = _adr_type;
1051   return TypeNode::hash() + (at ? at->hash() : 0);
1052 }
1053 bool PhiNode::cmp( const Node &n ) const {
1054   return TypeNode::cmp(n) && _adr_type == ((PhiNode&)n)._adr_type;
1055 }
1056 static inline
1057 const TypePtr* flatten_phi_adr_type(const TypePtr* at) {
1058   if (at == nullptr || at == TypePtr::BOTTOM)  return at;
1059   return Compile::current()->alias_type(at)->adr_type();
1060 }
1061 
1062 //----------------------------make---------------------------------------------
1063 // create a new phi with edges matching r and set (initially) to x
1064 PhiNode* PhiNode::make(Node* r, Node* x, const Type *t, const TypePtr* at) {
1065   uint preds = r->req();   // Number of predecessor paths
1066   assert(t != Type::MEMORY || at == flatten_phi_adr_type(at), "flatten at");
1067   PhiNode* p = new PhiNode(r, t, at);
1068   for (uint j = 1; j < preds; j++) {
1069     // Fill in all inputs, except those which the region does not yet have
1070     if (r->in(j) != nullptr)
1071       p->init_req(j, x);
1072   }
1073   return p;
1074 }
1075 PhiNode* PhiNode::make(Node* r, Node* x) {
1076   const Type*    t  = x->bottom_type();
1077   const TypePtr* at = nullptr;
1078   if (t == Type::MEMORY)  at = flatten_phi_adr_type(x->adr_type());
1079   return make(r, x, t, at);
1080 }
1081 PhiNode* PhiNode::make_blank(Node* r, Node* x) {
1082   const Type*    t  = x->bottom_type();
1083   const TypePtr* at = nullptr;
1084   if (t == Type::MEMORY)  at = flatten_phi_adr_type(x->adr_type());
1085   return new PhiNode(r, t, at);
1086 }

1175       np->as_Phi()->verify_adr_type(visited, at);
1176     } else if (n->bottom_type() == Type::TOP
1177                || (n->is_Mem() && n->in(MemNode::Address)->bottom_type() == Type::TOP)) {
1178       // ignore top inputs
1179     } else {
1180       const TypePtr* nat = flatten_phi_adr_type(n->adr_type());
1181       // recheck phi/non-phi consistency at leaves:
1182       assert((nat != nullptr) == (at != nullptr), "");
1183       assert(nat == at || nat == TypePtr::BOTTOM,
1184              "adr_type must be consistent at leaves of phi nest");
1185     }
1186   }
1187 }
1188 
1189 // Verify a whole nest of phis rooted at this one.
1190 void PhiNode::verify_adr_type(bool recursive) const {
1191   if (VMError::is_error_reported())  return;  // muzzle asserts when debugging an error
1192   if (Node::in_dump())               return;  // muzzle asserts when printing
1193 
1194   assert((_type == Type::MEMORY) == (_adr_type != nullptr), "adr_type for memory phis only");








1195 
1196   if (!VerifyAliases)       return;  // verify thoroughly only if requested
1197 
1198   assert(_adr_type == flatten_phi_adr_type(_adr_type),
1199          "Phi::adr_type must be pre-normalized");
1200 
1201   if (recursive) {
1202     VectorSet visited;
1203     verify_adr_type(visited, _adr_type);
1204   }
1205 }
1206 #endif
1207 
1208 
1209 //------------------------------Value------------------------------------------
1210 // Compute the type of the PhiNode
1211 const Type* PhiNode::Value(PhaseGVN* phase) const {
1212   Node *r = in(0);              // RegionNode
1213   if( !r )                      // Copy or dead
1214     return in(1) ? phase->type(in(1)) : Type::TOP;

1458   assert(req() == 3, "same as region");
1459   RegionNode* region = in(0)->as_Region();
1460   for (uint i = 1; i < 3; i++) {
1461     Node* phi_input = in(i);
1462     if (phi_input != nullptr && phi_input->is_MergeMem() && region->in(i)->outcnt() == 1) {
1463       // Nothing is control-dependent on path #i except the region itself.
1464       MergeMemNode* merge_mem = phi_input->as_MergeMem();
1465       uint j = 3 - i;
1466       Node* other_phi_input = in(j);
1467       if (other_phi_input != nullptr && other_phi_input == merge_mem->base_memory() && !is_data_loop(region, phi_input, igvn)) {
1468         // merge_mem is a successor memory to other_phi_input, and is not pinned inside the diamond, so push it out.
1469         // Only proceed if the transformation doesn't create a data loop
1470         // This will allow the diamond to collapse completely if there are no other phis left.
1471         igvn->replace_node(this, merge_mem);
1472         return true;
1473       }
1474     }
1475   }
1476   return false;
1477 }

1478 //----------------------------check_cmove_id-----------------------------------
1479 // Check for CMove'ing a constant after comparing against the constant.
1480 // Happens all the time now, since if we compare equality vs a constant in
1481 // the parser, we "know" the variable is constant on one path and we force
1482 // it.  Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
1483 // conditional move: "x = (x==0)?0:x;".  Yucko.  This fix is slightly more
1484 // general in that we don't need constants.  Since CMove's are only inserted
1485 // in very special circumstances, we do it here on generic Phi's.
1486 Node* PhiNode::is_cmove_id(PhaseTransform* phase, int true_path) {
1487   assert(true_path !=0, "only diamond shape graph expected");
1488 
1489   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1490   // phi->region->if_proj->ifnode->bool->cmp
1491   Node*     region = in(0);
1492   Node*     iff    = region->in(1)->in(0);
1493   BoolNode* b      = iff->in(1)->as_Bool();
1494   Node*     cmp    = b->in(1);
1495   Node*     tval   = in(true_path);
1496   Node*     fval   = in(3-true_path);
1497   Node*     id     = CMoveNode::is_cmove_id(phase, cmp, tval, fval, b);

1512   }
1513 
1514   return id;
1515 }
1516 
1517 //------------------------------Identity---------------------------------------
1518 // Check for Region being Identity.
1519 Node* PhiNode::Identity(PhaseGVN* phase) {
1520   if (must_wait_for_region_in_irreducible_loop(phase)) {
1521     return this;
1522   }
1523   // Check for no merging going on
1524   // (There used to be special-case code here when this->region->is_Loop.
1525   // It would check for a tributary phi on the backedge that the main phi
1526   // trivially, perhaps with a single cast.  The unique_input method
1527   // does all this and more, by reducing such tributaries to 'this'.)
1528   Node* uin = unique_input(phase, false);
1529   if (uin != nullptr) {
1530     return uin;
1531   }




1532 
1533   int true_path = is_diamond_phi();
1534   // Delay CMove'ing identity if Ideal has not had the chance to handle unsafe cases, yet.
1535   if (true_path != 0 && !(phase->is_IterGVN() && wait_for_region_igvn(phase))) {
1536     Node* id = is_cmove_id(phase, true_path);
1537     if (id != nullptr) {
1538       return id;
1539     }
1540   }
1541 
1542   // Looking for phis with identical inputs.  If we find one that has
1543   // type TypePtr::BOTTOM, replace the current phi with the bottom phi.
1544   if (phase->is_IterGVN() && type() == Type::MEMORY && adr_type() !=
1545       TypePtr::BOTTOM && !adr_type()->is_known_instance()) {
1546     uint phi_len = req();
1547     Node* phi_reg = region();
1548     for (DUIterator_Fast imax, i = phi_reg->fast_outs(imax); i < imax; i++) {
1549       Node* u = phi_reg->fast_out(i);
1550       assert(!u->is_Phi() || u->in(0) == phi_reg, "broken Phi/Region subgraph");
1551       if (u->is_Phi() && u->req() == phi_len && can_be_replaced_by(u->as_Phi())) {

1602     }
1603     // Check for a unique input (maybe uncasted)
1604     if (input == nullptr) {
1605       input = un;
1606     } else if (input != un) {
1607       input = NodeSentinel; // no unique input
1608     }
1609   }
1610   if (input == nullptr) {
1611     return phase->C->top();        // no inputs
1612   }
1613 
1614   if (input != NodeSentinel) {
1615     return input;           // one unique direct input
1616   }
1617 
1618   // Nothing.
1619   return nullptr;
1620 }
1621 




































1622 //------------------------------is_x2logic-------------------------------------
1623 // Check for simple convert-to-boolean pattern
1624 // If:(C Bool) Region:(IfF IfT) Phi:(Region 0 1)
1625 // Convert Phi to an ConvIB.
1626 static Node *is_x2logic( PhaseGVN *phase, PhiNode *phi, int true_path ) {
1627   assert(true_path !=0, "only diamond shape graph expected");
1628 
1629   // If we're late in the optimization process, we may have already expanded Conv2B nodes
1630   if (phase->C->post_loop_opts_phase() && !Matcher::match_rule_supported(Op_Conv2B)) {
1631     return nullptr;
1632   }
1633 
1634   // Convert the true/false index into an expected 0/1 return.
1635   // Map 2->0 and 1->1.
1636   int flipped = 2-true_path;
1637 
1638   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1639   // phi->region->if_proj->ifnode->bool->cmp
1640   Node *region = phi->in(0);
1641   Node *iff = region->in(1)->in(0);

2069 
2070     if (rc->in(0)->in(1) == nullptr || !rc->in(0)->in(1)->is_Bool()) { continue; }
2071     if (worklist.member(rc->in(0)->in(1))) {
2072       delay = true;
2073       break;
2074     }
2075 
2076     if (rc->in(0)->in(1)->in(1) == nullptr || !rc->in(0)->in(1)->in(1)->is_Cmp()) { continue; }
2077     if (worklist.member(rc->in(0)->in(1)->in(1))) {
2078       delay = true;
2079       break;
2080     }
2081   }
2082 
2083   if (delay) {
2084     worklist.push(this);
2085   }
2086   return delay;
2087 }
2088 

2089 // If the Phi's Region is in an irreducible loop, and the Region
2090 // has had an input removed, but not yet transformed, it could be
2091 // that the Region (and this Phi) are not reachable from Root.
2092 // If we allow the Phi to collapse before the Region, this may lead
2093 // to dead-loop data. Wait for the Region to check for reachability,
2094 // and potentially remove the dead code.
2095 bool PhiNode::must_wait_for_region_in_irreducible_loop(PhaseGVN* phase) const {
2096   RegionNode* region = in(0)->as_Region();
2097   if (region->loop_status() == RegionNode::LoopStatus::MaybeIrreducibleEntry) {
2098     Node* top = phase->C->top();
2099     for (uint j = 1; j < req(); j++) {
2100       Node* rc = region->in(j); // for each control input
2101       if (rc == nullptr || phase->type(rc) == Type::TOP) {
2102         // Region is missing a control input
2103         Node* n = in(j);
2104         if (n != nullptr && n != top) {
2105           // Phi still has its input, so region just lost its input
2106           return true;
2107         }
2108       }

2506     //                Phi (this)   |
2507     //                 |           |
2508     //                 +-----------+
2509     //
2510     // Generally, there are issues with non-termination with such circularity
2511     // (see comment further below). However, if there is a direct loop to self,
2512     // splitting the Phi through the MergeMem will result in the below.
2513     //
2514     //               +---+
2515     //               |   |
2516     //               v   |
2517     //              Phi  |
2518     //               |\  |
2519     //               | +-+
2520     // (base_memory) v
2521     //              MergeMem
2522     //
2523     // This split breaks the circularity and consequently does not lead to
2524     // non-termination.
2525     uint merge_width = 0;


2526     bool split_always_terminates = false; // Is splitting guaranteed to terminate?
2527     for( uint i=1; i<req(); ++i ) {// For all paths in
2528       Node *ii = in(i);
2529       // TOP inputs should not be counted as safe inputs because if the
2530       // Phi references itself through all other inputs then splitting the
2531       // Phi through memory merges would create dead loop at later stage.
2532       if (ii == top) {
2533         return progress; // Delay optimization until graph is cleaned.
2534       }
2535       if (ii->is_MergeMem()) {
2536         MergeMemNode* n = ii->as_MergeMem();
2537         merge_width = MAX2(merge_width, n->req());
2538         if (n->base_memory() == this) {
2539           split_always_terminates = true;
2540         }


2541       }
2542     }
2543 
2544     // There are cases with circular dependencies between bottom Phis
2545     // and MergeMems. Below is a minimal example.
2546     //
2547     //               +------------+
2548     //               |            |
2549     // (base_memory) v            |
2550     //              MergeMem      |
2551     //                 |          |
2552     //                 v          |
2553     //                Phi (this)  |
2554     //                 |          |
2555     //                 v          |
2556     //                Phi         |
2557     //                 |          |
2558     //                 +----------+
2559     //
2560     // Here, we cannot break the circularity through a self-loop as there
2561     // are two Phis involved. Repeatedly splitting the Phis through the
2562     // MergeMem leads to non-termination. We check for non-termination below.
2563     // Only check for non-termination if necessary.
2564     if (!split_always_terminates && adr_type() == TypePtr::BOTTOM &&
2565         merge_width > Compile::AliasIdxRaw) {
2566       split_always_terminates = is_split_through_mergemem_terminating();
2567     }
2568 
2569     if (merge_width > Compile::AliasIdxRaw) {
2570       // found at least one non-empty MergeMem
2571       const TypePtr* at = adr_type();
2572       if (at != TypePtr::BOTTOM) {
2573         // Patch the existing phi to select an input from the merge:
2574         // Phi:AT1(...MergeMem(m0, m1, m2)...) into
2575         //     Phi:AT1(...m1...)
2576         int alias_idx = phase->C->get_alias_index(at);
2577         for (uint i=1; i<req(); ++i) {
2578           Node *ii = in(i);
2579           if (ii->is_MergeMem()) {
2580             MergeMemNode* n = ii->as_MergeMem();
2581             // compress paths and change unreachable cycles to TOP
2582             // If not, we can update the input infinitely along a MergeMem cycle
2583             // Equivalent code is in MemNode::Ideal_common
2584             Node *m  = phase->transform(n);
2585             if (outcnt() == 0) {  // Above transform() may kill us!
2586               return top;
2587             }
2588             // If transformed to a MergeMem, get the desired slice
2589             // Otherwise the returned node represents memory for every slice
2590             Node *new_mem = (m->is_MergeMem()) ?
2591                              m->as_MergeMem()->memory_at(alias_idx) : m;
2592             // Update input if it is progress over what we have now
2593             if (new_mem != ii) {
2594               set_req_X(i, new_mem, phase->is_IterGVN());
2595               progress = this;
2596             }
2597           }
2598         }
2599       } else if (split_always_terminates) {
2600         // If all inputs reference this phi (directly or through data nodes) -
2601         // it is a dead loop.
2602         bool saw_safe_input = false;
2603         for (uint j = 1; j < req(); ++j) {
2604           Node* n = in(j);
2605           if (n->is_MergeMem()) {
2606             MergeMemNode* mm = n->as_MergeMem();
2607             if (mm->base_memory() == this || mm->base_memory() == mm->empty_memory()) {
2608               // Skip this input if it references back to this phi or if the memory path is dead
2609               continue;
2610             }
2611           }
2612           if (!is_unsafe_data_reference(n)) {
2613             saw_safe_input = true; // found safe input
2614             break;
2615           }
2616         }
2617         if (!saw_safe_input) {
2618           // There is a dead loop: All inputs are either dead or reference back to this phi
2619           return top;
2620         }
2621 
2622         // Phi(...MergeMem(m0, m1:AT1, m2:AT2)...) into
2623         //     MergeMem(Phi(...m0...), Phi:AT1(...m1...), Phi:AT2(...m2...))
2624         PhaseIterGVN* igvn = phase->is_IterGVN();
2625         assert(igvn != nullptr, "sanity check");
2626         PhiNode* new_base = (PhiNode*) clone();
2627         // Must eagerly register phis, since they participate in loops.
2628         igvn->register_new_node_with_optimizer(new_base);
2629 
2630         MergeMemNode* result = MergeMemNode::make(new_base);
2631         for (uint i = 1; i < req(); ++i) {
2632           Node *ii = in(i);
2633           if (ii->is_MergeMem()) {
2634             MergeMemNode* n = ii->as_MergeMem();





2635             for (MergeMemStream mms(result, n); mms.next_non_empty2(); ) {
2636               // If we have not seen this slice yet, make a phi for it.
2637               bool made_new_phi = false;
2638               if (mms.is_empty()) {
2639                 Node* new_phi = new_base->slice_memory(mms.adr_type(phase->C));
2640                 made_new_phi = true;
2641                 igvn->register_new_node_with_optimizer(new_phi);
2642                 mms.set_memory(new_phi);
2643               }
2644               Node* phi = mms.memory();
2645               assert(made_new_phi || phi->in(i) == n, "replace the i-th merge by a slice");
2646               phi->set_req_X(i, mms.memory2(), phase);
2647             }
2648           }
2649         }
2650         // Distribute all self-loops.
2651         { // (Extra braces to hide mms.)
2652           for (MergeMemStream mms(result); mms.next_non_empty(); ) {
2653             Node* phi = mms.memory();
2654             for (uint i = 1; i < req(); ++i) {

2663         // result in creating an excessive number of new nodes within a single
2664         // IGVN iteration. We have put the Phi nodes on the IGVN worklist, so
2665         // they are transformed later on in any case.
2666 
2667         // Replace self with the result.
2668         return result;
2669       }
2670     }
2671     //
2672     // Other optimizations on the memory chain
2673     //
2674     const TypePtr* at = adr_type();
2675     for( uint i=1; i<req(); ++i ) {// For all paths in
2676       Node *ii = in(i);
2677       Node *new_in = MemNode::optimize_memory_chain(ii, at, nullptr, phase);
2678       // MemNode::optimize_memory_chain above may kill us!
2679       if (outcnt() == 0) {
2680         return top;
2681       }
2682       if (ii != new_in ) {
2683         set_req_X(i, new_in, phase);
2684         progress = this;
2685       }
2686     }
2687   }
2688 
2689 #ifdef _LP64
2690   // Push DecodeN/DecodeNKlass down through phi.
2691   // The rest of phi graph will transform by split EncodeP node though phis up.
2692   if (can_reshape && progress == nullptr) {
2693     bool may_push = true;
2694     bool has_decodeN = false;
2695     bool is_decodeN = false;
2696     for (uint i=1; i<req(); ++i) {// For all paths in
2697       Node *ii = in(i);
2698       if (ii->is_DecodeNarrowPtr() && ii->bottom_type() == bottom_type()) {
2699         // Do optimization if a non dead path exist.
2700         if (ii->in(1)->bottom_type() != Type::TOP) {
2701           has_decodeN = true;
2702           is_decodeN = ii->is_DecodeN();
2703         }

2731             if (is_decodeN) {
2732               new_ii = new EncodePNode(ii, narrow_t);
2733             } else {
2734               new_ii = new EncodePKlassNode(ii, narrow_t);
2735             }
2736             igvn->register_new_node_with_optimizer(new_ii);
2737           }
2738         }
2739         new_phi->set_req(i, new_ii);
2740       }
2741       igvn->register_new_node_with_optimizer(new_phi, this);
2742       if (is_decodeN) {
2743         progress = new DecodeNNode(new_phi, bottom_type());
2744       } else {
2745         progress = new DecodeNKlassNode(new_phi, bottom_type());
2746       }
2747     }
2748   }
2749 #endif
2750 





2751   // Try to convert a Phi with two duplicated convert nodes into a phi of the pre-conversion type and the convert node
2752   // proceeding the phi, to de-duplicate the convert node and compact the IR.
2753   if (can_reshape && progress == nullptr) {
2754     ConvertNode* convert = in(1)->isa_Convert();
2755     if (convert != nullptr) {
2756       int conv_op = convert->Opcode();
2757       bool ok = true;
2758 
2759       // Check the rest of the inputs
2760       for (uint i = 2; i < req(); i++) {
2761         // Make sure that all inputs are of the same type of convert node
2762         if (in(i)->Opcode() != conv_op) {
2763           ok = false;
2764           break;
2765         }
2766       }
2767 
2768       if (ok) {
2769         // Find the local bottom type to set as the type of the phi
2770         const Type* source_type = Type::get_const_basic_type(convert->in_type()->basic_type());

2793   // only after the non-bottom memory phi is processed by igvn, PhiNode::Identity doesn't run and the transformation
2794   // doesn't happen.
2795   // Look for non-bottom Phis that should be transformed and enqueue them for igvn so that PhiNode::Identity executes for
2796   // them.
2797   if (can_reshape && type() == Type::MEMORY && adr_type() == TypePtr::BOTTOM) {
2798     PhaseIterGVN* igvn = phase->is_IterGVN();
2799     uint phi_len = req();
2800     Node* phi_reg = region();
2801     for (DUIterator_Fast imax, i = phi_reg->fast_outs(imax); i < imax; i++) {
2802       Node* u = phi_reg->fast_out(i);
2803       assert(!u->is_Phi() || (u->in(0) == phi_reg && u->req() == phi_len), "broken Phi/Region subgraph");
2804       if (u->is_Phi() && u->as_Phi()->can_be_replaced_by(this)) {
2805         igvn->_worklist.push(u);
2806       }
2807     }
2808   }
2809 
2810   return progress;              // Return any progress
2811 }
2812 










































































































































































































































































































































































































2813 static int compare_types(const Type* const& e1, const Type* const& e2) {
2814   return (intptr_t)e1 - (intptr_t)e2;
2815 }
2816 
2817 // Collect types at casts that are going to be eliminated at that Phi and store them in a TypeTuple.
2818 // Sort the types using an arbitrary order so a list of some types always hashes to the same TypeTuple (and TypeTuple
2819 // pointer comparison is enough to tell if 2 list of types are the same or not)
2820 const TypeTuple* PhiNode::collect_types(PhaseGVN* phase) const {
2821   const Node* region = in(0);
2822   const Type* phi_type = bottom_type();
2823   ResourceMark rm;
2824   GrowableArray<const Type*> types;
2825   for (uint i = 1; i < req(); i++) {
2826     if (region->in(i) == nullptr || phase->type(region->in(i)) == Type::TOP) {
2827       continue;
2828     }
2829     Node* in = Node::in(i);
2830     const Type* t = phase->type(in);
2831     if (in == nullptr || in == this || t == Type::TOP) {
2832       continue;

3182 #ifndef PRODUCT
3183 void CatchProjNode::dump_spec(outputStream *st) const {
3184   ProjNode::dump_spec(st);
3185   st->print("@bci %d ",_handler_bci);
3186 }
3187 #endif
3188 
3189 //=============================================================================
3190 //------------------------------Identity---------------------------------------
3191 // Check for CreateEx being Identity.
3192 Node* CreateExNode::Identity(PhaseGVN* phase) {
3193   if( phase->type(in(1)) == Type::TOP ) return in(1);
3194   if( phase->type(in(0)) == Type::TOP ) return in(0);
3195   if (phase->type(in(0)->in(0)) == Type::TOP) {
3196     assert(in(0)->is_CatchProj(), "control is CatchProj");
3197     return phase->C->top(); // dead code
3198   }
3199   // We only come from CatchProj, unless the CatchProj goes away.
3200   // If the CatchProj is optimized away, then we just carry the
3201   // exception oop through.






3202   CallNode *call = in(1)->in(0)->as_Call();
3203 
3204   return (in(0)->is_CatchProj() && in(0)->in(0)->is_Catch() &&
3205           in(0)->in(0)->in(1) == in(1)) ? this : call->in(TypeFunc::Parms);
3206 }
3207 
3208 //=============================================================================
3209 //------------------------------Value------------------------------------------
3210 // Check for being unreachable.
3211 const Type* NeverBranchNode::Value(PhaseGVN* phase) const {
3212   if (!in(0) || in(0)->is_top()) return Type::TOP;
3213   return bottom_type();
3214 }
3215 
3216 //------------------------------Ideal------------------------------------------
3217 // Check for no longer being part of a loop
3218 Node *NeverBranchNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3219   if (can_reshape && !in(0)->is_Region()) {
3220     // Dead code elimination can sometimes delete this projection so
3221     // if it's not there, there's nothing to do.

  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "gc/shared/barrierSet.hpp"
  26 #include "gc/shared/c2/barrierSetC2.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/objArrayKlass.hpp"
  30 #include "opto/addnode.hpp"
  31 #include "opto/castnode.hpp"
  32 #include "opto/cfgnode.hpp"
  33 #include "opto/connode.hpp"
  34 #include "opto/convertnode.hpp"
  35 #include "opto/inlinetypenode.hpp"
  36 #include "opto/loopnode.hpp"
  37 #include "opto/machnode.hpp"
  38 #include "opto/movenode.hpp"
  39 #include "opto/mulnode.hpp"
  40 #include "opto/narrowptrnode.hpp"
  41 #include "opto/phaseX.hpp"
  42 #include "opto/regalloc.hpp"
  43 #include "opto/regmask.hpp"
  44 #include "opto/runtime.hpp"
  45 #include "opto/subnode.hpp"
  46 #include "opto/vectornode.hpp"
  47 #include "utilities/vmError.hpp"
  48 
  49 // Portions of code courtesy of Clifford Click
  50 
  51 // Optimization - Graph Style
  52 
  53 //=============================================================================
  54 //------------------------------Value------------------------------------------
  55 // Compute the type of the RegionNode.

 504   if (left_path == nullptr || right_path == nullptr) {
 505     return false;
 506   }
 507   Node* diamond_if = left_path->in(0);
 508   if (diamond_if == nullptr || !diamond_if->is_If() || diamond_if != right_path->in(0)) {
 509     // Not an IfNode merging a diamond or TOP.
 510     return false;
 511   }
 512 
 513   // Check for a proper bool/cmp
 514   const Node* bol = diamond_if->in(1);
 515   if (!bol->is_Bool()) {
 516     return false;
 517   }
 518   const Node* cmp = bol->in(1);
 519   if (!cmp->is_Cmp()) {
 520     return false;
 521   }
 522   return true;
 523 }
 524 
 525 //------------------------------Ideal------------------------------------------
 526 // Return a node which is more "ideal" than the current node.  Must preserve
 527 // the CFG, but we can still strip out dead paths.
 528 Node *RegionNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 529   if( !can_reshape && !in(0) ) return nullptr;     // Already degraded to a Copy
 530   assert(!in(0) || !in(0)->is_Root(), "not a specially hidden merge");
 531 
 532   // Check for RegionNode with no Phi users and both inputs come from either
 533   // arm of the same IF.  If found, then the control-flow split is useless.
 534   bool has_phis = false;
 535   if (can_reshape) {            // Need DU info to check for Phi users
 536     try_clean_mem_phis(phase->is_IterGVN());
 537     has_phis = (has_phi() != nullptr);       // Cache result
 538 
 539     if (!has_phis) {            // No Phi users?  Nothing merging?
 540       for (uint i = 1; i < req()-1; i++) {
 541         Node *if1 = in(i);
 542         if( !if1 ) continue;
 543         Node *iff = if1->in(0);
 544         if( !iff || !iff->is_If() ) continue;

 951   if (iff1 == iff2) {
 952     igvn->add_users_to_worklist(iff1); // Make sure dead if is eliminated
 953     igvn->replace_input_of(region, idx1, iff1->in(0));
 954     igvn->replace_input_of(region, idx2, igvn->C->top());
 955     return (region == this); // Remove useless if (both projections map to the same control/value)
 956   }
 957   BoolNode* bol1 = iff1->in(1)->isa_Bool();
 958   BoolNode* bol2 = iff2->in(1)->isa_Bool();
 959   if (bol1 == nullptr || bol2 == nullptr) {
 960     return false; // No bool inputs found
 961   }
 962   Node* cmp1 = bol1->in(1);
 963   Node* cmp2 = bol2->in(1);
 964   bool commute = false;
 965   if (!cmp1->is_Cmp() || !cmp2->is_Cmp()) {
 966     return false; // No comparison
 967   } else if (cmp1->Opcode() == Op_CmpF || cmp1->Opcode() == Op_CmpD ||
 968              cmp2->Opcode() == Op_CmpF || cmp2->Opcode() == Op_CmpD ||
 969              cmp1->Opcode() == Op_CmpP || cmp1->Opcode() == Op_CmpN ||
 970              cmp2->Opcode() == Op_CmpP || cmp2->Opcode() == Op_CmpN ||
 971              cmp1->is_SubTypeCheck() || cmp2->is_SubTypeCheck() ||
 972              cmp1->is_FlatArrayCheck() || cmp2->is_FlatArrayCheck()) {
 973     // Floats and pointers don't exactly obey trichotomy. To be on the safe side, don't transform their tests.
 974     // SubTypeCheck is not commutative
 975     return false;
 976   } else if (cmp1 != cmp2) {
 977     if (cmp1->in(1) == cmp2->in(2) &&
 978         cmp1->in(2) == cmp2->in(1)) {
 979       commute = true; // Same but swapped inputs, commute the test
 980     } else {
 981       return false; // Ifs are not comparing the same values
 982     }
 983   }
 984   proj1 = proj1->other_if_proj();
 985   proj2 = proj2->other_if_proj();
 986   if (!((proj1->unique_ctrl_out_or_null() == iff2 &&
 987          proj2->unique_ctrl_out_or_null() == this) ||
 988         (proj2->unique_ctrl_out_or_null() == iff1 &&
 989          proj1->unique_ctrl_out_or_null() == this))) {
 990     return false; // Ifs are not connected through other projs
 991   }
 992   // Found 'iff -> proj -> iff -> proj -> this' shape where all other projs are merged

1031     st->print("#reducible ");
1032     break;
1033   case RegionNode::LoopStatus::NeverIrreducibleEntry:
1034     break; // nothing
1035   }
1036 }
1037 #endif
1038 
1039 // Find the one non-null required input.  RegionNode only
1040 Node *Node::nonnull_req() const {
1041   assert( is_Region(), "" );
1042   for( uint i = 1; i < _cnt; i++ )
1043     if( in(i) )
1044       return in(i);
1045   ShouldNotReachHere();
1046   return nullptr;
1047 }
1048 
1049 
1050 //=============================================================================
1051 // note that these functions assume that the _adr_type field is flat
1052 uint PhiNode::hash() const {
1053   const Type* at = _adr_type;
1054   return TypeNode::hash() + (at ? at->hash() : 0);
1055 }
1056 bool PhiNode::cmp( const Node &n ) const {
1057   return TypeNode::cmp(n) && _adr_type == ((PhiNode&)n)._adr_type;
1058 }
1059 static inline
1060 const TypePtr* flatten_phi_adr_type(const TypePtr* at) {
1061   if (at == nullptr || at == TypePtr::BOTTOM)  return at;
1062   return Compile::current()->alias_type(at)->adr_type();
1063 }
1064 
1065 //----------------------------make---------------------------------------------
1066 // create a new phi with edges matching r and set (initially) to x
1067 PhiNode* PhiNode::make(Node* r, Node* x, const Type *t, const TypePtr* at) {
1068   uint preds = r->req();   // Number of predecessor paths
1069   assert(t != Type::MEMORY || at == flatten_phi_adr_type(at) || (flatten_phi_adr_type(at) == TypeAryPtr::INLINES && Compile::current()->flat_accesses_share_alias()), "flatten at");
1070   PhiNode* p = new PhiNode(r, t, at);
1071   for (uint j = 1; j < preds; j++) {
1072     // Fill in all inputs, except those which the region does not yet have
1073     if (r->in(j) != nullptr)
1074       p->init_req(j, x);
1075   }
1076   return p;
1077 }
1078 PhiNode* PhiNode::make(Node* r, Node* x) {
1079   const Type*    t  = x->bottom_type();
1080   const TypePtr* at = nullptr;
1081   if (t == Type::MEMORY)  at = flatten_phi_adr_type(x->adr_type());
1082   return make(r, x, t, at);
1083 }
1084 PhiNode* PhiNode::make_blank(Node* r, Node* x) {
1085   const Type*    t  = x->bottom_type();
1086   const TypePtr* at = nullptr;
1087   if (t == Type::MEMORY)  at = flatten_phi_adr_type(x->adr_type());
1088   return new PhiNode(r, t, at);
1089 }

1178       np->as_Phi()->verify_adr_type(visited, at);
1179     } else if (n->bottom_type() == Type::TOP
1180                || (n->is_Mem() && n->in(MemNode::Address)->bottom_type() == Type::TOP)) {
1181       // ignore top inputs
1182     } else {
1183       const TypePtr* nat = flatten_phi_adr_type(n->adr_type());
1184       // recheck phi/non-phi consistency at leaves:
1185       assert((nat != nullptr) == (at != nullptr), "");
1186       assert(nat == at || nat == TypePtr::BOTTOM,
1187              "adr_type must be consistent at leaves of phi nest");
1188     }
1189   }
1190 }
1191 
1192 // Verify a whole nest of phis rooted at this one.
1193 void PhiNode::verify_adr_type(bool recursive) const {
1194   if (VMError::is_error_reported())  return;  // muzzle asserts when debugging an error
1195   if (Node::in_dump())               return;  // muzzle asserts when printing
1196 
1197   assert((_type == Type::MEMORY) == (_adr_type != nullptr), "adr_type for memory phis only");
1198   // Flat array elements shouldn't get their own memory slice until flat_accesses_share_alias is cleared.
1199   // It could be the graph has no loads/stores and flat_accesses_share_alias is never cleared. EA could still
1200   // create per-element Phis but that wouldn't be a problem as there are no memory accesses for that array.
1201   assert(_adr_type == nullptr || _adr_type->isa_aryptr() == nullptr ||
1202          _adr_type->is_aryptr()->is_known_instance() ||
1203          !_adr_type->is_aryptr()->is_flat() ||
1204          !Compile::current()->flat_accesses_share_alias() ||
1205          _adr_type == TypeAryPtr::INLINES, "flat array element shouldn't get its own slice yet");
1206 
1207   if (!VerifyAliases)       return;  // verify thoroughly only if requested
1208 
1209   assert(_adr_type == flatten_phi_adr_type(_adr_type),
1210          "Phi::adr_type must be pre-normalized");
1211 
1212   if (recursive) {
1213     VectorSet visited;
1214     verify_adr_type(visited, _adr_type);
1215   }
1216 }
1217 #endif
1218 
1219 
1220 //------------------------------Value------------------------------------------
1221 // Compute the type of the PhiNode
1222 const Type* PhiNode::Value(PhaseGVN* phase) const {
1223   Node *r = in(0);              // RegionNode
1224   if( !r )                      // Copy or dead
1225     return in(1) ? phase->type(in(1)) : Type::TOP;

1469   assert(req() == 3, "same as region");
1470   RegionNode* region = in(0)->as_Region();
1471   for (uint i = 1; i < 3; i++) {
1472     Node* phi_input = in(i);
1473     if (phi_input != nullptr && phi_input->is_MergeMem() && region->in(i)->outcnt() == 1) {
1474       // Nothing is control-dependent on path #i except the region itself.
1475       MergeMemNode* merge_mem = phi_input->as_MergeMem();
1476       uint j = 3 - i;
1477       Node* other_phi_input = in(j);
1478       if (other_phi_input != nullptr && other_phi_input == merge_mem->base_memory() && !is_data_loop(region, phi_input, igvn)) {
1479         // merge_mem is a successor memory to other_phi_input, and is not pinned inside the diamond, so push it out.
1480         // Only proceed if the transformation doesn't create a data loop
1481         // This will allow the diamond to collapse completely if there are no other phis left.
1482         igvn->replace_node(this, merge_mem);
1483         return true;
1484       }
1485     }
1486   }
1487   return false;
1488 }
1489 
1490 //----------------------------check_cmove_id-----------------------------------
1491 // Check for CMove'ing a constant after comparing against the constant.
1492 // Happens all the time now, since if we compare equality vs a constant in
1493 // the parser, we "know" the variable is constant on one path and we force
1494 // it.  Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
1495 // conditional move: "x = (x==0)?0:x;".  Yucko.  This fix is slightly more
1496 // general in that we don't need constants.  Since CMove's are only inserted
1497 // in very special circumstances, we do it here on generic Phi's.
1498 Node* PhiNode::is_cmove_id(PhaseTransform* phase, int true_path) {
1499   assert(true_path !=0, "only diamond shape graph expected");
1500 
1501   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1502   // phi->region->if_proj->ifnode->bool->cmp
1503   Node*     region = in(0);
1504   Node*     iff    = region->in(1)->in(0);
1505   BoolNode* b      = iff->in(1)->as_Bool();
1506   Node*     cmp    = b->in(1);
1507   Node*     tval   = in(true_path);
1508   Node*     fval   = in(3-true_path);
1509   Node*     id     = CMoveNode::is_cmove_id(phase, cmp, tval, fval, b);

1524   }
1525 
1526   return id;
1527 }
1528 
1529 //------------------------------Identity---------------------------------------
1530 // Check for Region being Identity.
1531 Node* PhiNode::Identity(PhaseGVN* phase) {
1532   if (must_wait_for_region_in_irreducible_loop(phase)) {
1533     return this;
1534   }
1535   // Check for no merging going on
1536   // (There used to be special-case code here when this->region->is_Loop.
1537   // It would check for a tributary phi on the backedge that the main phi
1538   // trivially, perhaps with a single cast.  The unique_input method
1539   // does all this and more, by reducing such tributaries to 'this'.)
1540   Node* uin = unique_input(phase, false);
1541   if (uin != nullptr) {
1542     return uin;
1543   }
1544   uin = unique_constant_input_recursive(phase);
1545   if (uin != nullptr) {
1546     return uin;
1547   }
1548 
1549   int true_path = is_diamond_phi();
1550   // Delay CMove'ing identity if Ideal has not had the chance to handle unsafe cases, yet.
1551   if (true_path != 0 && !(phase->is_IterGVN() && wait_for_region_igvn(phase))) {
1552     Node* id = is_cmove_id(phase, true_path);
1553     if (id != nullptr) {
1554       return id;
1555     }
1556   }
1557 
1558   // Looking for phis with identical inputs.  If we find one that has
1559   // type TypePtr::BOTTOM, replace the current phi with the bottom phi.
1560   if (phase->is_IterGVN() && type() == Type::MEMORY && adr_type() !=
1561       TypePtr::BOTTOM && !adr_type()->is_known_instance()) {
1562     uint phi_len = req();
1563     Node* phi_reg = region();
1564     for (DUIterator_Fast imax, i = phi_reg->fast_outs(imax); i < imax; i++) {
1565       Node* u = phi_reg->fast_out(i);
1566       assert(!u->is_Phi() || u->in(0) == phi_reg, "broken Phi/Region subgraph");
1567       if (u->is_Phi() && u->req() == phi_len && can_be_replaced_by(u->as_Phi())) {

1618     }
1619     // Check for a unique input (maybe uncasted)
1620     if (input == nullptr) {
1621       input = un;
1622     } else if (input != un) {
1623       input = NodeSentinel; // no unique input
1624     }
1625   }
1626   if (input == nullptr) {
1627     return phase->C->top();        // no inputs
1628   }
1629 
1630   if (input != NodeSentinel) {
1631     return input;           // one unique direct input
1632   }
1633 
1634   // Nothing.
1635   return nullptr;
1636 }
1637 
1638 // Find the unique input, try to look recursively through input Phis
1639 Node* PhiNode::unique_constant_input_recursive(PhaseGVN* phase) {
1640   if (!phase->is_IterGVN()) {
1641     return nullptr;
1642   }
1643 
1644   ResourceMark rm;
1645   Node* unique = nullptr;
1646   Unique_Node_List visited;
1647   visited.push(this);
1648 
1649   for (uint visited_idx = 0; visited_idx < visited.size(); visited_idx++) {
1650     Node* current = visited.at(visited_idx);
1651     for (uint i = 1; i < current->req(); i++) {
1652       Node* phi_in = current->in(i);
1653       if (phi_in == nullptr) {
1654         continue;
1655       }
1656 
1657       if (phi_in->is_Phi()) {
1658         visited.push(phi_in);
1659       } else {
1660         if (unique == nullptr) {
1661           if (!phi_in->is_Con()) {
1662             return nullptr;
1663           }
1664           unique = phi_in;
1665         } else if (unique != phi_in) {
1666           return nullptr;
1667         }
1668       }
1669     }
1670   }
1671   return unique;
1672 }
1673 
1674 //------------------------------is_x2logic-------------------------------------
1675 // Check for simple convert-to-boolean pattern
1676 // If:(C Bool) Region:(IfF IfT) Phi:(Region 0 1)
1677 // Convert Phi to an ConvIB.
1678 static Node *is_x2logic( PhaseGVN *phase, PhiNode *phi, int true_path ) {
1679   assert(true_path !=0, "only diamond shape graph expected");
1680 
1681   // If we're late in the optimization process, we may have already expanded Conv2B nodes
1682   if (phase->C->post_loop_opts_phase() && !Matcher::match_rule_supported(Op_Conv2B)) {
1683     return nullptr;
1684   }
1685 
1686   // Convert the true/false index into an expected 0/1 return.
1687   // Map 2->0 and 1->1.
1688   int flipped = 2-true_path;
1689 
1690   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1691   // phi->region->if_proj->ifnode->bool->cmp
1692   Node *region = phi->in(0);
1693   Node *iff = region->in(1)->in(0);

2121 
2122     if (rc->in(0)->in(1) == nullptr || !rc->in(0)->in(1)->is_Bool()) { continue; }
2123     if (worklist.member(rc->in(0)->in(1))) {
2124       delay = true;
2125       break;
2126     }
2127 
2128     if (rc->in(0)->in(1)->in(1) == nullptr || !rc->in(0)->in(1)->in(1)->is_Cmp()) { continue; }
2129     if (worklist.member(rc->in(0)->in(1)->in(1))) {
2130       delay = true;
2131       break;
2132     }
2133   }
2134 
2135   if (delay) {
2136     worklist.push(this);
2137   }
2138   return delay;
2139 }
2140 
2141 
2142 // If the Phi's Region is in an irreducible loop, and the Region
2143 // has had an input removed, but not yet transformed, it could be
2144 // that the Region (and this Phi) are not reachable from Root.
2145 // If we allow the Phi to collapse before the Region, this may lead
2146 // to dead-loop data. Wait for the Region to check for reachability,
2147 // and potentially remove the dead code.
2148 bool PhiNode::must_wait_for_region_in_irreducible_loop(PhaseGVN* phase) const {
2149   RegionNode* region = in(0)->as_Region();
2150   if (region->loop_status() == RegionNode::LoopStatus::MaybeIrreducibleEntry) {
2151     Node* top = phase->C->top();
2152     for (uint j = 1; j < req(); j++) {
2153       Node* rc = region->in(j); // for each control input
2154       if (rc == nullptr || phase->type(rc) == Type::TOP) {
2155         // Region is missing a control input
2156         Node* n = in(j);
2157         if (n != nullptr && n != top) {
2158           // Phi still has its input, so region just lost its input
2159           return true;
2160         }
2161       }

2559     //                Phi (this)   |
2560     //                 |           |
2561     //                 +-----------+
2562     //
2563     // Generally, there are issues with non-termination with such circularity
2564     // (see comment further below). However, if there is a direct loop to self,
2565     // splitting the Phi through the MergeMem will result in the below.
2566     //
2567     //               +---+
2568     //               |   |
2569     //               v   |
2570     //              Phi  |
2571     //               |\  |
2572     //               | +-+
2573     // (base_memory) v
2574     //              MergeMem
2575     //
2576     // This split breaks the circularity and consequently does not lead to
2577     // non-termination.
2578     uint merge_width = 0;
2579     // TODO revisit this with JDK-8247216
2580     bool mergemem_only = true;
2581     bool split_always_terminates = false; // Is splitting guaranteed to terminate?
2582     for( uint i=1; i<req(); ++i ) {// For all paths in
2583       Node *ii = in(i);
2584       // TOP inputs should not be counted as safe inputs because if the
2585       // Phi references itself through all other inputs then splitting the
2586       // Phi through memory merges would create dead loop at later stage.
2587       if (ii == top) {
2588         return progress; // Delay optimization until graph is cleaned.
2589       }
2590       if (ii->is_MergeMem()) {
2591         MergeMemNode* n = ii->as_MergeMem();
2592         merge_width = MAX2(merge_width, n->req());
2593         if (n->base_memory() == this) {
2594           split_always_terminates = true;
2595         }
2596       } else {
2597         mergemem_only = false;
2598       }
2599     }
2600 
2601     // There are cases with circular dependencies between bottom Phis
2602     // and MergeMems. Below is a minimal example.
2603     //
2604     //               +------------+
2605     //               |            |
2606     // (base_memory) v            |
2607     //              MergeMem      |
2608     //                 |          |
2609     //                 v          |
2610     //                Phi (this)  |
2611     //                 |          |
2612     //                 v          |
2613     //                Phi         |
2614     //                 |          |
2615     //                 +----------+
2616     //
2617     // Here, we cannot break the circularity through a self-loop as there
2618     // are two Phis involved. Repeatedly splitting the Phis through the
2619     // MergeMem leads to non-termination. We check for non-termination below.
2620     // Only check for non-termination if necessary.
2621     if (!mergemem_only && !split_always_terminates && adr_type() == TypePtr::BOTTOM &&
2622         merge_width > Compile::AliasIdxRaw) {
2623       split_always_terminates = is_split_through_mergemem_terminating();
2624     }
2625 
2626     if (merge_width > Compile::AliasIdxRaw) {
2627       // found at least one non-empty MergeMem
2628       const TypePtr* at = adr_type();
2629       if (at != TypePtr::BOTTOM) {
2630         // Patch the existing phi to select an input from the merge:
2631         // Phi:AT1(...MergeMem(m0, m1, m2)...) into
2632         //     Phi:AT1(...m1...)
2633         int alias_idx = phase->C->get_alias_index(at);
2634         for (uint i=1; i<req(); ++i) {
2635           Node *ii = in(i);
2636           if (ii->is_MergeMem()) {
2637             MergeMemNode* n = ii->as_MergeMem();
2638             // compress paths and change unreachable cycles to TOP
2639             // If not, we can update the input infinitely along a MergeMem cycle
2640             // Equivalent code is in MemNode::Ideal_common
2641             Node *m  = phase->transform(n);
2642             if (outcnt() == 0) {  // Above transform() may kill us!
2643               return top;
2644             }
2645             // If transformed to a MergeMem, get the desired slice
2646             // Otherwise the returned node represents memory for every slice
2647             Node *new_mem = (m->is_MergeMem()) ?
2648                              m->as_MergeMem()->memory_at(alias_idx) : m;
2649             // Update input if it is progress over what we have now
2650             if (new_mem != ii) {
2651               set_req_X(i, new_mem, phase->is_IterGVN());
2652               progress = this;
2653             }
2654           }
2655         }
2656       } else if (mergemem_only || split_always_terminates) {
2657         // If all inputs reference this phi (directly or through data nodes) -
2658         // it is a dead loop.
2659         bool saw_safe_input = false;
2660         for (uint j = 1; j < req(); ++j) {
2661           Node* n = in(j);
2662           if (n->is_MergeMem()) {
2663             MergeMemNode* mm = n->as_MergeMem();
2664             if (mm->base_memory() == this || mm->base_memory() == mm->empty_memory()) {
2665               // Skip this input if it references back to this phi or if the memory path is dead
2666               continue;
2667             }
2668           }
2669           if (!is_unsafe_data_reference(n)) {
2670             saw_safe_input = true; // found safe input
2671             break;
2672           }
2673         }
2674         if (!saw_safe_input) {
2675           // There is a dead loop: All inputs are either dead or reference back to this phi
2676           return top;
2677         }
2678 
2679         // Phi(...MergeMem(m0, m1:AT1, m2:AT2)...) into
2680         //     MergeMem(Phi(...m0...), Phi:AT1(...m1...), Phi:AT2(...m2...))
2681         PhaseIterGVN* igvn = phase->is_IterGVN();
2682         assert(igvn != nullptr, "sanity check");
2683         PhiNode* new_base = (PhiNode*) clone();
2684         // Must eagerly register phis, since they participate in loops.
2685         igvn->register_new_node_with_optimizer(new_base);
2686 
2687         MergeMemNode* result = MergeMemNode::make(new_base);
2688         for (uint i = 1; i < req(); ++i) {
2689           Node *ii = in(i);
2690           if (ii->is_MergeMem()) {
2691             MergeMemNode* n = ii->as_MergeMem();
2692             if (igvn) {
2693               // TODO revisit this with JDK-8247216
2694               // Put 'n' on the worklist because it might be modified by MergeMemStream::iteration_setup
2695               igvn->_worklist.push(n);
2696             }
2697             for (MergeMemStream mms(result, n); mms.next_non_empty2(); ) {
2698               // If we have not seen this slice yet, make a phi for it.
2699               bool made_new_phi = false;
2700               if (mms.is_empty()) {
2701                 Node* new_phi = new_base->slice_memory(mms.adr_type(phase->C));
2702                 made_new_phi = true;
2703                 igvn->register_new_node_with_optimizer(new_phi);
2704                 mms.set_memory(new_phi);
2705               }
2706               Node* phi = mms.memory();
2707               assert(made_new_phi || phi->in(i) == n, "replace the i-th merge by a slice");
2708               phi->set_req_X(i, mms.memory2(), phase);
2709             }
2710           }
2711         }
2712         // Distribute all self-loops.
2713         { // (Extra braces to hide mms.)
2714           for (MergeMemStream mms(result); mms.next_non_empty(); ) {
2715             Node* phi = mms.memory();
2716             for (uint i = 1; i < req(); ++i) {

2725         // result in creating an excessive number of new nodes within a single
2726         // IGVN iteration. We have put the Phi nodes on the IGVN worklist, so
2727         // they are transformed later on in any case.
2728 
2729         // Replace self with the result.
2730         return result;
2731       }
2732     }
2733     //
2734     // Other optimizations on the memory chain
2735     //
2736     const TypePtr* at = adr_type();
2737     for( uint i=1; i<req(); ++i ) {// For all paths in
2738       Node *ii = in(i);
2739       Node *new_in = MemNode::optimize_memory_chain(ii, at, nullptr, phase);
2740       // MemNode::optimize_memory_chain above may kill us!
2741       if (outcnt() == 0) {
2742         return top;
2743       }
2744       if (ii != new_in ) {
2745         set_req_X(i, new_in, phase->is_IterGVN());
2746         progress = this;
2747       }
2748     }
2749   }
2750 
2751 #ifdef _LP64
2752   // Push DecodeN/DecodeNKlass down through phi.
2753   // The rest of phi graph will transform by split EncodeP node though phis up.
2754   if (can_reshape && progress == nullptr) {
2755     bool may_push = true;
2756     bool has_decodeN = false;
2757     bool is_decodeN = false;
2758     for (uint i=1; i<req(); ++i) {// For all paths in
2759       Node *ii = in(i);
2760       if (ii->is_DecodeNarrowPtr() && ii->bottom_type() == bottom_type()) {
2761         // Do optimization if a non dead path exist.
2762         if (ii->in(1)->bottom_type() != Type::TOP) {
2763           has_decodeN = true;
2764           is_decodeN = ii->is_DecodeN();
2765         }

2793             if (is_decodeN) {
2794               new_ii = new EncodePNode(ii, narrow_t);
2795             } else {
2796               new_ii = new EncodePKlassNode(ii, narrow_t);
2797             }
2798             igvn->register_new_node_with_optimizer(new_ii);
2799           }
2800         }
2801         new_phi->set_req(i, new_ii);
2802       }
2803       igvn->register_new_node_with_optimizer(new_phi, this);
2804       if (is_decodeN) {
2805         progress = new DecodeNNode(new_phi, bottom_type());
2806       } else {
2807         progress = new DecodeNKlassNode(new_phi, bottom_type());
2808       }
2809     }
2810   }
2811 #endif
2812 
2813   Node* inline_type = try_push_inline_types_down(phase, can_reshape);
2814   if (inline_type != this) {
2815     return inline_type;
2816   }
2817 
2818   // Try to convert a Phi with two duplicated convert nodes into a phi of the pre-conversion type and the convert node
2819   // proceeding the phi, to de-duplicate the convert node and compact the IR.
2820   if (can_reshape && progress == nullptr) {
2821     ConvertNode* convert = in(1)->isa_Convert();
2822     if (convert != nullptr) {
2823       int conv_op = convert->Opcode();
2824       bool ok = true;
2825 
2826       // Check the rest of the inputs
2827       for (uint i = 2; i < req(); i++) {
2828         // Make sure that all inputs are of the same type of convert node
2829         if (in(i)->Opcode() != conv_op) {
2830           ok = false;
2831           break;
2832         }
2833       }
2834 
2835       if (ok) {
2836         // Find the local bottom type to set as the type of the phi
2837         const Type* source_type = Type::get_const_basic_type(convert->in_type()->basic_type());

2860   // only after the non-bottom memory phi is processed by igvn, PhiNode::Identity doesn't run and the transformation
2861   // doesn't happen.
2862   // Look for non-bottom Phis that should be transformed and enqueue them for igvn so that PhiNode::Identity executes for
2863   // them.
2864   if (can_reshape && type() == Type::MEMORY && adr_type() == TypePtr::BOTTOM) {
2865     PhaseIterGVN* igvn = phase->is_IterGVN();
2866     uint phi_len = req();
2867     Node* phi_reg = region();
2868     for (DUIterator_Fast imax, i = phi_reg->fast_outs(imax); i < imax; i++) {
2869       Node* u = phi_reg->fast_out(i);
2870       assert(!u->is_Phi() || (u->in(0) == phi_reg && u->req() == phi_len), "broken Phi/Region subgraph");
2871       if (u->is_Phi() && u->as_Phi()->can_be_replaced_by(this)) {
2872         igvn->_worklist.push(u);
2873       }
2874     }
2875   }
2876 
2877   return progress;              // Return any progress
2878 }
2879 
2880 // If an InlineType node references itself through a Phi (oop input):
2881 //
2882 //        /------ |
2883 //   InlineType   |
2884 // \   /          |
2885 //  Phi           |
2886 //   ^____________|
2887 //
2888 // and is pushed down through the Phi, the result is a new InlineType node that can be pushed down through the Phi
2889 // etc.
2890 //
2891 // To solve that problem, the code below finds InlineType nodes that are reachable from another InlineType node by
2892 // following the inline type node's oop inputs through Phis and casts such as, for instance:
2893 // (InlineType (Cast (Phi (InlineType oop
2894 // and replaces it with:
2895 // (InlineType (Cast (Phi oop
2896 // which requires cloning every Phi and cast nodes in the subgraph between the root InlineType and the leaf
2897 // InlineType node in this example.
2898 class PushInlineTypeDown {
2899 private:
2900   // Find the inlineType nodes that can be reached from this Phi without going through another InlineType (those that
2901   // must not reference another inline type node from their oop input through Phis and cast nodes)
2902   void collect_nodes_from_phi() {
2903     _nodes_from_phi.push(_root_phi);
2904     for (uint i = 0; i < _nodes_from_phi.size(); ++i) {
2905       Node* n = _nodes_from_phi.at(i);
2906       if (n->is_Phi()) {
2907         for (uint j = 1; j < n->req(); ++j) {
2908           Node* in = n->in(j);
2909           if (in != nullptr) {
2910             _nodes_from_phi.push(in);
2911           }
2912         }
2913       } else if (n->is_ConstraintCast()) {
2914         Node* in = n->in(1);
2915         if (in != nullptr) {
2916           _nodes_from_phi.push(in);
2917         }
2918       }
2919     }
2920   }
2921 
2922   void collect_nodes_from_inline_types() {
2923     // Only keep InlineType nodes
2924     for (int i = _nodes_from_phi.size() - 1; i >= 0; i--) {
2925       Node* n = _nodes_from_phi.at(i);
2926       if (!n->is_InlineType()) {
2927         _nodes_from_phi.remove(i);
2928       }
2929     }
2930     DEBUG_ONLY(_init_nodes = _nodes_from_phi.size());
2931     // Find the InlineType nodes reachable from the current set of inline type nodes.
2932     for (uint i = 0; i < _nodes_from_phi.size(); ++i) {
2933       Node* n = _nodes_from_phi.at(i);
2934       if (n->is_Phi()) {
2935         for (uint j = 1; j < n->req(); ++j) {
2936           Node* in = n->in(j);
2937           if (in != nullptr) {
2938             _nodes_from_phi.push(in);
2939           }
2940         }
2941       } else if (n->is_ConstraintCast()) {
2942         Node* in = n->in(1);
2943         if (in != nullptr) {
2944           _nodes_from_phi.push(in);
2945         }
2946       } else if (n->is_InlineType()) {
2947         Node* buf = n->as_InlineType()->get_oop();
2948         if (buf != nullptr) {
2949           _nodes_from_phi.push(buf);
2950           _subgraph_to_clone.push(n);
2951         }
2952       }
2953     }
2954   }
2955 
2956   void collect_nodes_to_clone() {
2957     collect_nodes_from_inline_types();
2958 
2959     // Find the subgraph that must be cloned by following uses from inline types.
2960     for (uint i = 0; i < _subgraph_to_clone.size(); ++i) {
2961       Node* n = _subgraph_to_clone.at(i);
2962       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2963         Node* u = n->fast_out(i);
2964         if (_nodes_from_phi.member(u)) {
2965           _subgraph_to_clone.push(u);
2966         }
2967       }
2968     }
2969   }
2970 
2971   void clone_nodes() {
2972     for (uint i = 0; i < _subgraph_to_clone.size(); ++i) {
2973       Node* n = _subgraph_to_clone.at(i);
2974       assert(_clones[n->_idx] == nullptr, "shouldn't be cloned yet");
2975       if (n->is_InlineType()) {
2976         _clones.map(n->_idx, n->as_InlineType()->get_oop());
2977       } else {
2978         Node* clone = n->clone();
2979         _phase->is_IterGVN()->register_new_node_with_optimizer(clone);
2980         _clones.map(n->_idx, clone);
2981       }
2982     }
2983   }
2984 
2985   Node* get_clone(Node* n) const {
2986     Node* clone = nullptr;
2987     while (true) {
2988       Node* m = _clones[n->_idx];
2989       if (m == nullptr) {
2990         return clone;
2991       }
2992       clone = m;
2993       n = clone;
2994     }
2995   }
2996 
2997   void update_clone_node_edges() {
2998     for (uint i = 0; i < _subgraph_to_clone.size(); ++i) {
2999       Node* n = _subgraph_to_clone.at(i);
3000       Node* n_clone = get_clone(n);
3001       assert(n_clone != nullptr, "must be cloned");
3002       if (n->is_Phi()) {
3003         for (uint j = 1; j < n->req(); ++j) {
3004           Node* in = n->in(j);
3005           if (in != nullptr) {
3006             Node* in_clone = get_clone(in);
3007             if (in_clone != nullptr) {
3008               n_clone->set_req(j, in_clone);
3009             }
3010           }
3011         }
3012       } else if (n->is_ConstraintCast()) {
3013         Node* in = n->in(1);
3014         Node* in_clone = get_clone(in);
3015         assert(in_clone != nullptr, "must be cloned");
3016         n_clone->set_req(1, in_clone);
3017       } else if (n->is_InlineType()) {
3018         Node* in = n->as_InlineType()->get_oop();
3019         Node* in_clone = get_clone(in);
3020         if (in_clone != nullptr) {
3021           _phase->is_IterGVN()->rehash_node_delayed(n);
3022           n->as_InlineType()->set_oop(*_phase, in_clone);
3023         }
3024       }
3025     }
3026   }
3027 
3028   void clone_subgraph() {
3029     clone_nodes();
3030     update_clone_node_edges();
3031   }
3032 
3033 #ifdef ASSERT
3034   void verify_clone() {
3035     uint vts_to_skip = 0;
3036     uint before_phis = 0;
3037     uint before_casts = 0;
3038     for (uint i = 0; i < _nodes_from_phi.size(); ++i) {
3039       Node *n = _nodes_from_phi.at(i);
3040       if (n->is_Phi()) {
3041         before_phis++;
3042       } else if (n->is_ConstraintCast()) {
3043         before_casts++;
3044       } else if (n->is_InlineType()) {
3045         Node* buf = n->as_InlineType()->get_oop();
3046         if (buf != nullptr && i >= _init_nodes) {
3047           vts_to_skip++;
3048         }
3049       }
3050     }
3051 
3052     Unique_Node_List after;
3053     after.push(_root_phi);
3054     for (uint i = 0; i < after.size(); ++i) {
3055       Node* n = after.at(i);
3056       if (n->is_Phi()) {
3057         for (uint j = 1; j < n->req(); ++j) {
3058           Node* in = n->in(j);
3059           if (in != nullptr) {
3060             after.push(in);
3061           }
3062         }
3063       } else if (n->is_ConstraintCast()) {
3064         Node* in = n->in(1);
3065         if (in != nullptr) {
3066           after.push(in);
3067         }
3068       }
3069     }
3070     for (int i = after.size() - 1; i >= 0; i--) {
3071       Node* n = after.at(i);
3072       if (!n->is_InlineType()) {
3073         after.remove(i);
3074       }
3075     }
3076     uint after_phis = 0;
3077     uint after_casts = 0;
3078     uint init_nodes = after.size();
3079     for (uint i = 0; i < after.size(); ++i) {
3080       Node* n = after.at(i);
3081       if (n->is_Phi()) {
3082         after_phis++;
3083         for (uint j = 1; j < n->req(); ++j) {
3084           Node *in = n->in(j);
3085           if (in != nullptr) {
3086             after.push(in);
3087           }
3088         }
3089       } else if (n->is_ConstraintCast()) {
3090         after_casts++;
3091         Node* in = n->in(1);
3092         if (in != nullptr) {
3093           after.push(in);
3094         }
3095       } else if (n->is_InlineType()) {
3096         assert(i < init_nodes, "");
3097         Node* buf = n->as_InlineType()->get_oop();
3098         if (buf != nullptr) {
3099           after.push(buf);
3100         }
3101       }
3102     }
3103     assert(after.size() + vts_to_skip == _nodes_from_phi.size(), "");
3104     assert(before_casts == after_casts, "no cast should have been dropped");
3105     assert(before_phis == after_phis, "no phi should");
3106   }
3107 #endif
3108 
3109   Node* do_transform(PhiNode* phi) {
3110     assert(_inline_klass != nullptr, "must be");
3111     InlineTypeNode *vt = InlineTypeNode::make_null(*_phase, _inline_klass, /* transform = */ false)->clone_with_phis(
3112       _phase, phi->in(0), nullptr, !phi->type()->maybe_null(), true);
3113     if (_can_reshape) {
3114       // Replace phi right away to be able to use the inline
3115       // type node when reaching the phi again through data loops.
3116       PhaseIterGVN* igvn = _phase->is_IterGVN();
3117       for (DUIterator_Fast imax, i = phi->fast_outs(imax); i < imax; i++) {
3118         Node* u = phi->fast_out(i);
3119         igvn->rehash_node_delayed(u);
3120         imax -= u->replace_edge(phi, vt);
3121         --i;
3122       }
3123       igvn->rehash_node_delayed(phi);
3124       assert(phi->outcnt() == 0, "should be dead now");
3125     }
3126     ResourceMark rm;
3127     Node_List casts;
3128     for (uint i = 1; i < phi->req(); ++i) {
3129       Node* n = phi->in(i);
3130       if (n == nullptr) {
3131         continue;
3132       }
3133       while (n->is_ConstraintCast()) {
3134         casts.push(n);
3135         n = n->in(1);
3136       }
3137       if (_phase->type(n)->is_zero_type()) {
3138         n = InlineTypeNode::make_null(*_phase, _inline_klass);
3139       } else if (n->is_Phi()) {
3140         assert(_can_reshape, "can only handle phis during IGVN");
3141         n = _phase->transform(do_transform(n->as_Phi()));
3142       }
3143       while (casts.size() != 0) {
3144         // Push the cast(s) through the InlineTypeNode
3145         Node *cast = casts.pop()->clone();
3146         cast->set_req_X(1, n->as_InlineType()->get_oop(), _phase);
3147         n = n->clone();
3148         n->as_InlineType()->set_oop(*_phase, _phase->transform(cast));
3149         n = _phase->transform(n);
3150         if (n->is_top()) {
3151           break;
3152         }
3153       }
3154       bool transform = !_can_reshape && (i == (phi->req() - 1)); // Transform phis on last merge
3155       assert(n->is_top() || n->is_InlineType(), "Only InlineType or top at this point.");
3156       if (n->is_InlineType()) {
3157         vt->merge_with(_phase, n->as_InlineType(), i, transform);
3158       } // else nothing to do: phis above vt created by clone_with_phis are initialized to top already.
3159     }
3160     return vt;
3161 
3162   }
3163 
3164   PhiNode* _root_phi;
3165   PhaseGVN* _phase;
3166   bool _can_reshape;
3167   ciInlineKlass* _inline_klass;
3168   Unique_Node_List _nodes_from_phi;
3169   Unique_Node_List _subgraph_to_clone;
3170   Node_List _clones;
3171   DEBUG_ONLY(uint _init_nodes);
3172 
3173 public:
3174 
3175   PushInlineTypeDown(PhiNode *root_phi, PhaseGVN *phase, bool can_reshape)
3176     : _root_phi(root_phi), _phase(phase), _can_reshape(can_reshape), _inline_klass(nullptr) {
3177     collect_nodes_from_phi();
3178   }
3179 
3180   bool can_do_it() {
3181     if (_root_phi->req() <= 2) {
3182       // Dead phi.
3183       return false;
3184     }
3185 
3186     for (uint next = 0; next < _nodes_from_phi.size(); next++) {
3187       Node* n = _nodes_from_phi.at(next);
3188       if (n->is_Phi()) {
3189         assert(n->bottom_type()->isa_ptr(), "broken graph");
3190         if (n != _root_phi && !_can_reshape) {
3191           return false;
3192         }
3193         continue;
3194       }
3195       if (n->is_ConstraintCast()) {
3196         if (n->in(0) != nullptr && n->in(0)->is_top()) {
3197           // Will die, don't optimize
3198           return false;
3199         }
3200         continue;
3201       }
3202       const Type* type = _phase->type(n);
3203       if (n->is_InlineType()) {
3204         if (_inline_klass == nullptr) {
3205           _inline_klass = type->inline_klass();
3206         } else if (_inline_klass != type->inline_klass()) {
3207           return false;
3208         }
3209         continue;
3210       }
3211       if (!type->is_zero_type()) {
3212         return false;
3213       }
3214     }
3215 
3216     if (_inline_klass == nullptr) {
3217       return false;
3218     }
3219 
3220     // Check if cast nodes can be pushed through
3221     const Type* t = Type::get_const_type(_inline_klass);
3222     for (uint next = 0; next < _nodes_from_phi.size(); next++) {
3223       Node* n = _nodes_from_phi.at(next);
3224       if (n->is_ConstraintCast()) {
3225         if (t->filter(n->bottom_type()) == Type::TOP) {
3226           return false;
3227         }
3228       }
3229     }
3230     return true;
3231   }
3232 
3233 
3234   Node* do_it() {
3235     if (_can_reshape) {
3236       Node_List clones;
3237       collect_nodes_to_clone();
3238       clone_subgraph();
3239       DEBUG_ONLY(verify_clone());
3240     }
3241     return do_transform(_root_phi);
3242   }
3243 };
3244 
3245 
3246 // Check recursively if inputs are either an inline type, constant null
3247 // or another Phi (including self references through data loops). If so,
3248 // push the inline types down through the phis to enable folding of loads.
3249 Node* PhiNode::try_push_inline_types_down(PhaseGVN* phase, const bool can_reshape) {
3250   if (!can_be_inline_type()) {
3251     return this;
3252   }
3253 
3254   ResourceMark rm;
3255   PushInlineTypeDown push_inline_type_down(this, phase, can_reshape);
3256   if (push_inline_type_down.can_do_it()) {
3257     return push_inline_type_down.do_it();
3258   }
3259   return this;
3260 }
3261 
3262 #ifdef ASSERT
3263 bool PhiNode::can_push_inline_types_down(PhaseGVN* phase) {
3264   if (!can_be_inline_type()) {
3265     return false;
3266   }
3267 
3268   ResourceMark rm;
3269   PushInlineTypeDown push_inline_type_down(this, phase, false);
3270   return push_inline_type_down.can_do_it();
3271 }
3272 #endif // ASSERT
3273 
3274 static int compare_types(const Type* const& e1, const Type* const& e2) {
3275   return (intptr_t)e1 - (intptr_t)e2;
3276 }
3277 
3278 // Collect types at casts that are going to be eliminated at that Phi and store them in a TypeTuple.
3279 // Sort the types using an arbitrary order so a list of some types always hashes to the same TypeTuple (and TypeTuple
3280 // pointer comparison is enough to tell if 2 list of types are the same or not)
3281 const TypeTuple* PhiNode::collect_types(PhaseGVN* phase) const {
3282   const Node* region = in(0);
3283   const Type* phi_type = bottom_type();
3284   ResourceMark rm;
3285   GrowableArray<const Type*> types;
3286   for (uint i = 1; i < req(); i++) {
3287     if (region->in(i) == nullptr || phase->type(region->in(i)) == Type::TOP) {
3288       continue;
3289     }
3290     Node* in = Node::in(i);
3291     const Type* t = phase->type(in);
3292     if (in == nullptr || in == this || t == Type::TOP) {
3293       continue;

3643 #ifndef PRODUCT
3644 void CatchProjNode::dump_spec(outputStream *st) const {
3645   ProjNode::dump_spec(st);
3646   st->print("@bci %d ",_handler_bci);
3647 }
3648 #endif
3649 
3650 //=============================================================================
3651 //------------------------------Identity---------------------------------------
3652 // Check for CreateEx being Identity.
3653 Node* CreateExNode::Identity(PhaseGVN* phase) {
3654   if( phase->type(in(1)) == Type::TOP ) return in(1);
3655   if( phase->type(in(0)) == Type::TOP ) return in(0);
3656   if (phase->type(in(0)->in(0)) == Type::TOP) {
3657     assert(in(0)->is_CatchProj(), "control is CatchProj");
3658     return phase->C->top(); // dead code
3659   }
3660   // We only come from CatchProj, unless the CatchProj goes away.
3661   // If the CatchProj is optimized away, then we just carry the
3662   // exception oop through.
3663 
3664   // CheckCastPPNode::Ideal() for inline types reuses the exception
3665   // paths of a call to perform an allocation: we can see a Phi here.
3666   if (in(1)->is_Phi()) {
3667     return this;
3668   }
3669   CallNode *call = in(1)->in(0)->as_Call();
3670 
3671   return (in(0)->is_CatchProj() && in(0)->in(0)->is_Catch() &&
3672           in(0)->in(0)->in(1) == in(1)) ? this : call->in(TypeFunc::Parms);
3673 }
3674 
3675 //=============================================================================
3676 //------------------------------Value------------------------------------------
3677 // Check for being unreachable.
3678 const Type* NeverBranchNode::Value(PhaseGVN* phase) const {
3679   if (!in(0) || in(0)->is_top()) return Type::TOP;
3680   return bottom_type();
3681 }
3682 
3683 //------------------------------Ideal------------------------------------------
3684 // Check for no longer being part of a loop
3685 Node *NeverBranchNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3686   if (can_reshape && !in(0)->is_Region()) {
3687     // Dead code elimination can sometimes delete this projection so
3688     // if it's not there, there's nothing to do.
< prev index next >