Mercurial > hg > truffle
comparison src/share/vm/opto/loopopts.cpp @ 0:a61af66fc99e jdk7-b24
Initial load
author | duke |
---|---|
date | Sat, 01 Dec 2007 00:00:00 +0000 |
parents | |
children | e2ae28d2ce91 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a61af66fc99e |
---|---|
1 /* | |
2 * Copyright 1999-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
20 * CA 95054 USA or visit www.sun.com if you need additional information or | |
21 * have any questions. | |
22 * | |
23 */ | |
24 | |
25 #include "incls/_precompiled.incl" | |
26 #include "incls/_loopopts.cpp.incl" | |
27 | |
28 //============================================================================= | |
29 //------------------------------split_thru_phi--------------------------------- | |
30 // Split Node 'n' through merge point if there is enough win. | |
31 Node *PhaseIdealLoop::split_thru_phi( Node *n, Node *region, int policy ) { | |
32 int wins = 0; | |
33 assert( !n->is_CFG(), "" ); | |
34 assert( region->is_Region(), "" ); | |
35 Node *phi = new (C, region->req()) PhiNode( region, n->bottom_type() ); | |
36 uint old_unique = C->unique(); | |
37 for( uint i = 1; i < region->req(); i++ ) { | |
38 Node *x; | |
39 Node* the_clone = NULL; | |
40 if( region->in(i) == C->top() ) { | |
41 x = C->top(); // Dead path? Use a dead data op | |
42 } else { | |
43 x = n->clone(); // Else clone up the data op | |
44 the_clone = x; // Remember for possible deletion. | |
45 // Alter data node to use pre-phi inputs | |
46 if( n->in(0) == region ) | |
47 x->set_req( 0, region->in(i) ); | |
48 for( uint j = 1; j < n->req(); j++ ) { | |
49 Node *in = n->in(j); | |
50 if( in->is_Phi() && in->in(0) == region ) | |
51 x->set_req( j, in->in(i) ); // Use pre-Phi input for the clone | |
52 } | |
53 } | |
54 // Check for a 'win' on some paths | |
55 const Type *t = x->Value(&_igvn); | |
56 | |
57 bool singleton = t->singleton(); | |
58 | |
59 // A TOP singleton indicates that there are no possible values incoming | |
60 // along a particular edge. In most cases, this is OK, and the Phi will | |
61 // be eliminated later in an Ideal call. However, we can't allow this to | |
62 // happen if the singleton occurs on loop entry, as the elimination of | |
63 // the PhiNode may cause the resulting node to migrate back to a previous | |
64 // loop iteration. | |
65 if( singleton && t == Type::TOP ) { | |
66 // Is_Loop() == false does not confirm the absence of a loop (e.g., an | |
67 // irreducible loop may not be indicated by an affirmative is_Loop()); | |
68 // therefore, the only top we can split thru a phi is on a backedge of | |
69 // a loop. | |
70 singleton &= region->is_Loop() && (i != LoopNode::EntryControl); | |
71 } | |
72 | |
73 if( singleton ) { | |
74 wins++; | |
75 x = ((PhaseGVN&)_igvn).makecon(t); | |
76 } else { | |
77 // We now call Identity to try to simplify the cloned node. | |
78 // Note that some Identity methods call phase->type(this). | |
79 // Make sure that the type array is big enough for | |
80 // our new node, even though we may throw the node away. | |
81 // (Note: This tweaking with igvn only works because x is a new node.) | |
82 _igvn.set_type(x, t); | |
83 Node *y = x->Identity(&_igvn); | |
84 if( y != x ) { | |
85 wins++; | |
86 x = y; | |
87 } else { | |
88 y = _igvn.hash_find(x); | |
89 if( y ) { | |
90 wins++; | |
91 x = y; | |
92 } else { | |
93 // Else x is a new node we are keeping | |
94 // We do not need register_new_node_with_optimizer | |
95 // because set_type has already been called. | |
96 _igvn._worklist.push(x); | |
97 } | |
98 } | |
99 } | |
100 if (x != the_clone && the_clone != NULL) | |
101 _igvn.remove_dead_node(the_clone); | |
102 phi->set_req( i, x ); | |
103 } | |
104 // Too few wins? | |
105 if( wins <= policy ) { | |
106 _igvn.remove_dead_node(phi); | |
107 return NULL; | |
108 } | |
109 | |
110 // Record Phi | |
111 register_new_node( phi, region ); | |
112 | |
113 for( uint i2 = 1; i2 < phi->req(); i2++ ) { | |
114 Node *x = phi->in(i2); | |
115 // If we commoned up the cloned 'x' with another existing Node, | |
116 // the existing Node picks up a new use. We need to make the | |
117 // existing Node occur higher up so it dominates its uses. | |
118 Node *old_ctrl; | |
119 IdealLoopTree *old_loop; | |
120 | |
121 // The occasional new node | |
122 if( x->_idx >= old_unique ) { // Found a new, unplaced node? | |
123 old_ctrl = x->is_Con() ? C->root() : NULL; | |
124 old_loop = NULL; // Not in any prior loop | |
125 } else { | |
126 old_ctrl = x->is_Con() ? C->root() : get_ctrl(x); | |
127 old_loop = get_loop(old_ctrl); // Get prior loop | |
128 } | |
129 // New late point must dominate new use | |
130 Node *new_ctrl = dom_lca( old_ctrl, region->in(i2) ); | |
131 // Set new location | |
132 set_ctrl(x, new_ctrl); | |
133 IdealLoopTree *new_loop = get_loop( new_ctrl ); | |
134 // If changing loop bodies, see if we need to collect into new body | |
135 if( old_loop != new_loop ) { | |
136 if( old_loop && !old_loop->_child ) | |
137 old_loop->_body.yank(x); | |
138 if( !new_loop->_child ) | |
139 new_loop->_body.push(x); // Collect body info | |
140 } | |
141 } | |
142 | |
143 return phi; | |
144 } | |
145 | |
146 //------------------------------dominated_by------------------------------------ | |
147 // Replace the dominated test with an obvious true or false. Place it on the | |
148 // IGVN worklist for later cleanup. Move control-dependent data Nodes on the | |
149 // live path up to the dominating control. | |
150 void PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff ) { | |
151 #ifndef PRODUCT | |
152 if( VerifyLoopOptimizations && PrintOpto ) tty->print_cr("dominating test"); | |
153 #endif | |
154 | |
155 | |
156 // prevdom is the dominating projection of the dominating test. | |
157 assert( iff->is_If(), "" ); | |
158 assert( iff->Opcode() == Op_If || iff->Opcode() == Op_CountedLoopEnd, "Check this code when new subtype is added"); | |
159 int pop = prevdom->Opcode(); | |
160 assert( pop == Op_IfFalse || pop == Op_IfTrue, "" ); | |
161 // 'con' is set to true or false to kill the dominated test. | |
162 Node *con = _igvn.makecon(pop == Op_IfTrue ? TypeInt::ONE : TypeInt::ZERO); | |
163 set_ctrl(con, C->root()); // Constant gets a new use | |
164 // Hack the dominated test | |
165 _igvn.hash_delete(iff); | |
166 iff->set_req(1, con); | |
167 _igvn._worklist.push(iff); | |
168 | |
169 // If I dont have a reachable TRUE and FALSE path following the IfNode then | |
170 // I can assume this path reaches an infinite loop. In this case it's not | |
171 // important to optimize the data Nodes - either the whole compilation will | |
172 // be tossed or this path (and all data Nodes) will go dead. | |
173 if( iff->outcnt() != 2 ) return; | |
174 | |
175 // Make control-dependent data Nodes on the live path (path that will remain | |
176 // once the dominated IF is removed) become control-dependent on the | |
177 // dominating projection. | |
178 Node* dp = ((IfNode*)iff)->proj_out(pop == Op_IfTrue); | |
179 IdealLoopTree *old_loop = get_loop(dp); | |
180 | |
181 for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) { | |
182 Node* cd = dp->fast_out(i); // Control-dependent node | |
183 if( cd->depends_only_on_test() ) { | |
184 assert( cd->in(0) == dp, "" ); | |
185 _igvn.hash_delete( cd ); | |
186 cd->set_req(0, prevdom); | |
187 set_early_ctrl( cd ); | |
188 _igvn._worklist.push(cd); | |
189 IdealLoopTree *new_loop = get_loop(get_ctrl(cd)); | |
190 if( old_loop != new_loop ) { | |
191 if( !old_loop->_child ) old_loop->_body.yank(cd); | |
192 if( !new_loop->_child ) new_loop->_body.push(cd); | |
193 } | |
194 --i; | |
195 --imax; | |
196 } | |
197 } | |
198 } | |
199 | |
200 //------------------------------has_local_phi_input---------------------------- | |
201 // Return TRUE if 'n' has Phi inputs from its local block and no other | |
202 // block-local inputs (all non-local-phi inputs come from earlier blocks) | |
203 Node *PhaseIdealLoop::has_local_phi_input( Node *n ) { | |
204 Node *n_ctrl = get_ctrl(n); | |
205 // See if some inputs come from a Phi in this block, or from before | |
206 // this block. | |
207 uint i; | |
208 for( i = 1; i < n->req(); i++ ) { | |
209 Node *phi = n->in(i); | |
210 if( phi->is_Phi() && phi->in(0) == n_ctrl ) | |
211 break; | |
212 } | |
213 if( i >= n->req() ) | |
214 return NULL; // No Phi inputs; nowhere to clone thru | |
215 | |
216 // Check for inputs created between 'n' and the Phi input. These | |
217 // must split as well; they have already been given the chance | |
218 // (courtesy of a post-order visit) and since they did not we must | |
219 // recover the 'cost' of splitting them by being very profitable | |
220 // when splitting 'n'. Since this is unlikely we simply give up. | |
221 for( i = 1; i < n->req(); i++ ) { | |
222 Node *m = n->in(i); | |
223 if( get_ctrl(m) == n_ctrl && !m->is_Phi() ) { | |
224 // We allow the special case of AddP's with no local inputs. | |
225 // This allows us to split-up address expressions. | |
226 if (m->is_AddP() && | |
227 get_ctrl(m->in(2)) != n_ctrl && | |
228 get_ctrl(m->in(3)) != n_ctrl) { | |
229 // Move the AddP up to dominating point | |
230 set_ctrl_and_loop(m, find_non_split_ctrl(idom(n_ctrl))); | |
231 continue; | |
232 } | |
233 return NULL; | |
234 } | |
235 } | |
236 | |
237 return n_ctrl; | |
238 } | |
239 | |
240 //------------------------------remix_address_expressions---------------------- | |
241 // Rework addressing expressions to get the most loop-invariant stuff | |
242 // moved out. We'd like to do all associative operators, but it's especially | |
243 // important (common) to do address expressions. | |
244 Node *PhaseIdealLoop::remix_address_expressions( Node *n ) { | |
245 if (!has_ctrl(n)) return NULL; | |
246 Node *n_ctrl = get_ctrl(n); | |
247 IdealLoopTree *n_loop = get_loop(n_ctrl); | |
248 | |
249 // See if 'n' mixes loop-varying and loop-invariant inputs and | |
250 // itself is loop-varying. | |
251 | |
252 // Only interested in binary ops (and AddP) | |
253 if( n->req() < 3 || n->req() > 4 ) return NULL; | |
254 | |
255 Node *n1_ctrl = get_ctrl(n->in( 1)); | |
256 Node *n2_ctrl = get_ctrl(n->in( 2)); | |
257 Node *n3_ctrl = get_ctrl(n->in(n->req() == 3 ? 2 : 3)); | |
258 IdealLoopTree *n1_loop = get_loop( n1_ctrl ); | |
259 IdealLoopTree *n2_loop = get_loop( n2_ctrl ); | |
260 IdealLoopTree *n3_loop = get_loop( n3_ctrl ); | |
261 | |
262 // Does one of my inputs spin in a tighter loop than self? | |
263 if( (n_loop->is_member( n1_loop ) && n_loop != n1_loop) || | |
264 (n_loop->is_member( n2_loop ) && n_loop != n2_loop) || | |
265 (n_loop->is_member( n3_loop ) && n_loop != n3_loop) ) | |
266 return NULL; // Leave well enough alone | |
267 | |
268 // Is at least one of my inputs loop-invariant? | |
269 if( n1_loop == n_loop && | |
270 n2_loop == n_loop && | |
271 n3_loop == n_loop ) | |
272 return NULL; // No loop-invariant inputs | |
273 | |
274 | |
275 int n_op = n->Opcode(); | |
276 | |
277 // Replace expressions like ((V+I) << 2) with (V<<2 + I<<2). | |
278 if( n_op == Op_LShiftI ) { | |
279 // Scale is loop invariant | |
280 Node *scale = n->in(2); | |
281 Node *scale_ctrl = get_ctrl(scale); | |
282 IdealLoopTree *scale_loop = get_loop(scale_ctrl ); | |
283 if( n_loop == scale_loop || !scale_loop->is_member( n_loop ) ) | |
284 return NULL; | |
285 const TypeInt *scale_t = scale->bottom_type()->isa_int(); | |
286 if( scale_t && scale_t->is_con() && scale_t->get_con() >= 16 ) | |
287 return NULL; // Dont bother with byte/short masking | |
288 // Add must vary with loop (else shift would be loop-invariant) | |
289 Node *add = n->in(1); | |
290 Node *add_ctrl = get_ctrl(add); | |
291 IdealLoopTree *add_loop = get_loop(add_ctrl); | |
292 //assert( n_loop == add_loop, "" ); | |
293 if( n_loop != add_loop ) return NULL; // happens w/ evil ZKM loops | |
294 | |
295 // Convert I-V into I+ (0-V); same for V-I | |
296 if( add->Opcode() == Op_SubI && | |
297 _igvn.type( add->in(1) ) != TypeInt::ZERO ) { | |
298 Node *zero = _igvn.intcon(0); | |
299 set_ctrl(zero, C->root()); | |
300 Node *neg = new (C, 3) SubINode( _igvn.intcon(0), add->in(2) ); | |
301 register_new_node( neg, get_ctrl(add->in(2) ) ); | |
302 add = new (C, 3) AddINode( add->in(1), neg ); | |
303 register_new_node( add, add_ctrl ); | |
304 } | |
305 if( add->Opcode() != Op_AddI ) return NULL; | |
306 // See if one add input is loop invariant | |
307 Node *add_var = add->in(1); | |
308 Node *add_var_ctrl = get_ctrl(add_var); | |
309 IdealLoopTree *add_var_loop = get_loop(add_var_ctrl ); | |
310 Node *add_invar = add->in(2); | |
311 Node *add_invar_ctrl = get_ctrl(add_invar); | |
312 IdealLoopTree *add_invar_loop = get_loop(add_invar_ctrl ); | |
313 if( add_var_loop == n_loop ) { | |
314 } else if( add_invar_loop == n_loop ) { | |
315 // Swap to find the invariant part | |
316 add_invar = add_var; | |
317 add_invar_ctrl = add_var_ctrl; | |
318 add_invar_loop = add_var_loop; | |
319 add_var = add->in(2); | |
320 Node *add_var_ctrl = get_ctrl(add_var); | |
321 IdealLoopTree *add_var_loop = get_loop(add_var_ctrl ); | |
322 } else // Else neither input is loop invariant | |
323 return NULL; | |
324 if( n_loop == add_invar_loop || !add_invar_loop->is_member( n_loop ) ) | |
325 return NULL; // No invariant part of the add? | |
326 | |
327 // Yes! Reshape address expression! | |
328 Node *inv_scale = new (C, 3) LShiftINode( add_invar, scale ); | |
329 register_new_node( inv_scale, add_invar_ctrl ); | |
330 Node *var_scale = new (C, 3) LShiftINode( add_var, scale ); | |
331 register_new_node( var_scale, n_ctrl ); | |
332 Node *var_add = new (C, 3) AddINode( var_scale, inv_scale ); | |
333 register_new_node( var_add, n_ctrl ); | |
334 _igvn.hash_delete( n ); | |
335 _igvn.subsume_node( n, var_add ); | |
336 return var_add; | |
337 } | |
338 | |
339 // Replace (I+V) with (V+I) | |
340 if( n_op == Op_AddI || | |
341 n_op == Op_AddL || | |
342 n_op == Op_AddF || | |
343 n_op == Op_AddD || | |
344 n_op == Op_MulI || | |
345 n_op == Op_MulL || | |
346 n_op == Op_MulF || | |
347 n_op == Op_MulD ) { | |
348 if( n2_loop == n_loop ) { | |
349 assert( n1_loop != n_loop, "" ); | |
350 n->swap_edges(1, 2); | |
351 } | |
352 } | |
353 | |
354 // Replace ((I1 +p V) +p I2) with ((I1 +p I2) +p V), | |
355 // but not if I2 is a constant. | |
356 if( n_op == Op_AddP ) { | |
357 if( n2_loop == n_loop && n3_loop != n_loop ) { | |
358 if( n->in(2)->Opcode() == Op_AddP && !n->in(3)->is_Con() ) { | |
359 Node *n22_ctrl = get_ctrl(n->in(2)->in(2)); | |
360 Node *n23_ctrl = get_ctrl(n->in(2)->in(3)); | |
361 IdealLoopTree *n22loop = get_loop( n22_ctrl ); | |
362 IdealLoopTree *n23_loop = get_loop( n23_ctrl ); | |
363 if( n22loop != n_loop && n22loop->is_member(n_loop) && | |
364 n23_loop == n_loop ) { | |
365 Node *add1 = new (C, 4) AddPNode( n->in(1), n->in(2)->in(2), n->in(3) ); | |
366 // Stuff new AddP in the loop preheader | |
367 register_new_node( add1, n_loop->_head->in(LoopNode::EntryControl) ); | |
368 Node *add2 = new (C, 4) AddPNode( n->in(1), add1, n->in(2)->in(3) ); | |
369 register_new_node( add2, n_ctrl ); | |
370 _igvn.hash_delete( n ); | |
371 _igvn.subsume_node( n, add2 ); | |
372 return add2; | |
373 } | |
374 } | |
375 } | |
376 | |
377 // Replace (I1 +p (I2 + V)) with ((I1 +p I2) +p V) | |
378 if( n2_loop != n_loop && n3_loop == n_loop ) { | |
379 if( n->in(3)->Opcode() == Op_AddI ) { | |
380 Node *V = n->in(3)->in(1); | |
381 Node *I = n->in(3)->in(2); | |
382 if( is_member(n_loop,get_ctrl(V)) ) { | |
383 } else { | |
384 Node *tmp = V; V = I; I = tmp; | |
385 } | |
386 if( !is_member(n_loop,get_ctrl(I)) ) { | |
387 Node *add1 = new (C, 4) AddPNode( n->in(1), n->in(2), I ); | |
388 // Stuff new AddP in the loop preheader | |
389 register_new_node( add1, n_loop->_head->in(LoopNode::EntryControl) ); | |
390 Node *add2 = new (C, 4) AddPNode( n->in(1), add1, V ); | |
391 register_new_node( add2, n_ctrl ); | |
392 _igvn.hash_delete( n ); | |
393 _igvn.subsume_node( n, add2 ); | |
394 return add2; | |
395 } | |
396 } | |
397 } | |
398 } | |
399 | |
400 return NULL; | |
401 } | |
402 | |
403 //------------------------------conditional_move------------------------------- | |
404 // Attempt to replace a Phi with a conditional move. We have some pretty | |
405 // strict profitability requirements. All Phis at the merge point must | |
406 // be converted, so we can remove the control flow. We need to limit the | |
407 // number of c-moves to a small handful. All code that was in the side-arms | |
408 // of the CFG diamond is now speculatively executed. This code has to be | |
409 // "cheap enough". We are pretty much limited to CFG diamonds that merge | |
410 // 1 or 2 items with a total of 1 or 2 ops executed speculatively. | |
411 Node *PhaseIdealLoop::conditional_move( Node *region ) { | |
412 | |
413 assert( region->is_Region(), "sanity check" ); | |
414 if( region->req() != 3 ) return NULL; | |
415 | |
416 // Check for CFG diamond | |
417 Node *lp = region->in(1); | |
418 Node *rp = region->in(2); | |
419 if( !lp || !rp ) return NULL; | |
420 Node *lp_c = lp->in(0); | |
421 if( lp_c == NULL || lp_c != rp->in(0) || !lp_c->is_If() ) return NULL; | |
422 IfNode *iff = lp_c->as_If(); | |
423 | |
424 // Check for highly predictable branch. No point in CMOV'ing if | |
425 // we are going to predict accurately all the time. | |
426 // %%% This hides patterns produced by utility methods like Math.min. | |
427 if( iff->_prob < PROB_UNLIKELY_MAG(3) || | |
428 iff->_prob > PROB_LIKELY_MAG(3) ) | |
429 return NULL; | |
430 | |
431 // Check for ops pinned in an arm of the diamond. | |
432 // Can't remove the control flow in this case | |
433 if( lp->outcnt() > 1 ) return NULL; | |
434 if( rp->outcnt() > 1 ) return NULL; | |
435 | |
436 // Check profitability | |
437 int cost = 0; | |
438 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { | |
439 Node *out = region->fast_out(i); | |
440 if( !out->is_Phi() ) continue; // Ignore other control edges, etc | |
441 PhiNode* phi = out->as_Phi(); | |
442 switch (phi->type()->basic_type()) { | |
443 case T_LONG: | |
444 cost++; // Probably encodes as 2 CMOV's | |
445 case T_INT: // These all CMOV fine | |
446 case T_FLOAT: | |
447 case T_DOUBLE: | |
448 case T_ADDRESS: // (RawPtr) | |
449 cost++; | |
450 break; | |
451 case T_OBJECT: { // Base oops are OK, but not derived oops | |
452 const TypeOopPtr *tp = phi->type()->isa_oopptr(); | |
453 // Derived pointers are Bad (tm): what's the Base (for GC purposes) of a | |
454 // CMOVE'd derived pointer? It's a CMOVE'd derived base. Thus | |
455 // CMOVE'ing a derived pointer requires we also CMOVE the base. If we | |
456 // have a Phi for the base here that we convert to a CMOVE all is well | |
457 // and good. But if the base is dead, we'll not make a CMOVE. Later | |
458 // the allocator will have to produce a base by creating a CMOVE of the | |
459 // relevant bases. This puts the allocator in the business of | |
460 // manufacturing expensive instructions, generally a bad plan. | |
461 // Just Say No to Conditionally-Moved Derived Pointers. | |
462 if( tp && tp->offset() != 0 ) | |
463 return NULL; | |
464 cost++; | |
465 break; | |
466 } | |
467 default: | |
468 return NULL; // In particular, can't do memory or I/O | |
469 } | |
470 // Add in cost any speculative ops | |
471 for( uint j = 1; j < region->req(); j++ ) { | |
472 Node *proj = region->in(j); | |
473 Node *inp = phi->in(j); | |
474 if (get_ctrl(inp) == proj) { // Found local op | |
475 cost++; | |
476 // Check for a chain of dependent ops; these will all become | |
477 // speculative in a CMOV. | |
478 for( uint k = 1; k < inp->req(); k++ ) | |
479 if (get_ctrl(inp->in(k)) == proj) | |
480 return NULL; // Too much speculative goo | |
481 } | |
482 } | |
483 // See if the Phi is used by a Cmp. This will likely Split-If, a | |
484 // higher-payoff operation. | |
485 for (DUIterator_Fast kmax, k = phi->fast_outs(kmax); k < kmax; k++) { | |
486 Node* use = phi->fast_out(k); | |
487 if( use->is_Cmp() ) | |
488 return NULL; | |
489 } | |
490 } | |
491 if( cost >= ConditionalMoveLimit ) return NULL; // Too much goo | |
492 | |
493 // -------------- | |
494 // Now replace all Phis with CMOV's | |
495 Node *cmov_ctrl = iff->in(0); | |
496 uint flip = (lp->Opcode() == Op_IfTrue); | |
497 while( 1 ) { | |
498 PhiNode* phi = NULL; | |
499 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { | |
500 Node *out = region->fast_out(i); | |
501 if (out->is_Phi()) { | |
502 phi = out->as_Phi(); | |
503 break; | |
504 } | |
505 } | |
506 if (phi == NULL) break; | |
507 #ifndef PRODUCT | |
508 if( PrintOpto && VerifyLoopOptimizations ) tty->print_cr("CMOV"); | |
509 #endif | |
510 // Move speculative ops | |
511 for( uint j = 1; j < region->req(); j++ ) { | |
512 Node *proj = region->in(j); | |
513 Node *inp = phi->in(j); | |
514 if (get_ctrl(inp) == proj) { // Found local op | |
515 #ifndef PRODUCT | |
516 if( PrintOpto && VerifyLoopOptimizations ) { | |
517 tty->print(" speculate: "); | |
518 inp->dump(); | |
519 } | |
520 #endif | |
521 set_ctrl(inp, cmov_ctrl); | |
522 } | |
523 } | |
524 Node *cmov = CMoveNode::make( C, cmov_ctrl, iff->in(1), phi->in(1+flip), phi->in(2-flip), _igvn.type(phi) ); | |
525 register_new_node( cmov, cmov_ctrl ); | |
526 _igvn.hash_delete(phi); | |
527 _igvn.subsume_node( phi, cmov ); | |
528 #ifndef PRODUCT | |
529 if( VerifyLoopOptimizations ) verify(); | |
530 #endif | |
531 } | |
532 | |
533 // The useless CFG diamond will fold up later; see the optimization in | |
534 // RegionNode::Ideal. | |
535 _igvn._worklist.push(region); | |
536 | |
537 return iff->in(1); | |
538 } | |
539 | |
540 //------------------------------split_if_with_blocks_pre----------------------- | |
541 // Do the real work in a non-recursive function. Data nodes want to be | |
542 // cloned in the pre-order so they can feed each other nicely. | |
543 Node *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) { | |
544 // Cloning these guys is unlikely to win | |
545 int n_op = n->Opcode(); | |
546 if( n_op == Op_MergeMem ) return n; | |
547 if( n->is_Proj() ) return n; | |
548 // Do not clone-up CmpFXXX variations, as these are always | |
549 // followed by a CmpI | |
550 if( n->is_Cmp() ) return n; | |
551 // Attempt to use a conditional move instead of a phi/branch | |
552 if( ConditionalMoveLimit > 0 && n_op == Op_Region ) { | |
553 Node *cmov = conditional_move( n ); | |
554 if( cmov ) return cmov; | |
555 } | |
556 if( n->is_CFG() || n_op == Op_StorePConditional || n_op == Op_StoreLConditional || n_op == Op_CompareAndSwapI || n_op == Op_CompareAndSwapL ||n_op == Op_CompareAndSwapP) return n; | |
557 if( n_op == Op_Opaque1 || // Opaque nodes cannot be mod'd | |
558 n_op == Op_Opaque2 ) { | |
559 if( !C->major_progress() ) // If chance of no more loop opts... | |
560 _igvn._worklist.push(n); // maybe we'll remove them | |
561 return n; | |
562 } | |
563 | |
564 if( n->is_Con() ) return n; // No cloning for Con nodes | |
565 | |
566 Node *n_ctrl = get_ctrl(n); | |
567 if( !n_ctrl ) return n; // Dead node | |
568 | |
569 // Attempt to remix address expressions for loop invariants | |
570 Node *m = remix_address_expressions( n ); | |
571 if( m ) return m; | |
572 | |
573 // Determine if the Node has inputs from some local Phi. | |
574 // Returns the block to clone thru. | |
575 Node *n_blk = has_local_phi_input( n ); | |
576 if( !n_blk ) return n; | |
577 // Do not clone the trip counter through on a CountedLoop | |
578 // (messes up the canonical shape). | |
579 if( n_blk->is_CountedLoop() && n->Opcode() == Op_AddI ) return n; | |
580 | |
581 // Check for having no control input; not pinned. Allow | |
582 // dominating control. | |
583 if( n->in(0) ) { | |
584 Node *dom = idom(n_blk); | |
585 if( dom_lca( n->in(0), dom ) != n->in(0) ) | |
586 return n; | |
587 } | |
588 // Policy: when is it profitable. You must get more wins than | |
589 // policy before it is considered profitable. Policy is usually 0, | |
590 // so 1 win is considered profitable. Big merges will require big | |
591 // cloning, so get a larger policy. | |
592 int policy = n_blk->req() >> 2; | |
593 | |
594 // If the loop is a candidate for range check elimination, | |
595 // delay splitting through it's phi until a later loop optimization | |
596 if (n_blk->is_CountedLoop()) { | |
597 IdealLoopTree *lp = get_loop(n_blk); | |
598 if (lp && lp->_rce_candidate) { | |
599 return n; | |
600 } | |
601 } | |
602 | |
603 // Use same limit as split_if_with_blocks_post | |
604 if( C->unique() > 35000 ) return n; // Method too big | |
605 | |
606 // Split 'n' through the merge point if it is profitable | |
607 Node *phi = split_thru_phi( n, n_blk, policy ); | |
608 if( !phi ) return n; | |
609 | |
610 // Found a Phi to split thru! | |
611 // Replace 'n' with the new phi | |
612 _igvn.hash_delete(n); | |
613 _igvn.subsume_node( n, phi ); | |
614 // Moved a load around the loop, 'en-registering' something. | |
615 if( n_blk->Opcode() == Op_Loop && n->is_Load() && | |
616 !phi->in(LoopNode::LoopBackControl)->is_Load() ) | |
617 C->set_major_progress(); | |
618 | |
619 return phi; | |
620 } | |
621 | |
622 static bool merge_point_too_heavy(Compile* C, Node* region) { | |
623 // Bail out if the region and its phis have too many users. | |
624 int weight = 0; | |
625 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { | |
626 weight += region->fast_out(i)->outcnt(); | |
627 } | |
628 int nodes_left = MaxNodeLimit - C->unique(); | |
629 if (weight * 8 > nodes_left) { | |
630 #ifndef PRODUCT | |
631 if (PrintOpto) | |
632 tty->print_cr("*** Split-if bails out: %d nodes, region weight %d", C->unique(), weight); | |
633 #endif | |
634 return true; | |
635 } else { | |
636 return false; | |
637 } | |
638 } | |
639 | |
640 #ifdef _LP64 | |
641 static bool merge_point_safe(Node* region) { | |
642 // 4799512: Stop split_if_with_blocks from splitting a block with a ConvI2LNode | |
643 // having a PhiNode input. This sidesteps the dangerous case where the split | |
644 // ConvI2LNode may become TOP if the input Value() does not | |
645 // overlap the ConvI2L range, leaving a node which may not dominate its | |
646 // uses. | |
647 // A better fix for this problem can be found in the BugTraq entry, but | |
648 // expediency for Mantis demands this hack. | |
649 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { | |
650 Node* n = region->fast_out(i); | |
651 if (n->is_Phi()) { | |
652 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { | |
653 Node* m = n->fast_out(j); | |
654 if (m->Opcode() == Op_ConvI2L) { | |
655 return false; | |
656 } | |
657 } | |
658 } | |
659 } | |
660 return true; | |
661 } | |
662 #endif | |
663 | |
664 | |
665 //------------------------------place_near_use--------------------------------- | |
666 // Place some computation next to use but not inside inner loops. | |
667 // For inner loop uses move it to the preheader area. | |
668 Node *PhaseIdealLoop::place_near_use( Node *useblock ) const { | |
669 IdealLoopTree *u_loop = get_loop( useblock ); | |
670 return (u_loop->_irreducible || u_loop->_child) | |
671 ? useblock | |
672 : u_loop->_head->in(LoopNode::EntryControl); | |
673 } | |
674 | |
675 | |
676 //------------------------------split_if_with_blocks_post---------------------- | |
677 // Do the real work in a non-recursive function. CFG hackery wants to be | |
678 // in the post-order, so it can dirty the I-DOM info and not use the dirtied | |
679 // info. | |
680 void PhaseIdealLoop::split_if_with_blocks_post( Node *n ) { | |
681 | |
682 // Cloning Cmp through Phi's involves the split-if transform. | |
683 // FastLock is not used by an If | |
684 if( n->is_Cmp() && !n->is_FastLock() ) { | |
685 if( C->unique() > 35000 ) return; // Method too big | |
686 | |
687 // Do not do 'split-if' if irreducible loops are present. | |
688 if( _has_irreducible_loops ) | |
689 return; | |
690 | |
691 Node *n_ctrl = get_ctrl(n); | |
692 // Determine if the Node has inputs from some local Phi. | |
693 // Returns the block to clone thru. | |
694 Node *n_blk = has_local_phi_input( n ); | |
695 if( n_blk != n_ctrl ) return; | |
696 | |
697 if( merge_point_too_heavy(C, n_ctrl) ) | |
698 return; | |
699 | |
700 if( n->outcnt() != 1 ) return; // Multiple bool's from 1 compare? | |
701 Node *bol = n->unique_out(); | |
702 assert( bol->is_Bool(), "expect a bool here" ); | |
703 if( bol->outcnt() != 1 ) return;// Multiple branches from 1 compare? | |
704 Node *iff = bol->unique_out(); | |
705 | |
706 // Check some safety conditions | |
707 if( iff->is_If() ) { // Classic split-if? | |
708 if( iff->in(0) != n_ctrl ) return; // Compare must be in same blk as if | |
709 } else if (iff->is_CMove()) { // Trying to split-up a CMOVE | |
710 if( get_ctrl(iff->in(2)) == n_ctrl || | |
711 get_ctrl(iff->in(3)) == n_ctrl ) | |
712 return; // Inputs not yet split-up | |
713 if ( get_loop(n_ctrl) != get_loop(get_ctrl(iff)) ) { | |
714 return; // Loop-invar test gates loop-varying CMOVE | |
715 } | |
716 } else { | |
717 return; // some other kind of node, such as an Allocate | |
718 } | |
719 | |
720 // Do not do 'split-if' if some paths are dead. First do dead code | |
721 // elimination and then see if its still profitable. | |
722 for( uint i = 1; i < n_ctrl->req(); i++ ) | |
723 if( n_ctrl->in(i) == C->top() ) | |
724 return; | |
725 | |
726 // When is split-if profitable? Every 'win' on means some control flow | |
727 // goes dead, so it's almost always a win. | |
728 int policy = 0; | |
729 // If trying to do a 'Split-If' at the loop head, it is only | |
730 // profitable if the cmp folds up on BOTH paths. Otherwise we | |
731 // risk peeling a loop forever. | |
732 | |
733 // CNC - Disabled for now. Requires careful handling of loop | |
734 // body selection for the cloned code. Also, make sure we check | |
735 // for any input path not being in the same loop as n_ctrl. For | |
736 // irreducible loops we cannot check for 'n_ctrl->is_Loop()' | |
737 // because the alternative loop entry points won't be converted | |
738 // into LoopNodes. | |
739 IdealLoopTree *n_loop = get_loop(n_ctrl); | |
740 for( uint j = 1; j < n_ctrl->req(); j++ ) | |
741 if( get_loop(n_ctrl->in(j)) != n_loop ) | |
742 return; | |
743 | |
744 #ifdef _LP64 | |
745 // Check for safety of the merge point. | |
746 if( !merge_point_safe(n_ctrl) ) { | |
747 return; | |
748 } | |
749 #endif | |
750 | |
751 // Split compare 'n' through the merge point if it is profitable | |
752 Node *phi = split_thru_phi( n, n_ctrl, policy ); | |
753 if( !phi ) return; | |
754 | |
755 // Found a Phi to split thru! | |
756 // Replace 'n' with the new phi | |
757 _igvn.hash_delete(n); | |
758 _igvn.subsume_node( n, phi ); | |
759 | |
760 // Now split the bool up thru the phi | |
761 Node *bolphi = split_thru_phi( bol, n_ctrl, -1 ); | |
762 _igvn.hash_delete(bol); | |
763 _igvn.subsume_node( bol, bolphi ); | |
764 assert( iff->in(1) == bolphi, "" ); | |
765 if( bolphi->Value(&_igvn)->singleton() ) | |
766 return; | |
767 | |
768 // Conditional-move? Must split up now | |
769 if( !iff->is_If() ) { | |
770 Node *cmovphi = split_thru_phi( iff, n_ctrl, -1 ); | |
771 _igvn.hash_delete(iff); | |
772 _igvn.subsume_node( iff, cmovphi ); | |
773 return; | |
774 } | |
775 | |
776 // Now split the IF | |
777 do_split_if( iff ); | |
778 return; | |
779 } | |
780 | |
781 // Check for an IF ready to split; one that has its | |
782 // condition codes input coming from a Phi at the block start. | |
783 int n_op = n->Opcode(); | |
784 | |
785 // Check for an IF being dominated by another IF same test | |
786 if( n_op == Op_If ) { | |
787 Node *bol = n->in(1); | |
788 uint max = bol->outcnt(); | |
789 // Check for same test used more than once? | |
790 if( n_op == Op_If && max > 1 && bol->is_Bool() ) { | |
791 // Search up IDOMs to see if this IF is dominated. | |
792 Node *cutoff = get_ctrl(bol); | |
793 | |
794 // Now search up IDOMs till cutoff, looking for a dominating test | |
795 Node *prevdom = n; | |
796 Node *dom = idom(prevdom); | |
797 while( dom != cutoff ) { | |
798 if( dom->req() > 1 && dom->in(1) == bol && prevdom->in(0) == dom ) { | |
799 // Replace the dominated test with an obvious true or false. | |
800 // Place it on the IGVN worklist for later cleanup. | |
801 C->set_major_progress(); | |
802 dominated_by( prevdom, n ); | |
803 #ifndef PRODUCT | |
804 if( VerifyLoopOptimizations ) verify(); | |
805 #endif | |
806 return; | |
807 } | |
808 prevdom = dom; | |
809 dom = idom(prevdom); | |
810 } | |
811 } | |
812 } | |
813 | |
814 // See if a shared loop-varying computation has no loop-varying uses. | |
815 // Happens if something is only used for JVM state in uncommon trap exits, | |
816 // like various versions of induction variable+offset. Clone the | |
817 // computation per usage to allow it to sink out of the loop. | |
818 if (has_ctrl(n) && !n->in(0)) {// n not dead and has no control edge (can float about) | |
819 Node *n_ctrl = get_ctrl(n); | |
820 IdealLoopTree *n_loop = get_loop(n_ctrl); | |
821 if( n_loop != _ltree_root ) { | |
822 DUIterator_Fast imax, i = n->fast_outs(imax); | |
823 for (; i < imax; i++) { | |
824 Node* u = n->fast_out(i); | |
825 if( !has_ctrl(u) ) break; // Found control user | |
826 IdealLoopTree *u_loop = get_loop(get_ctrl(u)); | |
827 if( u_loop == n_loop ) break; // Found loop-varying use | |
828 if( n_loop->is_member( u_loop ) ) break; // Found use in inner loop | |
829 if( u->Opcode() == Op_Opaque1 ) break; // Found loop limit, bugfix for 4677003 | |
830 } | |
831 bool did_break = (i < imax); // Did we break out of the previous loop? | |
832 if (!did_break && n->outcnt() > 1) { // All uses in outer loops! | |
833 Node *late_load_ctrl; | |
834 if (n->is_Load()) { | |
835 // If n is a load, get and save the result from get_late_ctrl(), | |
836 // to be later used in calculating the control for n's clones. | |
837 clear_dom_lca_tags(); | |
838 late_load_ctrl = get_late_ctrl(n, n_ctrl); | |
839 } | |
840 // If n is a load, and the late control is the same as the current | |
841 // control, then the cloning of n is a pointless exercise, because | |
842 // GVN will ensure that we end up where we started. | |
843 if (!n->is_Load() || late_load_ctrl != n_ctrl) { | |
844 for (DUIterator_Last jmin, j = n->last_outs(jmin); j >= jmin; ) { | |
845 Node *u = n->last_out(j); // Clone private computation per use | |
846 _igvn.hash_delete(u); | |
847 _igvn._worklist.push(u); | |
848 Node *x = n->clone(); // Clone computation | |
849 Node *x_ctrl = NULL; | |
850 if( u->is_Phi() ) { | |
851 // Replace all uses of normal nodes. Replace Phi uses | |
852 // individually, so the seperate Nodes can sink down | |
853 // different paths. | |
854 uint k = 1; | |
855 while( u->in(k) != n ) k++; | |
856 u->set_req( k, x ); | |
857 // x goes next to Phi input path | |
858 x_ctrl = u->in(0)->in(k); | |
859 --j; | |
860 } else { // Normal use | |
861 // Replace all uses | |
862 for( uint k = 0; k < u->req(); k++ ) { | |
863 if( u->in(k) == n ) { | |
864 u->set_req( k, x ); | |
865 --j; | |
866 } | |
867 } | |
868 x_ctrl = get_ctrl(u); | |
869 } | |
870 | |
871 // Find control for 'x' next to use but not inside inner loops. | |
872 // For inner loop uses get the preheader area. | |
873 x_ctrl = place_near_use(x_ctrl); | |
874 | |
875 if (n->is_Load()) { | |
876 // For loads, add a control edge to a CFG node outside of the loop | |
877 // to force them to not combine and return back inside the loop | |
878 // during GVN optimization (4641526). | |
879 // | |
880 // Because we are setting the actual control input, factor in | |
881 // the result from get_late_ctrl() so we respect any | |
882 // anti-dependences. (6233005). | |
883 x_ctrl = dom_lca(late_load_ctrl, x_ctrl); | |
884 | |
885 // Don't allow the control input to be a CFG splitting node. | |
886 // Such nodes should only have ProjNodes as outs, e.g. IfNode | |
887 // should only have IfTrueNode and IfFalseNode (4985384). | |
888 x_ctrl = find_non_split_ctrl(x_ctrl); | |
889 assert(dom_depth(n_ctrl) <= dom_depth(x_ctrl), "n is later than its clone"); | |
890 | |
891 x->set_req(0, x_ctrl); | |
892 } | |
893 register_new_node(x, x_ctrl); | |
894 | |
895 // Some institutional knowledge is needed here: 'x' is | |
896 // yanked because if the optimizer runs GVN on it all the | |
897 // cloned x's will common up and undo this optimization and | |
898 // be forced back in the loop. This is annoying because it | |
899 // makes +VerifyOpto report false-positives on progress. I | |
900 // tried setting control edges on the x's to force them to | |
901 // not combine, but the matching gets worried when it tries | |
902 // to fold a StoreP and an AddP together (as part of an | |
903 // address expression) and the AddP and StoreP have | |
904 // different controls. | |
905 if( !x->is_Load() ) _igvn._worklist.yank(x); | |
906 } | |
907 _igvn.remove_dead_node(n); | |
908 } | |
909 } | |
910 } | |
911 } | |
912 | |
913 // Check for Opaque2's who's loop has disappeared - who's input is in the | |
914 // same loop nest as their output. Remove 'em, they are no longer useful. | |
915 if( n_op == Op_Opaque2 && | |
916 n->in(1) != NULL && | |
917 get_loop(get_ctrl(n)) == get_loop(get_ctrl(n->in(1))) ) { | |
918 _igvn.add_users_to_worklist(n); | |
919 _igvn.hash_delete(n); | |
920 _igvn.subsume_node( n, n->in(1) ); | |
921 } | |
922 } | |
923 | |
924 //------------------------------split_if_with_blocks--------------------------- | |
925 // Check for aggressive application of 'split-if' optimization, | |
926 // using basic block level info. | |
927 void PhaseIdealLoop::split_if_with_blocks( VectorSet &visited, Node_Stack &nstack ) { | |
928 Node *n = C->root(); | |
929 visited.set(n->_idx); // first, mark node as visited | |
930 // Do pre-visit work for root | |
931 n = split_if_with_blocks_pre( n ); | |
932 uint cnt = n->outcnt(); | |
933 uint i = 0; | |
934 while (true) { | |
935 // Visit all children | |
936 if (i < cnt) { | |
937 Node* use = n->raw_out(i); | |
938 ++i; | |
939 if (use->outcnt() != 0 && !visited.test_set(use->_idx)) { | |
940 // Now do pre-visit work for this use | |
941 use = split_if_with_blocks_pre( use ); | |
942 nstack.push(n, i); // Save parent and next use's index. | |
943 n = use; // Process all children of current use. | |
944 cnt = use->outcnt(); | |
945 i = 0; | |
946 } | |
947 } | |
948 else { | |
949 // All of n's children have been processed, complete post-processing. | |
950 if (cnt != 0 && !n->is_Con()) { | |
951 assert(has_node(n), "no dead nodes"); | |
952 split_if_with_blocks_post( n ); | |
953 } | |
954 if (nstack.is_empty()) { | |
955 // Finished all nodes on stack. | |
956 break; | |
957 } | |
958 // Get saved parent node and next use's index. Visit the rest of uses. | |
959 n = nstack.node(); | |
960 cnt = n->outcnt(); | |
961 i = nstack.index(); | |
962 nstack.pop(); | |
963 } | |
964 } | |
965 } | |
966 | |
967 | |
968 //============================================================================= | |
969 // | |
970 // C L O N E A L O O P B O D Y | |
971 // | |
972 | |
973 //------------------------------clone_iff-------------------------------------- | |
974 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps. | |
975 // "Nearly" because all Nodes have been cloned from the original in the loop, | |
976 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs | |
977 // through the Phi recursively, and return a Bool. | |
978 BoolNode *PhaseIdealLoop::clone_iff( PhiNode *phi, IdealLoopTree *loop ) { | |
979 | |
980 // Convert this Phi into a Phi merging Bools | |
981 uint i; | |
982 for( i = 1; i < phi->req(); i++ ) { | |
983 Node *b = phi->in(i); | |
984 if( b->is_Phi() ) { | |
985 _igvn.hash_delete(phi); | |
986 _igvn._worklist.push(phi); | |
987 phi->set_req(i, clone_iff( b->as_Phi(), loop )); | |
988 } else { | |
989 assert( b->is_Bool(), "" ); | |
990 } | |
991 } | |
992 | |
993 Node *sample_bool = phi->in(1); | |
994 Node *sample_cmp = sample_bool->in(1); | |
995 | |
996 // Make Phis to merge the Cmp's inputs. | |
997 int size = phi->in(0)->req(); | |
998 PhiNode *phi1 = new (C, size) PhiNode( phi->in(0), Type::TOP ); | |
999 PhiNode *phi2 = new (C, size) PhiNode( phi->in(0), Type::TOP ); | |
1000 for( i = 1; i < phi->req(); i++ ) { | |
1001 Node *n1 = phi->in(i)->in(1)->in(1); | |
1002 Node *n2 = phi->in(i)->in(1)->in(2); | |
1003 phi1->set_req( i, n1 ); | |
1004 phi2->set_req( i, n2 ); | |
1005 phi1->set_type( phi1->type()->meet(n1->bottom_type()) ); | |
1006 phi2->set_type( phi2->type()->meet(n2->bottom_type()) ); | |
1007 } | |
1008 // See if these Phis have been made before. | |
1009 // Register with optimizer | |
1010 Node *hit1 = _igvn.hash_find_insert(phi1); | |
1011 if( hit1 ) { // Hit, toss just made Phi | |
1012 _igvn.remove_dead_node(phi1); // Remove new phi | |
1013 assert( hit1->is_Phi(), "" ); | |
1014 phi1 = (PhiNode*)hit1; // Use existing phi | |
1015 } else { // Miss | |
1016 _igvn.register_new_node_with_optimizer(phi1); | |
1017 } | |
1018 Node *hit2 = _igvn.hash_find_insert(phi2); | |
1019 if( hit2 ) { // Hit, toss just made Phi | |
1020 _igvn.remove_dead_node(phi2); // Remove new phi | |
1021 assert( hit2->is_Phi(), "" ); | |
1022 phi2 = (PhiNode*)hit2; // Use existing phi | |
1023 } else { // Miss | |
1024 _igvn.register_new_node_with_optimizer(phi2); | |
1025 } | |
1026 // Register Phis with loop/block info | |
1027 set_ctrl(phi1, phi->in(0)); | |
1028 set_ctrl(phi2, phi->in(0)); | |
1029 // Make a new Cmp | |
1030 Node *cmp = sample_cmp->clone(); | |
1031 cmp->set_req( 1, phi1 ); | |
1032 cmp->set_req( 2, phi2 ); | |
1033 _igvn.register_new_node_with_optimizer(cmp); | |
1034 set_ctrl(cmp, phi->in(0)); | |
1035 | |
1036 // Make a new Bool | |
1037 Node *b = sample_bool->clone(); | |
1038 b->set_req(1,cmp); | |
1039 _igvn.register_new_node_with_optimizer(b); | |
1040 set_ctrl(b, phi->in(0)); | |
1041 | |
1042 assert( b->is_Bool(), "" ); | |
1043 return (BoolNode*)b; | |
1044 } | |
1045 | |
1046 //------------------------------clone_bool------------------------------------- | |
1047 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps. | |
1048 // "Nearly" because all Nodes have been cloned from the original in the loop, | |
1049 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs | |
1050 // through the Phi recursively, and return a Bool. | |
1051 CmpNode *PhaseIdealLoop::clone_bool( PhiNode *phi, IdealLoopTree *loop ) { | |
1052 uint i; | |
1053 // Convert this Phi into a Phi merging Bools | |
1054 for( i = 1; i < phi->req(); i++ ) { | |
1055 Node *b = phi->in(i); | |
1056 if( b->is_Phi() ) { | |
1057 _igvn.hash_delete(phi); | |
1058 _igvn._worklist.push(phi); | |
1059 phi->set_req(i, clone_bool( b->as_Phi(), loop )); | |
1060 } else { | |
1061 assert( b->is_Cmp() || b->is_top(), "inputs are all Cmp or TOP" ); | |
1062 } | |
1063 } | |
1064 | |
1065 Node *sample_cmp = phi->in(1); | |
1066 | |
1067 // Make Phis to merge the Cmp's inputs. | |
1068 int size = phi->in(0)->req(); | |
1069 PhiNode *phi1 = new (C, size) PhiNode( phi->in(0), Type::TOP ); | |
1070 PhiNode *phi2 = new (C, size) PhiNode( phi->in(0), Type::TOP ); | |
1071 for( uint j = 1; j < phi->req(); j++ ) { | |
1072 Node *cmp_top = phi->in(j); // Inputs are all Cmp or TOP | |
1073 Node *n1, *n2; | |
1074 if( cmp_top->is_Cmp() ) { | |
1075 n1 = cmp_top->in(1); | |
1076 n2 = cmp_top->in(2); | |
1077 } else { | |
1078 n1 = n2 = cmp_top; | |
1079 } | |
1080 phi1->set_req( j, n1 ); | |
1081 phi2->set_req( j, n2 ); | |
1082 phi1->set_type( phi1->type()->meet(n1->bottom_type()) ); | |
1083 phi2->set_type( phi2->type()->meet(n2->bottom_type()) ); | |
1084 } | |
1085 | |
1086 // See if these Phis have been made before. | |
1087 // Register with optimizer | |
1088 Node *hit1 = _igvn.hash_find_insert(phi1); | |
1089 if( hit1 ) { // Hit, toss just made Phi | |
1090 _igvn.remove_dead_node(phi1); // Remove new phi | |
1091 assert( hit1->is_Phi(), "" ); | |
1092 phi1 = (PhiNode*)hit1; // Use existing phi | |
1093 } else { // Miss | |
1094 _igvn.register_new_node_with_optimizer(phi1); | |
1095 } | |
1096 Node *hit2 = _igvn.hash_find_insert(phi2); | |
1097 if( hit2 ) { // Hit, toss just made Phi | |
1098 _igvn.remove_dead_node(phi2); // Remove new phi | |
1099 assert( hit2->is_Phi(), "" ); | |
1100 phi2 = (PhiNode*)hit2; // Use existing phi | |
1101 } else { // Miss | |
1102 _igvn.register_new_node_with_optimizer(phi2); | |
1103 } | |
1104 // Register Phis with loop/block info | |
1105 set_ctrl(phi1, phi->in(0)); | |
1106 set_ctrl(phi2, phi->in(0)); | |
1107 // Make a new Cmp | |
1108 Node *cmp = sample_cmp->clone(); | |
1109 cmp->set_req( 1, phi1 ); | |
1110 cmp->set_req( 2, phi2 ); | |
1111 _igvn.register_new_node_with_optimizer(cmp); | |
1112 set_ctrl(cmp, phi->in(0)); | |
1113 | |
1114 assert( cmp->is_Cmp(), "" ); | |
1115 return (CmpNode*)cmp; | |
1116 } | |
1117 | |
1118 //------------------------------sink_use--------------------------------------- | |
1119 // If 'use' was in the loop-exit block, it now needs to be sunk | |
1120 // below the post-loop merge point. | |
1121 void PhaseIdealLoop::sink_use( Node *use, Node *post_loop ) { | |
1122 if (!use->is_CFG() && get_ctrl(use) == post_loop->in(2)) { | |
1123 set_ctrl(use, post_loop); | |
1124 for (DUIterator j = use->outs(); use->has_out(j); j++) | |
1125 sink_use(use->out(j), post_loop); | |
1126 } | |
1127 } | |
1128 | |
1129 //------------------------------clone_loop------------------------------------- | |
1130 // | |
1131 // C L O N E A L O O P B O D Y | |
1132 // | |
1133 // This is the basic building block of the loop optimizations. It clones an | |
1134 // entire loop body. It makes an old_new loop body mapping; with this mapping | |
1135 // you can find the new-loop equivalent to an old-loop node. All new-loop | |
1136 // nodes are exactly equal to their old-loop counterparts, all edges are the | |
1137 // same. All exits from the old-loop now have a RegionNode that merges the | |
1138 // equivalent new-loop path. This is true even for the normal "loop-exit" | |
1139 // condition. All uses of loop-invariant old-loop values now come from (one | |
1140 // or more) Phis that merge their new-loop equivalents. | |
1141 // | |
1142 // This operation leaves the graph in an illegal state: there are two valid | |
1143 // control edges coming from the loop pre-header to both loop bodies. I'll | |
1144 // definitely have to hack the graph after running this transform. | |
1145 // | |
1146 // From this building block I will further edit edges to perform loop peeling | |
1147 // or loop unrolling or iteration splitting (Range-Check-Elimination), etc. | |
1148 // | |
1149 // Parameter side_by_size_idom: | |
1150 // When side_by_size_idom is NULL, the dominator tree is constructed for | |
1151 // the clone loop to dominate the original. Used in construction of | |
1152 // pre-main-post loop sequence. | |
1153 // When nonnull, the clone and original are side-by-side, both are | |
1154 // dominated by the side_by_side_idom node. Used in construction of | |
1155 // unswitched loops. | |
1156 void PhaseIdealLoop::clone_loop( IdealLoopTree *loop, Node_List &old_new, int dd, | |
1157 Node* side_by_side_idom) { | |
1158 | |
1159 // Step 1: Clone the loop body. Make the old->new mapping. | |
1160 uint i; | |
1161 for( i = 0; i < loop->_body.size(); i++ ) { | |
1162 Node *old = loop->_body.at(i); | |
1163 Node *nnn = old->clone(); | |
1164 old_new.map( old->_idx, nnn ); | |
1165 _igvn.register_new_node_with_optimizer(nnn); | |
1166 } | |
1167 | |
1168 | |
1169 // Step 2: Fix the edges in the new body. If the old input is outside the | |
1170 // loop use it. If the old input is INside the loop, use the corresponding | |
1171 // new node instead. | |
1172 for( i = 0; i < loop->_body.size(); i++ ) { | |
1173 Node *old = loop->_body.at(i); | |
1174 Node *nnn = old_new[old->_idx]; | |
1175 // Fix CFG/Loop controlling the new node | |
1176 if (has_ctrl(old)) { | |
1177 set_ctrl(nnn, old_new[get_ctrl(old)->_idx]); | |
1178 } else { | |
1179 set_loop(nnn, loop->_parent); | |
1180 if (old->outcnt() > 0) { | |
1181 set_idom( nnn, old_new[idom(old)->_idx], dd ); | |
1182 } | |
1183 } | |
1184 // Correct edges to the new node | |
1185 for( uint j = 0; j < nnn->req(); j++ ) { | |
1186 Node *n = nnn->in(j); | |
1187 if( n ) { | |
1188 IdealLoopTree *old_in_loop = get_loop( has_ctrl(n) ? get_ctrl(n) : n ); | |
1189 if( loop->is_member( old_in_loop ) ) | |
1190 nnn->set_req(j, old_new[n->_idx]); | |
1191 } | |
1192 } | |
1193 _igvn.hash_find_insert(nnn); | |
1194 } | |
1195 Node *newhead = old_new[loop->_head->_idx]; | |
1196 set_idom(newhead, newhead->in(LoopNode::EntryControl), dd); | |
1197 | |
1198 | |
1199 // Step 3: Now fix control uses. Loop varying control uses have already | |
1200 // been fixed up (as part of all input edges in Step 2). Loop invariant | |
1201 // control uses must be either an IfFalse or an IfTrue. Make a merge | |
1202 // point to merge the old and new IfFalse/IfTrue nodes; make the use | |
1203 // refer to this. | |
1204 ResourceArea *area = Thread::current()->resource_area(); | |
1205 Node_List worklist(area); | |
1206 uint new_counter = C->unique(); | |
1207 for( i = 0; i < loop->_body.size(); i++ ) { | |
1208 Node* old = loop->_body.at(i); | |
1209 if( !old->is_CFG() ) continue; | |
1210 Node* nnn = old_new[old->_idx]; | |
1211 | |
1212 // Copy uses to a worklist, so I can munge the def-use info | |
1213 // with impunity. | |
1214 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++) | |
1215 worklist.push(old->fast_out(j)); | |
1216 | |
1217 while( worklist.size() ) { // Visit all uses | |
1218 Node *use = worklist.pop(); | |
1219 if (!has_node(use)) continue; // Ignore dead nodes | |
1220 IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use ); | |
1221 if( !loop->is_member( use_loop ) && use->is_CFG() ) { | |
1222 // Both OLD and USE are CFG nodes here. | |
1223 assert( use->is_Proj(), "" ); | |
1224 | |
1225 // Clone the loop exit control projection | |
1226 Node *newuse = use->clone(); | |
1227 newuse->set_req(0,nnn); | |
1228 _igvn.register_new_node_with_optimizer(newuse); | |
1229 set_loop(newuse, use_loop); | |
1230 set_idom(newuse, nnn, dom_depth(nnn) + 1 ); | |
1231 | |
1232 // We need a Region to merge the exit from the peeled body and the | |
1233 // exit from the old loop body. | |
1234 RegionNode *r = new (C, 3) RegionNode(3); | |
1235 // Map the old use to the new merge point | |
1236 old_new.map( use->_idx, r ); | |
1237 uint dd_r = MIN2(dom_depth(newuse),dom_depth(use)); | |
1238 assert( dd_r >= dom_depth(dom_lca(newuse,use)), "" ); | |
1239 | |
1240 // The original user of 'use' uses 'r' instead. | |
1241 for (DUIterator_Last lmin, l = use->last_outs(lmin); l >= lmin;) { | |
1242 Node* useuse = use->last_out(l); | |
1243 _igvn.hash_delete(useuse); | |
1244 _igvn._worklist.push(useuse); | |
1245 uint uses_found = 0; | |
1246 if( useuse->in(0) == use ) { | |
1247 useuse->set_req(0, r); | |
1248 uses_found++; | |
1249 if( useuse->is_CFG() ) { | |
1250 assert( dom_depth(useuse) > dd_r, "" ); | |
1251 set_idom(useuse, r, dom_depth(useuse)); | |
1252 } | |
1253 } | |
1254 for( uint k = 1; k < useuse->req(); k++ ) { | |
1255 if( useuse->in(k) == use ) { | |
1256 useuse->set_req(k, r); | |
1257 uses_found++; | |
1258 } | |
1259 } | |
1260 l -= uses_found; // we deleted 1 or more copies of this edge | |
1261 } | |
1262 | |
1263 // Now finish up 'r' | |
1264 r->set_req( 1, newuse ); | |
1265 r->set_req( 2, use ); | |
1266 _igvn.register_new_node_with_optimizer(r); | |
1267 set_loop(r, use_loop); | |
1268 set_idom(r, !side_by_side_idom ? newuse->in(0) : side_by_side_idom, dd_r); | |
1269 } // End of if a loop-exit test | |
1270 } | |
1271 } | |
1272 | |
1273 // Step 4: If loop-invariant use is not control, it must be dominated by a | |
1274 // loop exit IfFalse/IfTrue. Find "proper" loop exit. Make a Region | |
1275 // there if needed. Make a Phi there merging old and new used values. | |
1276 Node_List *split_if_set = NULL; | |
1277 Node_List *split_bool_set = NULL; | |
1278 Node_List *split_cex_set = NULL; | |
1279 for( i = 0; i < loop->_body.size(); i++ ) { | |
1280 Node* old = loop->_body.at(i); | |
1281 Node* nnn = old_new[old->_idx]; | |
1282 // Copy uses to a worklist, so I can munge the def-use info | |
1283 // with impunity. | |
1284 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++) | |
1285 worklist.push(old->fast_out(j)); | |
1286 | |
1287 while( worklist.size() ) { | |
1288 Node *use = worklist.pop(); | |
1289 if (!has_node(use)) continue; // Ignore dead nodes | |
1290 if (use->in(0) == C->top()) continue; | |
1291 IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use ); | |
1292 // Check for data-use outside of loop - at least one of OLD or USE | |
1293 // must not be a CFG node. | |
1294 if( !loop->is_member( use_loop ) && (!old->is_CFG() || !use->is_CFG())) { | |
1295 | |
1296 // If the Data use is an IF, that means we have an IF outside of the | |
1297 // loop that is switching on a condition that is set inside of the | |
1298 // loop. Happens if people set a loop-exit flag; then test the flag | |
1299 // in the loop to break the loop, then test is again outside of the | |
1300 // loop to determine which way the loop exited. | |
1301 if( use->is_If() || use->is_CMove() ) { | |
1302 // Since this code is highly unlikely, we lazily build the worklist | |
1303 // of such Nodes to go split. | |
1304 if( !split_if_set ) | |
1305 split_if_set = new Node_List(area); | |
1306 split_if_set->push(use); | |
1307 } | |
1308 if( use->is_Bool() ) { | |
1309 if( !split_bool_set ) | |
1310 split_bool_set = new Node_List(area); | |
1311 split_bool_set->push(use); | |
1312 } | |
1313 if( use->Opcode() == Op_CreateEx ) { | |
1314 if( !split_cex_set ) | |
1315 split_cex_set = new Node_List(area); | |
1316 split_cex_set->push(use); | |
1317 } | |
1318 | |
1319 | |
1320 // Get "block" use is in | |
1321 uint idx = 0; | |
1322 while( use->in(idx) != old ) idx++; | |
1323 Node *prev = use->is_CFG() ? use : get_ctrl(use); | |
1324 assert( !loop->is_member( get_loop( prev ) ), "" ); | |
1325 Node *cfg = prev->_idx >= new_counter | |
1326 ? prev->in(2) | |
1327 : idom(prev); | |
1328 if( use->is_Phi() ) // Phi use is in prior block | |
1329 cfg = prev->in(idx); // NOT in block of Phi itself | |
1330 if (cfg->is_top()) { // Use is dead? | |
1331 _igvn.hash_delete(use); | |
1332 _igvn._worklist.push(use); | |
1333 use->set_req(idx, C->top()); | |
1334 continue; | |
1335 } | |
1336 | |
1337 while( !loop->is_member( get_loop( cfg ) ) ) { | |
1338 prev = cfg; | |
1339 cfg = cfg->_idx >= new_counter ? cfg->in(2) : idom(cfg); | |
1340 } | |
1341 // If the use occurs after merging several exits from the loop, then | |
1342 // old value must have dominated all those exits. Since the same old | |
1343 // value was used on all those exits we did not need a Phi at this | |
1344 // merge point. NOW we do need a Phi here. Each loop exit value | |
1345 // is now merged with the peeled body exit; each exit gets its own | |
1346 // private Phi and those Phis need to be merged here. | |
1347 Node *phi; | |
1348 if( prev->is_Region() ) { | |
1349 if( idx == 0 ) { // Updating control edge? | |
1350 phi = prev; // Just use existing control | |
1351 } else { // Else need a new Phi | |
1352 phi = PhiNode::make( prev, old ); | |
1353 // Now recursively fix up the new uses of old! | |
1354 for( uint i = 1; i < prev->req(); i++ ) { | |
1355 worklist.push(phi); // Onto worklist once for each 'old' input | |
1356 } | |
1357 } | |
1358 } else { | |
1359 // Get new RegionNode merging old and new loop exits | |
1360 prev = old_new[prev->_idx]; | |
1361 assert( prev, "just made this in step 7" ); | |
1362 if( idx == 0 ) { // Updating control edge? | |
1363 phi = prev; // Just use existing control | |
1364 } else { // Else need a new Phi | |
1365 // Make a new Phi merging data values properly | |
1366 phi = PhiNode::make( prev, old ); | |
1367 phi->set_req( 1, nnn ); | |
1368 } | |
1369 } | |
1370 // If inserting a new Phi, check for prior hits | |
1371 if( idx != 0 ) { | |
1372 Node *hit = _igvn.hash_find_insert(phi); | |
1373 if( hit == NULL ) { | |
1374 _igvn.register_new_node_with_optimizer(phi); // Register new phi | |
1375 } else { // or | |
1376 // Remove the new phi from the graph and use the hit | |
1377 _igvn.remove_dead_node(phi); | |
1378 phi = hit; // Use existing phi | |
1379 } | |
1380 set_ctrl(phi, prev); | |
1381 } | |
1382 // Make 'use' use the Phi instead of the old loop body exit value | |
1383 _igvn.hash_delete(use); | |
1384 _igvn._worklist.push(use); | |
1385 use->set_req(idx, phi); | |
1386 if( use->_idx >= new_counter ) { // If updating new phis | |
1387 // Not needed for correctness, but prevents a weak assert | |
1388 // in AddPNode from tripping (when we end up with different | |
1389 // base & derived Phis that will become the same after | |
1390 // IGVN does CSE). | |
1391 Node *hit = _igvn.hash_find_insert(use); | |
1392 if( hit ) // Go ahead and re-hash for hits. | |
1393 _igvn.subsume_node( use, hit ); | |
1394 } | |
1395 | |
1396 // If 'use' was in the loop-exit block, it now needs to be sunk | |
1397 // below the post-loop merge point. | |
1398 sink_use( use, prev ); | |
1399 } | |
1400 } | |
1401 } | |
1402 | |
1403 // Check for IFs that need splitting/cloning. Happens if an IF outside of | |
1404 // the loop uses a condition set in the loop. The original IF probably | |
1405 // takes control from one or more OLD Regions (which in turn get from NEW | |
1406 // Regions). In any case, there will be a set of Phis for each merge point | |
1407 // from the IF up to where the original BOOL def exists the loop. | |
1408 if( split_if_set ) { | |
1409 while( split_if_set->size() ) { | |
1410 Node *iff = split_if_set->pop(); | |
1411 if( iff->in(1)->is_Phi() ) { | |
1412 BoolNode *b = clone_iff( iff->in(1)->as_Phi(), loop ); | |
1413 _igvn.hash_delete(iff); | |
1414 _igvn._worklist.push(iff); | |
1415 iff->set_req(1, b); | |
1416 } | |
1417 } | |
1418 } | |
1419 if( split_bool_set ) { | |
1420 while( split_bool_set->size() ) { | |
1421 Node *b = split_bool_set->pop(); | |
1422 Node *phi = b->in(1); | |
1423 assert( phi->is_Phi(), "" ); | |
1424 CmpNode *cmp = clone_bool( (PhiNode*)phi, loop ); | |
1425 _igvn.hash_delete(b); | |
1426 _igvn._worklist.push(b); | |
1427 b->set_req(1, cmp); | |
1428 } | |
1429 } | |
1430 if( split_cex_set ) { | |
1431 while( split_cex_set->size() ) { | |
1432 Node *b = split_cex_set->pop(); | |
1433 assert( b->in(0)->is_Region(), "" ); | |
1434 assert( b->in(1)->is_Phi(), "" ); | |
1435 assert( b->in(0)->in(0) == b->in(1)->in(0), "" ); | |
1436 split_up( b, b->in(0), NULL ); | |
1437 } | |
1438 } | |
1439 | |
1440 } | |
1441 | |
1442 | |
1443 //---------------------- stride_of_possible_iv ------------------------------------- | |
1444 // Looks for an iff/bool/comp with one operand of the compare | |
1445 // being a cycle involving an add and a phi, | |
1446 // with an optional truncation (left-shift followed by a right-shift) | |
1447 // of the add. Returns zero if not an iv. | |
1448 int PhaseIdealLoop::stride_of_possible_iv(Node* iff) { | |
1449 Node* trunc1 = NULL; | |
1450 Node* trunc2 = NULL; | |
1451 const TypeInt* ttype = NULL; | |
1452 if (!iff->is_If() || iff->in(1) == NULL || !iff->in(1)->is_Bool()) { | |
1453 return 0; | |
1454 } | |
1455 BoolNode* bl = iff->in(1)->as_Bool(); | |
1456 Node* cmp = bl->in(1); | |
1457 if (!cmp || cmp->Opcode() != Op_CmpI && cmp->Opcode() != Op_CmpU) { | |
1458 return 0; | |
1459 } | |
1460 // Must have an invariant operand | |
1461 if (is_member(get_loop(iff), get_ctrl(cmp->in(2)))) { | |
1462 return 0; | |
1463 } | |
1464 Node* add2 = NULL; | |
1465 Node* cmp1 = cmp->in(1); | |
1466 if (cmp1->is_Phi()) { | |
1467 // (If (Bool (CmpX phi:(Phi ...(Optional-trunc(AddI phi add2))) ))) | |
1468 Node* phi = cmp1; | |
1469 for (uint i = 1; i < phi->req(); i++) { | |
1470 Node* in = phi->in(i); | |
1471 Node* add = CountedLoopNode::match_incr_with_optional_truncation(in, | |
1472 &trunc1, &trunc2, &ttype); | |
1473 if (add && add->in(1) == phi) { | |
1474 add2 = add->in(2); | |
1475 break; | |
1476 } | |
1477 } | |
1478 } else { | |
1479 // (If (Bool (CmpX addtrunc:(Optional-trunc((AddI (Phi ...addtrunc...) add2)) ))) | |
1480 Node* addtrunc = cmp1; | |
1481 Node* add = CountedLoopNode::match_incr_with_optional_truncation(addtrunc, | |
1482 &trunc1, &trunc2, &ttype); | |
1483 if (add && add->in(1)->is_Phi()) { | |
1484 Node* phi = add->in(1); | |
1485 for (uint i = 1; i < phi->req(); i++) { | |
1486 if (phi->in(i) == addtrunc) { | |
1487 add2 = add->in(2); | |
1488 break; | |
1489 } | |
1490 } | |
1491 } | |
1492 } | |
1493 if (add2 != NULL) { | |
1494 const TypeInt* add2t = _igvn.type(add2)->is_int(); | |
1495 if (add2t->is_con()) { | |
1496 return add2t->get_con(); | |
1497 } | |
1498 } | |
1499 return 0; | |
1500 } | |
1501 | |
1502 | |
1503 //---------------------- stay_in_loop ------------------------------------- | |
1504 // Return the (unique) control output node that's in the loop (if it exists.) | |
1505 Node* PhaseIdealLoop::stay_in_loop( Node* n, IdealLoopTree *loop) { | |
1506 Node* unique = NULL; | |
1507 if (!n) return NULL; | |
1508 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { | |
1509 Node* use = n->fast_out(i); | |
1510 if (!has_ctrl(use) && loop->is_member(get_loop(use))) { | |
1511 if (unique != NULL) { | |
1512 return NULL; | |
1513 } | |
1514 unique = use; | |
1515 } | |
1516 } | |
1517 return unique; | |
1518 } | |
1519 | |
1520 //------------------------------ register_node ------------------------------------- | |
1521 // Utility to register node "n" with PhaseIdealLoop | |
1522 void PhaseIdealLoop::register_node(Node* n, IdealLoopTree *loop, Node* pred, int ddepth) { | |
1523 _igvn.register_new_node_with_optimizer(n); | |
1524 loop->_body.push(n); | |
1525 if (n->is_CFG()) { | |
1526 set_loop(n, loop); | |
1527 set_idom(n, pred, ddepth); | |
1528 } else { | |
1529 set_ctrl(n, pred); | |
1530 } | |
1531 } | |
1532 | |
1533 //------------------------------ proj_clone ------------------------------------- | |
1534 // Utility to create an if-projection | |
1535 ProjNode* PhaseIdealLoop::proj_clone(ProjNode* p, IfNode* iff) { | |
1536 ProjNode* c = p->clone()->as_Proj(); | |
1537 c->set_req(0, iff); | |
1538 return c; | |
1539 } | |
1540 | |
1541 //------------------------------ short_circuit_if ------------------------------------- | |
1542 // Force the iff control output to be the live_proj | |
1543 Node* PhaseIdealLoop::short_circuit_if(IfNode* iff, ProjNode* live_proj) { | |
1544 int proj_con = live_proj->_con; | |
1545 assert(proj_con == 0 || proj_con == 1, "false or true projection"); | |
1546 Node *con = _igvn.intcon(proj_con); | |
1547 set_ctrl(con, C->root()); | |
1548 if (iff) { | |
1549 iff->set_req(1, con); | |
1550 } | |
1551 return con; | |
1552 } | |
1553 | |
1554 //------------------------------ insert_if_before_proj ------------------------------------- | |
1555 // Insert a new if before an if projection (* - new node) | |
1556 // | |
1557 // before | |
1558 // if(test) | |
1559 // / \ | |
1560 // v v | |
1561 // other-proj proj (arg) | |
1562 // | |
1563 // after | |
1564 // if(test) | |
1565 // / \ | |
1566 // / v | |
1567 // | * proj-clone | |
1568 // v | | |
1569 // other-proj v | |
1570 // * new_if(relop(cmp[IU](left,right))) | |
1571 // / \ | |
1572 // v v | |
1573 // * new-proj proj | |
1574 // (returned) | |
1575 // | |
1576 ProjNode* PhaseIdealLoop::insert_if_before_proj(Node* left, bool Signed, BoolTest::mask relop, Node* right, ProjNode* proj) { | |
1577 IfNode* iff = proj->in(0)->as_If(); | |
1578 IdealLoopTree *loop = get_loop(proj); | |
1579 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj(); | |
1580 int ddepth = dom_depth(proj); | |
1581 | |
1582 _igvn.hash_delete(iff); | |
1583 _igvn._worklist.push(iff); | |
1584 _igvn.hash_delete(proj); | |
1585 _igvn._worklist.push(proj); | |
1586 | |
1587 proj->set_req(0, NULL); // temporary disconnect | |
1588 ProjNode* proj2 = proj_clone(proj, iff); | |
1589 register_node(proj2, loop, iff, ddepth); | |
1590 | |
1591 Node* cmp = Signed ? (Node*) new (C,3)CmpINode(left, right) : (Node*) new (C,3)CmpUNode(left, right); | |
1592 register_node(cmp, loop, proj2, ddepth); | |
1593 | |
1594 BoolNode* bol = new (C,2)BoolNode(cmp, relop); | |
1595 register_node(bol, loop, proj2, ddepth); | |
1596 | |
1597 IfNode* new_if = new (C,2)IfNode(proj2, bol, iff->_prob, iff->_fcnt); | |
1598 register_node(new_if, loop, proj2, ddepth); | |
1599 | |
1600 proj->set_req(0, new_if); // reattach | |
1601 set_idom(proj, new_if, ddepth); | |
1602 | |
1603 ProjNode* new_exit = proj_clone(other_proj, new_if)->as_Proj(); | |
1604 register_node(new_exit, get_loop(other_proj), new_if, ddepth); | |
1605 | |
1606 return new_exit; | |
1607 } | |
1608 | |
1609 //------------------------------ insert_region_before_proj ------------------------------------- | |
1610 // Insert a region before an if projection (* - new node) | |
1611 // | |
1612 // before | |
1613 // if(test) | |
1614 // / | | |
1615 // v | | |
1616 // proj v | |
1617 // other-proj | |
1618 // | |
1619 // after | |
1620 // if(test) | |
1621 // / | | |
1622 // v | | |
1623 // * proj-clone v | |
1624 // | other-proj | |
1625 // v | |
1626 // * new-region | |
1627 // | | |
1628 // v | |
1629 // * dum_if | |
1630 // / \ | |
1631 // v \ | |
1632 // * dum-proj v | |
1633 // proj | |
1634 // | |
1635 RegionNode* PhaseIdealLoop::insert_region_before_proj(ProjNode* proj) { | |
1636 IfNode* iff = proj->in(0)->as_If(); | |
1637 IdealLoopTree *loop = get_loop(proj); | |
1638 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj(); | |
1639 int ddepth = dom_depth(proj); | |
1640 | |
1641 _igvn.hash_delete(iff); | |
1642 _igvn._worklist.push(iff); | |
1643 _igvn.hash_delete(proj); | |
1644 _igvn._worklist.push(proj); | |
1645 | |
1646 proj->set_req(0, NULL); // temporary disconnect | |
1647 ProjNode* proj2 = proj_clone(proj, iff); | |
1648 register_node(proj2, loop, iff, ddepth); | |
1649 | |
1650 RegionNode* reg = new (C,2)RegionNode(2); | |
1651 reg->set_req(1, proj2); | |
1652 register_node(reg, loop, iff, ddepth); | |
1653 | |
1654 IfNode* dum_if = new (C,2)IfNode(reg, short_circuit_if(NULL, proj), iff->_prob, iff->_fcnt); | |
1655 register_node(dum_if, loop, reg, ddepth); | |
1656 | |
1657 proj->set_req(0, dum_if); // reattach | |
1658 set_idom(proj, dum_if, ddepth); | |
1659 | |
1660 ProjNode* dum_proj = proj_clone(other_proj, dum_if); | |
1661 register_node(dum_proj, loop, dum_if, ddepth); | |
1662 | |
1663 return reg; | |
1664 } | |
1665 | |
1666 //------------------------------ insert_cmpi_loop_exit ------------------------------------- | |
1667 // Clone a signed compare loop exit from an unsigned compare and | |
1668 // insert it before the unsigned cmp on the stay-in-loop path. | |
1669 // All new nodes inserted in the dominator tree between the original | |
1670 // if and it's projections. The original if test is replaced with | |
1671 // a constant to force the stay-in-loop path. | |
1672 // | |
1673 // This is done to make sure that the original if and it's projections | |
1674 // still dominate the same set of control nodes, that the ctrl() relation | |
1675 // from data nodes to them is preserved, and that their loop nesting is | |
1676 // preserved. | |
1677 // | |
1678 // before | |
1679 // if(i <u limit) unsigned compare loop exit | |
1680 // / | | |
1681 // v v | |
1682 // exit-proj stay-in-loop-proj | |
1683 // | |
1684 // after | |
1685 // if(stay-in-loop-const) original if | |
1686 // / | | |
1687 // / v | |
1688 // / if(i < limit) new signed test | |
1689 // / / | | |
1690 // / / v | |
1691 // / / if(i <u limit) new cloned unsigned test | |
1692 // / / / | | |
1693 // v v v | | |
1694 // region | | |
1695 // | | | |
1696 // dum-if | | |
1697 // / | | | |
1698 // ether | | | |
1699 // v v | |
1700 // exit-proj stay-in-loop-proj | |
1701 // | |
1702 IfNode* PhaseIdealLoop::insert_cmpi_loop_exit(IfNode* if_cmpu, IdealLoopTree *loop) { | |
1703 const bool Signed = true; | |
1704 const bool Unsigned = false; | |
1705 | |
1706 BoolNode* bol = if_cmpu->in(1)->as_Bool(); | |
1707 if (bol->_test._test != BoolTest::lt) return NULL; | |
1708 CmpNode* cmpu = bol->in(1)->as_Cmp(); | |
1709 if (cmpu->Opcode() != Op_CmpU) return NULL; | |
1710 int stride = stride_of_possible_iv(if_cmpu); | |
1711 if (stride == 0) return NULL; | |
1712 | |
1713 ProjNode* lp_continue = stay_in_loop(if_cmpu, loop)->as_Proj(); | |
1714 ProjNode* lp_exit = if_cmpu->proj_out(!lp_continue->is_IfTrue())->as_Proj(); | |
1715 | |
1716 Node* limit = NULL; | |
1717 if (stride > 0) { | |
1718 limit = cmpu->in(2); | |
1719 } else { | |
1720 limit = _igvn.makecon(TypeInt::ZERO); | |
1721 set_ctrl(limit, C->root()); | |
1722 } | |
1723 // Create a new region on the exit path | |
1724 RegionNode* reg = insert_region_before_proj(lp_exit); | |
1725 | |
1726 // Clone the if-cmpu-true-false using a signed compare | |
1727 BoolTest::mask rel_i = stride > 0 ? bol->_test._test : BoolTest::ge; | |
1728 ProjNode* cmpi_exit = insert_if_before_proj(cmpu->in(1), Signed, rel_i, limit, lp_continue); | |
1729 reg->add_req(cmpi_exit); | |
1730 | |
1731 // Clone the if-cmpu-true-false | |
1732 BoolTest::mask rel_u = bol->_test._test; | |
1733 ProjNode* cmpu_exit = insert_if_before_proj(cmpu->in(1), Unsigned, rel_u, cmpu->in(2), lp_continue); | |
1734 reg->add_req(cmpu_exit); | |
1735 | |
1736 // Force original if to stay in loop. | |
1737 short_circuit_if(if_cmpu, lp_continue); | |
1738 | |
1739 return cmpi_exit->in(0)->as_If(); | |
1740 } | |
1741 | |
1742 //------------------------------ remove_cmpi_loop_exit ------------------------------------- | |
1743 // Remove a previously inserted signed compare loop exit. | |
1744 void PhaseIdealLoop::remove_cmpi_loop_exit(IfNode* if_cmp, IdealLoopTree *loop) { | |
1745 Node* lp_proj = stay_in_loop(if_cmp, loop); | |
1746 assert(if_cmp->in(1)->in(1)->Opcode() == Op_CmpI && | |
1747 stay_in_loop(lp_proj, loop)->is_If() && | |
1748 stay_in_loop(lp_proj, loop)->in(1)->in(1)->Opcode() == Op_CmpU, "inserted cmpi before cmpu"); | |
1749 Node *con = _igvn.makecon(lp_proj->is_IfTrue() ? TypeInt::ONE : TypeInt::ZERO); | |
1750 set_ctrl(con, C->root()); | |
1751 if_cmp->set_req(1, con); | |
1752 } | |
1753 | |
1754 //------------------------------ scheduled_nodelist ------------------------------------- | |
1755 // Create a post order schedule of nodes that are in the | |
1756 // "member" set. The list is returned in "sched". | |
1757 // The first node in "sched" is the loop head, followed by | |
1758 // nodes which have no inputs in the "member" set, and then | |
1759 // followed by the nodes that have an immediate input dependence | |
1760 // on a node in "sched". | |
1761 void PhaseIdealLoop::scheduled_nodelist( IdealLoopTree *loop, VectorSet& member, Node_List &sched ) { | |
1762 | |
1763 assert(member.test(loop->_head->_idx), "loop head must be in member set"); | |
1764 Arena *a = Thread::current()->resource_area(); | |
1765 VectorSet visited(a); | |
1766 Node_Stack nstack(a, loop->_body.size()); | |
1767 | |
1768 Node* n = loop->_head; // top of stack is cached in "n" | |
1769 uint idx = 0; | |
1770 visited.set(n->_idx); | |
1771 | |
1772 // Initially push all with no inputs from within member set | |
1773 for(uint i = 0; i < loop->_body.size(); i++ ) { | |
1774 Node *elt = loop->_body.at(i); | |
1775 if (member.test(elt->_idx)) { | |
1776 bool found = false; | |
1777 for (uint j = 0; j < elt->req(); j++) { | |
1778 Node* def = elt->in(j); | |
1779 if (def && member.test(def->_idx) && def != elt) { | |
1780 found = true; | |
1781 break; | |
1782 } | |
1783 } | |
1784 if (!found && elt != loop->_head) { | |
1785 nstack.push(n, idx); | |
1786 n = elt; | |
1787 assert(!visited.test(n->_idx), "not seen yet"); | |
1788 visited.set(n->_idx); | |
1789 } | |
1790 } | |
1791 } | |
1792 | |
1793 // traverse out's that are in the member set | |
1794 while (true) { | |
1795 if (idx < n->outcnt()) { | |
1796 Node* use = n->raw_out(idx); | |
1797 idx++; | |
1798 if (!visited.test_set(use->_idx)) { | |
1799 if (member.test(use->_idx)) { | |
1800 nstack.push(n, idx); | |
1801 n = use; | |
1802 idx = 0; | |
1803 } | |
1804 } | |
1805 } else { | |
1806 // All outputs processed | |
1807 sched.push(n); | |
1808 if (nstack.is_empty()) break; | |
1809 n = nstack.node(); | |
1810 idx = nstack.index(); | |
1811 nstack.pop(); | |
1812 } | |
1813 } | |
1814 } | |
1815 | |
1816 | |
1817 //------------------------------ has_use_in_set ------------------------------------- | |
1818 // Has a use in the vector set | |
1819 bool PhaseIdealLoop::has_use_in_set( Node* n, VectorSet& vset ) { | |
1820 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { | |
1821 Node* use = n->fast_out(j); | |
1822 if (vset.test(use->_idx)) { | |
1823 return true; | |
1824 } | |
1825 } | |
1826 return false; | |
1827 } | |
1828 | |
1829 | |
1830 //------------------------------ has_use_internal_to_set ------------------------------------- | |
1831 // Has use internal to the vector set (ie. not in a phi at the loop head) | |
1832 bool PhaseIdealLoop::has_use_internal_to_set( Node* n, VectorSet& vset, IdealLoopTree *loop ) { | |
1833 Node* head = loop->_head; | |
1834 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { | |
1835 Node* use = n->fast_out(j); | |
1836 if (vset.test(use->_idx) && !(use->is_Phi() && use->in(0) == head)) { | |
1837 return true; | |
1838 } | |
1839 } | |
1840 return false; | |
1841 } | |
1842 | |
1843 | |
1844 //------------------------------ clone_for_use_outside_loop ------------------------------------- | |
1845 // clone "n" for uses that are outside of loop | |
1846 void PhaseIdealLoop::clone_for_use_outside_loop( IdealLoopTree *loop, Node* n, Node_List& worklist ) { | |
1847 | |
1848 assert(worklist.size() == 0, "should be empty"); | |
1849 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { | |
1850 Node* use = n->fast_out(j); | |
1851 if( !loop->is_member(get_loop(has_ctrl(use) ? get_ctrl(use) : use)) ) { | |
1852 worklist.push(use); | |
1853 } | |
1854 } | |
1855 while( worklist.size() ) { | |
1856 Node *use = worklist.pop(); | |
1857 if (!has_node(use) || use->in(0) == C->top()) continue; | |
1858 uint j; | |
1859 for (j = 0; j < use->req(); j++) { | |
1860 if (use->in(j) == n) break; | |
1861 } | |
1862 assert(j < use->req(), "must be there"); | |
1863 | |
1864 // clone "n" and insert it between the inputs of "n" and the use outside the loop | |
1865 Node* n_clone = n->clone(); | |
1866 _igvn.hash_delete(use); | |
1867 use->set_req(j, n_clone); | |
1868 _igvn._worklist.push(use); | |
1869 if (!use->is_Phi()) { | |
1870 Node* use_c = has_ctrl(use) ? get_ctrl(use) : use->in(0); | |
1871 set_ctrl(n_clone, use_c); | |
1872 assert(!loop->is_member(get_loop(use_c)), "should be outside loop"); | |
1873 get_loop(use_c)->_body.push(n_clone); | |
1874 } else { | |
1875 // Use in a phi is considered a use in the associated predecessor block | |
1876 Node *prevbb = use->in(0)->in(j); | |
1877 set_ctrl(n_clone, prevbb); | |
1878 assert(!loop->is_member(get_loop(prevbb)), "should be outside loop"); | |
1879 get_loop(prevbb)->_body.push(n_clone); | |
1880 } | |
1881 _igvn.register_new_node_with_optimizer(n_clone); | |
1882 #if !defined(PRODUCT) | |
1883 if (TracePartialPeeling) { | |
1884 tty->print_cr("loop exit cloning old: %d new: %d newbb: %d", n->_idx, n_clone->_idx, get_ctrl(n_clone)->_idx); | |
1885 } | |
1886 #endif | |
1887 } | |
1888 } | |
1889 | |
1890 | |
1891 //------------------------------ clone_for_special_use_inside_loop ------------------------------------- | |
1892 // clone "n" for special uses that are in the not_peeled region. | |
1893 // If these def-uses occur in separate blocks, the code generator | |
1894 // marks the method as not compilable. For example, if a "BoolNode" | |
1895 // is in a different basic block than the "IfNode" that uses it, then | |
1896 // the compilation is aborted in the code generator. | |
1897 void PhaseIdealLoop::clone_for_special_use_inside_loop( IdealLoopTree *loop, Node* n, | |
1898 VectorSet& not_peel, Node_List& sink_list, Node_List& worklist ) { | |
1899 if (n->is_Phi() || n->is_Load()) { | |
1900 return; | |
1901 } | |
1902 assert(worklist.size() == 0, "should be empty"); | |
1903 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { | |
1904 Node* use = n->fast_out(j); | |
1905 if ( not_peel.test(use->_idx) && | |
1906 (use->is_If() || use->is_CMove() || use->is_Bool()) && | |
1907 use->in(1) == n) { | |
1908 worklist.push(use); | |
1909 } | |
1910 } | |
1911 if (worklist.size() > 0) { | |
1912 // clone "n" and insert it between inputs of "n" and the use | |
1913 Node* n_clone = n->clone(); | |
1914 loop->_body.push(n_clone); | |
1915 _igvn.register_new_node_with_optimizer(n_clone); | |
1916 set_ctrl(n_clone, get_ctrl(n)); | |
1917 sink_list.push(n_clone); | |
1918 not_peel <<= n_clone->_idx; // add n_clone to not_peel set. | |
1919 #if !defined(PRODUCT) | |
1920 if (TracePartialPeeling) { | |
1921 tty->print_cr("special not_peeled cloning old: %d new: %d", n->_idx, n_clone->_idx); | |
1922 } | |
1923 #endif | |
1924 while( worklist.size() ) { | |
1925 Node *use = worklist.pop(); | |
1926 _igvn.hash_delete(use); | |
1927 _igvn._worklist.push(use); | |
1928 for (uint j = 1; j < use->req(); j++) { | |
1929 if (use->in(j) == n) { | |
1930 use->set_req(j, n_clone); | |
1931 } | |
1932 } | |
1933 } | |
1934 } | |
1935 } | |
1936 | |
1937 | |
1938 //------------------------------ insert_phi_for_loop ------------------------------------- | |
1939 // Insert phi(lp_entry_val, back_edge_val) at use->in(idx) for loop lp if phi does not already exist | |
1940 void PhaseIdealLoop::insert_phi_for_loop( Node* use, uint idx, Node* lp_entry_val, Node* back_edge_val, LoopNode* lp ) { | |
1941 Node *phi = PhiNode::make(lp, back_edge_val); | |
1942 phi->set_req(LoopNode::EntryControl, lp_entry_val); | |
1943 // Use existing phi if it already exists | |
1944 Node *hit = _igvn.hash_find_insert(phi); | |
1945 if( hit == NULL ) { | |
1946 _igvn.register_new_node_with_optimizer(phi); | |
1947 set_ctrl(phi, lp); | |
1948 } else { | |
1949 // Remove the new phi from the graph and use the hit | |
1950 _igvn.remove_dead_node(phi); | |
1951 phi = hit; | |
1952 } | |
1953 _igvn.hash_delete(use); | |
1954 _igvn._worklist.push(use); | |
1955 use->set_req(idx, phi); | |
1956 } | |
1957 | |
1958 #ifdef ASSERT | |
1959 //------------------------------ is_valid_loop_partition ------------------------------------- | |
1960 // Validate the loop partition sets: peel and not_peel | |
1961 bool PhaseIdealLoop::is_valid_loop_partition( IdealLoopTree *loop, VectorSet& peel, Node_List& peel_list, | |
1962 VectorSet& not_peel ) { | |
1963 uint i; | |
1964 // Check that peel_list entries are in the peel set | |
1965 for (i = 0; i < peel_list.size(); i++) { | |
1966 if (!peel.test(peel_list.at(i)->_idx)) { | |
1967 return false; | |
1968 } | |
1969 } | |
1970 // Check at loop members are in one of peel set or not_peel set | |
1971 for (i = 0; i < loop->_body.size(); i++ ) { | |
1972 Node *def = loop->_body.at(i); | |
1973 uint di = def->_idx; | |
1974 // Check that peel set elements are in peel_list | |
1975 if (peel.test(di)) { | |
1976 if (not_peel.test(di)) { | |
1977 return false; | |
1978 } | |
1979 // Must be in peel_list also | |
1980 bool found = false; | |
1981 for (uint j = 0; j < peel_list.size(); j++) { | |
1982 if (peel_list.at(j)->_idx == di) { | |
1983 found = true; | |
1984 break; | |
1985 } | |
1986 } | |
1987 if (!found) { | |
1988 return false; | |
1989 } | |
1990 } else if (not_peel.test(di)) { | |
1991 if (peel.test(di)) { | |
1992 return false; | |
1993 } | |
1994 } else { | |
1995 return false; | |
1996 } | |
1997 } | |
1998 return true; | |
1999 } | |
2000 | |
2001 //------------------------------ is_valid_clone_loop_exit_use ------------------------------------- | |
2002 // Ensure a use outside of loop is of the right form | |
2003 bool PhaseIdealLoop::is_valid_clone_loop_exit_use( IdealLoopTree *loop, Node* use, uint exit_idx) { | |
2004 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use; | |
2005 return (use->is_Phi() && | |
2006 use_c->is_Region() && use_c->req() == 3 && | |
2007 (use_c->in(exit_idx)->Opcode() == Op_IfTrue || | |
2008 use_c->in(exit_idx)->Opcode() == Op_IfFalse || | |
2009 use_c->in(exit_idx)->Opcode() == Op_JumpProj) && | |
2010 loop->is_member( get_loop( use_c->in(exit_idx)->in(0) ) ) ); | |
2011 } | |
2012 | |
2013 //------------------------------ is_valid_clone_loop_form ------------------------------------- | |
2014 // Ensure that all uses outside of loop are of the right form | |
2015 bool PhaseIdealLoop::is_valid_clone_loop_form( IdealLoopTree *loop, Node_List& peel_list, | |
2016 uint orig_exit_idx, uint clone_exit_idx) { | |
2017 uint len = peel_list.size(); | |
2018 for (uint i = 0; i < len; i++) { | |
2019 Node *def = peel_list.at(i); | |
2020 | |
2021 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) { | |
2022 Node *use = def->fast_out(j); | |
2023 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use; | |
2024 if (!loop->is_member(get_loop(use_c))) { | |
2025 // use is not in the loop, check for correct structure | |
2026 if (use->in(0) == def) { | |
2027 // Okay | |
2028 } else if (!is_valid_clone_loop_exit_use(loop, use, orig_exit_idx)) { | |
2029 return false; | |
2030 } | |
2031 } | |
2032 } | |
2033 } | |
2034 return true; | |
2035 } | |
2036 #endif | |
2037 | |
2038 //------------------------------ partial_peel ------------------------------------- | |
2039 // Partially peel (aka loop rotation) the top portion of a loop (called | |
2040 // the peel section below) by cloning it and placing one copy just before | |
2041 // the new loop head and the other copy at the bottom of the new loop. | |
2042 // | |
2043 // before after where it came from | |
2044 // | |
2045 // stmt1 stmt1 | |
2046 // loop: stmt2 clone | |
2047 // stmt2 if condA goto exitA clone | |
2048 // if condA goto exitA new_loop: new | |
2049 // stmt3 stmt3 clone | |
2050 // if !condB goto loop if condB goto exitB clone | |
2051 // exitB: stmt2 orig | |
2052 // stmt4 if !condA goto new_loop orig | |
2053 // exitA: goto exitA | |
2054 // exitB: | |
2055 // stmt4 | |
2056 // exitA: | |
2057 // | |
2058 // Step 1: find the cut point: an exit test on probable | |
2059 // induction variable. | |
2060 // Step 2: schedule (with cloning) operations in the peel | |
2061 // section that can be executed after the cut into | |
2062 // the section that is not peeled. This may need | |
2063 // to clone operations into exit blocks. For | |
2064 // instance, a reference to A[i] in the not-peel | |
2065 // section and a reference to B[i] in an exit block | |
2066 // may cause a left-shift of i by 2 to be placed | |
2067 // in the peel block. This step will clone the left | |
2068 // shift into the exit block and sink the left shift | |
2069 // from the peel to the not-peel section. | |
2070 // Step 3: clone the loop, retarget the control, and insert | |
2071 // phis for values that are live across the new loop | |
2072 // head. This is very dependent on the graph structure | |
2073 // from clone_loop. It creates region nodes for | |
2074 // exit control and associated phi nodes for values | |
2075 // flow out of the loop through that exit. The region | |
2076 // node is dominated by the clone's control projection. | |
2077 // So the clone's peel section is placed before the | |
2078 // new loop head, and the clone's not-peel section is | |
2079 // forms the top part of the new loop. The original | |
2080 // peel section forms the tail of the new loop. | |
2081 // Step 4: update the dominator tree and recompute the | |
2082 // dominator depth. | |
2083 // | |
2084 // orig | |
2085 // | |
2086 // stmt1 | |
2087 // | | |
2088 // v | |
2089 // loop<----+ | |
2090 // | | | |
2091 // stmt2 | | |
2092 // | | | |
2093 // v | | |
2094 // ifA | | |
2095 // / | | | |
2096 // v v | | |
2097 // false true ^ <-- last_peel | |
2098 // / | | | |
2099 // / ===|==cut | | |
2100 // / stmt3 | <-- first_not_peel | |
2101 // / | | | |
2102 // | v | | |
2103 // v ifB | | |
2104 // exitA: / \ | | |
2105 // / \ | | |
2106 // v v | | |
2107 // false true | | |
2108 // / \ | | |
2109 // / ----+ | |
2110 // | | |
2111 // v | |
2112 // exitB: | |
2113 // stmt4 | |
2114 // | |
2115 // | |
2116 // after clone loop | |
2117 // | |
2118 // stmt1 | |
2119 // / \ | |
2120 // clone / \ orig | |
2121 // / \ | |
2122 // / \ | |
2123 // v v | |
2124 // +---->loop loop<----+ | |
2125 // | | | | | |
2126 // | stmt2 stmt2 | | |
2127 // | | | | | |
2128 // | v v | | |
2129 // | ifA ifA | | |
2130 // | | \ / | | | |
2131 // | v v v v | | |
2132 // ^ true false false true ^ <-- last_peel | |
2133 // | | ^ \ / | | | |
2134 // | cut==|== \ \ / ===|==cut | | |
2135 // | stmt3 \ \ / stmt3 | <-- first_not_peel | |
2136 // | | dom | | | | | |
2137 // | v \ 1v v2 v | | |
2138 // | ifB regionA ifB | | |
2139 // | / \ | / \ | | |
2140 // | / \ v / \ | | |
2141 // | v v exitA: v v | | |
2142 // | true false false true | | |
2143 // | / ^ \ / \ | | |
2144 // +---- \ \ / ----+ | |
2145 // dom \ / | |
2146 // \ 1v v2 | |
2147 // regionB | |
2148 // | | |
2149 // v | |
2150 // exitB: | |
2151 // stmt4 | |
2152 // | |
2153 // | |
2154 // after partial peel | |
2155 // | |
2156 // stmt1 | |
2157 // / | |
2158 // clone / orig | |
2159 // / TOP | |
2160 // / \ | |
2161 // v v | |
2162 // TOP->region region----+ | |
2163 // | | | | |
2164 // stmt2 stmt2 | | |
2165 // | | | | |
2166 // v v | | |
2167 // ifA ifA | | |
2168 // | \ / | | | |
2169 // v v v v | | |
2170 // true false false true | <-- last_peel | |
2171 // | ^ \ / +------|---+ | |
2172 // +->newloop \ \ / === ==cut | | | |
2173 // | stmt3 \ \ / TOP | | | |
2174 // | | dom | | stmt3 | | <-- first_not_peel | |
2175 // | v \ 1v v2 v | | | |
2176 // | ifB regionA ifB ^ v | |
2177 // | / \ | / \ | | | |
2178 // | / \ v / \ | | | |
2179 // | v v exitA: v v | | | |
2180 // | true false false true | | | |
2181 // | / ^ \ / \ | | | |
2182 // | | \ \ / v | | | |
2183 // | | dom \ / TOP | | | |
2184 // | | \ 1v v2 | | | |
2185 // ^ v regionB | | | |
2186 // | | | | | | |
2187 // | | v ^ v | |
2188 // | | exitB: | | | |
2189 // | | stmt4 | | | |
2190 // | +------------>-----------------+ | | |
2191 // | | | |
2192 // +-----------------<---------------------+ | |
2193 // | |
2194 // | |
2195 // final graph | |
2196 // | |
2197 // stmt1 | |
2198 // | | |
2199 // v | |
2200 // ........> ifA clone | |
2201 // : / | | |
2202 // dom / | | |
2203 // : v v | |
2204 // : false true | |
2205 // : | | | |
2206 // : | stmt2 clone | |
2207 // : | | | |
2208 // : | v | |
2209 // : | newloop<-----+ | |
2210 // : | | | | |
2211 // : | stmt3 clone | | |
2212 // : | | | | |
2213 // : | v | | |
2214 // : | ifB | | |
2215 // : | / \ | | |
2216 // : | v v | | |
2217 // : | false true | | |
2218 // : | | | | | |
2219 // : | v stmt2 | | |
2220 // : | exitB: | | | |
2221 // : | stmt4 v | | |
2222 // : | ifA orig | | |
2223 // : | / \ | | |
2224 // : | / \ | | |
2225 // : | v v | | |
2226 // : | false true | | |
2227 // : | / \ | | |
2228 // : v v -----+ | |
2229 // RegionA | |
2230 // | | |
2231 // v | |
2232 // exitA | |
2233 // | |
2234 bool PhaseIdealLoop::partial_peel( IdealLoopTree *loop, Node_List &old_new ) { | |
2235 | |
2236 LoopNode *head = loop->_head->as_Loop(); | |
2237 | |
2238 if (head->is_partial_peel_loop() || head->partial_peel_has_failed()) { | |
2239 return false; | |
2240 } | |
2241 | |
2242 // Check for complex exit control | |
2243 for(uint ii = 0; ii < loop->_body.size(); ii++ ) { | |
2244 Node *n = loop->_body.at(ii); | |
2245 int opc = n->Opcode(); | |
2246 if (n->is_Call() || | |
2247 opc == Op_Catch || | |
2248 opc == Op_CatchProj || | |
2249 opc == Op_Jump || | |
2250 opc == Op_JumpProj) { | |
2251 #if !defined(PRODUCT) | |
2252 if (TracePartialPeeling) { | |
2253 tty->print_cr("\nExit control too complex: lp: %d", head->_idx); | |
2254 } | |
2255 #endif | |
2256 return false; | |
2257 } | |
2258 } | |
2259 | |
2260 int dd = dom_depth(head); | |
2261 | |
2262 // Step 1: find cut point | |
2263 | |
2264 // Walk up dominators to loop head looking for first loop exit | |
2265 // which is executed on every path thru loop. | |
2266 IfNode *peel_if = NULL; | |
2267 IfNode *peel_if_cmpu = NULL; | |
2268 | |
2269 Node *iff = loop->tail(); | |
2270 while( iff != head ) { | |
2271 if( iff->is_If() ) { | |
2272 Node *ctrl = get_ctrl(iff->in(1)); | |
2273 if (ctrl->is_top()) return false; // Dead test on live IF. | |
2274 // If loop-varying exit-test, check for induction variable | |
2275 if( loop->is_member(get_loop(ctrl)) && | |
2276 loop->is_loop_exit(iff) && | |
2277 is_possible_iv_test(iff)) { | |
2278 Node* cmp = iff->in(1)->in(1); | |
2279 if (cmp->Opcode() == Op_CmpI) { | |
2280 peel_if = iff->as_If(); | |
2281 } else { | |
2282 assert(cmp->Opcode() == Op_CmpU, "must be CmpI or CmpU"); | |
2283 peel_if_cmpu = iff->as_If(); | |
2284 } | |
2285 } | |
2286 } | |
2287 iff = idom(iff); | |
2288 } | |
2289 // Prefer signed compare over unsigned compare. | |
2290 IfNode* new_peel_if = NULL; | |
2291 if (peel_if == NULL) { | |
2292 if (!PartialPeelAtUnsignedTests || peel_if_cmpu == NULL) { | |
2293 return false; // No peel point found | |
2294 } | |
2295 new_peel_if = insert_cmpi_loop_exit(peel_if_cmpu, loop); | |
2296 if (new_peel_if == NULL) { | |
2297 return false; // No peel point found | |
2298 } | |
2299 peel_if = new_peel_if; | |
2300 } | |
2301 Node* last_peel = stay_in_loop(peel_if, loop); | |
2302 Node* first_not_peeled = stay_in_loop(last_peel, loop); | |
2303 if (first_not_peeled == NULL || first_not_peeled == head) { | |
2304 return false; | |
2305 } | |
2306 | |
2307 #if !defined(PRODUCT) | |
2308 if (TracePartialPeeling) { | |
2309 tty->print_cr("before partial peel one iteration"); | |
2310 Node_List wl; | |
2311 Node* t = head->in(2); | |
2312 while (true) { | |
2313 wl.push(t); | |
2314 if (t == head) break; | |
2315 t = idom(t); | |
2316 } | |
2317 while (wl.size() > 0) { | |
2318 Node* tt = wl.pop(); | |
2319 tt->dump(); | |
2320 if (tt == last_peel) tty->print_cr("-- cut --"); | |
2321 } | |
2322 } | |
2323 #endif | |
2324 ResourceArea *area = Thread::current()->resource_area(); | |
2325 VectorSet peel(area); | |
2326 VectorSet not_peel(area); | |
2327 Node_List peel_list(area); | |
2328 Node_List worklist(area); | |
2329 Node_List sink_list(area); | |
2330 | |
2331 // Set of cfg nodes to peel are those that are executable from | |
2332 // the head through last_peel. | |
2333 assert(worklist.size() == 0, "should be empty"); | |
2334 worklist.push(head); | |
2335 peel.set(head->_idx); | |
2336 while (worklist.size() > 0) { | |
2337 Node *n = worklist.pop(); | |
2338 if (n != last_peel) { | |
2339 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) { | |
2340 Node* use = n->fast_out(j); | |
2341 if (use->is_CFG() && | |
2342 loop->is_member(get_loop(use)) && | |
2343 !peel.test_set(use->_idx)) { | |
2344 worklist.push(use); | |
2345 } | |
2346 } | |
2347 } | |
2348 } | |
2349 | |
2350 // Set of non-cfg nodes to peel are those that are control | |
2351 // dependent on the cfg nodes. | |
2352 uint i; | |
2353 for(i = 0; i < loop->_body.size(); i++ ) { | |
2354 Node *n = loop->_body.at(i); | |
2355 Node *n_c = has_ctrl(n) ? get_ctrl(n) : n; | |
2356 if (peel.test(n_c->_idx)) { | |
2357 peel.set(n->_idx); | |
2358 } else { | |
2359 not_peel.set(n->_idx); | |
2360 } | |
2361 } | |
2362 | |
2363 // Step 2: move operations from the peeled section down into the | |
2364 // not-peeled section | |
2365 | |
2366 // Get a post order schedule of nodes in the peel region | |
2367 // Result in right-most operand. | |
2368 scheduled_nodelist(loop, peel, peel_list ); | |
2369 | |
2370 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition"); | |
2371 | |
2372 // For future check for too many new phis | |
2373 uint old_phi_cnt = 0; | |
2374 for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) { | |
2375 Node* use = head->fast_out(j); | |
2376 if (use->is_Phi()) old_phi_cnt++; | |
2377 } | |
2378 | |
2379 #if !defined(PRODUCT) | |
2380 if (TracePartialPeeling) { | |
2381 tty->print_cr("\npeeled list"); | |
2382 } | |
2383 #endif | |
2384 | |
2385 // Evacuate nodes in peel region into the not_peeled region if possible | |
2386 uint new_phi_cnt = 0; | |
2387 for (i = 0; i < peel_list.size();) { | |
2388 Node* n = peel_list.at(i); | |
2389 #if !defined(PRODUCT) | |
2390 if (TracePartialPeeling) n->dump(); | |
2391 #endif | |
2392 bool incr = true; | |
2393 if ( !n->is_CFG() ) { | |
2394 | |
2395 if ( has_use_in_set(n, not_peel) ) { | |
2396 | |
2397 // If not used internal to the peeled region, | |
2398 // move "n" from peeled to not_peeled region. | |
2399 | |
2400 if ( !has_use_internal_to_set(n, peel, loop) ) { | |
2401 | |
2402 // if not pinned and not a load (which maybe anti-dependent on a store) | |
2403 // and not a CMove (Matcher expects only bool->cmove). | |
2404 if ( n->in(0) == NULL && !n->is_Load() && !n->is_CMove() ) { | |
2405 clone_for_use_outside_loop( loop, n, worklist ); | |
2406 | |
2407 sink_list.push(n); | |
2408 peel >>= n->_idx; // delete n from peel set. | |
2409 not_peel <<= n->_idx; // add n to not_peel set. | |
2410 peel_list.remove(i); | |
2411 incr = false; | |
2412 #if !defined(PRODUCT) | |
2413 if (TracePartialPeeling) { | |
2414 tty->print_cr("sink to not_peeled region: %d newbb: %d", | |
2415 n->_idx, get_ctrl(n)->_idx); | |
2416 } | |
2417 #endif | |
2418 } | |
2419 } else { | |
2420 // Otherwise check for special def-use cases that span | |
2421 // the peel/not_peel boundary such as bool->if | |
2422 clone_for_special_use_inside_loop( loop, n, not_peel, sink_list, worklist ); | |
2423 new_phi_cnt++; | |
2424 } | |
2425 } | |
2426 } | |
2427 if (incr) i++; | |
2428 } | |
2429 | |
2430 if (new_phi_cnt > old_phi_cnt + PartialPeelNewPhiDelta) { | |
2431 #if !defined(PRODUCT) | |
2432 if (TracePartialPeeling) { | |
2433 tty->print_cr("\nToo many new phis: %d old %d new cmpi: %c", | |
2434 new_phi_cnt, old_phi_cnt, new_peel_if != NULL?'T':'F'); | |
2435 } | |
2436 #endif | |
2437 if (new_peel_if != NULL) { | |
2438 remove_cmpi_loop_exit(new_peel_if, loop); | |
2439 } | |
2440 // Inhibit more partial peeling on this loop | |
2441 assert(!head->is_partial_peel_loop(), "not partial peeled"); | |
2442 head->mark_partial_peel_failed(); | |
2443 return false; | |
2444 } | |
2445 | |
2446 // Step 3: clone loop, retarget control, and insert new phis | |
2447 | |
2448 // Create new loop head for new phis and to hang | |
2449 // the nodes being moved (sinked) from the peel region. | |
2450 LoopNode* new_head = new (C, 3) LoopNode(last_peel, last_peel); | |
2451 _igvn.register_new_node_with_optimizer(new_head); | |
2452 assert(first_not_peeled->in(0) == last_peel, "last_peel <- first_not_peeled"); | |
2453 first_not_peeled->set_req(0, new_head); | |
2454 set_loop(new_head, loop); | |
2455 loop->_body.push(new_head); | |
2456 not_peel.set(new_head->_idx); | |
2457 set_idom(new_head, last_peel, dom_depth(first_not_peeled)); | |
2458 set_idom(first_not_peeled, new_head, dom_depth(first_not_peeled)); | |
2459 | |
2460 while (sink_list.size() > 0) { | |
2461 Node* n = sink_list.pop(); | |
2462 set_ctrl(n, new_head); | |
2463 } | |
2464 | |
2465 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition"); | |
2466 | |
2467 clone_loop( loop, old_new, dd ); | |
2468 | |
2469 const uint clone_exit_idx = 1; | |
2470 const uint orig_exit_idx = 2; | |
2471 assert(is_valid_clone_loop_form( loop, peel_list, orig_exit_idx, clone_exit_idx ), "bad clone loop"); | |
2472 | |
2473 Node* head_clone = old_new[head->_idx]; | |
2474 LoopNode* new_head_clone = old_new[new_head->_idx]->as_Loop(); | |
2475 Node* orig_tail_clone = head_clone->in(2); | |
2476 | |
2477 // Add phi if "def" node is in peel set and "use" is not | |
2478 | |
2479 for(i = 0; i < peel_list.size(); i++ ) { | |
2480 Node *def = peel_list.at(i); | |
2481 if (!def->is_CFG()) { | |
2482 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) { | |
2483 Node *use = def->fast_out(j); | |
2484 if (has_node(use) && use->in(0) != C->top() && | |
2485 (!peel.test(use->_idx) || | |
2486 (use->is_Phi() && use->in(0) == head)) ) { | |
2487 worklist.push(use); | |
2488 } | |
2489 } | |
2490 while( worklist.size() ) { | |
2491 Node *use = worklist.pop(); | |
2492 for (uint j = 1; j < use->req(); j++) { | |
2493 Node* n = use->in(j); | |
2494 if (n == def) { | |
2495 | |
2496 // "def" is in peel set, "use" is not in peel set | |
2497 // or "use" is in the entry boundary (a phi) of the peel set | |
2498 | |
2499 Node* use_c = has_ctrl(use) ? get_ctrl(use) : use; | |
2500 | |
2501 if ( loop->is_member(get_loop( use_c )) ) { | |
2502 // use is in loop | |
2503 if (old_new[use->_idx] != NULL) { // null for dead code | |
2504 Node* use_clone = old_new[use->_idx]; | |
2505 _igvn.hash_delete(use); | |
2506 use->set_req(j, C->top()); | |
2507 _igvn._worklist.push(use); | |
2508 insert_phi_for_loop( use_clone, j, old_new[def->_idx], def, new_head_clone ); | |
2509 } | |
2510 } else { | |
2511 assert(is_valid_clone_loop_exit_use(loop, use, orig_exit_idx), "clone loop format"); | |
2512 // use is not in the loop, check if the live range includes the cut | |
2513 Node* lp_if = use_c->in(orig_exit_idx)->in(0); | |
2514 if (not_peel.test(lp_if->_idx)) { | |
2515 assert(j == orig_exit_idx, "use from original loop"); | |
2516 insert_phi_for_loop( use, clone_exit_idx, old_new[def->_idx], def, new_head_clone ); | |
2517 } | |
2518 } | |
2519 } | |
2520 } | |
2521 } | |
2522 } | |
2523 } | |
2524 | |
2525 // Step 3b: retarget control | |
2526 | |
2527 // Redirect control to the new loop head if a cloned node in | |
2528 // the not_peeled region has control that points into the peeled region. | |
2529 // This necessary because the cloned peeled region will be outside | |
2530 // the loop. | |
2531 // from to | |
2532 // cloned-peeled <---+ | |
2533 // new_head_clone: | <--+ | |
2534 // cloned-not_peeled in(0) in(0) | |
2535 // orig-peeled | |
2536 | |
2537 for(i = 0; i < loop->_body.size(); i++ ) { | |
2538 Node *n = loop->_body.at(i); | |
2539 if (!n->is_CFG() && n->in(0) != NULL && | |
2540 not_peel.test(n->_idx) && peel.test(n->in(0)->_idx)) { | |
2541 Node* n_clone = old_new[n->_idx]; | |
2542 _igvn.hash_delete(n_clone); | |
2543 n_clone->set_req(0, new_head_clone); | |
2544 _igvn._worklist.push(n_clone); | |
2545 } | |
2546 } | |
2547 | |
2548 // Backedge of the surviving new_head (the clone) is original last_peel | |
2549 _igvn.hash_delete(new_head_clone); | |
2550 new_head_clone->set_req(LoopNode::LoopBackControl, last_peel); | |
2551 _igvn._worklist.push(new_head_clone); | |
2552 | |
2553 // Cut first node in original not_peel set | |
2554 _igvn.hash_delete(new_head); | |
2555 new_head->set_req(LoopNode::EntryControl, C->top()); | |
2556 new_head->set_req(LoopNode::LoopBackControl, C->top()); | |
2557 _igvn._worklist.push(new_head); | |
2558 | |
2559 // Copy head_clone back-branch info to original head | |
2560 // and remove original head's loop entry and | |
2561 // clone head's back-branch | |
2562 _igvn.hash_delete(head); | |
2563 _igvn.hash_delete(head_clone); | |
2564 head->set_req(LoopNode::EntryControl, head_clone->in(LoopNode::LoopBackControl)); | |
2565 head->set_req(LoopNode::LoopBackControl, C->top()); | |
2566 head_clone->set_req(LoopNode::LoopBackControl, C->top()); | |
2567 _igvn._worklist.push(head); | |
2568 _igvn._worklist.push(head_clone); | |
2569 | |
2570 // Similarly modify the phis | |
2571 for (DUIterator_Fast kmax, k = head->fast_outs(kmax); k < kmax; k++) { | |
2572 Node* use = head->fast_out(k); | |
2573 if (use->is_Phi() && use->outcnt() > 0) { | |
2574 Node* use_clone = old_new[use->_idx]; | |
2575 _igvn.hash_delete(use); | |
2576 _igvn.hash_delete(use_clone); | |
2577 use->set_req(LoopNode::EntryControl, use_clone->in(LoopNode::LoopBackControl)); | |
2578 use->set_req(LoopNode::LoopBackControl, C->top()); | |
2579 use_clone->set_req(LoopNode::LoopBackControl, C->top()); | |
2580 _igvn._worklist.push(use); | |
2581 _igvn._worklist.push(use_clone); | |
2582 } | |
2583 } | |
2584 | |
2585 // Step 4: update dominator tree and dominator depth | |
2586 | |
2587 set_idom(head, orig_tail_clone, dd); | |
2588 recompute_dom_depth(); | |
2589 | |
2590 // Inhibit more partial peeling on this loop | |
2591 new_head_clone->set_partial_peel_loop(); | |
2592 C->set_major_progress(); | |
2593 | |
2594 #if !defined(PRODUCT) | |
2595 if (TracePartialPeeling) { | |
2596 tty->print_cr("\nafter partial peel one iteration"); | |
2597 Node_List wl(area); | |
2598 Node* t = last_peel; | |
2599 while (true) { | |
2600 wl.push(t); | |
2601 if (t == head_clone) break; | |
2602 t = idom(t); | |
2603 } | |
2604 while (wl.size() > 0) { | |
2605 Node* tt = wl.pop(); | |
2606 if (tt == head) tty->print_cr("orig head"); | |
2607 else if (tt == new_head_clone) tty->print_cr("new head"); | |
2608 else if (tt == head_clone) tty->print_cr("clone head"); | |
2609 tt->dump(); | |
2610 } | |
2611 } | |
2612 #endif | |
2613 return true; | |
2614 } | |
2615 | |
2616 //------------------------------reorg_offsets---------------------------------- | |
2617 // Reorganize offset computations to lower register pressure. Mostly | |
2618 // prevent loop-fallout uses of the pre-incremented trip counter (which are | |
2619 // then alive with the post-incremented trip counter forcing an extra | |
2620 // register move) | |
2621 void PhaseIdealLoop::reorg_offsets( IdealLoopTree *loop ) { | |
2622 | |
2623 CountedLoopNode *cl = loop->_head->as_CountedLoop(); | |
2624 CountedLoopEndNode *cle = cl->loopexit(); | |
2625 if( !cle ) return; // The occasional dead loop | |
2626 // Find loop exit control | |
2627 Node *exit = cle->proj_out(false); | |
2628 assert( exit->Opcode() == Op_IfFalse, "" ); | |
2629 | |
2630 // Check for the special case of folks using the pre-incremented | |
2631 // trip-counter on the fall-out path (forces the pre-incremented | |
2632 // and post-incremented trip counter to be live at the same time). | |
2633 // Fix this by adjusting to use the post-increment trip counter. | |
2634 Node *phi = cl->phi(); | |
2635 if( !phi ) return; // Dead infinite loop | |
2636 bool progress = true; | |
2637 while (progress) { | |
2638 progress = false; | |
2639 for (DUIterator_Fast imax, i = phi->fast_outs(imax); i < imax; i++) { | |
2640 Node* use = phi->fast_out(i); // User of trip-counter | |
2641 if (!has_ctrl(use)) continue; | |
2642 Node *u_ctrl = get_ctrl(use); | |
2643 if( use->is_Phi() ) { | |
2644 u_ctrl = NULL; | |
2645 for( uint j = 1; j < use->req(); j++ ) | |
2646 if( use->in(j) == phi ) | |
2647 u_ctrl = dom_lca( u_ctrl, use->in(0)->in(j) ); | |
2648 } | |
2649 IdealLoopTree *u_loop = get_loop(u_ctrl); | |
2650 // Look for loop-invariant use | |
2651 if( u_loop == loop ) continue; | |
2652 if( loop->is_member( u_loop ) ) continue; | |
2653 // Check that use is live out the bottom. Assuming the trip-counter | |
2654 // update is right at the bottom, uses of of the loop middle are ok. | |
2655 if( dom_lca( exit, u_ctrl ) != exit ) continue; | |
2656 // protect against stride not being a constant | |
2657 if( !cle->stride_is_con() ) continue; | |
2658 // Hit! Refactor use to use the post-incremented tripcounter. | |
2659 // Compute a post-increment tripcounter. | |
2660 Node *opaq = new (C, 2) Opaque2Node( cle->incr() ); | |
2661 register_new_node( opaq, u_ctrl ); | |
2662 Node *neg_stride = _igvn.intcon(-cle->stride_con()); | |
2663 set_ctrl(neg_stride, C->root()); | |
2664 Node *post = new (C, 3) AddINode( opaq, neg_stride); | |
2665 register_new_node( post, u_ctrl ); | |
2666 _igvn.hash_delete(use); | |
2667 _igvn._worklist.push(use); | |
2668 for( uint j = 1; j < use->req(); j++ ) | |
2669 if( use->in(j) == phi ) | |
2670 use->set_req(j, post); | |
2671 // Since DU info changed, rerun loop | |
2672 progress = true; | |
2673 break; | |
2674 } | |
2675 } | |
2676 | |
2677 } |