1 /*
   2  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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 "precompiled.hpp"
  26 #include "gc/shared/barrierSet.hpp"
  27 #include "gc/shared/c2/barrierSetC2.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/objArrayKlass.hpp"
  31 #include "opto/addnode.hpp"
  32 #include "opto/castnode.hpp"
  33 #include "opto/cfgnode.hpp"
  34 #include "opto/connode.hpp"
  35 #include "opto/convertnode.hpp"
  36 #include "opto/inlinetypenode.hpp"
  37 #include "opto/loopnode.hpp"
  38 #include "opto/machnode.hpp"
  39 #include "opto/movenode.hpp"
  40 #include "opto/narrowptrnode.hpp"
  41 #include "opto/mulnode.hpp"
  42 #include "opto/phaseX.hpp"
  43 #include "opto/regalloc.hpp"
  44 #include "opto/regmask.hpp"
  45 #include "opto/runtime.hpp"
  46 #include "opto/subnode.hpp"
  47 #include "opto/vectornode.hpp"
  48 #include "utilities/vmError.hpp"
  49 
  50 // Portions of code courtesy of Clifford Click
  51 
  52 // Optimization - Graph Style
  53 
  54 //=============================================================================
  55 //------------------------------Value------------------------------------------
  56 // Compute the type of the RegionNode.
  57 const Type* RegionNode::Value(PhaseGVN* phase) const {
  58   for( uint i=1; i<req(); ++i ) {       // For all paths in
  59     Node *n = in(i);            // Get Control source
  60     if( !n ) continue;          // Missing inputs are TOP
  61     if( phase->type(n) == Type::CONTROL )
  62       return Type::CONTROL;
  63   }
  64   return Type::TOP;             // All paths dead?  Then so are we
  65 }
  66 
  67 //------------------------------Identity---------------------------------------
  68 // Check for Region being Identity.
  69 Node* RegionNode::Identity(PhaseGVN* phase) {
  70   // Cannot have Region be an identity, even if it has only 1 input.
  71   // Phi users cannot have their Region input folded away for them,
  72   // since they need to select the proper data input
  73   return this;
  74 }
  75 
  76 //------------------------------merge_region-----------------------------------
  77 // If a Region flows into a Region, merge into one big happy merge.  This is
  78 // hard to do if there is stuff that has to happen
  79 static Node *merge_region(RegionNode *region, PhaseGVN *phase) {
  80   if( region->Opcode() != Op_Region ) // Do not do to LoopNodes
  81     return nullptr;
  82   Node *progress = nullptr;        // Progress flag
  83   PhaseIterGVN *igvn = phase->is_IterGVN();
  84 
  85   uint rreq = region->req();
  86   for( uint i = 1; i < rreq; i++ ) {
  87     Node *r = region->in(i);
  88     if( r && r->Opcode() == Op_Region && // Found a region?
  89         r->in(0) == r &&        // Not already collapsed?
  90         r != region &&          // Avoid stupid situations
  91         r->outcnt() == 2 ) {    // Self user and 'region' user only?
  92       assert(!r->as_Region()->has_phi(), "no phi users");
  93       if( !progress ) {         // No progress
  94         if (region->has_phi()) {
  95           return nullptr;        // Only flatten if no Phi users
  96           // igvn->hash_delete( phi );
  97         }
  98         igvn->hash_delete( region );
  99         progress = region;      // Making progress
 100       }
 101       igvn->hash_delete( r );
 102 
 103       // Append inputs to 'r' onto 'region'
 104       for( uint j = 1; j < r->req(); j++ ) {
 105         // Move an input from 'r' to 'region'
 106         region->add_req(r->in(j));
 107         r->set_req(j, phase->C->top());
 108         // Update phis of 'region'
 109         //for( uint k = 0; k < max; k++ ) {
 110         //  Node *phi = region->out(k);
 111         //  if( phi->is_Phi() ) {
 112         //    phi->add_req(phi->in(i));
 113         //  }
 114         //}
 115 
 116         rreq++;                 // One more input to Region
 117       } // Found a region to merge into Region
 118       igvn->_worklist.push(r);
 119       // Clobber pointer to the now dead 'r'
 120       region->set_req(i, phase->C->top());
 121     }
 122   }
 123 
 124   return progress;
 125 }
 126 
 127 
 128 
 129 //--------------------------------has_phi--------------------------------------
 130 // Helper function: Return any PhiNode that uses this region or null
 131 PhiNode* RegionNode::has_phi() const {
 132   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 133     Node* phi = fast_out(i);
 134     if (phi->is_Phi()) {   // Check for Phi users
 135       assert(phi->in(0) == (Node*)this, "phi uses region only via in(0)");
 136       return phi->as_Phi();  // this one is good enough
 137     }
 138   }
 139 
 140   return nullptr;
 141 }
 142 
 143 
 144 //-----------------------------has_unique_phi----------------------------------
 145 // Helper function: Return the only PhiNode that uses this region or null
 146 PhiNode* RegionNode::has_unique_phi() const {
 147   // Check that only one use is a Phi
 148   PhiNode* only_phi = nullptr;
 149   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 150     Node* phi = fast_out(i);
 151     if (phi->is_Phi()) {   // Check for Phi users
 152       assert(phi->in(0) == (Node*)this, "phi uses region only via in(0)");
 153       if (only_phi == nullptr) {
 154         only_phi = phi->as_Phi();
 155       } else {
 156         return nullptr;  // multiple phis
 157       }
 158     }
 159   }
 160 
 161   return only_phi;
 162 }
 163 
 164 
 165 //------------------------------check_phi_clipping-----------------------------
 166 // Helper function for RegionNode's identification of FP clipping
 167 // Check inputs to the Phi
 168 static bool check_phi_clipping( PhiNode *phi, ConNode * &min, uint &min_idx, ConNode * &max, uint &max_idx, Node * &val, uint &val_idx ) {
 169   min     = nullptr;
 170   max     = nullptr;
 171   val     = nullptr;
 172   min_idx = 0;
 173   max_idx = 0;
 174   val_idx = 0;
 175   uint  phi_max = phi->req();
 176   if( phi_max == 4 ) {
 177     for( uint j = 1; j < phi_max; ++j ) {
 178       Node *n = phi->in(j);
 179       int opcode = n->Opcode();
 180       switch( opcode ) {
 181       case Op_ConI:
 182         {
 183           if( min == nullptr ) {
 184             min     = n->Opcode() == Op_ConI ? (ConNode*)n : nullptr;
 185             min_idx = j;
 186           } else {
 187             max     = n->Opcode() == Op_ConI ? (ConNode*)n : nullptr;
 188             max_idx = j;
 189             if( min->get_int() > max->get_int() ) {
 190               // Swap min and max
 191               ConNode *temp;
 192               uint     temp_idx;
 193               temp     = min;     min     = max;     max     = temp;
 194               temp_idx = min_idx; min_idx = max_idx; max_idx = temp_idx;
 195             }
 196           }
 197         }
 198         break;
 199       default:
 200         {
 201           val = n;
 202           val_idx = j;
 203         }
 204         break;
 205       }
 206     }
 207   }
 208   return ( min && max && val && (min->get_int() <= 0) && (max->get_int() >=0) );
 209 }
 210 
 211 
 212 //------------------------------check_if_clipping------------------------------
 213 // Helper function for RegionNode's identification of FP clipping
 214 // Check that inputs to Region come from two IfNodes,
 215 //
 216 //            If
 217 //      False    True
 218 //       If        |
 219 //  False  True    |
 220 //    |      |     |
 221 //  RegionNode_inputs
 222 //
 223 static bool check_if_clipping( const RegionNode *region, IfNode * &bot_if, IfNode * &top_if ) {
 224   top_if = nullptr;
 225   bot_if = nullptr;
 226 
 227   // Check control structure above RegionNode for (if  ( if  ) )
 228   Node *in1 = region->in(1);
 229   Node *in2 = region->in(2);
 230   Node *in3 = region->in(3);
 231   // Check that all inputs are projections
 232   if( in1->is_Proj() && in2->is_Proj() && in3->is_Proj() ) {
 233     Node *in10 = in1->in(0);
 234     Node *in20 = in2->in(0);
 235     Node *in30 = in3->in(0);
 236     // Check that #1 and #2 are ifTrue and ifFalse from same If
 237     if( in10 != nullptr && in10->is_If() &&
 238         in20 != nullptr && in20->is_If() &&
 239         in30 != nullptr && in30->is_If() && in10 == in20 &&
 240         (in1->Opcode() != in2->Opcode()) ) {
 241       Node  *in100 = in10->in(0);
 242       Node *in1000 = (in100 != nullptr && in100->is_Proj()) ? in100->in(0) : nullptr;
 243       // Check that control for in10 comes from other branch of IF from in3
 244       if( in1000 != nullptr && in1000->is_If() &&
 245           in30 == in1000 && (in3->Opcode() != in100->Opcode()) ) {
 246         // Control pattern checks
 247         top_if = (IfNode*)in1000;
 248         bot_if = (IfNode*)in10;
 249       }
 250     }
 251   }
 252 
 253   return (top_if != nullptr);
 254 }
 255 
 256 
 257 //------------------------------check_convf2i_clipping-------------------------
 258 // Helper function for RegionNode's identification of FP clipping
 259 // Verify that the value input to the phi comes from "ConvF2I; LShift; RShift"
 260 static bool check_convf2i_clipping( PhiNode *phi, uint idx, ConvF2INode * &convf2i, Node *min, Node *max) {
 261   convf2i = nullptr;
 262 
 263   // Check for the RShiftNode
 264   Node *rshift = phi->in(idx);
 265   assert( rshift, "Previous checks ensure phi input is present");
 266   if( rshift->Opcode() != Op_RShiftI )  { return false; }
 267 
 268   // Check for the LShiftNode
 269   Node *lshift = rshift->in(1);
 270   assert( lshift, "Previous checks ensure phi input is present");
 271   if( lshift->Opcode() != Op_LShiftI )  { return false; }
 272 
 273   // Check for the ConvF2INode
 274   Node *conv = lshift->in(1);
 275   if( conv->Opcode() != Op_ConvF2I ) { return false; }
 276 
 277   // Check that shift amounts are only to get sign bits set after F2I
 278   jint max_cutoff     = max->get_int();
 279   jint min_cutoff     = min->get_int();
 280   jint left_shift     = lshift->in(2)->get_int();
 281   jint right_shift    = rshift->in(2)->get_int();
 282   jint max_post_shift = nth_bit(BitsPerJavaInteger - left_shift - 1);
 283   if( left_shift != right_shift ||
 284       0 > left_shift || left_shift >= BitsPerJavaInteger ||
 285       max_post_shift < max_cutoff ||
 286       max_post_shift < -min_cutoff ) {
 287     // Shifts are necessary but current transformation eliminates them
 288     return false;
 289   }
 290 
 291   // OK to return the result of ConvF2I without shifting
 292   convf2i = (ConvF2INode*)conv;
 293   return true;
 294 }
 295 
 296 
 297 //------------------------------check_compare_clipping-------------------------
 298 // Helper function for RegionNode's identification of FP clipping
 299 static bool check_compare_clipping( bool less_than, IfNode *iff, ConNode *limit, Node * & input ) {
 300   Node *i1 = iff->in(1);
 301   if ( !i1->is_Bool() ) { return false; }
 302   BoolNode *bool1 = i1->as_Bool();
 303   if(       less_than && bool1->_test._test != BoolTest::le ) { return false; }
 304   else if( !less_than && bool1->_test._test != BoolTest::lt ) { return false; }
 305   const Node *cmpF = bool1->in(1);
 306   if( cmpF->Opcode() != Op_CmpF )      { return false; }
 307   // Test that the float value being compared against
 308   // is equivalent to the int value used as a limit
 309   Node *nodef = cmpF->in(2);
 310   if( nodef->Opcode() != Op_ConF ) { return false; }
 311   jfloat conf = nodef->getf();
 312   jint   coni = limit->get_int();
 313   if( ((int)conf) != coni )        { return false; }
 314   input = cmpF->in(1);
 315   return true;
 316 }
 317 
 318 //------------------------------is_unreachable_region--------------------------
 319 // Check if the RegionNode is part of an unsafe loop and unreachable from root.
 320 bool RegionNode::is_unreachable_region(const PhaseGVN* phase) {
 321   Node* top = phase->C->top();
 322   assert(req() == 2 || (req() == 3 && in(1) != nullptr && in(2) == top), "sanity check arguments");
 323   if (_is_unreachable_region) {
 324     // Return cached result from previous evaluation which should still be valid
 325     assert(is_unreachable_from_root(phase), "walk the graph again and check if its indeed unreachable");
 326     return true;
 327   }
 328 
 329   // First, cut the simple case of fallthrough region when NONE of
 330   // region's phis references itself directly or through a data node.
 331   if (is_possible_unsafe_loop(phase)) {
 332     // If we have a possible unsafe loop, check if the region node is actually unreachable from root.
 333     if (is_unreachable_from_root(phase)) {
 334       _is_unreachable_region = true;
 335       return true;
 336     }
 337   }
 338   return false;
 339 }
 340 
 341 bool RegionNode::is_possible_unsafe_loop(const PhaseGVN* phase) const {
 342   uint max = outcnt();
 343   uint i;
 344   for (i = 0; i < max; i++) {
 345     Node* n = raw_out(i);
 346     if (n != nullptr && n->is_Phi()) {
 347       PhiNode* phi = n->as_Phi();
 348       assert(phi->in(0) == this, "sanity check phi");
 349       if (phi->outcnt() == 0) {
 350         continue; // Safe case - no loops
 351       }
 352       if (phi->outcnt() == 1) {
 353         Node* u = phi->raw_out(0);
 354         // Skip if only one use is an other Phi or Call or Uncommon trap.
 355         // It is safe to consider this case as fallthrough.
 356         if (u != nullptr && (u->is_Phi() || u->is_CFG())) {
 357           continue;
 358         }
 359       }
 360       // Check when phi references itself directly or through an other node.
 361       if (phi->as_Phi()->simple_data_loop_check(phi->in(1)) >= PhiNode::Unsafe) {
 362         break; // Found possible unsafe data loop.
 363       }
 364     }
 365   }
 366   if (i >= max) {
 367     return false; // An unsafe case was NOT found - don't need graph walk.
 368   }
 369   return true;
 370 }
 371 
 372 bool RegionNode::is_unreachable_from_root(const PhaseGVN* phase) const {
 373   ResourceMark rm;
 374   Node_List nstack;
 375   VectorSet visited;
 376 
 377   // Mark all control nodes reachable from root outputs
 378   Node* n = (Node*)phase->C->root();
 379   nstack.push(n);
 380   visited.set(n->_idx);
 381   while (nstack.size() != 0) {
 382     n = nstack.pop();
 383     uint max = n->outcnt();
 384     for (uint i = 0; i < max; i++) {
 385       Node* m = n->raw_out(i);
 386       if (m != nullptr && m->is_CFG()) {
 387         if (m == this) {
 388           return false; // We reached the Region node - it is not dead.
 389         }
 390         if (!visited.test_set(m->_idx))
 391           nstack.push(m);
 392       }
 393     }
 394   }
 395   return true; // The Region node is unreachable - it is dead.
 396 }
 397 
 398 #ifdef ASSERT
 399 // Is this region in an infinite subgraph?
 400 // (no path to root except through false NeverBranch exit)
 401 bool RegionNode::is_in_infinite_subgraph() {
 402   ResourceMark rm;
 403   Unique_Node_List worklist;
 404   worklist.push(this);
 405   return RegionNode::are_all_nodes_in_infinite_subgraph(worklist);
 406 }
 407 
 408 // Are all nodes in worklist in infinite subgraph?
 409 // (no path to root except through false NeverBranch exit)
 410 // worklist is directly used for the traversal
 411 bool RegionNode::are_all_nodes_in_infinite_subgraph(Unique_Node_List& worklist) {
 412   // BFS traversal down the CFG, except through NeverBranch exits
 413   for (uint i = 0; i < worklist.size(); ++i) {
 414     Node* n = worklist.at(i);
 415     assert(n->is_CFG(), "only traverse CFG");
 416     if (n->is_Root()) {
 417       // Found root -> there was an exit!
 418       return false;
 419     } else if (n->is_NeverBranch()) {
 420       // Only follow the loop-internal projection, not the NeverBranch exit
 421       ProjNode* proj = n->as_NeverBranch()->proj_out_or_null(0);
 422       assert(proj != nullptr, "must find loop-internal projection of NeverBranch");
 423       worklist.push(proj);
 424     } else {
 425       // Traverse all CFG outputs
 426       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
 427         Node* use = n->fast_out(i);
 428         if (use->is_CFG()) {
 429           worklist.push(use);
 430         }
 431       }
 432     }
 433   }
 434   // No exit found for any loop -> all are infinite
 435   return true;
 436 }
 437 #endif //ASSERT
 438 
 439 void RegionNode::set_loop_status(RegionNode::LoopStatus status) {
 440   assert(loop_status() == RegionNode::LoopStatus::NeverIrreducibleEntry, "why set our status again?");
 441   _loop_status = status;
 442 }
 443 
 444 #ifdef ASSERT
 445 void RegionNode::verify_can_be_irreducible_entry() const {
 446   assert(loop_status() == RegionNode::LoopStatus::MaybeIrreducibleEntry, "must be marked irreducible");
 447   assert(!is_Loop(), "LoopNode cannot be irreducible loop entry");
 448 }
 449 #endif //ASSERT
 450 
 451 void RegionNode::try_clean_mem_phis(PhaseIterGVN* igvn) {
 452   // Incremental inlining + PhaseStringOpts sometimes produce:
 453   //
 454   // cmpP with 1 top input
 455   //           |
 456   //          If
 457   //         /  \
 458   //   IfFalse  IfTrue  /- Some Node
 459   //         \  /      /    /
 460   //        Region    / /-MergeMem
 461   //             \---Phi
 462   //
 463   //
 464   // It's expected by PhaseStringOpts that the Region goes away and is
 465   // replaced by If's control input but because there's still a Phi,
 466   // the Region stays in the graph. The top input from the cmpP is
 467   // propagated forward and a subgraph that is useful goes away. The
 468   // code in PhiNode::try_clean_memory_phi() replaces the Phi with the
 469   // MergeMem in order to remove the Region if its last phi dies.
 470 
 471   if (!is_diamond()) {
 472     return;
 473   }
 474 
 475   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
 476     Node* phi = fast_out(i);
 477     if (phi->is_Phi() && phi->as_Phi()->try_clean_memory_phi(igvn)) {
 478       --i;
 479       --imax;
 480     }
 481   }
 482 }
 483 
 484 // Does this region merge a simple diamond formed by a proper IfNode?
 485 //
 486 //              Cmp
 487 //              /
 488 //     ctrl   Bool
 489 //       \    /
 490 //       IfNode
 491 //      /      \
 492 //  IfFalse   IfTrue
 493 //      \      /
 494 //       Region
 495 bool RegionNode::is_diamond() const {
 496   if (req() != 3) {
 497     return false;
 498   }
 499 
 500   Node* left_path = in(1);
 501   Node* right_path = in(2);
 502   if (left_path == nullptr || right_path == nullptr) {
 503     return false;
 504   }
 505   Node* diamond_if = left_path->in(0);
 506   if (diamond_if == nullptr || !diamond_if->is_If() || diamond_if != right_path->in(0)) {
 507     // Not an IfNode merging a diamond or TOP.
 508     return false;
 509   }
 510 
 511   // Check for a proper bool/cmp
 512   const Node* bol = diamond_if->in(1);
 513   if (!bol->is_Bool()) {
 514     return false;
 515   }
 516   const Node* cmp = bol->in(1);
 517   if (!cmp->is_Cmp()) {
 518     return false;
 519   }
 520   return true;
 521 }
 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;
 543         for( uint j=i+1; j<req(); j++ ) {
 544           if( in(j) && in(j)->in(0) == iff &&
 545               if1->Opcode() != in(j)->Opcode() ) {
 546             // Add the IF Projections to the worklist. They (and the IF itself)
 547             // will be eliminated if dead.
 548             phase->is_IterGVN()->add_users_to_worklist(iff);
 549             set_req(i, iff->in(0));// Skip around the useless IF diamond
 550             set_req(j, nullptr);
 551             return this;      // Record progress
 552           }
 553         }
 554       }
 555     }
 556   }
 557 
 558   // Remove TOP or null input paths. If only 1 input path remains, this Region
 559   // degrades to a copy.
 560   bool add_to_worklist = true;
 561   bool modified = false;
 562   int cnt = 0;                  // Count of values merging
 563   DEBUG_ONLY( int cnt_orig = req(); ) // Save original inputs count
 564   DEBUG_ONLY( uint outcnt_orig = outcnt(); )
 565   int del_it = 0;               // The last input path we delete
 566   bool found_top = false; // irreducible loops need to check reachability if we find TOP
 567   // For all inputs...
 568   for( uint i=1; i<req(); ++i ){// For all paths in
 569     Node *n = in(i);            // Get the input
 570     if( n != nullptr ) {
 571       // Remove useless control copy inputs
 572       if( n->is_Region() && n->as_Region()->is_copy() ) {
 573         set_req(i, n->nonnull_req());
 574         modified = true;
 575         i--;
 576         continue;
 577       }
 578       if( n->is_Proj() ) {      // Remove useless rethrows
 579         Node *call = n->in(0);
 580         if (call->is_Call() && call->as_Call()->entry_point() == OptoRuntime::rethrow_stub()) {
 581           set_req(i, call->in(0));
 582           modified = true;
 583           i--;
 584           continue;
 585         }
 586       }
 587       if( phase->type(n) == Type::TOP ) {
 588         set_req_X(i, nullptr, phase); // Ignore TOP inputs
 589         modified = true;
 590         found_top = true;
 591         i--;
 592         continue;
 593       }
 594       cnt++;                    // One more value merging
 595     } else if (can_reshape) {   // Else found dead path with DU info
 596       PhaseIterGVN *igvn = phase->is_IterGVN();
 597       del_req(i);               // Yank path from self
 598       del_it = i;
 599 
 600       for (DUIterator_Fast jmax, j = fast_outs(jmax); j < jmax; j++) {
 601         Node* use = fast_out(j);
 602 
 603         if (use->req() != req() && use->is_Phi()) {
 604           assert(use->in(0) == this, "unexpected control input");
 605           igvn->hash_delete(use);          // Yank from hash before hacking edges
 606           use->set_req_X(i, nullptr, igvn);// Correct DU info
 607           use->del_req(i);                 // Yank path from Phis
 608         }
 609       }
 610 
 611       if (add_to_worklist) {
 612         igvn->add_users_to_worklist(this);
 613         add_to_worklist = false;
 614       }
 615 
 616       i--;
 617     }
 618   }
 619 
 620   assert(outcnt() == outcnt_orig, "not expect to remove any use");
 621 
 622   if (can_reshape && found_top && loop_status() == RegionNode::LoopStatus::MaybeIrreducibleEntry) {
 623     // Is it a dead irreducible loop?
 624     // If an irreducible loop loses one of the multiple entries
 625     // that went into the loop head, or any secondary entries,
 626     // we need to verify if the irreducible loop is still reachable,
 627     // as the special logic in is_unreachable_region only works
 628     // for reducible loops.
 629     if (is_unreachable_from_root(phase)) {
 630       // The irreducible loop is dead - must remove it
 631       PhaseIterGVN* igvn = phase->is_IterGVN();
 632       remove_unreachable_subgraph(igvn);
 633       return nullptr;
 634     }
 635   } else if (can_reshape && cnt == 1) {
 636     // Is it dead loop?
 637     // If it is LoopNopde it had 2 (+1 itself) inputs and
 638     // one of them was cut. The loop is dead if it was EntryContol.
 639     // Loop node may have only one input because entry path
 640     // is removed in PhaseIdealLoop::Dominators().
 641     assert(!this->is_Loop() || cnt_orig <= 3, "Loop node should have 3 or less inputs");
 642     if ((this->is_Loop() && (del_it == LoopNode::EntryControl ||
 643                              (del_it == 0 && is_unreachable_region(phase)))) ||
 644         (!this->is_Loop() && has_phis && is_unreachable_region(phase))) {
 645       PhaseIterGVN* igvn = phase->is_IterGVN();
 646       remove_unreachable_subgraph(igvn);
 647       return nullptr;
 648     }
 649   }
 650 
 651   if( cnt <= 1 ) {              // Only 1 path in?
 652     set_req(0, nullptr);        // Null control input for region copy
 653     if( cnt == 0 && !can_reshape) { // Parse phase - leave the node as it is.
 654       // No inputs or all inputs are null.
 655       return nullptr;
 656     } else if (can_reshape) {   // Optimization phase - remove the node
 657       PhaseIterGVN *igvn = phase->is_IterGVN();
 658       // Strip mined (inner) loop is going away, remove outer loop.
 659       if (is_CountedLoop() &&
 660           as_Loop()->is_strip_mined()) {
 661         Node* outer_sfpt = as_CountedLoop()->outer_safepoint();
 662         Node* outer_out = as_CountedLoop()->outer_loop_exit();
 663         if (outer_sfpt != nullptr && outer_out != nullptr) {
 664           Node* in = outer_sfpt->in(0);
 665           igvn->replace_node(outer_out, in);
 666           LoopNode* outer = as_CountedLoop()->outer_loop();
 667           igvn->replace_input_of(outer, LoopNode::LoopBackControl, igvn->C->top());
 668         }
 669       }
 670       if (is_CountedLoop()) {
 671         Node* opaq = as_CountedLoop()->is_canonical_loop_entry();
 672         if (opaq != nullptr) {
 673           // This is not a loop anymore. No need to keep the Opaque1 node on the test that guards the loop as it won't be
 674           // subject to further loop opts.
 675           assert(opaq->Opcode() == Op_OpaqueZeroTripGuard, "");
 676           igvn->replace_node(opaq, opaq->in(1));
 677         }
 678       }
 679       Node *parent_ctrl;
 680       if( cnt == 0 ) {
 681         assert( req() == 1, "no inputs expected" );
 682         // During IGVN phase such region will be subsumed by TOP node
 683         // so region's phis will have TOP as control node.
 684         // Kill phis here to avoid it.
 685         // Also set other user's input to top.
 686         parent_ctrl = phase->C->top();
 687       } else {
 688         // The fallthrough case since we already checked dead loops above.
 689         parent_ctrl = in(1);
 690         assert(parent_ctrl != nullptr, "Region is a copy of some non-null control");
 691         assert(parent_ctrl != this, "Close dead loop");
 692       }
 693       if (add_to_worklist) {
 694         igvn->add_users_to_worklist(this); // Check for further allowed opts
 695       }
 696       for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
 697         Node* n = last_out(i);
 698         igvn->hash_delete(n); // Remove from worklist before modifying edges
 699         if (n->outcnt() == 0) {
 700           int uses_found = n->replace_edge(this, phase->C->top(), igvn);
 701           if (uses_found > 1) { // (--i) done at the end of the loop.
 702             i -= (uses_found - 1);
 703           }
 704           continue;
 705         }
 706         if( n->is_Phi() ) {   // Collapse all Phis
 707           // Eagerly replace phis to avoid regionless phis.
 708           Node* in;
 709           if( cnt == 0 ) {
 710             assert( n->req() == 1, "No data inputs expected" );
 711             in = parent_ctrl; // replaced by top
 712           } else {
 713             assert( n->req() == 2 &&  n->in(1) != nullptr, "Only one data input expected" );
 714             in = n->in(1);               // replaced by unique input
 715             if( n->as_Phi()->is_unsafe_data_reference(in) )
 716               in = phase->C->top();      // replaced by top
 717           }
 718           igvn->replace_node(n, in);
 719         }
 720         else if( n->is_Region() ) { // Update all incoming edges
 721           assert(n != this, "Must be removed from DefUse edges");
 722           int uses_found = n->replace_edge(this, parent_ctrl, igvn);
 723           if (uses_found > 1) { // (--i) done at the end of the loop.
 724             i -= (uses_found - 1);
 725           }
 726         }
 727         else {
 728           assert(n->in(0) == this, "Expect RegionNode to be control parent");
 729           n->set_req(0, parent_ctrl);
 730         }
 731 #ifdef ASSERT
 732         for( uint k=0; k < n->req(); k++ ) {
 733           assert(n->in(k) != this, "All uses of RegionNode should be gone");
 734         }
 735 #endif
 736       }
 737       // Remove the RegionNode itself from DefUse info
 738       igvn->remove_dead_node(this);
 739       return nullptr;
 740     }
 741     return this;                // Record progress
 742   }
 743 
 744 
 745   // If a Region flows into a Region, merge into one big happy merge.
 746   if (can_reshape) {
 747     Node *m = merge_region(this, phase);
 748     if (m != nullptr)  return m;
 749   }
 750 
 751   // Check if this region is the root of a clipping idiom on floats
 752   if( ConvertFloat2IntClipping && can_reshape && req() == 4 ) {
 753     // Check that only one use is a Phi and that it simplifies to two constants +
 754     PhiNode* phi = has_unique_phi();
 755     if (phi != nullptr) {          // One Phi user
 756       // Check inputs to the Phi
 757       ConNode *min;
 758       ConNode *max;
 759       Node    *val;
 760       uint     min_idx;
 761       uint     max_idx;
 762       uint     val_idx;
 763       if( check_phi_clipping( phi, min, min_idx, max, max_idx, val, val_idx )  ) {
 764         IfNode *top_if;
 765         IfNode *bot_if;
 766         if( check_if_clipping( this, bot_if, top_if ) ) {
 767           // Control pattern checks, now verify compares
 768           Node   *top_in = nullptr;   // value being compared against
 769           Node   *bot_in = nullptr;
 770           if( check_compare_clipping( true,  bot_if, min, bot_in ) &&
 771               check_compare_clipping( false, top_if, max, top_in ) ) {
 772             if( bot_in == top_in ) {
 773               PhaseIterGVN *gvn = phase->is_IterGVN();
 774               assert( gvn != nullptr, "Only had DefUse info in IterGVN");
 775               // Only remaining check is that bot_in == top_in == (Phi's val + mods)
 776 
 777               // Check for the ConvF2INode
 778               ConvF2INode *convf2i;
 779               if( check_convf2i_clipping( phi, val_idx, convf2i, min, max ) &&
 780                 convf2i->in(1) == bot_in ) {
 781                 // Matched pattern, including LShiftI; RShiftI, replace with integer compares
 782                 // max test
 783                 Node *cmp   = gvn->register_new_node_with_optimizer(new CmpINode( convf2i, min ));
 784                 Node *boo   = gvn->register_new_node_with_optimizer(new BoolNode( cmp, BoolTest::lt ));
 785                 IfNode *iff = (IfNode*)gvn->register_new_node_with_optimizer(new IfNode( top_if->in(0), boo, PROB_UNLIKELY_MAG(5), top_if->_fcnt ));
 786                 Node *if_min= gvn->register_new_node_with_optimizer(new IfTrueNode (iff));
 787                 Node *ifF   = gvn->register_new_node_with_optimizer(new IfFalseNode(iff));
 788                 // min test
 789                 cmp         = gvn->register_new_node_with_optimizer(new CmpINode( convf2i, max ));
 790                 boo         = gvn->register_new_node_with_optimizer(new BoolNode( cmp, BoolTest::gt ));
 791                 iff         = (IfNode*)gvn->register_new_node_with_optimizer(new IfNode( ifF, boo, PROB_UNLIKELY_MAG(5), bot_if->_fcnt ));
 792                 Node *if_max= gvn->register_new_node_with_optimizer(new IfTrueNode (iff));
 793                 ifF         = gvn->register_new_node_with_optimizer(new IfFalseNode(iff));
 794                 // update input edges to region node
 795                 set_req_X( min_idx, if_min, gvn );
 796                 set_req_X( max_idx, if_max, gvn );
 797                 set_req_X( val_idx, ifF,    gvn );
 798                 // remove unnecessary 'LShiftI; RShiftI' idiom
 799                 gvn->hash_delete(phi);
 800                 phi->set_req_X( val_idx, convf2i, gvn );
 801                 gvn->hash_find_insert(phi);
 802                 // Return transformed region node
 803                 return this;
 804               }
 805             }
 806           }
 807         }
 808       }
 809     }
 810   }
 811 
 812   if (can_reshape) {
 813     modified |= optimize_trichotomy(phase->is_IterGVN());
 814   }
 815 
 816   return modified ? this : nullptr;
 817 }
 818 
 819 //--------------------------remove_unreachable_subgraph----------------------
 820 // This region and therefore all nodes on the input control path(s) are unreachable
 821 // from root. To avoid incomplete removal of unreachable subgraphs, walk up the CFG
 822 // and aggressively replace all nodes by top.
 823 // If a control node "def" with a single control output "use" has its single output
 824 // "use" replaced with top, then "use" removes itself. This has the consequence that
 825 // when we visit "use", it already has all inputs removed. They are lost and we cannot
 826 // traverse them. This is why we fist find all unreachable nodes, and then remove
 827 // them in a second step.
 828 void RegionNode::remove_unreachable_subgraph(PhaseIterGVN* igvn) {
 829   Node* top = igvn->C->top();
 830   ResourceMark rm;
 831   Unique_Node_List unreachable; // visit each only once
 832   unreachable.push(this);
 833   // Recursively find all control inputs.
 834   for (uint i = 0; i < unreachable.size(); i++) {
 835     Node* n = unreachable.at(i);
 836     for (uint i = 0; i < n->req(); ++i) {
 837       Node* m = n->in(i);
 838       assert(m == nullptr || !m->is_Root(), "Should be unreachable from root");
 839       if (m != nullptr && m->is_CFG()) {
 840         unreachable.push(m);
 841       }
 842     }
 843   }
 844   // Remove all unreachable nodes.
 845   for (uint i = 0; i < unreachable.size(); i++) {
 846     Node* n = unreachable.at(i);
 847     if (n->is_Region()) {
 848       // Eagerly replace phis with top to avoid regionless phis.
 849       n->set_req(0, nullptr);
 850       bool progress = true;
 851       uint max = n->outcnt();
 852       DUIterator j;
 853       while (progress) {
 854         progress = false;
 855         for (j = n->outs(); n->has_out(j); j++) {
 856           Node* u = n->out(j);
 857           if (u->is_Phi()) {
 858             igvn->replace_node(u, top);
 859             if (max != n->outcnt()) {
 860               progress = true;
 861               j = n->refresh_out_pos(j);
 862               max = n->outcnt();
 863             }
 864           }
 865         }
 866       }
 867     }
 868     igvn->replace_node(n, top);
 869   }
 870 }
 871 
 872 //------------------------------optimize_trichotomy--------------------------
 873 // Optimize nested comparisons of the following kind:
 874 //
 875 // int compare(int a, int b) {
 876 //   return (a < b) ? -1 : (a == b) ? 0 : 1;
 877 // }
 878 //
 879 // Shape 1:
 880 // if (compare(a, b) == 1) { ... } -> if (a > b) { ... }
 881 //
 882 // Shape 2:
 883 // if (compare(a, b) == 0) { ... } -> if (a == b) { ... }
 884 //
 885 // Above code leads to the following IR shapes where both Ifs compare the
 886 // same value and two out of three region inputs idx1 and idx2 map to
 887 // the same value and control flow.
 888 //
 889 // (1)   If                 (2)   If
 890 //      /  \                     /  \
 891 //   Proj  Proj               Proj  Proj
 892 //     |      \                |      \
 893 //     |       If              |      If                      If
 894 //     |      /  \             |     /  \                    /  \
 895 //     |   Proj  Proj          |  Proj  Proj      ==>     Proj  Proj
 896 //     |   /      /            \    |    /                  |    /
 897 //    Region     /              \   |   /                   |   /
 898 //         \    /                \  |  /                    |  /
 899 //         Region                Region                    Region
 900 //
 901 // The method returns true if 'this' is modified and false otherwise.
 902 bool RegionNode::optimize_trichotomy(PhaseIterGVN* igvn) {
 903   int idx1 = 1, idx2 = 2;
 904   Node* region = nullptr;
 905   if (req() == 3 && in(1) != nullptr && in(2) != nullptr) {
 906     // Shape 1: Check if one of the inputs is a region that merges two control
 907     // inputs and has no other users (especially no Phi users).
 908     region = in(1)->isa_Region() ? in(1) : in(2)->isa_Region();
 909     if (region == nullptr || region->outcnt() != 2 || region->req() != 3) {
 910       return false; // No suitable region input found
 911     }
 912   } else if (req() == 4) {
 913     // Shape 2: Check if two control inputs map to the same value of the unique phi
 914     // user and treat these as if they would come from another region (shape (1)).
 915     PhiNode* phi = has_unique_phi();
 916     if (phi == nullptr) {
 917       return false; // No unique phi user
 918     }
 919     if (phi->in(idx1) != phi->in(idx2)) {
 920       idx2 = 3;
 921       if (phi->in(idx1) != phi->in(idx2)) {
 922         idx1 = 2;
 923         if (phi->in(idx1) != phi->in(idx2)) {
 924           return false; // No equal phi inputs found
 925         }
 926       }
 927     }
 928     assert(phi->in(idx1) == phi->in(idx2), "must be"); // Region is merging same value
 929     region = this;
 930   }
 931   if (region == nullptr || region->in(idx1) == nullptr || region->in(idx2) == nullptr) {
 932     return false; // Region does not merge two control inputs
 933   }
 934   // At this point we know that region->in(idx1) and region->(idx2) map to the same
 935   // value and control flow. Now search for ifs that feed into these region inputs.
 936   ProjNode* proj1 = region->in(idx1)->isa_Proj();
 937   ProjNode* proj2 = region->in(idx2)->isa_Proj();
 938   if (proj1 == nullptr || proj1->outcnt() != 1 ||
 939       proj2 == nullptr || proj2->outcnt() != 1) {
 940     return false; // No projection inputs with region as unique user found
 941   }
 942   assert(proj1 != proj2, "should be different projections");
 943   IfNode* iff1 = proj1->in(0)->isa_If();
 944   IfNode* iff2 = proj2->in(0)->isa_If();
 945   if (iff1 == nullptr || iff1->outcnt() != 2 ||
 946       iff2 == nullptr || iff2->outcnt() != 2) {
 947     return false; // No ifs found
 948   }
 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              cmp1->is_FlatArrayCheck() || cmp2->is_FlatArrayCheck()) {
 971     // Floats and pointers don't exactly obey trichotomy. To be on the safe side, don't transform their tests.
 972     // SubTypeCheck is not commutative
 973     return false;
 974   } else if (cmp1 != cmp2) {
 975     if (cmp1->in(1) == cmp2->in(2) &&
 976         cmp1->in(2) == cmp2->in(1)) {
 977       commute = true; // Same but swapped inputs, commute the test
 978     } else {
 979       return false; // Ifs are not comparing the same values
 980     }
 981   }
 982   proj1 = proj1->other_if_proj();
 983   proj2 = proj2->other_if_proj();
 984   if (!((proj1->unique_ctrl_out_or_null() == iff2 &&
 985          proj2->unique_ctrl_out_or_null() == this) ||
 986         (proj2->unique_ctrl_out_or_null() == iff1 &&
 987          proj1->unique_ctrl_out_or_null() == this))) {
 988     return false; // Ifs are not connected through other projs
 989   }
 990   // Found 'iff -> proj -> iff -> proj -> this' shape where all other projs are merged
 991   // through 'region' and map to the same value. Merge the boolean tests and replace
 992   // the ifs by a single comparison.
 993   BoolTest test1 = (proj1->_con == 1) ? bol1->_test : bol1->_test.negate();
 994   BoolTest test2 = (proj2->_con == 1) ? bol2->_test : bol2->_test.negate();
 995   test1 = commute ? test1.commute() : test1;
 996   // After possibly commuting test1, if we can merge test1 & test2, then proj2/iff2/bol2 are the nodes to refine.
 997   BoolTest::mask res = test1.merge(test2);
 998   if (res == BoolTest::illegal) {
 999     return false; // Unable to merge tests
1000   }
1001   // Adjust iff1 to always pass (only iff2 will remain)
1002   igvn->replace_input_of(iff1, 1, igvn->intcon(proj1->_con));
1003   if (res == BoolTest::never) {
1004     // Merged test is always false, adjust iff2 to always fail
1005     igvn->replace_input_of(iff2, 1, igvn->intcon(1 - proj2->_con));
1006   } else {
1007     // Replace bool input of iff2 with merged test
1008     BoolNode* new_bol = new BoolNode(bol2->in(1), res);
1009     igvn->replace_input_of(iff2, 1, igvn->transform((proj2->_con == 1) ? new_bol : new_bol->negate(igvn)));
1010     if (new_bol->outcnt() == 0) {
1011       igvn->remove_dead_node(new_bol);
1012     }
1013   }
1014   return false;
1015 }
1016 
1017 const RegMask &RegionNode::out_RegMask() const {
1018   return RegMask::Empty;
1019 }
1020 
1021 #ifndef PRODUCT
1022 void RegionNode::dump_spec(outputStream* st) const {
1023   Node::dump_spec(st);
1024   switch (loop_status()) {
1025   case RegionNode::LoopStatus::MaybeIrreducibleEntry:
1026     st->print("#irreducible ");
1027     break;
1028   case RegionNode::LoopStatus::Reducible:
1029     st->print("#reducible ");
1030     break;
1031   case RegionNode::LoopStatus::NeverIrreducibleEntry:
1032     break; // nothing
1033   }
1034 }
1035 #endif
1036 
1037 // Find the one non-null required input.  RegionNode only
1038 Node *Node::nonnull_req() const {
1039   assert( is_Region(), "" );
1040   for( uint i = 1; i < _cnt; i++ )
1041     if( in(i) )
1042       return in(i);
1043   ShouldNotReachHere();
1044   return nullptr;
1045 }
1046 
1047 
1048 //=============================================================================
1049 // note that these functions assume that the _adr_type field is flat
1050 uint PhiNode::hash() const {
1051   const Type* at = _adr_type;
1052   return TypeNode::hash() + (at ? at->hash() : 0);
1053 }
1054 bool PhiNode::cmp( const Node &n ) const {
1055   return TypeNode::cmp(n) && _adr_type == ((PhiNode&)n)._adr_type;
1056 }
1057 static inline
1058 const TypePtr* flatten_phi_adr_type(const TypePtr* at) {
1059   if (at == nullptr || at == TypePtr::BOTTOM)  return at;
1060   return Compile::current()->alias_type(at)->adr_type();
1061 }
1062 
1063 //----------------------------make---------------------------------------------
1064 // create a new phi with edges matching r and set (initially) to x
1065 PhiNode* PhiNode::make(Node* r, Node* x, const Type *t, const TypePtr* at) {
1066   uint preds = r->req();   // Number of predecessor paths
1067   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");
1068   PhiNode* p = new PhiNode(r, t, at);
1069   for (uint j = 1; j < preds; j++) {
1070     // Fill in all inputs, except those which the region does not yet have
1071     if (r->in(j) != nullptr)
1072       p->init_req(j, x);
1073   }
1074   return p;
1075 }
1076 PhiNode* PhiNode::make(Node* r, Node* x) {
1077   const Type*    t  = x->bottom_type();
1078   const TypePtr* at = nullptr;
1079   if (t == Type::MEMORY)  at = flatten_phi_adr_type(x->adr_type());
1080   return make(r, x, t, at);
1081 }
1082 PhiNode* PhiNode::make_blank(Node* r, Node* x) {
1083   const Type*    t  = x->bottom_type();
1084   const TypePtr* at = nullptr;
1085   if (t == Type::MEMORY)  at = flatten_phi_adr_type(x->adr_type());
1086   return new PhiNode(r, t, at);
1087 }
1088 
1089 
1090 //------------------------slice_memory-----------------------------------------
1091 // create a new phi with narrowed memory type
1092 PhiNode* PhiNode::slice_memory(const TypePtr* adr_type) const {
1093   PhiNode* mem = (PhiNode*) clone();
1094   *(const TypePtr**)&mem->_adr_type = adr_type;
1095   // convert self-loops, or else we get a bad graph
1096   for (uint i = 1; i < req(); i++) {
1097     if ((const Node*)in(i) == this)  mem->set_req(i, mem);
1098   }
1099   mem->verify_adr_type();
1100   return mem;
1101 }
1102 
1103 //------------------------split_out_instance-----------------------------------
1104 // Split out an instance type from a bottom phi.
1105 PhiNode* PhiNode::split_out_instance(const TypePtr* at, PhaseIterGVN *igvn) const {
1106   const TypeOopPtr *t_oop = at->isa_oopptr();
1107   assert(t_oop != nullptr && t_oop->is_known_instance(), "expecting instance oopptr");
1108 
1109   // Check if an appropriate node already exists.
1110   Node *region = in(0);
1111   for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
1112     Node* use = region->fast_out(k);
1113     if( use->is_Phi()) {
1114       PhiNode *phi2 = use->as_Phi();
1115       if (phi2->type() == Type::MEMORY && phi2->adr_type() == at) {
1116         return phi2;
1117       }
1118     }
1119   }
1120   Compile *C = igvn->C;
1121   Node_Array node_map;
1122   Node_Stack stack(C->live_nodes() >> 4);
1123   PhiNode *nphi = slice_memory(at);
1124   igvn->register_new_node_with_optimizer( nphi );
1125   node_map.map(_idx, nphi);
1126   stack.push((Node *)this, 1);
1127   while(!stack.is_empty()) {
1128     PhiNode *ophi = stack.node()->as_Phi();
1129     uint i = stack.index();
1130     assert(i >= 1, "not control edge");
1131     stack.pop();
1132     nphi = node_map[ophi->_idx]->as_Phi();
1133     for (; i < ophi->req(); i++) {
1134       Node *in = ophi->in(i);
1135       if (in == nullptr || igvn->type(in) == Type::TOP)
1136         continue;
1137       Node *opt = MemNode::optimize_simple_memory_chain(in, t_oop, nullptr, igvn);
1138       PhiNode *optphi = opt->is_Phi() ? opt->as_Phi() : nullptr;
1139       if (optphi != nullptr && optphi->adr_type() == TypePtr::BOTTOM) {
1140         opt = node_map[optphi->_idx];
1141         if (opt == nullptr) {
1142           stack.push(ophi, i);
1143           nphi = optphi->slice_memory(at);
1144           igvn->register_new_node_with_optimizer( nphi );
1145           node_map.map(optphi->_idx, nphi);
1146           ophi = optphi;
1147           i = 0; // will get incremented at top of loop
1148           continue;
1149         }
1150       }
1151       nphi->set_req(i, opt);
1152     }
1153   }
1154   return nphi;
1155 }
1156 
1157 //------------------------verify_adr_type--------------------------------------
1158 #ifdef ASSERT
1159 void PhiNode::verify_adr_type(VectorSet& visited, const TypePtr* at) const {
1160   if (visited.test_set(_idx))  return;  //already visited
1161 
1162   // recheck constructor invariants:
1163   verify_adr_type(false);
1164 
1165   // recheck local phi/phi consistency:
1166   assert(_adr_type == at || _adr_type == TypePtr::BOTTOM,
1167          "adr_type must be consistent across phi nest");
1168 
1169   // walk around
1170   for (uint i = 1; i < req(); i++) {
1171     Node* n = in(i);
1172     if (n == nullptr)  continue;
1173     const Node* np = in(i);
1174     if (np->is_Phi()) {
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   // Flat array element shouldn't get their own memory slice until flat_accesses_share_alias is cleared.
1196   // It could be the graph has no loads/stores and flat_accesses_share_alias is never cleared. EA could still
1197   // creates per element Phis but that wouldn't be a problem as there are no memory accesses for that array.
1198   assert(_adr_type == nullptr || _adr_type->isa_aryptr() == nullptr ||
1199          _adr_type->is_aryptr()->is_known_instance() ||
1200          !_adr_type->is_aryptr()->is_flat() ||
1201          !Compile::current()->flat_accesses_share_alias() ||
1202          _adr_type == TypeAryPtr::INLINES, "flat array element shouldn't get its own slice yet");
1203 
1204   if (!VerifyAliases)       return;  // verify thoroughly only if requested
1205 
1206   assert(_adr_type == flatten_phi_adr_type(_adr_type),
1207          "Phi::adr_type must be pre-normalized");
1208 
1209   if (recursive) {
1210     VectorSet visited;
1211     verify_adr_type(visited, _adr_type);
1212   }
1213 }
1214 #endif
1215 
1216 
1217 //------------------------------Value------------------------------------------
1218 // Compute the type of the PhiNode
1219 const Type* PhiNode::Value(PhaseGVN* phase) const {
1220   Node *r = in(0);              // RegionNode
1221   if( !r )                      // Copy or dead
1222     return in(1) ? phase->type(in(1)) : Type::TOP;
1223 
1224   // Note: During parsing, phis are often transformed before their regions.
1225   // This means we have to use type_or_null to defend against untyped regions.
1226   if( phase->type_or_null(r) == Type::TOP )  // Dead code?
1227     return Type::TOP;
1228 
1229   // Check for trip-counted loop.  If so, be smarter.
1230   BaseCountedLoopNode* l = r->is_BaseCountedLoop() ? r->as_BaseCountedLoop() : nullptr;
1231   if (l && ((const Node*)l->phi() == this)) { // Trip counted loop!
1232     // protect against init_trip() or limit() returning null
1233     if (l->can_be_counted_loop(phase)) {
1234       const Node* init = l->init_trip();
1235       const Node* limit = l->limit();
1236       const Node* stride = l->stride();
1237       if (init != nullptr && limit != nullptr && stride != nullptr) {
1238         const TypeInteger* lo = phase->type(init)->isa_integer(l->bt());
1239         const TypeInteger* hi = phase->type(limit)->isa_integer(l->bt());
1240         const TypeInteger* stride_t = phase->type(stride)->isa_integer(l->bt());
1241         if (lo != nullptr && hi != nullptr && stride_t != nullptr) { // Dying loops might have TOP here
1242           assert(stride_t->is_con(), "bad stride type");
1243           BoolTest::mask bt = l->loopexit()->test_trip();
1244           // If the loop exit condition is "not equal", the condition
1245           // would not trigger if init > limit (if stride > 0) or if
1246           // init < limit if (stride > 0) so we can't deduce bounds
1247           // for the iv from the exit condition.
1248           if (bt != BoolTest::ne) {
1249             jlong stride_con = stride_t->get_con_as_long(l->bt());
1250             if (stride_con < 0) {          // Down-counter loop
1251               swap(lo, hi);
1252               jlong iv_range_lower_limit = lo->lo_as_long();
1253               // Prevent overflow when adding one below
1254               if (iv_range_lower_limit < max_signed_integer(l->bt())) {
1255                 // The loop exit condition is: iv + stride > limit (iv is this Phi). So the loop iterates until
1256                 // iv + stride <= limit
1257                 // We know that: limit >= lo->lo_as_long() and stride <= -1
1258                 // So when the loop exits, iv has to be at most lo->lo_as_long() + 1
1259                 iv_range_lower_limit += 1; // lo is after decrement
1260                 // Exact bounds for the phi can be computed when ABS(stride) greater than 1 if bounds are constant.
1261                 if (lo->is_con() && hi->is_con() && hi->lo_as_long() > lo->hi_as_long() && stride_con != -1) {
1262                   julong uhi = static_cast<julong>(hi->lo_as_long());
1263                   julong ulo = static_cast<julong>(lo->hi_as_long());
1264                   julong diff = ((uhi - ulo - 1) / (-stride_con)) * (-stride_con);
1265                   julong ufirst = hi->lo_as_long() - diff;
1266                   iv_range_lower_limit = reinterpret_cast<jlong &>(ufirst);
1267                   assert(iv_range_lower_limit >= lo->lo_as_long() + 1, "should end up with narrower range");
1268                 }
1269               }
1270               return TypeInteger::make(MIN2(iv_range_lower_limit, hi->lo_as_long()), hi->hi_as_long(), 3, l->bt())->filter_speculative(_type);
1271             } else if (stride_con >= 0) {
1272               jlong iv_range_upper_limit = hi->hi_as_long();
1273               // Prevent overflow when subtracting one below
1274               if (iv_range_upper_limit > min_signed_integer(l->bt())) {
1275                 // The loop exit condition is: iv + stride < limit (iv is this Phi). So the loop iterates until
1276                 // iv + stride >= limit
1277                 // We know that: limit <= hi->hi_as_long() and stride >= 1
1278                 // So when the loop exits, iv has to be at most hi->hi_as_long() - 1
1279                 iv_range_upper_limit -= 1;
1280                 // Exact bounds for the phi can be computed when ABS(stride) greater than 1 if bounds are constant.
1281                 if (lo->is_con() && hi->is_con() && hi->lo_as_long() > lo->hi_as_long() && stride_con != 1) {
1282                   julong uhi = static_cast<julong>(hi->lo_as_long());
1283                   julong ulo = static_cast<julong>(lo->hi_as_long());
1284                   julong diff = ((uhi - ulo - 1) / stride_con) * stride_con;
1285                   julong ulast = lo->hi_as_long() + diff;
1286                   iv_range_upper_limit = reinterpret_cast<jlong &>(ulast);
1287                   assert(iv_range_upper_limit <= hi->hi_as_long() - 1, "should end up with narrower range");
1288                 }
1289               }
1290               return TypeInteger::make(lo->lo_as_long(), MAX2(lo->hi_as_long(), iv_range_upper_limit), 3, l->bt())->filter_speculative(_type);
1291             }
1292           }
1293         }
1294       }
1295     } else if (l->in(LoopNode::LoopBackControl) != nullptr &&
1296                in(LoopNode::EntryControl) != nullptr &&
1297                phase->type(l->in(LoopNode::LoopBackControl)) == Type::TOP) {
1298       // During CCP, if we saturate the type of a counted loop's Phi
1299       // before the special code for counted loop above has a chance
1300       // to run (that is as long as the type of the backedge's control
1301       // is top), we might end up with non monotonic types
1302       return phase->type(in(LoopNode::EntryControl))->filter_speculative(_type);
1303     }
1304   }
1305 
1306   // Default case: merge all inputs
1307   const Type *t = Type::TOP;        // Merged type starting value
1308   for (uint i = 1; i < req(); ++i) {// For all paths in
1309     // Reachable control path?
1310     if (r->in(i) && phase->type(r->in(i)) == Type::CONTROL) {
1311       const Type* ti = phase->type(in(i));
1312       t = t->meet_speculative(ti);
1313     }
1314   }
1315 
1316   // The worst-case type (from ciTypeFlow) should be consistent with "t".
1317   // That is, we expect that "t->higher_equal(_type)" holds true.
1318   // There are various exceptions:
1319   // - Inputs which are phis might in fact be widened unnecessarily.
1320   //   For example, an input might be a widened int while the phi is a short.
1321   // - Inputs might be BotPtrs but this phi is dependent on a null check,
1322   //   and postCCP has removed the cast which encodes the result of the check.
1323   // - The type of this phi is an interface, and the inputs are classes.
1324   // - Value calls on inputs might produce fuzzy results.
1325   //   (Occurrences of this case suggest improvements to Value methods.)
1326   //
1327   // It is not possible to see Type::BOTTOM values as phi inputs,
1328   // because the ciTypeFlow pre-pass produces verifier-quality types.
1329   const Type* ft = t->filter_speculative(_type);  // Worst case type
1330 
1331 #ifdef ASSERT
1332   // The following logic has been moved into TypeOopPtr::filter.
1333   const Type* jt = t->join_speculative(_type);
1334   if (jt->empty()) {           // Emptied out???
1335     // Otherwise it's something stupid like non-overlapping int ranges
1336     // found on dying counted loops.
1337     assert(ft == Type::TOP, ""); // Canonical empty value
1338   }
1339 
1340   else {
1341 
1342     if (jt != ft && jt->base() == ft->base()) {
1343       if (jt->isa_int() &&
1344           jt->is_int()->_lo == ft->is_int()->_lo &&
1345           jt->is_int()->_hi == ft->is_int()->_hi)
1346         jt = ft;
1347       if (jt->isa_long() &&
1348           jt->is_long()->_lo == ft->is_long()->_lo &&
1349           jt->is_long()->_hi == ft->is_long()->_hi)
1350         jt = ft;
1351     }
1352     if (jt != ft) {
1353       tty->print("merge type:  "); t->dump(); tty->cr();
1354       tty->print("kill type:   "); _type->dump(); tty->cr();
1355       tty->print("join type:   "); jt->dump(); tty->cr();
1356       tty->print("filter type: "); ft->dump(); tty->cr();
1357     }
1358     assert(jt == ft, "");
1359   }
1360 #endif //ASSERT
1361 
1362   // Deal with conversion problems found in data loops.
1363   ft = phase->saturate_and_maybe_push_to_igvn_worklist(this, ft);
1364   return ft;
1365 }
1366 
1367 // Does this Phi represent a simple well-shaped diamond merge?  Return the
1368 // index of the true path or 0 otherwise.
1369 int PhiNode::is_diamond_phi() const {
1370   Node* region = in(0);
1371   assert(region != nullptr && region->is_Region(), "phi must have region");
1372   if (!region->as_Region()->is_diamond()) {
1373     return 0;
1374   }
1375 
1376   if (region->in(1)->is_IfTrue()) {
1377     assert(region->in(2)->is_IfFalse(), "bad If");
1378     return 1;
1379   } else {
1380     // Flipped projections.
1381     assert(region->in(2)->is_IfTrue(), "bad If");
1382     return 2;
1383   }
1384 }
1385 
1386 // Do the following transformation if we find the corresponding graph shape, remove the involved memory phi and return
1387 // true. Otherwise, return false if the transformation cannot be applied.
1388 //
1389 //           If                                     If
1390 //          /  \                                   /  \
1391 //    IfFalse  IfTrue  /- Some Node          IfFalse  IfTrue
1392 //          \  /      /    /                       \  /        Some Node
1393 //         Region    / /-MergeMem     ===>        Region          |
1394 //          /   \---Phi                             |          MergeMem
1395 // [other phis]      \                        [other phis]        |
1396 //                   use                                         use
1397 bool PhiNode::try_clean_memory_phi(PhaseIterGVN* igvn) {
1398   if (_type != Type::MEMORY) {
1399     return false;
1400   }
1401   assert(is_diamond_phi() > 0, "sanity");
1402   assert(req() == 3, "same as region");
1403   const Node* region = in(0);
1404   for (uint i = 1; i < 3; i++) {
1405     Node* phi_input = in(i);
1406     if (phi_input != nullptr && phi_input->is_MergeMem() && region->in(i)->outcnt() == 1) {
1407       // Nothing is control-dependent on path #i except the region itself.
1408       MergeMemNode* merge_mem = phi_input->as_MergeMem();
1409       uint j = 3 - i;
1410       Node* other_phi_input = in(j);
1411       if (other_phi_input != nullptr && other_phi_input == merge_mem->base_memory()) {
1412         // merge_mem is a successor memory to other_phi_input, and is not pinned inside the diamond, so push it out.
1413         // This will allow the diamond to collapse completely if there are no other phis left.
1414         igvn->replace_node(this, merge_mem);
1415         return true;
1416       }
1417     }
1418   }
1419   return false;
1420 }
1421 
1422 //----------------------------check_cmove_id-----------------------------------
1423 // Check for CMove'ing a constant after comparing against the constant.
1424 // Happens all the time now, since if we compare equality vs a constant in
1425 // the parser, we "know" the variable is constant on one path and we force
1426 // it.  Thus code like "if( x==0 ) {/*EMPTY*/}" ends up inserting a
1427 // conditional move: "x = (x==0)?0:x;".  Yucko.  This fix is slightly more
1428 // general in that we don't need constants.  Since CMove's are only inserted
1429 // in very special circumstances, we do it here on generic Phi's.
1430 Node* PhiNode::is_cmove_id(PhaseTransform* phase, int true_path) {
1431   assert(true_path !=0, "only diamond shape graph expected");
1432 
1433   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1434   // phi->region->if_proj->ifnode->bool->cmp
1435   Node*     region = in(0);
1436   Node*     iff    = region->in(1)->in(0);
1437   BoolNode* b      = iff->in(1)->as_Bool();
1438   Node*     cmp    = b->in(1);
1439   Node*     tval   = in(true_path);
1440   Node*     fval   = in(3-true_path);
1441   Node*     id     = CMoveNode::is_cmove_id(phase, cmp, tval, fval, b);
1442   if (id == nullptr)
1443     return nullptr;
1444 
1445   // Either value might be a cast that depends on a branch of 'iff'.
1446   // Since the 'id' value will float free of the diamond, either
1447   // decast or return failure.
1448   Node* ctl = id->in(0);
1449   if (ctl != nullptr && ctl->in(0) == iff) {
1450     if (id->is_ConstraintCast()) {
1451       return id->in(1);
1452     } else {
1453       // Don't know how to disentangle this value.
1454       return nullptr;
1455     }
1456   }
1457 
1458   return id;
1459 }
1460 
1461 //------------------------------Identity---------------------------------------
1462 // Check for Region being Identity.
1463 Node* PhiNode::Identity(PhaseGVN* phase) {
1464   if (must_wait_for_region_in_irreducible_loop(phase)) {
1465     return this;
1466   }
1467   // Check for no merging going on
1468   // (There used to be special-case code here when this->region->is_Loop.
1469   // It would check for a tributary phi on the backedge that the main phi
1470   // trivially, perhaps with a single cast.  The unique_input method
1471   // does all this and more, by reducing such tributaries to 'this'.)
1472   Node* uin = unique_input(phase, false);
1473   if (uin != nullptr) {
1474     return uin;
1475   }
1476 
1477   int true_path = is_diamond_phi();
1478   // Delay CMove'ing identity if Ideal has not had the chance to handle unsafe cases, yet.
1479   if (true_path != 0 && !(phase->is_IterGVN() && wait_for_region_igvn(phase))) {
1480     Node* id = is_cmove_id(phase, true_path);
1481     if (id != nullptr) {
1482       return id;
1483     }
1484   }
1485 
1486   // Looking for phis with identical inputs.  If we find one that has
1487   // type TypePtr::BOTTOM, replace the current phi with the bottom phi.
1488   if (phase->is_IterGVN() && type() == Type::MEMORY && adr_type() !=
1489       TypePtr::BOTTOM && !adr_type()->is_known_instance()) {
1490     uint phi_len = req();
1491     Node* phi_reg = region();
1492     for (DUIterator_Fast imax, i = phi_reg->fast_outs(imax); i < imax; i++) {
1493       Node* u = phi_reg->fast_out(i);
1494       if (u->is_Phi() && u->as_Phi()->type() == Type::MEMORY &&
1495           u->adr_type() == TypePtr::BOTTOM && u->in(0) == phi_reg &&
1496           u->req() == phi_len) {
1497         for (uint j = 1; j < phi_len; j++) {
1498           if (in(j) != u->in(j)) {
1499             u = nullptr;
1500             break;
1501           }
1502         }
1503         if (u != nullptr) {
1504           return u;
1505         }
1506       }
1507     }
1508   }
1509 
1510   return this;                     // No identity
1511 }
1512 
1513 //-----------------------------unique_input------------------------------------
1514 // Find the unique value, discounting top, self-loops, and casts.
1515 // Return top if there are no inputs, and self if there are multiple.
1516 Node* PhiNode::unique_input(PhaseValues* phase, bool uncast) {
1517   //  1) One unique direct input,
1518   // or if uncast is true:
1519   //  2) some of the inputs have an intervening ConstraintCast
1520   //  3) an input is a self loop
1521   //
1522   //  1) input   or   2) input     or   3) input __
1523   //     /   \           /   \               \  /  \
1524   //     \   /          |    cast             phi  cast
1525   //      phi            \   /               /  \  /
1526   //                      phi               /    --
1527 
1528   Node* r = in(0);                      // RegionNode
1529   Node* input = nullptr; // The unique direct input (maybe uncasted = ConstraintCasts removed)
1530 
1531   for (uint i = 1, cnt = req(); i < cnt; ++i) {
1532     Node* rc = r->in(i);
1533     if (rc == nullptr || phase->type(rc) == Type::TOP)
1534       continue;                 // ignore unreachable control path
1535     Node* n = in(i);
1536     if (n == nullptr)
1537       continue;
1538     Node* un = n;
1539     if (uncast) {
1540 #ifdef ASSERT
1541       Node* m = un->uncast();
1542 #endif
1543       while (un != nullptr && un->req() == 2 && un->is_ConstraintCast()) {
1544         Node* next = un->in(1);
1545         if (phase->type(next)->isa_rawptr() && phase->type(un)->isa_oopptr()) {
1546           // risk exposing raw ptr at safepoint
1547           break;
1548         }
1549         un = next;
1550       }
1551       assert(m == un || un->in(1) == m, "Only expected at CheckCastPP from allocation");
1552     }
1553     if (un == nullptr || un == this || phase->type(un) == Type::TOP) {
1554       continue; // ignore if top, or in(i) and "this" are in a data cycle
1555     }
1556     // Check for a unique input (maybe uncasted)
1557     if (input == nullptr) {
1558       input = un;
1559     } else if (input != un) {
1560       input = NodeSentinel; // no unique input
1561     }
1562   }
1563   if (input == nullptr) {
1564     return phase->C->top();        // no inputs
1565   }
1566 
1567   if (input != NodeSentinel) {
1568     return input;           // one unique direct input
1569   }
1570 
1571   // Nothing.
1572   return nullptr;
1573 }
1574 
1575 //------------------------------is_x2logic-------------------------------------
1576 // Check for simple convert-to-boolean pattern
1577 // If:(C Bool) Region:(IfF IfT) Phi:(Region 0 1)
1578 // Convert Phi to an ConvIB.
1579 static Node *is_x2logic( PhaseGVN *phase, PhiNode *phi, int true_path ) {
1580   assert(true_path !=0, "only diamond shape graph expected");
1581 
1582   // If we're late in the optimization process, we may have already expanded Conv2B nodes
1583   if (phase->C->post_loop_opts_phase() && !Matcher::match_rule_supported(Op_Conv2B)) {
1584     return nullptr;
1585   }
1586 
1587   // Convert the true/false index into an expected 0/1 return.
1588   // Map 2->0 and 1->1.
1589   int flipped = 2-true_path;
1590 
1591   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1592   // phi->region->if_proj->ifnode->bool->cmp
1593   Node *region = phi->in(0);
1594   Node *iff = region->in(1)->in(0);
1595   BoolNode *b = (BoolNode*)iff->in(1);
1596   const CmpNode *cmp = (CmpNode*)b->in(1);
1597 
1598   Node *zero = phi->in(1);
1599   Node *one  = phi->in(2);
1600   const Type *tzero = phase->type( zero );
1601   const Type *tone  = phase->type( one  );
1602 
1603   // Check for compare vs 0
1604   const Type *tcmp = phase->type(cmp->in(2));
1605   if( tcmp != TypeInt::ZERO && tcmp != TypePtr::NULL_PTR ) {
1606     // Allow cmp-vs-1 if the other input is bounded by 0-1
1607     if( !(tcmp == TypeInt::ONE && phase->type(cmp->in(1)) == TypeInt::BOOL) )
1608       return nullptr;
1609     flipped = 1-flipped;        // Test is vs 1 instead of 0!
1610   }
1611 
1612   // Check for setting zero/one opposite expected
1613   if( tzero == TypeInt::ZERO ) {
1614     if( tone == TypeInt::ONE ) {
1615     } else return nullptr;
1616   } else if( tzero == TypeInt::ONE ) {
1617     if( tone == TypeInt::ZERO ) {
1618       flipped = 1-flipped;
1619     } else return nullptr;
1620   } else return nullptr;
1621 
1622   // Check for boolean test backwards
1623   if( b->_test._test == BoolTest::ne ) {
1624   } else if( b->_test._test == BoolTest::eq ) {
1625     flipped = 1-flipped;
1626   } else return nullptr;
1627 
1628   // Build int->bool conversion
1629   Node* n = new Conv2BNode(cmp->in(1));
1630   if (flipped) {
1631     n = new XorINode(phase->transform(n), phase->intcon(1));
1632   }
1633 
1634   return n;
1635 }
1636 
1637 //------------------------------is_cond_add------------------------------------
1638 // Check for simple conditional add pattern:  "(P < Q) ? X+Y : X;"
1639 // To be profitable the control flow has to disappear; there can be no other
1640 // values merging here.  We replace the test-and-branch with:
1641 // "(sgn(P-Q))&Y) + X".  Basically, convert "(P < Q)" into 0 or -1 by
1642 // moving the carry bit from (P-Q) into a register with 'sbb EAX,EAX'.
1643 // Then convert Y to 0-or-Y and finally add.
1644 // This is a key transform for SpecJava _201_compress.
1645 static Node* is_cond_add(PhaseGVN *phase, PhiNode *phi, int true_path) {
1646   assert(true_path !=0, "only diamond shape graph expected");
1647 
1648   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1649   // phi->region->if_proj->ifnode->bool->cmp
1650   RegionNode *region = (RegionNode*)phi->in(0);
1651   Node *iff = region->in(1)->in(0);
1652   BoolNode* b = iff->in(1)->as_Bool();
1653   const CmpNode *cmp = (CmpNode*)b->in(1);
1654 
1655   // Make sure only merging this one phi here
1656   if (region->has_unique_phi() != phi)  return nullptr;
1657 
1658   // Make sure each arm of the diamond has exactly one output, which we assume
1659   // is the region.  Otherwise, the control flow won't disappear.
1660   if (region->in(1)->outcnt() != 1) return nullptr;
1661   if (region->in(2)->outcnt() != 1) return nullptr;
1662 
1663   // Check for "(P < Q)" of type signed int
1664   if (b->_test._test != BoolTest::lt)  return nullptr;
1665   if (cmp->Opcode() != Op_CmpI)        return nullptr;
1666 
1667   Node *p = cmp->in(1);
1668   Node *q = cmp->in(2);
1669   Node *n1 = phi->in(  true_path);
1670   Node *n2 = phi->in(3-true_path);
1671 
1672   int op = n1->Opcode();
1673   if( op != Op_AddI           // Need zero as additive identity
1674       /*&&op != Op_SubI &&
1675       op != Op_AddP &&
1676       op != Op_XorI &&
1677       op != Op_OrI*/ )
1678     return nullptr;
1679 
1680   Node *x = n2;
1681   Node *y = nullptr;
1682   if( x == n1->in(1) ) {
1683     y = n1->in(2);
1684   } else if( x == n1->in(2) ) {
1685     y = n1->in(1);
1686   } else return nullptr;
1687 
1688   // Not so profitable if compare and add are constants
1689   if( q->is_Con() && phase->type(q) != TypeInt::ZERO && y->is_Con() )
1690     return nullptr;
1691 
1692   Node *cmplt = phase->transform( new CmpLTMaskNode(p,q) );
1693   Node *j_and   = phase->transform( new AndINode(cmplt,y) );
1694   return new AddINode(j_and,x);
1695 }
1696 
1697 //------------------------------is_absolute------------------------------------
1698 // Check for absolute value.
1699 static Node* is_absolute( PhaseGVN *phase, PhiNode *phi_root, int true_path) {
1700   assert(true_path !=0, "only diamond shape graph expected");
1701 
1702   int  cmp_zero_idx = 0;        // Index of compare input where to look for zero
1703   int  phi_x_idx = 0;           // Index of phi input where to find naked x
1704 
1705   // ABS ends with the merge of 2 control flow paths.
1706   // Find the false path from the true path. With only 2 inputs, 3 - x works nicely.
1707   int false_path = 3 - true_path;
1708 
1709   // is_diamond_phi() has guaranteed the correctness of the nodes sequence:
1710   // phi->region->if_proj->ifnode->bool->cmp
1711   BoolNode *bol = phi_root->in(0)->in(1)->in(0)->in(1)->as_Bool();
1712   Node *cmp = bol->in(1);
1713 
1714   // Check bool sense
1715   if (cmp->Opcode() == Op_CmpF || cmp->Opcode() == Op_CmpD) {
1716     switch (bol->_test._test) {
1717     case BoolTest::lt: cmp_zero_idx = 1; phi_x_idx = true_path;  break;
1718     case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = false_path; break;
1719     case BoolTest::gt: cmp_zero_idx = 2; phi_x_idx = true_path;  break;
1720     case BoolTest::ge: cmp_zero_idx = 1; phi_x_idx = false_path; break;
1721     default:           return nullptr;                           break;
1722     }
1723   } else if (cmp->Opcode() == Op_CmpI || cmp->Opcode() == Op_CmpL) {
1724     switch (bol->_test._test) {
1725     case BoolTest::lt:
1726     case BoolTest::le: cmp_zero_idx = 2; phi_x_idx = false_path; break;
1727     case BoolTest::gt:
1728     case BoolTest::ge: cmp_zero_idx = 2; phi_x_idx = true_path;  break;
1729     default:           return nullptr;                           break;
1730     }
1731   }
1732 
1733   // Test is next
1734   const Type *tzero = nullptr;
1735   switch (cmp->Opcode()) {
1736   case Op_CmpI:    tzero = TypeInt::ZERO; break;  // Integer ABS
1737   case Op_CmpL:    tzero = TypeLong::ZERO; break; // Long ABS
1738   case Op_CmpF:    tzero = TypeF::ZERO; break; // Float ABS
1739   case Op_CmpD:    tzero = TypeD::ZERO; break; // Double ABS
1740   default: return nullptr;
1741   }
1742 
1743   // Find zero input of compare; the other input is being abs'd
1744   Node *x = nullptr;
1745   bool flip = false;
1746   if( phase->type(cmp->in(cmp_zero_idx)) == tzero ) {
1747     x = cmp->in(3 - cmp_zero_idx);
1748   } else if( phase->type(cmp->in(3 - cmp_zero_idx)) == tzero ) {
1749     // The test is inverted, we should invert the result...
1750     x = cmp->in(cmp_zero_idx);
1751     flip = true;
1752   } else {
1753     return nullptr;
1754   }
1755 
1756   // Next get the 2 pieces being selected, one is the original value
1757   // and the other is the negated value.
1758   if( phi_root->in(phi_x_idx) != x ) return nullptr;
1759 
1760   // Check other phi input for subtract node
1761   Node *sub = phi_root->in(3 - phi_x_idx);
1762 
1763   bool is_sub = sub->Opcode() == Op_SubF || sub->Opcode() == Op_SubD ||
1764                 sub->Opcode() == Op_SubI || sub->Opcode() == Op_SubL;
1765 
1766   // Allow only Sub(0,X) and fail out for all others; Neg is not OK
1767   if (!is_sub || phase->type(sub->in(1)) != tzero || sub->in(2) != x) return nullptr;
1768 
1769   if (tzero == TypeF::ZERO) {
1770     x = new AbsFNode(x);
1771     if (flip) {
1772       x = new SubFNode(sub->in(1), phase->transform(x));
1773     }
1774   } else if (tzero == TypeD::ZERO) {
1775     x = new AbsDNode(x);
1776     if (flip) {
1777       x = new SubDNode(sub->in(1), phase->transform(x));
1778     }
1779   } else if (tzero == TypeInt::ZERO && Matcher::match_rule_supported(Op_AbsI)) {
1780     x = new AbsINode(x);
1781     if (flip) {
1782       x = new SubINode(sub->in(1), phase->transform(x));
1783     }
1784   } else if (tzero == TypeLong::ZERO && Matcher::match_rule_supported(Op_AbsL)) {
1785     x = new AbsLNode(x);
1786     if (flip) {
1787       x = new SubLNode(sub->in(1), phase->transform(x));
1788     }
1789   } else return nullptr;
1790 
1791   return x;
1792 }
1793 
1794 //------------------------------split_once-------------------------------------
1795 // Helper for split_flow_path
1796 static void split_once(PhaseIterGVN *igvn, Node *phi, Node *val, Node *n, Node *newn) {
1797   igvn->hash_delete(n);         // Remove from hash before hacking edges
1798 
1799   uint j = 1;
1800   for (uint i = phi->req()-1; i > 0; i--) {
1801     if (phi->in(i) == val) {   // Found a path with val?
1802       // Add to NEW Region/Phi, no DU info
1803       newn->set_req( j++, n->in(i) );
1804       // Remove from OLD Region/Phi
1805       n->del_req(i);
1806     }
1807   }
1808 
1809   // Register the new node but do not transform it.  Cannot transform until the
1810   // entire Region/Phi conglomerate has been hacked as a single huge transform.
1811   igvn->register_new_node_with_optimizer( newn );
1812 
1813   // Now I can point to the new node.
1814   n->add_req(newn);
1815   igvn->_worklist.push(n);
1816 }
1817 
1818 //------------------------------split_flow_path--------------------------------
1819 // Check for merging identical values and split flow paths
1820 static Node* split_flow_path(PhaseGVN *phase, PhiNode *phi) {
1821   // This optimization tries to find two or more inputs of phi with the same constant value
1822   // It then splits them into a separate Phi, and according Region. If this is a loop-entry,
1823   // and the loop entry has multiple fall-in edges, and some of those fall-in edges have that
1824   // constant, and others not, we may split the fall-in edges into separate Phi's, and create
1825   // an irreducible loop. For reducible loops, this never seems to happen, as the multiple
1826   // fall-in edges are already merged before the loop head during parsing. But with irreducible
1827   // loops present the order or merging during parsing can sometimes prevent this.
1828   if (phase->C->has_irreducible_loop()) {
1829     // Avoid this optimization if any irreducible loops are present. Else we may create
1830     // an irreducible loop that we do not detect.
1831     return nullptr;
1832   }
1833   BasicType bt = phi->type()->basic_type();
1834   if( bt == T_ILLEGAL || type2size[bt] <= 0 )
1835     return nullptr;             // Bail out on funny non-value stuff
1836   if( phi->req() <= 3 )         // Need at least 2 matched inputs and a
1837     return nullptr;             // third unequal input to be worth doing
1838 
1839   // Scan for a constant
1840   uint i;
1841   for( i = 1; i < phi->req()-1; i++ ) {
1842     Node *n = phi->in(i);
1843     if( !n ) return nullptr;
1844     if( phase->type(n) == Type::TOP ) return nullptr;
1845     if( n->Opcode() == Op_ConP || n->Opcode() == Op_ConN || n->Opcode() == Op_ConNKlass )
1846       break;
1847   }
1848   if( i >= phi->req() )         // Only split for constants
1849     return nullptr;
1850 
1851   Node *val = phi->in(i);       // Constant to split for
1852   uint hit = 0;                 // Number of times it occurs
1853   Node *r = phi->region();
1854 
1855   for( ; i < phi->req(); i++ ){ // Count occurrences of constant
1856     Node *n = phi->in(i);
1857     if( !n ) return nullptr;
1858     if( phase->type(n) == Type::TOP ) return nullptr;
1859     if( phi->in(i) == val ) {
1860       hit++;
1861       if (Node::may_be_loop_entry(r->in(i))) {
1862         return nullptr; // don't split loop entry path
1863       }
1864     }
1865   }
1866 
1867   if( hit <= 1 ||               // Make sure we find 2 or more
1868       hit == phi->req()-1 )     // and not ALL the same value
1869     return nullptr;
1870 
1871   // Now start splitting out the flow paths that merge the same value.
1872   // Split first the RegionNode.
1873   PhaseIterGVN *igvn = phase->is_IterGVN();
1874   RegionNode *newr = new RegionNode(hit+1);
1875   split_once(igvn, phi, val, r, newr);
1876 
1877   // Now split all other Phis than this one
1878   for (DUIterator_Fast kmax, k = r->fast_outs(kmax); k < kmax; k++) {
1879     Node* phi2 = r->fast_out(k);
1880     if( phi2->is_Phi() && phi2->as_Phi() != phi ) {
1881       PhiNode *newphi = PhiNode::make_blank(newr, phi2);
1882       split_once(igvn, phi, val, phi2, newphi);
1883     }
1884   }
1885 
1886   // Clean up this guy
1887   igvn->hash_delete(phi);
1888   for( i = phi->req()-1; i > 0; i-- ) {
1889     if( phi->in(i) == val ) {
1890       phi->del_req(i);
1891     }
1892   }
1893   phi->add_req(val);
1894 
1895   return phi;
1896 }
1897 
1898 // Returns the BasicType of a given convert node and a type, with special handling to ensure that conversions to
1899 // and from half float will return the SHORT basic type, as that wouldn't be returned typically from TypeInt.
1900 static BasicType get_convert_type(Node* convert, const Type* type) {
1901   int convert_op = convert->Opcode();
1902   if (type->isa_int() && (convert_op == Op_ConvHF2F || convert_op == Op_ConvF2HF)) {
1903     return T_SHORT;
1904   }
1905 
1906   return type->basic_type();
1907 }
1908 
1909 //=============================================================================
1910 //------------------------------simple_data_loop_check-------------------------
1911 //  Try to determining if the phi node in a simple safe/unsafe data loop.
1912 //  Returns:
1913 // enum LoopSafety { Safe = 0, Unsafe, UnsafeLoop };
1914 // Safe       - safe case when the phi and it's inputs reference only safe data
1915 //              nodes;
1916 // Unsafe     - the phi and it's inputs reference unsafe data nodes but there
1917 //              is no reference back to the phi - need a graph walk
1918 //              to determine if it is in a loop;
1919 // UnsafeLoop - unsafe case when the phi references itself directly or through
1920 //              unsafe data node.
1921 //  Note: a safe data node is a node which could/never reference itself during
1922 //  GVN transformations. For now it is Con, Proj, Phi, CastPP, CheckCastPP.
1923 //  I mark Phi nodes as safe node not only because they can reference itself
1924 //  but also to prevent mistaking the fallthrough case inside an outer loop
1925 //  as dead loop when the phi references itself through an other phi.
1926 PhiNode::LoopSafety PhiNode::simple_data_loop_check(Node *in) const {
1927   // It is unsafe loop if the phi node references itself directly.
1928   if (in == (Node*)this)
1929     return UnsafeLoop; // Unsafe loop
1930   // Unsafe loop if the phi node references itself through an unsafe data node.
1931   // Exclude cases with null inputs or data nodes which could reference
1932   // itself (safe for dead loops).
1933   if (in != nullptr && !in->is_dead_loop_safe()) {
1934     // Check inputs of phi's inputs also.
1935     // It is much less expensive then full graph walk.
1936     uint cnt = in->req();
1937     uint i = (in->is_Proj() && !in->is_CFG())  ? 0 : 1;
1938     for (; i < cnt; ++i) {
1939       Node* m = in->in(i);
1940       if (m == (Node*)this)
1941         return UnsafeLoop; // Unsafe loop
1942       if (m != nullptr && !m->is_dead_loop_safe()) {
1943         // Check the most common case (about 30% of all cases):
1944         // phi->Load/Store->AddP->(ConP ConP Con)/(Parm Parm Con).
1945         Node *m1 = (m->is_AddP() && m->req() > 3) ? m->in(1) : nullptr;
1946         if (m1 == (Node*)this)
1947           return UnsafeLoop; // Unsafe loop
1948         if (m1 != nullptr && m1 == m->in(2) &&
1949             m1->is_dead_loop_safe() && m->in(3)->is_Con()) {
1950           continue; // Safe case
1951         }
1952         // The phi references an unsafe node - need full analysis.
1953         return Unsafe;
1954       }
1955     }
1956   }
1957   return Safe; // Safe case - we can optimize the phi node.
1958 }
1959 
1960 //------------------------------is_unsafe_data_reference-----------------------
1961 // If phi can be reached through the data input - it is data loop.
1962 bool PhiNode::is_unsafe_data_reference(Node *in) const {
1963   assert(req() > 1, "");
1964   // First, check simple cases when phi references itself directly or
1965   // through an other node.
1966   LoopSafety safety = simple_data_loop_check(in);
1967   if (safety == UnsafeLoop)
1968     return true;  // phi references itself - unsafe loop
1969   else if (safety == Safe)
1970     return false; // Safe case - phi could be replaced with the unique input.
1971 
1972   // Unsafe case when we should go through data graph to determine
1973   // if the phi references itself.
1974 
1975   ResourceMark rm;
1976 
1977   Node_List nstack;
1978   VectorSet visited;
1979 
1980   nstack.push(in); // Start with unique input.
1981   visited.set(in->_idx);
1982   while (nstack.size() != 0) {
1983     Node* n = nstack.pop();
1984     uint cnt = n->req();
1985     uint i = (n->is_Proj() && !n->is_CFG()) ? 0 : 1;
1986     for (; i < cnt; i++) {
1987       Node* m = n->in(i);
1988       if (m == (Node*)this) {
1989         return true;    // Data loop
1990       }
1991       if (m != nullptr && !m->is_dead_loop_safe()) { // Only look for unsafe cases.
1992         if (!visited.test_set(m->_idx))
1993           nstack.push(m);
1994       }
1995     }
1996   }
1997   return false; // The phi is not reachable from its inputs
1998 }
1999 
2000 // Is this Phi's region or some inputs to the region enqueued for IGVN
2001 // and so could cause the region to be optimized out?
2002 bool PhiNode::wait_for_region_igvn(PhaseGVN* phase) {
2003   PhaseIterGVN* igvn = phase->is_IterGVN();
2004   Unique_Node_List& worklist = igvn->_worklist;
2005   bool delay = false;
2006   Node* r = in(0);
2007   for (uint j = 1; j < req(); j++) {
2008     Node* rc = r->in(j);
2009     Node* n = in(j);
2010 
2011     if (rc == nullptr || !rc->is_Proj()) { continue; }
2012     if (worklist.member(rc)) {
2013       delay = true;
2014       break;
2015     }
2016 
2017     if (rc->in(0) == nullptr || !rc->in(0)->is_If()) { continue; }
2018     if (worklist.member(rc->in(0))) {
2019       delay = true;
2020       break;
2021     }
2022 
2023     if (rc->in(0)->in(1) == nullptr || !rc->in(0)->in(1)->is_Bool()) { continue; }
2024     if (worklist.member(rc->in(0)->in(1))) {
2025       delay = true;
2026       break;
2027     }
2028 
2029     if (rc->in(0)->in(1)->in(1) == nullptr || !rc->in(0)->in(1)->in(1)->is_Cmp()) { continue; }
2030     if (worklist.member(rc->in(0)->in(1)->in(1))) {
2031       delay = true;
2032       break;
2033     }
2034   }
2035 
2036   if (delay) {
2037     worklist.push(this);
2038   }
2039   return delay;
2040 }
2041 
2042 // Push inline type input nodes (and null) down through the phi recursively (can handle data loops).
2043 InlineTypeNode* PhiNode::push_inline_types_down(PhaseGVN* phase, bool can_reshape, ciInlineKlass* inline_klass) {
2044   assert(inline_klass != nullptr, "must be");
2045   InlineTypeNode* vt = InlineTypeNode::make_null(*phase, inline_klass, /* transform = */ false)->clone_with_phis(phase, in(0), nullptr, !_type->maybe_null());
2046   if (can_reshape) {
2047     // Replace phi right away to be able to use the inline
2048     // type node when reaching the phi again through data loops.
2049     PhaseIterGVN* igvn = phase->is_IterGVN();
2050     for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
2051       Node* u = fast_out(i);
2052       igvn->rehash_node_delayed(u);
2053       imax -= u->replace_edge(this, vt);
2054       --i;
2055     }
2056     igvn->rehash_node_delayed(this);
2057     assert(outcnt() == 0, "should be dead now");
2058   }
2059   ResourceMark rm;
2060   Node_List casts;
2061   for (uint i = 1; i < req(); ++i) {
2062     Node* n = in(i);
2063     while (n->is_ConstraintCast()) {
2064       casts.push(n);
2065       n = n->in(1);
2066     }
2067     if (phase->type(n)->is_zero_type()) {
2068       n = InlineTypeNode::make_null(*phase, inline_klass);
2069     } else if (n->is_Phi()) {
2070       assert(can_reshape, "can only handle phis during IGVN");
2071       n = phase->transform(n->as_Phi()->push_inline_types_down(phase, can_reshape, inline_klass));
2072     }
2073     while (casts.size() != 0) {
2074       // Push the cast(s) through the InlineTypeNode
2075       // TODO 8325106 Can we avoid cloning?
2076       Node* cast = casts.pop()->clone();
2077       cast->set_req_X(1, n->as_InlineType()->get_oop(), phase);
2078       n = n->clone();
2079       n->as_InlineType()->set_oop(*phase, phase->transform(cast));
2080       n = phase->transform(n);
2081     }
2082     bool transform = !can_reshape && (i == (req()-1)); // Transform phis on last merge
2083     vt->merge_with(phase, n->as_InlineType(), i, transform);
2084   }
2085   return vt;
2086 }
2087 
2088 // If the Phi's Region is in an irreducible loop, and the Region
2089 // has had an input removed, but not yet transformed, it could be
2090 // that the Region (and this Phi) are not reachable from Root.
2091 // If we allow the Phi to collapse before the Region, this may lead
2092 // to dead-loop data. Wait for the Region to check for reachability,
2093 // and potentially remove the dead code.
2094 bool PhiNode::must_wait_for_region_in_irreducible_loop(PhaseGVN* phase) const {
2095   RegionNode* region = in(0)->as_Region();
2096   if (region->loop_status() == RegionNode::LoopStatus::MaybeIrreducibleEntry) {
2097     Node* top = phase->C->top();
2098     for (uint j = 1; j < req(); j++) {
2099       Node* rc = region->in(j); // for each control input
2100       if (rc == nullptr || phase->type(rc) == Type::TOP) {
2101         // Region is missing a control input
2102         Node* n = in(j);
2103         if (n != nullptr && n != top) {
2104           // Phi still has its input, so region just lost its input
2105           return true;
2106         }
2107       }
2108     }
2109   }
2110   return false;
2111 }
2112 
2113 //------------------------------Ideal------------------------------------------
2114 // Return a node which is more "ideal" than the current node.  Must preserve
2115 // the CFG, but we can still strip out dead paths.
2116 Node *PhiNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2117   Node *r = in(0);              // RegionNode
2118   assert(r != nullptr && r->is_Region(), "this phi must have a region");
2119   assert(r->in(0) == nullptr || !r->in(0)->is_Root(), "not a specially hidden merge");
2120 
2121   // Note: During parsing, phis are often transformed before their regions.
2122   // This means we have to use type_or_null to defend against untyped regions.
2123   if( phase->type_or_null(r) == Type::TOP ) // Dead code?
2124     return nullptr;                // No change
2125 
2126   Node *top = phase->C->top();
2127   bool new_phi = (outcnt() == 0); // transforming new Phi
2128   // No change for igvn if new phi is not hooked
2129   if (new_phi && can_reshape)
2130     return nullptr;
2131 
2132   if (must_wait_for_region_in_irreducible_loop(phase)) {
2133     return nullptr;
2134   }
2135 
2136   // The are 2 situations when only one valid phi's input is left
2137   // (in addition to Region input).
2138   // One: region is not loop - replace phi with this input.
2139   // Two: region is loop - replace phi with top since this data path is dead
2140   //                       and we need to break the dead data loop.
2141   Node* progress = nullptr;        // Record if any progress made
2142   for( uint j = 1; j < req(); ++j ){ // For all paths in
2143     // Check unreachable control paths
2144     Node* rc = r->in(j);
2145     Node* n = in(j);            // Get the input
2146     if (rc == nullptr || phase->type(rc) == Type::TOP) {
2147       if (n != top) {           // Not already top?
2148         PhaseIterGVN *igvn = phase->is_IterGVN();
2149         if (can_reshape && igvn != nullptr) {
2150           igvn->_worklist.push(r);
2151         }
2152         // Nuke it down
2153         set_req_X(j, top, phase);
2154         progress = this;        // Record progress
2155       }
2156     }
2157   }
2158 
2159   if (can_reshape && outcnt() == 0) {
2160     // set_req() above may kill outputs if Phi is referenced
2161     // only by itself on the dead (top) control path.
2162     return top;
2163   }
2164 
2165   bool uncasted = false;
2166   Node* uin = unique_input(phase, false);
2167   if (uin == nullptr && can_reshape &&
2168       // If there is a chance that the region can be optimized out do
2169       // not add a cast node that we can't remove yet.
2170       !wait_for_region_igvn(phase)) {
2171     uncasted = true;
2172     uin = unique_input(phase, true);
2173   }
2174   if (uin == top) {             // Simplest case: no alive inputs.
2175     if (can_reshape)            // IGVN transformation
2176       return top;
2177     else
2178       return nullptr;              // Identity will return TOP
2179   } else if (uin != nullptr) {
2180     // Only one not-null unique input path is left.
2181     // Determine if this input is backedge of a loop.
2182     // (Skip new phis which have no uses and dead regions).
2183     if (outcnt() > 0 && r->in(0) != nullptr) {
2184       if (is_data_loop(r->as_Region(), uin, phase)) {
2185         // Break this data loop to avoid creation of a dead loop.
2186         if (can_reshape) {
2187           return top;
2188         } else {
2189           // We can't return top if we are in Parse phase - cut inputs only
2190           // let Identity to handle the case.
2191           replace_edge(uin, top, phase);
2192           return nullptr;
2193         }
2194       }
2195     }
2196 
2197     if (uncasted) {
2198       // Add cast nodes between the phi to be removed and its unique input.
2199       // Wait until after parsing for the type information to propagate from the casts.
2200       assert(can_reshape, "Invalid during parsing");
2201       const Type* phi_type = bottom_type();
2202       // Add casts to carry the control dependency of the Phi that is
2203       // going away
2204       Node* cast = nullptr;
2205       const TypeTuple* extra_types = collect_types(phase);
2206       if (phi_type->isa_ptr()) {
2207         const Type* uin_type = phase->type(uin);
2208         if (!phi_type->isa_oopptr() && !uin_type->isa_oopptr()) {
2209           cast = ConstraintCastNode::make_cast(Op_CastPP, r, uin, phi_type, ConstraintCastNode::StrongDependency,
2210                                                extra_types);
2211         } else {
2212           // Use a CastPP for a cast to not null and a CheckCastPP for
2213           // a cast to a new klass (and both if both null-ness and
2214           // klass change).
2215 
2216           // If the type of phi is not null but the type of uin may be
2217           // null, uin's type must be casted to not null
2218           if (phi_type->join(TypePtr::NOTNULL) == phi_type->remove_speculative() &&
2219               uin_type->join(TypePtr::NOTNULL) != uin_type->remove_speculative()) {
2220             cast = ConstraintCastNode::make_cast(Op_CastPP, r, uin, TypePtr::NOTNULL,
2221                                                  ConstraintCastNode::StrongDependency, extra_types);
2222           }
2223 
2224           // If the type of phi and uin, both casted to not null,
2225           // differ the klass of uin must be (check)cast'ed to match
2226           // that of phi
2227           if (phi_type->join_speculative(TypePtr::NOTNULL) != uin_type->join_speculative(TypePtr::NOTNULL)) {
2228             Node* n = uin;
2229             if (cast != nullptr) {
2230               cast = phase->transform(cast);
2231               n = cast;
2232             }
2233             cast = ConstraintCastNode::make_cast(Op_CheckCastPP, r, n, phi_type, ConstraintCastNode::StrongDependency,
2234                                                  extra_types);
2235           }
2236           if (cast == nullptr) {
2237             cast = ConstraintCastNode::make_cast(Op_CastPP, r, uin, phi_type, ConstraintCastNode::StrongDependency,
2238                                                  extra_types);
2239           }
2240         }
2241       } else {
2242         cast = ConstraintCastNode::make_cast_for_type(r, uin, phi_type, ConstraintCastNode::StrongDependency, extra_types);
2243       }
2244       assert(cast != nullptr, "cast should be set");
2245       cast = phase->transform(cast);
2246       // set all inputs to the new cast(s) so the Phi is removed by Identity
2247       PhaseIterGVN* igvn = phase->is_IterGVN();
2248       for (uint i = 1; i < req(); i++) {
2249         set_req_X(i, cast, igvn);
2250       }
2251       uin = cast;
2252     }
2253 
2254     // One unique input.
2255     debug_only(Node* ident = Identity(phase));
2256     // The unique input must eventually be detected by the Identity call.
2257 #ifdef ASSERT
2258     if (ident != uin && !ident->is_top() && !must_wait_for_region_in_irreducible_loop(phase)) {
2259       // print this output before failing assert
2260       r->dump(3);
2261       this->dump(3);
2262       ident->dump();
2263       uin->dump();
2264     }
2265 #endif
2266     // Identity may not return the expected uin, if it has to wait for the region, in irreducible case
2267     assert(ident == uin || ident->is_top() || must_wait_for_region_in_irreducible_loop(phase), "Identity must clean this up");
2268     return nullptr;
2269   }
2270 
2271   Node* opt = nullptr;
2272   int true_path = is_diamond_phi();
2273   if (true_path != 0 &&
2274       // If one of the diamond's branch is in the process of dying then, the Phi's input for that branch might transform
2275       // to top. If that happens replacing the Phi with an operation that consumes the Phi's inputs will cause the Phi
2276       // to be replaced by top. To prevent that, delay the transformation until the branch has a chance to be removed.
2277       !(can_reshape && wait_for_region_igvn(phase))) {
2278     // Check for CMove'ing identity. If it would be unsafe,
2279     // handle it here. In the safe case, let Identity handle it.
2280     Node* unsafe_id = is_cmove_id(phase, true_path);
2281     if( unsafe_id != nullptr && is_unsafe_data_reference(unsafe_id) )
2282       opt = unsafe_id;
2283 
2284     // Check for simple convert-to-boolean pattern
2285     if( opt == nullptr )
2286       opt = is_x2logic(phase, this, true_path);
2287 
2288     // Check for absolute value
2289     if( opt == nullptr )
2290       opt = is_absolute(phase, this, true_path);
2291 
2292     // Check for conditional add
2293     if( opt == nullptr && can_reshape )
2294       opt = is_cond_add(phase, this, true_path);
2295 
2296     // These 4 optimizations could subsume the phi:
2297     // have to check for a dead data loop creation.
2298     if( opt != nullptr ) {
2299       if( opt == unsafe_id || is_unsafe_data_reference(opt) ) {
2300         // Found dead loop.
2301         if( can_reshape )
2302           return top;
2303         // We can't return top if we are in Parse phase - cut inputs only
2304         // to stop further optimizations for this phi. Identity will return TOP.
2305         assert(req() == 3, "only diamond merge phi here");
2306         set_req(1, top);
2307         set_req(2, top);
2308         return nullptr;
2309       } else {
2310         return opt;
2311       }
2312     }
2313   }
2314 
2315   // Check for merging identical values and split flow paths
2316   if (can_reshape) {
2317     opt = split_flow_path(phase, this);
2318     // This optimization only modifies phi - don't need to check for dead loop.
2319     assert(opt == nullptr || opt == this, "do not elide phi");
2320     if (opt != nullptr)  return opt;
2321   }
2322 
2323   if (in(1) != nullptr && in(1)->Opcode() == Op_AddP && can_reshape) {
2324     // Try to undo Phi of AddP:
2325     // (Phi (AddP base address offset) (AddP base2 address2 offset2))
2326     // becomes:
2327     // newbase := (Phi base base2)
2328     // newaddress := (Phi address address2)
2329     // newoffset := (Phi offset offset2)
2330     // (AddP newbase newaddress newoffset)
2331     //
2332     // This occurs as a result of unsuccessful split_thru_phi and
2333     // interferes with taking advantage of addressing modes. See the
2334     // clone_shift_expressions code in matcher.cpp
2335     Node* addp = in(1);
2336     Node* base = addp->in(AddPNode::Base);
2337     Node* address = addp->in(AddPNode::Address);
2338     Node* offset = addp->in(AddPNode::Offset);
2339     if (base != nullptr && address != nullptr && offset != nullptr &&
2340         !base->is_top() && !address->is_top() && !offset->is_top()) {
2341       const Type* base_type = base->bottom_type();
2342       const Type* address_type = address->bottom_type();
2343       // make sure that all the inputs are similar to the first one,
2344       // i.e. AddP with base == address and same offset as first AddP
2345       bool doit = true;
2346       for (uint i = 2; i < req(); i++) {
2347         if (in(i) == nullptr ||
2348             in(i)->Opcode() != Op_AddP ||
2349             in(i)->in(AddPNode::Base) == nullptr ||
2350             in(i)->in(AddPNode::Address) == nullptr ||
2351             in(i)->in(AddPNode::Offset) == nullptr ||
2352             in(i)->in(AddPNode::Base)->is_top() ||
2353             in(i)->in(AddPNode::Address)->is_top() ||
2354             in(i)->in(AddPNode::Offset)->is_top()) {
2355           doit = false;
2356           break;
2357         }
2358         if (in(i)->in(AddPNode::Base) != base) {
2359           base = nullptr;
2360         }
2361         if (in(i)->in(AddPNode::Offset) != offset) {
2362           offset = nullptr;
2363         }
2364         if (in(i)->in(AddPNode::Address) != address) {
2365           address = nullptr;
2366         }
2367         // Accumulate type for resulting Phi
2368         base_type = base_type->meet_speculative(in(i)->in(AddPNode::Base)->bottom_type());
2369         address_type = address_type->meet_speculative(in(i)->in(AddPNode::Address)->bottom_type());
2370       }
2371       if (doit && base == nullptr) {
2372         // Check for neighboring AddP nodes in a tree.
2373         // If they have a base, use that it.
2374         for (DUIterator_Fast kmax, k = this->fast_outs(kmax); k < kmax; k++) {
2375           Node* u = this->fast_out(k);
2376           if (u->is_AddP()) {
2377             Node* base2 = u->in(AddPNode::Base);
2378             if (base2 != nullptr && !base2->is_top()) {
2379               if (base == nullptr)
2380                 base = base2;
2381               else if (base != base2)
2382                 { doit = false; break; }
2383             }
2384           }
2385         }
2386       }
2387       if (doit) {
2388         if (base == nullptr) {
2389           base = new PhiNode(in(0), base_type, nullptr);
2390           for (uint i = 1; i < req(); i++) {
2391             base->init_req(i, in(i)->in(AddPNode::Base));
2392           }
2393           phase->is_IterGVN()->register_new_node_with_optimizer(base);
2394         }
2395         if (address == nullptr) {
2396           address = new PhiNode(in(0), address_type, nullptr);
2397           for (uint i = 1; i < req(); i++) {
2398             address->init_req(i, in(i)->in(AddPNode::Address));
2399           }
2400           phase->is_IterGVN()->register_new_node_with_optimizer(address);
2401         }
2402         if (offset == nullptr) {
2403           offset = new PhiNode(in(0), TypeX_X, nullptr);
2404           for (uint i = 1; i < req(); i++) {
2405             offset->init_req(i, in(i)->in(AddPNode::Offset));
2406           }
2407           phase->is_IterGVN()->register_new_node_with_optimizer(offset);
2408         }
2409         return new AddPNode(base, address, offset);
2410       }
2411     }
2412   }
2413 
2414   // Split phis through memory merges, so that the memory merges will go away.
2415   // Piggy-back this transformation on the search for a unique input....
2416   // It will be as if the merged memory is the unique value of the phi.
2417   // (Do not attempt this optimization unless parsing is complete.
2418   // It would make the parser's memory-merge logic sick.)
2419   // (MergeMemNode is not dead_loop_safe - need to check for dead loop.)
2420   if (progress == nullptr && can_reshape && type() == Type::MEMORY) {
2421     // see if this phi should be sliced
2422     uint merge_width = 0;
2423     bool saw_self = false;
2424     // TODO revisit this with JDK-8247216
2425     bool mergemem_only = true;
2426     for( uint i=1; i<req(); ++i ) {// For all paths in
2427       Node *ii = in(i);
2428       // TOP inputs should not be counted as safe inputs because if the
2429       // Phi references itself through all other inputs then splitting the
2430       // Phi through memory merges would create dead loop at later stage.
2431       if (ii == top) {
2432         return nullptr; // Delay optimization until graph is cleaned.
2433       }
2434       if (ii->is_MergeMem()) {
2435         MergeMemNode* n = ii->as_MergeMem();
2436         merge_width = MAX2(merge_width, n->req());
2437         saw_self = saw_self || (n->base_memory() == this);
2438       } else {
2439         mergemem_only = false;
2440       }
2441     }
2442 
2443     // This restriction is temporarily necessary to ensure termination:
2444     if (!mergemem_only && !saw_self && adr_type() == TypePtr::BOTTOM)  merge_width = 0;
2445 
2446     if (merge_width > Compile::AliasIdxRaw) {
2447       // found at least one non-empty MergeMem
2448       const TypePtr* at = adr_type();
2449       if (at != TypePtr::BOTTOM) {
2450         // Patch the existing phi to select an input from the merge:
2451         // Phi:AT1(...MergeMem(m0, m1, m2)...) into
2452         //     Phi:AT1(...m1...)
2453         int alias_idx = phase->C->get_alias_index(at);
2454         for (uint i=1; i<req(); ++i) {
2455           Node *ii = in(i);
2456           if (ii->is_MergeMem()) {
2457             MergeMemNode* n = ii->as_MergeMem();
2458             // compress paths and change unreachable cycles to TOP
2459             // If not, we can update the input infinitely along a MergeMem cycle
2460             // Equivalent code is in MemNode::Ideal_common
2461             Node *m  = phase->transform(n);
2462             if (outcnt() == 0) {  // Above transform() may kill us!
2463               return top;
2464             }
2465             // If transformed to a MergeMem, get the desired slice
2466             // Otherwise the returned node represents memory for every slice
2467             Node *new_mem = (m->is_MergeMem()) ?
2468                              m->as_MergeMem()->memory_at(alias_idx) : m;
2469             // Update input if it is progress over what we have now
2470             if (new_mem != ii) {
2471               set_req_X(i, new_mem, phase->is_IterGVN());
2472               progress = this;
2473             }
2474           }
2475         }
2476       } else {
2477         // We know that at least one MergeMem->base_memory() == this
2478         // (saw_self == true). If all other inputs also references this phi
2479         // (directly or through data nodes) - it is a dead loop.
2480         bool saw_safe_input = false;
2481         for (uint j = 1; j < req(); ++j) {
2482           Node* n = in(j);
2483           if (n->is_MergeMem()) {
2484             MergeMemNode* mm = n->as_MergeMem();
2485             if (mm->base_memory() == this || mm->base_memory() == mm->empty_memory()) {
2486               // Skip this input if it references back to this phi or if the memory path is dead
2487               continue;
2488             }
2489           }
2490           if (!is_unsafe_data_reference(n)) {
2491             saw_safe_input = true; // found safe input
2492             break;
2493           }
2494         }
2495         if (!saw_safe_input) {
2496           // There is a dead loop: All inputs are either dead or reference back to this phi
2497           return top;
2498         }
2499 
2500         // Phi(...MergeMem(m0, m1:AT1, m2:AT2)...) into
2501         //     MergeMem(Phi(...m0...), Phi:AT1(...m1...), Phi:AT2(...m2...))
2502         PhaseIterGVN* igvn = phase->is_IterGVN();
2503         assert(igvn != nullptr, "sanity check");
2504         Node* hook = new Node(1);
2505         PhiNode* new_base = (PhiNode*) clone();
2506         // Must eagerly register phis, since they participate in loops.
2507         igvn->register_new_node_with_optimizer(new_base);
2508         hook->add_req(new_base);
2509 
2510         MergeMemNode* result = MergeMemNode::make(new_base);
2511         for (uint i = 1; i < req(); ++i) {
2512           Node *ii = in(i);
2513           if (ii->is_MergeMem()) {
2514             MergeMemNode* n = ii->as_MergeMem();
2515             if (igvn) {
2516               // TODO revisit this with JDK-8247216
2517               // Put 'n' on the worklist because it might be modified by MergeMemStream::iteration_setup
2518               igvn->_worklist.push(n);
2519             }
2520             for (MergeMemStream mms(result, n); mms.next_non_empty2(); ) {
2521               // If we have not seen this slice yet, make a phi for it.
2522               bool made_new_phi = false;
2523               if (mms.is_empty()) {
2524                 Node* new_phi = new_base->slice_memory(mms.adr_type(phase->C));
2525                 made_new_phi = true;
2526                 igvn->register_new_node_with_optimizer(new_phi);
2527                 hook->add_req(new_phi);
2528                 mms.set_memory(new_phi);
2529               }
2530               Node* phi = mms.memory();
2531               assert(made_new_phi || phi->in(i) == n, "replace the i-th merge by a slice");
2532               phi->set_req(i, mms.memory2());
2533             }
2534           }
2535         }
2536         // Distribute all self-loops.
2537         { // (Extra braces to hide mms.)
2538           for (MergeMemStream mms(result); mms.next_non_empty(); ) {
2539             Node* phi = mms.memory();
2540             for (uint i = 1; i < req(); ++i) {
2541               if (phi->in(i) == this)  phi->set_req(i, phi);
2542             }
2543           }
2544         }
2545         // Already replace this phi node to cut it off from the graph to not interfere in dead loop checks during the
2546         // transformations of the new phi nodes below. Otherwise, we could wrongly conclude that there is no dead loop
2547         // because we are finding this phi node again. Also set the type of the new MergeMem node in case we are also
2548         // visiting it in the transformations below.
2549         igvn->replace_node(this, result);
2550         igvn->set_type(result, result->bottom_type());
2551 
2552         // now transform the new nodes, and return the mergemem
2553         for (MergeMemStream mms(result); mms.next_non_empty(); ) {
2554           Node* phi = mms.memory();
2555           mms.set_memory(phase->transform(phi));
2556         }
2557         hook->destruct(igvn);
2558         // Replace self with the result.
2559         return result;
2560       }
2561     }
2562     //
2563     // Other optimizations on the memory chain
2564     //
2565     const TypePtr* at = adr_type();
2566     for( uint i=1; i<req(); ++i ) {// For all paths in
2567       Node *ii = in(i);
2568       Node *new_in = MemNode::optimize_memory_chain(ii, at, nullptr, phase);
2569       if (ii != new_in ) {
2570         set_req(i, new_in);
2571         progress = this;
2572       }
2573     }
2574   }
2575 
2576 #ifdef _LP64
2577   // Push DecodeN/DecodeNKlass down through phi.
2578   // The rest of phi graph will transform by split EncodeP node though phis up.
2579   if ((UseCompressedOops || UseCompressedClassPointers) && can_reshape && progress == nullptr) {
2580     bool may_push = true;
2581     bool has_decodeN = false;
2582     bool is_decodeN = false;
2583     for (uint i=1; i<req(); ++i) {// For all paths in
2584       Node *ii = in(i);
2585       if (ii->is_DecodeNarrowPtr() && ii->bottom_type() == bottom_type()) {
2586         // Do optimization if a non dead path exist.
2587         if (ii->in(1)->bottom_type() != Type::TOP) {
2588           has_decodeN = true;
2589           is_decodeN = ii->is_DecodeN();
2590         }
2591       } else if (!ii->is_Phi()) {
2592         may_push = false;
2593       }
2594     }
2595 
2596     if (has_decodeN && may_push) {
2597       PhaseIterGVN *igvn = phase->is_IterGVN();
2598       // Make narrow type for new phi.
2599       const Type* narrow_t;
2600       if (is_decodeN) {
2601         narrow_t = TypeNarrowOop::make(this->bottom_type()->is_ptr());
2602       } else {
2603         narrow_t = TypeNarrowKlass::make(this->bottom_type()->is_ptr());
2604       }
2605       PhiNode* new_phi = new PhiNode(r, narrow_t);
2606       uint orig_cnt = req();
2607       for (uint i=1; i<req(); ++i) {// For all paths in
2608         Node *ii = in(i);
2609         Node* new_ii = nullptr;
2610         if (ii->is_DecodeNarrowPtr()) {
2611           assert(ii->bottom_type() == bottom_type(), "sanity");
2612           new_ii = ii->in(1);
2613         } else {
2614           assert(ii->is_Phi(), "sanity");
2615           if (ii->as_Phi() == this) {
2616             new_ii = new_phi;
2617           } else {
2618             if (is_decodeN) {
2619               new_ii = new EncodePNode(ii, narrow_t);
2620             } else {
2621               new_ii = new EncodePKlassNode(ii, narrow_t);
2622             }
2623             igvn->register_new_node_with_optimizer(new_ii);
2624           }
2625         }
2626         new_phi->set_req(i, new_ii);
2627       }
2628       igvn->register_new_node_with_optimizer(new_phi, this);
2629       if (is_decodeN) {
2630         progress = new DecodeNNode(new_phi, bottom_type());
2631       } else {
2632         progress = new DecodeNKlassNode(new_phi, bottom_type());
2633       }
2634     }
2635   }
2636 #endif
2637 
2638   Node* inline_type = try_push_inline_types_down(phase, can_reshape);
2639   if (inline_type != this) {
2640     return inline_type;
2641   }
2642 
2643   // Try to convert a Phi with two duplicated convert nodes into a phi of the pre-conversion type and the convert node
2644   // proceeding the phi, to de-duplicate the convert node and compact the IR.
2645   if (can_reshape && progress == nullptr) {
2646     ConvertNode* convert = in(1)->isa_Convert();
2647     if (convert != nullptr) {
2648       int conv_op = convert->Opcode();
2649       bool ok = true;
2650 
2651       // Check the rest of the inputs
2652       for (uint i = 2; i < req(); i++) {
2653         // Make sure that all inputs are of the same type of convert node
2654         if (in(i)->Opcode() != conv_op) {
2655           ok = false;
2656           break;
2657         }
2658       }
2659 
2660       if (ok) {
2661         // Find the local bottom type to set as the type of the phi
2662         const Type* source_type = Type::get_const_basic_type(convert->in_type()->basic_type());
2663         const Type* dest_type = convert->bottom_type();
2664 
2665         PhiNode* newphi = new PhiNode(in(0), source_type, nullptr);
2666         // Set inputs to the new phi be the inputs of the convert
2667         for (uint i = 1; i < req(); i++) {
2668           newphi->init_req(i, in(i)->in(1));
2669         }
2670 
2671         phase->is_IterGVN()->register_new_node_with_optimizer(newphi, this);
2672 
2673         return ConvertNode::create_convert(get_convert_type(convert, source_type), get_convert_type(convert, dest_type), newphi);
2674       }
2675     }
2676   }
2677 
2678   // Phi (VB ... VB) => VB (Phi ...) (Phi ...)
2679   if (EnableVectorReboxing && can_reshape && progress == nullptr && type()->isa_oopptr()) {
2680     progress = merge_through_phi(this, phase->is_IterGVN());
2681   }
2682 
2683   return progress;              // Return any progress
2684 }
2685 
2686 // Check recursively if inputs are either an inline type, constant null
2687 // or another Phi (including self references through data loops). If so,
2688 // push the inline types down through the phis to enable folding of loads.
2689 Node* PhiNode::try_push_inline_types_down(PhaseGVN* phase, const bool can_reshape) {
2690   if (!can_be_inline_type()) {
2691     return this;
2692   }
2693 
2694   ciInlineKlass* inline_klass;
2695   if (can_push_inline_types_down(phase, can_reshape, inline_klass)) {
2696     assert(inline_klass != nullptr, "must be");
2697     return push_inline_types_down(phase, can_reshape, inline_klass);
2698   }
2699   return this;
2700 }
2701 
2702 bool PhiNode::can_push_inline_types_down(PhaseGVN* phase, const bool can_reshape, ciInlineKlass*& inline_klass) {
2703   if (req() <= 2) {
2704     // Dead phi.
2705     return false;
2706   }
2707   inline_klass = nullptr;
2708 
2709   // TODO 8302217 We need to prevent endless pushing through
2710   bool only_phi = (outcnt() != 0);
2711   for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) {
2712     Node* n = fast_out(i);
2713     if (n->is_InlineType() && n->in(1) == this) {
2714       return false;
2715     }
2716     if (!n->is_Phi()) {
2717       only_phi = false;
2718     }
2719   }
2720   if (only_phi) {
2721     return false;
2722   }
2723 
2724   ResourceMark rm;
2725   Unique_Node_List worklist;
2726   worklist.push(this);
2727   Node_List casts;
2728 
2729   for (uint next = 0; next < worklist.size(); next++) {
2730     Node* phi = worklist.at(next);
2731     for (uint i = 1; i < phi->req(); i++) {
2732       Node* n = phi->in(i);
2733       if (n == nullptr) {
2734         return false;
2735       }
2736       while (n->is_ConstraintCast()) {
2737         if (n->in(0) != nullptr && n->in(0)->is_top()) {
2738           // Will die, don't optimize
2739           return false;
2740         }
2741         casts.push(n);
2742         n = n->in(1);
2743       }
2744       const Type* type = phase->type(n);
2745       if (n->is_InlineType() && (inline_klass == nullptr || inline_klass == type->inline_klass())) {
2746         inline_klass = type->inline_klass();
2747       } else if (n->is_Phi() && can_reshape && n->bottom_type()->isa_ptr()) {
2748         worklist.push(n);
2749       } else if (!type->is_zero_type()) {
2750         return false;
2751       }
2752     }
2753   }
2754   if (inline_klass == nullptr) {
2755     return false;
2756   }
2757 
2758   // Check if cast nodes can be pushed through
2759   const Type* t = Type::get_const_type(inline_klass);
2760   while (casts.size() != 0 && t != nullptr) {
2761     Node* cast = casts.pop();
2762     if (t->filter(cast->bottom_type()) == Type::TOP) {
2763       return false;
2764     }
2765   }
2766 
2767   return true;
2768 }
2769 
2770 static int compare_types(const Type* const& e1, const Type* const& e2) {
2771   return (intptr_t)e1 - (intptr_t)e2;
2772 }
2773 
2774 // Collect types at casts that are going to be eliminated at that Phi and store them in a TypeTuple.
2775 // Sort the types using an arbitrary order so a list of some types always hashes to the same TypeTuple (and TypeTuple
2776 // pointer comparison is enough to tell if 2 list of types are the same or not)
2777 const TypeTuple* PhiNode::collect_types(PhaseGVN* phase) const {
2778   const Node* region = in(0);
2779   const Type* phi_type = bottom_type();
2780   ResourceMark rm;
2781   GrowableArray<const Type*> types;
2782   for (uint i = 1; i < req(); i++) {
2783     if (region->in(i) == nullptr || phase->type(region->in(i)) == Type::TOP) {
2784       continue;
2785     }
2786     Node* in = Node::in(i);
2787     const Type* t = phase->type(in);
2788     if (in == nullptr || in == this || t == Type::TOP) {
2789       continue;
2790     }
2791     if (t != phi_type && t->higher_equal_speculative(phi_type)) {
2792       types.insert_sorted<compare_types>(t);
2793     }
2794     while (in != nullptr && in->is_ConstraintCast()) {
2795       Node* next = in->in(1);
2796       if (phase->type(next)->isa_rawptr() && phase->type(in)->isa_oopptr()) {
2797         break;
2798       }
2799       ConstraintCastNode* cast = in->as_ConstraintCast();
2800       for (int j = 0; j < cast->extra_types_count(); ++j) {
2801         const Type* extra_t = cast->extra_type_at(j);
2802         if (extra_t != phi_type && extra_t->higher_equal_speculative(phi_type)) {
2803           types.insert_sorted<compare_types>(extra_t);
2804         }
2805       }
2806       in = next;
2807     }
2808   }
2809   const Type **flds = (const Type **)(phase->C->type_arena()->AmallocWords(types.length()*sizeof(Type*)));
2810   for (int i = 0; i < types.length(); ++i) {
2811     flds[i] = types.at(i);
2812   }
2813   return TypeTuple::make(types.length(), flds);
2814 }
2815 
2816 Node* PhiNode::clone_through_phi(Node* root_phi, const Type* t, uint c, PhaseIterGVN* igvn) {
2817   Node_Stack stack(1);
2818   VectorSet  visited;
2819   Node_List  node_map;
2820 
2821   stack.push(root_phi, 1); // ignore control
2822   visited.set(root_phi->_idx);
2823 
2824   Node* new_phi = new PhiNode(root_phi->in(0), t);
2825   node_map.map(root_phi->_idx, new_phi);
2826 
2827   while (stack.is_nonempty()) {
2828     Node* n   = stack.node();
2829     uint  idx = stack.index();
2830     assert(n->is_Phi(), "not a phi");
2831     if (idx < n->req()) {
2832       stack.set_index(idx + 1);
2833       Node* def = n->in(idx);
2834       if (def == nullptr) {
2835         continue; // ignore dead path
2836       } else if (def->is_Phi()) { // inner node
2837         Node* new_phi = node_map[n->_idx];
2838         if (!visited.test_set(def->_idx)) { // not visited yet
2839           node_map.map(def->_idx, new PhiNode(def->in(0), t));
2840           stack.push(def, 1); // ignore control
2841         }
2842         Node* new_in = node_map[def->_idx];
2843         new_phi->set_req(idx, new_in);
2844       } else if (def->Opcode() == Op_VectorBox) { // leaf
2845         assert(n->is_Phi(), "not a phi");
2846         Node* new_phi = node_map[n->_idx];
2847         new_phi->set_req(idx, def->in(c));
2848       } else {
2849         assert(false, "not optimizeable");
2850         return nullptr;
2851       }
2852     } else {
2853       Node* new_phi = node_map[n->_idx];
2854       igvn->register_new_node_with_optimizer(new_phi, n);
2855       stack.pop();
2856     }
2857   }
2858   return new_phi;
2859 }
2860 
2861 Node* PhiNode::merge_through_phi(Node* root_phi, PhaseIterGVN* igvn) {
2862   Node_Stack stack(1);
2863   VectorSet  visited;
2864 
2865   stack.push(root_phi, 1); // ignore control
2866   visited.set(root_phi->_idx);
2867 
2868   VectorBoxNode* cached_vbox = nullptr;
2869   while (stack.is_nonempty()) {
2870     Node* n   = stack.node();
2871     uint  idx = stack.index();
2872     if (idx < n->req()) {
2873       stack.set_index(idx + 1);
2874       Node* in = n->in(idx);
2875       if (in == nullptr) {
2876         continue; // ignore dead path
2877       } else if (in->isa_Phi()) {
2878         if (!visited.test_set(in->_idx)) {
2879           stack.push(in, 1); // ignore control
2880         }
2881       } else if (in->Opcode() == Op_VectorBox) {
2882         VectorBoxNode* vbox = static_cast<VectorBoxNode*>(in);
2883         if (cached_vbox == nullptr) {
2884           cached_vbox = vbox;
2885         } else if (vbox->vec_type() != cached_vbox->vec_type()) {
2886           // TODO: vector type mismatch can be handled with additional reinterpret casts
2887           assert(Type::cmp(vbox->vec_type(), cached_vbox->vec_type()) != 0, "inconsistent");
2888           return nullptr; // not optimizable: vector type mismatch
2889         } else if (vbox->box_type() != cached_vbox->box_type()) {
2890           assert(Type::cmp(vbox->box_type(), cached_vbox->box_type()) != 0, "inconsistent");
2891           return nullptr; // not optimizable: box type mismatch
2892         }
2893       } else {
2894         return nullptr; // not optimizable: neither Phi nor VectorBox
2895       }
2896     } else {
2897       stack.pop();
2898     }
2899   }
2900   if (cached_vbox == nullptr) {
2901     // We have a Phi dead-loop (no data-input). Phi nodes are considered safe,
2902     // so just avoid this optimization.
2903     return nullptr;
2904   }
2905   const TypeInstPtr* btype = cached_vbox->box_type();
2906   const TypeVect*    vtype = cached_vbox->vec_type();
2907   Node* new_vbox_phi = clone_through_phi(root_phi, btype, VectorBoxNode::Box,   igvn);
2908   Node* new_vect_phi = clone_through_phi(root_phi, vtype, VectorBoxNode::Value, igvn);
2909   return new VectorBoxNode(igvn->C, new_vbox_phi, new_vect_phi, btype, vtype);
2910 }
2911 
2912 bool PhiNode::is_data_loop(RegionNode* r, Node* uin, const PhaseGVN* phase) {
2913   // First, take the short cut when we know it is a loop and the EntryControl data path is dead.
2914   // The loop node may only have one input because the entry path was removed in PhaseIdealLoop::Dominators().
2915   // Then, check if there is a data loop when the phi references itself directly or through other data nodes.
2916   assert(!r->is_Loop() || r->req() <= 3, "Loop node should have 3 or less inputs");
2917   const bool is_loop = (r->is_Loop() && r->req() == 3);
2918   const Node* top = phase->C->top();
2919   if (is_loop) {
2920     return !uin->eqv_uncast(in(LoopNode::EntryControl));
2921   } else {
2922     // We have a data loop either with an unsafe data reference or if a region is unreachable.
2923     return is_unsafe_data_reference(uin)
2924            || (r->req() == 3 && (r->in(1) != top && r->in(2) == top && r->is_unreachable_region(phase)));
2925   }
2926 }
2927 
2928 //------------------------------is_tripcount-----------------------------------
2929 bool PhiNode::is_tripcount(BasicType bt) const {
2930   return (in(0) != nullptr && in(0)->is_BaseCountedLoop() &&
2931           in(0)->as_BaseCountedLoop()->bt() == bt &&
2932           in(0)->as_BaseCountedLoop()->phi() == this);
2933 }
2934 
2935 //------------------------------out_RegMask------------------------------------
2936 const RegMask &PhiNode::in_RegMask(uint i) const {
2937   return i ? out_RegMask() : RegMask::Empty;
2938 }
2939 
2940 const RegMask &PhiNode::out_RegMask() const {
2941   uint ideal_reg = _type->ideal_reg();
2942   assert( ideal_reg != Node::NotAMachineReg, "invalid type at Phi" );
2943   if( ideal_reg == 0 ) return RegMask::Empty;
2944   assert(ideal_reg != Op_RegFlags, "flags register is not spillable");
2945   return *(Compile::current()->matcher()->idealreg2spillmask[ideal_reg]);
2946 }
2947 
2948 #ifndef PRODUCT
2949 void PhiNode::dump_spec(outputStream *st) const {
2950   TypeNode::dump_spec(st);
2951   if (is_tripcount(T_INT) || is_tripcount(T_LONG)) {
2952     st->print(" #tripcount");
2953   }
2954 }
2955 #endif
2956 
2957 
2958 //=============================================================================
2959 const Type* GotoNode::Value(PhaseGVN* phase) const {
2960   // If the input is reachable, then we are executed.
2961   // If the input is not reachable, then we are not executed.
2962   return phase->type(in(0));
2963 }
2964 
2965 Node* GotoNode::Identity(PhaseGVN* phase) {
2966   return in(0);                // Simple copy of incoming control
2967 }
2968 
2969 const RegMask &GotoNode::out_RegMask() const {
2970   return RegMask::Empty;
2971 }
2972 
2973 //=============================================================================
2974 const RegMask &JumpNode::out_RegMask() const {
2975   return RegMask::Empty;
2976 }
2977 
2978 //=============================================================================
2979 const RegMask &JProjNode::out_RegMask() const {
2980   return RegMask::Empty;
2981 }
2982 
2983 //=============================================================================
2984 const RegMask &CProjNode::out_RegMask() const {
2985   return RegMask::Empty;
2986 }
2987 
2988 
2989 
2990 //=============================================================================
2991 
2992 uint PCTableNode::hash() const { return Node::hash() + _size; }
2993 bool PCTableNode::cmp( const Node &n ) const
2994 { return _size == ((PCTableNode&)n)._size; }
2995 
2996 const Type *PCTableNode::bottom_type() const {
2997   const Type** f = TypeTuple::fields(_size);
2998   for( uint i = 0; i < _size; i++ ) f[i] = Type::CONTROL;
2999   return TypeTuple::make(_size, f);
3000 }
3001 
3002 //------------------------------Value------------------------------------------
3003 // Compute the type of the PCTableNode.  If reachable it is a tuple of
3004 // Control, otherwise the table targets are not reachable
3005 const Type* PCTableNode::Value(PhaseGVN* phase) const {
3006   if( phase->type(in(0)) == Type::CONTROL )
3007     return bottom_type();
3008   return Type::TOP;             // All paths dead?  Then so are we
3009 }
3010 
3011 //------------------------------Ideal------------------------------------------
3012 // Return a node which is more "ideal" than the current node.  Strip out
3013 // control copies
3014 Node *PCTableNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3015   return remove_dead_region(phase, can_reshape) ? this : nullptr;
3016 }
3017 
3018 //=============================================================================
3019 uint JumpProjNode::hash() const {
3020   return Node::hash() + _dest_bci;
3021 }
3022 
3023 bool JumpProjNode::cmp( const Node &n ) const {
3024   return ProjNode::cmp(n) &&
3025     _dest_bci == ((JumpProjNode&)n)._dest_bci;
3026 }
3027 
3028 #ifndef PRODUCT
3029 void JumpProjNode::dump_spec(outputStream *st) const {
3030   ProjNode::dump_spec(st);
3031   st->print("@bci %d ",_dest_bci);
3032 }
3033 
3034 void JumpProjNode::dump_compact_spec(outputStream *st) const {
3035   ProjNode::dump_compact_spec(st);
3036   st->print("(%d)%d@%d", _switch_val, _proj_no, _dest_bci);
3037 }
3038 #endif
3039 
3040 //=============================================================================
3041 //------------------------------Value------------------------------------------
3042 // Check for being unreachable, or for coming from a Rethrow.  Rethrow's cannot
3043 // have the default "fall_through_index" path.
3044 const Type* CatchNode::Value(PhaseGVN* phase) const {
3045   // Unreachable?  Then so are all paths from here.
3046   if( phase->type(in(0)) == Type::TOP ) return Type::TOP;
3047   // First assume all paths are reachable
3048   const Type** f = TypeTuple::fields(_size);
3049   for( uint i = 0; i < _size; i++ ) f[i] = Type::CONTROL;
3050   // Identify cases that will always throw an exception
3051   // () rethrow call
3052   // () virtual or interface call with null receiver
3053   // () call is a check cast with incompatible arguments
3054   if( in(1)->is_Proj() ) {
3055     Node *i10 = in(1)->in(0);
3056     if( i10->is_Call() ) {
3057       CallNode *call = i10->as_Call();
3058       // Rethrows always throw exceptions, never return
3059       if (call->entry_point() == OptoRuntime::rethrow_stub()) {
3060         f[CatchProjNode::fall_through_index] = Type::TOP;
3061       } else if (call->is_AllocateArray()) {
3062         Node* klass_node = call->in(AllocateNode::KlassNode);
3063         Node* length = call->in(AllocateNode::ALength);
3064         const Type* length_type = phase->type(length);
3065         const Type* klass_type = phase->type(klass_node);
3066         Node* valid_length_test = call->in(AllocateNode::ValidLengthTest);
3067         const Type* valid_length_test_t = phase->type(valid_length_test);
3068         if (length_type == Type::TOP || klass_type == Type::TOP || valid_length_test_t == Type::TOP ||
3069             valid_length_test_t->is_int()->is_con(0)) {
3070           f[CatchProjNode::fall_through_index] = Type::TOP;
3071         }
3072       } else if( call->req() > TypeFunc::Parms ) {
3073         const Type *arg0 = phase->type( call->in(TypeFunc::Parms) );
3074         // Check for null receiver to virtual or interface calls
3075         if( call->is_CallDynamicJava() &&
3076             arg0->higher_equal(TypePtr::NULL_PTR) ) {
3077           f[CatchProjNode::fall_through_index] = Type::TOP;
3078         }
3079       } // End of if not a runtime stub
3080     } // End of if have call above me
3081   } // End of slot 1 is not a projection
3082   return TypeTuple::make(_size, f);
3083 }
3084 
3085 //=============================================================================
3086 uint CatchProjNode::hash() const {
3087   return Node::hash() + _handler_bci;
3088 }
3089 
3090 
3091 bool CatchProjNode::cmp( const Node &n ) const {
3092   return ProjNode::cmp(n) &&
3093     _handler_bci == ((CatchProjNode&)n)._handler_bci;
3094 }
3095 
3096 
3097 //------------------------------Identity---------------------------------------
3098 // If only 1 target is possible, choose it if it is the main control
3099 Node* CatchProjNode::Identity(PhaseGVN* phase) {
3100   // If my value is control and no other value is, then treat as ID
3101   const TypeTuple *t = phase->type(in(0))->is_tuple();
3102   if (t->field_at(_con) != Type::CONTROL)  return this;
3103   // If we remove the last CatchProj and elide the Catch/CatchProj, then we
3104   // also remove any exception table entry.  Thus we must know the call
3105   // feeding the Catch will not really throw an exception.  This is ok for
3106   // the main fall-thru control (happens when we know a call can never throw
3107   // an exception) or for "rethrow", because a further optimization will
3108   // yank the rethrow (happens when we inline a function that can throw an
3109   // exception and the caller has no handler).  Not legal, e.g., for passing
3110   // a null receiver to a v-call, or passing bad types to a slow-check-cast.
3111   // These cases MUST throw an exception via the runtime system, so the VM
3112   // will be looking for a table entry.
3113   Node *proj = in(0)->in(1);    // Expect a proj feeding CatchNode
3114   CallNode *call;
3115   if (_con != TypeFunc::Control && // Bail out if not the main control.
3116       !(proj->is_Proj() &&      // AND NOT a rethrow
3117         proj->in(0)->is_Call() &&
3118         (call = proj->in(0)->as_Call()) &&
3119         call->entry_point() == OptoRuntime::rethrow_stub()))
3120     return this;
3121 
3122   // Search for any other path being control
3123   for (uint i = 0; i < t->cnt(); i++) {
3124     if (i != _con && t->field_at(i) == Type::CONTROL)
3125       return this;
3126   }
3127   // Only my path is possible; I am identity on control to the jump
3128   return in(0)->in(0);
3129 }
3130 
3131 
3132 #ifndef PRODUCT
3133 void CatchProjNode::dump_spec(outputStream *st) const {
3134   ProjNode::dump_spec(st);
3135   st->print("@bci %d ",_handler_bci);
3136 }
3137 #endif
3138 
3139 //=============================================================================
3140 //------------------------------Identity---------------------------------------
3141 // Check for CreateEx being Identity.
3142 Node* CreateExNode::Identity(PhaseGVN* phase) {
3143   if( phase->type(in(1)) == Type::TOP ) return in(1);
3144   if( phase->type(in(0)) == Type::TOP ) return in(0);
3145   if (phase->type(in(0)->in(0)) == Type::TOP) {
3146     assert(in(0)->is_CatchProj(), "control is CatchProj");
3147     return phase->C->top(); // dead code
3148   }
3149   // We only come from CatchProj, unless the CatchProj goes away.
3150   // If the CatchProj is optimized away, then we just carry the
3151   // exception oop through.
3152 
3153   // CheckCastPPNode::Ideal() for inline types reuses the exception
3154   // paths of a call to perform an allocation: we can see a Phi here.
3155   if (in(1)->is_Phi()) {
3156     return this;
3157   }
3158   CallNode *call = in(1)->in(0)->as_Call();
3159 
3160   return (in(0)->is_CatchProj() && in(0)->in(0)->is_Catch() &&
3161           in(0)->in(0)->in(1) == in(1)) ? this : call->in(TypeFunc::Parms);
3162 }
3163 
3164 //=============================================================================
3165 //------------------------------Value------------------------------------------
3166 // Check for being unreachable.
3167 const Type* NeverBranchNode::Value(PhaseGVN* phase) const {
3168   if (!in(0) || in(0)->is_top()) return Type::TOP;
3169   return bottom_type();
3170 }
3171 
3172 //------------------------------Ideal------------------------------------------
3173 // Check for no longer being part of a loop
3174 Node *NeverBranchNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3175   if (can_reshape && !in(0)->is_Region()) {
3176     // Dead code elimination can sometimes delete this projection so
3177     // if it's not there, there's nothing to do.
3178     Node* fallthru = proj_out_or_null(0);
3179     if (fallthru != nullptr) {
3180       phase->is_IterGVN()->replace_node(fallthru, in(0));
3181     }
3182     return phase->C->top();
3183   }
3184   return nullptr;
3185 }
3186 
3187 #ifndef PRODUCT
3188 void NeverBranchNode::format( PhaseRegAlloc *ra_, outputStream *st) const {
3189   st->print("%s", Name());
3190 }
3191 #endif
3192 
3193 #ifndef PRODUCT
3194 void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
3195   st->print("blackhole ");
3196   bool first = true;
3197   for (uint i = 0; i < req(); i++) {
3198     Node* n = in(i);
3199     if (n != nullptr && OptoReg::is_valid(ra->get_reg_first(n))) {
3200       if (first) {
3201         first = false;
3202       } else {
3203         st->print(", ");
3204       }
3205       char buf[128];
3206       ra->dump_register(n, buf, sizeof(buf));
3207       st->print("%s", buf);
3208     }
3209   }
3210   st->cr();
3211 }
3212 #endif
3213