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