Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/hotspot/share/c1/c1_RangeCheckElimination.cpp
64440 views
1
/*
2
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include "precompiled.hpp"
26
#include "c1/c1_ValueStack.hpp"
27
#include "c1/c1_RangeCheckElimination.hpp"
28
#include "c1/c1_IR.hpp"
29
#include "c1/c1_Canonicalizer.hpp"
30
#include "c1/c1_ValueMap.hpp"
31
#include "ci/ciMethodData.hpp"
32
#include "runtime/deoptimization.hpp"
33
#ifdef ASSERT
34
#include "utilities/bitMap.inline.hpp"
35
#endif
36
37
// Macros for the Trace and the Assertion flag
38
#ifdef ASSERT
39
#define TRACE_RANGE_CHECK_ELIMINATION(code) if (TraceRangeCheckElimination) { code; }
40
#define ASSERT_RANGE_CHECK_ELIMINATION(code) if (AssertRangeCheckElimination) { code; }
41
#define TRACE_OR_ASSERT_RANGE_CHECK_ELIMINATION(code) if (TraceRangeCheckElimination || AssertRangeCheckElimination) { code; }
42
#else
43
#define TRACE_RANGE_CHECK_ELIMINATION(code)
44
#define ASSERT_RANGE_CHECK_ELIMINATION(code)
45
#define TRACE_OR_ASSERT_RANGE_CHECK_ELIMINATION(code)
46
#endif
47
48
// Entry point for the optimization
49
void RangeCheckElimination::eliminate(IR *ir) {
50
bool do_elimination = ir->compilation()->has_access_indexed();
51
ASSERT_RANGE_CHECK_ELIMINATION(do_elimination = true);
52
if (do_elimination) {
53
RangeCheckEliminator rce(ir);
54
}
55
}
56
57
// Constructor
58
RangeCheckEliminator::RangeCheckEliminator(IR *ir) :
59
_bounds(Instruction::number_of_instructions(), Instruction::number_of_instructions(), NULL),
60
_access_indexed_info(Instruction::number_of_instructions(), Instruction::number_of_instructions(), NULL)
61
{
62
_visitor.set_range_check_eliminator(this);
63
_ir = ir;
64
_number_of_instructions = Instruction::number_of_instructions();
65
_optimistic = ir->compilation()->is_optimistic();
66
67
TRACE_RANGE_CHECK_ELIMINATION(
68
tty->cr();
69
tty->print_cr("Range check elimination");
70
ir->method()->print_name(tty);
71
tty->cr();
72
);
73
74
TRACE_RANGE_CHECK_ELIMINATION(
75
tty->print_cr("optimistic=%d", (int)_optimistic);
76
);
77
78
#ifdef ASSERT
79
// Verifies several conditions that must be true on the IR-input. Only used for debugging purposes.
80
TRACE_RANGE_CHECK_ELIMINATION(
81
tty->print_cr("Verification of IR . . .");
82
);
83
Verification verification(ir);
84
#endif
85
86
// Set process block flags
87
// Optimization so a blocks is only processed if it contains an access indexed instruction or if
88
// one of its children in the dominator tree contains an access indexed instruction.
89
set_process_block_flags(ir->start());
90
91
// Pass over instructions in the dominator tree
92
TRACE_RANGE_CHECK_ELIMINATION(
93
tty->print_cr("Starting pass over dominator tree . . .")
94
);
95
calc_bounds(ir->start(), NULL);
96
97
TRACE_RANGE_CHECK_ELIMINATION(
98
tty->print_cr("Finished!")
99
);
100
}
101
102
// Instruction specific work for some instructions
103
// Constant
104
void RangeCheckEliminator::Visitor::do_Constant(Constant *c) {
105
IntConstant *ic = c->type()->as_IntConstant();
106
if (ic != NULL) {
107
int value = ic->value();
108
_bound = new Bound(value, NULL, value, NULL);
109
}
110
}
111
112
// LogicOp
113
void RangeCheckEliminator::Visitor::do_LogicOp(LogicOp *lo) {
114
if (lo->type()->as_IntType() && lo->op() == Bytecodes::_iand && (lo->x()->as_Constant() || lo->y()->as_Constant())) {
115
int constant = 0;
116
Constant *c = lo->x()->as_Constant();
117
if (c != NULL) {
118
constant = c->type()->as_IntConstant()->value();
119
} else {
120
constant = lo->y()->as_Constant()->type()->as_IntConstant()->value();
121
}
122
if (constant >= 0) {
123
_bound = new Bound(0, NULL, constant, NULL);
124
}
125
}
126
}
127
128
// Phi
129
void RangeCheckEliminator::Visitor::do_Phi(Phi *phi) {
130
if (!phi->type()->as_IntType() && !phi->type()->as_ObjectType()) return;
131
132
BlockBegin *block = phi->block();
133
int op_count = phi->operand_count();
134
bool has_upper = true;
135
bool has_lower = true;
136
assert(phi, "Phi must not be null");
137
Bound *bound = NULL;
138
139
// TODO: support more difficult phis
140
for (int i=0; i<op_count; i++) {
141
Value v = phi->operand_at(i);
142
143
if (v == phi) continue;
144
145
// Check if instruction is connected with phi itself
146
Op2 *op2 = v->as_Op2();
147
if (op2 != NULL) {
148
Value x = op2->x();
149
Value y = op2->y();
150
if ((x == phi || y == phi)) {
151
Value other = x;
152
if (other == phi) {
153
other = y;
154
}
155
ArithmeticOp *ao = v->as_ArithmeticOp();
156
if (ao != NULL && ao->op() == Bytecodes::_iadd) {
157
assert(ao->op() == Bytecodes::_iadd, "Has to be add!");
158
if (ao->type()->as_IntType()) {
159
Constant *c = other->as_Constant();
160
if (c != NULL) {
161
assert(c->type()->as_IntConstant(), "Constant has to be of type integer");
162
int value = c->type()->as_IntConstant()->value();
163
if (value == 1) {
164
has_upper = false;
165
} else if (value > 1) {
166
// Overflow not guaranteed
167
has_upper = false;
168
has_lower = false;
169
} else if (value < 0) {
170
has_lower = false;
171
}
172
continue;
173
}
174
}
175
}
176
}
177
}
178
179
// No connection -> new bound
180
Bound *v_bound = _rce->get_bound(v);
181
Bound *cur_bound;
182
int cur_constant = 0;
183
Value cur_value = v;
184
185
if (v->type()->as_IntConstant()) {
186
cur_constant = v->type()->as_IntConstant()->value();
187
cur_value = NULL;
188
}
189
if (!v_bound->has_upper() || !v_bound->has_lower()) {
190
cur_bound = new Bound(cur_constant, cur_value, cur_constant, cur_value);
191
} else {
192
cur_bound = v_bound;
193
}
194
if (cur_bound) {
195
if (!bound) {
196
bound = cur_bound->copy();
197
} else {
198
bound->or_op(cur_bound);
199
}
200
} else {
201
// No bound!
202
bound = NULL;
203
break;
204
}
205
}
206
207
if (bound) {
208
if (!has_upper) {
209
bound->remove_upper();
210
}
211
if (!has_lower) {
212
bound->remove_lower();
213
}
214
_bound = bound;
215
} else {
216
_bound = new Bound();
217
}
218
}
219
220
221
// ArithmeticOp
222
void RangeCheckEliminator::Visitor::do_ArithmeticOp(ArithmeticOp *ao) {
223
Value x = ao->x();
224
Value y = ao->y();
225
226
if (ao->op() == Bytecodes::_irem) {
227
Bound* x_bound = _rce->get_bound(x);
228
Bound* y_bound = _rce->get_bound(y);
229
if (x_bound->lower() >= 0 && x_bound->lower_instr() == NULL && y->as_ArrayLength() != NULL) {
230
_bound = new Bound(0, NULL, -1, y);
231
} else if (y->type()->as_IntConstant() && y->type()->as_IntConstant()->value() != 0) {
232
// The binary % operator is said to yield the remainder of its operands from an implied division; the
233
// left-hand operand is the dividend and the right-hand operand is the divisor.
234
//
235
// % operator follows from this rule that the result of the remainder operation can be negative only
236
// if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the
237
// magnitude of the result is always less than the magnitude of the divisor(See JLS 15.17.3).
238
//
239
// So if y is a constant integer and not equal to 0, then we can deduce the bound of remainder operation:
240
// x % -y ==> [0, y - 1] Apply RCE
241
// x % y ==> [0, y - 1] Apply RCE
242
// -x % y ==> [-y + 1, 0]
243
// -x % -y ==> [-y + 1, 0]
244
if (x_bound->has_lower() && x_bound->lower() >= 0) {
245
_bound = new Bound(0, NULL, y->type()->as_IntConstant()->value() - 1, NULL);
246
} else {
247
_bound = new Bound();
248
}
249
} else {
250
_bound = new Bound();
251
}
252
} else if (!x->as_Constant() || !y->as_Constant()) {
253
assert(!x->as_Constant() || !y->as_Constant(), "One of the operands must be non-constant!");
254
if (((x->as_Constant() || y->as_Constant()) && (ao->op() == Bytecodes::_iadd)) || (y->as_Constant() && ao->op() == Bytecodes::_isub)) {
255
assert(ao->op() == Bytecodes::_iadd || ao->op() == Bytecodes::_isub, "Operand must be iadd or isub");
256
257
if (y->as_Constant()) {
258
Value tmp = x;
259
x = y;
260
y = tmp;
261
}
262
assert(x->as_Constant()->type()->as_IntConstant(), "Constant must be int constant!");
263
264
// Constant now in x
265
int const_value = x->as_Constant()->type()->as_IntConstant()->value();
266
if (ao->op() == Bytecodes::_iadd || const_value != min_jint) {
267
if (ao->op() == Bytecodes::_isub) {
268
const_value = -const_value;
269
}
270
271
Bound * bound = _rce->get_bound(y);
272
if (bound->has_upper() && bound->has_lower()) {
273
int new_lower = bound->lower() + const_value;
274
jlong new_lowerl = ((jlong)bound->lower()) + const_value;
275
int new_upper = bound->upper() + const_value;
276
jlong new_upperl = ((jlong)bound->upper()) + const_value;
277
278
if (((jlong)new_lower) == new_lowerl && ((jlong)new_upper == new_upperl)) {
279
Bound *newBound = new Bound(new_lower, bound->lower_instr(), new_upper, bound->upper_instr());
280
_bound = newBound;
281
} else {
282
// overflow
283
_bound = new Bound();
284
}
285
} else {
286
_bound = new Bound();
287
}
288
} else {
289
_bound = new Bound();
290
}
291
} else {
292
Bound *bound = _rce->get_bound(x);
293
if (ao->op() == Bytecodes::_isub) {
294
if (bound->lower_instr() == y) {
295
_bound = new Bound(Instruction::geq, NULL, bound->lower());
296
} else {
297
_bound = new Bound();
298
}
299
} else {
300
_bound = new Bound();
301
}
302
}
303
}
304
}
305
306
// IfOp
307
void RangeCheckEliminator::Visitor::do_IfOp(IfOp *ifOp)
308
{
309
if (ifOp->tval()->type()->as_IntConstant() && ifOp->fval()->type()->as_IntConstant()) {
310
int min = ifOp->tval()->type()->as_IntConstant()->value();
311
int max = ifOp->fval()->type()->as_IntConstant()->value();
312
if (min > max) {
313
// min ^= max ^= min ^= max;
314
int tmp = min;
315
min = max;
316
max = tmp;
317
}
318
_bound = new Bound(min, NULL, max, NULL);
319
}
320
}
321
322
// Get bound. Returns the current bound on Value v. Normally this is the topmost element on the bound stack.
323
RangeCheckEliminator::Bound *RangeCheckEliminator::get_bound(Value v) {
324
// Wrong type or NULL -> No bound
325
if (!v || (!v->type()->as_IntType() && !v->type()->as_ObjectType())) return NULL;
326
327
if (!_bounds.at(v->id())) {
328
// First (default) bound is calculated
329
// Create BoundStack
330
_bounds.at_put(v->id(), new BoundStack());
331
_visitor.clear_bound();
332
Value visit_value = v;
333
visit_value->visit(&_visitor);
334
Bound *bound = _visitor.bound();
335
if (bound) {
336
_bounds.at(v->id())->push(bound);
337
}
338
if (_bounds.at(v->id())->length() == 0) {
339
assert(!(v->as_Constant() && v->type()->as_IntConstant()), "constants not handled here");
340
_bounds.at(v->id())->push(new Bound());
341
}
342
} else if (_bounds.at(v->id())->length() == 0) {
343
// To avoid endless loops, bound is currently in calculation -> nothing known about it
344
return new Bound();
345
}
346
347
// Return bound
348
return _bounds.at(v->id())->top();
349
}
350
351
// Update bound
352
void RangeCheckEliminator::update_bound(IntegerStack &pushed, Value v, Instruction::Condition cond, Value value, int constant) {
353
if (cond == Instruction::gtr) {
354
cond = Instruction::geq;
355
constant++;
356
} else if (cond == Instruction::lss) {
357
cond = Instruction::leq;
358
constant--;
359
}
360
Bound *bound = new Bound(cond, value, constant);
361
update_bound(pushed, v, bound);
362
}
363
364
// Checks for loop invariance. Returns true if the instruction is outside of the loop which is identified by loop_header.
365
bool RangeCheckEliminator::loop_invariant(BlockBegin *loop_header, Instruction *instruction) {
366
assert(loop_header, "Loop header must not be null!");
367
if (!instruction) return true;
368
for (BlockBegin *d = loop_header->dominator(); d != NULL; d = d->dominator()) {
369
if (d == instruction->block()) {
370
return true;
371
}
372
}
373
return false;
374
}
375
376
// Update bound. Pushes a new bound onto the stack. Tries to do a conjunction with the current bound.
377
void RangeCheckEliminator::update_bound(IntegerStack &pushed, Value v, Bound *bound) {
378
if (v->as_Constant()) {
379
// No bound update for constants
380
return;
381
}
382
if (!_bounds.at(v->id())) {
383
get_bound(v);
384
assert(_bounds.at(v->id()), "Now Stack must exist");
385
}
386
Bound *top = NULL;
387
if (_bounds.at(v->id())->length() > 0) {
388
top = _bounds.at(v->id())->top();
389
}
390
if (top) {
391
bound->and_op(top);
392
}
393
_bounds.at(v->id())->push(bound);
394
pushed.append(v->id());
395
}
396
397
// Add instruction + idx for in block motion
398
void RangeCheckEliminator::add_access_indexed_info(InstructionList &indices, int idx, Value instruction, AccessIndexed *ai) {
399
int id = instruction->id();
400
AccessIndexedInfo *aii = _access_indexed_info.at(id);
401
if (aii == NULL) {
402
aii = new AccessIndexedInfo();
403
_access_indexed_info.at_put(id, aii);
404
indices.append(instruction);
405
aii->_min = idx;
406
aii->_max = idx;
407
aii->_list = new AccessIndexedList();
408
} else if (idx >= aii->_min && idx <= aii->_max) {
409
remove_range_check(ai);
410
return;
411
}
412
aii->_min = MIN2(aii->_min, idx);
413
aii->_max = MAX2(aii->_max, idx);
414
aii->_list->append(ai);
415
}
416
417
// In block motion. Tries to reorder checks in order to reduce some of them.
418
// Example:
419
// a[i] = 0;
420
// a[i+2] = 0;
421
// a[i+1] = 0;
422
// In this example the check for a[i+1] would be considered as unnecessary during the first iteration.
423
// After this i is only checked once for i >= 0 and i+2 < a.length before the first array access. If this
424
// check fails, deoptimization is called.
425
void RangeCheckEliminator::in_block_motion(BlockBegin *block, AccessIndexedList &accessIndexed, InstructionList &arrays) {
426
InstructionList indices;
427
428
// Now iterate over all arrays
429
for (int i=0; i<arrays.length(); i++) {
430
int max_constant = -1;
431
AccessIndexedList list_constant;
432
Value array = arrays.at(i);
433
434
// For all AccessIndexed-instructions in this block concerning the current array.
435
for(int j=0; j<accessIndexed.length(); j++) {
436
AccessIndexed *ai = accessIndexed.at(j);
437
if (ai->array() != array || !ai->check_flag(Instruction::NeedsRangeCheckFlag)) continue;
438
439
Value index = ai->index();
440
Constant *c = index->as_Constant();
441
if (c != NULL) {
442
int constant_value = c->type()->as_IntConstant()->value();
443
if (constant_value >= 0) {
444
if (constant_value <= max_constant) {
445
// No range check needed for this
446
remove_range_check(ai);
447
} else {
448
max_constant = constant_value;
449
list_constant.append(ai);
450
}
451
}
452
} else {
453
int last_integer = 0;
454
Instruction *last_instruction = index;
455
int base = 0;
456
ArithmeticOp *ao = index->as_ArithmeticOp();
457
458
while (ao != NULL && (ao->x()->as_Constant() || ao->y()->as_Constant()) && (ao->op() == Bytecodes::_iadd || ao->op() == Bytecodes::_isub)) {
459
c = ao->y()->as_Constant();
460
Instruction *other = ao->x();
461
if (!c && ao->op() == Bytecodes::_iadd) {
462
c = ao->x()->as_Constant();
463
other = ao->y();
464
}
465
466
if (c) {
467
int value = c->type()->as_IntConstant()->value();
468
if (value != min_jint) {
469
if (ao->op() == Bytecodes::_isub) {
470
value = -value;
471
}
472
base += value;
473
last_integer = base;
474
last_instruction = other;
475
}
476
index = other;
477
} else {
478
break;
479
}
480
ao = index->as_ArithmeticOp();
481
}
482
add_access_indexed_info(indices, last_integer, last_instruction, ai);
483
}
484
}
485
486
// Iterate over all different indices
487
if (_optimistic) {
488
for (int i = 0; i < indices.length(); i++) {
489
Instruction *index_instruction = indices.at(i);
490
AccessIndexedInfo *info = _access_indexed_info.at(index_instruction->id());
491
assert(info != NULL, "Info must not be null");
492
493
// if idx < 0, max > 0, max + idx may fall between 0 and
494
// length-1 and if min < 0, min + idx may overflow and be >=
495
// 0. The predicate wouldn't trigger but some accesses could
496
// be with a negative index. This test guarantees that for the
497
// min and max value that are kept the predicate can't let
498
// some incorrect accesses happen.
499
bool range_cond = (info->_max < 0 || info->_max + min_jint <= info->_min);
500
501
// Generate code only if more than 2 range checks can be eliminated because of that.
502
// 2 because at least 2 comparisons are done
503
if (info->_list->length() > 2 && range_cond) {
504
AccessIndexed *first = info->_list->at(0);
505
Instruction *insert_position = first->prev();
506
assert(insert_position->next() == first, "prev was calculated");
507
ValueStack *state = first->state_before();
508
509
// Load min Constant
510
Constant *min_constant = NULL;
511
if (info->_min != 0) {
512
min_constant = new Constant(new IntConstant(info->_min));
513
NOT_PRODUCT(min_constant->set_printable_bci(first->printable_bci()));
514
insert_position = insert_position->insert_after(min_constant);
515
}
516
517
// Load max Constant
518
Constant *max_constant = NULL;
519
if (info->_max != 0) {
520
max_constant = new Constant(new IntConstant(info->_max));
521
NOT_PRODUCT(max_constant->set_printable_bci(first->printable_bci()));
522
insert_position = insert_position->insert_after(max_constant);
523
}
524
525
// Load array length
526
Value length_instr = first->length();
527
if (!length_instr) {
528
ArrayLength *length = new ArrayLength(array, first->state_before()->copy());
529
length->set_exception_state(length->state_before());
530
length->set_flag(Instruction::DeoptimizeOnException, true);
531
insert_position = insert_position->insert_after_same_bci(length);
532
length_instr = length;
533
}
534
535
// Calculate lower bound
536
Instruction *lower_compare = index_instruction;
537
if (min_constant) {
538
ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, min_constant, lower_compare, NULL);
539
insert_position = insert_position->insert_after_same_bci(ao);
540
lower_compare = ao;
541
}
542
543
// Calculate upper bound
544
Instruction *upper_compare = index_instruction;
545
if (max_constant) {
546
ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, max_constant, upper_compare, NULL);
547
insert_position = insert_position->insert_after_same_bci(ao);
548
upper_compare = ao;
549
}
550
551
// Trick with unsigned compare is done
552
int bci = NOT_PRODUCT(first->printable_bci()) PRODUCT_ONLY(-1);
553
insert_position = predicate(upper_compare, Instruction::aeq, length_instr, state, insert_position, bci);
554
insert_position = predicate_cmp_with_const(lower_compare, Instruction::leq, -1, state, insert_position);
555
for (int j = 0; j<info->_list->length(); j++) {
556
AccessIndexed *ai = info->_list->at(j);
557
remove_range_check(ai);
558
}
559
}
560
}
561
562
if (list_constant.length() > 1) {
563
AccessIndexed *first = list_constant.at(0);
564
Instruction *insert_position = first->prev();
565
ValueStack *state = first->state_before();
566
// Load max Constant
567
Constant *constant = new Constant(new IntConstant(max_constant));
568
NOT_PRODUCT(constant->set_printable_bci(first->printable_bci()));
569
insert_position = insert_position->insert_after(constant);
570
Instruction *compare_instr = constant;
571
Value length_instr = first->length();
572
if (!length_instr) {
573
ArrayLength *length = new ArrayLength(array, state->copy());
574
length->set_exception_state(length->state_before());
575
length->set_flag(Instruction::DeoptimizeOnException, true);
576
insert_position = insert_position->insert_after_same_bci(length);
577
length_instr = length;
578
}
579
// Compare for greater or equal to array length
580
insert_position = predicate(compare_instr, Instruction::geq, length_instr, state, insert_position);
581
for (int j = 0; j<list_constant.length(); j++) {
582
AccessIndexed *ai = list_constant.at(j);
583
remove_range_check(ai);
584
}
585
}
586
}
587
588
// Clear data structures for next array
589
for (int i = 0; i < indices.length(); i++) {
590
Instruction *index_instruction = indices.at(i);
591
_access_indexed_info.at_put(index_instruction->id(), NULL);
592
}
593
indices.clear();
594
}
595
}
596
597
bool RangeCheckEliminator::set_process_block_flags(BlockBegin *block) {
598
Instruction *cur = block;
599
bool process = false;
600
601
while (cur) {
602
process |= (cur->as_AccessIndexed() != NULL);
603
cur = cur->next();
604
}
605
606
BlockList *dominates = block->dominates();
607
for (int i=0; i<dominates->length(); i++) {
608
BlockBegin *next = dominates->at(i);
609
process |= set_process_block_flags(next);
610
}
611
612
if (!process) {
613
block->set(BlockBegin::donot_eliminate_range_checks);
614
}
615
return process;
616
}
617
618
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) {
619
bool upper_check = true;
620
assert(lower_instr || lower >= 0, "If no lower_instr present, lower must be greater 0");
621
assert(!lower_instr || lower_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
622
assert(!upper_instr || upper_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
623
assert(array_instr, "Array instruction must exist");
624
assert(array_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
625
assert(!length_instr || length_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller");
626
627
if (upper_instr && upper_instr->as_ArrayLength() && upper_instr->as_ArrayLength()->array() == array_instr) {
628
// static check
629
if (upper >= 0) return false; // would always trigger a deopt:
630
// array_length + x >= array_length, x >= 0 is always true
631
upper_check = false;
632
}
633
if (lower_instr && lower_instr->as_ArrayLength() && lower_instr->as_ArrayLength()->array() == array_instr) {
634
if (lower > 0) return false;
635
}
636
// No upper check required -> skip
637
if (upper_check && upper_instr && upper_instr->type()->as_ObjectType() && upper_instr == array_instr) {
638
// upper_instr is object means that the upper bound is the length
639
// of the upper_instr.
640
return false;
641
}
642
return true;
643
}
644
645
Instruction* RangeCheckEliminator::insert_after(Instruction* insert_position, Instruction* instr, int bci) {
646
if (bci != -1) {
647
NOT_PRODUCT(instr->set_printable_bci(bci));
648
return insert_position->insert_after(instr);
649
} else {
650
return insert_position->insert_after_same_bci(instr);
651
}
652
}
653
654
Instruction* RangeCheckEliminator::predicate(Instruction* left, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci) {
655
RangeCheckPredicate *deoptimize = new RangeCheckPredicate(left, cond, true, right, state->copy());
656
return insert_after(insert_position, deoptimize, bci);
657
}
658
659
Instruction* RangeCheckEliminator::predicate_cmp_with_const(Instruction* instr, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci) {
660
Constant *const_instr = new Constant(new IntConstant(constant));
661
insert_position = insert_after(insert_position, const_instr, bci);
662
return predicate(instr, cond, const_instr, state, insert_position);
663
}
664
665
Instruction* RangeCheckEliminator::predicate_add(Instruction* left, int left_const, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci) {
666
Constant *constant = new Constant(new IntConstant(left_const));
667
insert_position = insert_after(insert_position, constant, bci);
668
ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, constant, left, NULL);
669
insert_position = insert_position->insert_after_same_bci(ao);
670
return predicate(ao, cond, right, state, insert_position);
671
}
672
673
Instruction* RangeCheckEliminator::predicate_add_cmp_with_const(Instruction* left, int left_const, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci) {
674
Constant *const_instr = new Constant(new IntConstant(constant));
675
insert_position = insert_after(insert_position, const_instr, bci);
676
return predicate_add(left, left_const, cond, const_instr, state, insert_position);
677
}
678
679
// Insert deoptimization
680
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) {
681
assert(is_ok_for_deoptimization(insert_position, array_instr, length_instr, lower_instr, lower, upper_instr, upper), "should have been tested before");
682
bool upper_check = !(upper_instr && upper_instr->as_ArrayLength() && upper_instr->as_ArrayLength()->array() == array_instr);
683
684
int bci = NOT_PRODUCT(ai->printable_bci()) PRODUCT_ONLY(-1);
685
if (lower_instr) {
686
assert(!lower_instr->type()->as_ObjectType(), "Must not be object type");
687
if (lower == 0) {
688
// Compare for less than 0
689
insert_position = predicate_cmp_with_const(lower_instr, Instruction::lss, 0, state, insert_position, bci);
690
} else if (lower > 0) {
691
// Compare for smaller 0
692
insert_position = predicate_add_cmp_with_const(lower_instr, lower, Instruction::lss, 0, state, insert_position, bci);
693
} else {
694
assert(lower < 0, "");
695
// Add 1
696
lower++;
697
lower = -lower;
698
// Compare for smaller or equal 0
699
insert_position = predicate_cmp_with_const(lower_instr, Instruction::leq, lower, state, insert_position, bci);
700
}
701
}
702
703
// No upper check required -> skip
704
if (!upper_check) return;
705
706
// We need to know length of array
707
if (!length_instr) {
708
// Load length if necessary
709
ArrayLength *length = new ArrayLength(array_instr, state->copy());
710
NOT_PRODUCT(length->set_printable_bci(ai->printable_bci()));
711
length->set_exception_state(length->state_before());
712
length->set_flag(Instruction::DeoptimizeOnException, true);
713
insert_position = insert_position->insert_after(length);
714
length_instr = length;
715
}
716
717
if (!upper_instr) {
718
// Compare for geq array.length
719
insert_position = predicate_cmp_with_const(length_instr, Instruction::leq, upper, state, insert_position, bci);
720
} else {
721
if (upper_instr->type()->as_ObjectType()) {
722
assert(state, "must not be null");
723
assert(upper_instr != array_instr, "should be");
724
ArrayLength *length = new ArrayLength(upper_instr, state->copy());
725
NOT_PRODUCT(length->set_printable_bci(ai->printable_bci()));
726
length->set_flag(Instruction::DeoptimizeOnException, true);
727
length->set_exception_state(length->state_before());
728
insert_position = insert_position->insert_after(length);
729
upper_instr = length;
730
}
731
assert(upper_instr->type()->as_IntType(), "Must not be object type!");
732
733
if (upper == 0) {
734
// Compare for geq array.length
735
insert_position = predicate(upper_instr, Instruction::geq, length_instr, state, insert_position, bci);
736
} else if (upper < 0) {
737
// Compare for geq array.length
738
insert_position = predicate_add(upper_instr, upper, Instruction::geq, length_instr, state, insert_position, bci);
739
} else {
740
assert(upper > 0, "");
741
upper = -upper;
742
// Compare for geq array.length
743
insert_position = predicate_add(length_instr, upper, Instruction::leq, upper_instr, state, insert_position, bci);
744
}
745
}
746
}
747
748
// Add if condition
749
void RangeCheckEliminator::add_if_condition(IntegerStack &pushed, Value x, Value y, Instruction::Condition condition) {
750
if (y->as_Constant()) return;
751
752
int const_value = 0;
753
Value instr_value = x;
754
Constant *c = x->as_Constant();
755
ArithmeticOp *ao = x->as_ArithmeticOp();
756
757
if (c != NULL) {
758
const_value = c->type()->as_IntConstant()->value();
759
instr_value = NULL;
760
} else if (ao != NULL && (!ao->x()->as_Constant() || !ao->y()->as_Constant()) && ((ao->op() == Bytecodes::_isub && ao->y()->as_Constant()) || ao->op() == Bytecodes::_iadd)) {
761
assert(!ao->x()->as_Constant() || !ao->y()->as_Constant(), "At least one operator must be non-constant!");
762
assert(ao->op() == Bytecodes::_isub || ao->op() == Bytecodes::_iadd, "Operation has to be add or sub!");
763
c = ao->x()->as_Constant();
764
if (c != NULL) {
765
const_value = c->type()->as_IntConstant()->value();
766
instr_value = ao->y();
767
} else {
768
c = ao->y()->as_Constant();
769
if (c != NULL) {
770
const_value = c->type()->as_IntConstant()->value();
771
instr_value = ao->x();
772
}
773
}
774
if (ao->op() == Bytecodes::_isub) {
775
assert(ao->y()->as_Constant(), "1 - x not supported, only x - 1 is valid!");
776
if (const_value > min_jint) {
777
const_value = -const_value;
778
} else {
779
const_value = 0;
780
instr_value = x;
781
}
782
}
783
}
784
785
update_bound(pushed, y, condition, instr_value, const_value);
786
}
787
788
// Process If
789
void RangeCheckEliminator::process_if(IntegerStack &pushed, BlockBegin *block, If *cond) {
790
// Only if we are direct true / false successor and NOT both ! (even this may occur)
791
if ((cond->tsux() == block || cond->fsux() == block) && cond->tsux() != cond->fsux()) {
792
Instruction::Condition condition = cond->cond();
793
if (cond->fsux() == block) {
794
condition = Instruction::negate(condition);
795
}
796
Value x = cond->x();
797
Value y = cond->y();
798
if (x->type()->as_IntType() && y->type()->as_IntType()) {
799
add_if_condition(pushed, y, x, condition);
800
add_if_condition(pushed, x, y, Instruction::mirror(condition));
801
}
802
}
803
}
804
805
// Process access indexed
806
void RangeCheckEliminator::process_access_indexed(BlockBegin *loop_header, BlockBegin *block, AccessIndexed *ai) {
807
TRACE_RANGE_CHECK_ELIMINATION(
808
tty->fill_to(block->dominator_depth()*2)
809
);
810
TRACE_RANGE_CHECK_ELIMINATION(
811
tty->print_cr("Access indexed: index=%d length=%d", ai->index()->id(), (ai->length() != NULL ? ai->length()->id() :-1 ))
812
);
813
814
if (ai->check_flag(Instruction::NeedsRangeCheckFlag)) {
815
Bound *index_bound = get_bound(ai->index());
816
if (!index_bound->has_lower() || !index_bound->has_upper()) {
817
TRACE_RANGE_CHECK_ELIMINATION(
818
tty->fill_to(block->dominator_depth()*2);
819
tty->print_cr("Index instruction %d has no lower and/or no upper bound!", ai->index()->id())
820
);
821
return;
822
}
823
824
Bound *array_bound;
825
if (ai->length()) {
826
array_bound = get_bound(ai->length());
827
} else {
828
array_bound = get_bound(ai->array());
829
}
830
831
TRACE_RANGE_CHECK_ELIMINATION(
832
tty->fill_to(block->dominator_depth()*2);
833
tty->print("Index bound: ");
834
index_bound->print();
835
tty->print(", Array bound: ");
836
array_bound->print();
837
tty->cr();
838
);
839
840
if (in_array_bound(index_bound, ai->array()) ||
841
(index_bound && array_bound && index_bound->is_smaller(array_bound) && !index_bound->lower_instr() && index_bound->lower() >= 0)) {
842
TRACE_RANGE_CHECK_ELIMINATION(
843
tty->fill_to(block->dominator_depth()*2);
844
tty->print_cr("Bounds check for instruction %d in block B%d can be fully eliminated!", ai->id(), ai->block()->block_id())
845
);
846
847
remove_range_check(ai);
848
} else if (_optimistic && loop_header) {
849
assert(ai->array(), "Array must not be null!");
850
assert(ai->index(), "Index must not be null!");
851
852
// Array instruction
853
Instruction *array_instr = ai->array();
854
if (!loop_invariant(loop_header, array_instr)) {
855
TRACE_RANGE_CHECK_ELIMINATION(
856
tty->fill_to(block->dominator_depth()*2);
857
tty->print_cr("Array %d is not loop invariant to header B%d", ai->array()->id(), loop_header->block_id())
858
);
859
return;
860
}
861
862
// Lower instruction
863
Value index_instr = ai->index();
864
Value lower_instr = index_bound->lower_instr();
865
if (!loop_invariant(loop_header, lower_instr)) {
866
TRACE_RANGE_CHECK_ELIMINATION(
867
tty->fill_to(block->dominator_depth()*2);
868
tty->print_cr("Lower instruction %d not loop invariant!", lower_instr->id())
869
);
870
return;
871
}
872
if (!lower_instr && index_bound->lower() < 0) {
873
TRACE_RANGE_CHECK_ELIMINATION(
874
tty->fill_to(block->dominator_depth()*2);
875
tty->print_cr("Lower bound smaller than 0 (%d)!", index_bound->lower())
876
);
877
return;
878
}
879
880
// Upper instruction
881
Value upper_instr = index_bound->upper_instr();
882
if (!loop_invariant(loop_header, upper_instr)) {
883
TRACE_RANGE_CHECK_ELIMINATION(
884
tty->fill_to(block->dominator_depth()*2);
885
tty->print_cr("Upper instruction %d not loop invariant!", upper_instr->id())
886
);
887
return;
888
}
889
890
// Length instruction
891
Value length_instr = ai->length();
892
if (!loop_invariant(loop_header, length_instr)) {
893
// Generate length instruction yourself!
894
length_instr = NULL;
895
}
896
897
TRACE_RANGE_CHECK_ELIMINATION(
898
tty->fill_to(block->dominator_depth()*2);
899
tty->print_cr("LOOP INVARIANT access indexed %d found in block B%d!", ai->id(), ai->block()->block_id())
900
);
901
902
BlockBegin *pred_block = loop_header->dominator();
903
assert(pred_block != NULL, "Every loop header has a dominator!");
904
BlockEnd *pred_block_end = pred_block->end();
905
Instruction *insert_position = pred_block_end->prev();
906
ValueStack *state = pred_block_end->state_before();
907
if (pred_block_end->as_Goto() && state == NULL) state = pred_block_end->state();
908
assert(state, "State must not be null");
909
910
// Add deoptimization to dominator of loop header
911
TRACE_RANGE_CHECK_ELIMINATION(
912
tty->fill_to(block->dominator_depth()*2);
913
tty->print_cr("Inserting deopt at bci %d in block B%d!", state->bci(), insert_position->block()->block_id())
914
);
915
916
if (!is_ok_for_deoptimization(insert_position, array_instr, length_instr, lower_instr, index_bound->lower(), upper_instr, index_bound->upper())) {
917
TRACE_RANGE_CHECK_ELIMINATION(
918
tty->fill_to(block->dominator_depth()*2);
919
tty->print_cr("Could not eliminate because of static analysis!")
920
);
921
return;
922
}
923
924
insert_deoptimization(state, insert_position, array_instr, length_instr, lower_instr, index_bound->lower(), upper_instr, index_bound->upper(), ai);
925
926
// Finally remove the range check!
927
remove_range_check(ai);
928
}
929
}
930
}
931
932
void RangeCheckEliminator::remove_range_check(AccessIndexed *ai) {
933
ai->set_flag(Instruction::NeedsRangeCheckFlag, false);
934
// no range check, no need for the length instruction anymore
935
ai->clear_length();
936
937
TRACE_RANGE_CHECK_ELIMINATION(
938
tty->fill_to(ai->dominator_depth()*2);
939
tty->print_cr("Range check for instruction %d eliminated!", ai->id());
940
);
941
942
ASSERT_RANGE_CHECK_ELIMINATION(
943
Value array_length = ai->length();
944
if (!array_length) {
945
array_length = ai->array();
946
assert(array_length->type()->as_ObjectType(), "Has to be object type!");
947
}
948
int cur_constant = -1;
949
Value cur_value = array_length;
950
if (cur_value->type()->as_IntConstant()) {
951
cur_constant += cur_value->type()->as_IntConstant()->value();
952
cur_value = NULL;
953
}
954
Bound *new_index_bound = new Bound(0, NULL, cur_constant, cur_value);
955
add_assertions(new_index_bound, ai->index(), ai);
956
);
957
}
958
959
// Calculate bounds for instruction in this block and children blocks in the dominator tree
960
void RangeCheckEliminator::calc_bounds(BlockBegin *block, BlockBegin *loop_header) {
961
// Ensures a valid loop_header
962
assert(!loop_header || loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Loop header has to be real !");
963
964
// Tracing output
965
TRACE_RANGE_CHECK_ELIMINATION(
966
tty->fill_to(block->dominator_depth()*2);
967
tty->print_cr("Block B%d", block->block_id());
968
);
969
970
// Pushed stack for conditions
971
IntegerStack pushed;
972
// Process If
973
BlockBegin *parent = block->dominator();
974
if (parent != NULL) {
975
If *cond = parent->end()->as_If();
976
if (cond != NULL) {
977
process_if(pushed, block, cond);
978
}
979
}
980
981
// Interate over current block
982
InstructionList arrays;
983
AccessIndexedList accessIndexed;
984
Instruction *cur = block;
985
986
while (cur) {
987
// Ensure cur wasn't inserted during the elimination
988
if (cur->id() < this->_bounds.length()) {
989
// Process only if it is an access indexed instruction
990
AccessIndexed *ai = cur->as_AccessIndexed();
991
if (ai != NULL) {
992
process_access_indexed(loop_header, block, ai);
993
accessIndexed.append(ai);
994
if (!arrays.contains(ai->array())) {
995
arrays.append(ai->array());
996
}
997
Bound *b = get_bound(ai->index());
998
if (!b->lower_instr()) {
999
// Lower bound is constant
1000
update_bound(pushed, ai->index(), Instruction::geq, NULL, 0);
1001
}
1002
if (!b->has_upper()) {
1003
if (ai->length() && ai->length()->type()->as_IntConstant()) {
1004
int value = ai->length()->type()->as_IntConstant()->value();
1005
update_bound(pushed, ai->index(), Instruction::lss, NULL, value);
1006
} else {
1007
// Has no upper bound
1008
Instruction *instr = ai->length();
1009
if (instr == NULL) instr = ai->array();
1010
update_bound(pushed, ai->index(), Instruction::lss, instr, 0);
1011
}
1012
}
1013
}
1014
}
1015
cur = cur->next();
1016
}
1017
1018
// Output current condition stack
1019
TRACE_RANGE_CHECK_ELIMINATION(dump_condition_stack(block));
1020
1021
// Do in block motion of range checks
1022
in_block_motion(block, accessIndexed, arrays);
1023
1024
// Call all dominated blocks
1025
for (int i=0; i<block->dominates()->length(); i++) {
1026
BlockBegin *next = block->dominates()->at(i);
1027
if (!next->is_set(BlockBegin::donot_eliminate_range_checks)) {
1028
// if current block is a loop header and:
1029
// - next block belongs to the same loop
1030
// or
1031
// - next block belongs to an inner loop
1032
// then current block is the loop header for next block
1033
if (block->is_set(BlockBegin::linear_scan_loop_header_flag) && (block->loop_index() == next->loop_index() || next->loop_depth() > block->loop_depth())) {
1034
calc_bounds(next, block);
1035
} else {
1036
calc_bounds(next, loop_header);
1037
}
1038
}
1039
}
1040
1041
// Reset stack
1042
for (int i=0; i<pushed.length(); i++) {
1043
_bounds.at(pushed.at(i))->pop();
1044
}
1045
}
1046
1047
#ifndef PRODUCT
1048
// Dump condition stack
1049
void RangeCheckEliminator::dump_condition_stack(BlockBegin *block) {
1050
for (int i=0; i<_ir->linear_scan_order()->length(); i++) {
1051
BlockBegin *cur_block = _ir->linear_scan_order()->at(i);
1052
Instruction *instr = cur_block;
1053
for_each_phi_fun(cur_block, phi,
1054
BoundStack *bound_stack = _bounds.at(phi->id());
1055
if (bound_stack && bound_stack->length() > 0) {
1056
Bound *bound = bound_stack->top();
1057
if ((bound->has_lower() || bound->has_upper()) && (bound->lower_instr() != phi || bound->upper_instr() != phi || bound->lower() != 0 || bound->upper() != 0)) {
1058
TRACE_RANGE_CHECK_ELIMINATION(tty->fill_to(2*block->dominator_depth());
1059
tty->print("i%d", phi->id());
1060
tty->print(": ");
1061
bound->print();
1062
tty->cr();
1063
);
1064
}
1065
});
1066
1067
while (!instr->as_BlockEnd()) {
1068
if (instr->id() < _bounds.length()) {
1069
BoundStack *bound_stack = _bounds.at(instr->id());
1070
if (bound_stack && bound_stack->length() > 0) {
1071
Bound *bound = bound_stack->top();
1072
if ((bound->has_lower() || bound->has_upper()) && (bound->lower_instr() != instr || bound->upper_instr() != instr || bound->lower() != 0 || bound->upper() != 0)) {
1073
TRACE_RANGE_CHECK_ELIMINATION(tty->fill_to(2*block->dominator_depth());
1074
tty->print("i%d", instr->id());
1075
tty->print(": ");
1076
bound->print();
1077
tty->cr();
1078
);
1079
}
1080
}
1081
}
1082
instr = instr->next();
1083
}
1084
}
1085
}
1086
#endif
1087
1088
#ifdef ASSERT
1089
// Verification or the IR
1090
RangeCheckEliminator::Verification::Verification(IR *ir) : _used(BlockBegin::number_of_blocks(), BlockBegin::number_of_blocks(), false) {
1091
this->_ir = ir;
1092
ir->iterate_linear_scan_order(this);
1093
}
1094
1095
// Verify this block
1096
void RangeCheckEliminator::Verification::block_do(BlockBegin *block) {
1097
If *cond = block->end()->as_If();
1098
// Watch out: tsux and fsux can be the same!
1099
if (block->number_of_sux() > 1) {
1100
for (int i=0; i<block->number_of_sux(); i++) {
1101
BlockBegin *sux = block->sux_at(i);
1102
BlockBegin *pred = NULL;
1103
for (int j=0; j<sux->number_of_preds(); j++) {
1104
BlockBegin *cur = sux->pred_at(j);
1105
assert(cur != NULL, "Predecessor must not be null");
1106
if (!pred) {
1107
pred = cur;
1108
}
1109
assert(cur == pred, "Block must not have more than one predecessor if its predecessor has more than one successor");
1110
}
1111
assert(sux->number_of_preds() >= 1, "Block must have at least one predecessor");
1112
assert(sux->pred_at(0) == block, "Wrong successor");
1113
}
1114
}
1115
1116
BlockBegin *dominator = block->dominator();
1117
if (dominator) {
1118
assert(block != _ir->start(), "Start block must not have a dominator!");
1119
assert(can_reach(dominator, block), "Dominator can't reach his block !");
1120
assert(can_reach(_ir->start(), dominator), "Dominator is unreachable !");
1121
assert(!can_reach(_ir->start(), block, dominator), "Wrong dominator ! Block can be reached anyway !");
1122
BlockList *all_blocks = _ir->linear_scan_order();
1123
for (int i=0; i<all_blocks->length(); i++) {
1124
BlockBegin *cur = all_blocks->at(i);
1125
if (cur != dominator && cur != block) {
1126
assert(can_reach(dominator, block, cur), "There has to be another dominator!");
1127
}
1128
}
1129
} else {
1130
assert(block == _ir->start(), "Only start block must not have a dominator");
1131
}
1132
1133
if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) {
1134
int loop_index = block->loop_index();
1135
BlockList *all_blocks = _ir->linear_scan_order();
1136
assert(block->number_of_preds() >= 1, "Block must have at least one predecessor");
1137
assert(!block->is_set(BlockBegin::exception_entry_flag), "Loop header must not be exception handler!");
1138
1139
bool loop_through_xhandler = false;
1140
for (int i=0; i<block->number_of_sux(); i++) {
1141
BlockBegin *sux = block->sux_at(i);
1142
if (!loop_through_xhandler) {
1143
if (sux->loop_depth() == block->loop_depth() && sux->loop_index() != block->loop_index()) {
1144
loop_through_xhandler = is_backbranch_from_xhandler(block);
1145
assert(loop_through_xhandler, "Loop indices have to be the same if same depths but no backbranch from xhandler");
1146
}
1147
}
1148
assert(sux->loop_depth() == block->loop_depth() || sux->loop_index() != block->loop_index(), "Loop index has to be different");
1149
}
1150
1151
for (int i=0; i<all_blocks->length(); i++) {
1152
BlockBegin *cur = all_blocks->at(i);
1153
if (cur->loop_index() == loop_index && cur != block) {
1154
assert(dominates(block->dominator(), cur), "Dominator of loop header must dominate all loop blocks");
1155
}
1156
}
1157
}
1158
1159
Instruction *cur = block;
1160
while (cur) {
1161
assert(cur->block() == block, "Block begin has to be set correctly!");
1162
cur = cur->next();
1163
}
1164
}
1165
1166
// Called when a successor of a block has the same loop depth but a different loop index. This can happen if a backbranch comes from
1167
// an exception handler of a loop head block, for example, when a loop is only executed once on the non-exceptional path but is
1168
// repeated in case of an exception. In this case, the edge block->sux is not critical and was not split before.
1169
// Check if there is such a backbranch from an xhandler of 'block'.
1170
bool RangeCheckEliminator::Verification::is_backbranch_from_xhandler(BlockBegin* block) {
1171
for (int i = 0; i < block->number_of_exception_handlers(); i++) {
1172
BlockBegin *xhandler = block->exception_handler_at(i);
1173
for (int j = 0; j < block->number_of_preds(); j++) {
1174
if (dominates(xhandler, block->pred_at(j)) || xhandler == block->pred_at(j)) {
1175
return true;
1176
}
1177
}
1178
}
1179
1180
// In case of nested xhandlers, we need to walk through the loop (and all blocks belonging to exception handlers)
1181
// to find an xhandler of 'block'.
1182
if (block->number_of_exception_handlers() > 0) {
1183
for (int i = 0; i < block->number_of_preds(); i++) {
1184
BlockBegin* pred = block->pred_at(i);
1185
if (pred->loop_index() == block->loop_index()) {
1186
// Only check blocks that belong to the loop
1187
// Do a BFS to find an xhandler block of 'block' starting from 'pred'
1188
ResourceMark rm;
1189
ResourceBitMap visited(BlockBegin::number_of_blocks());
1190
BlockBeginList list;
1191
list.push(pred);
1192
while (!list.is_empty()) {
1193
BlockBegin* next = list.pop();
1194
if (!visited.at(next->block_id())) {
1195
visited.set_bit(next->block_id());
1196
for (int j = 0; j < block->number_of_exception_handlers(); j++) {
1197
if (next == block->exception_handler_at(j)) {
1198
return true;
1199
}
1200
}
1201
for (int j = 0; j < next->number_of_preds(); j++) {
1202
if (next->pred_at(j) != block) {
1203
list.push(next->pred_at(j));
1204
}
1205
}
1206
}
1207
}
1208
}
1209
}
1210
}
1211
return false;
1212
}
1213
1214
// Loop header must dominate all loop blocks
1215
bool RangeCheckEliminator::Verification::dominates(BlockBegin *dominator, BlockBegin *block) {
1216
BlockBegin *cur = block->dominator();
1217
while (cur && cur != dominator) {
1218
cur = cur->dominator();
1219
}
1220
return cur == dominator;
1221
}
1222
1223
// Try to reach Block end beginning in Block start and not using Block dont_use
1224
bool RangeCheckEliminator::Verification::can_reach(BlockBegin *start, BlockBegin *end, BlockBegin *dont_use /* = NULL */) {
1225
if (start == end) return start != dont_use;
1226
// Simple BSF from start to end
1227
// BlockBeginList _current;
1228
for (int i=0; i < _used.length(); i++) {
1229
_used.at_put(i, false);
1230
}
1231
_current.trunc_to(0);
1232
_successors.trunc_to(0);
1233
if (start != dont_use) {
1234
_current.push(start);
1235
_used.at_put(start->block_id(), true);
1236
}
1237
1238
// BlockBeginList _successors;
1239
while (_current.length() > 0) {
1240
BlockBegin *cur = _current.pop();
1241
// Add exception handlers to list
1242
for (int i=0; i<cur->number_of_exception_handlers(); i++) {
1243
BlockBegin *xhandler = cur->exception_handler_at(i);
1244
_successors.push(xhandler);
1245
// Add exception handlers of _successors to list
1246
for (int j=0; j<xhandler->number_of_exception_handlers(); j++) {
1247
BlockBegin *sux_xhandler = xhandler->exception_handler_at(j);
1248
_successors.push(sux_xhandler);
1249
}
1250
}
1251
// Add normal _successors to list
1252
for (int i=0; i<cur->number_of_sux(); i++) {
1253
BlockBegin *sux = cur->sux_at(i);
1254
_successors.push(sux);
1255
// Add exception handlers of _successors to list
1256
for (int j=0; j<sux->number_of_exception_handlers(); j++) {
1257
BlockBegin *xhandler = sux->exception_handler_at(j);
1258
_successors.push(xhandler);
1259
}
1260
}
1261
for (int i=0; i<_successors.length(); i++) {
1262
BlockBegin *sux = _successors.at(i);
1263
assert(sux != NULL, "Successor must not be NULL!");
1264
if (sux == end) {
1265
return true;
1266
}
1267
if (sux != dont_use && !_used.at(sux->block_id())) {
1268
_used.at_put(sux->block_id(), true);
1269
_current.push(sux);
1270
}
1271
}
1272
_successors.trunc_to(0);
1273
}
1274
1275
return false;
1276
}
1277
#endif // ASSERT
1278
1279
// Bound
1280
RangeCheckEliminator::Bound::~Bound() {
1281
}
1282
1283
// Bound constructor
1284
RangeCheckEliminator::Bound::Bound() {
1285
this->_lower = min_jint;
1286
this->_upper = max_jint;
1287
this->_lower_instr = NULL;
1288
this->_upper_instr = NULL;
1289
}
1290
1291
// Bound constructor
1292
RangeCheckEliminator::Bound::Bound(int lower, Value lower_instr, int upper, Value upper_instr) {
1293
assert(!lower_instr || !lower_instr->as_Constant() || !lower_instr->type()->as_IntConstant(), "Must not be constant!");
1294
assert(!upper_instr || !upper_instr->as_Constant() || !upper_instr->type()->as_IntConstant(), "Must not be constant!");
1295
this->_lower = lower;
1296
this->_upper = upper;
1297
this->_lower_instr = lower_instr;
1298
this->_upper_instr = upper_instr;
1299
}
1300
1301
// Bound constructor
1302
RangeCheckEliminator::Bound::Bound(Instruction::Condition cond, Value v, int constant) {
1303
assert(!v || (v->type() && (v->type()->as_IntType() || v->type()->as_ObjectType())), "Type must be array or integer!");
1304
assert(!v || !v->as_Constant() || !v->type()->as_IntConstant(), "Must not be constant!");
1305
1306
if (cond == Instruction::eql) {
1307
_lower = constant;
1308
_lower_instr = v;
1309
_upper = constant;
1310
_upper_instr = v;
1311
} else if (cond == Instruction::neq) {
1312
_lower = min_jint;
1313
_upper = max_jint;
1314
_lower_instr = NULL;
1315
_upper_instr = NULL;
1316
if (v == NULL) {
1317
if (constant == min_jint) {
1318
_lower++;
1319
}
1320
if (constant == max_jint) {
1321
_upper--;
1322
}
1323
}
1324
} else if (cond == Instruction::geq) {
1325
_lower = constant;
1326
_lower_instr = v;
1327
_upper = max_jint;
1328
_upper_instr = NULL;
1329
} else if (cond == Instruction::leq) {
1330
_lower = min_jint;
1331
_lower_instr = NULL;
1332
_upper = constant;
1333
_upper_instr = v;
1334
} else {
1335
ShouldNotReachHere();
1336
}
1337
}
1338
1339
// Set lower
1340
void RangeCheckEliminator::Bound::set_lower(int value, Value v) {
1341
assert(!v || !v->as_Constant() || !v->type()->as_IntConstant(), "Must not be constant!");
1342
this->_lower = value;
1343
this->_lower_instr = v;
1344
}
1345
1346
// Set upper
1347
void RangeCheckEliminator::Bound::set_upper(int value, Value v) {
1348
assert(!v || !v->as_Constant() || !v->type()->as_IntConstant(), "Must not be constant!");
1349
this->_upper = value;
1350
this->_upper_instr = v;
1351
}
1352
1353
// Add constant -> no overflow may occur
1354
void RangeCheckEliminator::Bound::add_constant(int value) {
1355
this->_lower += value;
1356
this->_upper += value;
1357
}
1358
1359
// or
1360
void RangeCheckEliminator::Bound::or_op(Bound *b) {
1361
// Watch out, bound is not guaranteed not to overflow!
1362
// Update lower bound
1363
if (_lower_instr != b->_lower_instr || (_lower_instr && _lower != b->_lower)) {
1364
_lower_instr = NULL;
1365
_lower = min_jint;
1366
} else {
1367
_lower = MIN2(_lower, b->_lower);
1368
}
1369
// Update upper bound
1370
if (_upper_instr != b->_upper_instr || (_upper_instr && _upper != b->_upper)) {
1371
_upper_instr = NULL;
1372
_upper = max_jint;
1373
} else {
1374
_upper = MAX2(_upper, b->_upper);
1375
}
1376
}
1377
1378
// and
1379
void RangeCheckEliminator::Bound::and_op(Bound *b) {
1380
// Update lower bound
1381
if (_lower_instr == b->_lower_instr) {
1382
_lower = MAX2(_lower, b->_lower);
1383
}
1384
if (b->has_lower()) {
1385
bool set = true;
1386
if (_lower_instr != NULL && b->_lower_instr != NULL) {
1387
set = (_lower_instr->dominator_depth() > b->_lower_instr->dominator_depth());
1388
}
1389
if (set) {
1390
_lower = b->_lower;
1391
_lower_instr = b->_lower_instr;
1392
}
1393
}
1394
// Update upper bound
1395
if (_upper_instr == b->_upper_instr) {
1396
_upper = MIN2(_upper, b->_upper);
1397
}
1398
if (b->has_upper()) {
1399
bool set = true;
1400
if (_upper_instr != NULL && b->_upper_instr != NULL) {
1401
set = (_upper_instr->dominator_depth() > b->_upper_instr->dominator_depth());
1402
}
1403
if (set) {
1404
_upper = b->_upper;
1405
_upper_instr = b->_upper_instr;
1406
}
1407
}
1408
}
1409
1410
// has_upper
1411
bool RangeCheckEliminator::Bound::has_upper() {
1412
return _upper_instr != NULL || _upper < max_jint;
1413
}
1414
1415
// is_smaller
1416
bool RangeCheckEliminator::Bound::is_smaller(Bound *b) {
1417
if (b->_lower_instr != _upper_instr) {
1418
return false;
1419
}
1420
return _upper < b->_lower;
1421
}
1422
1423
// has_lower
1424
bool RangeCheckEliminator::Bound::has_lower() {
1425
return _lower_instr != NULL || _lower > min_jint;
1426
}
1427
1428
// in_array_bound
1429
bool RangeCheckEliminator::in_array_bound(Bound *bound, Value array){
1430
if (!bound) return false;
1431
assert(array != NULL, "Must not be null!");
1432
assert(bound != NULL, "Must not be null!");
1433
if (bound->lower() >=0 && bound->lower_instr() == NULL && bound->upper() < 0 && bound->upper_instr() != NULL) {
1434
ArrayLength *len = bound->upper_instr()->as_ArrayLength();
1435
if (bound->upper_instr() == array || (len != NULL && len->array() == array)) {
1436
return true;
1437
}
1438
}
1439
return false;
1440
}
1441
1442
// remove_lower
1443
void RangeCheckEliminator::Bound::remove_lower() {
1444
_lower = min_jint;
1445
_lower_instr = NULL;
1446
}
1447
1448
// remove_upper
1449
void RangeCheckEliminator::Bound::remove_upper() {
1450
_upper = max_jint;
1451
_upper_instr = NULL;
1452
}
1453
1454
// upper
1455
int RangeCheckEliminator::Bound::upper() {
1456
return _upper;
1457
}
1458
1459
// lower
1460
int RangeCheckEliminator::Bound::lower() {
1461
return _lower;
1462
}
1463
1464
// upper_instr
1465
Value RangeCheckEliminator::Bound::upper_instr() {
1466
return _upper_instr;
1467
}
1468
1469
// lower_instr
1470
Value RangeCheckEliminator::Bound::lower_instr() {
1471
return _lower_instr;
1472
}
1473
1474
// print
1475
void RangeCheckEliminator::Bound::print() {
1476
tty->print("%s", "");
1477
if (this->_lower_instr || this->_lower != min_jint) {
1478
if (this->_lower_instr) {
1479
tty->print("i%d", this->_lower_instr->id());
1480
if (this->_lower > 0) {
1481
tty->print("+%d", _lower);
1482
}
1483
if (this->_lower < 0) {
1484
tty->print("%d", _lower);
1485
}
1486
} else {
1487
tty->print("%d", _lower);
1488
}
1489
tty->print(" <= ");
1490
}
1491
tty->print("x");
1492
if (this->_upper_instr || this->_upper != max_jint) {
1493
tty->print(" <= ");
1494
if (this->_upper_instr) {
1495
tty->print("i%d", this->_upper_instr->id());
1496
if (this->_upper > 0) {
1497
tty->print("+%d", _upper);
1498
}
1499
if (this->_upper < 0) {
1500
tty->print("%d", _upper);
1501
}
1502
} else {
1503
tty->print("%d", _upper);
1504
}
1505
}
1506
}
1507
1508
// Copy
1509
RangeCheckEliminator::Bound *RangeCheckEliminator::Bound::copy() {
1510
Bound *b = new Bound();
1511
b->_lower = _lower;
1512
b->_lower_instr = _lower_instr;
1513
b->_upper = _upper;
1514
b->_upper_instr = _upper_instr;
1515
return b;
1516
}
1517
1518
#ifdef ASSERT
1519
// Add assertion
1520
void RangeCheckEliminator::Bound::add_assertion(Instruction *instruction, Instruction *position, int i, Value instr, Instruction::Condition cond) {
1521
Instruction *result = position;
1522
Instruction *compare_with = NULL;
1523
ValueStack *state = position->state_before();
1524
if (position->as_BlockEnd() && !position->as_Goto()) {
1525
state = position->as_BlockEnd()->state_before();
1526
}
1527
Instruction *instruction_before = position->prev();
1528
if (position->as_Return() && Compilation::current()->method()->is_synchronized() && instruction_before->as_MonitorExit()) {
1529
instruction_before = instruction_before->prev();
1530
}
1531
result = instruction_before;
1532
// Load constant only if needed
1533
Constant *constant = NULL;
1534
if (i != 0 || !instr) {
1535
constant = new Constant(new IntConstant(i));
1536
NOT_PRODUCT(constant->set_printable_bci(position->printable_bci()));
1537
result = result->insert_after(constant);
1538
compare_with = constant;
1539
}
1540
1541
if (instr) {
1542
assert(instr->type()->as_ObjectType() || instr->type()->as_IntType(), "Type must be array or integer!");
1543
compare_with = instr;
1544
// Load array length if necessary
1545
Instruction *op = instr;
1546
if (instr->type()->as_ObjectType()) {
1547
assert(state, "must not be null");
1548
ArrayLength *length = new ArrayLength(instr, state->copy());
1549
NOT_PRODUCT(length->set_printable_bci(position->printable_bci()));
1550
length->set_exception_state(length->state_before());
1551
result = result->insert_after(length);
1552
op = length;
1553
compare_with = length;
1554
}
1555
// Add operation only if necessary
1556
if (constant) {
1557
ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, constant, op, NULL);
1558
NOT_PRODUCT(ao->set_printable_bci(position->printable_bci()));
1559
result = result->insert_after(ao);
1560
compare_with = ao;
1561
// TODO: Check that add operation does not overflow!
1562
}
1563
}
1564
assert(compare_with != NULL, "You have to compare with something!");
1565
assert(instruction != NULL, "Instruction must not be null!");
1566
1567
if (instruction->type()->as_ObjectType()) {
1568
// Load array length if necessary
1569
Instruction *op = instruction;
1570
assert(state, "must not be null");
1571
ArrayLength *length = new ArrayLength(instruction, state->copy());
1572
length->set_exception_state(length->state_before());
1573
NOT_PRODUCT(length->set_printable_bci(position->printable_bci()));
1574
result = result->insert_after(length);
1575
instruction = length;
1576
}
1577
1578
Assert *assert = new Assert(instruction, cond, false, compare_with);
1579
NOT_PRODUCT(assert->set_printable_bci(position->printable_bci()));
1580
result->insert_after(assert);
1581
}
1582
1583
// Add assertions
1584
void RangeCheckEliminator::add_assertions(Bound *bound, Instruction *instruction, Instruction *position) {
1585
// Add lower bound assertion
1586
if (bound->has_lower()) {
1587
bound->add_assertion(instruction, position, bound->lower(), bound->lower_instr(), Instruction::geq);
1588
}
1589
// Add upper bound assertion
1590
if (bound->has_upper()) {
1591
bound->add_assertion(instruction, position, bound->upper(), bound->upper_instr(), Instruction::leq);
1592
}
1593
}
1594
#endif
1595
1596