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