1 /*
2 * Copyright (c) 1999, 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 "memory/allocation.inline.hpp"
26 #include "opto/addnode.hpp"
27 #include "opto/callnode.hpp"
28 #include "opto/loopnode.hpp"
29 #include "opto/movenode.hpp"
30 #include "opto/node.hpp"
31 #include "opto/opaquenode.hpp"
32 #include "opto/predicates.hpp"
33
34 //------------------------------split_thru_region------------------------------
35 // Split Node 'n' through merge point.
36 RegionNode* PhaseIdealLoop::split_thru_region(Node* n, RegionNode* region) {
37 assert(n->is_CFG(), "");
38 RegionNode* r = new RegionNode(region->req());
39 IdealLoopTree* loop = get_loop(n);
40 for (uint i = 1; i < region->req(); i++) {
41 Node* x = n->clone();
42 Node* in0 = n->in(0);
43 if (in0->in(0) == region) x->set_req(0, in0->in(i));
44 for (uint j = 1; j < n->req(); j++) {
45 Node* in = n->in(j);
46 if (get_ctrl(in) == region) {
47 x->set_req(j, in->in(i));
48 }
49 }
50 _igvn.register_new_node_with_optimizer(x);
51 set_loop(x, loop);
52 set_idom(x, x->in(0), dom_depth(x->in(0))+1);
53 r->init_req(i, x);
54 }
55
56 // Record region
57 r->set_req(0,region); // Not a TRUE RegionNode
58 _igvn.register_new_node_with_optimizer(r);
59 set_loop(r, loop);
60 if (!loop->_child) {
61 loop->_body.push(r);
62 }
63 return r;
64 }
65
66 //------------------------------split_up---------------------------------------
67 // Split block-local op up through the phis to empty the current block
68 bool PhaseIdealLoop::split_up( Node *n, Node *blk1, Node *blk2 ) {
69 if( n->is_CFG() ) {
70 assert( n->in(0) != blk1, "Lousy candidate for split-if" );
71 return false;
72 }
73 if (!at_relevant_ctrl(n, blk1, blk2))
74 return false; // Not block local
75 if( n->is_Phi() ) return false; // Local PHIs are expected
76
77 // Recursively split-up inputs
78 for (uint i = 1; i < n->req(); i++) {
79 if( split_up( n->in(i), blk1, blk2 ) ) {
80 // Got split recursively and self went dead?
81 if (n->outcnt() == 0)
82 _igvn.remove_dead_node(n);
83 return true;
84 }
85 }
86
87 if (clone_cmp_loadklass_down(n, blk1, blk2)) {
88 return true;
89 }
90
91 // Check for needing to clone-up a compare. Can't do that, it forces
92 // another (nested) split-if transform. Instead, clone it "down".
93 if (clone_cmp_down(n, blk1, blk2)) {
94 return true;
95 }
96
97 clone_template_assertion_expression_down(n);
98
99 if (n->Opcode() == Op_OpaqueZeroTripGuard) {
100 // If this Opaque1 is part of the zero trip guard for a loop:
101 // 1- it can't be shared
102 // 2- the zero trip guard can't be the if that's being split
103 // As a consequence, this node could be assigned control anywhere between its current control and the zero trip guard.
104 // Move it down to get it out of the way of split if and avoid breaking the zero trip guard shape.
105 Node* cmp = n->unique_out();
106 assert(cmp->Opcode() == Op_CmpI, "bad zero trip guard shape");
107 Node* bol = cmp->unique_out();
108 assert(bol->Opcode() == Op_Bool, "bad zero trip guard shape");
109 Node* iff = bol->unique_out();
110 assert(iff->Opcode() == Op_If, "bad zero trip guard shape");
111 set_ctrl(n, iff->in(0));
112 set_ctrl(cmp, iff->in(0));
113 set_ctrl(bol, iff->in(0));
114 return true;
115 }
116
117 // See if splitting-up a Store. Any anti-dep loads must go up as
118 // well. An anti-dep load might be in the wrong block, because in
119 // this particular layout/schedule we ignored anti-deps and allow
120 // memory to be alive twice. This only works if we do the same
121 // operations on anti-dep loads as we do their killing stores.
122 if( n->is_Store() && n->in(MemNode::Memory)->in(0) == n->in(0) ) {
123 // Get store's memory slice
124 int alias_idx = C->get_alias_index(_igvn.type(n->in(MemNode::Address))->is_ptr());
125
126 // Get memory-phi anti-dep loads will be using
127 Node *memphi = n->in(MemNode::Memory);
128 assert( memphi->is_Phi(), "" );
129 // Hoist any anti-dep load to the splitting block;
130 // it will then "split-up".
131 for (DUIterator_Fast imax,i = memphi->fast_outs(imax); i < imax; i++) {
132 Node *load = memphi->fast_out(i);
133 if( load->is_Load() && alias_idx == C->get_alias_index(_igvn.type(load->in(MemNode::Address))->is_ptr()) )
134 set_ctrl(load,blk1);
135 }
136 }
137
138 // ConvI2L may have type information on it which becomes invalid if
139 // it moves up in the graph so change any clones so widen the type
140 // to TypeLong::INT when pushing it up.
141 const Type* rtype = nullptr;
142 if (n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::INT) {
143 rtype = TypeLong::INT;
144 }
145
146 // Now actually split-up this guy. One copy per control path merging.
147 Node *phi = PhiNode::make_blank(blk1, n);
148 for( uint j = 1; j < blk1->req(); j++ ) {
149 Node *x = n->clone();
150 // Widen the type of the ConvI2L when pushing up.
151 if (rtype != nullptr) x->as_Type()->set_type(rtype);
152 if( n->in(0) && n->in(0) == blk1 )
153 x->set_req( 0, blk1->in(j) );
154 for( uint i = 1; i < n->req(); i++ ) {
155 Node *m = n->in(i);
156 if( get_ctrl(m) == blk1 ) {
157 assert( m->in(0) == blk1, "" );
158 x->set_req( i, m->in(j) );
159 }
160 }
161 register_new_node( x, blk1->in(j) );
162 phi->init_req( j, x );
163 }
164 // Announce phi to optimizer
165 register_new_node(phi, blk1);
166
167 // Remove cloned-up value from optimizer; use phi instead
168 _igvn.replace_node( n, phi );
169
170 // (There used to be a self-recursive call to split_up() here,
171 // but it is not needed. All necessary forward walking is done
172 // by do_split_if() below.)
173
174 return true;
175 }
176
177 // Look for a (If .. (Bool(CmpP (LoadKlass .. (AddP obj ..)) ..))) and clone all of it down.
178 // There's likely a CheckCastPP on one of the branches of the If, with obj as input.
179 // If the (LoadKlass .. (AddP obj ..)) is not cloned down, then split if transforms this to: (If .. (Bool(CmpP phi1 ..)))
180 // and the CheckCastPP to (CheckCastPP phi2). It's possible then that phi2 is transformed to a CheckCastPP
181 // (through PhiNode::Ideal) and that that CheckCastPP is replaced by another narrower CheckCastPP at the same control
182 // (through ConstraintCastNode::Identity). That could cause the CheckCastPP at the If to become top while (CmpP phi1)
183 // wouldn't constant fold because it's using a different data path. Cloning the whole subgraph down guarantees both the
184 // AddP and CheckCastPP have the same obj input after split if.
185 bool PhaseIdealLoop::clone_cmp_loadklass_down(Node* n, const Node* blk1, const Node* blk2) {
186 if (n->Opcode() == Op_AddP && at_relevant_ctrl(n, blk1, blk2)) {
187 Node_List cmp_nodes;
188 uint old = C->unique();
189 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
190 Node* u1 = n->fast_out(i);
191 if (u1->Opcode() == Op_LoadNKlass && at_relevant_ctrl(u1, blk1, blk2)) {
192 for (DUIterator_Fast jmax, j = u1->fast_outs(jmax); j < jmax; j++) {
193 Node* u2 = u1->fast_out(j);
194 if (u2->Opcode() == Op_DecodeNKlass && at_relevant_ctrl(u2, blk1, blk2)) {
195 for (DUIterator k = u2->outs(); u2->has_out(k); k++) {
196 Node* u3 = u2->out(k);
197 if (at_relevant_ctrl(u3, blk1, blk2) && clone_cmp_down(u3, blk1, blk2)) {
198 --k;
199 }
200 }
201 for (DUIterator_Fast kmax, k = u2->fast_outs(kmax); k < kmax; k++) {
202 Node* u3 = u2->fast_out(k);
203 if (u3->_idx >= old) {
204 cmp_nodes.push(u3);
205 }
206 }
207 }
208 }
209 } else if (u1->Opcode() == Op_LoadKlass && at_relevant_ctrl(u1, blk1, blk2)) {
210 for (DUIterator j = u1->outs(); u1->has_out(j); j++) {
211 Node* u2 = u1->out(j);
212 if (at_relevant_ctrl(u2, blk1, blk2) && clone_cmp_down(u2, blk1, blk2)) {
213 --j;
214 }
215 }
216 for (DUIterator_Fast kmax, k = u1->fast_outs(kmax); k < kmax; k++) {
217 Node* u2 = u1->fast_out(k);
218 if (u2->_idx >= old) {
219 cmp_nodes.push(u2);
220 }
221 }
222 }
223 }
224
225 for (uint i = 0; i < cmp_nodes.size(); ++i) {
226 Node* cmp = cmp_nodes.at(i);
227 clone_loadklass_nodes_at_cmp_index(n, cmp, 1);
228 clone_loadklass_nodes_at_cmp_index(n, cmp, 2);
229 }
230 if (n->outcnt() == 0) {
231 assert(n->is_dead(), "");
232 return true;
233 }
234 }
235 return false;
236 }
237
238 bool PhaseIdealLoop::at_relevant_ctrl(Node* n, const Node* blk1, const Node* blk2) {
239 return ctrl_or_self(n) == blk1 || ctrl_or_self(n) == blk2;
240 }
241
242 void PhaseIdealLoop::clone_loadklass_nodes_at_cmp_index(const Node* n, Node* cmp, int i) {
243 Node* decode = cmp->in(i);
244 if (decode->Opcode() == Op_DecodeNKlass) {
245 Node* loadklass = decode->in(1);
246 if (loadklass->Opcode() == Op_LoadNKlass) {
247 Node* addp = loadklass->in(MemNode::Address);
248 if (addp == n) {
249 Node* ctrl = get_ctrl(cmp);
250 Node* decode_clone = decode->clone();
251 Node* loadklass_clone = loadklass->clone();
252 Node* addp_clone = addp->clone();
253 register_new_node(decode_clone, ctrl);
254 register_new_node(loadklass_clone, ctrl);
255 register_new_node(addp_clone, ctrl);
256 _igvn.replace_input_of(cmp, i, decode_clone);
257 _igvn.replace_input_of(decode_clone, 1, loadklass_clone);
258 _igvn.replace_input_of(loadklass_clone, MemNode::Address, addp_clone);
259 if (decode->outcnt() == 0) {
260 _igvn.remove_dead_node(decode);
261 }
262 }
263 }
264 } else {
265 Node* loadklass = cmp->in(i);
266 if (loadklass->Opcode() == Op_LoadKlass) {
267 Node* addp = loadklass->in(MemNode::Address);
268 if (addp == n) {
269 Node* ctrl = get_ctrl(cmp);
270 Node* loadklass_clone = loadklass->clone();
271 Node* addp_clone = addp->clone();
272 register_new_node(loadklass_clone, ctrl);
273 register_new_node(addp_clone, ctrl);
274 _igvn.replace_input_of(cmp, i, loadklass_clone);
275 _igvn.replace_input_of(loadklass_clone, MemNode::Address, addp_clone);
276 if (loadklass->outcnt() == 0) {
277 _igvn.remove_dead_node(loadklass);
278 }
279 }
280 }
281 }
282 }
283
284 bool PhaseIdealLoop::clone_cmp_down(Node* n, const Node* blk1, const Node* blk2) {
285 if( n->is_Cmp() ) {
286 assert(get_ctrl(n) == blk2 || get_ctrl(n) == blk1, "must be in block with IF");
287 // Check for simple Cmp/Bool/CMove which we can clone-up. Cmp/Bool/CMove
288 // sequence can have no other users and it must all reside in the split-if
289 // block. Non-simple Cmp/Bool/CMove sequences are 'cloned-down' below -
290 // private, per-use versions of the Cmp and Bool are made. These sink to
291 // the CMove block. If the CMove is in the split-if block, then in the
292 // next iteration this will become a simple Cmp/Bool/CMove set to clone-up.
293 Node *bol, *cmov;
294 if (!(n->outcnt() == 1 && n->unique_out()->is_Bool() &&
295 (bol = n->unique_out()->as_Bool()) &&
296 (at_relevant_ctrl(bol, blk1, blk2) &&
297 bol->outcnt() == 1 &&
298 bol->unique_out()->is_CMove() &&
299 (cmov = bol->unique_out()->as_CMove()) &&
300 at_relevant_ctrl(cmov, blk1, blk2)))) {
301
302 // Must clone down
303 if (!n->is_FastLock()) {
304 // Clone down any block-local BoolNode uses of this CmpNode
305 for (DUIterator i = n->outs(); n->has_out(i); i++) {
306 Node* bol = n->out(i);
307 assert( bol->is_Bool(), "" );
308 if (bol->outcnt() == 1) {
309 Node* use = bol->unique_out();
310 if (use->is_OpaqueNotNull() || use->is_OpaqueTemplateAssertionPredicate() ||
311 use->is_OpaqueInitializedAssertionPredicate()) {
312 if (use->outcnt() == 1) {
313 Node* iff = use->unique_out();
314 assert(iff->is_If(), "unexpected node type");
315 Node *use_c = iff->in(0);
316 if (use_c == blk1 || use_c == blk2) {
317 continue;
318 }
319 }
320 } else {
321 // We might see an Opaque1 from a loop limit check here
322 assert(use->is_If() || use->is_CMove() || use->Opcode() == Op_Opaque1 || use->is_AllocateArray(), "unexpected node type");
323 Node *use_c = (use->is_If() || use->is_AllocateArray()) ? use->in(0) : get_ctrl(use);
324 if (use_c == blk1 || use_c == blk2) {
325 assert(use->is_CMove(), "unexpected node type");
326 continue;
327 }
328 }
329 }
330 if (at_relevant_ctrl(bol, blk1, blk2)) {
331 // Recursively sink any BoolNode
332 for (DUIterator j = bol->outs(); bol->has_out(j); j++) {
333 Node* u = bol->out(j);
334 // Uses are either IfNodes, CMoves, OpaqueNotNull, or Opaque*AssertionPredicate
335 if (u->is_OpaqueNotNull() || u->is_OpaqueTemplateAssertionPredicate() ||
336 u->is_OpaqueInitializedAssertionPredicate()) {
337 assert(u->in(1) == bol, "bad input");
338 for (DUIterator_Last kmin, k = u->last_outs(kmin); k >= kmin; --k) {
339 Node* iff = u->last_out(k);
340 assert(iff->is_If() || iff->is_CMove(), "unexpected node type");
341 assert( iff->in(1) == u, "" );
342 // Get control block of either the CMove or the If input
343 Node *iff_ctrl = iff->is_If() ? iff->in(0) : get_ctrl(iff);
344 Node *x1 = bol->clone();
345 Node *x2 = u->clone();
346 register_new_node(x1, iff_ctrl);
347 register_new_node(x2, iff_ctrl);
348 _igvn.replace_input_of(x2, 1, x1);
349 _igvn.replace_input_of(iff, 1, x2);
350 }
351 _igvn.remove_dead_node(u);
352 --j;
353 } else {
354 // We might see an Opaque1 from a loop limit check here
355 assert(u->is_If() || u->is_CMove() || u->Opcode() == Op_Opaque1 || u->is_AllocateArray(), "unexpected node type");
356 assert(u->is_AllocateArray() || u->in(1) == bol, "");
357 assert(!u->is_AllocateArray() || u->in(AllocateNode::ValidLengthTest) == bol, "wrong input to AllocateArray");
358 // Get control block of either the CMove or the If input
359 Node *u_ctrl = (u->is_If() || u->is_AllocateArray()) ? u->in(0) : get_ctrl(u);
360 assert((u_ctrl != blk1 && u_ctrl != blk2) || u->is_CMove(), "won't converge");
361 Node *x = bol->clone();
362 register_new_node(x, u_ctrl);
363 _igvn.replace_input_of(u, u->is_AllocateArray() ? AllocateNode::ValidLengthTest : 1, x);
364 --j;
365 }
366 }
367 _igvn.remove_dead_node(bol);
368 --i;
369 }
370 }
371 }
372 // Clone down this CmpNode
373 for (DUIterator_Last jmin, j = n->last_outs(jmin); j >= jmin; --j) {
374 Node* use = n->last_out(j);
375 uint pos = 1;
376 if (n->is_FastLock()) {
377 pos = TypeFunc::Parms + 2;
378 assert(use->is_Lock(), "FastLock only used by LockNode");
379 }
380 assert(use->in(pos) == n, "" );
381 Node *x = n->clone();
382 register_new_node(x, ctrl_or_self(use));
383 _igvn.replace_input_of(use, pos, x);
384 }
385 _igvn.remove_dead_node(n);
386
387 return true;
388 }
389 }
390 return false;
391 }
392
393 // 'n' could be a node belonging to a Template Assertion Expression (i.e. any node between a Template Assertion Predicate
394 // and its OpaqueLoop* nodes (included)). We cannot simply split this node up since this would create a phi node inside
395 // the Template Assertion Expression - making it unrecognizable as such. Therefore, we completely clone the entire
396 // Template Assertion Expression "down". This ensures that we have an untouched copy that is still recognized by the
397 // Template Assertion Predicate matching code.
398 void PhaseIdealLoop::clone_template_assertion_expression_down(Node* node) {
399 if (!TemplateAssertionExpressionNode::is_in_expression(node)) {
400 return;
401 }
402
403 TemplateAssertionExpressionNode template_assertion_expression_node(node);
404 auto clone_expression = [&](IfNode* template_assertion_predicate) {
405 OpaqueTemplateAssertionPredicateNode* opaque_node =
406 template_assertion_predicate->in(1)->as_OpaqueTemplateAssertionPredicate();
407 TemplateAssertionExpression template_assertion_expression(opaque_node, this);
408 Node* new_control = template_assertion_predicate->in(0);
409 OpaqueTemplateAssertionPredicateNode* cloned_opaque_node = template_assertion_expression.clone(new_control,
410 opaque_node->loop_node());
411 igvn().replace_input_of(template_assertion_predicate, 1, cloned_opaque_node);
412 };
413 template_assertion_expression_node.for_each_template_assertion_predicate(clone_expression);
414 }
415
416 //------------------------------register_new_node------------------------------
417 void PhaseIdealLoop::register_new_node( Node *n, Node *blk ) {
418 assert(!n->is_CFG(), "must be data node");
419 _igvn.register_new_node_with_optimizer(n);
420 set_ctrl(n, blk);
421 IdealLoopTree *loop = get_loop(blk);
422 if( !loop->_child )
423 loop->_body.push(n);
424 }
425
426 //------------------------------small_cache------------------------------------
427 struct small_cache : public Dict {
428
429 small_cache() : Dict( cmpkey, hashptr ) {}
430 Node *probe( Node *use_blk ) { return (Node*)((*this)[use_blk]); }
431 void lru_insert( Node *use_blk, Node *new_def ) { Insert(use_blk,new_def); }
432 };
433
434 //------------------------------spinup-----------------------------------------
435 // "Spin up" the dominator tree, starting at the use site and stopping when we
436 // find the post-dominating point.
437
438 // We must be at the merge point which post-dominates 'new_false' and
439 // 'new_true'. Figure out which edges into the RegionNode eventually lead up
440 // to false and which to true. Put in a PhiNode to merge values; plug in
441 // the appropriate false-arm or true-arm values. If some path leads to the
442 // original IF, then insert a Phi recursively.
443 Node *PhaseIdealLoop::spinup( Node *iff_dom, Node *new_false, Node *new_true, Node *use_blk, Node *def, small_cache *cache ) {
444 if (use_blk->is_top()) // Handle dead uses
445 return use_blk;
446 Node *prior_n = (Node*)((intptr_t)0xdeadbeef);
447 Node *n = use_blk; // Get path input
448 assert( use_blk != iff_dom, "" );
449 // Here's the "spinup" the dominator tree loop. Do a cache-check
450 // along the way, in case we've come this way before.
451 while( n != iff_dom ) { // Found post-dominating point?
452 prior_n = n;
453 n = idom(n); // Search higher
454 Node *s = cache->probe( prior_n ); // Check cache
455 if( s ) return s; // Cache hit!
456 }
457
458 Node *phi_post;
459 if( prior_n == new_false || prior_n == new_true ) {
460 phi_post = def->clone();
461 phi_post->set_req(0, prior_n );
462 register_new_node(phi_post, prior_n);
463 } else {
464 // This method handles both control uses (looking for Regions) or data
465 // uses (looking for Phis). If looking for a control use, then we need
466 // to insert a Region instead of a Phi; however Regions always exist
467 // previously (the hash_find_insert below would always hit) so we can
468 // return the existing Region.
469 if( def->is_CFG() ) {
470 phi_post = prior_n; // If looking for CFG, return prior
471 } else {
472 assert( def->is_Phi(), "" );
473 assert( prior_n->is_Region(), "must be a post-dominating merge point" );
474
475 // Need a Phi here
476 phi_post = PhiNode::make_blank(prior_n, def);
477 // Search for both true and false on all paths till find one.
478 for( uint i = 1; i < phi_post->req(); i++ ) // For all paths
479 phi_post->init_req( i, spinup( iff_dom, new_false, new_true, prior_n->in(i), def, cache ) );
480 Node *t = _igvn.hash_find_insert(phi_post);
481 if( t ) { // See if we already have this one
482 // phi_post will not be used, so kill it
483 _igvn.remove_dead_node(phi_post);
484 phi_post->destruct(&_igvn);
485 phi_post = t;
486 } else {
487 register_new_node( phi_post, prior_n );
488 }
489 }
490 }
491
492 // Update cache everywhere
493 prior_n = (Node*)((intptr_t)0xdeadbeef); // Reset IDOM walk
494 n = use_blk; // Get path input
495 // Spin-up the idom tree again, basically doing path-compression.
496 // Insert cache entries along the way, so that if we ever hit this
497 // point in the IDOM tree again we'll stop immediately on a cache hit.
498 while( n != iff_dom ) { // Found post-dominating point?
499 prior_n = n;
500 n = idom(n); // Search higher
501 cache->lru_insert( prior_n, phi_post ); // Fill cache
502 } // End of while not gone high enough
503
504 return phi_post;
505 }
506
507 //------------------------------find_use_block---------------------------------
508 // Find the block a USE is in. Normally USE's are in the same block as the
509 // using instruction. For Phi-USE's, the USE is in the predecessor block
510 // along the corresponding path.
511 Node *PhaseIdealLoop::find_use_block( Node *use, Node *def, Node *old_false, Node *new_false, Node *old_true, Node *new_true ) {
512 // CFG uses are their own block
513 if( use->is_CFG() )
514 return use;
515
516 if( use->is_Phi() ) { // Phi uses in prior block
517 // Grab the first Phi use; there may be many.
518 // Each will be handled as a separate iteration of
519 // the "while( phi->outcnt() )" loop.
520 uint j;
521 for( j = 1; j < use->req(); j++ )
522 if( use->in(j) == def )
523 break;
524 assert( j < use->req(), "def should be among use's inputs" );
525 return use->in(0)->in(j);
526 }
527 // Normal (non-phi) use
528 Node *use_blk = get_ctrl(use);
529 // Some uses are directly attached to the old (and going away)
530 // false and true branches.
531 if( use_blk == old_false ) {
532 use_blk = new_false;
533 set_ctrl(use, new_false);
534 }
535 if( use_blk == old_true ) {
536 use_blk = new_true;
537 set_ctrl(use, new_true);
538 }
539
540 if (use_blk == nullptr) { // He's dead, Jim
541 _igvn.replace_node(use, C->top());
542 }
543
544 return use_blk;
545 }
546
547 //------------------------------handle_use-------------------------------------
548 // Handle uses of the merge point. Basically, split-if makes the merge point
549 // go away so all uses of the merge point must go away as well. Most block
550 // local uses have already been split-up, through the merge point. Uses from
551 // far below the merge point can't always be split up (e.g., phi-uses are
552 // pinned) and it makes too much stuff live. Instead we use a path-based
553 // solution to move uses down.
554 //
555 // If the use is along the pre-split-CFG true branch, then the new use will
556 // be from the post-split-CFG true merge point. Vice-versa for the false
557 // path. Some uses will be along both paths; then we sink the use to the
558 // post-dominating location; we may need to insert a Phi there.
559 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 ) {
560
561 Node *use_blk = find_use_block(use,def,old_false,new_false,old_true,new_true);
562 if( !use_blk ) return; // He's dead, Jim
563
564 // Walk up the dominator tree until I hit either the old IfFalse, the old
565 // IfTrue or the old If. Insert Phis where needed.
566 Node *new_def = spinup( region_dom, new_false, new_true, use_blk, def, cache );
567
568 // Found where this USE goes. Re-point him.
569 uint i;
570 for( i = 0; i < use->req(); i++ )
571 if( use->in(i) == def )
572 break;
573 assert( i < use->req(), "def should be among use's inputs" );
574 _igvn.replace_input_of(use, i, new_def);
575 }
576
577 //------------------------------do_split_if------------------------------------
578 // Found an If getting its condition-code input from a Phi in the same block.
579 // Split thru the Region.
580 void PhaseIdealLoop::do_split_if(Node* iff, RegionNode** new_false_region, RegionNode** new_true_region) {
581
582 C->set_major_progress();
583 RegionNode *region = iff->in(0)->as_Region();
584 Node *region_dom = idom(region);
585
586 // We are going to clone this test (and the control flow with it) up through
587 // the incoming merge point. We need to empty the current basic block.
588 // Clone any instructions which must be in this block up through the merge
589 // point.
590 DUIterator i, j;
591 bool progress = true;
592 while (progress) {
593 progress = false;
594 for (i = region->outs(); region->has_out(i); i++) {
595 Node* n = region->out(i);
596 if( n == region ) continue;
597 // The IF to be split is OK.
598 if( n == iff ) continue;
599 if( !n->is_Phi() ) { // Found pinned memory op or such
600 if (split_up(n, region, iff)) {
601 i = region->refresh_out_pos(i);
602 progress = true;
603 }
604 continue;
605 }
606 assert( n->in(0) == region, "" );
607
608 // Recursively split up all users of a Phi
609 for (j = n->outs(); n->has_out(j); j++) {
610 Node* m = n->out(j);
611 // If m is dead, throw it away, and declare progress
612 if (_loop_or_ctrl[m->_idx] == nullptr) {
613 _igvn.remove_dead_node(m);
614 // fall through
615 }
616 else if (m != iff && split_up(m, region, iff)) {
617 // fall through
618 } else {
619 continue;
620 }
621 // Something unpredictable changed.
622 // Tell the iterators to refresh themselves, and rerun the loop.
623 i = region->refresh_out_pos(i);
624 j = region->refresh_out_pos(j);
625 progress = true;
626 }
627 }
628 }
629
630 // Now we have no instructions in the block containing the IF.
631 // Split the IF.
632 RegionNode *new_iff = split_thru_region(iff, region);
633
634 // Replace both uses of 'new_iff' with Regions merging True/False
635 // paths. This makes 'new_iff' go dead.
636 Node *old_false = nullptr, *old_true = nullptr;
637 RegionNode* new_false = nullptr;
638 RegionNode* new_true = nullptr;
639 for (DUIterator_Last j2min, j2 = iff->last_outs(j2min); j2 >= j2min; --j2) {
640 Node *ifp = iff->last_out(j2);
641 assert( ifp->Opcode() == Op_IfFalse || ifp->Opcode() == Op_IfTrue, "" );
642 ifp->set_req(0, new_iff);
643 RegionNode* ifpx = split_thru_region(ifp, region);
644
645 // Replace 'If' projection of a Region with a Region of
646 // 'If' projections.
647 ifpx->set_req(0, ifpx); // A TRUE RegionNode
648
649 // Setup dominator info
650 set_idom(ifpx, region_dom, dom_depth(region_dom) + 1);
651
652 // Check for splitting loop tails
653 if( get_loop(iff)->tail() == ifp )
654 get_loop(iff)->_tail = ifpx;
655
656 // Replace in the graph with lazy-update mechanism
657 new_iff->set_req(0, new_iff); // hook self so it does not go dead
658 lazy_replace(ifp, ifpx);
659 new_iff->set_req(0, region);
660
661 // Record bits for later xforms
662 if( ifp->Opcode() == Op_IfFalse ) {
663 old_false = ifp;
664 new_false = ifpx;
665 } else {
666 old_true = ifp;
667 new_true = ifpx;
668 }
669 }
670 _igvn.remove_dead_node(new_iff);
671 // Lazy replace IDOM info with the region's dominator
672 lazy_replace(iff, region_dom);
673 lazy_update(region, region_dom); // idom must be update before handle_uses
674 region->set_req(0, nullptr); // Break the self-cycle. Required for lazy_update to work on region
675
676 // Now make the original merge point go dead, by handling all its uses.
677 small_cache region_cache;
678 // Preload some control flow in region-cache
679 region_cache.lru_insert( new_false, new_false );
680 region_cache.lru_insert( new_true , new_true );
681 // Now handle all uses of the splitting block
682 for (DUIterator k = region->outs(); region->has_out(k); k++) {
683 Node* phi = region->out(k);
684 if (!phi->in(0)) { // Dead phi? Remove it
685 _igvn.remove_dead_node(phi);
686 } else if (phi == region) { // Found the self-reference
687 continue; // No roll-back of DUIterator
688 } else if (phi->is_Phi()) { // Expected common case: Phi hanging off of Region
689 assert(phi->in(0) == region, "Inconsistent graph");
690 // Need a per-def cache. Phi represents a def, so make a cache
691 small_cache phi_cache;
692
693 // Inspect all Phi uses to make the Phi go dead
694 for (DUIterator_Last lmin, l = phi->last_outs(lmin); l >= lmin; --l) {
695 Node* use = phi->last_out(l);
696 // Compute the new DEF for this USE. New DEF depends on the path
697 // taken from the original DEF to the USE. The new DEF may be some
698 // collection of PHI's merging values from different paths. The Phis
699 // inserted depend only on the location of the USE. We use a
700 // 2-element cache to handle multiple uses from the same block.
701 handle_use(use, phi, &phi_cache, region_dom, new_false, new_true, old_false, old_true);
702 } // End of while phi has uses
703 // Remove the dead Phi
704 _igvn.remove_dead_node( phi );
705 } else {
706 assert(phi->in(0) == region, "Inconsistent graph");
707 // Random memory op guarded by Region. Compute new DEF for USE.
708 handle_use(phi, region, ®ion_cache, region_dom, new_false, new_true, old_false, old_true);
709 }
710 // Every path above deletes a use of the region, except for the region
711 // self-cycle (which is needed by handle_use calling find_use_block
712 // calling get_ctrl calling get_ctrl_no_update looking for dead
713 // regions). So roll back the DUIterator innards.
714 --k;
715 } // End of while merge point has phis
716
717 _igvn.remove_dead_node(region);
718 if (iff->Opcode() == Op_RangeCheck) {
719 // Pin array access nodes: control is updated here to a region. If, after some transformations, only one path
720 // into the region is left, an array load could become dependent on a condition that's not a range check for
721 // that access. If that condition is replaced by an identical dominating one, then an unpinned load would risk
722 // floating above its range check.
723 pin_array_access_nodes_dependent_on(new_true);
724 pin_array_access_nodes_dependent_on(new_false);
725 }
726
727 if (new_false_region != nullptr) {
728 *new_false_region = new_false;
729 }
730 if (new_true_region != nullptr) {
731 *new_true_region = new_true;
732 }
733
734 DEBUG_ONLY( if (VerifyLoopOptimizations) { verify(); } );
735 }
736
737 void PhaseIdealLoop::pin_array_access_nodes_dependent_on(Node* ctrl) {
738 for (DUIterator i = ctrl->outs(); ctrl->has_out(i); i++) {
739 Node* use = ctrl->out(i);
740 if (!use->depends_only_on_test()) {
741 continue;
742 }
743 Node* pinned_clone = use->pin_array_access_node();
744 if (pinned_clone != nullptr) {
745 register_new_node_with_ctrl_of(pinned_clone, use);
746 _igvn.replace_node(use, pinned_clone);
747 --i;
748 }
749 }
750 }