1 /*
2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "ci/ciTypeFlow.hpp"
27 #include "memory/allocation.inline.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "opto/addnode.hpp"
30 #include "opto/castnode.hpp"
31 #include "opto/cfgnode.hpp"
32 #include "opto/connode.hpp"
33 #include "opto/loopnode.hpp"
34 #include "opto/phaseX.hpp"
35 #include "opto/runtime.hpp"
36 #include "opto/rootnode.hpp"
37 #include "opto/subnode.hpp"
38
39 // Portions of code courtesy of Clifford Click
40
41 // Optimization - Graph Style
42
43
44 #ifndef PRODUCT
45 extern int explicit_null_checks_elided;
46 #endif
47
48 //=============================================================================
49 //------------------------------Value------------------------------------------
50 // Return a tuple for whichever arm of the IF is reachable
Value(PhaseGVN * phase) const51 const Type* IfNode::Value(PhaseGVN* phase) const {
52 if( !in(0) ) return Type::TOP;
53 if( phase->type(in(0)) == Type::TOP )
54 return Type::TOP;
55 const Type *t = phase->type(in(1));
56 if( t == Type::TOP ) // data is undefined
57 return TypeTuple::IFNEITHER; // unreachable altogether
58 if( t == TypeInt::ZERO ) // zero, or false
59 return TypeTuple::IFFALSE; // only false branch is reachable
60 if( t == TypeInt::ONE ) // 1, or true
61 return TypeTuple::IFTRUE; // only true branch is reachable
62 assert( t == TypeInt::BOOL, "expected boolean type" );
63
64 return TypeTuple::IFBOTH; // No progress
65 }
66
out_RegMask() const67 const RegMask &IfNode::out_RegMask() const {
68 return RegMask::Empty;
69 }
70
71 //------------------------------split_if---------------------------------------
72 // Look for places where we merge constants, then test on the merged value.
73 // If the IF test will be constant folded on the path with the constant, we
74 // win by splitting the IF to before the merge point.
split_if(IfNode * iff,PhaseIterGVN * igvn)75 static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) {
76 // I could be a lot more general here, but I'm trying to squeeze this
77 // in before the Christmas '98 break so I'm gonna be kinda restrictive
78 // on the patterns I accept. CNC
79
80 // Look for a compare of a constant and a merged value
81 Node *i1 = iff->in(1);
82 if( !i1->is_Bool() ) return NULL;
83 BoolNode *b = i1->as_Bool();
84 Node *cmp = b->in(1);
85 if( !cmp->is_Cmp() ) return NULL;
86 i1 = cmp->in(1);
87 if( i1 == NULL || !i1->is_Phi() ) return NULL;
88 PhiNode *phi = i1->as_Phi();
89 if( phi->is_copy() ) return NULL;
90 Node *con2 = cmp->in(2);
91 if( !con2->is_Con() ) return NULL;
92 // See that the merge point contains some constants
93 Node *con1=NULL;
94 uint i4;
95 for( i4 = 1; i4 < phi->req(); i4++ ) {
96 con1 = phi->in(i4);
97 if( !con1 ) return NULL; // Do not optimize partially collapsed merges
98 if( con1->is_Con() ) break; // Found a constant
99 // Also allow null-vs-not-null checks
100 const TypePtr *tp = igvn->type(con1)->isa_ptr();
101 if( tp && tp->_ptr == TypePtr::NotNull )
102 break;
103 }
104 if( i4 >= phi->req() ) return NULL; // Found no constants
105
106 igvn->C->set_has_split_ifs(true); // Has chance for split-if
107
108 // Make sure that the compare can be constant folded away
109 Node *cmp2 = cmp->clone();
110 cmp2->set_req(1,con1);
111 cmp2->set_req(2,con2);
112 const Type *t = cmp2->Value(igvn);
113 // This compare is dead, so whack it!
114 igvn->remove_dead_node(cmp2);
115 if( !t->singleton() ) return NULL;
116
117 // No intervening control, like a simple Call
118 Node *r = iff->in(0);
119 if( !r->is_Region() ) return NULL;
120 if (r->is_Loop()) return NULL;
121 if( phi->region() != r ) return NULL;
122 // No other users of the cmp/bool
123 if (b->outcnt() != 1 || cmp->outcnt() != 1) {
124 //tty->print_cr("many users of cmp/bool");
125 return NULL;
126 }
127
128 // Make sure we can determine where all the uses of merged values go
129 for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) {
130 Node* u = r->fast_out(j);
131 if( u == r ) continue;
132 if( u == iff ) continue;
133 if( u->outcnt() == 0 ) continue; // use is dead & ignorable
134 if( !u->is_Phi() ) {
135 /*
136 if( u->is_Start() ) {
137 tty->print_cr("Region has inlined start use");
138 } else {
139 tty->print_cr("Region has odd use");
140 u->dump(2);
141 }*/
142 return NULL;
143 }
144 if( u != phi ) {
145 // CNC - do not allow any other merged value
146 //tty->print_cr("Merging another value");
147 //u->dump(2);
148 return NULL;
149 }
150 // Make sure we can account for all Phi uses
151 for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) {
152 Node* v = u->fast_out(k); // User of the phi
153 // CNC - Allow only really simple patterns.
154 // In particular I disallow AddP of the Phi, a fairly common pattern
155 if (v == cmp) continue; // The compare is OK
156 if (v->is_ConstraintCast()) {
157 // If the cast is derived from data flow edges, it may not have a control edge.
158 // If so, it should be safe to split. But follow-up code can not deal with
159 // this (l. 359). So skip.
160 if (v->in(0) == NULL) {
161 return NULL;
162 }
163 if (v->in(0)->in(0) == iff) {
164 continue; // CastPP/II of the IfNode is OK
165 }
166 }
167 // Disabled following code because I cannot tell if exactly one
168 // path dominates without a real dominator check. CNC 9/9/1999
169 //uint vop = v->Opcode();
170 //if( vop == Op_Phi ) { // Phi from another merge point might be OK
171 // Node *r = v->in(0); // Get controlling point
172 // if( !r ) return NULL; // Degraded to a copy
173 // // Find exactly one path in (either True or False doms, but not IFF)
174 // int cnt = 0;
175 // for( uint i = 1; i < r->req(); i++ )
176 // if( r->in(i) && r->in(i)->in(0) == iff )
177 // cnt++;
178 // if( cnt == 1 ) continue; // Exactly one of True or False guards Phi
179 //}
180 if( !v->is_Call() ) {
181 /*
182 if( v->Opcode() == Op_AddP ) {
183 tty->print_cr("Phi has AddP use");
184 } else if( v->Opcode() == Op_CastPP ) {
185 tty->print_cr("Phi has CastPP use");
186 } else if( v->Opcode() == Op_CastII ) {
187 tty->print_cr("Phi has CastII use");
188 } else {
189 tty->print_cr("Phi has use I cant be bothered with");
190 }
191 */
192 }
193 return NULL;
194
195 /* CNC - Cut out all the fancy acceptance tests
196 // Can we clone this use when doing the transformation?
197 // If all uses are from Phis at this merge or constants, then YES.
198 if( !v->in(0) && v != cmp ) {
199 tty->print_cr("Phi has free-floating use");
200 v->dump(2);
201 return NULL;
202 }
203 for( uint l = 1; l < v->req(); l++ ) {
204 if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) &&
205 !v->in(l)->is_Con() ) {
206 tty->print_cr("Phi has use");
207 v->dump(2);
208 return NULL;
209 } // End of if Phi-use input is neither Phi nor Constant
210 } // End of for all inputs to Phi-use
211 */
212 } // End of for all uses of Phi
213 } // End of for all uses of Region
214
215 // Only do this if the IF node is in a sane state
216 if (iff->outcnt() != 2)
217 return NULL;
218
219 // Got a hit! Do the Mondo Hack!
220 //
221 //ABC a1c def ghi B 1 e h A C a c d f g i
222 // R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi
223 // cmp - 2 cmp - 2 cmp - 2
224 // bool bool_c bool_x
225 // if if_c if_x
226 // T F T F T F
227 // ..s.. ..t .. ..s.. ..t.. ..s.. ..t..
228 //
229 // Split the paths coming into the merge point into 2 separate groups of
230 // merges. On the left will be all the paths feeding constants into the
231 // Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi
232 // will fold up into a constant; this will let the Cmp fold up as well as
233 // all the control flow. Below the original IF we have 2 control
234 // dependent regions, 's' and 't'. Now we will merge the two paths
235 // just prior to 's' and 't' from the two IFs. At least 1 path (and quite
236 // likely 2 or more) will promptly constant fold away.
237 PhaseGVN *phase = igvn;
238
239 // Make a region merging constants and a region merging the rest
240 uint req_c = 0;
241 for (uint ii = 1; ii < r->req(); ii++) {
242 if (phi->in(ii) == con1) {
243 req_c++;
244 }
245 Node* proj = PhaseIdealLoop::find_predicate(r->in(ii));
246 if (proj != NULL) {
247 return NULL;
248 }
249 }
250
251 // If all the defs of the phi are the same constant, we already have the desired end state.
252 // Skip the split that would create empty phi and region nodes.
253 if((r->req() - req_c) == 1) {
254 return NULL;
255 }
256
257 Node *region_c = new RegionNode(req_c + 1);
258 Node *phi_c = con1;
259 uint len = r->req();
260 Node *region_x = new RegionNode(len - req_c);
261 Node *phi_x = PhiNode::make_blank(region_x, phi);
262 for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) {
263 if (phi->in(i) == con1) {
264 region_c->init_req( i_c++, r ->in(i) );
265 } else {
266 region_x->init_req( i_x, r ->in(i) );
267 phi_x ->init_req( i_x++, phi->in(i) );
268 }
269 }
270
271 // Register the new RegionNodes but do not transform them. Cannot
272 // transform until the entire Region/Phi conglomerate has been hacked
273 // as a single huge transform.
274 igvn->register_new_node_with_optimizer( region_c );
275 igvn->register_new_node_with_optimizer( region_x );
276 // Prevent the untimely death of phi_x. Currently he has no uses. He is
277 // about to get one. If this only use goes away, then phi_x will look dead.
278 // However, he will be picking up some more uses down below.
279 Node *hook = new Node(4);
280 hook->init_req(0, phi_x);
281 hook->init_req(1, phi_c);
282 phi_x = phase->transform( phi_x );
283
284 // Make the compare
285 Node *cmp_c = phase->makecon(t);
286 Node *cmp_x = cmp->clone();
287 cmp_x->set_req(1,phi_x);
288 cmp_x->set_req(2,con2);
289 cmp_x = phase->transform(cmp_x);
290 // Make the bool
291 Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test));
292 Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test));
293 // Make the IfNode
294 IfNode* iff_c = iff->clone()->as_If();
295 iff_c->set_req(0, region_c);
296 iff_c->set_req(1, b_c);
297 igvn->set_type_bottom(iff_c);
298 igvn->_worklist.push(iff_c);
299 hook->init_req(2, iff_c);
300
301 IfNode* iff_x = iff->clone()->as_If();
302 iff_x->set_req(0, region_x);
303 iff_x->set_req(1, b_x);
304 igvn->set_type_bottom(iff_x);
305 igvn->_worklist.push(iff_x);
306 hook->init_req(3, iff_x);
307
308 // Make the true/false arms
309 Node *iff_c_t = phase->transform(new IfTrueNode (iff_c));
310 Node *iff_c_f = phase->transform(new IfFalseNode(iff_c));
311 Node *iff_x_t = phase->transform(new IfTrueNode (iff_x));
312 Node *iff_x_f = phase->transform(new IfFalseNode(iff_x));
313
314 // Merge the TRUE paths
315 Node *region_s = new RegionNode(3);
316 igvn->_worklist.push(region_s);
317 region_s->init_req(1, iff_c_t);
318 region_s->init_req(2, iff_x_t);
319 igvn->register_new_node_with_optimizer( region_s );
320
321 // Merge the FALSE paths
322 Node *region_f = new RegionNode(3);
323 igvn->_worklist.push(region_f);
324 region_f->init_req(1, iff_c_f);
325 region_f->init_req(2, iff_x_f);
326 igvn->register_new_node_with_optimizer( region_f );
327
328 igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table.
329 cmp->set_req(1,NULL); // Whack the inputs to cmp because it will be dead
330 cmp->set_req(2,NULL);
331 // Check for all uses of the Phi and give them a new home.
332 // The 'cmp' got cloned, but CastPP/IIs need to be moved.
333 Node *phi_s = NULL; // do not construct unless needed
334 Node *phi_f = NULL; // do not construct unless needed
335 for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) {
336 Node* v = phi->last_out(i2);// User of the phi
337 igvn->rehash_node_delayed(v); // Have to fixup other Phi users
338 uint vop = v->Opcode();
339 Node *proj = NULL;
340 if( vop == Op_Phi ) { // Remote merge point
341 Node *r = v->in(0);
342 for (uint i3 = 1; i3 < r->req(); i3++)
343 if (r->in(i3) && r->in(i3)->in(0) == iff) {
344 proj = r->in(i3);
345 break;
346 }
347 } else if( v->is_ConstraintCast() ) {
348 proj = v->in(0); // Controlling projection
349 } else {
350 assert( 0, "do not know how to handle this guy" );
351 }
352 guarantee(proj != NULL, "sanity");
353
354 Node *proj_path_data, *proj_path_ctrl;
355 if( proj->Opcode() == Op_IfTrue ) {
356 if( phi_s == NULL ) {
357 // Only construct phi_s if needed, otherwise provides
358 // interfering use.
359 phi_s = PhiNode::make_blank(region_s,phi);
360 phi_s->init_req( 1, phi_c );
361 phi_s->init_req( 2, phi_x );
362 hook->add_req(phi_s);
363 phi_s = phase->transform(phi_s);
364 }
365 proj_path_data = phi_s;
366 proj_path_ctrl = region_s;
367 } else {
368 if( phi_f == NULL ) {
369 // Only construct phi_f if needed, otherwise provides
370 // interfering use.
371 phi_f = PhiNode::make_blank(region_f,phi);
372 phi_f->init_req( 1, phi_c );
373 phi_f->init_req( 2, phi_x );
374 hook->add_req(phi_f);
375 phi_f = phase->transform(phi_f);
376 }
377 proj_path_data = phi_f;
378 proj_path_ctrl = region_f;
379 }
380
381 // Fixup 'v' for for the split
382 if( vop == Op_Phi ) { // Remote merge point
383 uint i;
384 for( i = 1; i < v->req(); i++ )
385 if( v->in(i) == phi )
386 break;
387 v->set_req(i, proj_path_data );
388 } else if( v->is_ConstraintCast() ) {
389 v->set_req(0, proj_path_ctrl );
390 v->set_req(1, proj_path_data );
391 } else
392 ShouldNotReachHere();
393 }
394
395 // Now replace the original iff's True/False with region_s/region_t.
396 // This makes the original iff go dead.
397 for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) {
398 Node* p = iff->last_out(i3);
399 assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" );
400 Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f;
401 // Replace p with u
402 igvn->add_users_to_worklist(p);
403 for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) {
404 Node* x = p->last_out(l);
405 igvn->hash_delete(x);
406 uint uses_found = 0;
407 for( uint j = 0; j < x->req(); j++ ) {
408 if( x->in(j) == p ) {
409 x->set_req(j, u);
410 uses_found++;
411 }
412 }
413 l -= uses_found; // we deleted 1 or more copies of this edge
414 }
415 igvn->remove_dead_node(p);
416 }
417
418 // Force the original merge dead
419 igvn->hash_delete(r);
420 // First, remove region's dead users.
421 for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) {
422 Node* u = r->last_out(l);
423 if( u == r ) {
424 r->set_req(0, NULL);
425 } else {
426 assert(u->outcnt() == 0, "only dead users");
427 igvn->remove_dead_node(u);
428 }
429 l -= 1;
430 }
431 igvn->remove_dead_node(r);
432
433 // Now remove the bogus extra edges used to keep things alive
434 igvn->remove_dead_node( hook );
435
436 // Must return either the original node (now dead) or a new node
437 // (Do not return a top here, since that would break the uniqueness of top.)
438 return new ConINode(TypeInt::ZERO);
439 }
440
441 // if this IfNode follows a range check pattern return the projection
442 // for the failed path
range_check_trap_proj(int & flip_test,Node * & l,Node * & r)443 ProjNode* IfNode::range_check_trap_proj(int& flip_test, Node*& l, Node*& r) {
444 if (outcnt() != 2) {
445 return NULL;
446 }
447 Node* b = in(1);
448 if (b == NULL || !b->is_Bool()) return NULL;
449 BoolNode* bn = b->as_Bool();
450 Node* cmp = bn->in(1);
451 if (cmp == NULL) return NULL;
452 if (cmp->Opcode() != Op_CmpU) return NULL;
453
454 l = cmp->in(1);
455 r = cmp->in(2);
456 flip_test = 1;
457 if (bn->_test._test == BoolTest::le) {
458 l = cmp->in(2);
459 r = cmp->in(1);
460 flip_test = 2;
461 } else if (bn->_test._test != BoolTest::lt) {
462 return NULL;
463 }
464 if (l->is_top()) return NULL; // Top input means dead test
465 if (r->Opcode() != Op_LoadRange && !is_RangeCheck()) return NULL;
466
467 // We have recognized one of these forms:
468 // Flip 1: If (Bool[<] CmpU(l, LoadRange)) ...
469 // Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ...
470
471 ProjNode* iftrap = proj_out_or_null(flip_test == 2 ? true : false);
472 return iftrap;
473 }
474
475
476 //------------------------------is_range_check---------------------------------
477 // Return 0 if not a range check. Return 1 if a range check and set index and
478 // offset. Return 2 if we had to negate the test. Index is NULL if the check
479 // is versus a constant.
is_range_check(Node * & range,Node * & index,jint & offset)480 int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) {
481 int flip_test = 0;
482 Node* l = NULL;
483 Node* r = NULL;
484 ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
485
486 if (iftrap == NULL) {
487 return 0;
488 }
489
490 // Make sure it's a real range check by requiring an uncommon trap
491 // along the OOB path. Otherwise, it's possible that the user wrote
492 // something which optimized to look like a range check but behaves
493 // in some other way.
494 if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == NULL) {
495 return 0;
496 }
497
498 // Look for index+offset form
499 Node* ind = l;
500 jint off = 0;
501 if (l->is_top()) {
502 return 0;
503 } else if (l->Opcode() == Op_AddI) {
504 if ((off = l->in(1)->find_int_con(0)) != 0) {
505 ind = l->in(2)->uncast();
506 } else if ((off = l->in(2)->find_int_con(0)) != 0) {
507 ind = l->in(1)->uncast();
508 }
509 } else if ((off = l->find_int_con(-1)) >= 0) {
510 // constant offset with no variable index
511 ind = NULL;
512 } else {
513 // variable index with no constant offset (or dead negative index)
514 off = 0;
515 }
516
517 // Return all the values:
518 index = ind;
519 offset = off;
520 range = r;
521 return flip_test;
522 }
523
524 //------------------------------adjust_check-----------------------------------
525 // Adjust (widen) a prior range check
adjust_check(Node * proj,Node * range,Node * index,int flip,jint off_lo,PhaseIterGVN * igvn)526 static void adjust_check(Node* proj, Node* range, Node* index,
527 int flip, jint off_lo, PhaseIterGVN* igvn) {
528 PhaseGVN *gvn = igvn;
529 // Break apart the old check
530 Node *iff = proj->in(0);
531 Node *bol = iff->in(1);
532 if( bol->is_top() ) return; // In case a partially dead range check appears
533 // bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode
534 DEBUG_ONLY( if( !bol->is_Bool() ) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } )
535 if( !bol->is_Bool() ) return;
536
537 Node *cmp = bol->in(1);
538 // Compute a new check
539 Node *new_add = gvn->intcon(off_lo);
540 if( index ) {
541 new_add = off_lo ? gvn->transform(new AddINode( index, new_add )) : index;
542 }
543 Node *new_cmp = (flip == 1)
544 ? new CmpUNode( new_add, range )
545 : new CmpUNode( range, new_add );
546 new_cmp = gvn->transform(new_cmp);
547 // See if no need to adjust the existing check
548 if( new_cmp == cmp ) return;
549 // Else, adjust existing check
550 Node *new_bol = gvn->transform( new BoolNode( new_cmp, bol->as_Bool()->_test._test ) );
551 igvn->rehash_node_delayed( iff );
552 iff->set_req_X( 1, new_bol, igvn );
553 }
554
555 //------------------------------up_one_dom-------------------------------------
556 // Walk up the dominator tree one step. Return NULL at root or true
557 // complex merges. Skips through small diamonds.
up_one_dom(Node * curr,bool linear_only)558 Node* IfNode::up_one_dom(Node *curr, bool linear_only) {
559 Node *dom = curr->in(0);
560 if( !dom ) // Found a Region degraded to a copy?
561 return curr->nonnull_req(); // Skip thru it
562
563 if( curr != dom ) // Normal walk up one step?
564 return dom;
565
566 // Use linear_only if we are still parsing, since we cannot
567 // trust the regions to be fully filled in.
568 if (linear_only)
569 return NULL;
570
571 if( dom->is_Root() )
572 return NULL;
573
574 // Else hit a Region. Check for a loop header
575 if( dom->is_Loop() )
576 return dom->in(1); // Skip up thru loops
577
578 // Check for small diamonds
579 Node *din1, *din2, *din3, *din4;
580 if( dom->req() == 3 && // 2-path merge point
581 (din1 = dom ->in(1)) && // Left path exists
582 (din2 = dom ->in(2)) && // Right path exists
583 (din3 = din1->in(0)) && // Left path up one
584 (din4 = din2->in(0)) ) { // Right path up one
585 if( din3->is_Call() && // Handle a slow-path call on either arm
586 (din3 = din3->in(0)) )
587 din3 = din3->in(0);
588 if( din4->is_Call() && // Handle a slow-path call on either arm
589 (din4 = din4->in(0)) )
590 din4 = din4->in(0);
591 if (din3 != NULL && din3 == din4 && din3->is_If()) // Regions not degraded to a copy
592 return din3; // Skip around diamonds
593 }
594
595 // Give up the search at true merges
596 return NULL; // Dead loop? Or hit root?
597 }
598
599
600 //------------------------------filtered_int_type--------------------------------
601 // Return a possibly more restrictive type for val based on condition control flow for an if
filtered_int_type(PhaseGVN * gvn,Node * val,Node * if_proj)602 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node *val, Node* if_proj) {
603 assert(if_proj &&
604 (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");
605 if (if_proj->in(0) && if_proj->in(0)->is_If()) {
606 IfNode* iff = if_proj->in(0)->as_If();
607 if (iff->in(1) && iff->in(1)->is_Bool()) {
608 BoolNode* bol = iff->in(1)->as_Bool();
609 if (bol->in(1) && bol->in(1)->is_Cmp()) {
610 const CmpNode* cmp = bol->in(1)->as_Cmp();
611 if (cmp->in(1) == val) {
612 const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();
613 if (cmp2_t != NULL) {
614 jint lo = cmp2_t->_lo;
615 jint hi = cmp2_t->_hi;
616 BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();
617 switch (msk) {
618 case BoolTest::ne:
619 // Can't refine type
620 return NULL;
621 case BoolTest::eq:
622 return cmp2_t;
623 case BoolTest::lt:
624 lo = TypeInt::INT->_lo;
625 if (hi - 1 < hi) {
626 hi = hi - 1;
627 }
628 break;
629 case BoolTest::le:
630 lo = TypeInt::INT->_lo;
631 break;
632 case BoolTest::gt:
633 if (lo + 1 > lo) {
634 lo = lo + 1;
635 }
636 hi = TypeInt::INT->_hi;
637 break;
638 case BoolTest::ge:
639 // lo unchanged
640 hi = TypeInt::INT->_hi;
641 break;
642 default:
643 break;
644 }
645 const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);
646 return rtn_t;
647 }
648 }
649 }
650 }
651 }
652 return NULL;
653 }
654
655 //------------------------------fold_compares----------------------------
656 // See if a pair of CmpIs can be converted into a CmpU. In some cases
657 // the direction of this if is determined by the preceding if so it
658 // can be eliminate entirely.
659 //
660 // Given an if testing (CmpI n v) check for an immediately control
661 // dependent if that is testing (CmpI n v2) and has one projection
662 // leading to this if and the other projection leading to a region
663 // that merges one of this ifs control projections.
664 //
665 // If
666 // / |
667 // / |
668 // / |
669 // If |
670 // /\ |
671 // / \ |
672 // / \ |
673 // / Region
674 //
675 // Or given an if testing (CmpI n v) check for a dominating if that is
676 // testing (CmpI n v2), both having one projection leading to an
677 // uncommon trap. Allow Another independent guard in between to cover
678 // an explicit range check:
679 // if (index < 0 || index >= array.length) {
680 // which may need a null check to guard the LoadRange
681 //
682 // If
683 // / \
684 // / \
685 // / \
686 // If unc
687 // /\
688 // / \
689 // / \
690 // / unc
691 //
692
693 // Is the comparison for this If suitable for folding?
cmpi_folds(PhaseIterGVN * igvn)694 bool IfNode::cmpi_folds(PhaseIterGVN* igvn) {
695 return in(1) != NULL &&
696 in(1)->is_Bool() &&
697 in(1)->in(1) != NULL &&
698 in(1)->in(1)->Opcode() == Op_CmpI &&
699 in(1)->in(1)->in(2) != NULL &&
700 in(1)->in(1)->in(2) != igvn->C->top() &&
701 (in(1)->as_Bool()->_test.is_less() ||
702 in(1)->as_Bool()->_test.is_greater());
703 }
704
705 // Is a dominating control suitable for folding with this if?
is_ctrl_folds(Node * ctrl,PhaseIterGVN * igvn)706 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
707 return ctrl != NULL &&
708 ctrl->is_Proj() &&
709 ctrl->in(0) != NULL &&
710 ctrl->in(0)->Opcode() == Op_If &&
711 ctrl->in(0)->outcnt() == 2 &&
712 ctrl->in(0)->as_If()->cmpi_folds(igvn) &&
713 // Must compare same value
714 ctrl->in(0)->in(1)->in(1)->in(1) != NULL &&
715 ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
716 }
717
718 // Do this If and the dominating If share a region?
has_shared_region(ProjNode * proj,ProjNode * & success,ProjNode * & fail)719 bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
720 ProjNode* otherproj = proj->other_if_proj();
721 Node* otherproj_ctrl_use = otherproj->unique_ctrl_out();
722 RegionNode* region = (otherproj_ctrl_use != NULL && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : NULL;
723 success = NULL;
724 fail = NULL;
725
726 if (otherproj->outcnt() == 1 && region != NULL && !region->has_phi()) {
727 for (int i = 0; i < 2; i++) {
728 ProjNode* proj = proj_out(i);
729 if (success == NULL && proj->outcnt() == 1 && proj->unique_out() == region) {
730 success = proj;
731 } else if (fail == NULL) {
732 fail = proj;
733 } else {
734 success = fail = NULL;
735 }
736 }
737 }
738 return success != NULL && fail != NULL;
739 }
740
is_dominator_unc(CallStaticJavaNode * dom_unc,CallStaticJavaNode * unc)741 bool IfNode::is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc) {
742 // Different methods and methods containing jsrs are not supported.
743 ciMethod* method = unc->jvms()->method();
744 ciMethod* dom_method = dom_unc->jvms()->method();
745 if (method != dom_method || method->has_jsrs()) {
746 return false;
747 }
748 // Check that both traps are in the same activation of the method (instead
749 // of two activations being inlined through different call sites) by verifying
750 // that the call stacks are equal for both JVMStates.
751 JVMState* dom_caller = dom_unc->jvms()->caller();
752 JVMState* caller = unc->jvms()->caller();
753 if ((dom_caller == NULL) != (caller == NULL)) {
754 // The current method must either be inlined into both dom_caller and
755 // caller or must not be inlined at all (top method). Bail out otherwise.
756 return false;
757 } else if (dom_caller != NULL && !dom_caller->same_calls_as(caller)) {
758 return false;
759 }
760 // Check that the bci of the dominating uncommon trap dominates the bci
761 // of the dominated uncommon trap. Otherwise we may not re-execute
762 // the dominated check after deoptimization from the merged uncommon trap.
763 ciTypeFlow* flow = dom_method->get_flow_analysis();
764 int bci = unc->jvms()->bci();
765 int dom_bci = dom_unc->jvms()->bci();
766 if (!flow->is_dominated_by(bci, dom_bci)) {
767 return false;
768 }
769
770 return true;
771 }
772
773 // Return projection that leads to an uncommon trap if any
uncommon_trap_proj(CallStaticJavaNode * & call) const774 ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call) const {
775 for (int i = 0; i < 2; i++) {
776 call = proj_out(i)->is_uncommon_trap_proj(Deoptimization::Reason_none);
777 if (call != NULL) {
778 return proj_out(i);
779 }
780 }
781 return NULL;
782 }
783
784 // Do this If and the dominating If both branch out to an uncommon trap
has_only_uncommon_traps(ProjNode * proj,ProjNode * & success,ProjNode * & fail,PhaseIterGVN * igvn)785 bool IfNode::has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn) {
786 ProjNode* otherproj = proj->other_if_proj();
787 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
788
789 if (otherproj->outcnt() == 1 && dom_unc != NULL) {
790 // We need to re-execute the folded Ifs after deoptimization from the merged traps
791 if (!dom_unc->jvms()->should_reexecute()) {
792 return false;
793 }
794
795 CallStaticJavaNode* unc = NULL;
796 ProjNode* unc_proj = uncommon_trap_proj(unc);
797 if (unc_proj != NULL && unc_proj->outcnt() == 1) {
798 if (dom_unc == unc) {
799 // Allow the uncommon trap to be shared through a region
800 RegionNode* r = unc->in(0)->as_Region();
801 if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) {
802 return false;
803 }
804 assert(r->has_phi() == NULL, "simple region shouldn't have a phi");
805 } else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) {
806 return false;
807 }
808
809 if (!is_dominator_unc(dom_unc, unc)) {
810 return false;
811 }
812
813 // See merge_uncommon_traps: the reason of the uncommon trap
814 // will be changed and the state of the dominating If will be
815 // used. Checked that we didn't apply this transformation in a
816 // previous compilation and it didn't cause too many traps
817 ciMethod* dom_method = dom_unc->jvms()->method();
818 int dom_bci = dom_unc->jvms()->bci();
819 if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) &&
820 !igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check)) {
821 success = unc_proj;
822 fail = unc_proj->other_if_proj();
823 return true;
824 }
825 }
826 }
827 return false;
828 }
829
830 // Check that the 2 CmpI can be folded into as single CmpU and proceed with the folding
fold_compares_helper(ProjNode * proj,ProjNode * success,ProjNode * fail,PhaseIterGVN * igvn)831 bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
832 Node* this_cmp = in(1)->in(1);
833 BoolNode* this_bool = in(1)->as_Bool();
834 IfNode* dom_iff = proj->in(0)->as_If();
835 BoolNode* dom_bool = dom_iff->in(1)->as_Bool();
836 Node* lo = dom_iff->in(1)->in(1)->in(2);
837 Node* hi = this_cmp->in(2);
838 Node* n = this_cmp->in(1);
839 ProjNode* otherproj = proj->other_if_proj();
840
841 const TypeInt* lo_type = IfNode::filtered_int_type(igvn, n, otherproj);
842 const TypeInt* hi_type = IfNode::filtered_int_type(igvn, n, success);
843
844 BoolTest::mask lo_test = dom_bool->_test._test;
845 BoolTest::mask hi_test = this_bool->_test._test;
846 BoolTest::mask cond = hi_test;
847
848 // convert:
849 //
850 // dom_bool = x {<,<=,>,>=} a
851 // / \
852 // proj = {True,False} / \ otherproj = {False,True}
853 // /
854 // this_bool = x {<,<=} b
855 // / \
856 // fail = {True,False} / \ success = {False,True}
857 // /
858 //
859 // (Second test guaranteed canonicalized, first one may not have
860 // been canonicalized yet)
861 //
862 // into:
863 //
864 // cond = (x - lo) {<u,<=u,>u,>=u} adjusted_lim
865 // / \
866 // fail / \ success
867 // /
868 //
869
870 // Figure out which of the two tests sets the upper bound and which
871 // sets the lower bound if any.
872 Node* adjusted_lim = NULL;
873 if (lo_type != NULL && hi_type != NULL && hi_type->_lo > lo_type->_hi &&
874 hi_type->_hi == max_jint && lo_type->_lo == min_jint) {
875 assert((dom_bool->_test.is_less() && !proj->_con) ||
876 (dom_bool->_test.is_greater() && proj->_con), "incorrect test");
877 // this test was canonicalized
878 assert(this_bool->_test.is_less() && fail->_con, "incorrect test");
879
880 // this_bool = <
881 // dom_bool = >= (proj = True) or dom_bool = < (proj = False)
882 // x in [a, b[ on the fail (= True) projection, b > a-1 (because of hi_type->_lo > lo_type->_hi test above):
883 // lo = a, hi = b, adjusted_lim = b-a, cond = <u
884 // dom_bool = > (proj = True) or dom_bool = <= (proj = False)
885 // x in ]a, b[ on the fail (= True) projection, b > a:
886 // lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <u
887 // this_bool = <=
888 // dom_bool = >= (proj = True) or dom_bool = < (proj = False)
889 // x in [a, b] on the fail (= True) projection, b+1 > a-1:
890 // lo = a, hi = b, adjusted_lim = b-a+1, cond = <u
891 // lo = a, hi = b, adjusted_lim = b-a, cond = <=u doesn't work because b = a - 1 is possible, then b-a = -1
892 // dom_bool = > (proj = True) or dom_bool = <= (proj = False)
893 // x in ]a, b] on the fail (= True) projection b+1 > a:
894 // lo = a+1, hi = b, adjusted_lim = b-a, cond = <u
895 // lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <=u doesn't work because a = b is possible, then b-a-1 = -1
896
897 if (hi_test == BoolTest::lt) {
898 if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
899 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
900 }
901 } else {
902 assert(hi_test == BoolTest::le, "bad test");
903 if (lo_test == BoolTest::ge || lo_test == BoolTest::lt) {
904 adjusted_lim = igvn->transform(new SubINode(hi, lo));
905 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1)));
906 cond = BoolTest::lt;
907 } else {
908 assert(lo_test == BoolTest::gt || lo_test == BoolTest::le, "bad test");
909 adjusted_lim = igvn->transform(new SubINode(hi, lo));
910 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
911 cond = BoolTest::lt;
912 }
913 }
914 } else if (lo_type != NULL && hi_type != NULL && lo_type->_lo > hi_type->_hi &&
915 lo_type->_hi == max_jint && hi_type->_lo == min_jint) {
916
917 // this_bool = <
918 // dom_bool = < (proj = True) or dom_bool = >= (proj = False)
919 // x in [b, a[ on the fail (= False) projection, a > b-1 (because of lo_type->_lo > hi_type->_hi above):
920 // lo = b, hi = a, adjusted_lim = a-b, cond = >=u
921 // dom_bool = <= (proj = True) or dom_bool = > (proj = False)
922 // x in [b, a] on the fail (= False) projection, a+1 > b-1:
923 // lo = b, hi = a, adjusted_lim = a-b+1, cond = >=u
924 // lo = b, hi = a, adjusted_lim = a-b, cond = >u doesn't work because a = b - 1 is possible, then b-a = -1
925 // this_bool = <=
926 // dom_bool = < (proj = True) or dom_bool = >= (proj = False)
927 // x in ]b, a[ on the fail (= False) projection, a > b:
928 // lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >=u
929 // dom_bool = <= (proj = True) or dom_bool = > (proj = False)
930 // x in ]b, a] on the fail (= False) projection, a+1 > b:
931 // lo = b+1, hi = a, adjusted_lim = a-b, cond = >=u
932 // lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >u doesn't work because a = b is possible, then b-a-1 = -1
933
934 swap(lo, hi);
935 swap(lo_type, hi_type);
936 swap(lo_test, hi_test);
937
938 assert((dom_bool->_test.is_less() && proj->_con) ||
939 (dom_bool->_test.is_greater() && !proj->_con), "incorrect test");
940 // this test was canonicalized
941 assert(this_bool->_test.is_less() && !fail->_con, "incorrect test");
942
943 cond = (hi_test == BoolTest::le || hi_test == BoolTest::gt) ? BoolTest::gt : BoolTest::ge;
944
945 if (lo_test == BoolTest::lt) {
946 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
947 cond = BoolTest::ge;
948 } else {
949 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test");
950 adjusted_lim = igvn->transform(new SubINode(hi, lo));
951 adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1)));
952 cond = BoolTest::ge;
953 }
954 } else if (lo_test == BoolTest::le) {
955 if (hi_test == BoolTest::lt || hi_test == BoolTest::ge) {
956 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
957 cond = BoolTest::ge;
958 } else {
959 assert(hi_test == BoolTest::le || hi_test == BoolTest::gt, "bad test");
960 adjusted_lim = igvn->transform(new SubINode(hi, lo));
961 lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
962 cond = BoolTest::ge;
963 }
964 }
965 } else {
966 const TypeInt* failtype = filtered_int_type(igvn, n, proj);
967 if (failtype != NULL) {
968 const TypeInt* type2 = filtered_int_type(igvn, n, fail);
969 if (type2 != NULL) {
970 failtype = failtype->join(type2)->is_int();
971 if (failtype->_lo > failtype->_hi) {
972 // previous if determines the result of this if so
973 // replace Bool with constant
974 igvn->_worklist.push(in(1));
975 igvn->replace_input_of(this, 1, igvn->intcon(success->_con));
976 return true;
977 }
978 }
979 }
980 lo = NULL;
981 hi = NULL;
982 }
983
984 if (lo && hi) {
985 Node* hook = new Node(1);
986 hook->init_req(0, lo); // Add a use to lo to prevent him from dying
987 // Merge the two compares into a single unsigned compare by building (CmpU (n - lo) (hi - lo))
988 Node* adjusted_val = igvn->transform(new SubINode(n, lo));
989 if (adjusted_lim == NULL) {
990 adjusted_lim = igvn->transform(new SubINode(hi, lo));
991 }
992 hook->del_req(0); // Just yank bogus edge
993 hook->destruct();
994 Node* newcmp = igvn->transform(new CmpUNode(adjusted_val, adjusted_lim));
995 Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
996
997 igvn->replace_input_of(dom_iff, 1, igvn->intcon(proj->_con));
998 igvn->_worklist.push(in(1));
999 igvn->replace_input_of(this, 1, newbool);
1000
1001 return true;
1002 }
1003 return false;
1004 }
1005
1006 // Merge the branches that trap for this If and the dominating If into
1007 // a single region that branches to the uncommon trap for the
1008 // dominating If
merge_uncommon_traps(ProjNode * proj,ProjNode * success,ProjNode * fail,PhaseIterGVN * igvn)1009 Node* IfNode::merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
1010 Node* res = this;
1011 assert(success->in(0) == this, "bad projection");
1012
1013 ProjNode* otherproj = proj->other_if_proj();
1014
1015 CallStaticJavaNode* unc = success->is_uncommon_trap_proj(Deoptimization::Reason_none);
1016 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj(Deoptimization::Reason_none);
1017
1018 if (unc != dom_unc) {
1019 Node* r = new RegionNode(3);
1020
1021 r->set_req(1, otherproj);
1022 r->set_req(2, success);
1023 r = igvn->transform(r);
1024 assert(r->is_Region(), "can't go away");
1025
1026 // Make both If trap at the state of the first If: once the CmpI
1027 // nodes are merged, if we trap we don't know which of the CmpI
1028 // nodes would have caused the trap so we have to restart
1029 // execution at the first one
1030 igvn->replace_input_of(dom_unc, 0, r);
1031 igvn->replace_input_of(unc, 0, igvn->C->top());
1032 }
1033 int trap_request = dom_unc->uncommon_trap_request();
1034 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1035 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
1036
1037 int flip_test = 0;
1038 Node* l = NULL;
1039 Node* r = NULL;
1040
1041 if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != NULL) {
1042 // If this looks like a range check, change the trap to
1043 // Reason_range_check so the compiler recognizes it as a range
1044 // check and applies the corresponding optimizations
1045 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1046
1047 improve_address_types(l, r, fail, igvn);
1048
1049 res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt));
1050 } else if (unc != dom_unc) {
1051 // If we trap we won't know what CmpI would have caused the trap
1052 // so use a special trap reason to mark this pair of CmpI nodes as
1053 // bad candidate for folding. On recompilation we won't fold them
1054 // and we may trap again but this time we'll know what branch
1055 // traps
1056 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1057 }
1058 igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
1059 return res;
1060 }
1061
1062 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1063 // of a rangecheck on index i, on 64 bit the compares may be followed
1064 // by memory accesses using i as index. In that case, the CmpU tells
1065 // us something about the values taken by i that can help the compiler
1066 // (see Compile::conv_I2X_index())
improve_address_types(Node * l,Node * r,ProjNode * fail,PhaseIterGVN * igvn)1067 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1068 #ifdef _LP64
1069 ResourceMark rm;
1070 Node_Stack stack(2);
1071
1072 assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1073 const TypeInt* array_size = igvn->type(r)->is_int();
1074
1075 stack.push(l, 0);
1076
1077 while(stack.size() > 0) {
1078 Node* n = stack.node();
1079 uint start = stack.index();
1080
1081 uint i = start;
1082 for (; i < n->outcnt(); i++) {
1083 Node* use = n->raw_out(i);
1084 if (stack.size() == 1) {
1085 if (use->Opcode() == Op_ConvI2L) {
1086 const TypeLong* bounds = use->as_Type()->type()->is_long();
1087 if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi &&
1088 (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) {
1089 stack.set_index(i+1);
1090 stack.push(use, 0);
1091 break;
1092 }
1093 }
1094 } else if (use->is_Mem()) {
1095 Node* ctrl = use->in(0);
1096 for (int i = 0; i < 10 && ctrl != NULL && ctrl != fail; i++) {
1097 ctrl = up_one_dom(ctrl);
1098 }
1099 if (ctrl == fail) {
1100 Node* init_n = stack.node_at(1);
1101 assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node");
1102 // Create a new narrow ConvI2L node that is dependent on the range check
1103 Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size, fail);
1104
1105 // The type of the ConvI2L may be widen and so the new
1106 // ConvI2L may not be better than an existing ConvI2L
1107 if (new_n != init_n) {
1108 for (uint j = 2; j < stack.size(); j++) {
1109 Node* n = stack.node_at(j);
1110 Node* clone = n->clone();
1111 int rep = clone->replace_edge(init_n, new_n);
1112 assert(rep > 0, "can't find expected node?");
1113 clone = igvn->transform(clone);
1114 init_n = n;
1115 new_n = clone;
1116 }
1117 igvn->hash_delete(use);
1118 int rep = use->replace_edge(init_n, new_n);
1119 assert(rep > 0, "can't find expected node?");
1120 igvn->transform(use);
1121 if (init_n->outcnt() == 0) {
1122 igvn->_worklist.push(init_n);
1123 }
1124 }
1125 }
1126 } else if (use->in(0) == NULL && (igvn->type(use)->isa_long() ||
1127 igvn->type(use)->isa_ptr())) {
1128 stack.set_index(i+1);
1129 stack.push(use, 0);
1130 break;
1131 }
1132 }
1133 if (i == n->outcnt()) {
1134 stack.pop();
1135 }
1136 }
1137 #endif
1138 }
1139
is_cmp_with_loadrange(ProjNode * proj)1140 bool IfNode::is_cmp_with_loadrange(ProjNode* proj) {
1141 if (in(1) != NULL &&
1142 in(1)->in(1) != NULL &&
1143 in(1)->in(1)->in(2) != NULL) {
1144 Node* other = in(1)->in(1)->in(2);
1145 if (other->Opcode() == Op_LoadRange &&
1146 ((other->in(0) != NULL && other->in(0) == proj) ||
1147 (other->in(0) == NULL &&
1148 other->in(2) != NULL &&
1149 other->in(2)->is_AddP() &&
1150 other->in(2)->in(1) != NULL &&
1151 other->in(2)->in(1)->Opcode() == Op_CastPP &&
1152 other->in(2)->in(1)->in(0) == proj))) {
1153 return true;
1154 }
1155 }
1156 return false;
1157 }
1158
is_null_check(ProjNode * proj,PhaseIterGVN * igvn)1159 bool IfNode::is_null_check(ProjNode* proj, PhaseIterGVN* igvn) {
1160 Node* other = in(1)->in(1)->in(2);
1161 if (other->in(MemNode::Address) != NULL &&
1162 proj->in(0)->in(1) != NULL &&
1163 proj->in(0)->in(1)->is_Bool() &&
1164 proj->in(0)->in(1)->in(1) != NULL &&
1165 proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP &&
1166 proj->in(0)->in(1)->in(1)->in(2) != NULL &&
1167 proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() &&
1168 igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) {
1169 return true;
1170 }
1171 return false;
1172 }
1173
1174 // Check that the If that is in between the 2 integer comparisons has
1175 // no side effect
is_side_effect_free_test(ProjNode * proj,PhaseIterGVN * igvn)1176 bool IfNode::is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn) {
1177 if (proj == NULL) {
1178 return false;
1179 }
1180 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1181 if (unc != NULL && proj->outcnt() <= 2) {
1182 if (proj->outcnt() == 1 ||
1183 // Allow simple null check from LoadRange
1184 (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) {
1185 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1186 CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1187 assert(dom_unc != NULL, "is_uncommon_trap_if_pattern returned NULL");
1188
1189 // reroute_side_effect_free_unc changes the state of this
1190 // uncommon trap to restart execution at the previous
1191 // CmpI. Check that this change in a previous compilation didn't
1192 // cause too many traps.
1193 int trap_request = unc->uncommon_trap_request();
1194 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1195
1196 if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) {
1197 return false;
1198 }
1199
1200 if (!is_dominator_unc(dom_unc, unc)) {
1201 return false;
1202 }
1203
1204 return true;
1205 }
1206 }
1207 return false;
1208 }
1209
1210 // Make the If between the 2 integer comparisons trap at the state of
1211 // the first If: the last CmpI is the one replaced by a CmpU and the
1212 // first CmpI is eliminated, so the test between the 2 CmpI nodes
1213 // won't be guarded by the first CmpI anymore. It can trap in cases
1214 // where the first CmpI would have prevented it from executing: on a
1215 // trap, we need to restart execution at the state of the first CmpI
reroute_side_effect_free_unc(ProjNode * proj,ProjNode * dom_proj,PhaseIterGVN * igvn)1216 void IfNode::reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn) {
1217 CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1218 ProjNode* otherproj = proj->other_if_proj();
1219 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1220 Node* call_proj = dom_unc->unique_ctrl_out();
1221 Node* halt = call_proj->unique_ctrl_out();
1222
1223 Node* new_unc = dom_unc->clone();
1224 call_proj = call_proj->clone();
1225 halt = halt->clone();
1226 Node* c = otherproj->clone();
1227
1228 c = igvn->transform(c);
1229 new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms));
1230 new_unc->set_req(0, c);
1231 new_unc = igvn->transform(new_unc);
1232 call_proj->set_req(0, new_unc);
1233 call_proj = igvn->transform(call_proj);
1234 halt->set_req(0, call_proj);
1235 halt = igvn->transform(halt);
1236
1237 igvn->replace_node(otherproj, igvn->C->top());
1238 igvn->C->root()->add_req(halt);
1239 }
1240
fold_compares(PhaseIterGVN * igvn)1241 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1242 if (Opcode() != Op_If) return NULL;
1243
1244 if (cmpi_folds(igvn)) {
1245 Node* ctrl = in(0);
1246 if (is_ctrl_folds(ctrl, igvn) &&
1247 ctrl->outcnt() == 1) {
1248 // A integer comparison immediately dominated by another integer
1249 // comparison
1250 ProjNode* success = NULL;
1251 ProjNode* fail = NULL;
1252 ProjNode* dom_cmp = ctrl->as_Proj();
1253 if (has_shared_region(dom_cmp, success, fail) &&
1254 // Next call modifies graph so must be last
1255 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1256 return this;
1257 }
1258 if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1259 // Next call modifies graph so must be last
1260 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1261 return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1262 }
1263 return NULL;
1264 } else if (ctrl->in(0) != NULL &&
1265 ctrl->in(0)->in(0) != NULL) {
1266 ProjNode* success = NULL;
1267 ProjNode* fail = NULL;
1268 Node* dom = ctrl->in(0)->in(0);
1269 ProjNode* dom_cmp = dom->isa_Proj();
1270 ProjNode* other_cmp = ctrl->isa_Proj();
1271
1272 // Check if it's an integer comparison dominated by another
1273 // integer comparison with another test in between
1274 if (is_ctrl_folds(dom, igvn) &&
1275 has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1276 is_side_effect_free_test(other_cmp, igvn) &&
1277 // Next call modifies graph so must be last
1278 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1279 reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1280 return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1281 }
1282 }
1283 }
1284 return NULL;
1285 }
1286
1287 //------------------------------remove_useless_bool----------------------------
1288 // Check for people making a useless boolean: things like
1289 // if( (x < y ? true : false) ) { ... }
1290 // Replace with if( x < y ) { ... }
remove_useless_bool(IfNode * iff,PhaseGVN * phase)1291 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1292 Node *i1 = iff->in(1);
1293 if( !i1->is_Bool() ) return NULL;
1294 BoolNode *bol = i1->as_Bool();
1295
1296 Node *cmp = bol->in(1);
1297 if( cmp->Opcode() != Op_CmpI ) return NULL;
1298
1299 // Must be comparing against a bool
1300 const Type *cmp2_t = phase->type( cmp->in(2) );
1301 if( cmp2_t != TypeInt::ZERO &&
1302 cmp2_t != TypeInt::ONE )
1303 return NULL;
1304
1305 // Find a prior merge point merging the boolean
1306 i1 = cmp->in(1);
1307 if( !i1->is_Phi() ) return NULL;
1308 PhiNode *phi = i1->as_Phi();
1309 if( phase->type( phi ) != TypeInt::BOOL )
1310 return NULL;
1311
1312 // Check for diamond pattern
1313 int true_path = phi->is_diamond_phi();
1314 if( true_path == 0 ) return NULL;
1315
1316 // Make sure that iff and the control of the phi are different. This
1317 // should really only happen for dead control flow since it requires
1318 // an illegal cycle.
1319 if (phi->in(0)->in(1)->in(0) == iff) return NULL;
1320
1321 // phi->region->if_proj->ifnode->bool->cmp
1322 BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
1323
1324 // Now get the 'sense' of the test correct so we can plug in
1325 // either iff2->in(1) or its complement.
1326 int flip = 0;
1327 if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
1328 else if( bol->_test._test != BoolTest::eq ) return NULL;
1329 if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
1330
1331 const Type *phi1_t = phase->type( phi->in(1) );
1332 const Type *phi2_t = phase->type( phi->in(2) );
1333 // Check for Phi(0,1) and flip
1334 if( phi1_t == TypeInt::ZERO ) {
1335 if( phi2_t != TypeInt::ONE ) return NULL;
1336 flip = 1-flip;
1337 } else {
1338 // Check for Phi(1,0)
1339 if( phi1_t != TypeInt::ONE ) return NULL;
1340 if( phi2_t != TypeInt::ZERO ) return NULL;
1341 }
1342 if( true_path == 2 ) {
1343 flip = 1-flip;
1344 }
1345
1346 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1347 assert(new_bol != iff->in(1), "must make progress");
1348 iff->set_req(1, new_bol);
1349 // Intervening diamond probably goes dead
1350 phase->C->set_major_progress();
1351 return iff;
1352 }
1353
1354 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1355
1356 struct RangeCheck {
1357 Node* ctl;
1358 jint off;
1359 };
1360
Ideal_common(PhaseGVN * phase,bool can_reshape)1361 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
1362 if (remove_dead_region(phase, can_reshape)) return this;
1363 // No Def-Use info?
1364 if (!can_reshape) return NULL;
1365
1366 // Don't bother trying to transform a dead if
1367 if (in(0)->is_top()) return NULL;
1368 // Don't bother trying to transform an if with a dead test
1369 if (in(1)->is_top()) return NULL;
1370 // Another variation of a dead test
1371 if (in(1)->is_Con()) return NULL;
1372 // Another variation of a dead if
1373 if (outcnt() < 2) return NULL;
1374
1375 // Canonicalize the test.
1376 Node* idt_if = idealize_test(phase, this);
1377 if (idt_if != NULL) return idt_if;
1378
1379 // Try to split the IF
1380 PhaseIterGVN *igvn = phase->is_IterGVN();
1381 Node *s = split_if(this, igvn);
1382 if (s != NULL) return s;
1383
1384 return NodeSentinel;
1385 }
1386
1387 //------------------------------Ideal------------------------------------------
1388 // Return a node which is more "ideal" than the current node. Strip out
1389 // control copies
Ideal(PhaseGVN * phase,bool can_reshape)1390 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1391 Node* res = Ideal_common(phase, can_reshape);
1392 if (res != NodeSentinel) {
1393 return res;
1394 }
1395
1396 // Check for people making a useless boolean: things like
1397 // if( (x < y ? true : false) ) { ... }
1398 // Replace with if( x < y ) { ... }
1399 Node *bol2 = remove_useless_bool(this, phase);
1400 if( bol2 ) return bol2;
1401
1402 if (in(0) == NULL) return NULL; // Dead loop?
1403
1404 PhaseIterGVN *igvn = phase->is_IterGVN();
1405 Node* result = fold_compares(igvn);
1406 if (result != NULL) {
1407 return result;
1408 }
1409
1410 // Scan for an equivalent test
1411 Node *cmp;
1412 int dist = 0; // Cutoff limit for search
1413 int op = Opcode();
1414 if( op == Op_If &&
1415 (cmp=in(1)->in(1))->Opcode() == Op_CmpP ) {
1416 if( cmp->in(2) != NULL && // make sure cmp is not already dead
1417 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR ) {
1418 dist = 64; // Limit for null-pointer scans
1419 } else {
1420 dist = 4; // Do not bother for random pointer tests
1421 }
1422 } else {
1423 dist = 4; // Limit for random junky scans
1424 }
1425
1426 Node* prev_dom = search_identical(dist);
1427
1428 if (prev_dom == NULL) {
1429 return NULL;
1430 }
1431
1432 // Replace dominated IfNode
1433 return dominated_by(prev_dom, igvn);
1434 }
1435
1436 //------------------------------dominated_by-----------------------------------
dominated_by(Node * prev_dom,PhaseIterGVN * igvn)1437 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN *igvn) {
1438 #ifndef PRODUCT
1439 if (TraceIterativeGVN) {
1440 tty->print(" Removing IfNode: "); this->dump();
1441 }
1442 if (VerifyOpto && !igvn->allow_progress()) {
1443 // Found an equivalent dominating test,
1444 // we can not guarantee reaching a fix-point for these during iterativeGVN
1445 // since intervening nodes may not change.
1446 return NULL;
1447 }
1448 #endif
1449
1450 igvn->hash_delete(this); // Remove self to prevent spurious V-N
1451 Node *idom = in(0);
1452 // Need opcode to decide which way 'this' test goes
1453 int prev_op = prev_dom->Opcode();
1454 Node *top = igvn->C->top(); // Shortcut to top
1455
1456 // Loop predicates may have depending checks which should not
1457 // be skipped. For example, range check predicate has two checks
1458 // for lower and upper bounds.
1459 ProjNode* unc_proj = proj_out(1 - prev_dom->as_Proj()->_con)->as_Proj();
1460 if (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL ||
1461 unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_profile_predicate) != NULL) {
1462 prev_dom = idom;
1463 }
1464
1465 // Now walk the current IfNode's projections.
1466 // Loop ends when 'this' has no more uses.
1467 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1468 Node *ifp = last_out(i); // Get IfTrue/IfFalse
1469 igvn->add_users_to_worklist(ifp);
1470 // Check which projection it is and set target.
1471 // Data-target is either the dominating projection of the same type
1472 // or TOP if the dominating projection is of opposite type.
1473 // Data-target will be used as the new control edge for the non-CFG
1474 // nodes like Casts and Loads.
1475 Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
1476 // Control-target is just the If's immediate dominator or TOP.
1477 Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top;
1478
1479 // For each child of an IfTrue/IfFalse projection, reroute.
1480 // Loop ends when projection has no more uses.
1481 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1482 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse
1483 if( !s->depends_only_on_test() ) {
1484 // Find the control input matching this def-use edge.
1485 // For Regions it may not be in slot 0.
1486 uint l;
1487 for( l = 0; s->in(l) != ifp; l++ ) { }
1488 igvn->replace_input_of(s, l, ctrl_target);
1489 } else { // Else, for control producers,
1490 igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1491 }
1492 } // End for each child of a projection
1493
1494 igvn->remove_dead_node(ifp);
1495 } // End for each IfTrue/IfFalse child of If
1496
1497 // Kill the IfNode
1498 igvn->remove_dead_node(this);
1499
1500 // Must return either the original node (now dead) or a new node
1501 // (Do not return a top here, since that would break the uniqueness of top.)
1502 return new ConINode(TypeInt::ZERO);
1503 }
1504
search_identical(int dist)1505 Node* IfNode::search_identical(int dist) {
1506 // Setup to scan up the CFG looking for a dominating test
1507 Node* dom = in(0);
1508 Node* prev_dom = this;
1509 int op = Opcode();
1510 // Search up the dominator tree for an If with an identical test
1511 while (dom->Opcode() != op || // Not same opcode?
1512 dom->in(1) != in(1) || // Not same input 1?
1513 prev_dom->in(0) != dom) { // One path of test does not dominate?
1514 if (dist < 0) return NULL;
1515
1516 dist--;
1517 prev_dom = dom;
1518 dom = up_one_dom(dom);
1519 if (!dom) return NULL;
1520 }
1521
1522 // Check that we did not follow a loop back to ourselves
1523 if (this == dom) {
1524 return NULL;
1525 }
1526
1527 #ifndef PRODUCT
1528 if (dist > 2) { // Add to count of NULL checks elided
1529 explicit_null_checks_elided++;
1530 }
1531 #endif
1532
1533 return prev_dom;
1534 }
1535
1536 //------------------------------Identity---------------------------------------
1537 // If the test is constant & we match, then we are the input Control
Identity(PhaseGVN * phase)1538 Node* IfProjNode::Identity(PhaseGVN* phase) {
1539 // Can only optimize if cannot go the other way
1540 const TypeTuple *t = phase->type(in(0))->is_tuple();
1541 if (t == TypeTuple::IFNEITHER || (always_taken(t) &&
1542 // During parsing (GVN) we don't remove dead code aggressively.
1543 // Cut off dead branch and let PhaseRemoveUseless take care of it.
1544 (!phase->is_IterGVN() ||
1545 // During IGVN, first wait for the dead branch to be killed.
1546 // Otherwise, the IfNode's control will have two control uses (the IfNode
1547 // that doesn't go away because it still has uses and this branch of the
1548 // If) which breaks other optimizations. Node::has_special_unique_user()
1549 // will cause this node to be reprocessed once the dead branch is killed.
1550 in(0)->outcnt() == 1))) {
1551 // IfNode control
1552 return in(0)->in(0);
1553 }
1554 // no progress
1555 return this;
1556 }
1557
1558 #ifndef PRODUCT
1559 //-------------------------------related---------------------------------------
1560 // An IfProjNode's related node set consists of its input (an IfNode) including
1561 // the IfNode's condition, plus all of its outputs at level 1. In compact mode,
1562 // the restrictions for IfNode apply (see IfNode::rel).
related(GrowableArray<Node * > * in_rel,GrowableArray<Node * > * out_rel,bool compact) const1563 void IfProjNode::related(GrowableArray<Node*> *in_rel, GrowableArray<Node*> *out_rel, bool compact) const {
1564 Node* ifNode = this->in(0);
1565 in_rel->append(ifNode);
1566 if (compact) {
1567 ifNode->collect_nodes(in_rel, 3, false, true);
1568 } else {
1569 ifNode->collect_nodes_in_all_data(in_rel, false);
1570 }
1571 this->collect_nodes(out_rel, -1, false, false);
1572 }
1573
1574 //------------------------------dump_spec--------------------------------------
dump_spec(outputStream * st) const1575 void IfNode::dump_spec(outputStream *st) const {
1576 st->print("P=%f, C=%f",_prob,_fcnt);
1577 }
1578
1579 //-------------------------------related---------------------------------------
1580 // For an IfNode, the set of related output nodes is just the output nodes till
1581 // depth 2, i.e, the IfTrue/IfFalse projection nodes plus the nodes they refer.
1582 // The related input nodes contain no control nodes, but all data nodes
1583 // pertaining to the condition. In compact mode, the input nodes are collected
1584 // up to a depth of 3.
related(GrowableArray<Node * > * in_rel,GrowableArray<Node * > * out_rel,bool compact) const1585 void IfNode::related(GrowableArray <Node *> *in_rel, GrowableArray <Node *> *out_rel, bool compact) const {
1586 if (compact) {
1587 this->collect_nodes(in_rel, 3, false, true);
1588 } else {
1589 this->collect_nodes_in_all_data(in_rel, false);
1590 }
1591 this->collect_nodes(out_rel, -2, false, false);
1592 }
1593 #endif
1594
1595 //------------------------------idealize_test----------------------------------
1596 // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and
1597 // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms
1598 // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as
1599 // needed.
idealize_test(PhaseGVN * phase,IfNode * iff)1600 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
1601 assert(iff->in(0) != NULL, "If must be live");
1602
1603 if (iff->outcnt() != 2) return NULL; // Malformed projections.
1604 Node* old_if_f = iff->proj_out(false);
1605 Node* old_if_t = iff->proj_out(true);
1606
1607 // CountedLoopEnds want the back-control test to be TRUE, irregardless of
1608 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition
1609 // happens in count-down loops
1610 if (iff->is_CountedLoopEnd()) return NULL;
1611 if (!iff->in(1)->is_Bool()) return NULL; // Happens for partially optimized IF tests
1612 BoolNode *b = iff->in(1)->as_Bool();
1613 BoolTest bt = b->_test;
1614 // Test already in good order?
1615 if( bt.is_canonical() )
1616 return NULL;
1617
1618 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and
1619 // cloning the IfNode.
1620 Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
1621 if( !new_b->is_Bool() ) return NULL;
1622 b = new_b->as_Bool();
1623
1624 PhaseIterGVN *igvn = phase->is_IterGVN();
1625 assert( igvn, "Test is not canonical in parser?" );
1626
1627 // The IF node never really changes, but it needs to be cloned
1628 iff = iff->clone()->as_If();
1629 iff->set_req(1, b);
1630 iff->_prob = 1.0-iff->_prob;
1631
1632 Node *prior = igvn->hash_find_insert(iff);
1633 if( prior ) {
1634 igvn->remove_dead_node(iff);
1635 iff = (IfNode*)prior;
1636 } else {
1637 // Cannot call transform on it just yet
1638 igvn->set_type_bottom(iff);
1639 }
1640 igvn->_worklist.push(iff);
1641
1642 // Now handle projections. Cloning not required.
1643 Node* new_if_f = (Node*)(new IfFalseNode( iff ));
1644 Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
1645
1646 igvn->register_new_node_with_optimizer(new_if_f);
1647 igvn->register_new_node_with_optimizer(new_if_t);
1648 // Flip test, so flip trailing control
1649 igvn->replace_node(old_if_f, new_if_t);
1650 igvn->replace_node(old_if_t, new_if_f);
1651
1652 // Progress
1653 return iff;
1654 }
1655
Ideal(PhaseGVN * phase,bool can_reshape)1656 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1657 Node* res = Ideal_common(phase, can_reshape);
1658 if (res != NodeSentinel) {
1659 return res;
1660 }
1661
1662 PhaseIterGVN *igvn = phase->is_IterGVN();
1663 // Setup to scan up the CFG looking for a dominating test
1664 Node* prev_dom = this;
1665
1666 // Check for range-check vs other kinds of tests
1667 Node* index1;
1668 Node* range1;
1669 jint offset1;
1670 int flip1 = is_range_check(range1, index1, offset1);
1671 if (flip1) {
1672 Node* dom = in(0);
1673 // Try to remove extra range checks. All 'up_one_dom' gives up at merges
1674 // so all checks we inspect post-dominate the top-most check we find.
1675 // If we are going to fail the current check and we reach the top check
1676 // then we are guaranteed to fail, so just start interpreting there.
1677 // We 'expand' the top 3 range checks to include all post-dominating
1678 // checks.
1679
1680 // The top 3 range checks seen
1681 const int NRC =3;
1682 RangeCheck prev_checks[NRC];
1683 int nb_checks = 0;
1684
1685 // Low and high offsets seen so far
1686 jint off_lo = offset1;
1687 jint off_hi = offset1;
1688
1689 bool found_immediate_dominator = false;
1690
1691 // Scan for the top checks and collect range of offsets
1692 for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
1693 if (dom->Opcode() == Op_RangeCheck && // Not same opcode?
1694 prev_dom->in(0) == dom) { // One path of test does dominate?
1695 if (dom == this) return NULL; // dead loop
1696 // See if this is a range check
1697 Node* index2;
1698 Node* range2;
1699 jint offset2;
1700 int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
1701 // See if this is a _matching_ range check, checking against
1702 // the same array bounds.
1703 if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
1704 dom->outcnt() == 2) {
1705 if (nb_checks == 0 && dom->in(1) == in(1)) {
1706 // Found an immediately dominating test at the same offset.
1707 // This kind of back-to-back test can be eliminated locally,
1708 // and there is no need to search further for dominating tests.
1709 assert(offset2 == offset1, "Same test but different offsets");
1710 found_immediate_dominator = true;
1711 break;
1712 }
1713 // Gather expanded bounds
1714 off_lo = MIN2(off_lo,offset2);
1715 off_hi = MAX2(off_hi,offset2);
1716 // Record top NRC range checks
1717 prev_checks[nb_checks%NRC].ctl = prev_dom;
1718 prev_checks[nb_checks%NRC].off = offset2;
1719 nb_checks++;
1720 }
1721 }
1722 prev_dom = dom;
1723 dom = up_one_dom(dom);
1724 if (!dom) break;
1725 }
1726
1727 if (!found_immediate_dominator) {
1728 // Attempt to widen the dominating range check to cover some later
1729 // ones. Since range checks "fail" by uncommon-trapping to the
1730 // interpreter, widening a check can make us speculatively enter
1731 // the interpreter. If we see range-check deopt's, do not widen!
1732 if (!phase->C->allow_range_check_smearing()) return NULL;
1733
1734 // Didn't find prior covering check, so cannot remove anything.
1735 if (nb_checks == 0) {
1736 return NULL;
1737 }
1738 // Constant indices only need to check the upper bound.
1739 // Non-constant indices must check both low and high.
1740 int chk0 = (nb_checks - 1) % NRC;
1741 if (index1) {
1742 if (nb_checks == 1) {
1743 return NULL;
1744 } else {
1745 // If the top range check's constant is the min or max of
1746 // all constants we widen the next one to cover the whole
1747 // range of constants.
1748 RangeCheck rc0 = prev_checks[chk0];
1749 int chk1 = (nb_checks - 2) % NRC;
1750 RangeCheck rc1 = prev_checks[chk1];
1751 if (rc0.off == off_lo) {
1752 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1753 prev_dom = rc1.ctl;
1754 } else if (rc0.off == off_hi) {
1755 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1756 prev_dom = rc1.ctl;
1757 } else {
1758 // If the top test's constant is not the min or max of all
1759 // constants, we need 3 range checks. We must leave the
1760 // top test unchanged because widening it would allow the
1761 // accesses it protects to successfully read/write out of
1762 // bounds.
1763 if (nb_checks == 2) {
1764 return NULL;
1765 }
1766 int chk2 = (nb_checks - 3) % NRC;
1767 RangeCheck rc2 = prev_checks[chk2];
1768 // The top range check a+i covers interval: -a <= i < length-a
1769 // The second range check b+i covers interval: -b <= i < length-b
1770 if (rc1.off <= rc0.off) {
1771 // if b <= a, we change the second range check to:
1772 // -min_of_all_constants <= i < length-min_of_all_constants
1773 // Together top and second range checks now cover:
1774 // -min_of_all_constants <= i < length-a
1775 // which is more restrictive than -b <= i < length-b:
1776 // -b <= -min_of_all_constants <= i < length-a <= length-b
1777 // The third check is then changed to:
1778 // -max_of_all_constants <= i < length-max_of_all_constants
1779 // so 2nd and 3rd checks restrict allowed values of i to:
1780 // -min_of_all_constants <= i < length-max_of_all_constants
1781 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
1782 adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
1783 } else {
1784 // if b > a, we change the second range check to:
1785 // -max_of_all_constants <= i < length-max_of_all_constants
1786 // Together top and second range checks now cover:
1787 // -a <= i < length-max_of_all_constants
1788 // which is more restrictive than -b <= i < length-b:
1789 // -b < -a <= i < length-max_of_all_constants <= length-b
1790 // The third check is then changed to:
1791 // -max_of_all_constants <= i < length-max_of_all_constants
1792 // so 2nd and 3rd checks restrict allowed values of i to:
1793 // -min_of_all_constants <= i < length-max_of_all_constants
1794 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
1795 adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
1796 }
1797 prev_dom = rc2.ctl;
1798 }
1799 }
1800 } else {
1801 RangeCheck rc0 = prev_checks[chk0];
1802 // 'Widen' the offset of the 1st and only covering check
1803 adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
1804 // Test is now covered by prior checks, dominate it out
1805 prev_dom = rc0.ctl;
1806 }
1807 }
1808 } else {
1809 prev_dom = search_identical(4);
1810
1811 if (prev_dom == NULL) {
1812 return NULL;
1813 }
1814 }
1815
1816 // Replace dominated IfNode
1817 return dominated_by(prev_dom, igvn);
1818 }
1819