/
c1_RangeCheckElimination.cpp
1590 lines (1445 loc) · 57.3 KB
/
c1_RangeCheckElimination.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) 2012, 2016, 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 "precompiled.hpp"
#include "c1/c1_ValueStack.hpp"
#include "c1/c1_RangeCheckElimination.hpp"
#include "c1/c1_IR.hpp"
#include "c1/c1_Canonicalizer.hpp"
#include "c1/c1_ValueMap.hpp"
#include "ci/ciMethodData.hpp"
#include "runtime/deoptimization.hpp"
#ifdef ASSERT
#include "utilities/bitMap.inline.hpp"
#endif
// Macros for the Trace and the Assertion flag
#ifdef ASSERT
#define TRACE_RANGE_CHECK_ELIMINATION(code) if (TraceRangeCheckElimination) { code; }
#define ASSERT_RANGE_CHECK_ELIMINATION(code) if (AssertRangeCheckElimination) { code; }
#define TRACE_OR_ASSERT_RANGE_CHECK_ELIMINATION(code) if (TraceRangeCheckElimination || AssertRangeCheckElimination) { code; }
#else
#define TRACE_RANGE_CHECK_ELIMINATION(code)
#define ASSERT_RANGE_CHECK_ELIMINATION(code)
#define TRACE_OR_ASSERT_RANGE_CHECK_ELIMINATION(code)
#endif
// Entry point for the optimization
void RangeCheckElimination::eliminate(IR *ir) {
bool do_elimination = ir->compilation()->has_access_indexed();
ASSERT_RANGE_CHECK_ELIMINATION(do_elimination = true);
if (do_elimination) {
RangeCheckEliminator rce(ir);
}
}
// Constructor
RangeCheckEliminator::RangeCheckEliminator(IR *ir) :
_bounds(Instruction::number_of_instructions(), Instruction::number_of_instructions(), NULL),
_access_indexed_info(Instruction::number_of_instructions(), Instruction::number_of_instructions(), NULL)
{
_visitor.set_range_check_eliminator(this);
_ir = ir;
_number_of_instructions = Instruction::number_of_instructions();
_optimistic = ir->compilation()->is_optimistic();
TRACE_RANGE_CHECK_ELIMINATION(
tty->cr();
tty->print_cr("Range check elimination");
ir->method()->print_name(tty);
tty->cr();
);
TRACE_RANGE_CHECK_ELIMINATION(
tty->print_cr("optimistic=%d", (int)_optimistic);
);
#ifdef ASSERT
// Verifies several conditions that must be true on the IR-input. Only used for debugging purposes.
TRACE_RANGE_CHECK_ELIMINATION(
tty->print_cr("Verification of IR . . .");
);
Verification verification(ir);
#endif
// Set process block flags
// Optimization so a blocks is only processed if it contains an access indexed instruction or if
// one of its children in the dominator tree contains an access indexed instruction.
set_process_block_flags(ir->start());
// Pass over instructions in the dominator tree
TRACE_RANGE_CHECK_ELIMINATION(
tty->print_cr("Starting pass over dominator tree . . .")
);
calc_bounds(ir->start(), NULL);
TRACE_RANGE_CHECK_ELIMINATION(
tty->print_cr("Finished!")
);
}
// Instruction specific work for some instructions
// Constant
void RangeCheckEliminator::Visitor::do_Constant(Constant *c) {
IntConstant *ic = c->type()->as_IntConstant();
if (ic != NULL) {
int value = ic->value();
_bound = new Bound(value, NULL, value, NULL);
}
}
// LogicOp
void RangeCheckEliminator::Visitor::do_LogicOp(LogicOp *lo) {
if (lo->type()->as_IntType() && lo->op() == Bytecodes::_iand && (lo->x()->as_Constant() || lo->y()->as_Constant())) {
int constant = 0;
Constant *c = lo->x()->as_Constant();
if (c != NULL) {
constant = c->type()->as_IntConstant()->value();
} else {
constant = lo->y()->as_Constant()->type()->as_IntConstant()->value();
}
if (constant >= 0) {
_bound = new Bound(0, NULL, constant, NULL);
}
}
}
// Phi
void RangeCheckEliminator::Visitor::do_Phi(Phi *phi) {
if (!phi->type()->as_IntType() && !phi->type()->as_ObjectType()) return;
BlockBegin *block = phi->block();
int op_count = phi->operand_count();
bool has_upper = true;
bool has_lower = true;
assert(phi, "Phi must not be null");
Bound *bound = NULL;
// TODO: support more difficult phis
for (int i=0; i<op_count; i++) {
Value v = phi->operand_at(i);
if (v == phi) continue;
// Check if instruction is connected with phi itself
Op2 *op2 = v->as_Op2();
if (op2 != NULL) {
Value x = op2->x();
Value y = op2->y();
if ((x == phi || y == phi)) {
Value other = x;
if (other == phi) {
other = y;
}
ArithmeticOp *ao = v->as_ArithmeticOp();
if (ao != NULL && ao->op() == Bytecodes::_iadd) {
assert(ao->op() == Bytecodes::_iadd, "Has to be add!");
if (ao->type()->as_IntType()) {
Constant *c = other->as_Constant();
if (c != NULL) {
assert(c->type()->as_IntConstant(), "Constant has to be of type integer");
int value = c->type()->as_IntConstant()->value();
if (value == 1) {
has_upper = false;
} else if (value > 1) {
// Overflow not guaranteed
has_upper = false;
has_lower = false;
} else if (value < 0) {
has_lower = false;
}
continue;
}
}
}
}
}
// No connection -> new bound
Bound *v_bound = _rce->get_bound(v);
Bound *cur_bound;
int cur_constant = 0;
Value cur_value = v;
if (v->type()->as_IntConstant()) {
cur_constant = v->type()->as_IntConstant()->value();
cur_value = NULL;
}
if (!v_bound->has_upper() || !v_bound->has_lower()) {
cur_bound = new Bound(cur_constant, cur_value, cur_constant, cur_value);
} else {
cur_bound = v_bound;
}
if (cur_bound) {
if (!bound) {
bound = cur_bound->copy();
} else {
bound->or_op(cur_bound);
}
} else {
// No bound!
bound = NULL;
break;
}
}
if (bound) {
if (!has_upper) {
bound->remove_upper();
}
if (!has_lower) {
bound->remove_lower();
}
_bound = bound;
} else {
_bound = new Bound();
}
}
// ArithmeticOp
void RangeCheckEliminator::Visitor::do_ArithmeticOp(ArithmeticOp *ao) {
Value x = ao->x();
Value y = ao->y();
if (ao->op() == Bytecodes::_irem) {
Bound* x_bound = _rce->get_bound(x);
Bound* y_bound = _rce->get_bound(y);
if (x_bound->lower() >= 0 && x_bound->lower_instr() == NULL && y->as_ArrayLength() != NULL) {
_bound = new Bound(0, NULL, -1, y);
} else if (y->type()->as_IntConstant() && y->type()->as_IntConstant()->value() != 0) {
// The binary % operator is said to yield the remainder of its operands from an implied division; the
// left-hand operand is the dividend and the right-hand operand is the divisor.
//
// % operator follows from this rule that the result of the remainder operation can be negative only
// if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the
// magnitude of the result is always less than the magnitude of the divisor(See JLS 15.17.3).
//
// So if y is a constant integer and not equal to 0, then we can deduce the bound of remainder operation:
// x % -y ==> [0, y - 1] Apply RCE
// x % y ==> [0, y - 1] Apply RCE
// -x % y ==> [-y + 1, 0]
// -x % -y ==> [-y + 1, 0]
if (x_bound->has_lower() && x_bound->lower() >= 0) {
_bound = new Bound(0, NULL, y->type()->as_IntConstant()->value() - 1, NULL);
} else {
_bound = new Bound();
}
} else {
_bound = new Bound();
}
} else if (!x->as_Constant() || !y->as_Constant()) {
assert(!x->as_Constant() || !y->as_Constant(), "One of the operands must be non-constant!");
if (((x->as_Constant() || y->as_Constant()) && (ao->op() == Bytecodes::_iadd)) || (y->as_Constant() && ao->op() == Bytecodes::_isub)) {
assert(ao->op() == Bytecodes::_iadd || ao->op() == Bytecodes::_isub, "Operand must be iadd or isub");
if (y->as_Constant()) {
Value tmp = x;
x = y;
y = tmp;
}
assert(x->as_Constant()->type()->as_IntConstant(), "Constant must be int constant!");
// Constant now in x
int const_value = x->as_Constant()->type()->as_IntConstant()->value();
if (ao->op() == Bytecodes::_iadd || const_value != min_jint) {
if (ao->op() == Bytecodes::_isub) {
const_value = -const_value;
}
Bound * bound = _rce->get_bound(y);
if (bound->has_upper() && bound->has_lower()) {
int new_lower = bound->lower() + const_value;
jlong new_lowerl = ((jlong)bound->lower()) + const_value;
int new_upper = bound->upper() + const_value;
jlong new_upperl = ((jlong)bound->upper()) + const_value;
if (((jlong)new_lower) == new_lowerl && ((jlong)new_upper == new_upperl)) {
Bound *newBound = new Bound(new_lower, bound->lower_instr(), new_upper, bound->upper_instr());
_bound = newBound;
} else {
// overflow
_bound = new Bound();
}
} else {
_bound = new Bound();
}
} else {
_bound = new Bound();
}
} else {
Bound *bound = _rce->get_bound(x);
if (ao->op() == Bytecodes::_isub) {
if (bound->lower_instr() == y) {
_bound = new Bound(Instruction::geq, NULL, bound->lower());
} else {
_bound = new Bound();
}
} else {
_bound = new Bound();
}
}
}
}
// IfOp
void RangeCheckEliminator::Visitor::do_IfOp(IfOp *ifOp)
{
if (ifOp->tval()->type()->as_IntConstant() && ifOp->fval()->type()->as_IntConstant()) {
int min = ifOp->tval()->type()->as_IntConstant()->value();
int max = ifOp->fval()->type()->as_IntConstant()->value();
if (min > max) {
// min ^= max ^= min ^= max;
int tmp = min;
min = max;
max = tmp;
}
_bound = new Bound(min, NULL, max, NULL);
}
}
// Get bound. Returns the current bound on Value v. Normally this is the topmost element on the bound stack.
RangeCheckEliminator::Bound *RangeCheckEliminator::get_bound(Value v) {
// Wrong type or NULL -> No bound
if (!v || (!v->type()->as_IntType() && !v->type()->as_ObjectType())) return NULL;
if (!_bounds.at(v->id())) {
// First (default) bound is calculated
// Create BoundStack
_bounds.at_put(v->id(), new BoundStack());
_visitor.clear_bound();
Value visit_value = v;
visit_value->visit(&_visitor);
Bound *bound = _visitor.bound();
if (bound) {
_bounds.at(v->id())->push(bound);
}
if (_bounds.at(v->id())->length() == 0) {
assert(!(v->as_Constant() && v->type()->as_IntConstant()), "constants not handled here");
_bounds.at(v->id())->push(new Bound());
}
} else if (_bounds.at(v->id())->length() == 0) {
// To avoid endless loops, bound is currently in calculation -> nothing known about it
return new Bound();
}
// Return bound
return _bounds.at(v->id())->top();
}
// Update bound
void RangeCheckEliminator::update_bound(IntegerStack &pushed, Value v, Instruction::Condition cond, Value value, int constant) {
if (cond == Instruction::gtr) {
cond = Instruction::geq;
constant++;
} else if (cond == Instruction::lss) {
cond = Instruction::leq;
constant--;
}
Bound *bound = new Bound(cond, value, constant);
update_bound(pushed, v, bound);
}
// Checks for loop invariance. Returns true if the instruction is outside of the loop which is identified by loop_header.
bool RangeCheckEliminator::loop_invariant(BlockBegin *loop_header, Instruction *instruction) {
assert(loop_header, "Loop header must not be null!");
if (!instruction) return true;
return instruction->dominator_depth() < loop_header->dominator_depth();
}
// Update bound. Pushes a new bound onto the stack. Tries to do a conjunction with the current bound.
void RangeCheckEliminator::update_bound(IntegerStack &pushed, Value v, Bound *bound) {
if (v->as_Constant()) {
// No bound update for constants
return;
}
if (!_bounds.at(v->id())) {
get_bound(v);
assert(_bounds.at(v->id()), "Now Stack must exist");
}
Bound *top = NULL;
if (_bounds.at(v->id())->length() > 0) {
top = _bounds.at(v->id())->top();
}
if (top) {
bound->and_op(top);
}
_bounds.at(v->id())->push(bound);
pushed.append(v->id());
}
// Add instruction + idx for in block motion
void RangeCheckEliminator::add_access_indexed_info(InstructionList &indices, int idx, Value instruction, AccessIndexed *ai) {
int id = instruction->id();
AccessIndexedInfo *aii = _access_indexed_info.at(id);
if (aii == NULL) {
aii = new AccessIndexedInfo();
_access_indexed_info.at_put(id, aii);
indices.append(instruction);
aii->_min = idx;
aii->_max = idx;
aii->_list = new AccessIndexedList();
} else if (idx >= aii->_min && idx <= aii->_max) {
remove_range_check(ai);
return;
}
aii->_min = MIN2(aii->_min, idx);
aii->_max = MAX2(aii->_max, idx);
aii->_list->append(ai);
}
// In block motion. Tries to reorder checks in order to reduce some of them.
// Example:
// a[i] = 0;
// a[i+2] = 0;
// a[i+1] = 0;
// In this example the check for a[i+1] would be considered as unnecessary during the first iteration.
// After this i is only checked once for i >= 0 and i+2 < a.length before the first array access. If this
// check fails, deoptimization is called.
void RangeCheckEliminator::in_block_motion(BlockBegin *block, AccessIndexedList &accessIndexed, InstructionList &arrays) {
InstructionList indices;
// Now iterate over all arrays
for (int i=0; i<arrays.length(); i++) {
int max_constant = -1;
AccessIndexedList list_constant;
Value array = arrays.at(i);
// For all AccessIndexed-instructions in this block concerning the current array.
for(int j=0; j<accessIndexed.length(); j++) {
AccessIndexed *ai = accessIndexed.at(j);
if (ai->array() != array || !ai->check_flag(Instruction::NeedsRangeCheckFlag)) continue;
Value index = ai->index();
Constant *c = index->as_Constant();
if (c != NULL) {
int constant_value = c->type()->as_IntConstant()->value();
if (constant_value >= 0) {
if (constant_value <= max_constant) {
// No range check needed for this
remove_range_check(ai);
} else {
max_constant = constant_value;
list_constant.append(ai);
}
}
} else {
int last_integer = 0;
Instruction *last_instruction = index;
int base = 0;
ArithmeticOp *ao = index->as_ArithmeticOp();
while (ao != NULL && (ao->x()->as_Constant() || ao->y()->as_Constant()) && (ao->op() == Bytecodes::_iadd || ao->op() == Bytecodes::_isub)) {
c = ao->y()->as_Constant();
Instruction *other = ao->x();
if (!c && ao->op() == Bytecodes::_iadd) {
c = ao->x()->as_Constant();
other = ao->y();
}
if (c) {
int value = c->type()->as_IntConstant()->value();
if (value != min_jint) {
if (ao->op() == Bytecodes::_isub) {
value = -value;
}
base += value;
last_integer = base;
last_instruction = other;
}
index = other;
} else {
break;
}
ao = index->as_ArithmeticOp();
}
add_access_indexed_info(indices, last_integer, last_instruction, ai);
}
}
// Iterate over all different indices
if (_optimistic) {
for (int i = 0; i < indices.length(); i++) {
Instruction *index_instruction = indices.at(i);
AccessIndexedInfo *info = _access_indexed_info.at(index_instruction->id());
assert(info != NULL, "Info must not be null");
// if idx < 0, max > 0, max + idx may fall between 0 and
// length-1 and if min < 0, min + idx may overflow and be >=
// 0. The predicate wouldn't trigger but some accesses could
// be with a negative index. This test guarantees that for the
// min and max value that are kept the predicate can't let
// some incorrect accesses happen.
bool range_cond = (info->_max < 0 || info->_max + min_jint <= info->_min);
// Generate code only if more than 2 range checks can be eliminated because of that.
// 2 because at least 2 comparisons are done
if (info->_list->length() > 2 && range_cond) {
AccessIndexed *first = info->_list->at(0);
Instruction *insert_position = first->prev();
assert(insert_position->next() == first, "prev was calculated");
ValueStack *state = first->state_before();
// Load min Constant
Constant *min_constant = NULL;
if (info->_min != 0) {
min_constant = new Constant(new IntConstant(info->_min));
NOT_PRODUCT(min_constant->set_printable_bci(first->printable_bci()));
insert_position = insert_position->insert_after(min_constant);
}
// Load max Constant
Constant *max_constant = NULL;
if (info->_max != 0) {
max_constant = new Constant(new IntConstant(info->_max));
NOT_PRODUCT(max_constant->set_printable_bci(first->printable_bci()));
insert_position = insert_position->insert_after(max_constant);
}
// Load array length
Value length_instr = first->length();
if (!length_instr) {
ArrayLength *length = new ArrayLength(array, first->state_before()->copy());
length->set_exception_state(length->state_before());
length->set_flag(Instruction::DeoptimizeOnException, true);
insert_position = insert_position->insert_after_same_bci(length);
length_instr = length;
}
// Calculate lower bound
Instruction *lower_compare = index_instruction;
if (min_constant) {
ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, min_constant, lower_compare, false, NULL);
insert_position = insert_position->insert_after_same_bci(ao);
lower_compare = ao;
}
// Calculate upper bound
Instruction *upper_compare = index_instruction;
if (max_constant) {
ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, max_constant, upper_compare, false, NULL);
insert_position = insert_position->insert_after_same_bci(ao);
upper_compare = ao;
}
// Trick with unsigned compare is done
int bci = NOT_PRODUCT(first->printable_bci()) PRODUCT_ONLY(-1);
insert_position = predicate(upper_compare, Instruction::aeq, length_instr, state, insert_position, bci);
insert_position = predicate_cmp_with_const(lower_compare, Instruction::leq, -1, state, insert_position);
for (int j = 0; j<info->_list->length(); j++) {
AccessIndexed *ai = info->_list->at(j);
remove_range_check(ai);
}
}
}
if (list_constant.length() > 1) {
AccessIndexed *first = list_constant.at(0);
Instruction *insert_position = first->prev();
ValueStack *state = first->state_before();
// Load max Constant
Constant *constant = new Constant(new IntConstant(max_constant));
NOT_PRODUCT(constant->set_printable_bci(first->printable_bci()));
insert_position = insert_position->insert_after(constant);
Instruction *compare_instr = constant;
Value length_instr = first->length();
if (!length_instr) {
ArrayLength *length = new ArrayLength(array, state->copy());
length->set_exception_state(length->state_before());
length->set_flag(Instruction::DeoptimizeOnException, true);
insert_position = insert_position->insert_after_same_bci(length);
length_instr = length;
}
// Compare for greater or equal to array length
insert_position = predicate(compare_instr, Instruction::geq, length_instr, state, insert_position);
for (int j = 0; j<list_constant.length(); j++) {
AccessIndexed *ai = list_constant.at(j);
remove_range_check(ai);
}
}
}
// Clear data structures for next array
for (int i = 0; i < indices.length(); i++) {
Instruction *index_instruction = indices.at(i);
_access_indexed_info.at_put(index_instruction->id(), NULL);
}
indices.clear();
}
}
bool RangeCheckEliminator::set_process_block_flags(BlockBegin *block) {
Instruction *cur = block;
bool process = false;
while (cur) {
process |= (cur->as_AccessIndexed() != NULL);
cur = cur->next();
}
BlockList *dominates = block->dominates();
for (int i=0; i<dominates->length(); i++) {
BlockBegin *next = dominates->at(i);
process |= set_process_block_flags(next);
}
if (!process) {
block->set(BlockBegin::donot_eliminate_range_checks);
}
return process;
}
bool RangeCheckEliminator::is_ok_for_deoptimization(Instruction *insert_position, Instruction *array_instr, Instruction *length_instr, Instruction *lower_instr, int lower, Instruction *upper_instr, int upper) {
bool upper_check = true;
assert(lower_instr || lower >= 0, "If no lower_instr present, lower must be greater 0");
assert(!lower_instr || lower_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
assert(!upper_instr || upper_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
assert(array_instr, "Array instruction must exist");
assert(array_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
assert(!length_instr || length_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
if (upper_instr && upper_instr->as_ArrayLength() && upper_instr->as_ArrayLength()->array() == array_instr) {
// static check
if (upper >= 0) return false; // would always trigger a deopt:
// array_length + x >= array_length, x >= 0 is always true
upper_check = false;
}
if (lower_instr && lower_instr->as_ArrayLength() && lower_instr->as_ArrayLength()->array() == array_instr) {
if (lower > 0) return false;
}
// No upper check required -> skip
if (upper_check && upper_instr && upper_instr->type()->as_ObjectType() && upper_instr == array_instr) {
// upper_instr is object means that the upper bound is the length
// of the upper_instr.
return false;
}
return true;
}
Instruction* RangeCheckEliminator::insert_after(Instruction* insert_position, Instruction* instr, int bci) {
if (bci != -1) {
NOT_PRODUCT(instr->set_printable_bci(bci));
return insert_position->insert_after(instr);
} else {
return insert_position->insert_after_same_bci(instr);
}
}
Instruction* RangeCheckEliminator::predicate(Instruction* left, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci) {
RangeCheckPredicate *deoptimize = new RangeCheckPredicate(left, cond, true, right, state->copy());
return insert_after(insert_position, deoptimize, bci);
}
Instruction* RangeCheckEliminator::predicate_cmp_with_const(Instruction* instr, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci) {
Constant *const_instr = new Constant(new IntConstant(constant));
insert_position = insert_after(insert_position, const_instr, bci);
return predicate(instr, cond, const_instr, state, insert_position);
}
Instruction* RangeCheckEliminator::predicate_add(Instruction* left, int left_const, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci) {
Constant *constant = new Constant(new IntConstant(left_const));
insert_position = insert_after(insert_position, constant, bci);
ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, constant, left, false, NULL);
insert_position = insert_position->insert_after_same_bci(ao);
return predicate(ao, cond, right, state, insert_position);
}
Instruction* RangeCheckEliminator::predicate_add_cmp_with_const(Instruction* left, int left_const, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci) {
Constant *const_instr = new Constant(new IntConstant(constant));
insert_position = insert_after(insert_position, const_instr, bci);
return predicate_add(left, left_const, cond, const_instr, state, insert_position);
}
// Insert deoptimization
void RangeCheckEliminator::insert_deoptimization(ValueStack *state, Instruction *insert_position, Instruction *array_instr, Instruction *length_instr, Instruction *lower_instr, int lower, Instruction *upper_instr, int upper, AccessIndexed *ai) {
assert(is_ok_for_deoptimization(insert_position, array_instr, length_instr, lower_instr, lower, upper_instr, upper), "should have been tested before");
bool upper_check = !(upper_instr && upper_instr->as_ArrayLength() && upper_instr->as_ArrayLength()->array() == array_instr);
int bci = NOT_PRODUCT(ai->printable_bci()) PRODUCT_ONLY(-1);
if (lower_instr) {
assert(!lower_instr->type()->as_ObjectType(), "Must not be object type");
if (lower == 0) {
// Compare for less than 0
insert_position = predicate_cmp_with_const(lower_instr, Instruction::lss, 0, state, insert_position, bci);
} else if (lower > 0) {
// Compare for smaller 0
insert_position = predicate_add_cmp_with_const(lower_instr, lower, Instruction::lss, 0, state, insert_position, bci);
} else {
assert(lower < 0, "");
// Add 1
lower++;
lower = -lower;
// Compare for smaller or equal 0
insert_position = predicate_cmp_with_const(lower_instr, Instruction::leq, lower, state, insert_position, bci);
}
}
// No upper check required -> skip
if (!upper_check) return;
// We need to know length of array
if (!length_instr) {
// Load length if necessary
ArrayLength *length = new ArrayLength(array_instr, state->copy());
NOT_PRODUCT(length->set_printable_bci(ai->printable_bci()));
length->set_exception_state(length->state_before());
length->set_flag(Instruction::DeoptimizeOnException, true);
insert_position = insert_position->insert_after(length);
length_instr = length;
}
if (!upper_instr) {
// Compare for geq array.length
insert_position = predicate_cmp_with_const(length_instr, Instruction::leq, upper, state, insert_position, bci);
} else {
if (upper_instr->type()->as_ObjectType()) {
assert(state, "must not be null");
assert(upper_instr != array_instr, "should be");
ArrayLength *length = new ArrayLength(upper_instr, state->copy());
NOT_PRODUCT(length->set_printable_bci(ai->printable_bci()));
length->set_flag(Instruction::DeoptimizeOnException, true);
length->set_exception_state(length->state_before());
insert_position = insert_position->insert_after(length);
upper_instr = length;
}
assert(upper_instr->type()->as_IntType(), "Must not be object type!");
if (upper == 0) {
// Compare for geq array.length
insert_position = predicate(upper_instr, Instruction::geq, length_instr, state, insert_position, bci);
} else if (upper < 0) {
// Compare for geq array.length
insert_position = predicate_add(upper_instr, upper, Instruction::geq, length_instr, state, insert_position, bci);
} else {
assert(upper > 0, "");
upper = -upper;
// Compare for geq array.length
insert_position = predicate_add(length_instr, upper, Instruction::leq, upper_instr, state, insert_position, bci);
}
}
}
// Add if condition
void RangeCheckEliminator::add_if_condition(IntegerStack &pushed, Value x, Value y, Instruction::Condition condition) {
if (y->as_Constant()) return;
int const_value = 0;
Value instr_value = x;
Constant *c = x->as_Constant();
ArithmeticOp *ao = x->as_ArithmeticOp();
if (c != NULL) {
const_value = c->type()->as_IntConstant()->value();
instr_value = NULL;
} else if (ao != NULL && (!ao->x()->as_Constant() || !ao->y()->as_Constant()) && ((ao->op() == Bytecodes::_isub && ao->y()->as_Constant()) || ao->op() == Bytecodes::_iadd)) {
assert(!ao->x()->as_Constant() || !ao->y()->as_Constant(), "At least one operator must be non-constant!");
assert(ao->op() == Bytecodes::_isub || ao->op() == Bytecodes::_iadd, "Operation has to be add or sub!");
c = ao->x()->as_Constant();
if (c != NULL) {
const_value = c->type()->as_IntConstant()->value();
instr_value = ao->y();
} else {
c = ao->y()->as_Constant();
if (c != NULL) {
const_value = c->type()->as_IntConstant()->value();
instr_value = ao->x();
}
}
if (ao->op() == Bytecodes::_isub) {
assert(ao->y()->as_Constant(), "1 - x not supported, only x - 1 is valid!");
if (const_value > min_jint) {
const_value = -const_value;
} else {
const_value = 0;
instr_value = x;
}
}
}
update_bound(pushed, y, condition, instr_value, const_value);
}
// Process If
void RangeCheckEliminator::process_if(IntegerStack &pushed, BlockBegin *block, If *cond) {
// Only if we are direct true / false successor and NOT both ! (even this may occur)
if ((cond->tsux() == block || cond->fsux() == block) && cond->tsux() != cond->fsux()) {
Instruction::Condition condition = cond->cond();
if (cond->fsux() == block) {
condition = Instruction::negate(condition);
}
Value x = cond->x();
Value y = cond->y();
if (x->type()->as_IntType() && y->type()->as_IntType()) {
add_if_condition(pushed, y, x, condition);
add_if_condition(pushed, x, y, Instruction::mirror(condition));
}
}
}
// Process access indexed
void RangeCheckEliminator::process_access_indexed(BlockBegin *loop_header, BlockBegin *block, AccessIndexed *ai) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2)
);
TRACE_RANGE_CHECK_ELIMINATION(
tty->print_cr("Access indexed: index=%d length=%d", ai->index()->id(), (ai->length() != NULL ? ai->length()->id() :-1 ))
);
if (ai->check_flag(Instruction::NeedsRangeCheckFlag)) {
Bound *index_bound = get_bound(ai->index());
if (!index_bound->has_lower() || !index_bound->has_upper()) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Index instruction %d has no lower and/or no upper bound!", ai->index()->id())
);
return;
}
Bound *array_bound;
if (ai->length()) {
array_bound = get_bound(ai->length());
} else {
array_bound = get_bound(ai->array());
}
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print("Index bound: ");
index_bound->print();
tty->print(", Array bound: ");
array_bound->print();
tty->cr();
);
if (in_array_bound(index_bound, ai->array()) ||
(index_bound && array_bound && index_bound->is_smaller(array_bound) && !index_bound->lower_instr() && index_bound->lower() >= 0)) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Bounds check for instruction %d in block B%d can be fully eliminated!", ai->id(), ai->block()->block_id())
);
remove_range_check(ai);
} else if (_optimistic && loop_header) {
assert(ai->array(), "Array must not be null!");
assert(ai->index(), "Index must not be null!");
// Array instruction
Instruction *array_instr = ai->array();
if (!loop_invariant(loop_header, array_instr)) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Array %d is not loop invariant to header B%d", ai->array()->id(), loop_header->block_id())
);
return;
}
// Lower instruction
Value index_instr = ai->index();
Value lower_instr = index_bound->lower_instr();
if (!loop_invariant(loop_header, lower_instr)) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Lower instruction %d not loop invariant!", lower_instr->id())
);
return;
}
if (!lower_instr && index_bound->lower() < 0) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Lower bound smaller than 0 (%d)!", index_bound->lower())
);
return;
}
// Upper instruction
Value upper_instr = index_bound->upper_instr();
if (!loop_invariant(loop_header, upper_instr)) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Upper instruction %d not loop invariant!", upper_instr->id())
);
return;
}
// Length instruction
Value length_instr = ai->length();
if (!loop_invariant(loop_header, length_instr)) {
// Generate length instruction yourself!
length_instr = NULL;
}
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("LOOP INVARIANT access indexed %d found in block B%d!", ai->id(), ai->block()->block_id())
);
BlockBegin *pred_block = loop_header->dominator();
assert(pred_block != NULL, "Every loop header has a dominator!");
BlockEnd *pred_block_end = pred_block->end();
Instruction *insert_position = pred_block_end->prev();
ValueStack *state = pred_block_end->state_before();
if (pred_block_end->as_Goto() && state == NULL) state = pred_block_end->state();
assert(state, "State must not be null");
// Add deoptimization to dominator of loop header
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Inserting deopt at bci %d in block B%d!", state->bci(), insert_position->block()->block_id())
);
if (!is_ok_for_deoptimization(insert_position, array_instr, length_instr, lower_instr, index_bound->lower(), upper_instr, index_bound->upper())) {
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Could not eliminate because of static analysis!")
);
return;
}
insert_deoptimization(state, insert_position, array_instr, length_instr, lower_instr, index_bound->lower(), upper_instr, index_bound->upper(), ai);
// Finally remove the range check!
remove_range_check(ai);
}
}
}
void RangeCheckEliminator::remove_range_check(AccessIndexed *ai) {
ai->set_flag(Instruction::NeedsRangeCheckFlag, false);
// no range check, no need for the length instruction anymore
ai->clear_length();
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(ai->dominator_depth()*2);
tty->print_cr("Range check for instruction %d eliminated!", ai->id());
);
ASSERT_RANGE_CHECK_ELIMINATION(
Value array_length = ai->length();
if (!array_length) {
array_length = ai->array();
assert(array_length->type()->as_ObjectType(), "Has to be object type!");
}
int cur_constant = -1;
Value cur_value = array_length;
if (cur_value->type()->as_IntConstant()) {
cur_constant += cur_value->type()->as_IntConstant()->value();
cur_value = NULL;
}
Bound *new_index_bound = new Bound(0, NULL, cur_constant, cur_value);
add_assertions(new_index_bound, ai->index(), ai);
);
}
// Calculate bounds for instruction in this block and children blocks in the dominator tree
void RangeCheckEliminator::calc_bounds(BlockBegin *block, BlockBegin *loop_header) {
// Ensures a valid loop_header
assert(!loop_header || loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Loop header has to be real !");
// Tracing output
TRACE_RANGE_CHECK_ELIMINATION(
tty->fill_to(block->dominator_depth()*2);
tty->print_cr("Block B%d", block->block_id());
);
// Pushed stack for conditions
IntegerStack pushed;
// Process If
BlockBegin *parent = block->dominator();
if (parent != NULL) {
If *cond = parent->end()->as_If();
if (cond != NULL) {
process_if(pushed, block, cond);
}
}
// Interate over current block
InstructionList arrays;
AccessIndexedList accessIndexed;
Instruction *cur = block;
while (cur) {
// Ensure cur wasn't inserted during the elimination
if (cur->id() < this->_bounds.length()) {
// Process only if it is an access indexed instruction
AccessIndexed *ai = cur->as_AccessIndexed();
if (ai != NULL) {
process_access_indexed(loop_header, block, ai);
accessIndexed.append(ai);
if (!arrays.contains(ai->array())) {
arrays.append(ai->array());
}
Bound *b = get_bound(ai->index());
if (!b->lower_instr()) {
// Lower bound is constant
update_bound(pushed, ai->index(), Instruction::geq, NULL, 0);
}
if (!b->has_upper()) {
if (ai->length() && ai->length()->type()->as_IntConstant()) {
int value = ai->length()->type()->as_IntConstant()->value();
update_bound(pushed, ai->index(), Instruction::lss, NULL, value);