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