roland@4860: /* drchase@6680: * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. roland@4860: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. roland@4860: * roland@4860: * This code is free software; you can redistribute it and/or modify it roland@4860: * under the terms of the GNU General Public License version 2 only, as roland@4860: * published by the Free Software Foundation. roland@4860: * roland@4860: * This code is distributed in the hope that it will be useful, but WITHOUT roland@4860: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or roland@4860: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License roland@4860: * version 2 for more details (a copy is included in the LICENSE file that roland@4860: * accompanied this code). roland@4860: * roland@4860: * You should have received a copy of the GNU General Public License version roland@4860: * 2 along with this work; if not, write to the Free Software Foundation, roland@4860: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. roland@4860: * roland@4860: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA roland@4860: * or visit www.oracle.com if you need additional information or have any roland@4860: * questions. roland@4860: * roland@4860: */ roland@4860: roland@4860: #include "precompiled.hpp" roland@4860: #include "c1/c1_ValueStack.hpp" roland@4860: #include "c1/c1_RangeCheckElimination.hpp" roland@4860: #include "c1/c1_IR.hpp" roland@4860: #include "c1/c1_Canonicalizer.hpp" roland@4860: #include "c1/c1_ValueMap.hpp" roland@4860: #include "ci/ciMethodData.hpp" roland@4860: #include "runtime/deoptimization.hpp" roland@4860: roland@4860: // Macros for the Trace and the Assertion flag roland@4860: #ifdef ASSERT roland@4860: #define TRACE_RANGE_CHECK_ELIMINATION(code) if (TraceRangeCheckElimination) { code; } roland@4860: #define ASSERT_RANGE_CHECK_ELIMINATION(code) if (AssertRangeCheckElimination) { code; } roland@4860: #define TRACE_OR_ASSERT_RANGE_CHECK_ELIMINATION(code) if (TraceRangeCheckElimination || AssertRangeCheckElimination) { code; } roland@4860: #else roland@4860: #define TRACE_RANGE_CHECK_ELIMINATION(code) roland@4860: #define ASSERT_RANGE_CHECK_ELIMINATION(code) roland@4860: #define TRACE_OR_ASSERT_RANGE_CHECK_ELIMINATION(code) roland@4860: #endif roland@4860: roland@4860: // Entry point for the optimization roland@4860: void RangeCheckElimination::eliminate(IR *ir) { roland@4860: bool do_elimination = ir->compilation()->has_access_indexed(); roland@4860: ASSERT_RANGE_CHECK_ELIMINATION(do_elimination = true); roland@4860: if (do_elimination) { roland@4860: RangeCheckEliminator rce(ir); roland@4860: } roland@4860: } roland@4860: roland@4860: // Constructor roland@4860: RangeCheckEliminator::RangeCheckEliminator(IR *ir) : roland@4860: _bounds(Instruction::number_of_instructions(), NULL), roland@4860: _access_indexed_info(Instruction::number_of_instructions(), NULL) roland@4860: { roland@4860: _visitor.set_range_check_eliminator(this); roland@4860: _ir = ir; roland@4860: _number_of_instructions = Instruction::number_of_instructions(); roland@4860: _optimistic = ir->compilation()->is_optimistic(); roland@4860: roland@4860: TRACE_RANGE_CHECK_ELIMINATION( drchase@6680: tty->cr(); roland@4860: tty->print_cr("Range check elimination"); roland@4860: ir->method()->print_name(tty); drchase@6680: tty->cr(); roland@4860: ); roland@4860: roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->print_cr("optimistic=%d", (int)_optimistic); roland@4860: ); roland@4860: roland@4860: #ifdef ASSERT roland@4860: // Verifies several conditions that must be true on the IR-input. Only used for debugging purposes. roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->print_cr("Verification of IR . . ."); roland@4860: ); roland@4860: Verification verification(ir); roland@4860: #endif roland@4860: roland@4860: // Set process block flags roland@4860: // Optimization so a blocks is only processed if it contains an access indexed instruction or if roland@4860: // one of its children in the dominator tree contains an access indexed instruction. roland@4860: set_process_block_flags(ir->start()); roland@4860: roland@4860: // Pass over instructions in the dominator tree roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->print_cr("Starting pass over dominator tree . . .") roland@4860: ); roland@4860: calc_bounds(ir->start(), NULL); roland@4860: roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->print_cr("Finished!") roland@4860: ); roland@4860: } roland@4860: roland@4860: // Instruction specific work for some instructions roland@4860: // Constant roland@4860: void RangeCheckEliminator::Visitor::do_Constant(Constant *c) { roland@4860: IntConstant *ic = c->type()->as_IntConstant(); roland@4860: if (ic != NULL) { roland@4860: int value = ic->value(); roland@4860: _bound = new Bound(value, NULL, value, NULL); roland@4860: } roland@4860: } roland@4860: roland@4860: // LogicOp roland@4860: void RangeCheckEliminator::Visitor::do_LogicOp(LogicOp *lo) { roland@4860: if (lo->type()->as_IntType() && lo->op() == Bytecodes::_iand && (lo->x()->as_Constant() || lo->y()->as_Constant())) { roland@4860: int constant = 0; roland@4860: Constant *c = lo->x()->as_Constant(); roland@4860: if (c != NULL) { roland@4860: constant = c->type()->as_IntConstant()->value(); roland@4860: } else { roland@4860: constant = lo->y()->as_Constant()->type()->as_IntConstant()->value(); roland@4860: } roland@4860: if (constant >= 0) { roland@4860: _bound = new Bound(0, NULL, constant, NULL); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // Phi roland@4860: void RangeCheckEliminator::Visitor::do_Phi(Phi *phi) { roland@4860: if (!phi->type()->as_IntType() && !phi->type()->as_ObjectType()) return; roland@4860: roland@4860: BlockBegin *block = phi->block(); roland@4860: int op_count = phi->operand_count(); roland@4860: bool has_upper = true; roland@4860: bool has_lower = true; roland@4860: assert(phi, "Phi must not be null"); roland@4860: Bound *bound = NULL; roland@4860: roland@4860: // TODO: support more difficult phis roland@4860: for (int i=0; ioperand_at(i); roland@4860: roland@4860: if (v == phi) continue; roland@4860: roland@4860: // Check if instruction is connected with phi itself roland@4860: Op2 *op2 = v->as_Op2(); roland@4860: if (op2 != NULL) { roland@4860: Value x = op2->x(); roland@4860: Value y = op2->y(); roland@4860: if ((x == phi || y == phi)) { roland@4860: Value other = x; roland@4860: if (other == phi) { roland@4860: other = y; roland@4860: } roland@4860: ArithmeticOp *ao = v->as_ArithmeticOp(); roland@4860: if (ao != NULL && ao->op() == Bytecodes::_iadd) { roland@4860: assert(ao->op() == Bytecodes::_iadd, "Has to be add!"); roland@4860: if (ao->type()->as_IntType()) { roland@4860: Constant *c = other->as_Constant(); roland@4860: if (c != NULL) { roland@4860: assert(c->type()->as_IntConstant(), "Constant has to be of type integer"); roland@4860: int value = c->type()->as_IntConstant()->value(); roland@4860: if (value == 1) { roland@4860: has_upper = false; roland@4860: } else if (value > 1) { roland@4860: // Overflow not guaranteed roland@4860: has_upper = false; roland@4860: has_lower = false; roland@4860: } else if (value < 0) { roland@4860: has_lower = false; roland@4860: } roland@4860: continue; roland@4860: } roland@4860: } roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // No connection -> new bound roland@4860: Bound *v_bound = _rce->get_bound(v); roland@4860: Bound *cur_bound; roland@4860: int cur_constant = 0; roland@4860: Value cur_value = v; roland@4860: roland@4860: if (v->type()->as_IntConstant()) { roland@4860: cur_constant = v->type()->as_IntConstant()->value(); roland@4860: cur_value = NULL; roland@4860: } roland@4860: if (!v_bound->has_upper() || !v_bound->has_lower()) { roland@4860: cur_bound = new Bound(cur_constant, cur_value, cur_constant, cur_value); roland@4860: } else { roland@4860: cur_bound = v_bound; roland@4860: } roland@4860: if (cur_bound) { roland@4860: if (!bound) { roland@4860: bound = cur_bound->copy(); roland@4860: } else { roland@4860: bound->or_op(cur_bound); roland@4860: } roland@4860: } else { roland@4860: // No bound! roland@4860: bound = NULL; roland@4860: break; roland@4860: } roland@4860: } roland@4860: roland@4860: if (bound) { roland@4860: if (!has_upper) { roland@4860: bound->remove_upper(); roland@4860: } roland@4860: if (!has_lower) { roland@4860: bound->remove_lower(); roland@4860: } roland@4860: _bound = bound; roland@4860: } else { roland@4860: _bound = new Bound(); roland@4860: } roland@4860: } roland@4860: roland@4860: roland@4860: // ArithmeticOp roland@4860: void RangeCheckEliminator::Visitor::do_ArithmeticOp(ArithmeticOp *ao) { roland@4860: Value x = ao->x(); roland@4860: Value y = ao->y(); roland@4860: roland@4860: if (ao->op() == Bytecodes::_irem) { roland@4860: Bound* x_bound = _rce->get_bound(x); roland@4860: Bound* y_bound = _rce->get_bound(y); roland@4860: if (x_bound->lower() >= 0 && x_bound->lower_instr() == NULL && y->as_ArrayLength() != NULL) { roland@4860: _bound = new Bound(0, NULL, -1, y); roland@4860: } else { roland@4860: _bound = new Bound(); roland@4860: } roland@4860: } else if (!x->as_Constant() || !y->as_Constant()) { roland@4860: assert(!x->as_Constant() || !y->as_Constant(), "One of the operands must be non-constant!"); roland@4860: if (((x->as_Constant() || y->as_Constant()) && (ao->op() == Bytecodes::_iadd)) || (y->as_Constant() && ao->op() == Bytecodes::_isub)) { roland@4860: assert(ao->op() == Bytecodes::_iadd || ao->op() == Bytecodes::_isub, "Operand must be iadd or isub"); roland@4860: roland@4860: if (y->as_Constant()) { roland@4860: Value tmp = x; roland@4860: x = y; roland@4860: y = tmp; roland@4860: } roland@4860: assert(x->as_Constant()->type()->as_IntConstant(), "Constant must be int constant!"); roland@4860: roland@4860: // Constant now in x roland@4860: int const_value = x->as_Constant()->type()->as_IntConstant()->value(); roland@4860: if (ao->op() == Bytecodes::_iadd || const_value != min_jint) { roland@4860: if (ao->op() == Bytecodes::_isub) { roland@4860: const_value = -const_value; roland@4860: } roland@4860: roland@4860: Bound * bound = _rce->get_bound(y); roland@4860: if (bound->has_upper() && bound->has_lower()) { roland@4860: int new_lower = bound->lower() + const_value; roland@4860: jlong new_lowerl = ((jlong)bound->lower()) + const_value; roland@4860: int new_upper = bound->upper() + const_value; roland@4860: jlong new_upperl = ((jlong)bound->upper()) + const_value; roland@4860: roland@4860: if (((jlong)new_lower) == new_lowerl && ((jlong)new_upper == new_upperl)) { roland@4860: Bound *newBound = new Bound(new_lower, bound->lower_instr(), new_upper, bound->upper_instr()); roland@4860: _bound = newBound; roland@4860: } else { roland@4860: // overflow roland@4860: _bound = new Bound(); roland@4860: } roland@4860: } else { roland@4860: _bound = new Bound(); roland@4860: } roland@4860: } else { roland@4860: _bound = new Bound(); roland@4860: } roland@4860: } else { roland@4860: Bound *bound = _rce->get_bound(x); roland@4860: if (ao->op() == Bytecodes::_isub) { roland@4860: if (bound->lower_instr() == y) { roland@4860: _bound = new Bound(Instruction::geq, NULL, bound->lower()); roland@4860: } else { roland@4860: _bound = new Bound(); roland@4860: } roland@4860: } else { roland@4860: _bound = new Bound(); roland@4860: } roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // IfOp roland@4860: void RangeCheckEliminator::Visitor::do_IfOp(IfOp *ifOp) roland@4860: { roland@4860: if (ifOp->tval()->type()->as_IntConstant() && ifOp->fval()->type()->as_IntConstant()) { roland@4860: int min = ifOp->tval()->type()->as_IntConstant()->value(); roland@4860: int max = ifOp->fval()->type()->as_IntConstant()->value(); roland@4860: if (min > max) { roland@4860: // min ^= max ^= min ^= max; roland@4860: int tmp = min; roland@4860: min = max; roland@4860: max = tmp; roland@4860: } roland@4860: _bound = new Bound(min, NULL, max, NULL); roland@4860: } roland@4860: } roland@4860: roland@4860: // Get bound. Returns the current bound on Value v. Normally this is the topmost element on the bound stack. roland@4860: RangeCheckEliminator::Bound *RangeCheckEliminator::get_bound(Value v) { roland@4860: // Wrong type or NULL -> No bound roland@4860: if (!v || (!v->type()->as_IntType() && !v->type()->as_ObjectType())) return NULL; roland@4860: roland@4860: if (!_bounds[v->id()]) { roland@4860: // First (default) bound is calculated roland@4860: // Create BoundStack roland@4860: _bounds[v->id()] = new BoundStack(); roland@4860: _visitor.clear_bound(); roland@4860: Value visit_value = v; roland@4860: visit_value->visit(&_visitor); roland@4860: Bound *bound = _visitor.bound(); roland@4860: if (bound) { roland@4860: _bounds[v->id()]->push(bound); roland@4860: } roland@4860: if (_bounds[v->id()]->length() == 0) { roland@4860: assert(!(v->as_Constant() && v->type()->as_IntConstant()), "constants not handled here"); roland@4860: _bounds[v->id()]->push(new Bound()); roland@4860: } roland@4860: } else if (_bounds[v->id()]->length() == 0) { roland@4860: // To avoid endless loops, bound is currently in calculation -> nothing known about it roland@4860: return new Bound(); roland@4860: } roland@4860: roland@4860: // Return bound roland@4860: return _bounds[v->id()]->top(); roland@4860: } roland@4860: roland@4860: // Update bound roland@4860: void RangeCheckEliminator::update_bound(IntegerStack &pushed, Value v, Instruction::Condition cond, Value value, int constant) { roland@4860: if (cond == Instruction::gtr) { roland@4860: cond = Instruction::geq; roland@4860: constant++; roland@4860: } else if (cond == Instruction::lss) { roland@4860: cond = Instruction::leq; roland@4860: constant--; roland@4860: } roland@4860: Bound *bound = new Bound(cond, value, constant); roland@4860: update_bound(pushed, v, bound); roland@4860: } roland@4860: roland@4860: // Checks for loop invariance. Returns true if the instruction is outside of the loop which is identified by loop_header. roland@4860: bool RangeCheckEliminator::loop_invariant(BlockBegin *loop_header, Instruction *instruction) { roland@4860: assert(loop_header, "Loop header must not be null!"); roland@4860: if (!instruction) return true; roland@4860: return instruction->dominator_depth() < loop_header->dominator_depth(); roland@4860: } roland@4860: roland@4860: // Update bound. Pushes a new bound onto the stack. Tries to do a conjunction with the current bound. roland@4860: void RangeCheckEliminator::update_bound(IntegerStack &pushed, Value v, Bound *bound) { roland@4860: if (v->as_Constant()) { roland@4860: // No bound update for constants roland@4860: return; roland@4860: } roland@4860: if (!_bounds[v->id()]) { roland@4860: get_bound(v); roland@4860: assert(_bounds[v->id()], "Now Stack must exist"); roland@4860: } roland@4860: Bound *top = NULL; roland@4860: if (_bounds[v->id()]->length() > 0) { roland@4860: top = _bounds[v->id()]->top(); roland@4860: } roland@4860: if (top) { roland@4860: bound->and_op(top); roland@4860: } roland@4860: _bounds[v->id()]->push(bound); roland@4860: pushed.append(v->id()); roland@4860: } roland@4860: roland@4860: // Add instruction + idx for in block motion roland@4860: void RangeCheckEliminator::add_access_indexed_info(InstructionList &indices, int idx, Value instruction, AccessIndexed *ai) { roland@4860: int id = instruction->id(); roland@4860: AccessIndexedInfo *aii = _access_indexed_info[id]; roland@4860: if (aii == NULL) { roland@4860: aii = new AccessIndexedInfo(); roland@4860: _access_indexed_info[id] = aii; roland@4860: indices.append(instruction); roland@4860: aii->_min = idx; roland@4860: aii->_max = idx; roland@4860: aii->_list = new AccessIndexedList(); roland@4860: } else if (idx >= aii->_min && idx <= aii->_max) { roland@4860: remove_range_check(ai); roland@4860: return; roland@4860: } roland@4860: aii->_min = MIN2(aii->_min, idx); roland@4860: aii->_max = MAX2(aii->_max, idx); roland@4860: aii->_list->append(ai); roland@4860: } roland@4860: roland@4860: // In block motion. Tries to reorder checks in order to reduce some of them. roland@4860: // Example: roland@4860: // a[i] = 0; roland@4860: // a[i+2] = 0; roland@4860: // a[i+1] = 0; roland@4860: // In this example the check for a[i+1] would be considered as unnecessary during the first iteration. roland@4860: // After this i is only checked once for i >= 0 and i+2 < a.length before the first array access. If this roland@4860: // check fails, deoptimization is called. roland@4860: void RangeCheckEliminator::in_block_motion(BlockBegin *block, AccessIndexedList &accessIndexed, InstructionList &arrays) { roland@4860: InstructionList indices; roland@4860: roland@4860: // Now iterate over all arrays roland@4860: for (int i=0; iarray() != array || !ai->check_flag(Instruction::NeedsRangeCheckFlag)) continue; roland@4860: roland@4860: Value index = ai->index(); roland@4860: Constant *c = index->as_Constant(); roland@4860: if (c != NULL) { roland@4860: int constant_value = c->type()->as_IntConstant()->value(); roland@4860: if (constant_value >= 0) { roland@4860: if (constant_value <= max_constant) { roland@4860: // No range check needed for this roland@4860: remove_range_check(ai); roland@4860: } else { roland@4860: max_constant = constant_value; roland@4860: list_constant.append(ai); roland@4860: } roland@4860: } roland@4860: } else { roland@4860: int last_integer = 0; roland@4860: Instruction *last_instruction = index; roland@4860: int base = 0; roland@4860: ArithmeticOp *ao = index->as_ArithmeticOp(); roland@4860: roland@4860: while (ao != NULL && (ao->x()->as_Constant() || ao->y()->as_Constant()) && (ao->op() == Bytecodes::_iadd || ao->op() == Bytecodes::_isub)) { roland@4860: c = ao->y()->as_Constant(); roland@4860: Instruction *other = ao->x(); roland@4860: if (!c && ao->op() == Bytecodes::_iadd) { roland@4860: c = ao->x()->as_Constant(); roland@4860: other = ao->y(); roland@4860: } roland@4860: roland@4860: if (c) { roland@4860: int value = c->type()->as_IntConstant()->value(); roland@4860: if (value != min_jint) { roland@4860: if (ao->op() == Bytecodes::_isub) { roland@4860: value = -value; roland@4860: } roland@4860: base += value; roland@4860: last_integer = base; roland@4860: last_instruction = other; roland@4860: } roland@4860: index = other; roland@4860: } else { roland@4860: break; roland@4860: } roland@4860: ao = index->as_ArithmeticOp(); roland@4860: } roland@4860: add_access_indexed_info(indices, last_integer, last_instruction, ai); roland@4860: } roland@4860: } roland@4860: roland@4860: // Iterate over all different indices roland@4860: if (_optimistic) { roland@4972: for (int i = 0; i < indices.length(); i++) { roland@4860: Instruction *index_instruction = indices.at(i); roland@4860: AccessIndexedInfo *info = _access_indexed_info[index_instruction->id()]; roland@4860: assert(info != NULL, "Info must not be null"); roland@4860: roland@4860: // if idx < 0, max > 0, max + idx may fall between 0 and roland@4860: // length-1 and if min < 0, min + idx may overflow and be >= roland@4860: // 0. The predicate wouldn't trigger but some accesses could roland@4860: // be with a negative index. This test guarantees that for the roland@4860: // min and max value that are kept the predicate can't let roland@4860: // some incorrect accesses happen. roland@4860: bool range_cond = (info->_max < 0 || info->_max + min_jint <= info->_min); roland@4860: roland@4860: // Generate code only if more than 2 range checks can be eliminated because of that. roland@4860: // 2 because at least 2 comparisons are done roland@4860: if (info->_list->length() > 2 && range_cond) { roland@4860: AccessIndexed *first = info->_list->at(0); roland@4860: Instruction *insert_position = first->prev(); roland@4860: assert(insert_position->next() == first, "prev was calculated"); roland@4860: ValueStack *state = first->state_before(); roland@4860: roland@4860: // Load min Constant roland@4860: Constant *min_constant = NULL; roland@4860: if (info->_min != 0) { roland@4860: min_constant = new Constant(new IntConstant(info->_min)); roland@4860: NOT_PRODUCT(min_constant->set_printable_bci(first->printable_bci())); roland@4860: insert_position = insert_position->insert_after(min_constant); roland@4860: } roland@4860: roland@4860: // Load max Constant roland@4860: Constant *max_constant = NULL; roland@4860: if (info->_max != 0) { roland@4860: max_constant = new Constant(new IntConstant(info->_max)); roland@4860: NOT_PRODUCT(max_constant->set_printable_bci(first->printable_bci())); roland@4860: insert_position = insert_position->insert_after(max_constant); roland@4860: } roland@4860: roland@4860: // Load array length roland@4860: Value length_instr = first->length(); roland@4860: if (!length_instr) { roland@4860: ArrayLength *length = new ArrayLength(array, first->state_before()->copy()); roland@4860: length->set_exception_state(length->state_before()); roland@4860: length->set_flag(Instruction::DeoptimizeOnException, true); roland@4860: insert_position = insert_position->insert_after_same_bci(length); roland@4860: length_instr = length; roland@4860: } roland@4860: roland@4860: // Calculate lower bound roland@4860: Instruction *lower_compare = index_instruction; roland@4860: if (min_constant) { roland@4860: ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, min_constant, lower_compare, false, NULL); roland@4860: insert_position = insert_position->insert_after_same_bci(ao); roland@4860: lower_compare = ao; roland@4860: } roland@4860: roland@4860: // Calculate upper bound roland@4860: Instruction *upper_compare = index_instruction; roland@4860: if (max_constant) { roland@4860: ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, max_constant, upper_compare, false, NULL); roland@4860: insert_position = insert_position->insert_after_same_bci(ao); roland@4860: upper_compare = ao; roland@4860: } roland@4860: roland@4860: // Trick with unsigned compare is done roland@4860: int bci = NOT_PRODUCT(first->printable_bci()) PRODUCT_ONLY(-1); roland@4860: insert_position = predicate(upper_compare, Instruction::aeq, length_instr, state, insert_position, bci); roland@4860: insert_position = predicate_cmp_with_const(lower_compare, Instruction::leq, -1, state, insert_position); roland@4860: for (int j = 0; j_list->length(); j++) { roland@4860: AccessIndexed *ai = info->_list->at(j); roland@4860: remove_range_check(ai); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: if (list_constant.length() > 1) { roland@4860: AccessIndexed *first = list_constant.at(0); roland@4860: Instruction *insert_position = first->prev(); roland@4860: ValueStack *state = first->state_before(); roland@4860: // Load max Constant roland@4860: Constant *constant = new Constant(new IntConstant(max_constant)); roland@4860: NOT_PRODUCT(constant->set_printable_bci(first->printable_bci())); roland@4860: insert_position = insert_position->insert_after(constant); roland@4860: Instruction *compare_instr = constant; roland@4860: Value length_instr = first->length(); roland@4860: if (!length_instr) { roland@4860: ArrayLength *length = new ArrayLength(array, state->copy()); roland@4860: length->set_exception_state(length->state_before()); roland@4860: length->set_flag(Instruction::DeoptimizeOnException, true); roland@4860: insert_position = insert_position->insert_after_same_bci(length); roland@4860: length_instr = length; roland@4860: } roland@4860: // Compare for greater or equal to array length roland@4860: insert_position = predicate(compare_instr, Instruction::geq, length_instr, state, insert_position); roland@4860: for (int j = 0; jid()] = NULL; roland@4972: } roland@4972: indices.clear(); roland@4860: } roland@4860: } roland@4860: roland@4860: bool RangeCheckEliminator::set_process_block_flags(BlockBegin *block) { roland@4860: Instruction *cur = block; roland@4860: bool process = false; roland@4860: roland@4860: while (cur) { roland@4860: process |= (cur->as_AccessIndexed() != NULL); roland@4860: cur = cur->next(); roland@4860: } roland@4860: roland@4860: BlockList *dominates = block->dominates(); roland@4860: for (int i=0; ilength(); i++) { roland@4860: BlockBegin *next = dominates->at(i); roland@4860: process |= set_process_block_flags(next); roland@4860: } roland@4860: roland@4860: if (!process) { roland@4860: block->set(BlockBegin::donot_eliminate_range_checks); roland@4860: } roland@4860: return process; roland@4860: } roland@4860: roland@4860: 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) { roland@4860: bool upper_check = true; roland@4860: assert(lower_instr || lower >= 0, "If no lower_instr present, lower must be greater 0"); roland@4860: assert(!lower_instr || lower_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller"); roland@4860: assert(!upper_instr || upper_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller"); roland@4860: assert(array_instr, "Array instruction must exist"); roland@4860: assert(array_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller"); roland@4860: assert(!length_instr || length_instr->dominator_depth() <= insert_position->dominator_depth(), "Dominator depth must be smaller"); roland@4860: roland@4860: if (upper_instr && upper_instr->as_ArrayLength() && upper_instr->as_ArrayLength()->array() == array_instr) { roland@4860: // static check roland@4860: if (upper >= 0) return false; // would always trigger a deopt: roland@4860: // array_length + x >= array_length, x >= 0 is always true roland@4860: upper_check = false; roland@4860: } roland@4860: if (lower_instr && lower_instr->as_ArrayLength() && lower_instr->as_ArrayLength()->array() == array_instr) { roland@4860: if (lower > 0) return false; roland@4860: } roland@4860: // No upper check required -> skip roland@4860: if (upper_check && upper_instr && upper_instr->type()->as_ObjectType() && upper_instr == array_instr) { roland@4860: // upper_instr is object means that the upper bound is the length roland@4860: // of the upper_instr. roland@4860: return false; roland@4860: } roland@4860: return true; roland@4860: } roland@4860: roland@4860: Instruction* RangeCheckEliminator::insert_after(Instruction* insert_position, Instruction* instr, int bci) { roland@4860: if (bci != -1) { roland@4860: NOT_PRODUCT(instr->set_printable_bci(bci)); roland@4860: return insert_position->insert_after(instr); roland@4860: } else { roland@4860: return insert_position->insert_after_same_bci(instr); roland@4860: } roland@4860: } roland@4860: roland@4860: Instruction* RangeCheckEliminator::predicate(Instruction* left, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci) { roland@4860: RangeCheckPredicate *deoptimize = new RangeCheckPredicate(left, cond, true, right, state->copy()); roland@4860: return insert_after(insert_position, deoptimize, bci); roland@4860: } roland@4860: roland@4860: Instruction* RangeCheckEliminator::predicate_cmp_with_const(Instruction* instr, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci) { roland@4860: Constant *const_instr = new Constant(new IntConstant(constant)); roland@4860: insert_position = insert_after(insert_position, const_instr, bci); roland@4860: return predicate(instr, cond, const_instr, state, insert_position); roland@4860: } roland@4860: roland@4860: Instruction* RangeCheckEliminator::predicate_add(Instruction* left, int left_const, Instruction::Condition cond, Instruction* right, ValueStack* state, Instruction *insert_position, int bci) { roland@4860: Constant *constant = new Constant(new IntConstant(left_const)); roland@4860: insert_position = insert_after(insert_position, constant, bci); roland@4860: ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, constant, left, false, NULL); roland@4860: insert_position = insert_position->insert_after_same_bci(ao); roland@4860: return predicate(ao, cond, right, state, insert_position); roland@4860: } roland@4860: roland@4860: Instruction* RangeCheckEliminator::predicate_add_cmp_with_const(Instruction* left, int left_const, Instruction::Condition cond, int constant, ValueStack* state, Instruction *insert_position, int bci) { roland@4860: Constant *const_instr = new Constant(new IntConstant(constant)); roland@4860: insert_position = insert_after(insert_position, const_instr, bci); roland@4860: return predicate_add(left, left_const, cond, const_instr, state, insert_position); roland@4860: } roland@4860: roland@4869: // Insert deoptimization roland@4860: 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) { roland@4860: assert(is_ok_for_deoptimization(insert_position, array_instr, length_instr, lower_instr, lower, upper_instr, upper), "should have been tested before"); roland@4860: bool upper_check = !(upper_instr && upper_instr->as_ArrayLength() && upper_instr->as_ArrayLength()->array() == array_instr); roland@4860: roland@4860: int bci = NOT_PRODUCT(ai->printable_bci()) PRODUCT_ONLY(-1); roland@4860: if (lower_instr) { roland@4860: assert(!lower_instr->type()->as_ObjectType(), "Must not be object type"); roland@4860: if (lower == 0) { roland@4860: // Compare for less than 0 roland@4860: insert_position = predicate_cmp_with_const(lower_instr, Instruction::lss, 0, state, insert_position, bci); roland@4860: } else if (lower > 0) { roland@4860: // Compare for smaller 0 roland@4860: insert_position = predicate_add_cmp_with_const(lower_instr, lower, Instruction::lss, 0, state, insert_position, bci); roland@4860: } else { roland@4860: assert(lower < 0, ""); roland@4860: // Add 1 roland@4860: lower++; roland@4860: lower = -lower; roland@4860: // Compare for smaller or equal 0 roland@4860: insert_position = predicate_cmp_with_const(lower_instr, Instruction::leq, lower, state, insert_position, bci); roland@4860: } roland@4860: } roland@4860: roland@4869: // No upper check required -> skip roland@4869: if (!upper_check) return; roland@4869: roland@4860: // We need to know length of array roland@4860: if (!length_instr) { roland@4860: // Load length if necessary roland@4860: ArrayLength *length = new ArrayLength(array_instr, state->copy()); roland@4860: NOT_PRODUCT(length->set_printable_bci(ai->printable_bci())); roland@4860: length->set_exception_state(length->state_before()); roland@4860: length->set_flag(Instruction::DeoptimizeOnException, true); roland@4860: insert_position = insert_position->insert_after(length); roland@4860: length_instr = length; roland@4860: } roland@4860: roland@4860: if (!upper_instr) { roland@4860: // Compare for geq array.length roland@4860: insert_position = predicate_cmp_with_const(length_instr, Instruction::leq, upper, state, insert_position, bci); roland@4860: } else { roland@4860: if (upper_instr->type()->as_ObjectType()) { roland@4860: assert(state, "must not be null"); roland@4860: assert(upper_instr != array_instr, "should be"); roland@4860: ArrayLength *length = new ArrayLength(upper_instr, state->copy()); roland@4860: NOT_PRODUCT(length->set_printable_bci(ai->printable_bci())); roland@4860: length->set_flag(Instruction::DeoptimizeOnException, true); roland@4860: length->set_exception_state(length->state_before()); roland@4860: insert_position = insert_position->insert_after(length); roland@4860: upper_instr = length; roland@4860: } roland@4860: assert(upper_instr->type()->as_IntType(), "Must not be object type!"); roland@4860: roland@4860: if (upper == 0) { roland@4860: // Compare for geq array.length roland@4860: insert_position = predicate(upper_instr, Instruction::geq, length_instr, state, insert_position, bci); roland@4860: } else if (upper < 0) { roland@4860: // Compare for geq array.length roland@4860: insert_position = predicate_add(upper_instr, upper, Instruction::geq, length_instr, state, insert_position, bci); roland@4860: } else { roland@4860: assert(upper > 0, ""); roland@4860: upper = -upper; roland@4860: // Compare for geq array.length roland@4860: insert_position = predicate_add(length_instr, upper, Instruction::leq, upper_instr, state, insert_position, bci); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // Add if condition roland@4860: void RangeCheckEliminator::add_if_condition(IntegerStack &pushed, Value x, Value y, Instruction::Condition condition) { roland@4860: if (y->as_Constant()) return; roland@4860: roland@4860: int const_value = 0; roland@4860: Value instr_value = x; roland@4860: Constant *c = x->as_Constant(); roland@4860: ArithmeticOp *ao = x->as_ArithmeticOp(); roland@4860: roland@4860: if (c != NULL) { roland@4860: const_value = c->type()->as_IntConstant()->value(); roland@4860: instr_value = NULL; roland@4860: } else if (ao != NULL && (!ao->x()->as_Constant() || !ao->y()->as_Constant()) && ((ao->op() == Bytecodes::_isub && ao->y()->as_Constant()) || ao->op() == Bytecodes::_iadd)) { roland@4860: assert(!ao->x()->as_Constant() || !ao->y()->as_Constant(), "At least one operator must be non-constant!"); roland@4860: assert(ao->op() == Bytecodes::_isub || ao->op() == Bytecodes::_iadd, "Operation has to be add or sub!"); roland@4860: c = ao->x()->as_Constant(); roland@4860: if (c != NULL) { roland@4860: const_value = c->type()->as_IntConstant()->value(); roland@4860: instr_value = ao->y(); roland@4860: } else { roland@4860: c = ao->y()->as_Constant(); roland@4860: if (c != NULL) { roland@4860: const_value = c->type()->as_IntConstant()->value(); roland@4860: instr_value = ao->x(); roland@4860: } roland@4860: } roland@4860: if (ao->op() == Bytecodes::_isub) { roland@4860: assert(ao->y()->as_Constant(), "1 - x not supported, only x - 1 is valid!"); roland@4860: if (const_value > min_jint) { roland@4860: const_value = -const_value; roland@4860: } else { roland@4860: const_value = 0; roland@4860: instr_value = x; roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: update_bound(pushed, y, condition, instr_value, const_value); roland@4860: } roland@4860: roland@4860: // Process If roland@4860: void RangeCheckEliminator::process_if(IntegerStack &pushed, BlockBegin *block, If *cond) { roland@4860: // Only if we are direct true / false successor and NOT both ! (even this may occur) roland@4860: if ((cond->tsux() == block || cond->fsux() == block) && cond->tsux() != cond->fsux()) { roland@4860: Instruction::Condition condition = cond->cond(); roland@4860: if (cond->fsux() == block) { roland@4860: condition = Instruction::negate(condition); roland@4860: } roland@4860: Value x = cond->x(); roland@4860: Value y = cond->y(); roland@4860: if (x->type()->as_IntType() && y->type()->as_IntType()) { roland@4860: add_if_condition(pushed, y, x, condition); roland@4860: add_if_condition(pushed, x, y, Instruction::mirror(condition)); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // Process access indexed roland@4860: void RangeCheckEliminator::process_access_indexed(BlockBegin *loop_header, BlockBegin *block, AccessIndexed *ai) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2) roland@4860: ); roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4869: tty->print_cr("Access indexed: index=%d length=%d", ai->index()->id(), (ai->length() != NULL ? ai->length()->id() :-1 )) roland@4860: ); roland@4860: roland@4860: if (ai->check_flag(Instruction::NeedsRangeCheckFlag)) { roland@4860: Bound *index_bound = get_bound(ai->index()); roland@4860: if (!index_bound->has_lower() || !index_bound->has_upper()) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Index instruction %d has no lower and/or no upper bound!", ai->index()->id()) roland@4860: ); roland@4860: return; roland@4860: } roland@4860: roland@4860: Bound *array_bound; roland@4860: if (ai->length()) { roland@4860: array_bound = get_bound(ai->length()); roland@4860: } else { roland@4860: array_bound = get_bound(ai->array()); roland@4860: } roland@4860: roland@4860: if (in_array_bound(index_bound, ai->array()) || roland@4860: (index_bound && array_bound && index_bound->is_smaller(array_bound) && !index_bound->lower_instr() && index_bound->lower() >= 0)) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Bounds check for instruction %d in block B%d can be fully eliminated!", ai->id(), ai->block()->block_id()) roland@4860: ); roland@4860: roland@4860: remove_range_check(ai); roland@4860: } else if (_optimistic && loop_header) { roland@4860: assert(ai->array(), "Array must not be null!"); roland@4860: assert(ai->index(), "Index must not be null!"); roland@4860: roland@4860: // Array instruction roland@4860: Instruction *array_instr = ai->array(); roland@4860: if (!loop_invariant(loop_header, array_instr)) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Array %d is not loop invariant to header B%d", ai->array()->id(), loop_header->block_id()) roland@4860: ); roland@4860: return; roland@4860: } roland@4860: roland@4860: // Lower instruction roland@4860: Value index_instr = ai->index(); roland@4860: Value lower_instr = index_bound->lower_instr(); roland@4860: if (!loop_invariant(loop_header, lower_instr)) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Lower instruction %d not loop invariant!", lower_instr->id()) roland@4860: ); roland@4860: return; roland@4860: } roland@4860: if (!lower_instr && index_bound->lower() < 0) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Lower bound smaller than 0 (%d)!", index_bound->lower()) roland@4860: ); roland@4860: return; roland@4860: } roland@4860: roland@4860: // Upper instruction roland@4860: Value upper_instr = index_bound->upper_instr(); roland@4860: if (!loop_invariant(loop_header, upper_instr)) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Upper instruction %d not loop invariant!", upper_instr->id()) roland@4860: ); roland@4860: return; roland@4860: } roland@4860: roland@4860: // Length instruction roland@4860: Value length_instr = ai->length(); roland@4860: if (!loop_invariant(loop_header, length_instr)) { roland@4860: // Generate length instruction yourself! roland@4860: length_instr = NULL; roland@4860: } roland@4860: roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("LOOP INVARIANT access indexed %d found in block B%d!", ai->id(), ai->block()->block_id()) roland@4860: ); roland@4860: roland@4860: BlockBegin *pred_block = loop_header->dominator(); roland@4860: assert(pred_block != NULL, "Every loop header has a dominator!"); roland@4860: BlockEnd *pred_block_end = pred_block->end(); roland@4860: Instruction *insert_position = pred_block_end->prev(); roland@4860: ValueStack *state = pred_block_end->state_before(); roland@4860: if (pred_block_end->as_Goto() && state == NULL) state = pred_block_end->state(); roland@4860: assert(state, "State must not be null"); roland@4860: roland@4860: // Add deoptimization to dominator of loop header roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Inserting deopt at bci %d in block B%d!", state->bci(), insert_position->block()->block_id()) roland@4860: ); roland@4860: roland@4860: if (!is_ok_for_deoptimization(insert_position, array_instr, length_instr, lower_instr, index_bound->lower(), upper_instr, index_bound->upper())) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Could not eliminate because of static analysis!") roland@4860: ); roland@4860: return; roland@4860: } roland@4860: roland@4860: insert_deoptimization(state, insert_position, array_instr, length_instr, lower_instr, index_bound->lower(), upper_instr, index_bound->upper(), ai); roland@4860: roland@4860: // Finally remove the range check! roland@4860: remove_range_check(ai); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: void RangeCheckEliminator::remove_range_check(AccessIndexed *ai) { roland@4860: ai->set_flag(Instruction::NeedsRangeCheckFlag, false); roland@4860: // no range check, no need for the length instruction anymore roland@4860: ai->clear_length(); roland@4860: roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(ai->dominator_depth()*2); roland@4860: tty->print_cr("Range check for instruction %d eliminated!", ai->id()); roland@4860: ); roland@4860: roland@4860: ASSERT_RANGE_CHECK_ELIMINATION( roland@4860: Value array_length = ai->length(); roland@4860: if (!array_length) { roland@4860: array_length = ai->array(); roland@4860: assert(array_length->type()->as_ObjectType(), "Has to be object type!"); roland@4860: } roland@4860: int cur_constant = -1; roland@4860: Value cur_value = array_length; roland@4860: if (cur_value->type()->as_IntConstant()) { roland@4860: cur_constant += cur_value->type()->as_IntConstant()->value(); roland@4860: cur_value = NULL; roland@4860: } roland@4860: Bound *new_index_bound = new Bound(0, NULL, cur_constant, cur_value); roland@4860: add_assertions(new_index_bound, ai->index(), ai); roland@4860: ); roland@4860: } roland@4860: roland@4860: // Calculate bounds for instruction in this block and children blocks in the dominator tree roland@4860: void RangeCheckEliminator::calc_bounds(BlockBegin *block, BlockBegin *loop_header) { roland@4860: // Ensures a valid loop_header roland@4860: assert(!loop_header || loop_header->is_set(BlockBegin::linear_scan_loop_header_flag), "Loop header has to be real !"); roland@4860: roland@4860: // Tracing output roland@4860: TRACE_RANGE_CHECK_ELIMINATION( roland@4860: tty->fill_to(block->dominator_depth()*2); roland@4860: tty->print_cr("Block B%d", block->block_id()); roland@4860: ); roland@4860: roland@4860: // Pushed stack for conditions roland@4860: IntegerStack pushed; roland@4860: // Process If roland@4860: BlockBegin *parent = block->dominator(); roland@4860: if (parent != NULL) { roland@4860: If *cond = parent->end()->as_If(); roland@4860: if (cond != NULL) { roland@4860: process_if(pushed, block, cond); roland@4860: } roland@4860: } roland@4860: roland@4860: // Interate over current block roland@4860: InstructionList arrays; roland@4860: AccessIndexedList accessIndexed; roland@4860: Instruction *cur = block; roland@4860: roland@4860: while (cur) { roland@4860: // Ensure cur wasn't inserted during the elimination roland@4860: if (cur->id() < this->_bounds.length()) { roland@4860: // Process only if it is an access indexed instruction roland@4860: AccessIndexed *ai = cur->as_AccessIndexed(); roland@4860: if (ai != NULL) { roland@4860: process_access_indexed(loop_header, block, ai); roland@4860: accessIndexed.append(ai); roland@4860: if (!arrays.contains(ai->array())) { roland@4860: arrays.append(ai->array()); roland@4860: } roland@4860: Bound *b = get_bound(ai->index()); roland@4860: if (!b->lower_instr()) { roland@4860: // Lower bound is constant roland@4860: update_bound(pushed, ai->index(), Instruction::geq, NULL, 0); roland@4860: } roland@4860: if (!b->has_upper()) { roland@4860: if (ai->length() && ai->length()->type()->as_IntConstant()) { roland@4860: int value = ai->length()->type()->as_IntConstant()->value(); roland@4860: update_bound(pushed, ai->index(), Instruction::lss, NULL, value); roland@4860: } else { roland@4860: // Has no upper bound roland@4860: Instruction *instr = ai->length(); roland@4860: if (instr != NULL) instr = ai->array(); roland@4860: update_bound(pushed, ai->index(), Instruction::lss, instr, 0); roland@4860: } roland@4860: } roland@4860: } roland@4860: } roland@4860: cur = cur->next(); roland@4860: } roland@4860: roland@4860: // Output current condition stack roland@4860: TRACE_RANGE_CHECK_ELIMINATION(dump_condition_stack(block)); roland@4860: roland@4860: // Do in block motion of range checks roland@4860: in_block_motion(block, accessIndexed, arrays); roland@4860: roland@4860: // Call all dominated blocks roland@4860: for (int i=0; idominates()->length(); i++) { roland@4860: BlockBegin *next = block->dominates()->at(i); roland@4860: if (!next->is_set(BlockBegin::donot_eliminate_range_checks)) { roland@4860: // if current block is a loop header and: roland@4860: // - next block belongs to the same loop roland@4860: // or roland@4860: // - next block belongs to an inner loop roland@4860: // then current block is the loop header for next block roland@4860: if (block->is_set(BlockBegin::linear_scan_loop_header_flag) && (block->loop_index() == next->loop_index() || next->loop_depth() > block->loop_depth())) { roland@4860: calc_bounds(next, block); roland@4860: } else { roland@4860: calc_bounds(next, loop_header); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // Reset stack roland@4860: for (int i=0; ipop(); roland@4860: } roland@4860: } roland@4860: roland@4860: #ifndef PRODUCT roland@4860: // Dump condition stack roland@4860: void RangeCheckEliminator::dump_condition_stack(BlockBegin *block) { roland@4860: for (int i=0; i<_ir->linear_scan_order()->length(); i++) { roland@4860: BlockBegin *cur_block = _ir->linear_scan_order()->at(i); roland@4860: Instruction *instr = cur_block; roland@4860: for_each_phi_fun(cur_block, phi, roland@4860: BoundStack *bound_stack = _bounds.at(phi->id()); roland@4860: if (bound_stack && bound_stack->length() > 0) { roland@4860: Bound *bound = bound_stack->top(); roland@4860: if ((bound->has_lower() || bound->has_upper()) && (bound->lower_instr() != phi || bound->upper_instr() != phi || bound->lower() != 0 || bound->upper() != 0)) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION(tty->fill_to(2*block->dominator_depth()); roland@4860: tty->print("i%d", phi->id()); roland@4860: tty->print(": "); roland@4860: bound->print(); drchase@6680: tty->cr(); roland@4860: ); roland@4860: } roland@4860: }); roland@4860: roland@4860: while (!instr->as_BlockEnd()) { roland@4860: if (instr->id() < _bounds.length()) { roland@4860: BoundStack *bound_stack = _bounds.at(instr->id()); roland@4860: if (bound_stack && bound_stack->length() > 0) { roland@4860: Bound *bound = bound_stack->top(); roland@4860: if ((bound->has_lower() || bound->has_upper()) && (bound->lower_instr() != instr || bound->upper_instr() != instr || bound->lower() != 0 || bound->upper() != 0)) { roland@4860: TRACE_RANGE_CHECK_ELIMINATION(tty->fill_to(2*block->dominator_depth()); roland@4860: tty->print("i%d", instr->id()); roland@4860: tty->print(": "); roland@4860: bound->print(); drchase@6680: tty->cr(); roland@4860: ); roland@4860: } roland@4860: } roland@4860: } roland@4860: instr = instr->next(); roland@4860: } roland@4860: } roland@4860: } roland@4860: #endif roland@4860: roland@4860: // Verification or the IR roland@4860: RangeCheckEliminator::Verification::Verification(IR *ir) : _used(BlockBegin::number_of_blocks(), false) { roland@4860: this->_ir = ir; roland@4860: ir->iterate_linear_scan_order(this); roland@4860: } roland@4860: roland@4860: // Verify this block roland@4860: void RangeCheckEliminator::Verification::block_do(BlockBegin *block) { roland@4860: If *cond = block->end()->as_If(); roland@4860: // Watch out: tsux and fsux can be the same! roland@4860: if (block->number_of_sux() > 1) { roland@4860: for (int i=0; inumber_of_sux(); i++) { roland@4860: BlockBegin *sux = block->sux_at(i); roland@4860: BlockBegin *pred = NULL; roland@4860: for (int j=0; jnumber_of_preds(); j++) { roland@4860: BlockBegin *cur = sux->pred_at(j); roland@4860: assert(cur != NULL, "Predecessor must not be null"); roland@4860: if (!pred) { roland@4860: pred = cur; roland@4860: } roland@4860: assert(cur == pred, "Block must not have more than one predecessor if its predecessor has more than one successor"); roland@4860: } roland@4860: assert(sux->number_of_preds() >= 1, "Block must have at least one predecessor"); roland@4860: assert(sux->pred_at(0) == block, "Wrong successor"); roland@4860: } roland@4860: } roland@4860: roland@4860: BlockBegin *dominator = block->dominator(); roland@4860: if (dominator) { roland@4860: assert(block != _ir->start(), "Start block must not have a dominator!"); roland@4860: assert(can_reach(dominator, block), "Dominator can't reach his block !"); roland@4860: assert(can_reach(_ir->start(), dominator), "Dominator is unreachable !"); roland@4860: assert(!can_reach(_ir->start(), block, dominator), "Wrong dominator ! Block can be reached anyway !"); roland@4860: BlockList *all_blocks = _ir->linear_scan_order(); roland@4860: for (int i=0; ilength(); i++) { roland@4860: BlockBegin *cur = all_blocks->at(i); roland@4860: if (cur != dominator && cur != block) { roland@4860: assert(can_reach(dominator, block, cur), "There has to be another dominator!"); roland@4860: } roland@4860: } roland@4860: } else { roland@4860: assert(block == _ir->start(), "Only start block must not have a dominator"); roland@4860: } roland@4860: roland@4860: if (block->is_set(BlockBegin::linear_scan_loop_header_flag)) { roland@4860: int loop_index = block->loop_index(); roland@4860: BlockList *all_blocks = _ir->linear_scan_order(); roland@4860: assert(block->number_of_preds() >= 1, "Block must have at least one predecessor"); roland@4860: assert(!block->is_set(BlockBegin::exception_entry_flag), "Loop header must not be exception handler!"); roland@4860: // Sometimes, the backbranch comes from an exception handler. In roland@4860: // this case, loop indexes/loop depths may not appear correct. roland@4860: bool loop_through_xhandler = false; roland@4860: for (int i = 0; i < block->number_of_exception_handlers(); i++) { roland@4860: BlockBegin *xhandler = block->exception_handler_at(i); roland@4860: for (int j = 0; j < block->number_of_preds(); j++) { roland@4860: if (dominates(xhandler, block->pred_at(j)) || xhandler == block->pred_at(j)) { roland@4860: loop_through_xhandler = true; roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: for (int i=0; inumber_of_sux(); i++) { roland@4860: BlockBegin *sux = block->sux_at(i); roland@4860: assert(sux->loop_depth() != block->loop_depth() || sux->loop_index() == block->loop_index() || loop_through_xhandler, "Loop index has to be same"); roland@4860: assert(sux->loop_depth() == block->loop_depth() || sux->loop_index() != block->loop_index(), "Loop index has to be different"); roland@4860: } roland@4860: roland@4860: for (int i=0; ilength(); i++) { roland@4860: BlockBegin *cur = all_blocks->at(i); roland@4860: if (cur->loop_index() == loop_index && cur != block) { roland@4860: assert(dominates(block->dominator(), cur), "Dominator of loop header must dominate all loop blocks"); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: Instruction *cur = block; roland@4860: while (cur) { roland@4860: assert(cur->block() == block, "Block begin has to be set correctly!"); roland@4860: cur = cur->next(); roland@4860: } roland@4860: } roland@4860: roland@4860: // Loop header must dominate all loop blocks roland@4860: bool RangeCheckEliminator::Verification::dominates(BlockBegin *dominator, BlockBegin *block) { roland@4860: BlockBegin *cur = block->dominator(); roland@4860: while (cur && cur != dominator) { roland@4860: cur = cur->dominator(); roland@4860: } roland@4860: return cur == dominator; roland@4860: } roland@4860: roland@4860: // Try to reach Block end beginning in Block start and not using Block dont_use roland@4860: bool RangeCheckEliminator::Verification::can_reach(BlockBegin *start, BlockBegin *end, BlockBegin *dont_use /* = NULL */) { roland@4860: if (start == end) return start != dont_use; roland@4860: // Simple BSF from start to end roland@4860: // BlockBeginList _current; roland@4860: for (int i=0; i<_used.length(); i++) { roland@4860: _used[i] = false; roland@4860: } roland@4860: _current.truncate(0); roland@4860: _successors.truncate(0); roland@4860: if (start != dont_use) { roland@4860: _current.push(start); roland@4860: _used[start->block_id()] = true; roland@4860: } roland@4860: roland@4860: // BlockBeginList _successors; roland@4860: while (_current.length() > 0) { roland@4860: BlockBegin *cur = _current.pop(); roland@4860: // Add exception handlers to list roland@4860: for (int i=0; inumber_of_exception_handlers(); i++) { roland@4860: BlockBegin *xhandler = cur->exception_handler_at(i); roland@4860: _successors.push(xhandler); roland@4860: // Add exception handlers of _successors to list roland@4860: for (int j=0; jnumber_of_exception_handlers(); j++) { roland@4860: BlockBegin *sux_xhandler = xhandler->exception_handler_at(j); roland@4860: _successors.push(sux_xhandler); roland@4860: } roland@4860: } roland@4860: // Add normal _successors to list roland@4860: for (int i=0; inumber_of_sux(); i++) { roland@4860: BlockBegin *sux = cur->sux_at(i); roland@4860: _successors.push(sux); roland@4860: // Add exception handlers of _successors to list roland@4860: for (int j=0; jnumber_of_exception_handlers(); j++) { roland@4860: BlockBegin *xhandler = sux->exception_handler_at(j); roland@4860: _successors.push(xhandler); roland@4860: } roland@4860: } roland@4860: for (int i=0; i<_successors.length(); i++) { roland@4860: BlockBegin *sux = _successors[i]; roland@4860: assert(sux != NULL, "Successor must not be NULL!"); roland@4860: if (sux == end) { roland@4860: return true; roland@4860: } roland@4860: if (sux != dont_use && !_used[sux->block_id()]) { roland@4860: _used[sux->block_id()] = true; roland@4860: _current.push(sux); roland@4860: } roland@4860: } roland@4860: _successors.truncate(0); roland@4860: } roland@4860: roland@4860: return false; roland@4860: } roland@4860: roland@4860: // Bound roland@4860: RangeCheckEliminator::Bound::~Bound() { roland@4860: } roland@4860: roland@4860: // Bound constructor roland@4860: RangeCheckEliminator::Bound::Bound() { roland@4860: init(); roland@4860: this->_lower = min_jint; roland@4860: this->_upper = max_jint; roland@4860: this->_lower_instr = NULL; roland@4860: this->_upper_instr = NULL; roland@4860: } roland@4860: roland@4860: // Bound constructor roland@4860: RangeCheckEliminator::Bound::Bound(int lower, Value lower_instr, int upper, Value upper_instr) { roland@4860: init(); roland@4860: assert(!lower_instr || !lower_instr->as_Constant() || !lower_instr->type()->as_IntConstant(), "Must not be constant!"); roland@4860: assert(!upper_instr || !upper_instr->as_Constant() || !upper_instr->type()->as_IntConstant(), "Must not be constant!"); roland@4860: this->_lower = lower; roland@4860: this->_upper = upper; roland@4860: this->_lower_instr = lower_instr; roland@4860: this->_upper_instr = upper_instr; roland@4860: } roland@4860: roland@4860: // Bound constructor roland@4860: RangeCheckEliminator::Bound::Bound(Instruction::Condition cond, Value v, int constant) { roland@4860: assert(!v || (v->type() && (v->type()->as_IntType() || v->type()->as_ObjectType())), "Type must be array or integer!"); roland@4860: assert(!v || !v->as_Constant() || !v->type()->as_IntConstant(), "Must not be constant!"); roland@4860: roland@4860: init(); roland@4860: if (cond == Instruction::eql) { roland@4860: _lower = constant; roland@4860: _lower_instr = v; roland@4860: _upper = constant; roland@4860: _upper_instr = v; roland@4860: } else if (cond == Instruction::neq) { roland@4860: _lower = min_jint; roland@4860: _upper = max_jint; roland@4860: _lower_instr = NULL; roland@4860: _upper_instr = NULL; roland@4860: if (v == NULL) { roland@4860: if (constant == min_jint) { roland@4860: _lower++; roland@4860: } roland@4860: if (constant == max_jint) { roland@4860: _upper--; roland@4860: } roland@4860: } roland@4860: } else if (cond == Instruction::geq) { roland@4860: _lower = constant; roland@4860: _lower_instr = v; roland@4860: _upper = max_jint; roland@4860: _upper_instr = NULL; roland@4860: } else if (cond == Instruction::leq) { roland@4860: _lower = min_jint; roland@4860: _lower_instr = NULL; roland@4860: _upper = constant; roland@4860: _upper_instr = v; roland@4860: } else { roland@4860: ShouldNotReachHere(); roland@4860: } roland@4860: } roland@4860: roland@4860: // Set lower roland@4860: void RangeCheckEliminator::Bound::set_lower(int value, Value v) { roland@4860: assert(!v || !v->as_Constant() || !v->type()->as_IntConstant(), "Must not be constant!"); roland@4860: this->_lower = value; roland@4860: this->_lower_instr = v; roland@4860: } roland@4860: roland@4860: // Set upper roland@4860: void RangeCheckEliminator::Bound::set_upper(int value, Value v) { roland@4860: assert(!v || !v->as_Constant() || !v->type()->as_IntConstant(), "Must not be constant!"); roland@4860: this->_upper = value; roland@4860: this->_upper_instr = v; roland@4860: } roland@4860: roland@4860: // Add constant -> no overflow may occur roland@4860: void RangeCheckEliminator::Bound::add_constant(int value) { roland@4860: this->_lower += value; roland@4860: this->_upper += value; roland@4860: } roland@4860: roland@4860: // Init roland@4860: void RangeCheckEliminator::Bound::init() { roland@4860: } roland@4860: roland@4860: // or roland@4860: void RangeCheckEliminator::Bound::or_op(Bound *b) { roland@4860: // Watch out, bound is not guaranteed not to overflow! roland@4860: // Update lower bound roland@4860: if (_lower_instr != b->_lower_instr || (_lower_instr && _lower != b->_lower)) { roland@4860: _lower_instr = NULL; roland@4860: _lower = min_jint; roland@4860: } else { roland@4860: _lower = MIN2(_lower, b->_lower); roland@4860: } roland@4860: // Update upper bound roland@4860: if (_upper_instr != b->_upper_instr || (_upper_instr && _upper != b->_upper)) { roland@4860: _upper_instr = NULL; roland@4860: _upper = max_jint; roland@4860: } else { roland@4860: _upper = MAX2(_upper, b->_upper); roland@4860: } roland@4860: } roland@4860: roland@4860: // and roland@4860: void RangeCheckEliminator::Bound::and_op(Bound *b) { roland@4860: // Update lower bound roland@4860: if (_lower_instr == b->_lower_instr) { roland@4860: _lower = MAX2(_lower, b->_lower); roland@4860: } roland@4860: if (b->has_lower()) { roland@4860: bool set = true; roland@4860: if (_lower_instr != NULL && b->_lower_instr != NULL) { roland@4860: set = (_lower_instr->dominator_depth() > b->_lower_instr->dominator_depth()); roland@4860: } roland@4860: if (set) { roland@4860: _lower = b->_lower; roland@4860: _lower_instr = b->_lower_instr; roland@4860: } roland@4860: } roland@4860: // Update upper bound roland@4860: if (_upper_instr == b->_upper_instr) { roland@4860: _upper = MIN2(_upper, b->_upper); roland@4860: } roland@4860: if (b->has_upper()) { roland@4860: bool set = true; roland@4860: if (_upper_instr != NULL && b->_upper_instr != NULL) { roland@4860: set = (_upper_instr->dominator_depth() > b->_upper_instr->dominator_depth()); roland@4860: } roland@4860: if (set) { roland@4860: _upper = b->_upper; roland@4860: _upper_instr = b->_upper_instr; roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // has_upper roland@4860: bool RangeCheckEliminator::Bound::has_upper() { roland@4860: return _upper_instr != NULL || _upper < max_jint; roland@4860: } roland@4860: roland@4860: // is_smaller roland@4860: bool RangeCheckEliminator::Bound::is_smaller(Bound *b) { roland@4860: if (b->_lower_instr != _upper_instr) { roland@4860: return false; roland@4860: } roland@4860: return _upper < b->_lower; roland@4860: } roland@4860: roland@4860: // has_lower roland@4860: bool RangeCheckEliminator::Bound::has_lower() { roland@4860: return _lower_instr != NULL || _lower > min_jint; roland@4860: } roland@4860: roland@4860: // in_array_bound roland@4860: bool RangeCheckEliminator::in_array_bound(Bound *bound, Value array){ roland@4860: if (!bound) return false; roland@4860: assert(array != NULL, "Must not be null!"); roland@4860: assert(bound != NULL, "Must not be null!"); roland@4860: if (bound->lower() >=0 && bound->lower_instr() == NULL && bound->upper() < 0 && bound->upper_instr() != NULL) { roland@4860: ArrayLength *len = bound->upper_instr()->as_ArrayLength(); roland@4860: if (bound->upper_instr() == array || (len != NULL && len->array() == array)) { roland@4860: return true; roland@4860: } roland@4860: } roland@4860: return false; roland@4860: } roland@4860: roland@4860: // remove_lower roland@4860: void RangeCheckEliminator::Bound::remove_lower() { roland@4860: _lower = min_jint; roland@4860: _lower_instr = NULL; roland@4860: } roland@4860: roland@4860: // remove_upper roland@4860: void RangeCheckEliminator::Bound::remove_upper() { roland@4860: _upper = max_jint; roland@4860: _upper_instr = NULL; roland@4860: } roland@4860: roland@4860: // upper roland@4860: int RangeCheckEliminator::Bound::upper() { roland@4860: return _upper; roland@4860: } roland@4860: roland@4860: // lower roland@4860: int RangeCheckEliminator::Bound::lower() { roland@4860: return _lower; roland@4860: } roland@4860: roland@4860: // upper_instr roland@4860: Value RangeCheckEliminator::Bound::upper_instr() { roland@4860: return _upper_instr; roland@4860: } roland@4860: roland@4860: // lower_instr roland@4860: Value RangeCheckEliminator::Bound::lower_instr() { roland@4860: return _lower_instr; roland@4860: } roland@4860: roland@4860: // print roland@4860: void RangeCheckEliminator::Bound::print() { drchase@6680: tty->print("%s", ""); roland@4860: if (this->_lower_instr || this->_lower != min_jint) { roland@4860: if (this->_lower_instr) { roland@4860: tty->print("i%d", this->_lower_instr->id()); roland@4860: if (this->_lower > 0) { roland@4860: tty->print("+%d", _lower); roland@4860: } roland@4860: if (this->_lower < 0) { roland@4860: tty->print("%d", _lower); roland@4860: } roland@4860: } else { roland@4860: tty->print("%d", _lower); roland@4860: } roland@4860: tty->print(" <= "); roland@4860: } roland@4860: tty->print("x"); roland@4860: if (this->_upper_instr || this->_upper != max_jint) { roland@4860: tty->print(" <= "); roland@4860: if (this->_upper_instr) { roland@4860: tty->print("i%d", this->_upper_instr->id()); roland@4860: if (this->_upper > 0) { roland@4860: tty->print("+%d", _upper); roland@4860: } roland@4860: if (this->_upper < 0) { roland@4860: tty->print("%d", _upper); roland@4860: } roland@4860: } else { roland@4860: tty->print("%d", _upper); roland@4860: } roland@4860: } roland@4860: } roland@4860: roland@4860: // Copy roland@4860: RangeCheckEliminator::Bound *RangeCheckEliminator::Bound::copy() { roland@4860: Bound *b = new Bound(); roland@4860: b->_lower = _lower; roland@4860: b->_lower_instr = _lower_instr; roland@4860: b->_upper = _upper; roland@4860: b->_upper_instr = _upper_instr; roland@4860: return b; roland@4860: } roland@4860: roland@4860: #ifdef ASSERT roland@4860: // Add assertion roland@4860: void RangeCheckEliminator::Bound::add_assertion(Instruction *instruction, Instruction *position, int i, Value instr, Instruction::Condition cond) { roland@4860: Instruction *result = position; roland@4860: Instruction *compare_with = NULL; roland@4860: ValueStack *state = position->state_before(); roland@4860: if (position->as_BlockEnd() && !position->as_Goto()) { roland@4860: state = position->as_BlockEnd()->state_before(); roland@4860: } roland@4860: Instruction *instruction_before = position->prev(); roland@4860: if (position->as_Return() && Compilation::current()->method()->is_synchronized() && instruction_before->as_MonitorExit()) { roland@4860: instruction_before = instruction_before->prev(); roland@4860: } roland@4860: result = instruction_before; roland@4860: // Load constant only if needed roland@4860: Constant *constant = NULL; roland@4860: if (i != 0 || !instr) { roland@4860: constant = new Constant(new IntConstant(i)); roland@4860: NOT_PRODUCT(constant->set_printable_bci(position->printable_bci())); roland@4860: result = result->insert_after(constant); roland@4860: compare_with = constant; roland@4860: } roland@4860: roland@4860: if (instr) { roland@4860: assert(instr->type()->as_ObjectType() || instr->type()->as_IntType(), "Type must be array or integer!"); roland@4860: compare_with = instr; roland@4860: // Load array length if necessary roland@4860: Instruction *op = instr; roland@4860: if (instr->type()->as_ObjectType()) { roland@4860: assert(state, "must not be null"); roland@4860: ArrayLength *length = new ArrayLength(instr, state->copy()); roland@4860: NOT_PRODUCT(length->set_printable_bci(position->printable_bci())); roland@4860: length->set_exception_state(length->state_before()); roland@4860: result = result->insert_after(length); roland@4860: op = length; roland@4860: compare_with = length; roland@4860: } roland@4860: // Add operation only if necessary roland@4860: if (constant) { roland@4860: ArithmeticOp *ao = new ArithmeticOp(Bytecodes::_iadd, constant, op, false, NULL); roland@4860: NOT_PRODUCT(ao->set_printable_bci(position->printable_bci())); roland@4860: result = result->insert_after(ao); roland@4860: compare_with = ao; roland@4860: // TODO: Check that add operation does not overflow! roland@4860: } roland@4860: } roland@4860: assert(compare_with != NULL, "You have to compare with something!"); roland@4860: assert(instruction != NULL, "Instruction must not be null!"); roland@4860: roland@4860: if (instruction->type()->as_ObjectType()) { roland@4860: // Load array length if necessary roland@4860: Instruction *op = instruction; roland@4860: assert(state, "must not be null"); roland@4860: ArrayLength *length = new ArrayLength(instruction, state->copy()); roland@4860: length->set_exception_state(length->state_before()); roland@4860: NOT_PRODUCT(length->set_printable_bci(position->printable_bci())); roland@4860: result = result->insert_after(length); roland@4860: instruction = length; roland@4860: } roland@4860: roland@4860: Assert *assert = new Assert(instruction, cond, false, compare_with); roland@4860: NOT_PRODUCT(assert->set_printable_bci(position->printable_bci())); roland@4860: result->insert_after(assert); roland@4860: } roland@4860: roland@4860: // Add assertions roland@4860: void RangeCheckEliminator::add_assertions(Bound *bound, Instruction *instruction, Instruction *position) { roland@4860: // Add lower bound assertion roland@4860: if (bound->has_lower()) { roland@4860: bound->add_assertion(instruction, position, bound->lower(), bound->lower_instr(), Instruction::geq); roland@4860: } roland@4860: // Add upper bound assertion roland@4860: if (bound->has_upper()) { roland@4860: bound->add_assertion(instruction, position, bound->upper(), bound->upper_instr(), Instruction::leq); roland@4860: } roland@4860: } roland@4860: #endif roland@4860: