aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "c1/c1_IR.hpp" aoqi@0: #include "c1/c1_Instruction.hpp" aoqi@0: #include "c1/c1_InstructionPrinter.hpp" aoqi@0: #include "c1/c1_ValueStack.hpp" aoqi@0: #include "ci/ciObjArrayKlass.hpp" aoqi@0: #include "ci/ciTypeArrayKlass.hpp" aoqi@0: aoqi@0: aoqi@0: // Implementation of Instruction aoqi@0: aoqi@0: aoqi@0: int Instruction::dominator_depth() { aoqi@0: int result = -1; aoqi@0: if (block()) { aoqi@0: result = block()->dominator_depth(); aoqi@0: } aoqi@0: assert(result != -1 || this->as_Local(), "Only locals have dominator depth -1"); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: Instruction::Condition Instruction::mirror(Condition cond) { aoqi@0: switch (cond) { aoqi@0: case eql: return eql; aoqi@0: case neq: return neq; aoqi@0: case lss: return gtr; aoqi@0: case leq: return geq; aoqi@0: case gtr: return lss; aoqi@0: case geq: return leq; aoqi@0: case aeq: return beq; aoqi@0: case beq: return aeq; aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return eql; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Instruction::Condition Instruction::negate(Condition cond) { aoqi@0: switch (cond) { aoqi@0: case eql: return neq; aoqi@0: case neq: return eql; aoqi@0: case lss: return geq; aoqi@0: case leq: return gtr; aoqi@0: case gtr: return leq; aoqi@0: case geq: return lss; aoqi@0: case aeq: assert(false, "Above equal cannot be negated"); aoqi@0: case beq: assert(false, "Below equal cannot be negated"); aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return eql; aoqi@0: } aoqi@0: aoqi@0: void Instruction::update_exception_state(ValueStack* state) { aoqi@0: if (state != NULL && (state->kind() == ValueStack::EmptyExceptionState || state->kind() == ValueStack::ExceptionState)) { aoqi@0: assert(state->kind() == ValueStack::EmptyExceptionState || Compilation::current()->env()->jvmti_can_access_local_variables(), "unexpected state kind"); aoqi@0: _exception_state = state; aoqi@0: } else { aoqi@0: _exception_state = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Prev without need to have BlockBegin aoqi@0: Instruction* Instruction::prev() { aoqi@0: Instruction* p = NULL; aoqi@0: Instruction* q = block(); aoqi@0: while (q != this) { aoqi@0: assert(q != NULL, "this is not in the block's instruction list"); aoqi@0: p = q; q = q->next(); aoqi@0: } aoqi@0: return p; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Instruction::state_values_do(ValueVisitor* f) { aoqi@0: if (state_before() != NULL) { aoqi@0: state_before()->values_do(f); aoqi@0: } aoqi@0: if (exception_state() != NULL){ aoqi@0: exception_state()->values_do(f); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ciType* Instruction::exact_type() const { aoqi@0: ciType* t = declared_type(); aoqi@0: if (t != NULL && t->is_klass()) { aoqi@0: return t->as_klass()->exact_klass(); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void Instruction::check_state(ValueStack* state) { aoqi@0: if (state != NULL) { aoqi@0: state->verify(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Instruction::print() { aoqi@0: InstructionPrinter ip; aoqi@0: print(ip); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Instruction::print_line() { aoqi@0: InstructionPrinter ip; aoqi@0: ip.print_line(this); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Instruction::print(InstructionPrinter& ip) { aoqi@0: ip.print_head(); aoqi@0: ip.print_line(this); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: aoqi@0: // perform constant and interval tests on index value aoqi@0: bool AccessIndexed::compute_needs_range_check() { aoqi@0: if (length()) { aoqi@0: Constant* clength = length()->as_Constant(); aoqi@0: Constant* cindex = index()->as_Constant(); aoqi@0: if (clength && cindex) { aoqi@0: IntConstant* l = clength->type()->as_IntConstant(); aoqi@0: IntConstant* i = cindex->type()->as_IntConstant(); aoqi@0: if (l && i && i->value() < l->value() && i->value() >= 0) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (!this->check_flag(NeedsRangeCheckFlag)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: ciType* Constant::exact_type() const { aoqi@0: if (type()->is_object() && type()->as_ObjectType()->is_loaded()) { aoqi@0: return type()->as_ObjectType()->exact_type(); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: ciType* LoadIndexed::exact_type() const { aoqi@0: ciType* array_type = array()->exact_type(); aoqi@0: if (array_type != NULL) { aoqi@0: assert(array_type->is_array_klass(), "what else?"); aoqi@0: ciArrayKlass* ak = (ciArrayKlass*)array_type; aoqi@0: aoqi@0: if (ak->element_type()->is_instance_klass()) { aoqi@0: ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type(); aoqi@0: if (ik->is_loaded() && ik->is_final()) { aoqi@0: return ik; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return Instruction::exact_type(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: ciType* LoadIndexed::declared_type() const { aoqi@0: ciType* array_type = array()->declared_type(); aoqi@0: if (array_type == NULL || !array_type->is_loaded()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: assert(array_type->is_array_klass(), "what else?"); aoqi@0: ciArrayKlass* ak = (ciArrayKlass*)array_type; aoqi@0: return ak->element_type(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: ciType* LoadField::declared_type() const { aoqi@0: return field()->type(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: ciType* NewTypeArray::exact_type() const { aoqi@0: return ciTypeArrayKlass::make(elt_type()); aoqi@0: } aoqi@0: aoqi@0: ciType* NewObjectArray::exact_type() const { aoqi@0: return ciObjArrayKlass::make(klass()); aoqi@0: } aoqi@0: aoqi@0: ciType* NewArray::declared_type() const { aoqi@0: return exact_type(); aoqi@0: } aoqi@0: aoqi@0: ciType* NewInstance::exact_type() const { aoqi@0: return klass(); aoqi@0: } aoqi@0: aoqi@0: ciType* NewInstance::declared_type() const { aoqi@0: return exact_type(); aoqi@0: } aoqi@0: aoqi@0: ciType* CheckCast::declared_type() const { aoqi@0: return klass(); aoqi@0: } aoqi@0: aoqi@0: // Implementation of ArithmeticOp aoqi@0: aoqi@0: bool ArithmeticOp::is_commutative() const { aoqi@0: switch (op()) { aoqi@0: case Bytecodes::_iadd: // fall through aoqi@0: case Bytecodes::_ladd: // fall through aoqi@0: case Bytecodes::_fadd: // fall through aoqi@0: case Bytecodes::_dadd: // fall through aoqi@0: case Bytecodes::_imul: // fall through aoqi@0: case Bytecodes::_lmul: // fall through aoqi@0: case Bytecodes::_fmul: // fall through aoqi@0: case Bytecodes::_dmul: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool ArithmeticOp::can_trap() const { aoqi@0: switch (op()) { aoqi@0: case Bytecodes::_idiv: // fall through aoqi@0: case Bytecodes::_ldiv: // fall through aoqi@0: case Bytecodes::_irem: // fall through aoqi@0: case Bytecodes::_lrem: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of LogicOp aoqi@0: aoqi@0: bool LogicOp::is_commutative() const { aoqi@0: #ifdef ASSERT aoqi@0: switch (op()) { aoqi@0: case Bytecodes::_iand: // fall through aoqi@0: case Bytecodes::_land: // fall through aoqi@0: case Bytecodes::_ior : // fall through aoqi@0: case Bytecodes::_lor : // fall through aoqi@0: case Bytecodes::_ixor: // fall through aoqi@0: case Bytecodes::_lxor: break; aoqi@0: default : ShouldNotReachHere(); aoqi@0: } aoqi@0: #endif aoqi@0: // all LogicOps are commutative aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of IfOp aoqi@0: aoqi@0: bool IfOp::is_commutative() const { aoqi@0: return cond() == eql || cond() == neq; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of StateSplit aoqi@0: aoqi@0: void StateSplit::substitute(BlockList& list, BlockBegin* old_block, BlockBegin* new_block) { aoqi@0: NOT_PRODUCT(bool assigned = false;) aoqi@0: for (int i = 0; i < list.length(); i++) { aoqi@0: BlockBegin** b = list.adr_at(i); aoqi@0: if (*b == old_block) { aoqi@0: *b = new_block; aoqi@0: NOT_PRODUCT(assigned = true;) aoqi@0: } aoqi@0: } aoqi@0: assert(assigned == true, "should have assigned at least once"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: IRScope* StateSplit::scope() const { aoqi@0: return _state->scope(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StateSplit::state_values_do(ValueVisitor* f) { aoqi@0: Instruction::state_values_do(f); aoqi@0: if (state() != NULL) state()->values_do(f); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::state_values_do(ValueVisitor* f) { aoqi@0: StateSplit::state_values_do(f); aoqi@0: aoqi@0: if (is_set(BlockBegin::exception_entry_flag)) { aoqi@0: for (int i = 0; i < number_of_exception_states(); i++) { aoqi@0: exception_state_at(i)->values_do(f); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of Invoke aoqi@0: aoqi@0: aoqi@0: Invoke::Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args, aoqi@0: int vtable_index, ciMethod* target, ValueStack* state_before) aoqi@0: : StateSplit(result_type, state_before) aoqi@0: , _code(code) aoqi@0: , _recv(recv) aoqi@0: , _args(args) aoqi@0: , _vtable_index(vtable_index) aoqi@0: , _target(target) aoqi@0: { aoqi@0: set_flag(TargetIsLoadedFlag, target->is_loaded()); aoqi@0: set_flag(TargetIsFinalFlag, target_is_loaded() && target->is_final_method()); aoqi@0: set_flag(TargetIsStrictfpFlag, target_is_loaded() && target->is_strict()); aoqi@0: aoqi@0: assert(args != NULL, "args must exist"); aoqi@0: #ifdef ASSERT aoqi@0: AssertValues assert_value; aoqi@0: values_do(&assert_value); aoqi@0: #endif aoqi@0: aoqi@0: // provide an initial guess of signature size. aoqi@0: _signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0)); aoqi@0: if (has_receiver()) { aoqi@0: _signature->append(as_BasicType(receiver()->type())); aoqi@0: } aoqi@0: for (int i = 0; i < number_of_arguments(); i++) { aoqi@0: ValueType* t = argument_at(i)->type(); aoqi@0: BasicType bt = as_BasicType(t); aoqi@0: _signature->append(bt); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Invoke::state_values_do(ValueVisitor* f) { aoqi@0: StateSplit::state_values_do(f); aoqi@0: if (state_before() != NULL) state_before()->values_do(f); aoqi@0: if (state() != NULL) state()->values_do(f); aoqi@0: } aoqi@0: aoqi@0: ciType* Invoke::declared_type() const { aoqi@0: ciType *t = _target->signature()->return_type(); aoqi@0: assert(t->basic_type() != T_VOID, "need return value of void method?"); aoqi@0: return t; aoqi@0: } aoqi@0: aoqi@0: // Implementation of Contant aoqi@0: intx Constant::hash() const { aoqi@0: if (state_before() == NULL) { aoqi@0: switch (type()->tag()) { aoqi@0: case intTag: aoqi@0: return HASH2(name(), type()->as_IntConstant()->value()); aoqi@0: case addressTag: aoqi@0: return HASH2(name(), type()->as_AddressConstant()->value()); aoqi@0: case longTag: aoqi@0: { aoqi@0: jlong temp = type()->as_LongConstant()->value(); aoqi@0: return HASH3(name(), high(temp), low(temp)); aoqi@0: } aoqi@0: case floatTag: aoqi@0: return HASH2(name(), jint_cast(type()->as_FloatConstant()->value())); aoqi@0: case doubleTag: aoqi@0: { aoqi@0: jlong temp = jlong_cast(type()->as_DoubleConstant()->value()); aoqi@0: return HASH3(name(), high(temp), low(temp)); aoqi@0: } aoqi@0: case objectTag: aoqi@0: assert(type()->as_ObjectType()->is_loaded(), "can't handle unloaded values"); aoqi@0: return HASH2(name(), type()->as_ObjectType()->constant_value()); aoqi@0: case metaDataTag: aoqi@0: assert(type()->as_MetadataType()->is_loaded(), "can't handle unloaded values"); aoqi@0: return HASH2(name(), type()->as_MetadataType()->constant_value()); aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: bool Constant::is_equal(Value v) const { aoqi@0: if (v->as_Constant() == NULL) return false; aoqi@0: aoqi@0: switch (type()->tag()) { aoqi@0: case intTag: aoqi@0: { aoqi@0: IntConstant* t1 = type()->as_IntConstant(); aoqi@0: IntConstant* t2 = v->type()->as_IntConstant(); aoqi@0: return (t1 != NULL && t2 != NULL && aoqi@0: t1->value() == t2->value()); aoqi@0: } aoqi@0: case longTag: aoqi@0: { aoqi@0: LongConstant* t1 = type()->as_LongConstant(); aoqi@0: LongConstant* t2 = v->type()->as_LongConstant(); aoqi@0: return (t1 != NULL && t2 != NULL && aoqi@0: t1->value() == t2->value()); aoqi@0: } aoqi@0: case floatTag: aoqi@0: { aoqi@0: FloatConstant* t1 = type()->as_FloatConstant(); aoqi@0: FloatConstant* t2 = v->type()->as_FloatConstant(); aoqi@0: return (t1 != NULL && t2 != NULL && aoqi@0: jint_cast(t1->value()) == jint_cast(t2->value())); aoqi@0: } aoqi@0: case doubleTag: aoqi@0: { aoqi@0: DoubleConstant* t1 = type()->as_DoubleConstant(); aoqi@0: DoubleConstant* t2 = v->type()->as_DoubleConstant(); aoqi@0: return (t1 != NULL && t2 != NULL && aoqi@0: jlong_cast(t1->value()) == jlong_cast(t2->value())); aoqi@0: } aoqi@0: case objectTag: aoqi@0: { aoqi@0: ObjectType* t1 = type()->as_ObjectType(); aoqi@0: ObjectType* t2 = v->type()->as_ObjectType(); aoqi@0: return (t1 != NULL && t2 != NULL && aoqi@0: t1->is_loaded() && t2->is_loaded() && aoqi@0: t1->constant_value() == t2->constant_value()); aoqi@0: } aoqi@0: case metaDataTag: aoqi@0: { aoqi@0: MetadataType* t1 = type()->as_MetadataType(); aoqi@0: MetadataType* t2 = v->type()->as_MetadataType(); aoqi@0: return (t1 != NULL && t2 != NULL && aoqi@0: t1->is_loaded() && t2->is_loaded() && aoqi@0: t1->constant_value() == t2->constant_value()); aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: Constant::CompareResult Constant::compare(Instruction::Condition cond, Value right) const { aoqi@0: Constant* rc = right->as_Constant(); aoqi@0: // other is not a constant aoqi@0: if (rc == NULL) return not_comparable; aoqi@0: aoqi@0: ValueType* lt = type(); aoqi@0: ValueType* rt = rc->type(); aoqi@0: // different types aoqi@0: if (lt->base() != rt->base()) return not_comparable; aoqi@0: switch (lt->tag()) { aoqi@0: case intTag: { aoqi@0: int x = lt->as_IntConstant()->value(); aoqi@0: int y = rt->as_IntConstant()->value(); aoqi@0: switch (cond) { aoqi@0: case If::eql: return x == y ? cond_true : cond_false; aoqi@0: case If::neq: return x != y ? cond_true : cond_false; aoqi@0: case If::lss: return x < y ? cond_true : cond_false; aoqi@0: case If::leq: return x <= y ? cond_true : cond_false; aoqi@0: case If::gtr: return x > y ? cond_true : cond_false; aoqi@0: case If::geq: return x >= y ? cond_true : cond_false; aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case longTag: { aoqi@0: jlong x = lt->as_LongConstant()->value(); aoqi@0: jlong y = rt->as_LongConstant()->value(); aoqi@0: switch (cond) { aoqi@0: case If::eql: return x == y ? cond_true : cond_false; aoqi@0: case If::neq: return x != y ? cond_true : cond_false; aoqi@0: case If::lss: return x < y ? cond_true : cond_false; aoqi@0: case If::leq: return x <= y ? cond_true : cond_false; aoqi@0: case If::gtr: return x > y ? cond_true : cond_false; aoqi@0: case If::geq: return x >= y ? cond_true : cond_false; aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case objectTag: { aoqi@0: ciObject* xvalue = lt->as_ObjectType()->constant_value(); aoqi@0: ciObject* yvalue = rt->as_ObjectType()->constant_value(); aoqi@0: assert(xvalue != NULL && yvalue != NULL, "not constants"); aoqi@0: if (xvalue->is_loaded() && yvalue->is_loaded()) { aoqi@0: switch (cond) { aoqi@0: case If::eql: return xvalue == yvalue ? cond_true : cond_false; aoqi@0: case If::neq: return xvalue != yvalue ? cond_true : cond_false; aoqi@0: } aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case metaDataTag: { aoqi@0: ciMetadata* xvalue = lt->as_MetadataType()->constant_value(); aoqi@0: ciMetadata* yvalue = rt->as_MetadataType()->constant_value(); aoqi@0: assert(xvalue != NULL && yvalue != NULL, "not constants"); aoqi@0: if (xvalue->is_loaded() && yvalue->is_loaded()) { aoqi@0: switch (cond) { aoqi@0: case If::eql: return xvalue == yvalue ? cond_true : cond_false; aoqi@0: case If::neq: return xvalue != yvalue ? cond_true : cond_false; aoqi@0: } aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: return not_comparable; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of BlockBegin aoqi@0: aoqi@0: void BlockBegin::set_end(BlockEnd* end) { aoqi@0: assert(end != NULL, "should not reset block end to NULL"); aoqi@0: if (end == _end) { aoqi@0: return; aoqi@0: } aoqi@0: clear_end(); aoqi@0: aoqi@0: // Set the new end aoqi@0: _end = end; aoqi@0: aoqi@0: _successors.clear(); aoqi@0: // Now reset successors list based on BlockEnd aoqi@0: for (int i = 0; i < end->number_of_sux(); i++) { aoqi@0: BlockBegin* sux = end->sux_at(i); aoqi@0: _successors.append(sux); aoqi@0: sux->_predecessors.append(this); aoqi@0: } aoqi@0: _end->set_begin(this); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::clear_end() { aoqi@0: // Must make the predecessors/successors match up with the aoqi@0: // BlockEnd's notion. aoqi@0: if (_end != NULL) { aoqi@0: // disconnect from the old end aoqi@0: _end->set_begin(NULL); aoqi@0: aoqi@0: // disconnect this block from it's current successors aoqi@0: for (int i = 0; i < _successors.length(); i++) { aoqi@0: _successors.at(i)->remove_predecessor(this); aoqi@0: } aoqi@0: _end = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::disconnect_edge(BlockBegin* from, BlockBegin* to) { aoqi@0: // disconnect any edges between from and to aoqi@0: #ifndef PRODUCT aoqi@0: if (PrintIR && Verbose) { aoqi@0: tty->print_cr("Disconnected edge B%d -> B%d", from->block_id(), to->block_id()); aoqi@0: } aoqi@0: #endif aoqi@0: for (int s = 0; s < from->number_of_sux();) { aoqi@0: BlockBegin* sux = from->sux_at(s); aoqi@0: if (sux == to) { aoqi@0: int index = sux->_predecessors.index_of(from); aoqi@0: if (index >= 0) { aoqi@0: sux->_predecessors.remove_at(index); aoqi@0: } aoqi@0: from->_successors.remove_at(s); aoqi@0: } else { aoqi@0: s++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::disconnect_from_graph() { aoqi@0: // disconnect this block from all other blocks aoqi@0: for (int p = 0; p < number_of_preds(); p++) { aoqi@0: pred_at(p)->remove_successor(this); aoqi@0: } aoqi@0: for (int s = 0; s < number_of_sux(); s++) { aoqi@0: sux_at(s)->remove_predecessor(this); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) { aoqi@0: // modify predecessors before substituting successors aoqi@0: for (int i = 0; i < number_of_sux(); i++) { aoqi@0: if (sux_at(i) == old_sux) { aoqi@0: // remove old predecessor before adding new predecessor aoqi@0: // otherwise there is a dead predecessor in the list aoqi@0: new_sux->remove_predecessor(old_sux); aoqi@0: new_sux->add_predecessor(this); aoqi@0: } aoqi@0: } aoqi@0: old_sux->remove_predecessor(this); aoqi@0: end()->substitute_sux(old_sux, new_sux); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: // In general it is not possible to calculate a value for the field "depth_first_number" aoqi@0: // of the inserted block, without recomputing the values of the other blocks aoqi@0: // in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless. aoqi@0: BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) { aoqi@0: int bci = sux->bci(); aoqi@0: // critical edge splitting may introduce a goto after a if and array aoqi@0: // bound check elimination may insert a predicate between the if and aoqi@0: // goto. The bci of the goto can't be the one of the if otherwise aoqi@0: // the state and bci are inconsistent and a deoptimization triggered aoqi@0: // by the predicate would lead to incorrect execution/a crash. aoqi@0: BlockBegin* new_sux = new BlockBegin(bci); aoqi@0: aoqi@0: // mark this block (special treatment when block order is computed) aoqi@0: new_sux->set(critical_edge_split_flag); aoqi@0: aoqi@0: // This goto is not a safepoint. aoqi@0: Goto* e = new Goto(sux, false); aoqi@0: new_sux->set_next(e, bci); aoqi@0: new_sux->set_end(e); aoqi@0: // setup states aoqi@0: ValueStack* s = end()->state(); aoqi@0: new_sux->set_state(s->copy(s->kind(), bci)); aoqi@0: e->set_state(s->copy(s->kind(), bci)); aoqi@0: assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!"); aoqi@0: assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!"); aoqi@0: assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!"); aoqi@0: aoqi@0: // link predecessor to new block aoqi@0: end()->substitute_sux(sux, new_sux); aoqi@0: aoqi@0: // The ordering needs to be the same, so remove the link that the aoqi@0: // set_end call above added and substitute the new_sux for this aoqi@0: // block. aoqi@0: sux->remove_predecessor(new_sux); aoqi@0: aoqi@0: // the successor could be the target of a switch so it might have aoqi@0: // multiple copies of this predecessor, so substitute the new_sux aoqi@0: // for the first and delete the rest. aoqi@0: bool assigned = false; aoqi@0: BlockList& list = sux->_predecessors; aoqi@0: for (int i = 0; i < list.length(); i++) { aoqi@0: BlockBegin** b = list.adr_at(i); aoqi@0: if (*b == this) { aoqi@0: if (assigned) { aoqi@0: list.remove_at(i); aoqi@0: // reprocess this index aoqi@0: i--; aoqi@0: } else { aoqi@0: assigned = true; aoqi@0: *b = new_sux; aoqi@0: } aoqi@0: // link the new block back to it's predecessors. aoqi@0: new_sux->add_predecessor(this); aoqi@0: } aoqi@0: } aoqi@0: assert(assigned == true, "should have assigned at least once"); aoqi@0: return new_sux; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::remove_successor(BlockBegin* pred) { aoqi@0: int idx; aoqi@0: while ((idx = _successors.index_of(pred)) >= 0) { aoqi@0: _successors.remove_at(idx); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::add_predecessor(BlockBegin* pred) { aoqi@0: _predecessors.append(pred); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::remove_predecessor(BlockBegin* pred) { aoqi@0: int idx; aoqi@0: while ((idx = _predecessors.index_of(pred)) >= 0) { aoqi@0: _predecessors.remove_at(idx); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::add_exception_handler(BlockBegin* b) { aoqi@0: assert(b != NULL && (b->is_set(exception_entry_flag)), "exception handler must exist"); aoqi@0: // add only if not in the list already aoqi@0: if (!_exception_handlers.contains(b)) _exception_handlers.append(b); aoqi@0: } aoqi@0: aoqi@0: int BlockBegin::add_exception_state(ValueStack* state) { aoqi@0: assert(is_set(exception_entry_flag), "only for xhandlers"); aoqi@0: if (_exception_states == NULL) { aoqi@0: _exception_states = new ValueStackStack(4); aoqi@0: } aoqi@0: _exception_states->append(state); aoqi@0: return _exception_states->length() - 1; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::iterate_preorder(boolArray& mark, BlockClosure* closure) { aoqi@0: if (!mark.at(block_id())) { aoqi@0: mark.at_put(block_id(), true); aoqi@0: closure->block_do(this); aoqi@0: BlockEnd* e = end(); // must do this after block_do because block_do may change it! aoqi@0: { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_preorder(mark, closure); } aoqi@0: { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_preorder(mark, closure); } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::iterate_postorder(boolArray& mark, BlockClosure* closure) { aoqi@0: if (!mark.at(block_id())) { aoqi@0: mark.at_put(block_id(), true); aoqi@0: BlockEnd* e = end(); aoqi@0: { for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_postorder(mark, closure); } aoqi@0: { for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_postorder(mark, closure); } aoqi@0: closure->block_do(this); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::iterate_preorder(BlockClosure* closure) { aoqi@0: boolArray mark(number_of_blocks(), false); aoqi@0: iterate_preorder(mark, closure); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::iterate_postorder(BlockClosure* closure) { aoqi@0: boolArray mark(number_of_blocks(), false); aoqi@0: iterate_postorder(mark, closure); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::block_values_do(ValueVisitor* f) { aoqi@0: for (Instruction* n = this; n != NULL; n = n->next()) n->values_do(f); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: #define TRACE_PHI(code) if (PrintPhiFunctions) { code; } aoqi@0: #else aoqi@0: #define TRACE_PHI(coce) aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: bool BlockBegin::try_merge(ValueStack* new_state) { aoqi@0: TRACE_PHI(tty->print_cr("********** try_merge for block B%d", block_id())); aoqi@0: aoqi@0: // local variables used for state iteration aoqi@0: int index; aoqi@0: Value new_value, existing_value; aoqi@0: aoqi@0: ValueStack* existing_state = state(); aoqi@0: if (existing_state == NULL) { aoqi@0: TRACE_PHI(tty->print_cr("first call of try_merge for this block")); aoqi@0: aoqi@0: if (is_set(BlockBegin::was_visited_flag)) { aoqi@0: // this actually happens for complicated jsr/ret structures aoqi@0: return false; // BAILOUT in caller aoqi@0: } aoqi@0: aoqi@0: // copy state because it is altered aoqi@0: new_state = new_state->copy(ValueStack::BlockBeginState, bci()); aoqi@0: aoqi@0: // Use method liveness to invalidate dead locals aoqi@0: MethodLivenessResult liveness = new_state->scope()->method()->liveness_at_bci(bci()); aoqi@0: if (liveness.is_valid()) { aoqi@0: assert((int)liveness.size() == new_state->locals_size(), "error in use of liveness"); aoqi@0: aoqi@0: for_each_local_value(new_state, index, new_value) { aoqi@0: if (!liveness.at(index) || new_value->type()->is_illegal()) { aoqi@0: new_state->invalidate_local(index); aoqi@0: TRACE_PHI(tty->print_cr("invalidating dead local %d", index)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (is_set(BlockBegin::parser_loop_header_flag)) { aoqi@0: TRACE_PHI(tty->print_cr("loop header block, initializing phi functions")); aoqi@0: aoqi@0: for_each_stack_value(new_state, index, new_value) { aoqi@0: new_state->setup_phi_for_stack(this, index); aoqi@0: TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", new_state->stack_at(index)->type()->tchar(), new_state->stack_at(index)->id(), index)); aoqi@0: } aoqi@0: aoqi@0: BitMap requires_phi_function = new_state->scope()->requires_phi_function(); aoqi@0: aoqi@0: for_each_local_value(new_state, index, new_value) { aoqi@0: bool requires_phi = requires_phi_function.at(index) || (new_value->type()->is_double_word() && requires_phi_function.at(index + 1)); aoqi@0: if (requires_phi || !SelectivePhiFunctions) { aoqi@0: new_state->setup_phi_for_local(this, index); aoqi@0: TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", new_state->local_at(index)->type()->tchar(), new_state->local_at(index)->id(), index)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // initialize state of block aoqi@0: set_state(new_state); aoqi@0: aoqi@0: } else if (existing_state->is_same(new_state)) { aoqi@0: TRACE_PHI(tty->print_cr("exisiting state found")); aoqi@0: aoqi@0: assert(existing_state->scope() == new_state->scope(), "not matching"); aoqi@0: assert(existing_state->locals_size() == new_state->locals_size(), "not matching"); aoqi@0: assert(existing_state->stack_size() == new_state->stack_size(), "not matching"); aoqi@0: aoqi@0: if (is_set(BlockBegin::was_visited_flag)) { aoqi@0: TRACE_PHI(tty->print_cr("loop header block, phis must be present")); aoqi@0: aoqi@0: if (!is_set(BlockBegin::parser_loop_header_flag)) { aoqi@0: // this actually happens for complicated jsr/ret structures aoqi@0: return false; // BAILOUT in caller aoqi@0: } aoqi@0: aoqi@0: for_each_local_value(existing_state, index, existing_value) { aoqi@0: Value new_value = new_state->local_at(index); aoqi@0: if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) { aoqi@0: // The old code invalidated the phi function here aoqi@0: // Because dead locals are replaced with NULL, this is a very rare case now, so simply bail out aoqi@0: return false; // BAILOUT in caller aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // check that all necessary phi functions are present aoqi@0: for_each_stack_value(existing_state, index, existing_value) { aoqi@0: assert(existing_value->as_Phi() != NULL && existing_value->as_Phi()->block() == this, "phi function required"); aoqi@0: } aoqi@0: for_each_local_value(existing_state, index, existing_value) { aoqi@0: assert(existing_value == new_state->local_at(index) || (existing_value->as_Phi() != NULL && existing_value->as_Phi()->as_Phi()->block() == this), "phi function required"); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: } else { aoqi@0: TRACE_PHI(tty->print_cr("creating phi functions on demand")); aoqi@0: aoqi@0: // create necessary phi functions for stack aoqi@0: for_each_stack_value(existing_state, index, existing_value) { aoqi@0: Value new_value = new_state->stack_at(index); aoqi@0: Phi* existing_phi = existing_value->as_Phi(); aoqi@0: aoqi@0: if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) { aoqi@0: existing_state->setup_phi_for_stack(this, index); aoqi@0: TRACE_PHI(tty->print_cr("creating phi-function %c%d for stack %d", existing_state->stack_at(index)->type()->tchar(), existing_state->stack_at(index)->id(), index)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // create necessary phi functions for locals aoqi@0: for_each_local_value(existing_state, index, existing_value) { aoqi@0: Value new_value = new_state->local_at(index); aoqi@0: Phi* existing_phi = existing_value->as_Phi(); aoqi@0: aoqi@0: if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) { aoqi@0: existing_state->invalidate_local(index); aoqi@0: TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index)); aoqi@0: } else if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) { aoqi@0: existing_state->setup_phi_for_local(this, index); aoqi@0: TRACE_PHI(tty->print_cr("creating phi-function %c%d for local %d", existing_state->local_at(index)->type()->tchar(), existing_state->local_at(index)->id(), index)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: assert(existing_state->caller_state() == new_state->caller_state(), "caller states must be equal"); aoqi@0: aoqi@0: } else { aoqi@0: assert(false, "stack or locks not matching (invalid bytecodes)"); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: TRACE_PHI(tty->print_cr("********** try_merge for block B%d successful", block_id())); aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void BlockBegin::print_block() { aoqi@0: InstructionPrinter ip; aoqi@0: print_block(ip, false); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockBegin::print_block(InstructionPrinter& ip, bool live_only) { aoqi@0: ip.print_instr(this); tty->cr(); aoqi@0: ip.print_stack(this->state()); tty->cr(); aoqi@0: ip.print_inline_level(this); aoqi@0: ip.print_head(); aoqi@0: for (Instruction* n = next(); n != NULL; n = n->next()) { aoqi@0: if (!live_only || n->is_pinned() || n->use_count() > 0) { aoqi@0: ip.print_line(n); aoqi@0: } aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: aoqi@0: // Implementation of BlockList aoqi@0: aoqi@0: void BlockList::iterate_forward (BlockClosure* closure) { aoqi@0: const int l = length(); aoqi@0: for (int i = 0; i < l; i++) closure->block_do(at(i)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockList::iterate_backward(BlockClosure* closure) { aoqi@0: for (int i = length() - 1; i >= 0; i--) closure->block_do(at(i)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockList::blocks_do(void f(BlockBegin*)) { aoqi@0: for (int i = length() - 1; i >= 0; i--) f(at(i)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockList::values_do(ValueVisitor* f) { aoqi@0: for (int i = length() - 1; i >= 0; i--) at(i)->block_values_do(f); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void BlockList::print(bool cfg_only, bool live_only) { aoqi@0: InstructionPrinter ip; aoqi@0: for (int i = 0; i < length(); i++) { aoqi@0: BlockBegin* block = at(i); aoqi@0: if (cfg_only) { aoqi@0: ip.print_instr(block); tty->cr(); aoqi@0: } else { aoqi@0: block->print_block(ip, live_only); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: aoqi@0: // Implementation of BlockEnd aoqi@0: aoqi@0: void BlockEnd::set_begin(BlockBegin* begin) { aoqi@0: BlockList* sux = NULL; aoqi@0: if (begin != NULL) { aoqi@0: sux = begin->successors(); aoqi@0: } else if (this->begin() != NULL) { aoqi@0: // copy our sux list aoqi@0: BlockList* sux = new BlockList(this->begin()->number_of_sux()); aoqi@0: for (int i = 0; i < this->begin()->number_of_sux(); i++) { aoqi@0: sux->append(this->begin()->sux_at(i)); aoqi@0: } aoqi@0: } aoqi@0: _sux = sux; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) { aoqi@0: substitute(*_sux, old_sux, new_sux); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of Phi aoqi@0: aoqi@0: // Normal phi functions take their operands from the last instruction of the aoqi@0: // predecessor. Special handling is needed for xhanlder entries because there aoqi@0: // the state of arbitrary instructions are needed. aoqi@0: aoqi@0: Value Phi::operand_at(int i) const { aoqi@0: ValueStack* state; aoqi@0: if (_block->is_set(BlockBegin::exception_entry_flag)) { aoqi@0: state = _block->exception_state_at(i); aoqi@0: } else { aoqi@0: state = _block->pred_at(i)->end()->state(); aoqi@0: } aoqi@0: assert(state != NULL, ""); aoqi@0: aoqi@0: if (is_local()) { aoqi@0: return state->local_at(local_index()); aoqi@0: } else { aoqi@0: return state->stack_at(stack_index()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int Phi::operand_count() const { aoqi@0: if (_block->is_set(BlockBegin::exception_entry_flag)) { aoqi@0: return _block->number_of_exception_states(); aoqi@0: } else { aoqi@0: return _block->number_of_preds(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // Constructor of Assert aoqi@0: Assert::Assert(Value x, Condition cond, bool unordered_is_true, Value y) : Instruction(illegalType) aoqi@0: , _x(x) aoqi@0: , _cond(cond) aoqi@0: , _y(y) aoqi@0: { aoqi@0: set_flag(UnorderedIsTrueFlag, unordered_is_true); aoqi@0: assert(x->type()->tag() == y->type()->tag(), "types must match"); aoqi@0: pin(); aoqi@0: aoqi@0: stringStream strStream; aoqi@0: Compilation::current()->method()->print_name(&strStream); aoqi@0: aoqi@0: stringStream strStream1; aoqi@0: InstructionPrinter ip1(1, &strStream1); aoqi@0: ip1.print_instr(x); aoqi@0: aoqi@0: stringStream strStream2; aoqi@0: InstructionPrinter ip2(1, &strStream2); aoqi@0: ip2.print_instr(y); aoqi@0: aoqi@0: stringStream ss; aoqi@0: ss.print("Assertion %s %s %s in method %s", strStream1.as_string(), ip2.cond_name(cond), strStream2.as_string(), strStream.as_string()); aoqi@0: aoqi@0: _message = ss.as_string(); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: void RangeCheckPredicate::check_state() { aoqi@0: assert(state()->kind() != ValueStack::EmptyExceptionState && state()->kind() != ValueStack::ExceptionState, "will deopt with empty state"); aoqi@0: } aoqi@0: aoqi@0: void ProfileInvoke::state_values_do(ValueVisitor* f) { aoqi@0: if (state() != NULL) state()->values_do(f); aoqi@0: }