comparison src/share/vm/opto/loopnode.cpp @ 2383:9dc311b8473e

7008866: Missing loop predicate for loop with multiple entries Summary: Add predicates when loop head bytecode is parsed instead of when back branch bytecode is parsed. Reviewed-by: never
author kvn
date Mon, 21 Mar 2011 11:28:14 -0700
parents 194c9fdee631
children 1927db75dd85
comparison
equal deleted inserted replaced
2382:3ef1a1866a60 2383:9dc311b8473e
54 //============================================================================= 54 //=============================================================================
55 //------------------------------dump_spec-------------------------------------- 55 //------------------------------dump_spec--------------------------------------
56 // Dump special per-node info 56 // Dump special per-node info
57 #ifndef PRODUCT 57 #ifndef PRODUCT
58 void LoopNode::dump_spec(outputStream *st) const { 58 void LoopNode::dump_spec(outputStream *st) const {
59 if( is_inner_loop () ) st->print( "inner " ); 59 if (is_inner_loop()) st->print( "inner " );
60 if( is_partial_peel_loop () ) st->print( "partial_peel " ); 60 if (is_partial_peel_loop()) st->print( "partial_peel " );
61 if( partial_peel_has_failed () ) st->print( "partial_peel_failed " ); 61 if (partial_peel_has_failed()) st->print( "partial_peel_failed " );
62 } 62 }
63 #endif 63 #endif
64
65 //------------------------------is_valid_counted_loop-------------------------
66 bool LoopNode::is_valid_counted_loop() const {
67 if (is_CountedLoop()) {
68 CountedLoopNode* l = as_CountedLoop();
69 CountedLoopEndNode* le = l->loopexit();
70 if (le != NULL &&
71 le->proj_out(1 /* true */) == l->in(LoopNode::LoopBackControl)) {
72 Node* phi = l->phi();
73 Node* exit = le->proj_out(0 /* false */);
74 if (exit != NULL && exit->Opcode() == Op_IfFalse &&
75 phi != NULL && phi->is_Phi() &&
76 phi->in(LoopNode::LoopBackControl) == l->incr() &&
77 le->loopnode() == l && le->stride_is_con()) {
78 return true;
79 }
80 }
81 }
82 return false;
83 }
64 84
65 //------------------------------get_early_ctrl--------------------------------- 85 //------------------------------get_early_ctrl---------------------------------
66 // Compute earliest legal control 86 // Compute earliest legal control
67 Node *PhaseIdealLoop::get_early_ctrl( Node *n ) { 87 Node *PhaseIdealLoop::get_early_ctrl( Node *n ) {
68 assert( !n->is_Phi() && !n->is_CFG(), "this code only handles data nodes" ); 88 assert( !n->is_Phi() && !n->is_CFG(), "this code only handles data nodes" );
140 // Fixup self 160 // Fixup self
141 set_early_ctrl( n ); 161 set_early_ctrl( n );
142 } 162 }
143 163
144 //------------------------------is_counted_loop-------------------------------- 164 //------------------------------is_counted_loop--------------------------------
145 Node *PhaseIdealLoop::is_counted_loop( Node *x, IdealLoopTree *loop ) { 165 bool PhaseIdealLoop::is_counted_loop( Node *x, IdealLoopTree *loop ) {
146 PhaseGVN *gvn = &_igvn; 166 PhaseGVN *gvn = &_igvn;
147 167
148 // Counted loop head must be a good RegionNode with only 3 not NULL 168 // Counted loop head must be a good RegionNode with only 3 not NULL
149 // control input edges: Self, Entry, LoopBack. 169 // control input edges: Self, Entry, LoopBack.
150 if ( x->in(LoopNode::Self) == NULL || x->req() != 3 ) 170 if (x->in(LoopNode::Self) == NULL || x->req() != 3)
151 return NULL; 171 return false;
152 172
153 Node *init_control = x->in(LoopNode::EntryControl); 173 Node *init_control = x->in(LoopNode::EntryControl);
154 Node *back_control = x->in(LoopNode::LoopBackControl); 174 Node *back_control = x->in(LoopNode::LoopBackControl);
155 if( init_control == NULL || back_control == NULL ) // Partially dead 175 if (init_control == NULL || back_control == NULL) // Partially dead
156 return NULL; 176 return false;
157 // Must also check for TOP when looking for a dead loop 177 // Must also check for TOP when looking for a dead loop
158 if( init_control->is_top() || back_control->is_top() ) 178 if (init_control->is_top() || back_control->is_top())
159 return NULL; 179 return false;
160 180
161 // Allow funny placement of Safepoint 181 // Allow funny placement of Safepoint
162 if( back_control->Opcode() == Op_SafePoint ) 182 if (back_control->Opcode() == Op_SafePoint)
163 back_control = back_control->in(TypeFunc::Control); 183 back_control = back_control->in(TypeFunc::Control);
164 184
165 // Controlling test for loop 185 // Controlling test for loop
166 Node *iftrue = back_control; 186 Node *iftrue = back_control;
167 uint iftrue_op = iftrue->Opcode(); 187 uint iftrue_op = iftrue->Opcode();
168 if( iftrue_op != Op_IfTrue && 188 if (iftrue_op != Op_IfTrue &&
169 iftrue_op != Op_IfFalse ) 189 iftrue_op != Op_IfFalse)
170 // I have a weird back-control. Probably the loop-exit test is in 190 // I have a weird back-control. Probably the loop-exit test is in
171 // the middle of the loop and I am looking at some trailing control-flow 191 // the middle of the loop and I am looking at some trailing control-flow
172 // merge point. To fix this I would have to partially peel the loop. 192 // merge point. To fix this I would have to partially peel the loop.
173 return NULL; // Obscure back-control 193 return false; // Obscure back-control
174 194
175 // Get boolean guarding loop-back test 195 // Get boolean guarding loop-back test
176 Node *iff = iftrue->in(0); 196 Node *iff = iftrue->in(0);
177 if( get_loop(iff) != loop || !iff->in(1)->is_Bool() ) return NULL; 197 if (get_loop(iff) != loop || !iff->in(1)->is_Bool())
198 return false;
178 BoolNode *test = iff->in(1)->as_Bool(); 199 BoolNode *test = iff->in(1)->as_Bool();
179 BoolTest::mask bt = test->_test._test; 200 BoolTest::mask bt = test->_test._test;
180 float cl_prob = iff->as_If()->_prob; 201 float cl_prob = iff->as_If()->_prob;
181 if( iftrue_op == Op_IfFalse ) { 202 if (iftrue_op == Op_IfFalse) {
182 bt = BoolTest(bt).negate(); 203 bt = BoolTest(bt).negate();
183 cl_prob = 1.0 - cl_prob; 204 cl_prob = 1.0 - cl_prob;
184 } 205 }
185 // Get backedge compare 206 // Get backedge compare
186 Node *cmp = test->in(1); 207 Node *cmp = test->in(1);
187 int cmp_op = cmp->Opcode(); 208 int cmp_op = cmp->Opcode();
188 if( cmp_op != Op_CmpI ) 209 if( cmp_op != Op_CmpI )
189 return NULL; // Avoid pointer & float compares 210 return false; // Avoid pointer & float compares
190 211
191 // Find the trip-counter increment & limit. Limit must be loop invariant. 212 // Find the trip-counter increment & limit. Limit must be loop invariant.
192 Node *incr = cmp->in(1); 213 Node *incr = cmp->in(1);
193 Node *limit = cmp->in(2); 214 Node *limit = cmp->in(2);
194 215
195 // --------- 216 // ---------
196 // need 'loop()' test to tell if limit is loop invariant 217 // need 'loop()' test to tell if limit is loop invariant
197 // --------- 218 // ---------
198 219
199 if( !is_member( loop, get_ctrl(incr) ) ) { // Swapped trip counter and limit? 220 if (!is_member(loop, get_ctrl(incr))) { // Swapped trip counter and limit?
200 Node *tmp = incr; // Then reverse order into the CmpI 221 Node *tmp = incr; // Then reverse order into the CmpI
201 incr = limit; 222 incr = limit;
202 limit = tmp; 223 limit = tmp;
203 bt = BoolTest(bt).commute(); // And commute the exit test 224 bt = BoolTest(bt).commute(); // And commute the exit test
204 } 225 }
205 if( is_member( loop, get_ctrl(limit) ) ) // Limit must loop-invariant 226 if (is_member(loop, get_ctrl(limit))) // Limit must be loop-invariant
206 return NULL; 227 return false;
207 228 if (!is_member(loop, get_ctrl(incr))) // Trip counter must be loop-variant
229 return false;
230
231 Node* phi_incr = NULL;
208 // Trip-counter increment must be commutative & associative. 232 // Trip-counter increment must be commutative & associative.
209 uint incr_op = incr->Opcode(); 233 if (incr->is_Phi()) {
210 if( incr_op == Op_Phi && incr->req() == 3 ) { 234 if (incr->as_Phi()->region() != x || incr->req() != 3)
211 incr = incr->in(2); // Assume incr is on backedge of Phi 235 return false; // Not simple trip counter expression
212 incr_op = incr->Opcode(); 236 phi_incr = incr;
213 } 237 incr = phi_incr->in(LoopNode::LoopBackControl); // Assume incr is on backedge of Phi
238 if (!is_member(loop, get_ctrl(incr))) // Trip counter must be loop-variant
239 return false;
240 }
241
214 Node* trunc1 = NULL; 242 Node* trunc1 = NULL;
215 Node* trunc2 = NULL; 243 Node* trunc2 = NULL;
216 const TypeInt* iv_trunc_t = NULL; 244 const TypeInt* iv_trunc_t = NULL;
217 if (!(incr = CountedLoopNode::match_incr_with_optional_truncation(incr, &trunc1, &trunc2, &iv_trunc_t))) { 245 if (!(incr = CountedLoopNode::match_incr_with_optional_truncation(incr, &trunc1, &trunc2, &iv_trunc_t))) {
218 return NULL; // Funny increment opcode 246 return false; // Funny increment opcode
219 } 247 }
248 assert(incr->Opcode() == Op_AddI, "wrong increment code");
220 249
221 // Get merge point 250 // Get merge point
222 Node *xphi = incr->in(1); 251 Node *xphi = incr->in(1);
223 Node *stride = incr->in(2); 252 Node *stride = incr->in(2);
224 if( !stride->is_Con() ) { // Oops, swap these 253 if (!stride->is_Con()) { // Oops, swap these
225 if( !xphi->is_Con() ) // Is the other guy a constant? 254 if (!xphi->is_Con()) // Is the other guy a constant?
226 return NULL; // Nope, unknown stride, bail out 255 return false; // Nope, unknown stride, bail out
227 Node *tmp = xphi; // 'incr' is commutative, so ok to swap 256 Node *tmp = xphi; // 'incr' is commutative, so ok to swap
228 xphi = stride; 257 xphi = stride;
229 stride = tmp; 258 stride = tmp;
230 } 259 }
231 //if( loop(xphi) != l) return NULL;// Merge point is in inner loop?? 260 // Stride must be constant
232 if( !xphi->is_Phi() ) return NULL; // Too much math on the trip counter 261 int stride_con = stride->get_int();
262 assert(stride_con != 0, "missed some peephole opt");
263
264 if (!xphi->is_Phi())
265 return false; // Too much math on the trip counter
266 if (phi_incr != NULL && phi_incr != xphi)
267 return false;
233 PhiNode *phi = xphi->as_Phi(); 268 PhiNode *phi = xphi->as_Phi();
234 269
235 // Stride must be constant
236 const Type *stride_t = stride->bottom_type();
237 int stride_con = stride_t->is_int()->get_con();
238 assert( stride_con, "missed some peephole opt" );
239
240 // Phi must be of loop header; backedge must wrap to increment 270 // Phi must be of loop header; backedge must wrap to increment
241 if( phi->region() != x ) return NULL; 271 if (phi->region() != x)
242 if( trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr || 272 return false;
243 trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1 ) { 273 if (trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr ||
244 return NULL; 274 trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1) {
275 return false;
245 } 276 }
246 Node *init_trip = phi->in(LoopNode::EntryControl); 277 Node *init_trip = phi->in(LoopNode::EntryControl);
247 //if (!init_trip->is_Con()) return NULL; // avoid rolling over MAXINT/MININT
248 278
249 // If iv trunc type is smaller than int, check for possible wrap. 279 // If iv trunc type is smaller than int, check for possible wrap.
250 if (!TypeInt::INT->higher_equal(iv_trunc_t)) { 280 if (!TypeInt::INT->higher_equal(iv_trunc_t)) {
251 assert(trunc1 != NULL, "must have found some truncation"); 281 assert(trunc1 != NULL, "must have found some truncation");
252 282
265 // ensure no truncation occurs after the increment. 295 // ensure no truncation occurs after the increment.
266 296
267 if (stride_con > 0) { 297 if (stride_con > 0) {
268 if (iv_trunc_t->_hi - phi_ft->_hi < stride_con || 298 if (iv_trunc_t->_hi - phi_ft->_hi < stride_con ||
269 iv_trunc_t->_lo > phi_ft->_lo) { 299 iv_trunc_t->_lo > phi_ft->_lo) {
270 return NULL; // truncation may occur 300 return false; // truncation may occur
271 } 301 }
272 } else if (stride_con < 0) { 302 } else if (stride_con < 0) {
273 if (iv_trunc_t->_lo - phi_ft->_lo > stride_con || 303 if (iv_trunc_t->_lo - phi_ft->_lo > stride_con ||
274 iv_trunc_t->_hi < phi_ft->_hi) { 304 iv_trunc_t->_hi < phi_ft->_hi) {
275 return NULL; // truncation may occur 305 return false; // truncation may occur
276 } 306 }
277 } 307 }
278 // No possibility of wrap so truncation can be discarded 308 // No possibility of wrap so truncation can be discarded
279 // Promote iv type to Int 309 // Promote iv type to Int
280 } else { 310 } else {
281 assert(trunc1 == NULL && trunc2 == NULL, "no truncation for int"); 311 assert(trunc1 == NULL && trunc2 == NULL, "no truncation for int");
282 } 312 }
283 313
314 // If the condition is inverted and we will be rolling
315 // through MININT to MAXINT, then bail out.
316 if (bt == BoolTest::eq || // Bail out, but this loop trips at most twice!
317 // Odd stride
318 bt == BoolTest::ne && stride_con != 1 && stride_con != -1 ||
319 // Count down loop rolls through MAXINT
320 (bt == BoolTest::le || bt == BoolTest::lt) && stride_con < 0 ||
321 // Count up loop rolls through MININT
322 (bt == BoolTest::ge || bt == BoolTest::gt) && stride_con > 0 ) {
323 return false; // Bail out
324 }
325
326 const TypeInt* init_t = gvn->type(init_trip)->is_int();
327 const TypeInt* limit_t = gvn->type(limit)->is_int();
328
329 if (stride_con > 0) {
330 long init_p = (long)init_t->_lo + stride_con;
331 if (init_p > (long)max_jint || init_p > (long)limit_t->_hi)
332 return false; // cyclic loop or this loop trips only once
333 } else {
334 long init_p = (long)init_t->_hi + stride_con;
335 if (init_p < (long)min_jint || init_p < (long)limit_t->_lo)
336 return false; // cyclic loop or this loop trips only once
337 }
338
284 // ================================================= 339 // =================================================
285 // ---- SUCCESS! Found A Trip-Counted Loop! ----- 340 // ---- SUCCESS! Found A Trip-Counted Loop! -----
286 // 341 //
287 // Canonicalize the condition on the test. If we can exactly determine 342 assert(x->Opcode() == Op_Loop, "regular loops only");
288 // the trip-counter exit value, then set limit to that value and use
289 // a '!=' test. Otherwise use condition '<' for count-up loops and
290 // '>' for count-down loops. If the condition is inverted and we will
291 // be rolling through MININT to MAXINT, then bail out.
292
293 C->print_method("Before CountedLoop", 3); 343 C->print_method("Before CountedLoop", 3);
294
295 // Check for SafePoint on backedge and remove
296 Node *sfpt = x->in(LoopNode::LoopBackControl);
297 if( sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) {
298 lazy_replace( sfpt, iftrue );
299 loop->_tail = iftrue;
300 }
301
302 344
303 // If compare points to incr, we are ok. Otherwise the compare 345 // If compare points to incr, we are ok. Otherwise the compare
304 // can directly point to the phi; in this case adjust the compare so that 346 // can directly point to the phi; in this case adjust the compare so that
305 // it points to the incr by adjusting the limit. 347 // it points to the incr by adjusting the limit.
306 if( cmp->in(1) == phi || cmp->in(2) == phi ) 348 if (cmp->in(1) == phi || cmp->in(2) == phi)
307 limit = gvn->transform(new (C, 3) AddINode(limit,stride)); 349 limit = gvn->transform(new (C, 3) AddINode(limit,stride));
308 350
309 // trip-count for +-tive stride should be: (limit - init_trip + stride - 1)/stride. 351 // trip-count for +-tive stride should be: (limit - init_trip + stride - 1)/stride.
310 // Final value for iterator should be: trip_count * stride + init_trip. 352 // Final value for iterator should be: trip_count * stride + init_trip.
311 const Type *limit_t = limit->bottom_type();
312 const Type *init_t = init_trip->bottom_type();
313 Node *one_p = gvn->intcon( 1); 353 Node *one_p = gvn->intcon( 1);
314 Node *one_m = gvn->intcon(-1); 354 Node *one_m = gvn->intcon(-1);
315 355
316 Node *trip_count = NULL; 356 Node *trip_count = NULL;
317 Node *hook = new (C, 6) Node(6); 357 Node *hook = new (C, 6) Node(6);
318 switch( bt ) { 358 switch( bt ) {
319 case BoolTest::eq: 359 case BoolTest::eq:
320 return NULL; // Bail out, but this loop trips at most twice! 360 ShouldNotReachHere();
321 case BoolTest::ne: // Ahh, the case we desire 361 case BoolTest::ne: // Ahh, the case we desire
322 if( stride_con == 1 ) 362 if (stride_con == 1)
323 trip_count = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 363 trip_count = gvn->transform(new (C, 3) SubINode(limit,init_trip));
324 else if( stride_con == -1 ) 364 else if (stride_con == -1)
325 trip_count = gvn->transform(new (C, 3) SubINode(init_trip,limit)); 365 trip_count = gvn->transform(new (C, 3) SubINode(init_trip,limit));
326 else 366 else
327 return NULL; // Odd stride; must prove we hit limit exactly 367 ShouldNotReachHere();
328 set_subtree_ctrl( trip_count ); 368 set_subtree_ctrl(trip_count);
329 //_loop.map(trip_count->_idx,loop(limit)); 369 //_loop.map(trip_count->_idx,loop(limit));
330 break; 370 break;
331 case BoolTest::le: // Maybe convert to '<' case 371 case BoolTest::le: // Maybe convert to '<' case
332 limit = gvn->transform(new (C, 3) AddINode(limit,one_p)); 372 limit = gvn->transform(new (C, 3) AddINode(limit,one_p));
333 set_subtree_ctrl( limit ); 373 set_subtree_ctrl( limit );
336 bt = BoolTest::lt; 376 bt = BoolTest::lt;
337 // Make the new limit be in the same loop nest as the old limit 377 // Make the new limit be in the same loop nest as the old limit
338 //_loop.map(limit->_idx,limit_loop); 378 //_loop.map(limit->_idx,limit_loop);
339 // Fall into next case 379 // Fall into next case
340 case BoolTest::lt: { // Maybe convert to '!=' case 380 case BoolTest::lt: { // Maybe convert to '!=' case
341 if( stride_con < 0 ) return NULL; // Count down loop rolls through MAXINT 381 if (stride_con < 0) // Count down loop rolls through MAXINT
382 ShouldNotReachHere();
342 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 383 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip));
343 set_subtree_ctrl( range ); 384 set_subtree_ctrl( range );
344 hook->init_req(0, range); 385 hook->init_req(0, range);
345 386
346 Node *bias = gvn->transform(new (C, 3) AddINode(range,stride)); 387 Node *bias = gvn->transform(new (C, 3) AddINode(range,stride));
365 bt = BoolTest::gt; 406 bt = BoolTest::gt;
366 // Make the new limit be in the same loop nest as the old limit 407 // Make the new limit be in the same loop nest as the old limit
367 //_loop.map(limit->_idx,limit_loop); 408 //_loop.map(limit->_idx,limit_loop);
368 // Fall into next case 409 // Fall into next case
369 case BoolTest::gt: { // Maybe convert to '!=' case 410 case BoolTest::gt: { // Maybe convert to '!=' case
370 if( stride_con > 0 ) return NULL; // count up loop rolls through MININT 411 if (stride_con > 0) // count up loop rolls through MININT
412 ShouldNotReachHere();
371 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 413 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip));
372 set_subtree_ctrl( range ); 414 set_subtree_ctrl( range );
373 hook->init_req(0, range); 415 hook->init_req(0, range);
374 416
375 Node *bias = gvn->transform(new (C, 3) AddINode(range,stride)); 417 Node *bias = gvn->transform(new (C, 3) AddINode(range,stride));
383 trip_count = gvn->transform(new (C, 3) DivINode(0,bias1,stride)); 425 trip_count = gvn->transform(new (C, 3) DivINode(0,bias1,stride));
384 set_subtree_ctrl( trip_count ); 426 set_subtree_ctrl( trip_count );
385 hook->init_req(3, trip_count); 427 hook->init_req(3, trip_count);
386 break; 428 break;
387 } 429 }
388 } 430 } // switch( bt )
389 431
390 Node *span = gvn->transform(new (C, 3) MulINode(trip_count,stride)); 432 Node *span = gvn->transform(new (C, 3) MulINode(trip_count,stride));
391 set_subtree_ctrl( span ); 433 set_subtree_ctrl( span );
392 hook->init_req(5, span); 434 hook->init_req(5, span);
393 435
394 limit = gvn->transform(new (C, 3) AddINode(span,init_trip)); 436 limit = gvn->transform(new (C, 3) AddINode(span,init_trip));
395 set_subtree_ctrl( limit ); 437 set_subtree_ctrl( limit );
396 438
439 // Check for SafePoint on backedge and remove
440 Node *sfpt = x->in(LoopNode::LoopBackControl);
441 if (sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) {
442 lazy_replace( sfpt, iftrue );
443 loop->_tail = iftrue;
444 }
445
397 // Build a canonical trip test. 446 // Build a canonical trip test.
398 // Clone code, as old values may be in use. 447 // Clone code, as old values may be in use.
448 Node* nphi = PhiNode::make(x, init_trip, TypeInt::INT);
449 nphi = _igvn.register_new_node_with_optimizer(nphi);
450 set_ctrl(nphi, get_ctrl(phi));
451
399 incr = incr->clone(); 452 incr = incr->clone();
400 incr->set_req(1,phi); 453 incr->set_req(1,nphi);
401 incr->set_req(2,stride); 454 incr->set_req(2,stride);
402 incr = _igvn.register_new_node_with_optimizer(incr); 455 incr = _igvn.register_new_node_with_optimizer(incr);
403 set_early_ctrl( incr ); 456 set_early_ctrl( incr );
404 _igvn.hash_delete(phi); 457
405 phi->set_req_X( LoopNode::LoopBackControl, incr, &_igvn ); 458 nphi->set_req(LoopNode::LoopBackControl, incr);
406 459 _igvn.replace_node(phi, nphi);
407 // If phi type is more restrictive than Int, raise to 460 phi = nphi->as_Phi();
408 // Int to prevent (almost) infinite recursion in igvn 461
409 // which can only handle integer types for constants or minint..maxint.
410 if (!TypeInt::INT->higher_equal(phi->bottom_type())) {
411 Node* nphi = PhiNode::make(phi->in(0), phi->in(LoopNode::EntryControl), TypeInt::INT);
412 nphi->set_req(LoopNode::LoopBackControl, phi->in(LoopNode::LoopBackControl));
413 nphi = _igvn.register_new_node_with_optimizer(nphi);
414 set_ctrl(nphi, get_ctrl(phi));
415 _igvn.replace_node(phi, nphi);
416 phi = nphi->as_Phi();
417 }
418 cmp = cmp->clone(); 462 cmp = cmp->clone();
419 cmp->set_req(1,incr); 463 cmp->set_req(1,incr);
420 cmp->set_req(2,limit); 464 cmp->set_req(2,limit);
421 cmp = _igvn.register_new_node_with_optimizer(cmp); 465 cmp = _igvn.register_new_node_with_optimizer(cmp);
422 set_ctrl(cmp, iff->in(0)); 466 set_ctrl(cmp, iff->in(0));
423 467
424 Node *tmp = test->clone(); 468 test = test->clone()->as_Bool();
425 assert( tmp->is_Bool(), "" ); 469 (*(BoolTest*)&test->_test)._test = bt;
426 test = (BoolNode*)tmp;
427 (*(BoolTest*)&test->_test)._test = bt; //BoolTest::ne;
428 test->set_req(1,cmp); 470 test->set_req(1,cmp);
429 _igvn.register_new_node_with_optimizer(test); 471 _igvn.register_new_node_with_optimizer(test);
430 set_ctrl(test, iff->in(0)); 472 set_ctrl(test, iff->in(0));
431 // If the exit test is dead, STOP!
432 if( test == NULL ) return NULL;
433 _igvn.hash_delete(iff);
434 iff->set_req_X( 1, test, &_igvn );
435 473
436 // Replace the old IfNode with a new LoopEndNode 474 // Replace the old IfNode with a new LoopEndNode
437 Node *lex = _igvn.register_new_node_with_optimizer(new (C, 2) CountedLoopEndNode( iff->in(0), iff->in(1), cl_prob, iff->as_If()->_fcnt )); 475 Node *lex = _igvn.register_new_node_with_optimizer(new (C, 2) CountedLoopEndNode( iff->in(0), test, cl_prob, iff->as_If()->_fcnt ));
438 IfNode *le = lex->as_If(); 476 IfNode *le = lex->as_If();
439 uint dd = dom_depth(iff); 477 uint dd = dom_depth(iff);
440 set_idom(le, le->in(0), dd); // Update dominance for loop exit 478 set_idom(le, le->in(0), dd); // Update dominance for loop exit
441 set_loop(le, loop); 479 set_loop(le, loop);
442 480
443 // Get the loop-exit control 481 // Get the loop-exit control
444 Node *if_f = iff->as_If()->proj_out(!(iftrue_op == Op_IfTrue)); 482 Node *iffalse = iff->as_If()->proj_out(!(iftrue_op == Op_IfTrue));
445 483
446 // Need to swap loop-exit and loop-back control? 484 // Need to swap loop-exit and loop-back control?
447 if( iftrue_op == Op_IfFalse ) { 485 if (iftrue_op == Op_IfFalse) {
448 Node *ift2=_igvn.register_new_node_with_optimizer(new (C, 1) IfTrueNode (le)); 486 Node *ift2=_igvn.register_new_node_with_optimizer(new (C, 1) IfTrueNode (le));
449 Node *iff2=_igvn.register_new_node_with_optimizer(new (C, 1) IfFalseNode(le)); 487 Node *iff2=_igvn.register_new_node_with_optimizer(new (C, 1) IfFalseNode(le));
450 488
451 loop->_tail = back_control = ift2; 489 loop->_tail = back_control = ift2;
452 set_loop(ift2, loop); 490 set_loop(ift2, loop);
453 set_loop(iff2, get_loop(if_f)); 491 set_loop(iff2, get_loop(iffalse));
454 492
455 // Lazy update of 'get_ctrl' mechanism. 493 // Lazy update of 'get_ctrl' mechanism.
456 lazy_replace_proj( if_f , iff2 ); 494 lazy_replace_proj( iffalse, iff2 );
457 lazy_replace_proj( iftrue, ift2 ); 495 lazy_replace_proj( iftrue, ift2 );
458 496
459 // Swap names 497 // Swap names
460 if_f = iff2; 498 iffalse = iff2;
461 iftrue = ift2; 499 iftrue = ift2;
462 } else { 500 } else {
463 _igvn.hash_delete(if_f ); 501 _igvn.hash_delete(iffalse);
464 _igvn.hash_delete(iftrue); 502 _igvn.hash_delete(iftrue);
465 if_f ->set_req_X( 0, le, &_igvn ); 503 iffalse->set_req_X( 0, le, &_igvn );
466 iftrue->set_req_X( 0, le, &_igvn ); 504 iftrue ->set_req_X( 0, le, &_igvn );
467 } 505 }
468 506
469 set_idom(iftrue, le, dd+1); 507 set_idom(iftrue, le, dd+1);
470 set_idom(if_f, le, dd+1); 508 set_idom(iffalse, le, dd+1);
509 assert(iff->outcnt() == 0, "should be dead now");
510 lazy_replace( iff, le ); // fix 'get_ctrl'
471 511
472 // Now setup a new CountedLoopNode to replace the existing LoopNode 512 // Now setup a new CountedLoopNode to replace the existing LoopNode
473 CountedLoopNode *l = new (C, 3) CountedLoopNode(init_control, back_control); 513 CountedLoopNode *l = new (C, 3) CountedLoopNode(init_control, back_control);
514 l->set_unswitch_count(x->as_Loop()->unswitch_count()); // Preserve
474 // The following assert is approximately true, and defines the intention 515 // The following assert is approximately true, and defines the intention
475 // of can_be_counted_loop. It fails, however, because phase->type 516 // of can_be_counted_loop. It fails, however, because phase->type
476 // is not yet initialized for this loop and its parts. 517 // is not yet initialized for this loop and its parts.
477 //assert(l->can_be_counted_loop(this), "sanity"); 518 //assert(l->can_be_counted_loop(this), "sanity");
478 _igvn.register_new_node_with_optimizer(l); 519 _igvn.register_new_node_with_optimizer(l);
489 lazy_replace( sfpt2, sfpt2->in(TypeFunc::Control)); 530 lazy_replace( sfpt2, sfpt2->in(TypeFunc::Control));
490 531
491 // Free up intermediate goo 532 // Free up intermediate goo
492 _igvn.remove_dead_node(hook); 533 _igvn.remove_dead_node(hook);
493 534
535 #ifdef ASSERT
536 assert(l->is_valid_counted_loop(), "counted loop shape is messed up");
537 assert(l == loop->_head && l->phi() == phi && l->loopexit() == lex, "" );
538 #endif
539
494 C->print_method("After CountedLoop", 3); 540 C->print_method("After CountedLoop", 3);
495 541
496 // Return trip counter 542 return true;
497 return trip_count;
498 } 543 }
499 544
500 545
501 //------------------------------Ideal------------------------------------------ 546 //------------------------------Ideal------------------------------------------
502 // Return a node which is more "ideal" than the current node. 547 // Return a node which is more "ideal" than the current node.
1254 lp = lp->_parent; 1299 lp = lp->_parent;
1255 } 1300 }
1256 return true; 1301 return true;
1257 } 1302 }
1258 1303
1304 //---------------------------replace_parallel_iv-------------------------------
1305 // Replace parallel induction variable (parallel to trip counter)
1306 void PhaseIdealLoop::replace_parallel_iv(IdealLoopTree *loop) {
1307 assert(loop->_head->is_CountedLoop(), "");
1308 CountedLoopNode *cl = loop->_head->as_CountedLoop();
1309 Node *incr = cl->incr();
1310 if (incr == NULL)
1311 return; // Dead loop?
1312 Node *init = cl->init_trip();
1313 Node *phi = cl->phi();
1314 // protect against stride not being a constant
1315 if (!cl->stride_is_con())
1316 return;
1317 int stride_con = cl->stride_con();
1318
1319 PhaseGVN *gvn = &_igvn;
1320
1321 // Visit all children, looking for Phis
1322 for (DUIterator i = cl->outs(); cl->has_out(i); i++) {
1323 Node *out = cl->out(i);
1324 // Look for other phis (secondary IVs). Skip dead ones
1325 if (!out->is_Phi() || out == phi || !has_node(out))
1326 continue;
1327 PhiNode* phi2 = out->as_Phi();
1328 Node *incr2 = phi2->in( LoopNode::LoopBackControl );
1329 // Look for induction variables of the form: X += constant
1330 if (phi2->region() != loop->_head ||
1331 incr2->req() != 3 ||
1332 incr2->in(1) != phi2 ||
1333 incr2 == incr ||
1334 incr2->Opcode() != Op_AddI ||
1335 !incr2->in(2)->is_Con())
1336 continue;
1337
1338 // Check for parallel induction variable (parallel to trip counter)
1339 // via an affine function. In particular, count-down loops with
1340 // count-up array indices are common. We only RCE references off
1341 // the trip-counter, so we need to convert all these to trip-counter
1342 // expressions.
1343 Node *init2 = phi2->in( LoopNode::EntryControl );
1344 int stride_con2 = incr2->in(2)->get_int();
1345
1346 // The general case here gets a little tricky. We want to find the
1347 // GCD of all possible parallel IV's and make a new IV using this
1348 // GCD for the loop. Then all possible IVs are simple multiples of
1349 // the GCD. In practice, this will cover very few extra loops.
1350 // Instead we require 'stride_con2' to be a multiple of 'stride_con',
1351 // where +/-1 is the common case, but other integer multiples are
1352 // also easy to handle.
1353 int ratio_con = stride_con2/stride_con;
1354
1355 if ((ratio_con * stride_con) == stride_con2) { // Check for exact
1356 // Convert to using the trip counter. The parallel induction
1357 // variable differs from the trip counter by a loop-invariant
1358 // amount, the difference between their respective initial values.
1359 // It is scaled by the 'ratio_con'.
1360 // Perform local Ideal transformation since in most cases ratio == 1.
1361 Node* ratio = _igvn.intcon(ratio_con);
1362 set_ctrl(ratio, C->root());
1363 Node* hook = new (C, 3) Node(3);
1364 Node* ratio_init = gvn->transform(new (C, 3) MulINode(init, ratio));
1365 hook->init_req(0, ratio_init);
1366 Node* diff = gvn->transform(new (C, 3) SubINode(init2, ratio_init));
1367 hook->init_req(1, diff);
1368 Node* ratio_idx = gvn->transform(new (C, 3) MulINode(phi, ratio));
1369 hook->init_req(2, ratio_idx);
1370 Node* add = gvn->transform(new (C, 3) AddINode(ratio_idx, diff));
1371 set_subtree_ctrl(add);
1372 _igvn.replace_node( phi2, add );
1373 // Free up intermediate goo
1374 _igvn.remove_dead_node(hook);
1375 // Sometimes an induction variable is unused
1376 if (add->outcnt() == 0) {
1377 _igvn.remove_dead_node(add);
1378 }
1379 --i; // deleted this phi; rescan starting with next position
1380 continue;
1381 }
1382 }
1383 }
1384
1259 //------------------------------counted_loop----------------------------------- 1385 //------------------------------counted_loop-----------------------------------
1260 // Convert to counted loops where possible 1386 // Convert to counted loops where possible
1261 void IdealLoopTree::counted_loop( PhaseIdealLoop *phase ) { 1387 void IdealLoopTree::counted_loop( PhaseIdealLoop *phase ) {
1262 1388
1263 // For grins, set the inner-loop flag here 1389 // For grins, set the inner-loop flag here
1264 if( !_child ) { 1390 if (!_child) {
1265 if( _head->is_Loop() ) _head->as_Loop()->set_inner_loop(); 1391 if (_head->is_Loop()) _head->as_Loop()->set_inner_loop();
1266 } 1392 }
1267 1393
1268 if( _head->is_CountedLoop() || 1394 if (_head->is_CountedLoop() ||
1269 phase->is_counted_loop( _head, this ) ) { 1395 phase->is_counted_loop(_head, this)) {
1270 _has_sfpt = 1; // Indicate we do not need a safepoint here 1396 _has_sfpt = 1; // Indicate we do not need a safepoint here
1271 1397
1272 // Look for a safepoint to remove 1398 // Look for a safepoint to remove
1273 for (Node* n = tail(); n != _head; n = phase->idom(n)) 1399 for (Node* n = tail(); n != _head; n = phase->idom(n))
1274 if (n->Opcode() == Op_SafePoint && phase->get_loop(n) == this && 1400 if (n->Opcode() == Op_SafePoint && phase->get_loop(n) == this &&
1275 phase->is_deleteable_safept(n)) 1401 phase->is_deleteable_safept(n))
1276 phase->lazy_replace(n,n->in(TypeFunc::Control)); 1402 phase->lazy_replace(n,n->in(TypeFunc::Control));
1277 1403
1278 CountedLoopNode *cl = _head->as_CountedLoop();
1279 Node *incr = cl->incr();
1280 if( !incr ) return; // Dead loop?
1281 Node *init = cl->init_trip();
1282 Node *phi = cl->phi();
1283 // protect against stride not being a constant
1284 if( !cl->stride_is_con() ) return;
1285 int stride_con = cl->stride_con();
1286
1287 // Look for induction variables 1404 // Look for induction variables
1288 1405 phase->replace_parallel_iv(this);
1289 // Visit all children, looking for Phis 1406
1290 for (DUIterator i = cl->outs(); cl->has_out(i); i++) {
1291 Node *out = cl->out(i);
1292 // Look for other phis (secondary IVs). Skip dead ones
1293 if (!out->is_Phi() || out == phi || !phase->has_node(out)) continue;
1294 PhiNode* phi2 = out->as_Phi();
1295 Node *incr2 = phi2->in( LoopNode::LoopBackControl );
1296 // Look for induction variables of the form: X += constant
1297 if( phi2->region() != _head ||
1298 incr2->req() != 3 ||
1299 incr2->in(1) != phi2 ||
1300 incr2 == incr ||
1301 incr2->Opcode() != Op_AddI ||
1302 !incr2->in(2)->is_Con() )
1303 continue;
1304
1305 // Check for parallel induction variable (parallel to trip counter)
1306 // via an affine function. In particular, count-down loops with
1307 // count-up array indices are common. We only RCE references off
1308 // the trip-counter, so we need to convert all these to trip-counter
1309 // expressions.
1310 Node *init2 = phi2->in( LoopNode::EntryControl );
1311 int stride_con2 = incr2->in(2)->get_int();
1312
1313 // The general case here gets a little tricky. We want to find the
1314 // GCD of all possible parallel IV's and make a new IV using this
1315 // GCD for the loop. Then all possible IVs are simple multiples of
1316 // the GCD. In practice, this will cover very few extra loops.
1317 // Instead we require 'stride_con2' to be a multiple of 'stride_con',
1318 // where +/-1 is the common case, but other integer multiples are
1319 // also easy to handle.
1320 int ratio_con = stride_con2/stride_con;
1321
1322 if( ratio_con * stride_con == stride_con2 ) { // Check for exact
1323 // Convert to using the trip counter. The parallel induction
1324 // variable differs from the trip counter by a loop-invariant
1325 // amount, the difference between their respective initial values.
1326 // It is scaled by the 'ratio_con'.
1327 Compile* C = phase->C;
1328 Node* ratio = phase->_igvn.intcon(ratio_con);
1329 phase->set_ctrl(ratio, C->root());
1330 Node* ratio_init = new (C, 3) MulINode(init, ratio);
1331 phase->_igvn.register_new_node_with_optimizer(ratio_init, init);
1332 phase->set_early_ctrl(ratio_init);
1333 Node* diff = new (C, 3) SubINode(init2, ratio_init);
1334 phase->_igvn.register_new_node_with_optimizer(diff, init2);
1335 phase->set_early_ctrl(diff);
1336 Node* ratio_idx = new (C, 3) MulINode(phi, ratio);
1337 phase->_igvn.register_new_node_with_optimizer(ratio_idx, phi);
1338 phase->set_ctrl(ratio_idx, cl);
1339 Node* add = new (C, 3) AddINode(ratio_idx, diff);
1340 phase->_igvn.register_new_node_with_optimizer(add);
1341 phase->set_ctrl(add, cl);
1342 phase->_igvn.replace_node( phi2, add );
1343 // Sometimes an induction variable is unused
1344 if (add->outcnt() == 0) {
1345 phase->_igvn.remove_dead_node(add);
1346 }
1347 --i; // deleted this phi; rescan starting with next position
1348 continue;
1349 }
1350 }
1351 } else if (_parent != NULL && !_irreducible) { 1407 } else if (_parent != NULL && !_irreducible) {
1352 // Not a counted loop. 1408 // Not a counted loop.
1353 // Look for a safepoint on the idom-path to remove, preserving the first one 1409 // Look for a safepoint on the idom-path to remove, preserving the first one
1354 bool found = false; 1410 bool found = false;
1355 Node* n = tail(); 1411 Node* n = tail();
1364 phase->lazy_replace(n,n->in(TypeFunc::Control)); 1420 phase->lazy_replace(n,n->in(TypeFunc::Control));
1365 } 1421 }
1366 } 1422 }
1367 1423
1368 // Recursively 1424 // Recursively
1369 if( _child ) _child->counted_loop( phase ); 1425 if (_child) _child->counted_loop( phase );
1370 if( _next ) _next ->counted_loop( phase ); 1426 if (_next) _next ->counted_loop( phase );
1371 } 1427 }
1372 1428
1373 #ifndef PRODUCT 1429 #ifndef PRODUCT
1374 //------------------------------dump_head-------------------------------------- 1430 //------------------------------dump_head--------------------------------------
1375 // Dump 1 liner for loop header info 1431 // Dump 1 liner for loop header info
1376 void IdealLoopTree::dump_head( ) const { 1432 void IdealLoopTree::dump_head( ) const {
1377 for( uint i=0; i<_nest; i++ ) 1433 for (uint i=0; i<_nest; i++)
1378 tty->print(" "); 1434 tty->print(" ");
1379 tty->print("Loop: N%d/N%d ",_head->_idx,_tail->_idx); 1435 tty->print("Loop: N%d/N%d ",_head->_idx,_tail->_idx);
1380 if( _irreducible ) tty->print(" IRREDUCIBLE"); 1436 if (_irreducible) tty->print(" IRREDUCIBLE");
1381 if( _head->is_CountedLoop() ) { 1437 if (UseLoopPredicate) {
1438 Node* entry = _head->in(LoopNode::EntryControl);
1439 if (entry != NULL && entry->is_Proj() &&
1440 PhaseIdealLoop::is_uncommon_trap_if_pattern(entry->as_Proj(), Deoptimization::Reason_predicate)) {
1441 tty->print(" predicated");
1442 }
1443 }
1444 if (_head->is_CountedLoop()) {
1382 CountedLoopNode *cl = _head->as_CountedLoop(); 1445 CountedLoopNode *cl = _head->as_CountedLoop();
1383 tty->print(" counted"); 1446 tty->print(" counted");
1384 if( cl->is_pre_loop () ) tty->print(" pre" ); 1447 if (cl->is_pre_loop ()) tty->print(" pre" );
1385 if( cl->is_main_loop() ) tty->print(" main"); 1448 if (cl->is_main_loop()) tty->print(" main");
1386 if( cl->is_post_loop() ) tty->print(" post"); 1449 if (cl->is_post_loop()) tty->print(" post");
1387 } 1450 }
1388 tty->cr(); 1451 tty->cr();
1389 } 1452 }
1390 1453
1391 //------------------------------dump------------------------------------------- 1454 //------------------------------dump-------------------------------------------
1392 // Dump loops by loop tree 1455 // Dump loops by loop tree
1393 void IdealLoopTree::dump( ) const { 1456 void IdealLoopTree::dump( ) const {
1394 dump_head(); 1457 dump_head();
1395 if( _child ) _child->dump(); 1458 if (_child) _child->dump();
1396 if( _next ) _next ->dump(); 1459 if (_next) _next ->dump();
1397 } 1460 }
1398 1461
1399 #endif 1462 #endif
1400 1463
1401 static void log_loop_tree(IdealLoopTree* root, IdealLoopTree* loop, CompileLog* log) { 1464 static void log_loop_tree(IdealLoopTree* root, IdealLoopTree* loop, CompileLog* log) {
1437 if (loop->_child) { // child 1500 if (loop->_child) { // child
1438 collect_potentially_useful_predicates(loop->_child, useful_predicates); 1501 collect_potentially_useful_predicates(loop->_child, useful_predicates);
1439 } 1502 }
1440 1503
1441 // self (only loops that we can apply loop predication may use their predicates) 1504 // self (only loops that we can apply loop predication may use their predicates)
1442 if (loop->_head->is_Loop() && 1505 if (loop->_head->is_Loop() &&
1443 !loop->_irreducible && 1506 !loop->_irreducible &&
1444 !loop->tail()->is_top()) { 1507 !loop->tail()->is_top()) {
1445 LoopNode *lpn = loop->_head->as_Loop(); 1508 LoopNode* lpn = loop->_head->as_Loop();
1446 Node* entry = lpn->in(LoopNode::EntryControl); 1509 Node* entry = lpn->in(LoopNode::EntryControl);
1447 ProjNode *predicate_proj = find_predicate_insertion_point(entry); 1510 Node* predicate_proj = find_predicate(entry);
1448 if (predicate_proj != NULL ) { // right pattern that can be used by loop predication 1511 if (predicate_proj != NULL ) { // right pattern that can be used by loop predication
1449 assert(entry->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be"); 1512 assert(entry->in(0)->in(1)->in(1)->Opcode() == Op_Opaque1, "must be");
1450 useful_predicates.push(entry->in(0)->in(1)->in(1)); // good one 1513 useful_predicates.push(entry->in(0)->in(1)->in(1)); // good one
1451 } 1514 }
1452 } 1515 }
1453 1516
1454 if ( loop->_next ) { // sibling 1517 if (loop->_next) { // sibling
1455 collect_potentially_useful_predicates(loop->_next, useful_predicates); 1518 collect_potentially_useful_predicates(loop->_next, useful_predicates);
1456 } 1519 }
1457 } 1520 }
1458 1521
1459 //------------------------eliminate_useless_predicates----------------------------- 1522 //------------------------eliminate_useless_predicates-----------------------------
1460 // Eliminate all inserted predicates if they could not be used by loop predication. 1523 // Eliminate all inserted predicates if they could not be used by loop predication.
1461 void PhaseIdealLoop::eliminate_useless_predicates() { 1524 void PhaseIdealLoop::eliminate_useless_predicates() {
1462 if (C->predicate_count() == 0) return; // no predicate left 1525 if (C->predicate_count() == 0)
1526 return; // no predicate left
1463 1527
1464 Unique_Node_List useful_predicates; // to store useful predicates 1528 Unique_Node_List useful_predicates; // to store useful predicates
1465 if (C->has_loops()) { 1529 if (C->has_loops()) {
1466 collect_potentially_useful_predicates(_ltree_root->_child, useful_predicates); 1530 collect_potentially_useful_predicates(_ltree_root->_child, useful_predicates);
1467 } 1531 }
1645 _igvn.remove_globally_dead_node(_deadlist.pop()); 1709 _igvn.remove_globally_dead_node(_deadlist.pop());
1646 } 1710 }
1647 1711
1648 #ifndef PRODUCT 1712 #ifndef PRODUCT
1649 C->verify_graph_edges(); 1713 C->verify_graph_edges();
1650 if( _verify_me ) { // Nested verify pass? 1714 if (_verify_me) { // Nested verify pass?
1651 // Check to see if the verify mode is broken 1715 // Check to see if the verify mode is broken
1652 assert(C->unique() == unique, "non-optimize mode made Nodes? ? ?"); 1716 assert(C->unique() == unique, "non-optimize mode made Nodes? ? ?");
1653 return; 1717 return;
1654 } 1718 }
1655 if( VerifyLoopOptimizations ) verify(); 1719 if(VerifyLoopOptimizations) verify();
1720 if(TraceLoopOpts && C->has_loops()) {
1721 _ltree_root->dump();
1722 }
1656 #endif 1723 #endif
1657 1724
1658 if (ReassociateInvariants) { 1725 if (ReassociateInvariants) {
1659 // Reassociate invariants and prep for split_thru_phi 1726 // Reassociate invariants and prep for split_thru_phi
1660 for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) { 1727 for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) {