-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathifnode.cpp
2237 lines (2032 loc) · 84.3 KB
/
ifnode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "ci/ciTypeFlow.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "opto/addnode.hpp"
#include "opto/castnode.hpp"
#include "opto/cfgnode.hpp"
#include "opto/connode.hpp"
#include "opto/loopnode.hpp"
#include "opto/phaseX.hpp"
#include "opto/predicates_enums.hpp"
#include "opto/runtime.hpp"
#include "opto/rootnode.hpp"
#include "opto/subnode.hpp"
#include "opto/subtypenode.hpp"
// Portions of code courtesy of Clifford Click
// Optimization - Graph Style
#ifndef PRODUCT
extern uint explicit_null_checks_elided;
#endif
IfNode::IfNode(Node* control, Node* bol, float p, float fcnt)
: MultiBranchNode(2),
_prob(p),
_fcnt(fcnt),
_assertion_predicate_type(AssertionPredicateType::None) {
init_node(control, bol);
}
IfNode::IfNode(Node* control, Node* bol, float p, float fcnt, AssertionPredicateType assertion_predicate_type)
: MultiBranchNode(2),
_prob(p),
_fcnt(fcnt),
_assertion_predicate_type(assertion_predicate_type) {
init_node(control, bol);
}
//=============================================================================
//------------------------------Value------------------------------------------
// Return a tuple for whichever arm of the IF is reachable
const Type* IfNode::Value(PhaseGVN* phase) const {
if( !in(0) ) return Type::TOP;
if( phase->type(in(0)) == Type::TOP )
return Type::TOP;
const Type *t = phase->type(in(1));
if( t == Type::TOP ) // data is undefined
return TypeTuple::IFNEITHER; // unreachable altogether
if( t == TypeInt::ZERO ) // zero, or false
return TypeTuple::IFFALSE; // only false branch is reachable
if( t == TypeInt::ONE ) // 1, or true
return TypeTuple::IFTRUE; // only true branch is reachable
assert( t == TypeInt::BOOL, "expected boolean type" );
return TypeTuple::IFBOTH; // No progress
}
const RegMask &IfNode::out_RegMask() const {
return RegMask::Empty;
}
//------------------------------split_if---------------------------------------
// Look for places where we merge constants, then test on the merged value.
// If the IF test will be constant folded on the path with the constant, we
// win by splitting the IF to before the merge point.
static Node* split_if(IfNode *iff, PhaseIterGVN *igvn) {
// I could be a lot more general here, but I'm trying to squeeze this
// in before the Christmas '98 break so I'm gonna be kinda restrictive
// on the patterns I accept. CNC
// Look for a compare of a constant and a merged value
Node *i1 = iff->in(1);
if( !i1->is_Bool() ) return nullptr;
BoolNode *b = i1->as_Bool();
Node *cmp = b->in(1);
if( !cmp->is_Cmp() ) return nullptr;
i1 = cmp->in(1);
if( i1 == nullptr || !i1->is_Phi() ) return nullptr;
PhiNode *phi = i1->as_Phi();
Node *con2 = cmp->in(2);
if( !con2->is_Con() ) return nullptr;
// See that the merge point contains some constants
Node *con1=nullptr;
uint i4;
RegionNode* phi_region = phi->region();
for (i4 = 1; i4 < phi->req(); i4++ ) {
con1 = phi->in(i4);
// Do not optimize partially collapsed merges
if (con1 == nullptr || phi_region->in(i4) == nullptr || igvn->type(phi_region->in(i4)) == Type::TOP) {
igvn->_worklist.push(iff);
return nullptr;
}
if( con1->is_Con() ) break; // Found a constant
// Also allow null-vs-not-null checks
const TypePtr *tp = igvn->type(con1)->isa_ptr();
if( tp && tp->_ptr == TypePtr::NotNull )
break;
}
if( i4 >= phi->req() ) return nullptr; // Found no constants
igvn->C->set_has_split_ifs(true); // Has chance for split-if
// Make sure that the compare can be constant folded away
Node *cmp2 = cmp->clone();
cmp2->set_req(1,con1);
cmp2->set_req(2,con2);
const Type *t = cmp2->Value(igvn);
// This compare is dead, so whack it!
igvn->remove_dead_node(cmp2);
if( !t->singleton() ) return nullptr;
// No intervening control, like a simple Call
Node* r = iff->in(0);
if (!r->is_Region() || r->is_Loop() || phi_region != r || r->as_Region()->is_copy()) {
return nullptr;
}
// No other users of the cmp/bool
if (b->outcnt() != 1 || cmp->outcnt() != 1) {
//tty->print_cr("many users of cmp/bool");
return nullptr;
}
// Make sure we can determine where all the uses of merged values go
for (DUIterator_Fast jmax, j = r->fast_outs(jmax); j < jmax; j++) {
Node* u = r->fast_out(j);
if( u == r ) continue;
if( u == iff ) continue;
if( u->outcnt() == 0 ) continue; // use is dead & ignorable
if( !u->is_Phi() ) {
/*
if( u->is_Start() ) {
tty->print_cr("Region has inlined start use");
} else {
tty->print_cr("Region has odd use");
u->dump(2);
}*/
return nullptr;
}
if( u != phi ) {
// CNC - do not allow any other merged value
//tty->print_cr("Merging another value");
//u->dump(2);
return nullptr;
}
// Make sure we can account for all Phi uses
for (DUIterator_Fast kmax, k = u->fast_outs(kmax); k < kmax; k++) {
Node* v = u->fast_out(k); // User of the phi
// CNC - Allow only really simple patterns.
// In particular I disallow AddP of the Phi, a fairly common pattern
if (v == cmp) continue; // The compare is OK
if (v->is_ConstraintCast()) {
// If the cast is derived from data flow edges, it may not have a control edge.
// If so, it should be safe to split. But follow-up code can not deal with
// this (l. 359). So skip.
if (v->in(0) == nullptr) {
return nullptr;
}
if (v->in(0)->in(0) == iff) {
continue; // CastPP/II of the IfNode is OK
}
}
// Disabled following code because I cannot tell if exactly one
// path dominates without a real dominator check. CNC 9/9/1999
//uint vop = v->Opcode();
//if( vop == Op_Phi ) { // Phi from another merge point might be OK
// Node *r = v->in(0); // Get controlling point
// if( !r ) return nullptr; // Degraded to a copy
// // Find exactly one path in (either True or False doms, but not IFF)
// int cnt = 0;
// for( uint i = 1; i < r->req(); i++ )
// if( r->in(i) && r->in(i)->in(0) == iff )
// cnt++;
// if( cnt == 1 ) continue; // Exactly one of True or False guards Phi
//}
if( !v->is_Call() ) {
/*
if( v->Opcode() == Op_AddP ) {
tty->print_cr("Phi has AddP use");
} else if( v->Opcode() == Op_CastPP ) {
tty->print_cr("Phi has CastPP use");
} else if( v->Opcode() == Op_CastII ) {
tty->print_cr("Phi has CastII use");
} else {
tty->print_cr("Phi has use I can't be bothered with");
}
*/
}
return nullptr;
/* CNC - Cut out all the fancy acceptance tests
// Can we clone this use when doing the transformation?
// If all uses are from Phis at this merge or constants, then YES.
if( !v->in(0) && v != cmp ) {
tty->print_cr("Phi has free-floating use");
v->dump(2);
return nullptr;
}
for( uint l = 1; l < v->req(); l++ ) {
if( (!v->in(l)->is_Phi() || v->in(l)->in(0) != r) &&
!v->in(l)->is_Con() ) {
tty->print_cr("Phi has use");
v->dump(2);
return nullptr;
} // End of if Phi-use input is neither Phi nor Constant
} // End of for all inputs to Phi-use
*/
} // End of for all uses of Phi
} // End of for all uses of Region
// Only do this if the IF node is in a sane state
if (iff->outcnt() != 2)
return nullptr;
// Got a hit! Do the Mondo Hack!
//
//ABC a1c def ghi B 1 e h A C a c d f g i
// R - Phi - Phi - Phi Rc - Phi - Phi - Phi Rx - Phi - Phi - Phi
// cmp - 2 cmp - 2 cmp - 2
// bool bool_c bool_x
// if if_c if_x
// T F T F T F
// ..s.. ..t .. ..s.. ..t.. ..s.. ..t..
//
// Split the paths coming into the merge point into 2 separate groups of
// merges. On the left will be all the paths feeding constants into the
// Cmp's Phi. On the right will be the remaining paths. The Cmp's Phi
// will fold up into a constant; this will let the Cmp fold up as well as
// all the control flow. Below the original IF we have 2 control
// dependent regions, 's' and 't'. Now we will merge the two paths
// just prior to 's' and 't' from the two IFs. At least 1 path (and quite
// likely 2 or more) will promptly constant fold away.
PhaseGVN *phase = igvn;
// Make a region merging constants and a region merging the rest
uint req_c = 0;
for (uint ii = 1; ii < r->req(); ii++) {
if (phi->in(ii) == con1) {
req_c++;
}
if (Node::may_be_loop_entry(r->in(ii))) {
// Bail out if splitting through a region with a Parse Predicate input (could
// also be a loop header before loop opts creates a LoopNode for it).
return nullptr;
}
}
// If all the defs of the phi are the same constant, we already have the desired end state.
// Skip the split that would create empty phi and region nodes.
if ((r->req() - req_c) == 1) {
return nullptr;
}
// At this point we know that we can apply the split if optimization. If the region is still on the worklist,
// we should wait until it is processed. The region might be removed which makes this optimization redundant.
// This also avoids the creation of dead data loops when rewiring data nodes below when a region is dying.
if (igvn->_worklist.member(r)) {
igvn->_worklist.push(iff); // retry split if later again
return nullptr;
}
Node *region_c = new RegionNode(req_c + 1);
Node *phi_c = con1;
uint len = r->req();
Node *region_x = new RegionNode(len - req_c);
Node *phi_x = PhiNode::make_blank(region_x, phi);
for (uint i = 1, i_c = 1, i_x = 1; i < len; i++) {
if (phi->in(i) == con1) {
region_c->init_req( i_c++, r ->in(i) );
} else {
region_x->init_req( i_x, r ->in(i) );
phi_x ->init_req( i_x++, phi->in(i) );
}
}
// Register the new RegionNodes but do not transform them. Cannot
// transform until the entire Region/Phi conglomerate has been hacked
// as a single huge transform.
igvn->register_new_node_with_optimizer( region_c );
igvn->register_new_node_with_optimizer( region_x );
// Prevent the untimely death of phi_x. Currently he has no uses. He is
// about to get one. If this only use goes away, then phi_x will look dead.
// However, he will be picking up some more uses down below.
Node *hook = new Node(4);
hook->init_req(0, phi_x);
hook->init_req(1, phi_c);
phi_x = phase->transform( phi_x );
// Make the compare
Node *cmp_c = phase->makecon(t);
Node *cmp_x = cmp->clone();
cmp_x->set_req(1,phi_x);
cmp_x->set_req(2,con2);
cmp_x = phase->transform(cmp_x);
// Make the bool
Node *b_c = phase->transform(new BoolNode(cmp_c,b->_test._test));
Node *b_x = phase->transform(new BoolNode(cmp_x,b->_test._test));
// Make the IfNode
IfNode* iff_c = iff->clone()->as_If();
iff_c->set_req(0, region_c);
iff_c->set_req(1, b_c);
igvn->set_type_bottom(iff_c);
igvn->_worklist.push(iff_c);
hook->init_req(2, iff_c);
IfNode* iff_x = iff->clone()->as_If();
iff_x->set_req(0, region_x);
iff_x->set_req(1, b_x);
igvn->set_type_bottom(iff_x);
igvn->_worklist.push(iff_x);
hook->init_req(3, iff_x);
// Make the true/false arms
Node *iff_c_t = phase->transform(new IfTrueNode (iff_c));
Node *iff_c_f = phase->transform(new IfFalseNode(iff_c));
Node *iff_x_t = phase->transform(new IfTrueNode (iff_x));
Node *iff_x_f = phase->transform(new IfFalseNode(iff_x));
// Merge the TRUE paths
Node *region_s = new RegionNode(3);
igvn->_worklist.push(region_s);
region_s->init_req(1, iff_c_t);
region_s->init_req(2, iff_x_t);
igvn->register_new_node_with_optimizer( region_s );
// Merge the FALSE paths
Node *region_f = new RegionNode(3);
igvn->_worklist.push(region_f);
region_f->init_req(1, iff_c_f);
region_f->init_req(2, iff_x_f);
igvn->register_new_node_with_optimizer( region_f );
igvn->hash_delete(cmp);// Remove soon-to-be-dead node from hash table.
cmp->set_req(1,nullptr); // Whack the inputs to cmp because it will be dead
cmp->set_req(2,nullptr);
// Check for all uses of the Phi and give them a new home.
// The 'cmp' got cloned, but CastPP/IIs need to be moved.
Node *phi_s = nullptr; // do not construct unless needed
Node *phi_f = nullptr; // do not construct unless needed
for (DUIterator_Last i2min, i2 = phi->last_outs(i2min); i2 >= i2min; --i2) {
Node* v = phi->last_out(i2);// User of the phi
igvn->rehash_node_delayed(v); // Have to fixup other Phi users
uint vop = v->Opcode();
Node *proj = nullptr;
if( vop == Op_Phi ) { // Remote merge point
Node *r = v->in(0);
for (uint i3 = 1; i3 < r->req(); i3++)
if (r->in(i3) && r->in(i3)->in(0) == iff) {
proj = r->in(i3);
break;
}
} else if( v->is_ConstraintCast() ) {
proj = v->in(0); // Controlling projection
} else {
assert( 0, "do not know how to handle this guy" );
}
guarantee(proj != nullptr, "sanity");
Node *proj_path_data, *proj_path_ctrl;
if( proj->Opcode() == Op_IfTrue ) {
if( phi_s == nullptr ) {
// Only construct phi_s if needed, otherwise provides
// interfering use.
phi_s = PhiNode::make_blank(region_s,phi);
phi_s->init_req( 1, phi_c );
phi_s->init_req( 2, phi_x );
hook->add_req(phi_s);
phi_s = phase->transform(phi_s);
}
proj_path_data = phi_s;
proj_path_ctrl = region_s;
} else {
if( phi_f == nullptr ) {
// Only construct phi_f if needed, otherwise provides
// interfering use.
phi_f = PhiNode::make_blank(region_f,phi);
phi_f->init_req( 1, phi_c );
phi_f->init_req( 2, phi_x );
hook->add_req(phi_f);
phi_f = phase->transform(phi_f);
}
proj_path_data = phi_f;
proj_path_ctrl = region_f;
}
// Fixup 'v' for for the split
if( vop == Op_Phi ) { // Remote merge point
uint i;
for( i = 1; i < v->req(); i++ )
if( v->in(i) == phi )
break;
v->set_req(i, proj_path_data );
} else if( v->is_ConstraintCast() ) {
v->set_req(0, proj_path_ctrl );
v->set_req(1, proj_path_data );
} else
ShouldNotReachHere();
}
// Now replace the original iff's True/False with region_s/region_t.
// This makes the original iff go dead.
for (DUIterator_Last i3min, i3 = iff->last_outs(i3min); i3 >= i3min; --i3) {
Node* p = iff->last_out(i3);
assert( p->Opcode() == Op_IfTrue || p->Opcode() == Op_IfFalse, "" );
Node *u = (p->Opcode() == Op_IfTrue) ? region_s : region_f;
// Replace p with u
igvn->add_users_to_worklist(p);
for (DUIterator_Last lmin, l = p->last_outs(lmin); l >= lmin;) {
Node* x = p->last_out(l);
igvn->hash_delete(x);
uint uses_found = 0;
for( uint j = 0; j < x->req(); j++ ) {
if( x->in(j) == p ) {
x->set_req(j, u);
uses_found++;
}
}
l -= uses_found; // we deleted 1 or more copies of this edge
}
igvn->remove_dead_node(p);
}
// Force the original merge dead
igvn->hash_delete(r);
// First, remove region's dead users.
for (DUIterator_Last lmin, l = r->last_outs(lmin); l >= lmin;) {
Node* u = r->last_out(l);
if( u == r ) {
r->set_req(0, nullptr);
} else {
assert(u->outcnt() == 0, "only dead users");
igvn->remove_dead_node(u);
}
l -= 1;
}
igvn->remove_dead_node(r);
// Now remove the bogus extra edges used to keep things alive
igvn->remove_dead_node( hook );
// Must return either the original node (now dead) or a new node
// (Do not return a top here, since that would break the uniqueness of top.)
return new ConINode(TypeInt::ZERO);
}
IfNode* IfNode::make_with_same_profile(IfNode* if_node_profile, Node* ctrl, Node* bol) {
// Assert here that we only try to create a clone from an If node with the same profiling if that actually makes sense.
// Some If node subtypes should not be cloned in this way. In theory, we should not clone BaseCountedLoopEndNodes.
// But they can end up being used as normal If nodes when peeling a loop - they serve as zero-trip guard.
// Allow them as well.
assert(if_node_profile->Opcode() == Op_If || if_node_profile->is_RangeCheck()
|| if_node_profile->is_BaseCountedLoopEnd(), "should not clone other nodes");
if (if_node_profile->is_RangeCheck()) {
// RangeCheck nodes could be further optimized.
return new RangeCheckNode(ctrl, bol, if_node_profile->_prob, if_node_profile->_fcnt);
} else {
// Not a RangeCheckNode? Fall back to IfNode.
return new IfNode(ctrl, bol, if_node_profile->_prob, if_node_profile->_fcnt);
}
}
// if this IfNode follows a range check pattern return the projection
// for the failed path
ProjNode* IfNode::range_check_trap_proj(int& flip_test, Node*& l, Node*& r) {
if (outcnt() != 2) {
return nullptr;
}
Node* b = in(1);
if (b == nullptr || !b->is_Bool()) return nullptr;
BoolNode* bn = b->as_Bool();
Node* cmp = bn->in(1);
if (cmp == nullptr) return nullptr;
if (cmp->Opcode() != Op_CmpU) return nullptr;
l = cmp->in(1);
r = cmp->in(2);
flip_test = 1;
if (bn->_test._test == BoolTest::le) {
l = cmp->in(2);
r = cmp->in(1);
flip_test = 2;
} else if (bn->_test._test != BoolTest::lt) {
return nullptr;
}
if (l->is_top()) return nullptr; // Top input means dead test
if (r->Opcode() != Op_LoadRange && !is_RangeCheck()) return nullptr;
// We have recognized one of these forms:
// Flip 1: If (Bool[<] CmpU(l, LoadRange)) ...
// Flip 2: If (Bool[<=] CmpU(LoadRange, l)) ...
ProjNode* iftrap = proj_out_or_null(flip_test == 2 ? true : false);
return iftrap;
}
//------------------------------is_range_check---------------------------------
// Return 0 if not a range check. Return 1 if a range check and set index and
// offset. Return 2 if we had to negate the test. Index is null if the check
// is versus a constant.
int RangeCheckNode::is_range_check(Node* &range, Node* &index, jint &offset) {
int flip_test = 0;
Node* l = nullptr;
Node* r = nullptr;
ProjNode* iftrap = range_check_trap_proj(flip_test, l, r);
if (iftrap == nullptr) {
return 0;
}
// Make sure it's a real range check by requiring an uncommon trap
// along the OOB path. Otherwise, it's possible that the user wrote
// something which optimized to look like a range check but behaves
// in some other way.
if (iftrap->is_uncommon_trap_proj(Deoptimization::Reason_range_check) == nullptr) {
return 0;
}
// Look for index+offset form
Node* ind = l;
jint off = 0;
if (l->is_top()) {
return 0;
} else if (l->Opcode() == Op_AddI) {
if ((off = l->in(1)->find_int_con(0)) != 0) {
ind = l->in(2)->uncast();
} else if ((off = l->in(2)->find_int_con(0)) != 0) {
ind = l->in(1)->uncast();
}
} else if ((off = l->find_int_con(-1)) >= 0) {
// constant offset with no variable index
ind = nullptr;
} else {
// variable index with no constant offset (or dead negative index)
off = 0;
}
// Return all the values:
index = ind;
offset = off;
range = r;
return flip_test;
}
//------------------------------adjust_check-----------------------------------
// Adjust (widen) a prior range check
static void adjust_check(IfProjNode* proj, Node* range, Node* index,
int flip, jint off_lo, PhaseIterGVN* igvn) {
PhaseGVN *gvn = igvn;
// Break apart the old check
Node *iff = proj->in(0);
Node *bol = iff->in(1);
if( bol->is_top() ) return; // In case a partially dead range check appears
// bail (or bomb[ASSERT/DEBUG]) if NOT projection-->IfNode-->BoolNode
DEBUG_ONLY( if (!bol->is_Bool()) { proj->dump(3); fatal("Expect projection-->IfNode-->BoolNode"); } )
if (!bol->is_Bool()) return;
Node *cmp = bol->in(1);
// Compute a new check
Node *new_add = gvn->intcon(off_lo);
if (index) {
new_add = off_lo ? gvn->transform(new AddINode(index, new_add)) : index;
}
Node *new_cmp = (flip == 1)
? new CmpUNode(new_add, range)
: new CmpUNode(range, new_add);
new_cmp = gvn->transform(new_cmp);
// See if no need to adjust the existing check
if (new_cmp == cmp) return;
// Else, adjust existing check
Node* new_bol = gvn->transform(new BoolNode(new_cmp, bol->as_Bool()->_test._test));
igvn->rehash_node_delayed(iff);
iff->set_req_X(1, new_bol, igvn);
// As part of range check smearing, this range check is widened. Loads and range check Cast nodes that are control
// dependent on this range check now depend on multiple dominating range checks. These control dependent nodes end up
// at the lowest/nearest dominating check in the graph. To ensure that these Loads/Casts do not float above any of the
// dominating checks (even when the lowest dominating check is later replaced by yet another dominating check), we
// need to pin them at the lowest dominating check.
proj->pin_array_access_nodes(igvn);
}
//------------------------------up_one_dom-------------------------------------
// Walk up the dominator tree one step. Return null at root or true
// complex merges. Skips through small diamonds.
Node* IfNode::up_one_dom(Node *curr, bool linear_only) {
Node *dom = curr->in(0);
if( !dom ) // Found a Region degraded to a copy?
return curr->nonnull_req(); // Skip thru it
if( curr != dom ) // Normal walk up one step?
return dom;
// Use linear_only if we are still parsing, since we cannot
// trust the regions to be fully filled in.
if (linear_only)
return nullptr;
if( dom->is_Root() )
return nullptr;
// Else hit a Region. Check for a loop header
if( dom->is_Loop() )
return dom->in(1); // Skip up thru loops
// Check for small diamonds
Node *din1, *din2, *din3, *din4;
if( dom->req() == 3 && // 2-path merge point
(din1 = dom ->in(1)) && // Left path exists
(din2 = dom ->in(2)) && // Right path exists
(din3 = din1->in(0)) && // Left path up one
(din4 = din2->in(0)) ) { // Right path up one
if( din3->is_Call() && // Handle a slow-path call on either arm
(din3 = din3->in(0)) )
din3 = din3->in(0);
if( din4->is_Call() && // Handle a slow-path call on either arm
(din4 = din4->in(0)) )
din4 = din4->in(0);
if (din3 != nullptr && din3 == din4 && din3->is_If()) // Regions not degraded to a copy
return din3; // Skip around diamonds
}
// Give up the search at true merges
return nullptr; // Dead loop? Or hit root?
}
//------------------------------filtered_int_type--------------------------------
// Return a possibly more restrictive type for val based on condition control flow for an if
const TypeInt* IfNode::filtered_int_type(PhaseGVN* gvn, Node* val, Node* if_proj) {
assert(if_proj &&
(if_proj->Opcode() == Op_IfTrue || if_proj->Opcode() == Op_IfFalse), "expecting an if projection");
if (if_proj->in(0) && if_proj->in(0)->is_If()) {
IfNode* iff = if_proj->in(0)->as_If();
if (iff->in(1) && iff->in(1)->is_Bool()) {
BoolNode* bol = iff->in(1)->as_Bool();
if (bol->in(1) && bol->in(1)->is_Cmp()) {
const CmpNode* cmp = bol->in(1)->as_Cmp();
if (cmp->in(1) == val) {
const TypeInt* cmp2_t = gvn->type(cmp->in(2))->isa_int();
if (cmp2_t != nullptr) {
jint lo = cmp2_t->_lo;
jint hi = cmp2_t->_hi;
BoolTest::mask msk = if_proj->Opcode() == Op_IfTrue ? bol->_test._test : bol->_test.negate();
switch (msk) {
case BoolTest::ne: {
// If val is compared to its lower or upper bound, we can narrow the type
const TypeInt* val_t = gvn->type(val)->isa_int();
if (val_t != nullptr && !val_t->singleton() && cmp2_t->is_con()) {
if (val_t->_lo == lo) {
return TypeInt::make(val_t->_lo + 1, val_t->_hi, val_t->_widen);
} else if (val_t->_hi == hi) {
return TypeInt::make(val_t->_lo, val_t->_hi - 1, val_t->_widen);
}
}
// Can't refine type
return nullptr;
}
case BoolTest::eq:
return cmp2_t;
case BoolTest::lt:
lo = TypeInt::INT->_lo;
if (hi != min_jint) {
hi = hi - 1;
}
break;
case BoolTest::le:
lo = TypeInt::INT->_lo;
break;
case BoolTest::gt:
if (lo != max_jint) {
lo = lo + 1;
}
hi = TypeInt::INT->_hi;
break;
case BoolTest::ge:
// lo unchanged
hi = TypeInt::INT->_hi;
break;
default:
break;
}
const TypeInt* rtn_t = TypeInt::make(lo, hi, cmp2_t->_widen);
return rtn_t;
}
}
}
}
}
return nullptr;
}
//------------------------------fold_compares----------------------------
// See if a pair of CmpIs can be converted into a CmpU. In some cases
// the direction of this if is determined by the preceding if so it
// can be eliminate entirely.
//
// Given an if testing (CmpI n v) check for an immediately control
// dependent if that is testing (CmpI n v2) and has one projection
// leading to this if and the other projection leading to a region
// that merges one of this ifs control projections.
//
// If
// / |
// / |
// / |
// If |
// /\ |
// / \ |
// / \ |
// / Region
//
// Or given an if testing (CmpI n v) check for a dominating if that is
// testing (CmpI n v2), both having one projection leading to an
// uncommon trap. Allow Another independent guard in between to cover
// an explicit range check:
// if (index < 0 || index >= array.length) {
// which may need a null check to guard the LoadRange
//
// If
// / \
// / \
// / \
// If unc
// /\
// / \
// / \
// / unc
//
// Is the comparison for this If suitable for folding?
bool IfNode::cmpi_folds(PhaseIterGVN* igvn, bool fold_ne) {
return in(1) != nullptr &&
in(1)->is_Bool() &&
in(1)->in(1) != nullptr &&
in(1)->in(1)->Opcode() == Op_CmpI &&
in(1)->in(1)->in(2) != nullptr &&
in(1)->in(1)->in(2) != igvn->C->top() &&
(in(1)->as_Bool()->_test.is_less() ||
in(1)->as_Bool()->_test.is_greater() ||
(fold_ne && in(1)->as_Bool()->_test._test == BoolTest::ne));
}
// Is a dominating control suitable for folding with this if?
bool IfNode::is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn) {
return ctrl != nullptr &&
ctrl->is_Proj() &&
ctrl->outcnt() == 1 && // No side-effects
ctrl->in(0) != nullptr &&
ctrl->in(0)->Opcode() == Op_If &&
ctrl->in(0)->outcnt() == 2 &&
ctrl->in(0)->as_If()->cmpi_folds(igvn, true) &&
// Must compare same value
ctrl->in(0)->in(1)->in(1)->in(1) != nullptr &&
ctrl->in(0)->in(1)->in(1)->in(1) != igvn->C->top() &&
ctrl->in(0)->in(1)->in(1)->in(1) == in(1)->in(1)->in(1);
}
// Do this If and the dominating If share a region?
bool IfNode::has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail) {
ProjNode* otherproj = proj->other_if_proj();
Node* otherproj_ctrl_use = otherproj->unique_ctrl_out_or_null();
RegionNode* region = (otherproj_ctrl_use != nullptr && otherproj_ctrl_use->is_Region()) ? otherproj_ctrl_use->as_Region() : nullptr;
success = nullptr;
fail = nullptr;
if (otherproj->outcnt() == 1 && region != nullptr && !region->has_phi()) {
for (int i = 0; i < 2; i++) {
ProjNode* proj = proj_out(i);
if (success == nullptr && proj->outcnt() == 1 && proj->unique_out() == region) {
success = proj;
} else if (fail == nullptr) {
fail = proj;
} else {
success = fail = nullptr;
}
}
}
return success != nullptr && fail != nullptr;
}
bool IfNode::is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc) {
// Different methods and methods containing jsrs are not supported.
ciMethod* method = unc->jvms()->method();
ciMethod* dom_method = dom_unc->jvms()->method();
if (method != dom_method || method->has_jsrs()) {
return false;
}
// Check that both traps are in the same activation of the method (instead
// of two activations being inlined through different call sites) by verifying
// that the call stacks are equal for both JVMStates.
JVMState* dom_caller = dom_unc->jvms()->caller();
JVMState* caller = unc->jvms()->caller();
if ((dom_caller == nullptr) != (caller == nullptr)) {
// The current method must either be inlined into both dom_caller and
// caller or must not be inlined at all (top method). Bail out otherwise.
return false;
} else if (dom_caller != nullptr && !dom_caller->same_calls_as(caller)) {
return false;
}
// Check that the bci of the dominating uncommon trap dominates the bci
// of the dominated uncommon trap. Otherwise we may not re-execute
// the dominated check after deoptimization from the merged uncommon trap.
ciTypeFlow* flow = dom_method->get_flow_analysis();
int bci = unc->jvms()->bci();
int dom_bci = dom_unc->jvms()->bci();
if (!flow->is_dominated_by(bci, dom_bci)) {
return false;
}
return true;
}
// Return projection that leads to an uncommon trap if any
ProjNode* IfNode::uncommon_trap_proj(CallStaticJavaNode*& call, Deoptimization::DeoptReason reason) const {
for (int i = 0; i < 2; i++) {
call = proj_out(i)->is_uncommon_trap_proj(reason);
if (call != nullptr) {
return proj_out(i);
}
}
return nullptr;
}
// Do this If and the dominating If both branch out to an uncommon trap
bool IfNode::has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn) {
ProjNode* otherproj = proj->other_if_proj();
CallStaticJavaNode* dom_unc = otherproj->is_uncommon_trap_proj();
if (otherproj->outcnt() == 1 && dom_unc != nullptr) {
// We need to re-execute the folded Ifs after deoptimization from the merged traps
if (!dom_unc->jvms()->should_reexecute()) {
return false;
}
CallStaticJavaNode* unc = nullptr;
ProjNode* unc_proj = uncommon_trap_proj(unc);
if (unc_proj != nullptr && unc_proj->outcnt() == 1) {
if (dom_unc == unc) {
// Allow the uncommon trap to be shared through a region
RegionNode* r = unc->in(0)->as_Region();
if (r->outcnt() != 2 || r->req() != 3 || r->find_edge(otherproj) == -1 || r->find_edge(unc_proj) == -1) {
return false;
}
assert(r->has_phi() == nullptr, "simple region shouldn't have a phi");
} else if (dom_unc->in(0) != otherproj || unc->in(0) != unc_proj) {
return false;
}
if (!is_dominator_unc(dom_unc, unc)) {
return false;
}
// See merge_uncommon_traps: the reason of the uncommon trap
// will be changed and the state of the dominating If will be
// used. Checked that we didn't apply this transformation in a
// previous compilation and it didn't cause too many traps
ciMethod* dom_method = dom_unc->jvms()->method();
int dom_bci = dom_unc->jvms()->bci();
if (!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_unstable_fused_if) &&
!igvn->C->too_many_traps(dom_method, dom_bci, Deoptimization::Reason_range_check) &&
// Return true if c2 manages to reconcile with UnstableIf optimization. See the comments for it.
igvn->C->remove_unstable_if_trap(dom_unc, true/*yield*/)) {
success = unc_proj;
fail = unc_proj->other_if_proj();
return true;
}
}
}
return false;
}
// Check that the 2 CmpI can be folded into as single CmpU and proceed with the folding
bool IfNode::fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn) {
Node* this_cmp = in(1)->in(1);
BoolNode* this_bool = in(1)->as_Bool();
IfNode* dom_iff = proj->in(0)->as_If();
BoolNode* dom_bool = dom_iff->in(1)->as_Bool();
Node* lo = dom_iff->in(1)->in(1)->in(2);
Node* hi = this_cmp->in(2);
Node* n = this_cmp->in(1);
ProjNode* otherproj = proj->other_if_proj();
const TypeInt* lo_type = IfNode::filtered_int_type(igvn, n, otherproj);
const TypeInt* hi_type = IfNode::filtered_int_type(igvn, n, success);
BoolTest::mask lo_test = dom_bool->_test._test;
BoolTest::mask hi_test = this_bool->_test._test;
BoolTest::mask cond = hi_test;
// convert:
//
// dom_bool = x {<,<=,>,>=} a
// / \
// proj = {True,False} / \ otherproj = {False,True}
// /
// this_bool = x {<,<=} b
// / \
// fail = {True,False} / \ success = {False,True}
// /
//
// (Second test guaranteed canonicalized, first one may not have
// been canonicalized yet)
//
// into:
//
// cond = (x - lo) {<u,<=u,>u,>=u} adjusted_lim
// / \
// fail / \ success
// /
//
// Figure out which of the two tests sets the upper bound and which
// sets the lower bound if any.
Node* adjusted_lim = nullptr;
if (lo_type != nullptr && hi_type != nullptr && hi_type->_lo > lo_type->_hi &&
hi_type->_hi == max_jint && lo_type->_lo == min_jint && lo_test != BoolTest::ne) {
assert((dom_bool->_test.is_less() && !proj->_con) ||
(dom_bool->_test.is_greater() && proj->_con), "incorrect test");
// this_bool = <
// dom_bool = >= (proj = True) or dom_bool = < (proj = False)
// x in [a, b[ on the fail (= True) projection, b > a-1 (because of hi_type->_lo > lo_type->_hi test above):
// lo = a, hi = b, adjusted_lim = b-a, cond = <u
// dom_bool = > (proj = True) or dom_bool = <= (proj = False)
// x in ]a, b[ on the fail (= True) projection, b > a:
// lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <u
// this_bool = <=
// dom_bool = >= (proj = True) or dom_bool = < (proj = False)
// x in [a, b] on the fail (= True) projection, b+1 > a-1:
// lo = a, hi = b, adjusted_lim = b-a+1, cond = <u
// lo = a, hi = b, adjusted_lim = b-a, cond = <=u doesn't work because b = a - 1 is possible, then b-a = -1
// dom_bool = > (proj = True) or dom_bool = <= (proj = False)
// x in ]a, b] on the fail (= True) projection b+1 > a:
// lo = a+1, hi = b, adjusted_lim = b-a, cond = <u
// lo = a+1, hi = b, adjusted_lim = b-a-1, cond = <=u doesn't work because a = b is possible, then b-a-1 = -1
if (hi_test == BoolTest::lt) {
if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
}
} else if (hi_test == BoolTest::le) {
if (lo_test == BoolTest::ge || lo_test == BoolTest::lt) {
adjusted_lim = igvn->transform(new SubINode(hi, lo));
adjusted_lim = igvn->transform(new AddINode(adjusted_lim, igvn->intcon(1)));
cond = BoolTest::lt;
} else if (lo_test == BoolTest::gt || lo_test == BoolTest::le) {
adjusted_lim = igvn->transform(new SubINode(hi, lo));
lo = igvn->transform(new AddINode(lo, igvn->intcon(1)));
cond = BoolTest::lt;
} else {
assert(false, "unhandled lo_test: %d", lo_test);
return false;
}
} else {
assert(igvn->_worklist.member(in(1)) && in(1)->Value(igvn) != igvn->type(in(1)), "unhandled hi_test: %d", hi_test);
return false;
}
// this test was canonicalized
assert(this_bool->_test.is_less() && fail->_con, "incorrect test");
} else if (lo_type != nullptr && hi_type != nullptr && lo_type->_lo > hi_type->_hi &&
lo_type->_hi == max_jint && hi_type->_lo == min_jint && lo_test != BoolTest::ne) {
// this_bool = <
// dom_bool = < (proj = True) or dom_bool = >= (proj = False)
// x in [b, a[ on the fail (= False) projection, a > b-1 (because of lo_type->_lo > hi_type->_hi above):
// lo = b, hi = a, adjusted_lim = a-b, cond = >=u
// dom_bool = <= (proj = True) or dom_bool = > (proj = False)
// x in [b, a] on the fail (= False) projection, a+1 > b-1:
// lo = b, hi = a, adjusted_lim = a-b+1, cond = >=u
// lo = b, hi = a, adjusted_lim = a-b, cond = >u doesn't work because a = b - 1 is possible, then b-a = -1
// this_bool = <=
// dom_bool = < (proj = True) or dom_bool = >= (proj = False)
// x in ]b, a[ on the fail (= False) projection, a > b:
// lo = b+1, hi = a, adjusted_lim = a-b-1, cond = >=u