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 //
658 // Important: we only parse if val is on the lhs. This is a limitation, but it makes
659 // optimizations simpler. We rely on canonicalization to get us to this
660 // shape, which works well for comparisions with constants, as they are
661 // canonicalized to the rhs. This may not happen with variables, and so
662 // the optimization may not work for those cases, when val stays on the rhs.
663 const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node* val, Node* if_proj) {
664 assert(if_proj &&
665 (if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");
666 if (if_proj->in(0) && if_proj->in(0)->is_If()) {
667 IfNode* iff = if_proj->in(0)->as_If();
668 if (iff->in(1) && iff->in(1)->is_Bool()) {
669 BoolNode* bol = iff->in(1)->as_Bool();
670 if (bol->in(1) && bol->in(1)->is_Cmp()) {
671 const CmpNode* cmp = bol->in(1)->as_Cmp();
672 // Val is always the lhs of the comparision: val CmpI cmp2
673 if (cmp->Opcode() == Op_CmpI && cmp->in(1) == val) {
674 // Only CmpI allowed, assumed by signed logic below.
675 // We could extend to CmpU in the future, and would
676 // have to implement unsigned range logic below.
677 const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();
678 if (cmp2_t != nullptr) {
679 jint lo = cmp2_t->_lo;
680 jint hi = cmp2_t->_hi;
681 // Negate the test if we are on the false branch.
682 BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();
683 switch (msk) {
684 case BoolTest::ne: {
685 // If val is compared to its lower or upper bound, we can narrow the type
686 const TypeInt* val_t = gvn->type(val)->isa_int();
687 if (val_t != nullptr && !val_t->singleton() && cmp2_t->is_con()) {
688 if (val_t->_lo == lo) {
689 // Condition leading to if_proj: val != val->lo
690 // val in [val->lo + 1, val->hi]
691 return TypeInt::make(val_t->_lo + 1, val_t->_hi, val_t->_widen);
692 } else if (val_t->_hi == hi) {
693 // Condition leading to if_proj: val != val->hi
694 // val in [val->lo, val->hi - 1]
695 return TypeInt::make(val_t->_lo, val_t->_hi - 1, val_t->_widen);
696 }
697 }
698 // Can't refine type
699 return nullptr;
700 }
701 case BoolTest::eq:
702 // Condition leading to if_proj: val == cmp2
703 // val in cmp2_t
704 return cmp2_t;
705 case BoolTest::lt:
706 // Condition leading to if_proj: val < cmp2
707 // val in [min_int .. max(min_int, cmp2->_hi - 1)]
708 lo = min_jint;
709 if (hi != min_jint) {
710 hi = hi - 1;
711 }
712 break;
713 case BoolTest::le:
714 // Condition leading to if_proj: val <= cmp2
715 // val in [min_int .. cmp2->_hi]
716 lo = min_jint;
717 break;
718 case BoolTest::gt:
719 // Condition leading to if_proj: val > cmp2
720 // val in [min(cmp2->_lo + 1, max_int) .. max_int]
721 if (lo != max_jint) {
722 lo = lo + 1;
723 }
724 hi = max_jint;
725 break;
726 case BoolTest::ge:
727 // Condition leading to if_proj: val >= cmp2
728 // val in [cmp2->_lo .. max_int]
729 hi = max_jint;
730 break;
731 default:
732 assert(false, "impossible case");
733 return nullptr;
734 }
735 const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);
736 return rtn_t;
737 }
738 }
739 }
740 }
741 }
742 return nullptr;
743 }
744
745 //------------------------------fold_compares----------------------------
746 // See if a pair of CmpIs can be converted into a CmpU. In some cases
747 // the direction of this if is determined by the preceding if so it
748 // can be eliminate entirely.
749 //
750 // Given an if testing (CmpI n v) check for an immediately control
751 // dependent if that is testing (CmpI n v2) and has one projection
752 // leading to this if and the other projection leading to a region
753 // that merges one of this ifs control projections.
754 //
755 // If
756 // / |
757 // / |
758 // / |
759 // If |
760 // /\ |
761 // / \ |
762 // / \ |
763 // / Region
764 //
765 // Or given an if testing (CmpI n v) check for a dominating if that is
766 // testing (CmpI n v2), both having one projection leading to an
767 // uncommon trap. Allow Another independent guard in between to cover
768 // an explicit range check:
769 // if (index < 0 || index >= array.length) {
770 // which may need a null check to guard the LoadRange
771 //
772 // If
773 // / \
774 // / \
775 // / \
776 // If unc
777 // /\
778 // / \
779 // / \
780 // / unc
781 //
782
783 // Is the comparison for this If suitable for folding?
784 bool IfNode::cmpi_folds(PhaseIterGVN* igvn, bool fold_ne) {
785 return in(1) != nullptr &&
786 in(1)->is_Bool() &&
787 in(1)->in(1) != nullptr &&
788 in(1)->in(1)->Opcode() == Op_CmpI &&
789 in(1)->in(1)->in(2) != nullptr &&
790 in(1)->in(1)->in(2) != igvn->C->top() &&
791 (in(1)->as_Bool()->_test.is_less() ||
792 in(1)->as_Bool()->_test.is_greater() ||
793 (fold_ne && in(1)->as_Bool()->_test._test == BoolTest::ne));
794 }
795
796 // Is a dominating control suitable for folding with this if?
797 bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
798 return ctrl != nullptr &&
799 ctrl->is_IfProj() &&
800 ctrl->outcnt() == 1 && // No side-effects
801 ctrl->in(0) != nullptr &&
802 ctrl->in(0)->Opcode() == Op_If &&
803 ctrl->in(0)->outcnt() == 2 &&
804 ctrl->in(0)->as_If()->cmpi_folds(igvn, true) &&
805 // Must compare same value
806 ctrl->in(0)->in(1)->in(1)->in(1) != nullptr &&
807 ctrl->in(0)->in(1)->in(1)->in(1) != igvn->C->top() &&
808 ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
809 }
810
811 // Do this If and the dominating If share a region?
812 bool IfNode::has_shared_region(IfProjNode* proj, IfProjNode*& success, IfProjNode*& fail) const {
813 IfProjNode* otherproj = proj->other_if_proj();
814 Node* otherproj_ctrl_use = otherproj->unique_ctrl_out_or_null();
815 RegionNode* region = (otherproj_ctrl_use != nullptr && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : nullptr;
816 success = nullptr;
817 fail = nullptr;
818
819 if (otherproj->outcnt() == 1 && region != nullptr && !region->has_phi()) {
820 for (int i = 0; i < 2; i++) {
821 IfProjNode* next_proj = proj_out(i)->as_IfProj();
822 if (success == nullptr && next_proj->outcnt() == 1 && next_proj->unique_out() == region) {
823 success = next_proj;
824 } else if (fail == nullptr) {
825 fail = next_proj;
826 } else {
827 success = nullptr;
828 fail = nullptr;
829 }
830 }
831 }
832 return success != nullptr && fail != nullptr;
833 }
834
835 bool IfNode::is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc) {
836 // Different methods and methods containing jsrs are not supported.
837 ciMethod* method = unc->jvms()->method();
838 ciMethod* dom_method = dom_unc->jvms()->method();
839 if (method != dom_method || method->has_jsrs()) {
840 return false;
841 }
842 // Check that both traps are in the same activation of the method (instead
843 // of two activations being inlined through different call sites) by verifying
844 // that the call stacks are equal for both JVMStates.
845 JVMState* dom_caller = dom_unc->jvms()->caller();
846 JVMState* caller = unc->jvms()->caller();
847 if ((dom_caller == nullptr) != (caller == nullptr)) {
848 // The current method must either be inlined into both dom_caller and
849 // caller or must not be inlined at all (top method). Bail out otherwise.
850 return false;
851 } else if (dom_caller != nullptr && !dom_caller->same_calls_as(caller)) {
852 return false;
853 }
854 // Check that the bci of the dominating uncommon trap dominates the bci
855 // of the dominated uncommon trap. Otherwise we may not re-execute
856 // the dominated check after deoptimization from the merged uncommon trap.
857 ciTypeFlow* flow = dom_method->get_flow_analysis();
858 int bci = unc->jvms()->bci();
859 int dom_bci = dom_unc->jvms()->bci();
860 if (!flow->is_dominated_by(bci, dom_bci)) {
861 return false;
862 }
863
864 return true;
865 }
866
867 // Return projection that leads to an uncommon trap if any
868 ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call, Deoptimization::DeoptReason reason) const {
869 for (int i = 0; i < 2; i++) {
870 call = proj_out(i)->is_uncommon_trap_proj(reason);
871 if (call != nullptr) {
872 return proj_out(i);
873 }
874 }
875 return nullptr;
876 }
877
878 // Do this If and the dominating If both branch out to an uncommon trap
879 bool IfNode::has_only_uncommon_traps(IfProjNode* proj, IfProjNode*& success, IfProjNode*& fail, PhaseIterGVN* igvn) const {
880 IfProjNode* otherproj = proj->other_if_proj();
881 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj();
882
883 if (otherproj->outcnt() == 1 && dom_unc != nullptr) {
884 // We need to re-execute the folded Ifs after deoptimization from the merged traps
885 if (!dom_unc->jvms()->should_reexecute()) {
886 return false;
887 }
888
889 CallStaticJavaNode* unc = nullptr;
890 ProjNode* unc_proj = uncommon_trap_proj(unc);
891 if (unc_proj != nullptr && unc_proj->outcnt() == 1) {
892 if (dom_unc == unc) {
893 // Allow the uncommon trap to be shared through a region
894 RegionNode* r = unc->in(0)->as_Region();
895 if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) {
896 return false;
897 }
898 assert(r->has_phi() == nullptr, "simple region shouldn't have a phi");
899 } else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) {
900 return false;
901 }
902
903 if (!is_dominator_unc(dom_unc, unc)) {
904 return false;
905 }
906
907 if (!dom_unc->safe_for_fold_compare()) {
908 return false;
909 }
910
911 // See merge_uncommon_traps: the reason of the uncommon trap
912 // will be changed and the state of the dominating If will be
913 // used. Checked that we didn't apply this transformation in a
914 // previous compilation and it didn't cause too many traps
915 ciMethod* dom_method = dom_unc->jvms()->method();
916 int dom_bci = dom_unc->jvms()->bci();
917 if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) &&
918 !igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check) &&
919 // Return true if c2 manages to reconcile with UnstableIf optimization. See the comments for it.
920 igvn->C->remove_unstable_if_trap(dom_unc, true/*yield*/)) {
921 success = unc_proj->as_IfProj();
922 fail = unc_proj->as_IfProj()->other_if_proj();
923 return true;
924 }
925 }
926 }
927 return false;
928 }
929
930 // We are given the following code shape with two CmpI:
931 //
932 // n v1
933 // | |
934 // cmp1
935 // |
936 // entry bool1(test1)
937 // | |
938 // iff1
939 // | \
940 // middle fail1-------------+
941 // | |
942 // | n v2 |
943 // | | | |
944 // maybe cmp2 |
945 // null-check | |
946 // | bool2(test2) |
947 // | | |
948 // iff2 |
949 // | \ v
950 // succ fail2----> go to same region
951 // or uncommon trap
952 //
953 // 1. In some cases, we can prove that succ cannot be reached,
954 // and we can fold away the iff2. Example:
955 //
956 // if (n < -1 && n > 1) { succ } else { fail }
957 // // 1st condition: n in [min_int .. -2]
958 // // 2nd condition: n in [2 .. max_int]
959 // // -> no overlap -> constant fold iff2 towards fail2
960 // //
961 // // Equivalent, if we flip everything:
962 // if (n >= -1 || n <= 1) { fail } else { succ }
963 //
964 // 2. In other cases, we can replace the two CmpI with
965 // a single CmpU. We fold iff1 towards middle, and
966 // replace the iff2 condition with the CmpU. Example:
967 //
968 // if (n >= 0 && n < 10) { succ } else { fail }
969 // // transformed to:
970 // if (n <u 10) { succ } else { fail }
971 //
972 // if (n < 0 || n >= arr.length) { throw ArrayOutOfBoundsException }
973 // // transformed to:
974 // if (n >=u arr.length) { throw ArrayOutOfBoundsException }
975 //
976 // Note1: we assume that the CmpI nodes are canonicalized to the
977 // point where n is always on the lhs. This is a limitation,
978 // but as long as v1 and v2 are constants they will eventually
979 // be canonicalized to the rhs. For variables, this may not always
980 // happen.
981 //
982 // Note2: We are flexible about the IfProj nodes: middle and succ
983 // could both be either IfTrue or IfFalse.
984 //
985 // Note3: Surrounding code has a different naming scheme!
986 // In has_only_uncommon_traps, the path towards the
987 // uncommon trap (e.g. failed range check) is called
988 // "success", while the path that does not go to
989 // the uncommon trap (e.g. in-bounds access) is called
990 // "fail". I think that is counter-intuitive, so I now
991 // used a different naming scheme here.
992 //
993 // Return true iff we could perform one of the optimizations.
994 bool IfNode::fold_compares_helper(IfProjNode* middle, IfProjNode* fail2, IfProjNode* succ, PhaseIterGVN* igvn) {
995 assert(fail2->in(0) == this, "link iff2->fail2");
996 assert(succ->in(0) == this, "link iff2->succ");
997
998 IfNode* iff1 = middle->in(0)->as_If();
999 IfNode* iff2 = this;
1000 BoolNode* bool1 = iff1->in(1)->as_Bool();
1001 BoolNode* bool2 = iff2->in(1)->as_Bool();
1002 CmpNode* cmp1 = bool1->in(1)->as_Cmp();
1003 CmpNode* cmp2 = bool2->in(1)->as_Cmp();
1004 assert(cmp1->Opcode() == Op_CmpI, "comparisons must be CmpI");
1005 assert(cmp2->Opcode() == Op_CmpI, "comparisons must be CmpI");
1006
1007 IfProjNode* fail1 = middle->other_if_proj();
1008
1009 Node* v1 = cmp1->in(2);
1010 Node* v2 = cmp2->in(2);
1011 Node* n = cmp1->in(1);
1012 assert(cmp2->in(1) == n, "n must be lhs in both CmpI");
1013
1014 // Optimization 1: try to prove that succ is not reachable.
1015 // Which values of n can pass iff1 to middle AND iff2 to succ?
1016 const TypeInt* type_middle = filtered_int_type(igvn, n, middle);
1017 if (type_middle != nullptr) {
1018 const TypeInt* type_succ = filtered_int_type(igvn, n, succ);
1019 if (type_succ != nullptr) {
1020 if (type_middle->filter(type_succ) == Type::TOP) {
1021 // The intersection is empty -> succ is not reachable.
1022 // Fold iff2 towards fail2 (and away from succ).
1023 igvn->replace_input_of(iff2, 1, igvn->intcon(fail2->_con));
1024 return true; // success: succ not reachable
1025 }
1026 }
1027 }
1028
1029 // Optimization 2: try to replace the two CmpI with one CmpU
1030 // We can handle the following 4 cases:
1031 // Input: two CmpI Output: one CmpU Assumption
1032 // -------------------- ------------------------- -------------------
1033 // a) (n > lo && n < hi) -> n - lo - 1 <u hi - lo - 1 (assuming lo < hi)
1034 // (n > 2 && n < 5 ) n - 3 <u 2
1035 // range: [3, 4]
1036 //
1037 // b) (n > lo && n <= hi) -> n - lo - 1 <u hi - lo (assuming lo <= hi)
1038 // (n > 2 && n <= 5 ) n - 3 <u 3
1039 // range: [3, 4, 5]
1040 //
1041 // c) (n >= lo && n < hi) -> n - lo <u hi - lo (assuming lo <= hi)
1042 // (n >= 2 && n < 5 ) n - 2 <u 3
1043 // range: [2, 3, 4]
1044 //
1045 // d) (n >= lo && n <= hi) -> n - lo <=u hi - lo (assuming lo <= hi)
1046 // (n >= 2 && n <= 5 ) n - 2 <=u 3
1047 // range: [2, 3, 4, 5]
1048 //
1049 // Note1: the rhs of the CmpU indicates the cardinality of the range,
1050 // allowing n to have exactly that many different values.
1051 //
1052 // Note2: all 4 case have an assumption: lo must be sufficiently smaller
1053 // than hi. Below, and with the use of Lemma1 from below, we will
1054 // prove that this implies that the rhs of the CmpU never
1055 // underflows or overflows, which is critical for correctness.
1056 //
1057 // Below, we will prove and implement each of these cases. But first,
1058 // we must handle the combinations of IfTrue/IfFalse projections for
1059 // middle and succ, and extract which one is the lower bound (lo) and
1060 // which one the upper bound (hi).
1061 //
1062 // <---- lower bound -----> <----------- succ -------------> <---- upper bound ----->
1063 // [min_int .. lo_type->hi] [lo_type->hi+1 .. hi_type->lo-1] [hi_type->lo .. max_int]
1064 // ^ ^
1065 // n {>/>=} lo n {</<=} hi
1066 //
1067 // The trick is then to "shift down" the succ range, to create only
1068 // a single transition point.
1069 //
1070 // <----------- succ -------------> <------------ unsigned upper bound ------------->
1071 // [0 .. ] [ .. max_uint]
1072 // ^
1073 // CmpU
1074
1075 BoolTest::mask test1 = bool1->_test._test;
1076 BoolTest::mask test2 = bool2->_test._test;
1077 if (middle->Opcode() == Op_IfFalse) { test1 = BoolTest::negate_mask(test1); }
1078 if (succ->Opcode() == Op_IfFalse) { test2 = BoolTest::negate_mask(test2); }
1079
1080 Node* lo = nullptr;
1081 Node* hi = nullptr;
1082 const TypeInt* lo_type = nullptr;
1083 const TypeInt* hi_type = nullptr;
1084 BoolTest::mask lo_test = BoolTest::illegal;
1085 BoolTest::mask hi_test = BoolTest::illegal;
1086 if (BoolTest::is_greater(test1) && BoolTest::is_less(test2)) {
1087 lo = v1;
1088 hi = v2;
1089 lo_type = IfNode::filtered_int_type(igvn, n, fail1);
1090 hi_type = IfNode::filtered_int_type(igvn, n, fail2);
1091 lo_test = test1;
1092 hi_test = test2;
1093 } else if (BoolTest::is_less(test1) && BoolTest::is_greater(test2)) {
1094 lo = v2;
1095 hi = v1;
1096 lo_type = IfNode::filtered_int_type(igvn, n, fail2);
1097 hi_type = IfNode::filtered_int_type(igvn, n, fail1);
1098 lo_test = test2;
1099 hi_test = test1;
1100 } else {
1101 // Could not find upper and lower bound.
1102 return false;
1103 }
1104 assert(BoolTest::is_greater(lo_test), "lower bound: n {>/>=} lo");
1105 assert(BoolTest::is_less(hi_test), "upper bound: n {</<=} lo");
1106
1107 // Check that we got lower and upper bounds as expected.
1108 if (lo_type == nullptr ||
1109 hi_type == nullptr ||
1110 hi_type->_hi != max_jint ||
1111 lo_type->_lo != min_jint) {
1112 // Upper and lower bounds could not be established.
1113 return false;
1114 }
1115
1116 // -------------------------------------------------------------------
1117 // In the proofs below, we need some basic Lemmas to deal with integer
1118 // signed and unsigned arithmetic.
1119 //
1120 // Lemma1:
1121 // Let a and b be in [min_int .. max_int].
1122 // If a >=s b, then:
1123 // U(a - b) = a - b
1124 //
1125 // Proof:
1126 // a >= b
1127 // -> a - b >= 0
1128 //
1129 // a <= max_int
1130 // b >= min_int
1131 // -> a - b <= max_int - min_int = 2^32-1
1132 //
1133 // 0 <= a - b <= 2^32-1
1134 // -> cast to unsigned has no overflow
1135 // -> U(a - b) = a - b
1136 //
1137 // Lemma2:
1138 // Let a and b be in [min_int .. max_int].
1139 // If a <s b, then:
1140 // U(a - b) = a - b + 2^32
1141 //
1142 // Proof:
1143 // a < b
1144 // -> a - b < 0
1145 //
1146 // a >= min_int
1147 // b <= max_int
1148 // -> a - b >= min_int - max_int = 2^32-1
1149 //
1150 // 2^32-1 <= a - b < 0
1151 // -> cast to unsigned leads to exactly one overflow
1152 // -> U(a - b) = a - b + 2^32
1153 //
1154 // Lemma3:
1155 // Let a and b be in [min_int .. max_int].
1156 // a + 2^32 > b
1157 //
1158 // Proof:
1159 // Using a >= min_int, and b <= max_int:
1160 // a + 2^32 >= min_int + 2^32
1161 // = max_int + 1
1162 // >= b + 1
1163 // > b
1164 // -------------------------------------------------------------------
1165
1166 // Handle the 4 cases.
1167 // All produce this form: n - lo + x1 <cond> hi - lo + x2
1168 Node* x1 = nullptr;
1169 Node* x2 = nullptr;
1170 BoolTest::mask cond = BoolTest::illegal;
1171 if (lo_test == BoolTest::gt && hi_test == BoolTest::lt) {
1172 // We perform the the (CHECK) below, which implies (LO-HI),
1173 // as we will show below.
1174 if (lo_type->_hi >= hi_type->_lo) {
1175 return false; // (CHECK) fails, we cannot establish (LO-HI) assumption.
1176 }
1177 // a) (n > lo && n < hi) -> n - lo - 1 <u hi - lo - 1 (assuming lo < hi)
1178 // (BEFORE) (AFTER) (LO-HI)
1179 //
1180 // Proof:
1181 // From IfNode::filtered_int_type, we get:
1182 // lo_type = [min_int .. lo->_hi] for n <= lo
1183 // -> lo_type->_hi = lo->_hi
1184 // hi_type = [hi->_lo .. max_int] for n >= lo
1185 // -> hi_type->_lo = hi->_lo
1186 // We will need the assumption (LO-HI) below, which we can
1187 // establish with the following (CHECK):
1188 // lo_type->_hi < hi_type->_lo (CHECK)
1189 // -> lo->_hi < hi->_lo
1190 // -> lo < hi (LO-HI)
1191 //
1192 // Case n <= lo:
1193 // (BEFORE) is always false, show (AFTER) is always false.
1194 // Since lo < hi (LO-HI), S(lo+1) = lo+1 (no overflow):
1195 // -> lo+1 <= hi
1196 // -> n < lo+1
1197 // U(n - (lo + 1)) < U(hi - (lo + 1))
1198 // -- Lemma2 (n < lo+1) -- -- Lemma1 (lo+1 <= hi) --
1199 // n - (lo + 1) + 2^32 < hi - (lo + 1)
1200 // n + 2^32 < hi
1201 // Always false by Lemma3.
1202 //
1203 // Case lo < n < hi:
1204 // (BEFORE) is always true, show (AFTER) is always true.
1205 // Since lo < hi (LO-HI), S(lo+1) = lo+1 (no overflow):
1206 // -> lo+1 <= hi
1207 // -> n >= lo+1
1208 // U(n - (lo + 1)) < U(hi - (lo + 1))
1209 // -- Lemma1 (n >= lo+1) -- -- Lemma1 (lo+1 <= hi) --
1210 // n - (lo + 1) < hi - (lo + 1)
1211 // n < hi
1212 // Corresponds to case assumption, so always true.
1213 //
1214 // Case n >= hi:
1215 // (BEFORE) is always false, show (AFTER) is always false.
1216 // Since lo < hi (LO-HI), S(lo+1) = lo+1 (no overflow):
1217 // -> lo+1 <= hi
1218 // U(n - (lo + 1)) < U(hi - (lo + 1))
1219 // -- Lemma1 (n >= lo+1) -- -- Lemma1 (lo+1 <= hi) --
1220 // n - (lo + 1) < hi - (lo + 1)
1221 // n < hi
1222 // Contradicts case assumption, so always false.
1223 // QED.
1224 //
1225 // Note: we cannot use anything more relaxed than the assumption
1226 // lo < hi: with lo=hi the rhs of the CmpU would underflow.
1227 //
1228 // Produce form: n - lo + x1 <cond> hi - lo + x2
1229 // n - lo - 1 <u hi - lo - 1
1230 x1 = igvn->intcon(-1);
1231 x2 = igvn->intcon(-1);
1232 cond = BoolTest::lt;
1233 } else if (lo_test == BoolTest::gt && hi_test == BoolTest::le) {
1234 // We perform the the (CHECK) below, which implies (LO-HI),
1235 // as we will show below.
1236 if (lo_type->_hi >= hi_type->_lo) {
1237 return false; // (CHECK) fails, we cannot establish (LO-HI) assumption.
1238 }
1239 // b) (n > lo && n <= hi) -> n - lo - 1 <u hi - lo (assuming lo <= hi)
1240 // (BEFORE) (AFTER) (LO-HI)
1241 //
1242 // Proof:
1243 // From IfNode::filtered_int_type, we get:
1244 // lo_type = [min_int .. lo->_hi] for n <= lo
1245 // -> lo_type->_hi = lo->_hi
1246 // hi_type = [min(hi->_lo+1, max_int) .. max_int] for n > hi
1247 // -> hi_type->_lo <= lo->_lo + 1
1248 // We will need the assumption (LO-HI) below, which we can
1249 // establish with the following (CHECK):
1250 // lo_type->_hi < hi_type->_lo (CHECK)
1251 // -> lo->_hi < hi->_lo + 1
1252 // -> lo < hi + 1
1253 // -> lo <= hi (LO-HI)
1254 //
1255 // Case A: lo = hi
1256 // Let y = lo = hi
1257 // -> n > lo && n <= hi vs n - lo - 1 <u hi - lo
1258 // -> n > y && n <= y vs n - y - 1 <u y - y = 0
1259 // false false
1260 // Hence, (BEFORE) and (AFTER) are both always false.
1261 //
1262 // Case B: lo < hi
1263 // Case n <= lo:
1264 // (BEFORE) is always false, show (AFTER) is always false.
1265 // Since lo < hi (Case B), S(lo+1) = lo+1 (no overflow):
1266 // -> n < lo+1
1267 // U(n - (lo + 1)) < U(hi - lo)
1268 // -- Lemma2 (n < lo+1) -- -- Lemma1 (lo <= hi, LO-HI) --
1269 // n - (lo + 1) + 2^32 < hi - lo
1270 // n - 1 + 2^32 < hi
1271 // n + 2^32 <= hi
1272 // Always false by Lemma3.
1273 // Note: To apply Lemma2 above, we must use (Case B), we
1274 // could not have done it with (LO-HI) alone.
1275 //
1276 // Case lo < n <= hi:
1277 // (BEFORE) is always true, show (AFTER) is always true.
1278 // Since lo < hi (Case B), S(lo+1) = lo+1 (no overflow):
1279 // -> n >= lo+1
1280 // U(n - (lo + 1)) < U(hi - lo)
1281 // -- Lemma1 (n >= lo+1) -- -- Lemma1 (lo <= hi, LO-HI) --
1282 // n - (lo + 1) < hi - lo
1283 // n - 1 < hi
1284 // n <= hi
1285 // Follows from case assumption, so always true.
1286 //
1287 // Case n > hi:
1288 // (BEFORE) is always false, show (AFTER) is always false.
1289 // Since lo < hi (Case B), S(lo+1) = lo+1 (no overflow):
1290 // -> lo+1 <= hi
1291 // -> n > lo+1
1292 // U(n - (lo + 1)) < U(hi - lo)
1293 // -- Lemma1 (n > lo+1) -- -- Lemma1 (lo <= hi, LO-HI) --
1294 // n - (lo + 1) < hi - lo
1295 // n - 1 < hi
1296 // n <= hi
1297 // Contradicts case assumption, so always false.
1298 // QED.
1299 //
1300 // Note: we cannot use anything more relaxed than the assumption
1301 // lo <= hi: with lo=hi+1 the rhs of the CmpU would underflow.
1302 //
1303 // Produce form: n - lo + x1 <cond> hi - lo + x2
1304 // n - lo - 1 <u hi - lo
1305 x1 = igvn->intcon(-1);
1306 x2 = igvn->intcon(0);
1307 cond = BoolTest::lt;
1308 } else if (lo_test == BoolTest::ge && hi_test == BoolTest::lt) {
1309 // We perform the the (CHECK) below, which implies (LO-HI),
1310 // as we will show below.
1311 if (lo_type->_hi >= hi_type->_lo) {
1312 return false; // (CHECK) fails, we cannot establish (LO-HI) assumption.
1313 }
1314 // c) (n >= lo && n < hi) -> n - lo <u hi - lo (assuming lo <= hi)
1315 // (BEFORE) (AFTER) (LO-HI)
1316 //
1317 // Proof:
1318 // From IfNode::filtered_int_type, we get:
1319 // lo_type = [min_int .. max(min_int, lo->_hi - 1)] for n < lo
1320 // -> lo_type->_hi >= lo->_hi - 1
1321 // hi_type = [b->_lo .. max_int] for n >= hi
1322 // -> hi_type->_lo = hi->_lo
1323 // We will need the assumption (LO-HI) below, which we can
1324 // establish with the following (CHECK):
1325 // lo_type->_hi < hi_type->_lo
1326 // -> lo->_hi - 1 < hi->_lo
1327 // -> lo->_hi <= hi->_lo
1328 // -> lo <= hi (HI-LO)
1329 //
1330 // Case n < lo:
1331 // (BEFORE) is always false, show (AFTER) is always false.
1332 // U(n - lo) < U(hi - lo)
1333 // -- Lemma2 (n < lo) -- -- Lemma1 (lo <= hi, LO-HI) --
1334 // n - lo + 2^32 < hi - lo
1335 // n + 2^32 < hi
1336 // Always false by Lemma3.
1337 //
1338 // Case lo <=s n <s hi:
1339 // (BEFORE) is always true, show (AFTER) is always true.
1340 // U(n - lo) < U(hi - lo)
1341 // -- Lemma1 (n >= lo) -- -- Lemma1 (lo <= hi, LO-HI) --
1342 // n - lo < hi - lo
1343 // n < hi
1344 // Follows from case assumption, so always true.
1345 //
1346 // Case n >=s hi:
1347 // (BEFORE) is always false, show (AFTER) is always false.
1348 // U(n - lo) < U(hi - lo)
1349 // -- Lemma1 (n >= lo) -- -- Lemma1 (lo <= hi, LO-HI) --
1350 // n - lo < hi - lo
1351 // n < hi
1352 // Contradicts case assumption, so always false.
1353 // QED.
1354 //
1355 /// Note: we cannot use anything more relaxed than the assumption
1356 // lo <= hi: with lo=hi+1 the rhs of the CmpU would underflow.
1357 //
1358 // Produce form: n - lo + x1 <cond> hi - lo + x2
1359 // n - lo <u hi - lo
1360 x1 = igvn->intcon(0);
1361 x2 = igvn->intcon(0);
1362 cond = BoolTest::lt;
1363 } else {
1364 assert (lo_test == BoolTest::ge && hi_test == BoolTest::le, "");
1365 // We perform the the (CHECK) below, which implies (LO-HI),
1366 // as we will show below.
1367 jlong lo_type_hi = lo_type->_hi;
1368 jlong hi_type_lo = hi_type->_lo;
1369 if (lo_type_hi >= hi_type_lo - 1) {
1370 return false; // (CHECK) fails, we cannot establish (LO-HI) assumption.
1371 }
1372 // d) (n >= lo && n <= hi) -> n - lo <=u hi - lo (assuming lo <= hi)
1373 // (BEFORE) (AFTER) (LO-HI)
1374 //
1375 // Proof:
1376 // From IfNode::filtered_int_type, we get:
1377 // lo_type = [min_int .. max(min_int, lo->_hi-1)] for n < lo
1378 // -> lo_type->_hi >= lo->_hi - 1
1379 // hi_type = [min(hi->_lo+1, max_int) .. max_int] for n > hi
1380 // -> hi_type->_lo <= hi->_lo + 1
1381 // We will need the assumption (LO-HI) below, which we can
1382 // establish with the following (CHECK), which we must compute in
1383 // long to avoid underflow:
1384 // lo_type->_hi < hi_type->_lo - 1 (CHECK)
1385 // -> lo_type->_hi + 1 <= hi_type->_lo - 1
1386 // -> lo->_hi <= hi->_lo
1387 // -> lo <= hi (LO-HI)
1388 //
1389 // Case n <s lo:
1390 // (BEFORE) is always false, show (AFTER) is always false.
1391 // U(n - lo) <= U(hi - lo)
1392 // -- Lemma2 (n < lo) -- -- Lemma1 (hi >= lo, LO-HI) --
1393 // n - lo + 2^32 <= hi - lo
1394 // n + 2^32 <= hi
1395 // Always false by Lemma3.
1396 //
1397 // Case lo <=s n <=s hi:
1398 // (BEFORE) is always true, show (AFTER) is always true.
1399 // U(n - lo) <= U(hi - lo)
1400 // -- Lemma1 (n >= lo) -- -- Lemma1 (hi >= lo, LO-HI) --
1401 // n - lo <= hi - lo
1402 // n <= hi
1403 // Corresponds to case assumption, so always true.
1404 //
1405 // Case n >s hi:
1406 // (BEFORE) is always false, show (AFTER) is always false.
1407 // U(n - lo) <= U(hi - lo)
1408 // -- Lemma1 (n > lo) -- -- Lemma1 (hi >= lo, LO-HI) --
1409 // n - lo <= hi - lo
1410 // n <= hi
1411 // n <= hi
1412 // Contradicts case assumption, so always false.
1413 // QED.
1414 //
1415 // Note: (CHECK) is stronger in this case than in (a, b, c). We have
1416 // had multiple bugs around this case (d) in the past. For example:
1417 // - Before JDK-8135069: transform into: n - lo <=u hi - lo
1418 // leads to rhs underflow with lo=0 and hi=-1
1419 // -> we are coming back to this solution, but instead
1420 // of checking lo_type->_hi < hi_type->_lo
1421 // we now check: lo_type->_hi < hi_type->_lo - 1
1422 // which implies lo <= hi and excludes this bad case.
1423 // - Before JDK-8346420: transform into: n - lo <u hi - lo + 1
1424 // leads to rhs overflow with lo=min_int and hi=max_int
1425 //
1426 // Produce form: n - lo + x1 <cond> hi - lo + x2
1427 // n - lo <=u hi - lo
1428 x1 = igvn->intcon(0);
1429 x2 = igvn->intcon(0);
1430 cond = BoolTest::le;
1431 }
1432
1433 // Construct the new check: n - lo + x1 <cond> hi - lo + x2
1434 Node* lhs = igvn->transform(new SubINode(n, lo));
1435 lhs = igvn->transform(new AddINode(lhs, x1));
1436 Node* rhs = igvn->transform(new SubINode(hi, lo));
1437 rhs = igvn->transform(new AddINode(rhs, x2));
1438 Node* newcmp = igvn->transform(new CmpUNode(lhs, rhs));
1439 if (succ->Opcode() == Op_IfFalse) { cond = BoolTest::negate_mask(cond); }
1440 Node* newbool = igvn->transform(new BoolNode(newcmp, cond));
1441
1442 // Fold iff1 towards middle, and replace the iff2 condition:
1443 igvn->replace_input_of(iff1, 1, igvn->intcon(middle->_con));
1444 igvn->replace_input_of(iff2, 1, newbool);
1445
1446 return true; // Success with CmpU
1447 }
1448
1449 // Merge the branches that trap for this If and the dominating If into
1450 // a single region that branches to the uncommon trap for the
1451 // dominating If
1452 Node* IfNode::merge_uncommon_traps(IfProjNode* proj, IfProjNode* success, IfProjNode* fail, PhaseIterGVN* igvn) {
1453 Node* res = this;
1454 assert(success->in(0) == this, "bad projection");
1455
1456 IfProjNode* otherproj = proj->other_if_proj();
1457
1458 CallStaticJavaNode* unc = success->is_uncommon_trap_proj();
1459 CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj();
1460
1461 if (unc != dom_unc) {
1462 Node* r = new RegionNode(3);
1463
1464 r->set_req(1, otherproj);
1465 r->set_req(2, success);
1466 r = igvn->transform(r);
1467 assert(r->is_Region(), "can't go away");
1468
1469 // Make both If trap at the state of the first If: once the CmpI
1470 // nodes are merged, if we trap we don't know which of the CmpI
1471 // nodes would have caused the trap so we have to restart
1472 // execution at the first one
1473 igvn->replace_input_of(dom_unc, 0, r);
1474 igvn->replace_input_of(unc, 0, igvn->C->top());
1475 }
1476 int trap_request = dom_unc->uncommon_trap_request();
1477 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1478 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
1479
1480 int flip_test = 0;
1481 Node* l = nullptr;
1482 Node* r = nullptr;
1483
1484 if (success->in(0)->as_If()->range_check_trap_proj(flip_test, l, r) != nullptr) {
1485 // If this looks like a range check, change the trap to
1486 // Reason_range_check so the compiler recognizes it as a range
1487 // check and applies the corresponding optimizations
1488 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_range_check, action);
1489
1490 improve_address_types(l, r, fail, igvn);
1491
1492 res = igvn->transform(new RangeCheckNode(in(0), in(1), _prob, _fcnt));
1493 } else if (unc != dom_unc) {
1494 // If we trap we won't know what CmpI would have caused the trap
1495 // so use a special trap reason to mark this pair of CmpI nodes as
1496 // bad candidate for folding. On recompilation we won't fold them
1497 // and we may trap again but this time we'll know what branch
1498 // traps
1499 trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_unstable_fused_if, action);
1500 }
1501 igvn->replace_input_of(dom_unc, TypeFunc::Parms, igvn->intcon(trap_request));
1502 return res;
1503 }
1504
1505 // If we are turning 2 CmpI nodes into a CmpU that follows the pattern
1506 // of a rangecheck on index i, on 64 bit the compares may be followed
1507 // by memory accesses using i as index. In that case, the CmpU tells
1508 // us something about the values taken by i that can help the compiler
1509 // (see Compile::conv_I2X_index())
1510 void IfNode::improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn) {
1511 #ifdef _LP64
1512 ResourceMark rm;
1513 Node_Stack stack(2);
1514
1515 assert(r->Opcode() == Op_LoadRange, "unexpected range check");
1516 const TypeInt* array_size = igvn->type(r)->is_int();
1517
1518 stack.push(l, 0);
1519
1520 while(stack.size() > 0) {
1521 Node* n = stack.node();
1522 uint start = stack.index();
1523
1524 uint i = start;
1525 for (; i < n->outcnt(); i++) {
1526 Node* use = n->raw_out(i);
1527 if (stack.size() == 1) {
1528 if (use->Opcode() == Op_ConvI2L) {
1529 const TypeLong* bounds = use->as_Type()->type()->is_long();
1530 if (bounds->_lo <= array_size->_lo && bounds->_hi >= array_size->_hi &&
1531 (bounds->_lo != array_size->_lo || bounds->_hi != array_size->_hi)) {
1532 stack.set_index(i+1);
1533 stack.push(use, 0);
1534 break;
1535 }
1536 }
1537 } else if (use->is_Mem()) {
1538 Node* ctrl = use->in(0);
1539 for (int i = 0; i < 10 && ctrl != nullptr && ctrl != fail; i++) {
1540 ctrl = up_one_dom(ctrl);
1541 }
1542 if (ctrl == fail) {
1543 Node* init_n = stack.node_at(1);
1544 assert(init_n->Opcode() == Op_ConvI2L, "unexpected first node");
1545 // Create a new narrow ConvI2L node that is dependent on the range check
1546 Node* new_n = igvn->C->conv_I2X_index(igvn, l, array_size, fail);
1547
1548 // The type of the ConvI2L may be widen and so the new
1549 // ConvI2L may not be better than an existing ConvI2L
1550 if (new_n != init_n) {
1551 for (uint j = 2; j < stack.size(); j++) {
1552 Node* n = stack.node_at(j);
1553 Node* clone = n->clone();
1554 int rep = clone->replace_edge(init_n, new_n, igvn);
1555 assert(rep > 0, "can't find expected node?");
1556 clone = igvn->transform(clone);
1557 init_n = n;
1558 new_n = clone;
1559 }
1560 igvn->hash_delete(use);
1561 int rep = use->replace_edge(init_n, new_n, igvn);
1562 assert(rep > 0, "can't find expected node?");
1563 igvn->transform(use);
1564 if (init_n->outcnt() == 0) {
1565 igvn->_worklist.push(init_n);
1566 }
1567 }
1568 }
1569 } else if (use->in(0) == nullptr && (igvn->type(use)->isa_long() ||
1570 igvn->type(use)->isa_ptr())) {
1571 stack.set_index(i+1);
1572 stack.push(use, 0);
1573 break;
1574 }
1575 }
1576 if (i == n->outcnt()) {
1577 stack.pop();
1578 }
1579 }
1580 #endif
1581 }
1582
1583 bool IfNode::is_cmp_with_loadrange(IfProjNode* proj) const {
1584 if (in(1) != nullptr &&
1585 in(1)->in(1) != nullptr &&
1586 in(1)->in(1)->in(2) != nullptr) {
1587 Node* other = in(1)->in(1)->in(2);
1588 if (other->Opcode() == Op_LoadRange &&
1589 ((other->in(0) != nullptr && other->in(0) == proj) ||
1590 (other->in(0) == nullptr &&
1591 other->in(2) != nullptr &&
1592 other->in(2)->is_AddP() &&
1593 other->in(2)->in(1) != nullptr &&
1594 other->in(2)->in(1)->Opcode() == Op_CastPP &&
1595 other->in(2)->in(1)->in(0) == proj))) {
1596 return true;
1597 }
1598 }
1599 return false;
1600 }
1601
1602 bool IfNode::is_null_check(IfProjNode* proj, PhaseIterGVN* igvn) const {
1603 Node* other = in(1)->in(1)->in(2);
1604 if (other->in(MemNode::Address) != nullptr &&
1605 proj->in(0)->in(1) != nullptr &&
1606 proj->in(0)->in(1)->is_Bool() &&
1607 proj->in(0)->in(1)->in(1) != nullptr &&
1608 proj->in(0)->in(1)->in(1)->Opcode() == Op_CmpP &&
1609 proj->in(0)->in(1)->in(1)->in(2) != nullptr &&
1610 proj->in(0)->in(1)->in(1)->in(1) == other->in(MemNode::Address)->in(AddPNode::Address)->uncast() &&
1611 igvn->type(proj->in(0)->in(1)->in(1)->in(2)) == TypePtr::NULL_PTR) {
1612 return true;
1613 }
1614 return false;
1615 }
1616
1617 // Check that the If that is in between the 2 integer comparisons has
1618 // no side effect
1619 bool IfNode::is_side_effect_free_test(IfProjNode* proj, PhaseIterGVN* igvn) const {
1620 if (proj == nullptr) {
1621 return false;
1622 }
1623 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern();
1624 if (unc != nullptr && proj->outcnt() <= 2) {
1625 if (proj->outcnt() == 1 ||
1626 // Allow simple null check from LoadRange
1627 (is_cmp_with_loadrange(proj) && is_null_check(proj, igvn))) {
1628 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern();
1629 CallStaticJavaNode* dom_unc = proj->in(0)->in(0)->as_Proj()->is_uncommon_trap_if_pattern();
1630 assert(dom_unc != nullptr, "is_uncommon_trap_if_pattern returned null");
1631
1632 // reroute_side_effect_free_unc changes the state of this
1633 // uncommon trap to restart execution at the previous
1634 // CmpI. Check that this change in a previous compilation didn't
1635 // cause too many traps.
1636 int trap_request = unc->uncommon_trap_request();
1637 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
1638
1639 if (igvn->C->too_many_traps(dom_unc->jvms()->method(), dom_unc->jvms()->bci(), reason)) {
1640 return false;
1641 }
1642
1643 if (!is_dominator_unc(dom_unc, unc)) {
1644 return false;
1645 }
1646
1647 return true;
1648 }
1649 }
1650 return false;
1651 }
1652
1653 // Make the If between the 2 integer comparisons trap at the state of
1654 // the first If: the last CmpI is the one replaced by a CmpU and the
1655 // first CmpI is eliminated, so the test between the 2 CmpI nodes
1656 // won't be guarded by the first CmpI anymore. It can trap in cases
1657 // where the first CmpI would have prevented it from executing: on a
1658 // trap, we need to restart execution at the state of the first CmpI
1659 void IfNode::reroute_side_effect_free_unc(IfProjNode* proj, IfProjNode* dom_proj, PhaseIterGVN* igvn) {
1660 CallStaticJavaNode* dom_unc = dom_proj->is_uncommon_trap_if_pattern();
1661 IfProjNode* otherproj = proj->other_if_proj();
1662 CallStaticJavaNode* unc = proj->is_uncommon_trap_if_pattern();
1663 Node* call_proj = dom_unc->unique_ctrl_out();
1664 Node* halt = call_proj->unique_ctrl_out();
1665
1666 Node* new_unc = dom_unc->clone();
1667 call_proj = call_proj->clone();
1668 halt = halt->clone();
1669 Node* c = otherproj->clone();
1670
1671 c = igvn->transform(c);
1672 new_unc->set_req(TypeFunc::Parms, unc->in(TypeFunc::Parms));
1673 new_unc->set_req(0, c);
1674 new_unc = igvn->transform(new_unc);
1675 call_proj->set_req(0, new_unc);
1676 call_proj = igvn->transform(call_proj);
1677 halt->set_req(0, call_proj);
1678 halt = igvn->transform(halt);
1679
1680 igvn->replace_node(otherproj, igvn->C->top());
1681 igvn->C->root()->add_req(halt);
1682 }
1683
1684 Node* IfNode::fold_compares(PhaseIterGVN* igvn) {
1685 if (Opcode() != Op_If) return nullptr;
1686
1687 if (cmpi_folds(igvn)) {
1688 Node* ctrl = in(0);
1689 if (is_ctrl_folds(ctrl, igvn)) {
1690 // A integer comparison immediately dominated by another integer
1691 // comparison
1692 IfProjNode* success = nullptr;
1693 IfProjNode* fail = nullptr;
1694 IfProjNode* dom_cmp = ctrl->as_IfProj();
1695 if (has_shared_region(dom_cmp, success, fail) &&
1696 // Next call modifies graph so must be last
1697 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1698 return this;
1699 }
1700 if (has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1701 // Next call modifies graph so must be last
1702 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1703 return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1704 }
1705 return nullptr;
1706 } else if (ctrl->in(0) != nullptr &&
1707 ctrl->in(0)->in(0) != nullptr) {
1708 IfProjNode* success = nullptr;
1709 IfProjNode* fail = nullptr;
1710 Node* dom = ctrl->in(0)->in(0);
1711 IfProjNode* dom_cmp = dom->isa_IfProj();
1712 IfProjNode* other_cmp = ctrl->isa_IfProj();
1713
1714 // Check if it's an integer comparison dominated by another
1715 // integer comparison with another test in between
1716 if (is_ctrl_folds(dom, igvn) &&
1717 has_only_uncommon_traps(dom_cmp, success, fail, igvn) &&
1718 is_side_effect_free_test(other_cmp, igvn) &&
1719 // Next call modifies graph so must be last
1720 fold_compares_helper(dom_cmp, success, fail, igvn)) {
1721 reroute_side_effect_free_unc(other_cmp, dom_cmp, igvn);
1722 return merge_uncommon_traps(dom_cmp, success, fail, igvn);
1723 }
1724 }
1725 }
1726 return nullptr;
1727 }
1728
1729 //------------------------------remove_useless_bool----------------------------
1730 // Check for people making a useless boolean: things like
1731 // if( (x < y ? true : false) ) { ... }
1732 // Replace with if( x < y ) { ... }
1733 static Node *remove_useless_bool(IfNode *iff, PhaseGVN *phase) {
1734 Node *i1 = iff->in(1);
1735 if( !i1->is_Bool() ) return nullptr;
1736 BoolNode *bol = i1->as_Bool();
1737
1738 Node *cmp = bol->in(1);
1739 if( cmp->Opcode() != Op_CmpI ) return nullptr;
1740
1741 // Must be comparing against a bool
1742 const Type *cmp2_t = phase->type( cmp->in(2) );
1743 if( cmp2_t != TypeInt::ZERO &&
1744 cmp2_t != TypeInt::ONE )
1745 return nullptr;
1746
1747 // Find a prior merge point merging the boolean
1748 i1 = cmp->in(1);
1749 if( !i1->is_Phi() ) return nullptr;
1750 PhiNode *phi = i1->as_Phi();
1751 if( phase->type( phi ) != TypeInt::BOOL )
1752 return nullptr;
1753
1754 // Check for diamond pattern
1755 int true_path = phi->is_diamond_phi();
1756 if( true_path == 0 ) return nullptr;
1757
1758 // Make sure that iff and the control of the phi are different. This
1759 // should really only happen for dead control flow since it requires
1760 // an illegal cycle.
1761 if (phi->in(0)->in(1)->in(0) == iff) return nullptr;
1762
1763 // phi->region->if_proj->ifnode->bool->cmp
1764 BoolNode *bol2 = phi->in(0)->in(1)->in(0)->in(1)->as_Bool();
1765
1766 // Now get the 'sense' of the test correct so we can plug in
1767 // either iff2->in(1) or its complement.
1768 int flip = 0;
1769 if( bol->_test._test == BoolTest::ne ) flip = 1-flip;
1770 else if( bol->_test._test != BoolTest::eq ) return nullptr;
1771 if( cmp2_t == TypeInt::ZERO ) flip = 1-flip;
1772
1773 const Type *phi1_t = phase->type( phi->in(1) );
1774 const Type *phi2_t = phase->type( phi->in(2) );
1775 // Check for Phi(0,1) and flip
1776 if( phi1_t == TypeInt::ZERO ) {
1777 if( phi2_t != TypeInt::ONE ) return nullptr;
1778 flip = 1-flip;
1779 } else {
1780 // Check for Phi(1,0)
1781 if( phi1_t != TypeInt::ONE ) return nullptr;
1782 if( phi2_t != TypeInt::ZERO ) return nullptr;
1783 }
1784 if( true_path == 2 ) {
1785 flip = 1-flip;
1786 }
1787
1788 Node* new_bol = (flip ? phase->transform( bol2->negate(phase) ) : bol2);
1789 assert(new_bol != iff->in(1), "must make progress");
1790 iff->set_req_X(1, new_bol, phase);
1791 // Intervening diamond probably goes dead
1792 phase->C->set_major_progress();
1793 return iff;
1794 }
1795
1796 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff);
1797
1798 struct RangeCheck {
1799 IfProjNode* ctl;
1800 jint off;
1801 };
1802
1803 Node* IfNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
1804 if (remove_dead_region(phase, can_reshape)) return this;
1805 // No Def-Use info?
1806 if (!can_reshape) return nullptr;
1807
1808 // Don't bother trying to transform a dead if
1809 if (in(0)->is_top()) return nullptr;
1810 // Don't bother trying to transform an if with a dead test
1811 if (in(1)->is_top()) return nullptr;
1812 // Another variation of a dead test
1813 if (in(1)->is_Con()) return nullptr;
1814 // Another variation of a dead if
1815 if (outcnt() < 2) return nullptr;
1816
1817 // Canonicalize the test.
1818 Node* idt_if = idealize_test(phase, this);
1819 if (idt_if != nullptr) return idt_if;
1820
1821 // Try to split the IF
1822 PhaseIterGVN *igvn = phase->is_IterGVN();
1823 Node *s = split_if(this, igvn);
1824 if (s != nullptr) return s;
1825
1826 return NodeSentinel;
1827 }
1828
1829 //------------------------------Ideal------------------------------------------
1830 // Return a node which is more "ideal" than the current node. Strip out
1831 // control copies
1832 Node* IfNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1833 Node* res = Ideal_common(phase, can_reshape);
1834 if (res != NodeSentinel) {
1835 return res;
1836 }
1837
1838 // Check for people making a useless boolean: things like
1839 // if( (x < y ? true : false) ) { ... }
1840 // Replace with if( x < y ) { ... }
1841 Node* bol2 = remove_useless_bool(this, phase);
1842 if (bol2) return bol2;
1843
1844 if (in(0) == nullptr) return nullptr; // Dead loop?
1845
1846 PhaseIterGVN* igvn = phase->is_IterGVN();
1847 Node* result = fold_compares(igvn);
1848 if (result != nullptr) {
1849 return result;
1850 }
1851
1852 // Scan for an equivalent test
1853 int dist = 4; // Cutoff limit for search
1854 if (is_If() && in(1)->is_Bool()) {
1855 Node* cmp = in(1)->in(1);
1856 if (cmp->Opcode() == Op_CmpP &&
1857 cmp->in(2) != nullptr && // make sure cmp is not already dead
1858 cmp->in(2)->bottom_type() == TypePtr::NULL_PTR) {
1859 dist = 64; // Limit for null-pointer scans
1860 }
1861 }
1862
1863 Node* prev_dom = search_identical(dist, igvn);
1864
1865 if (prev_dom != nullptr) {
1866 // Dominating CountedLoopEnd (left over from some now dead loop) will become the new loop exit. Outer strip mined
1867 // loop will go away. Mark this loop as no longer strip mined.
1868 if (is_CountedLoopEnd()) {
1869 CountedLoopNode* counted_loop_node = as_CountedLoopEnd()->loopnode();
1870 if (counted_loop_node != nullptr) {
1871 counted_loop_node->clear_strip_mined();
1872 }
1873 }
1874 // Replace dominated IfNode
1875 return dominated_by(prev_dom, igvn, false);
1876 }
1877
1878 return simple_subsuming(igvn);
1879 }
1880
1881 //------------------------------dominated_by-----------------------------------
1882 Node* IfNode::dominated_by(Node* prev_dom, PhaseIterGVN* igvn, bool prev_dom_not_imply_this) {
1883 #ifndef PRODUCT
1884 if (TraceIterativeGVN) {
1885 tty->print(" Removing IfNode: "); this->dump();
1886 }
1887 #endif
1888
1889 igvn->hash_delete(this); // Remove self to prevent spurious V-N
1890 Node *idom = in(0);
1891 // Need opcode to decide which way 'this' test goes
1892 int prev_op = prev_dom->Opcode();
1893 Node *top = igvn->C->top(); // Shortcut to top
1894
1895 // Now walk the current IfNode's projections.
1896 // Loop ends when 'this' has no more uses.
1897 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; --i) {
1898 Node *ifp = last_out(i); // Get IfTrue/IfFalse
1899 igvn->add_users_to_worklist(ifp);
1900 // Check which projection it is and set target.
1901 // Data-target is either the dominating projection of the same type
1902 // or TOP if the dominating projection is of opposite type.
1903 // Data-target will be used as the new control edge for the non-CFG
1904 // nodes like Casts and Loads.
1905 Node *data_target = (ifp->Opcode() == prev_op) ? prev_dom : top;
1906 // Control-target is just the If's immediate dominator or TOP.
1907 Node *ctrl_target = (ifp->Opcode() == prev_op) ? idom : top;
1908
1909 // For each child of an IfTrue/IfFalse projection, reroute.
1910 // Loop ends when projection has no more uses.
1911 for (DUIterator_Last jmin, j = ifp->last_outs(jmin); j >= jmin; --j) {
1912 Node* s = ifp->last_out(j); // Get child of IfTrue/IfFalse
1913 if (s->depends_only_on_test()) {
1914 // For control producers
1915 igvn->replace_input_of(s, 0, data_target); // Move child to data-target
1916 if (prev_dom_not_imply_this && data_target != top) {
1917 // If prev_dom_not_imply_this, s now depends on multiple tests with prev_dom being the
1918 // lowest dominating one. As a result, it must be pinned there. Otherwise, it can be
1919 // incorrectly moved to a dominating test equivalent to the lowest one here.
1920 Node* clone = s->pin_node_under_control();
1921 if (clone != nullptr) {
1922 igvn->register_new_node_with_optimizer(clone, s);
1923 igvn->replace_node(s, clone);
1924 }
1925 }
1926 } else {
1927 // Find the control input matching this def-use edge.
1928 // For Regions it may not be in slot 0.
1929 uint l;
1930 for (l = 0; s->in(l) != ifp; l++) { }
1931 igvn->replace_input_of(s, l, ctrl_target);
1932 }
1933 } // End for each child of a projection
1934
1935 igvn->remove_dead_node(ifp, PhaseIterGVN::NodeOrigin::Graph);
1936 } // End for each IfTrue/IfFalse child of If
1937
1938 // Kill the IfNode
1939 igvn->remove_dead_node(this, PhaseIterGVN::NodeOrigin::Graph);
1940
1941 // Must return either the original node (now dead) or a new node
1942 // (Do not return a top here, since that would break the uniqueness of top.)
1943 return new ConINode(TypeInt::ZERO);
1944 }
1945
1946 Node* IfNode::search_identical(int dist, PhaseIterGVN* igvn) {
1947 // Setup to scan up the CFG looking for a dominating test
1948 Node* dom = in(0);
1949 Node* prev_dom = this;
1950 int op = Opcode();
1951 // Search up the dominator tree for an If with an identical test
1952 while (dom->Opcode() != op || // Not same opcode?
1953 !same_condition(dom, igvn) || // Not same input 1?
1954 prev_dom->in(0) != dom) { // One path of test does not dominate?
1955 if (dist < 0) return nullptr;
1956
1957 dist--;
1958 prev_dom = dom;
1959 dom = up_one_dom(dom);
1960 if (!dom) return nullptr;
1961 }
1962
1963 // Check that we did not follow a loop back to ourselves
1964 if (this == dom) {
1965 return nullptr;
1966 }
1967
1968 #ifndef PRODUCT
1969 if (dist > 2) { // Add to count of null checks elided
1970 explicit_null_checks_elided++;
1971 }
1972 #endif
1973
1974 return prev_dom;
1975 }
1976
1977 bool IfNode::same_condition(const Node* dom, PhaseIterGVN* igvn) const {
1978 Node* dom_bool = dom->in(1);
1979 Node* this_bool = in(1);
1980 if (dom_bool == this_bool) {
1981 return true;
1982 }
1983
1984 if (dom_bool == nullptr || !dom_bool->is_Bool() ||
1985 this_bool == nullptr || !this_bool->is_Bool()) {
1986 return false;
1987 }
1988 Node* dom_cmp = dom_bool->in(1);
1989 Node* this_cmp = this_bool->in(1);
1990
1991 // If the comparison is a subtype check, then SubTypeCheck nodes may have profile data attached to them and may be
1992 // different nodes even-though they perform the same subtype check
1993 if (dom_cmp == nullptr || !dom_cmp->is_SubTypeCheck() ||
1994 this_cmp == nullptr || !this_cmp->is_SubTypeCheck()) {
1995 return false;
1996 }
1997
1998 if (dom_cmp->in(1) != this_cmp->in(1) ||
1999 dom_cmp->in(2) != this_cmp->in(2) ||
2000 dom_bool->as_Bool()->_test._test != this_bool->as_Bool()->_test._test) {
2001 return false;
2002 }
2003
2004 return true;
2005 }
2006
2007 void IfNode::mark_projections_unsafe_for_fold_compare() const {
2008 // With the following code pattern
2009 //
2010 // if (some_condition) {
2011 // v = 0;
2012 // } else {
2013 // v = 1;
2014 // } // v is Phi(0, 1)
2015 // if (v == 0) {
2016 // uncommon_trap(); // reexecutes the "if (v == 0) {" above, captures v as stack argument to ifeq bytecode
2017 // }
2018 // if (some_other_condition) {
2019 // uncommon_trap(); // reexecutes the "if (some_other_condition) {"
2020 // }
2021 //
2022 // if the second if is split thru Phi, the result is:
2023 //
2024 // if (some_condition) {
2025 // uncommon_trap(); // reexecutes the "if (v == 0) {" that was removed above, captures v = 0 as stack argument to ifeq bytecode
2026 // }
2027 // if (some_other_condition) {
2028 // uncommon_trap(); // reexecutes the "if (some_other_condition) {"
2029 // }
2030 //
2031 // some_condition and some_other_condition could be folded into
2032 // a single new condition that is narrower than some_condition
2033 // (done by IfNode::fold_compares(), for instance):
2034 //
2035 // if (combined_narrower_condition) {
2036 // uncommon_trap(); // reexecutes the "if (v == 0) {" that was removed, captures v = 0 as stack argument to ifeq bytecode
2037 // }
2038 //
2039 // Then combined_narrower_condition is true for some input value for
2040 // which some_condition is false. When such an input value is used
2041 // at runtime, the trap is taken which causes "if (v == 0) {" to be
2042 // reexecuted with v = 0 even though some_condition is wrong, causing
2043 // the wrong branch to be executed.
2044 //
2045 // Mark the uncommon trap nodes to prevent such a transformation
2046 // from happening.
2047 IfProjNode* true_projection = true_proj();
2048 IfProjNode* false_projection = false_proj();
2049 CallStaticJavaNode* unc = true_projection->is_uncommon_trap_proj();
2050 if (unc != nullptr) {
2051 unc->clear_safe_for_fold_compare();
2052 }
2053 unc = false_projection->is_uncommon_trap_proj();
2054 if (unc != nullptr) {
2055 unc->clear_safe_for_fold_compare();
2056 }
2057 }
2058
2059 static int subsuming_bool_test_encode(Node*);
2060
2061 // Check if dominating test is subsuming 'this' one.
2062 //
2063 // cmp
2064 // / \
2065 // (r1) bool \
2066 // / bool (r2)
2067 // (dom) if \
2068 // \ )
2069 // (pre) if[TF] /
2070 // \ /
2071 // if (this)
2072 // \r1
2073 // r2\ eqT eqF neT neF ltT ltF leT leF gtT gtF geT geF
2074 // eq t f f t f - - f f - - f
2075 // ne f t t f t - - t t - - t
2076 // lt f - - f t f - f f - f t
2077 // le t - - t t - t f f t - t
2078 // gt f - - f f - f t t f - f
2079 // ge t - - t f t - t t - t f
2080 //
2081 Node* IfNode::simple_subsuming(PhaseIterGVN* igvn) {
2082 // Table encoding: N/A (na), True-branch (tb), False-branch (fb).
2083 static enum { na, tb, fb } s_short_circuit_map[6][12] = {
2084 /*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*/
2085 /*eq*/{ tb, fb, fb, tb, fb, na, na, fb, fb, na, na, fb },
2086 /*ne*/{ fb, tb, tb, fb, tb, na, na, tb, tb, na, na, tb },
2087 /*lt*/{ fb, na, na, fb, tb, fb, na, fb, fb, na, fb, tb },
2088 /*le*/{ tb, na, na, tb, tb, na, tb, fb, fb, tb, na, tb },
2089 /*gt*/{ fb, na, na, fb, fb, na, fb, tb, tb, fb, na, fb },
2090 /*ge*/{ tb, na, na, tb, fb, tb, na, tb, tb, na, tb, fb }};
2091
2092 Node* pre = in(0);
2093 if (!pre->is_IfTrue() && !pre->is_IfFalse()) {
2094 return nullptr;
2095 }
2096 Node* dom = pre->in(0);
2097 if (!dom->is_If()) {
2098 return nullptr;
2099 }
2100 Node* bol = in(1);
2101 if (!bol->is_Bool()) {
2102 return nullptr;
2103 }
2104 Node* cmp = in(1)->in(1);
2105 if (!cmp->is_Cmp()) {
2106 return nullptr;
2107 }
2108
2109 if (!dom->in(1)->is_Bool()) {
2110 return nullptr;
2111 }
2112 if (dom->in(1)->in(1) != cmp) { // Not same cond?
2113 return nullptr;
2114 }
2115
2116 int drel = subsuming_bool_test_encode(dom->in(1));
2117 int trel = subsuming_bool_test_encode(bol);
2118 int bout = pre->is_IfFalse() ? 1 : 0;
2119
2120 if (drel < 0 || trel < 0) {
2121 return nullptr;
2122 }
2123 int br = s_short_circuit_map[trel][2*drel+bout];
2124 if (br == na) {
2125 return nullptr;
2126 }
2127 #ifndef PRODUCT
2128 if (TraceIterativeGVN) {
2129 tty->print(" Subsumed IfNode: "); dump();
2130 }
2131 #endif
2132 // Replace condition with constant True(1)/False(0).
2133 bool is_always_true = br == tb;
2134 set_req(1, igvn->intcon(is_always_true ? 1 : 0));
2135
2136 // Update any data dependencies to the directly dominating test. This subsumed test is not immediately removed by igvn
2137 // and therefore subsequent optimizations might miss these data dependencies otherwise. There might be a dead loop
2138 // ('always_taken_proj' == 'pre') that is cleaned up later. Skip this case to make the iterator work properly.
2139 Node* always_taken_proj = proj_out(is_always_true);
2140 if (always_taken_proj != pre) {
2141 for (DUIterator_Fast imax, i = always_taken_proj->fast_outs(imax); i < imax; i++) {
2142 Node* u = always_taken_proj->fast_out(i);
2143 if (!u->is_CFG()) {
2144 igvn->replace_input_of(u, 0, pre);
2145 --i;
2146 --imax;
2147 }
2148 }
2149 }
2150
2151 if (bol->outcnt() == 0) {
2152 igvn->remove_dead_node(bol, PhaseIterGVN::NodeOrigin::Graph); // Kill the BoolNode.
2153 }
2154 return this;
2155 }
2156
2157 // Map BoolTest to local table encoding. The BoolTest (e)numerals
2158 // { eq = 0, ne = 4, le = 5, ge = 7, lt = 3, gt = 1 }
2159 // are mapped to table indices, while the remaining (e)numerals in BoolTest
2160 // { overflow = 2, no_overflow = 6, never = 8, illegal = 9 }
2161 // are ignored (these are not modeled in the table).
2162 //
2163 static int subsuming_bool_test_encode(Node* node) {
2164 precond(node->is_Bool());
2165 BoolTest::mask x = node->as_Bool()->_test._test;
2166 switch (x) {
2167 case BoolTest::eq: return 0;
2168 case BoolTest::ne: return 1;
2169 case BoolTest::lt: return 2;
2170 case BoolTest::le: return 3;
2171 case BoolTest::gt: return 4;
2172 case BoolTest::ge: return 5;
2173 case BoolTest::overflow:
2174 case BoolTest::no_overflow:
2175 case BoolTest::never:
2176 case BoolTest::illegal:
2177 default:
2178 return -1;
2179 }
2180 }
2181
2182 //------------------------------Identity---------------------------------------
2183 // If the test is constant & we match, then we are the input Control
2184 Node* IfProjNode::Identity(PhaseGVN* phase) {
2185 // Can only optimize if cannot go the other way
2186 const TypeTuple *t = phase->type(in(0))->is_tuple();
2187 if (t == TypeTuple::IFNEITHER || (always_taken(t) &&
2188 // During parsing (GVN) we don't remove dead code aggressively.
2189 // Cut off dead branch and let PhaseRemoveUseless take care of it.
2190 (!phase->is_IterGVN() ||
2191 // During IGVN, first wait for the dead branch to be killed.
2192 // Otherwise, the IfNode's control will have two control uses (the IfNode
2193 // that doesn't go away because it still has uses and this branch of the
2194 // If) which breaks other optimizations. Node::has_special_unique_user()
2195 // will cause this node to be reprocessed once the dead branch is killed.
2196 in(0)->outcnt() == 1))) {
2197 // IfNode control
2198 if (in(0)->is_BaseCountedLoopEnd()) {
2199 // CountedLoopEndNode may be eliminated by if subsuming, replace CountedLoopNode with LoopNode to
2200 // avoid mismatching between CountedLoopNode and CountedLoopEndNode in the following optimization.
2201 Node* head = unique_ctrl_out_or_null();
2202 if (head != nullptr && head->is_BaseCountedLoop() && head->in(LoopNode::LoopBackControl) == this) {
2203 Node* new_head = new LoopNode(head->in(LoopNode::EntryControl), this);
2204 phase->is_IterGVN()->register_new_node_with_optimizer(new_head);
2205 phase->is_IterGVN()->replace_node(head, new_head);
2206 }
2207 }
2208 return in(0)->in(0);
2209 }
2210 // no progress
2211 return this;
2212 }
2213
2214 bool IfNode::is_zero_trip_guard() const {
2215 if (in(1)->is_Bool() && in(1)->in(1)->is_Cmp()) {
2216 return in(1)->in(1)->in(1)->Opcode() == Op_OpaqueZeroTripGuard;
2217 }
2218 return false;
2219 }
2220
2221 void IfProjNode::pin_dependent_nodes(PhaseIterGVN* igvn) {
2222 for (DUIterator i = outs(); has_out(i); i++) {
2223 Node* u = out(i);
2224 if (!u->depends_only_on_test()) {
2225 continue;
2226 }
2227 Node* clone = u->pin_node_under_control();
2228 if (clone != nullptr) {
2229 igvn->register_new_node_with_optimizer(clone, u);
2230 igvn->replace_node(u, clone);
2231 --i;
2232 }
2233 }
2234 }
2235
2236 #ifndef PRODUCT
2237 void IfNode::dump_spec(outputStream* st) const {
2238 switch (_assertion_predicate_type) {
2239 case AssertionPredicateType::InitValue:
2240 st->print("#Init Value Assertion Predicate ");
2241 break;
2242 case AssertionPredicateType::LastValue:
2243 st->print("#Last Value Assertion Predicate ");
2244 break;
2245 case AssertionPredicateType::FinalIv:
2246 st->print("#Final IV Assertion Predicate ");
2247 break;
2248 case AssertionPredicateType::None:
2249 // No Assertion Predicate
2250 break;
2251 default:
2252 fatal("Unknown Assertion Predicate type");
2253 }
2254 st->print("P=%f, C=%f", _prob, _fcnt);
2255 }
2256 #endif // NOT PRODUCT
2257
2258 //------------------------------idealize_test----------------------------------
2259 // Try to canonicalize tests better. Peek at the Cmp/Bool/If sequence and
2260 // come up with a canonical sequence. Bools getting 'eq', 'gt' and 'ge' forms
2261 // converted to 'ne', 'le' and 'lt' forms. IfTrue/IfFalse get swapped as
2262 // needed.
2263 static IfNode* idealize_test(PhaseGVN* phase, IfNode* iff) {
2264 assert(iff->in(0) != nullptr, "If must be live");
2265
2266 if (iff->outcnt() != 2) return nullptr; // Malformed projections.
2267 IfFalseNode* old_if_f = iff->false_proj();
2268 IfTrueNode* old_if_t = iff->true_proj();
2269
2270 // CountedLoopEnds want the back-control test to be TRUE, regardless of
2271 // whether they are testing a 'gt' or 'lt' condition. The 'gt' condition
2272 // happens in count-down loops
2273 if (iff->is_BaseCountedLoopEnd()) return nullptr;
2274 if (!iff->in(1)->is_Bool()) return nullptr; // Happens for partially optimized IF tests
2275 BoolNode *b = iff->in(1)->as_Bool();
2276 BoolTest bt = b->_test;
2277 // Test already in good order?
2278 if( bt.is_canonical() )
2279 return nullptr;
2280
2281 // Flip test to be canonical. Requires flipping the IfFalse/IfTrue and
2282 // cloning the IfNode.
2283 Node* new_b = phase->transform( new BoolNode(b->in(1), bt.negate()) );
2284 if( !new_b->is_Bool() ) return nullptr;
2285 b = new_b->as_Bool();
2286
2287 PhaseIterGVN *igvn = phase->is_IterGVN();
2288 assert( igvn, "Test is not canonical in parser?" );
2289
2290 // The IF node never really changes, but it needs to be cloned
2291 iff = iff->clone()->as_If();
2292 iff->set_req(1, b);
2293 iff->_prob = 1.0-iff->_prob;
2294
2295 Node *prior = igvn->hash_find_insert(iff);
2296 if( prior ) {
2297 igvn->remove_dead_node(iff, PhaseIterGVN::NodeOrigin::Graph);
2298 iff = (IfNode*)prior;
2299 } else {
2300 // Cannot call transform on it just yet
2301 igvn->set_type_bottom(iff);
2302 }
2303 igvn->_worklist.push(iff);
2304
2305 // Now handle projections. Cloning not required.
2306 Node* new_if_f = (Node*)(new IfFalseNode( iff ));
2307 Node* new_if_t = (Node*)(new IfTrueNode ( iff ));
2308
2309 igvn->register_new_node_with_optimizer(new_if_f);
2310 igvn->register_new_node_with_optimizer(new_if_t);
2311 // Flip test, so flip trailing control
2312 igvn->replace_node(old_if_f, new_if_t);
2313 igvn->replace_node(old_if_t, new_if_f);
2314
2315 // Progress
2316 return iff;
2317 }
2318
2319 Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2320 Node* res = Ideal_common(phase, can_reshape);
2321 if (res != NodeSentinel) {
2322 return res;
2323 }
2324
2325 PhaseIterGVN *igvn = phase->is_IterGVN();
2326 // Setup to scan up the CFG looking for a dominating test
2327 Node* prev_dom = this;
2328
2329 // Check for range-check vs other kinds of tests
2330 Node* index1;
2331 Node* range1;
2332 jint offset1;
2333 int flip1 = is_range_check(range1, index1, offset1);
2334 if (flip1) {
2335 Node* dom = in(0);
2336 // Try to remove extra range checks. All 'up_one_dom' gives up at merges
2337 // so all checks we inspect post-dominate the top-most check we find.
2338 // If we are going to fail the current check and we reach the top check
2339 // then we are guaranteed to fail, so just start interpreting there.
2340 // We 'expand' the top 3 range checks to include all post-dominating
2341 // checks.
2342 //
2343 // Example:
2344 // a[i+x] // (1) 1 < x < 6
2345 // a[i+3] // (2)
2346 // a[i+4] // (3)
2347 // a[i+6] // max = max of all constants
2348 // a[i+2]
2349 // a[i+1] // min = min of all constants
2350 //
2351 // If x < 3:
2352 // (1) a[i+x]: Leave unchanged
2353 // (2) a[i+3]: Replace with a[i+max] = a[i+6]: i+x < i+3 <= i+6 -> (2) is covered
2354 // (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
2355 // Remove all other a[i+c] checks
2356 //
2357 // If x >= 3:
2358 // (1) a[i+x]: Leave unchanged
2359 // (2) a[i+3]: Replace with a[i+min] = a[i+1]: i+1 < i+3 <= i+x -> (2) is covered
2360 // (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
2361 // Remove all other a[i+c] checks
2362 //
2363 // We only need the top 2 range checks if x is the min or max of all constants.
2364 //
2365 // This, however, only works if the interval [i+min,i+max] is not larger than max_int (i.e. abs(max - min) < max_int):
2366 // The theoretical max size of an array is max_int with:
2367 // - Valid index space: [0,max_int-1]
2368 // - Invalid index space: [max_int,-1] // max_int, min_int, min_int - 1 ..., -1
2369 //
2370 // The size of the consecutive valid index space is smaller than the size of the consecutive invalid index space.
2371 // If we choose min and max in such a way that:
2372 // - abs(max - min) < max_int
2373 // - i+max and i+min are inside the valid index space
2374 // then all indices [i+min,i+max] must be in the valid index space. Otherwise, the invalid index space must be
2375 // smaller than the valid index space which is never the case for any array size.
2376 //
2377 // Choosing a smaller array size only makes the valid index space smaller and the invalid index space larger and
2378 // the argument above still holds.
2379 //
2380 // Note that the same optimization with the same maximal accepted interval size can also be found in C1.
2381 const jlong maximum_number_of_min_max_interval_indices = (jlong)max_jint;
2382
2383 // The top 3 range checks seen
2384 const int NRC = 3;
2385 RangeCheck prev_checks[NRC];
2386 int nb_checks = 0;
2387
2388 // Low and high offsets seen so far
2389 jint off_lo = offset1;
2390 jint off_hi = offset1;
2391
2392 bool found_immediate_dominator = false;
2393
2394 // Scan for the top checks and collect range of offsets
2395 for (int dist = 0; dist < 999; dist++) { // Range-Check scan limit
2396 if (dom->Opcode() == Op_RangeCheck && // Not same opcode?
2397 prev_dom->in(0) == dom) { // One path of test does dominate?
2398 if (dom == this) return nullptr; // dead loop
2399 // See if this is a range check
2400 Node* index2;
2401 Node* range2;
2402 jint offset2;
2403 int flip2 = dom->as_RangeCheck()->is_range_check(range2, index2, offset2);
2404 // See if this is a _matching_ range check, checking against
2405 // the same array bounds.
2406 if (flip2 == flip1 && range2 == range1 && index2 == index1 &&
2407 dom->outcnt() == 2) {
2408 if (nb_checks == 0 && dom->in(1) == in(1)) {
2409 // Found an immediately dominating test at the same offset.
2410 // This kind of back-to-back test can be eliminated locally,
2411 // and there is no need to search further for dominating tests.
2412 assert(offset2 == offset1, "Same test but different offsets");
2413 found_immediate_dominator = true;
2414 break;
2415 }
2416
2417 // "x - y" -> must add one to the difference for number of elements in [x,y]
2418 const jlong diff = (jlong)MIN2(offset2, off_lo) - (jlong)MAX2(offset2, off_hi);
2419 if (ABS(diff) < maximum_number_of_min_max_interval_indices) {
2420 // Gather expanded bounds
2421 off_lo = MIN2(off_lo, offset2);
2422 off_hi = MAX2(off_hi, offset2);
2423 // Record top NRC range checks
2424 prev_checks[nb_checks % NRC].ctl = prev_dom->as_IfProj();
2425 prev_checks[nb_checks % NRC].off = offset2;
2426 nb_checks++;
2427 }
2428 }
2429 }
2430 prev_dom = dom;
2431 dom = up_one_dom(dom);
2432 if (!dom) break;
2433 }
2434
2435 if (!found_immediate_dominator) {
2436 // Attempt to widen the dominating range check to cover some later
2437 // ones. Since range checks "fail" by uncommon-trapping to the
2438 // interpreter, widening a check can make us speculatively enter
2439 // the interpreter. If we see range-check deopt's, do not widen!
2440 if (!phase->C->allow_range_check_smearing()) return nullptr;
2441
2442 if (can_reshape && !phase->C->post_loop_opts_phase()) {
2443 // We are about to perform range check smearing (i.e. remove this RangeCheck if it is dominated by
2444 // a series of RangeChecks which have a range that covers this RangeCheck). This can cause array access nodes to
2445 // be pinned. We want to avoid that and first allow range check elimination a chance to remove the RangeChecks
2446 // from loops. Hence, we delay range check smearing until after loop opts.
2447 phase->C->record_for_post_loop_opts_igvn(this);
2448 return nullptr;
2449 }
2450
2451 // Didn't find prior covering check, so cannot remove anything.
2452 if (nb_checks == 0) {
2453 return nullptr;
2454 }
2455 // Constant indices only need to check the upper bound.
2456 // Non-constant indices must check both low and high.
2457 int chk0 = (nb_checks - 1) % NRC;
2458 if (index1) {
2459 if (nb_checks == 1) {
2460 return nullptr;
2461 } else {
2462 // If the top range check's constant is the min or max of
2463 // all constants we widen the next one to cover the whole
2464 // range of constants.
2465 RangeCheck rc0 = prev_checks[chk0];
2466 int chk1 = (nb_checks - 2) % NRC;
2467 RangeCheck rc1 = prev_checks[chk1];
2468 if (rc0.off == off_lo) {
2469 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
2470 prev_dom = rc1.ctl;
2471 } else if (rc0.off == off_hi) {
2472 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
2473 prev_dom = rc1.ctl;
2474 } else {
2475 // If the top test's constant is not the min or max of all
2476 // constants, we need 3 range checks. We must leave the
2477 // top test unchanged because widening it would allow the
2478 // accesses it protects to successfully read/write out of
2479 // bounds.
2480 if (nb_checks == 2) {
2481 return nullptr;
2482 }
2483 int chk2 = (nb_checks - 3) % NRC;
2484 RangeCheck rc2 = prev_checks[chk2];
2485 // The top range check a+i covers interval: -a <= i < length-a
2486 // The second range check b+i covers interval: -b <= i < length-b
2487 if (rc1.off <= rc0.off) {
2488 // if b <= a, we change the second range check to:
2489 // -min_of_all_constants <= i < length-min_of_all_constants
2490 // Together top and second range checks now cover:
2491 // -min_of_all_constants <= i < length-a
2492 // which is more restrictive than -b <= i < length-b:
2493 // -b <= -min_of_all_constants <= i < length-a <= length-b
2494 // The third check is then changed to:
2495 // -max_of_all_constants <= i < length-max_of_all_constants
2496 // so 2nd and 3rd checks restrict allowed values of i to:
2497 // -min_of_all_constants <= i < length-max_of_all_constants
2498 adjust_check(rc1.ctl, range1, index1, flip1, off_lo, igvn);
2499 adjust_check(rc2.ctl, range1, index1, flip1, off_hi, igvn);
2500 } else {
2501 // if b > a, we change the second range check to:
2502 // -max_of_all_constants <= i < length-max_of_all_constants
2503 // Together top and second range checks now cover:
2504 // -a <= i < length-max_of_all_constants
2505 // which is more restrictive than -b <= i < length-b:
2506 // -b < -a <= i < length-max_of_all_constants <= length-b
2507 // The third check is then changed to:
2508 // -max_of_all_constants <= i < length-max_of_all_constants
2509 // so 2nd and 3rd checks restrict allowed values of i to:
2510 // -min_of_all_constants <= i < length-max_of_all_constants
2511 adjust_check(rc1.ctl, range1, index1, flip1, off_hi, igvn);
2512 adjust_check(rc2.ctl, range1, index1, flip1, off_lo, igvn);
2513 }
2514 prev_dom = rc2.ctl;
2515 }
2516 }
2517 } else {
2518 RangeCheck rc0 = prev_checks[chk0];
2519 // 'Widen' the offset of the 1st and only covering check
2520 adjust_check(rc0.ctl, range1, index1, flip1, off_hi, igvn);
2521 // Test is now covered by prior checks, dominate it out
2522 prev_dom = rc0.ctl;
2523 }
2524 // The last RangeCheck is found to be redundant with a sequence of n (n >= 2) preceding RangeChecks.
2525 // If an array load is control dependent on the eliminated range check, the array load nodes (CastII and Load)
2526 // become control dependent on the last range check of the sequence, but they are really dependent on the entire
2527 // sequence of RangeChecks. If RangeCheck#n is later replaced by a dominating identical check, the array load
2528 // nodes must not float above the n-1 other RangeCheck in the sequence. We pin the array load nodes here to
2529 // guarantee it doesn't happen.
2530 //
2531 // RangeCheck#1 RangeCheck#1
2532 // | \ | \
2533 // | uncommon trap | uncommon trap
2534 // .. ..
2535 // RangeCheck#n -> RangeCheck#n
2536 // | \ | \
2537 // | uncommon trap CastII uncommon trap
2538 // RangeCheck Load
2539 // | \
2540 // CastII uncommon trap
2541 // Load
2542
2543 return dominated_by(prev_dom, igvn, true);
2544 }
2545 } else {
2546 prev_dom = search_identical(4, igvn);
2547
2548 if (prev_dom == nullptr) {
2549 return nullptr;
2550 }
2551 }
2552
2553 // Replace dominated IfNode
2554 return dominated_by(prev_dom, igvn, false);
2555 }
2556
2557 ParsePredicateNode::ParsePredicateNode(Node* control, Deoptimization::DeoptReason deopt_reason, PhaseGVN* gvn)
2558 : IfNode(control, gvn->intcon(1), PROB_MAX, COUNT_UNKNOWN),
2559 _deopt_reason(deopt_reason),
2560 _predicate_state(PredicateState::Useful) {
2561 init_class_id(Class_ParsePredicate);
2562 gvn->C->add_parse_predicate(this);
2563 gvn->C->record_for_post_loop_opts_igvn(this);
2564 #ifdef ASSERT
2565 switch (deopt_reason) {
2566 case Deoptimization::Reason_predicate:
2567 case Deoptimization::Reason_profile_predicate:
2568 case Deoptimization::Reason_auto_vectorization_check:
2569 case Deoptimization::Reason_loop_limit_check:
2570 case Deoptimization::Reason_short_running_long_loop:
2571 break;
2572 default:
2573 assert(false, "unsupported deoptimization reason for Parse Predicate");
2574 }
2575 #endif // ASSERT
2576 }
2577
2578 void ParsePredicateNode::mark_useless(PhaseIterGVN& igvn) {
2579 _predicate_state = PredicateState::Useless;
2580 igvn._worklist.push(this);
2581 }
2582
2583 Node* ParsePredicateNode::uncommon_trap() const {
2584 ParsePredicateUncommonProj* uncommon_proj = false_proj();
2585 Node* uct_region_or_call = uncommon_proj->unique_ctrl_out();
2586 assert(uct_region_or_call->is_Region() || uct_region_or_call->is_Call(), "must be a region or call uct");
2587 return uct_region_or_call;
2588 }
2589
2590 // Fold this node away once it becomes useless or at latest in post loop opts IGVN.
2591 const Type* ParsePredicateNode::Value(PhaseGVN* phase) const {
2592 assert(_predicate_state != PredicateState::MaybeUseful, "should only be MaybeUseful when eliminating useless "
2593 "predicates during loop opts");
2594 if (phase->type(in(0)) == Type::TOP) {
2595 return Type::TOP;
2596 }
2597 if (_predicate_state == PredicateState::Useless || phase->C->post_loop_opts_phase()) {
2598 return TypeTuple::IFTRUE;
2599 }
2600 return bottom_type();
2601 }
2602
2603 #ifndef PRODUCT
2604 void ParsePredicateNode::dump_spec(outputStream* st) const {
2605 st->print(" #");
2606 switch (_deopt_reason) {
2607 case Deoptimization::DeoptReason::Reason_predicate:
2608 st->print("Loop ");
2609 break;
2610 case Deoptimization::DeoptReason::Reason_profile_predicate:
2611 st->print("Profiled_Loop ");
2612 break;
2613 case Deoptimization::DeoptReason::Reason_auto_vectorization_check:
2614 st->print("Auto_Vectorization_Check ");
2615 break;
2616 case Deoptimization::DeoptReason::Reason_loop_limit_check:
2617 st->print("Loop_Limit_Check ");
2618 break;
2619 case Deoptimization::DeoptReason::Reason_short_running_long_loop:
2620 st->print("Short_Running_Long_Loop ");
2621 break;
2622 default:
2623 fatal("unknown kind");
2624 }
2625 if (_predicate_state == PredicateState::Useless) {
2626 st->print("#useless ");
2627 }
2628 }
2629 #endif // NOT PRODUCT