1 /*
  2  * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "opto/addnode.hpp"
 26 #include "opto/node.hpp"
 27 #include "precompiled.hpp"
 28 #include "memory/allocation.inline.hpp"
 29 #include "opto/callnode.hpp"
 30 #include "opto/inlinetypenode.hpp"
 31 #include "opto/loopnode.hpp"
 32 #include "opto/movenode.hpp"
 33 #include "opto/opaquenode.hpp"
 34 
 35 
 36 //------------------------------split_thru_region------------------------------
 37 // Split Node 'n' through merge point.
 38 RegionNode* PhaseIdealLoop::split_thru_region(Node* n, RegionNode* region) {
 39   assert(n->is_CFG(), "");
 40   RegionNode* r = new RegionNode(region->req());
 41   IdealLoopTree* loop = get_loop(n);
 42   for (uint i = 1; i < region->req(); i++) {
 43     Node* x = n->clone();
 44     Node* in0 = n->in(0);
 45     if (in0->in(0) == region) x->set_req(0, in0->in(i));
 46     for (uint j = 1; j < n->req(); j++) {
 47       Node* in = n->in(j);
 48       if (get_ctrl(in) == region) {
 49         x->set_req(j, in->in(i));
 50       }
 51     }
 52     _igvn.register_new_node_with_optimizer(x);
 53     set_loop(x, loop);
 54     set_idom(x, x->in(0), dom_depth(x->in(0))+1);
 55     r->init_req(i, x);
 56   }
 57 
 58   // Record region
 59   r->set_req(0,region);         // Not a TRUE RegionNode
 60   _igvn.register_new_node_with_optimizer(r);
 61   set_loop(r, loop);
 62   if (!loop->_child) {
 63     loop->_body.push(r);
 64   }
 65   return r;
 66 }
 67 
 68 //------------------------------split_up---------------------------------------
 69 // Split block-local op up through the phis to empty the current block
 70 bool PhaseIdealLoop::split_up( Node *n, Node *blk1, Node *blk2 ) {
 71   if( n->is_CFG() ) {
 72     assert( n->in(0) != blk1, "Lousy candidate for split-if" );
 73     return false;
 74   }
 75   if (!at_relevant_ctrl(n, blk1, blk2))
 76     return false;               // Not block local
 77   if( n->is_Phi() ) return false; // Local PHIs are expected
 78 
 79   // Recursively split-up inputs
 80   for (uint i = 1; i < n->req(); i++) {
 81     if( split_up( n->in(i), blk1, blk2 ) ) {
 82       // Got split recursively and self went dead?
 83       if (n->outcnt() == 0)
 84         _igvn.remove_dead_node(n);
 85       return true;
 86     }
 87   }
 88 
 89   if (clone_cmp_loadklass_down(n, blk1, blk2)) {
 90     return true;
 91   }
 92 
 93   // Check for needing to clone-up a compare.  Can't do that, it forces
 94   // another (nested) split-if transform.  Instead, clone it "down".
 95   if (clone_cmp_down(n, blk1, blk2)) {
 96     return true;
 97   }
 98 
 99   if (subgraph_has_opaque(n)) {
100     Unique_Node_List wq;
101     wq.push(n);
102     for (uint i = 0; i < wq.size(); i++) {
103       Node* m = wq.at(i);
104       if (m->is_If()) {
105         assert(assertion_predicate_has_loop_opaque_node(m->as_If()), "opaque node not reachable from if?");
106         Node* bol = create_bool_from_template_assertion_predicate(m, nullptr, nullptr, m->in(0));
107         _igvn.replace_input_of(m, 1, bol);
108       } else {
109         assert(!m->is_CFG(), "not CFG expected");
110         for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
111           Node* u = m->fast_out(j);
112           wq.push(u);
113         }
114       }
115     }
116   }
117 
118   if (n->Opcode() == Op_OpaqueZeroTripGuard) {
119     // If this Opaque1 is part of the zero trip guard for a loop:
120     // 1- it can't be shared
121     // 2- the zero trip guard can't be the if that's being split
122     // As a consequence, this node could be assigned control anywhere between its current control and the zero trip guard.
123     // Move it down to get it out of the way of split if and avoid breaking the zero trip guard shape.
124     Node* cmp = n->unique_out();
125     assert(cmp->Opcode() == Op_CmpI, "bad zero trip guard shape");
126     Node* bol = cmp->unique_out();
127     assert(bol->Opcode() == Op_Bool, "bad zero trip guard shape");
128     Node* iff = bol->unique_out();
129     assert(iff->Opcode() == Op_If, "bad zero trip guard shape");
130     set_ctrl(n, iff->in(0));
131     set_ctrl(cmp, iff->in(0));
132     set_ctrl(bol, iff->in(0));
133     return true;
134   }
135 
136   // See if splitting-up a Store.  Any anti-dep loads must go up as
137   // well.  An anti-dep load might be in the wrong block, because in
138   // this particular layout/schedule we ignored anti-deps and allow
139   // memory to be alive twice.  This only works if we do the same
140   // operations on anti-dep loads as we do their killing stores.
141   if( n->is_Store() && n->in(MemNode::Memory)->in(0) == n->in(0) ) {
142     // Get store's memory slice
143     int alias_idx = C->get_alias_index(_igvn.type(n->in(MemNode::Address))->is_ptr());
144 
145     // Get memory-phi anti-dep loads will be using
146     Node *memphi = n->in(MemNode::Memory);
147     assert( memphi->is_Phi(), "" );
148     // Hoist any anti-dep load to the splitting block;
149     // it will then "split-up".
150     for (DUIterator_Fast imax,i = memphi->fast_outs(imax); i < imax; i++) {
151       Node *load = memphi->fast_out(i);
152       if( load->is_Load() && alias_idx == C->get_alias_index(_igvn.type(load->in(MemNode::Address))->is_ptr()) )
153         set_ctrl(load,blk1);
154     }
155   }
156 
157   // Found some other Node; must clone it up
158 #ifndef PRODUCT
159   if( PrintOpto && VerifyLoopOptimizations ) {
160     tty->print("Cloning up: ");
161     n->dump();
162   }
163 #endif
164 
165   // ConvI2L may have type information on it which becomes invalid if
166   // it moves up in the graph so change any clones so widen the type
167   // to TypeLong::INT when pushing it up.
168   const Type* rtype = nullptr;
169   if (n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::INT) {
170     rtype = TypeLong::INT;
171   }
172 
173   // Now actually split-up this guy.  One copy per control path merging.
174   Node *phi = PhiNode::make_blank(blk1, n);
175   for( uint j = 1; j < blk1->req(); j++ ) {
176     Node *x = n->clone();
177     // Widen the type of the ConvI2L when pushing up.
178     if (rtype != nullptr) x->as_Type()->set_type(rtype);
179     if( n->in(0) && n->in(0) == blk1 )
180       x->set_req( 0, blk1->in(j) );
181     for( uint i = 1; i < n->req(); i++ ) {
182       Node *m = n->in(i);
183       if( get_ctrl(m) == blk1 ) {
184         assert( m->in(0) == blk1, "" );
185         x->set_req( i, m->in(j) );
186       }
187     }
188     register_new_node( x, blk1->in(j) );
189     phi->init_req( j, x );
190   }
191   // Announce phi to optimizer
192   register_new_node(phi, blk1);
193 
194   // Remove cloned-up value from optimizer; use phi instead
195   _igvn.replace_node( n, phi );
196 
197   // (There used to be a self-recursive call to split_up() here,
198   // but it is not needed.  All necessary forward walking is done
199   // by do_split_if() below.)
200 
201   return true;
202 }
203 
204 // Look for a (If .. (Bool(CmpP (LoadKlass .. (AddP obj ..)) ..))) and clone all of it down.
205 // There's likely a CheckCastPP on one of the branches of the If, with obj as input.
206 // If the (LoadKlass .. (AddP obj ..)) is not cloned down, then split if transforms this to: (If .. (Bool(CmpP phi1 ..)))
207 // and the CheckCastPP to (CheckCastPP phi2). It's possible then that phi2 is transformed to a CheckCastPP
208 // (through PhiNode::Ideal) and that that CheckCastPP is replaced by another narrower CheckCastPP at the same control
209 // (through ConstraintCastNode::Identity). That could cause the CheckCastPP at the If to become top while (CmpP phi1)
210 // wouldn't constant fold because it's using a different data path. Cloning the whole subgraph down guarantees both the
211 // AddP and CheckCastPP have the same obj input after split if.
212 bool PhaseIdealLoop::clone_cmp_loadklass_down(Node* n, const Node* blk1, const Node* blk2) {
213   if (n->Opcode() == Op_AddP && at_relevant_ctrl(n, blk1, blk2)) {
214     Node_List cmp_nodes;
215     uint old = C->unique();
216     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
217       Node* u1 = n->fast_out(i);
218       if (u1->Opcode() == Op_LoadNKlass && at_relevant_ctrl(u1, blk1, blk2)) {
219         for (DUIterator_Fast jmax, j = u1->fast_outs(jmax); j < jmax; j++) {
220           Node* u2 = u1->fast_out(j);
221           if (u2->Opcode() == Op_DecodeNKlass && at_relevant_ctrl(u2, blk1, blk2)) {
222             for (DUIterator k = u2->outs(); u2->has_out(k); k++) {
223               Node* u3 = u2->out(k);
224               if (at_relevant_ctrl(u3, blk1, blk2) && clone_cmp_down(u3, blk1, blk2)) {
225                 --k;
226               }
227             }
228             for (DUIterator_Fast kmax, k = u2->fast_outs(kmax); k < kmax; k++) {
229               Node* u3 = u2->fast_out(k);
230               if (u3->_idx >= old) {
231                 cmp_nodes.push(u3);
232               }
233             }
234           }
235         }
236       } else if (u1->Opcode() == Op_LoadKlass && at_relevant_ctrl(u1, blk1, blk2)) {
237         for (DUIterator j = u1->outs(); u1->has_out(j); j++) {
238           Node* u2 = u1->out(j);
239           if (at_relevant_ctrl(u2, blk1, blk2) && clone_cmp_down(u2, blk1, blk2)) {
240             --j;
241           }
242         }
243         for (DUIterator_Fast kmax, k = u1->fast_outs(kmax); k < kmax; k++) {
244           Node* u2 = u1->fast_out(k);
245           if (u2->_idx >= old) {
246             cmp_nodes.push(u2);
247           }
248         }
249       }
250     }
251 
252     for (uint i = 0; i < cmp_nodes.size(); ++i) {
253       Node* cmp = cmp_nodes.at(i);
254       clone_loadklass_nodes_at_cmp_index(n, cmp, 1);
255       clone_loadklass_nodes_at_cmp_index(n, cmp, 2);
256     }
257     if (n->outcnt() == 0) {
258       assert(n->is_dead(), "");
259       return true;
260     }
261   }
262   return false;
263 }
264 
265 bool PhaseIdealLoop::at_relevant_ctrl(Node* n, const Node* blk1, const Node* blk2) {
266   return ctrl_or_self(n) == blk1 || ctrl_or_self(n) == blk2;
267 }
268 
269 void PhaseIdealLoop::clone_loadklass_nodes_at_cmp_index(const Node* n, Node* cmp, int i) {
270   Node* decode = cmp->in(i);
271   if (decode->Opcode() == Op_DecodeNKlass) {
272     Node* loadklass = decode->in(1);
273     if (loadklass->Opcode() == Op_LoadNKlass) {
274       Node* addp = loadklass->in(MemNode::Address);
275       if (addp == n) {
276         Node* ctrl = get_ctrl(cmp);
277         Node* decode_clone = decode->clone();
278         Node* loadklass_clone = loadklass->clone();
279         Node* addp_clone = addp->clone();
280         register_new_node(decode_clone, ctrl);
281         register_new_node(loadklass_clone, ctrl);
282         register_new_node(addp_clone, ctrl);
283         _igvn.replace_input_of(cmp, i, decode_clone);
284         _igvn.replace_input_of(decode_clone, 1, loadklass_clone);
285         _igvn.replace_input_of(loadklass_clone, MemNode::Address, addp_clone);
286         if (decode->outcnt() == 0) {
287           _igvn.remove_dead_node(decode);
288         }
289       }
290     }
291   } else {
292     Node* loadklass = cmp->in(i);
293     if (loadklass->Opcode() == Op_LoadKlass) {
294       Node* addp = loadklass->in(MemNode::Address);
295       if (addp == n) {
296         Node* ctrl = get_ctrl(cmp);
297         Node* loadklass_clone = loadklass->clone();
298         Node* addp_clone = addp->clone();
299         register_new_node(loadklass_clone, ctrl);
300         register_new_node(addp_clone, ctrl);
301         _igvn.replace_input_of(cmp, i, loadklass_clone);
302         _igvn.replace_input_of(loadklass_clone, MemNode::Address, addp_clone);
303         if (loadklass->outcnt() == 0) {
304           _igvn.remove_dead_node(loadklass);
305         }
306       }
307     }
308   }
309 }
310 
311 bool PhaseIdealLoop::clone_cmp_down(Node* n, const Node* blk1, const Node* blk2) {
312   if( n->is_Cmp() ) {
313     assert(get_ctrl(n) == blk2 || get_ctrl(n) == blk1, "must be in block with IF");
314     // Check for simple Cmp/Bool/CMove which we can clone-up.  Cmp/Bool/CMove
315     // sequence can have no other users and it must all reside in the split-if
316     // block.  Non-simple Cmp/Bool/CMove sequences are 'cloned-down' below -
317     // private, per-use versions of the Cmp and Bool are made.  These sink to
318     // the CMove block.  If the CMove is in the split-if block, then in the
319     // next iteration this will become a simple Cmp/Bool/CMove set to clone-up.
320     Node *bol, *cmov;
321     if (!(n->outcnt() == 1 && n->unique_out()->is_Bool() &&
322           (bol = n->unique_out()->as_Bool()) &&
323           (at_relevant_ctrl(bol, blk1, blk2) &&
324            bol->outcnt() == 1 &&
325            bol->unique_out()->is_CMove() &&
326            (cmov = bol->unique_out()->as_CMove()) &&
327            at_relevant_ctrl(cmov, blk1, blk2)))) {
328 
329       // Must clone down
330 #ifndef PRODUCT
331       if( PrintOpto && VerifyLoopOptimizations ) {
332         tty->print("Cloning down: ");
333         n->dump();
334       }
335 #endif
336       if (!n->is_FastLock()) {
337         // Clone down any block-local BoolNode uses of this CmpNode
338         for (DUIterator i = n->outs(); n->has_out(i); i++) {
339           Node* bol = n->out(i);
340           assert( bol->is_Bool(), "" );
341           if (bol->outcnt() == 1) {
342             Node* use = bol->unique_out();
343             if (use->Opcode() == Op_Opaque4) {
344               if (use->outcnt() == 1) {
345                 Node* iff = use->unique_out();
346                 assert(iff->is_If(), "unexpected node type");
347                 Node *use_c = iff->in(0);
348                 if (use_c == blk1 || use_c == blk2) {
349                   continue;
350                 }
351               }
352             } else {
353               // We might see an Opaque1 from a loop limit check here
354               assert(use->is_If() || use->is_CMove() || use->Opcode() == Op_Opaque1 || use->is_AllocateArray(), "unexpected node type");
355               Node *use_c = (use->is_If() || use->is_AllocateArray()) ? use->in(0) : get_ctrl(use);
356               if (use_c == blk1 || use_c == blk2) {
357                 assert(use->is_CMove(), "unexpected node type");
358                 continue;
359               }
360             }
361           }
362           if (at_relevant_ctrl(bol, blk1, blk2)) {
363             // Recursively sink any BoolNode
364 #ifndef PRODUCT
365             if( PrintOpto && VerifyLoopOptimizations ) {
366               tty->print("Cloning down: ");
367               bol->dump();
368             }
369 #endif
370             for (DUIterator j = bol->outs(); bol->has_out(j); j++) {
371               Node* u = bol->out(j);
372               // Uses are either IfNodes, CMoves or Opaque4
373               if (u->Opcode() == Op_Opaque4) {
374                 assert(u->in(1) == bol, "bad input");
375                 for (DUIterator_Last kmin, k = u->last_outs(kmin); k >= kmin; --k) {
376                   Node* iff = u->last_out(k);
377                   assert(iff->is_If() || iff->is_CMove(), "unexpected node type");
378                   assert( iff->in(1) == u, "" );
379                   // Get control block of either the CMove or the If input
380                   Node *iff_ctrl = iff->is_If() ? iff->in(0) : get_ctrl(iff);
381                   Node *x1 = bol->clone();
382                   Node *x2 = u->clone();
383                   register_new_node(x1, iff_ctrl);
384                   register_new_node(x2, iff_ctrl);
385                   _igvn.replace_input_of(x2, 1, x1);
386                   _igvn.replace_input_of(iff, 1, x2);
387                 }
388                 _igvn.remove_dead_node(u);
389                 --j;
390               } else {
391                 // We might see an Opaque1 from a loop limit check here
392                 assert(u->is_If() || u->is_CMove() || u->Opcode() == Op_Opaque1 || u->is_AllocateArray(), "unexpected node type");
393                 assert(u->is_AllocateArray() || u->in(1) == bol, "");
394                 assert(!u->is_AllocateArray() || u->in(AllocateNode::ValidLengthTest) == bol, "wrong input to AllocateArray");
395                 // Get control block of either the CMove or the If input
396                 Node *u_ctrl = (u->is_If() || u->is_AllocateArray()) ? u->in(0) : get_ctrl(u);
397                 assert((u_ctrl != blk1 && u_ctrl != blk2) || u->is_CMove(), "won't converge");
398                 Node *x = bol->clone();
399                 register_new_node(x, u_ctrl);
400                 _igvn.replace_input_of(u, u->is_AllocateArray() ? AllocateNode::ValidLengthTest : 1, x);
401                 --j;
402               }
403             }
404             _igvn.remove_dead_node(bol);
405             --i;
406           }
407         }
408       }
409       // Clone down this CmpNode
410       for (DUIterator_Last jmin, j = n->last_outs(jmin); j >= jmin; --j) {
411         Node* use = n->last_out(j);
412         uint pos = 1;
413         if (n->is_FastLock()) {
414           pos = TypeFunc::Parms + 2;
415           assert(use->is_Lock(), "FastLock only used by LockNode");
416         }
417         assert(use->in(pos) == n, "" );
418         Node *x = n->clone();
419         register_new_node(x, ctrl_or_self(use));
420         _igvn.replace_input_of(use, pos, x);
421       }
422       _igvn.remove_dead_node(n);
423 
424       return true;
425     }
426   }
427   return false;
428 }
429 
430 //------------------------------register_new_node------------------------------
431 void PhaseIdealLoop::register_new_node( Node *n, Node *blk ) {
432   assert(!n->is_CFG(), "must be data node");
433   _igvn.register_new_node_with_optimizer(n);
434   set_ctrl(n, blk);
435   IdealLoopTree *loop = get_loop(blk);
436   if( !loop->_child )
437     loop->_body.push(n);
438 }
439 
440 //------------------------------small_cache------------------------------------
441 struct small_cache : public Dict {
442 
443   small_cache() : Dict( cmpkey, hashptr ) {}
444   Node *probe( Node *use_blk ) { return (Node*)((*this)[use_blk]); }
445   void lru_insert( Node *use_blk, Node *new_def ) { Insert(use_blk,new_def); }
446 };
447 
448 //------------------------------spinup-----------------------------------------
449 // "Spin up" the dominator tree, starting at the use site and stopping when we
450 // find the post-dominating point.
451 
452 // We must be at the merge point which post-dominates 'new_false' and
453 // 'new_true'.  Figure out which edges into the RegionNode eventually lead up
454 // to false and which to true.  Put in a PhiNode to merge values; plug in
455 // the appropriate false-arm or true-arm values.  If some path leads to the
456 // original IF, then insert a Phi recursively.
457 Node *PhaseIdealLoop::spinup( Node *iff_dom, Node *new_false, Node *new_true, Node *use_blk, Node *def, small_cache *cache ) {
458   if (use_blk->is_top())        // Handle dead uses
459     return use_blk;
460   Node *prior_n = (Node*)((intptr_t)0xdeadbeef);
461   Node *n = use_blk;            // Get path input
462   assert( use_blk != iff_dom, "" );
463   // Here's the "spinup" the dominator tree loop.  Do a cache-check
464   // along the way, in case we've come this way before.
465   while( n != iff_dom ) {       // Found post-dominating point?
466     prior_n = n;
467     n = idom(n);                // Search higher
468     Node *s = cache->probe( prior_n ); // Check cache
469     if( s ) return s;           // Cache hit!
470   }
471 
472   Node *phi_post;
473   if( prior_n == new_false || prior_n == new_true ) {
474     phi_post = def->clone();
475     phi_post->set_req(0, prior_n );
476     register_new_node(phi_post, prior_n);
477   } else {
478     // This method handles both control uses (looking for Regions) or data
479     // uses (looking for Phis).  If looking for a control use, then we need
480     // to insert a Region instead of a Phi; however Regions always exist
481     // previously (the hash_find_insert below would always hit) so we can
482     // return the existing Region.
483     if( def->is_CFG() ) {
484       phi_post = prior_n;       // If looking for CFG, return prior
485     } else {
486       assert( def->is_Phi(), "" );
487       assert( prior_n->is_Region(), "must be a post-dominating merge point" );
488 
489       // Need a Phi here
490       phi_post = PhiNode::make_blank(prior_n, def);
491       // Search for both true and false on all paths till find one.
492       for( uint i = 1; i < phi_post->req(); i++ ) // For all paths
493         phi_post->init_req( i, spinup( iff_dom, new_false, new_true, prior_n->in(i), def, cache ) );
494       Node *t = _igvn.hash_find_insert(phi_post);
495       if( t ) {                 // See if we already have this one
496         // phi_post will not be used, so kill it
497         _igvn.remove_dead_node(phi_post);
498         phi_post->destruct(&_igvn);
499         phi_post = t;
500       } else {
501         register_new_node( phi_post, prior_n );
502       }
503     }
504   }
505 
506   // Update cache everywhere
507   prior_n = (Node*)((intptr_t)0xdeadbeef);  // Reset IDOM walk
508   n = use_blk;                  // Get path input
509   // Spin-up the idom tree again, basically doing path-compression.
510   // Insert cache entries along the way, so that if we ever hit this
511   // point in the IDOM tree again we'll stop immediately on a cache hit.
512   while( n != iff_dom ) {       // Found post-dominating point?
513     prior_n = n;
514     n = idom(n);                // Search higher
515     cache->lru_insert( prior_n, phi_post ); // Fill cache
516   } // End of while not gone high enough
517 
518   return phi_post;
519 }
520 
521 //------------------------------find_use_block---------------------------------
522 // Find the block a USE is in.  Normally USE's are in the same block as the
523 // using instruction.  For Phi-USE's, the USE is in the predecessor block
524 // along the corresponding path.
525 Node *PhaseIdealLoop::find_use_block( Node *use, Node *def, Node *old_false, Node *new_false, Node *old_true, Node *new_true ) {
526   // CFG uses are their own block
527   if( use->is_CFG() )
528     return use;
529 
530   if( use->is_Phi() ) {         // Phi uses in prior block
531     // Grab the first Phi use; there may be many.
532     // Each will be handled as a separate iteration of
533     // the "while( phi->outcnt() )" loop.
534     uint j;
535     for( j = 1; j < use->req(); j++ )
536       if( use->in(j) == def )
537         break;
538     assert( j < use->req(), "def should be among use's inputs" );
539     return use->in(0)->in(j);
540   }
541   // Normal (non-phi) use
542   Node *use_blk = get_ctrl(use);
543   // Some uses are directly attached to the old (and going away)
544   // false and true branches.
545   if( use_blk == old_false ) {
546     use_blk = new_false;
547     set_ctrl(use, new_false);
548   }
549   if( use_blk == old_true ) {
550     use_blk = new_true;
551     set_ctrl(use, new_true);
552   }
553 
554   if (use_blk == nullptr) {        // He's dead, Jim
555     _igvn.replace_node(use, C->top());
556   }
557 
558   return use_blk;
559 }
560 
561 //------------------------------handle_use-------------------------------------
562 // Handle uses of the merge point.  Basically, split-if makes the merge point
563 // go away so all uses of the merge point must go away as well.  Most block
564 // local uses have already been split-up, through the merge point.  Uses from
565 // far below the merge point can't always be split up (e.g., phi-uses are
566 // pinned) and it makes too much stuff live.  Instead we use a path-based
567 // solution to move uses down.
568 //
569 // If the use is along the pre-split-CFG true branch, then the new use will
570 // be from the post-split-CFG true merge point.  Vice-versa for the false
571 // path.  Some uses will be along both paths; then we sink the use to the
572 // post-dominating location; we may need to insert a Phi there.
573 void PhaseIdealLoop::handle_use( Node *use, Node *def, small_cache *cache, Node *region_dom, Node *new_false, Node *new_true, Node *old_false, Node *old_true ) {
574 
575   Node *use_blk = find_use_block(use,def,old_false,new_false,old_true,new_true);
576   if( !use_blk ) return;        // He's dead, Jim
577 
578   // Walk up the dominator tree until I hit either the old IfFalse, the old
579   // IfTrue or the old If.  Insert Phis where needed.
580   Node *new_def = spinup( region_dom, new_false, new_true, use_blk, def, cache );
581 
582   // Found where this USE goes.  Re-point him.
583   uint i;
584   for( i = 0; i < use->req(); i++ )
585     if( use->in(i) == def )
586       break;
587   assert( i < use->req(), "def should be among use's inputs" );
588   _igvn.replace_input_of(use, i, new_def);
589 }
590 
591 //------------------------------do_split_if------------------------------------
592 // Found an If getting its condition-code input from a Phi in the same block.
593 // Split thru the Region.
594 void PhaseIdealLoop::do_split_if(Node* iff, RegionNode** new_false_region, RegionNode** new_true_region) {
595   if (PrintOpto && VerifyLoopOptimizations) {
596     tty->print_cr("Split-if");
597   }
598   if (TraceLoopOpts) {
599     tty->print_cr("SplitIf");
600   }
601 
602   C->set_major_progress();
603   RegionNode *region = iff->in(0)->as_Region();
604   Node *region_dom = idom(region);
605 
606   // We are going to clone this test (and the control flow with it) up through
607   // the incoming merge point.  We need to empty the current basic block.
608   // Clone any instructions which must be in this block up through the merge
609   // point.
610   DUIterator i, j;
611   bool progress = true;
612   while (progress) {
613     progress = false;
614     for (i = region->outs(); region->has_out(i); i++) {
615       Node* n = region->out(i);
616       if( n == region ) continue;
617       // The IF to be split is OK.
618       if( n == iff ) continue;
619       if( !n->is_Phi() ) {      // Found pinned memory op or such
620         if (split_up(n, region, iff)) {
621           i = region->refresh_out_pos(i);
622           progress = true;
623         }
624         continue;
625       }
626       assert( n->in(0) == region, "" );
627 
628       // Recursively split up all users of a Phi
629       for (j = n->outs(); n->has_out(j); j++) {
630         Node* m = n->out(j);
631         // If m is dead, throw it away, and declare progress
632         if (_loop_or_ctrl[m->_idx] == nullptr) {
633           _igvn.remove_dead_node(m);
634           // fall through
635         } else if (m != iff && split_up(m, region, iff)) {

636           // fall through
637         } else {
638           continue;
639         }
640         // Something unpredictable changed.
641         // Tell the iterators to refresh themselves, and rerun the loop.
642         i = region->refresh_out_pos(i);
643         j = region->refresh_out_pos(j);
644         progress = true;
645       }
646     }
647   }
648 
649   // Now we have no instructions in the block containing the IF.
650   // Split the IF.
651   RegionNode *new_iff = split_thru_region(iff, region);
652 
653   // Replace both uses of 'new_iff' with Regions merging True/False
654   // paths.  This makes 'new_iff' go dead.
655   Node *old_false = nullptr, *old_true = nullptr;
656   RegionNode* new_false = nullptr;
657   RegionNode* new_true = nullptr;
658   for (DUIterator_Last j2min, j2 = iff->last_outs(j2min); j2 >= j2min; --j2) {
659     Node *ifp = iff->last_out(j2);
660     assert( ifp->Opcode() == Op_IfFalse || ifp->Opcode() == Op_IfTrue, "" );
661     ifp->set_req(0, new_iff);
662     RegionNode* ifpx = split_thru_region(ifp, region);
663 
664     // Replace 'If' projection of a Region with a Region of
665     // 'If' projections.
666     ifpx->set_req(0, ifpx);       // A TRUE RegionNode
667 
668     // Setup dominator info
669     set_idom(ifpx, region_dom, dom_depth(region_dom) + 1);
670 
671     // Check for splitting loop tails
672     if( get_loop(iff)->tail() == ifp )
673       get_loop(iff)->_tail = ifpx;
674 
675     // Replace in the graph with lazy-update mechanism
676     new_iff->set_req(0, new_iff); // hook self so it does not go dead
677     lazy_replace(ifp, ifpx);
678     new_iff->set_req(0, region);
679 
680     // Record bits for later xforms
681     if( ifp->Opcode() == Op_IfFalse ) {
682       old_false = ifp;
683       new_false = ifpx;
684     } else {
685       old_true = ifp;
686       new_true = ifpx;
687     }
688   }
689   _igvn.remove_dead_node(new_iff);
690   // Lazy replace IDOM info with the region's dominator
691   lazy_replace(iff, region_dom);
692   lazy_update(region, region_dom); // idom must be update before handle_uses
693   region->set_req(0, nullptr);        // Break the self-cycle. Required for lazy_update to work on region
694 
695   // Now make the original merge point go dead, by handling all its uses.
696   small_cache region_cache;
697   // Preload some control flow in region-cache
698   region_cache.lru_insert( new_false, new_false );
699   region_cache.lru_insert( new_true , new_true  );
700   // Now handle all uses of the splitting block
701   for (DUIterator k = region->outs(); region->has_out(k); k++) {
702     Node* phi = region->out(k);
703     if (!phi->in(0)) {         // Dead phi?  Remove it
704       _igvn.remove_dead_node(phi);
705     } else if (phi == region) { // Found the self-reference
706       continue;                 // No roll-back of DUIterator
707     } else if (phi->is_Phi()) { // Expected common case: Phi hanging off of Region
708       assert(phi->in(0) == region, "Inconsistent graph");
709       // Need a per-def cache.  Phi represents a def, so make a cache
710       small_cache phi_cache;
711 
712       // Inspect all Phi uses to make the Phi go dead
713       for (DUIterator_Last lmin, l = phi->last_outs(lmin); l >= lmin; --l) {
714         Node* use = phi->last_out(l);
715         // Compute the new DEF for this USE.  New DEF depends on the path
716         // taken from the original DEF to the USE.  The new DEF may be some
717         // collection of PHI's merging values from different paths.  The Phis
718         // inserted depend only on the location of the USE.  We use a
719         // 2-element cache to handle multiple uses from the same block.
720         handle_use(use, phi, &phi_cache, region_dom, new_false, new_true, old_false, old_true);
721       } // End of while phi has uses
722       // Remove the dead Phi
723       _igvn.remove_dead_node( phi );
724     } else {
725       assert(phi->in(0) == region, "Inconsistent graph");
726       // Random memory op guarded by Region.  Compute new DEF for USE.
727       handle_use(phi, region, &region_cache, region_dom, new_false, new_true, old_false, old_true);
728     }
729     // Every path above deletes a use of the region, except for the region
730     // self-cycle (which is needed by handle_use calling find_use_block
731     // calling get_ctrl calling get_ctrl_no_update looking for dead
732     // regions).  So roll back the DUIterator innards.
733     --k;
734   } // End of while merge point has phis
735 
736   _igvn.remove_dead_node(region);
737 
738   if (new_false_region != nullptr) {
739     *new_false_region = new_false;
740   }
741   if (new_true_region != nullptr) {
742     *new_true_region = new_true;
743   }
744 
745   DEBUG_ONLY( if (VerifyLoopOptimizations) { verify(); } );
746 }
--- EOF ---