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