Path: blob/master/src/hotspot/share/c1/c1_Instruction.cpp
40930 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_IR.hpp"26#include "c1/c1_Instruction.hpp"27#include "c1/c1_InstructionPrinter.hpp"28#include "c1/c1_ValueStack.hpp"29#include "ci/ciObjArrayKlass.hpp"30#include "ci/ciTypeArrayKlass.hpp"31#include "utilities/bitMap.inline.hpp"323334// Implementation of Instruction353637int Instruction::dominator_depth() {38int result = -1;39if (block()) {40result = block()->dominator_depth();41}42assert(result != -1 || this->as_Local(), "Only locals have dominator depth -1");43return result;44}4546Instruction::Condition Instruction::mirror(Condition cond) {47switch (cond) {48case eql: return eql;49case neq: return neq;50case lss: return gtr;51case leq: return geq;52case gtr: return lss;53case geq: return leq;54case aeq: return beq;55case beq: return aeq;56}57ShouldNotReachHere();58return eql;59}606162Instruction::Condition Instruction::negate(Condition cond) {63switch (cond) {64case eql: return neq;65case neq: return eql;66case lss: return geq;67case leq: return gtr;68case gtr: return leq;69case geq: return lss;70case aeq: assert(false, "Above equal cannot be negated");71case beq: assert(false, "Below equal cannot be negated");72}73ShouldNotReachHere();74return eql;75}7677void Instruction::update_exception_state(ValueStack* state) {78if (state != NULL && (state->kind() == ValueStack::EmptyExceptionState || state->kind() == ValueStack::ExceptionState)) {79assert(state->kind() == ValueStack::EmptyExceptionState || Compilation::current()->env()->should_retain_local_variables(), "unexpected state kind");80_exception_state = state;81} else {82_exception_state = NULL;83}84}8586// Prev without need to have BlockBegin87Instruction* Instruction::prev() {88Instruction* p = NULL;89Instruction* q = block();90while (q != this) {91assert(q != NULL, "this is not in the block's instruction list");92p = q; q = q->next();93}94return p;95}969798void Instruction::state_values_do(ValueVisitor* f) {99if (state_before() != NULL) {100state_before()->values_do(f);101}102if (exception_state() != NULL){103exception_state()->values_do(f);104}105}106107ciType* Instruction::exact_type() const {108ciType* t = declared_type();109if (t != NULL && t->is_klass()) {110return t->as_klass()->exact_klass();111}112return NULL;113}114115116#ifndef PRODUCT117void Instruction::check_state(ValueStack* state) {118if (state != NULL) {119state->verify();120}121}122123124void Instruction::print() {125InstructionPrinter ip;126print(ip);127}128129130void Instruction::print_line() {131InstructionPrinter ip;132ip.print_line(this);133}134135136void Instruction::print(InstructionPrinter& ip) {137ip.print_head();138ip.print_line(this);139tty->cr();140}141#endif // PRODUCT142143144// perform constant and interval tests on index value145bool AccessIndexed::compute_needs_range_check() {146if (length()) {147Constant* clength = length()->as_Constant();148Constant* cindex = index()->as_Constant();149if (clength && cindex) {150IntConstant* l = clength->type()->as_IntConstant();151IntConstant* i = cindex->type()->as_IntConstant();152if (l && i && i->value() < l->value() && i->value() >= 0) {153return false;154}155}156}157158if (!this->check_flag(NeedsRangeCheckFlag)) {159return false;160}161162return true;163}164165166ciType* Constant::exact_type() const {167if (type()->is_object() && type()->as_ObjectType()->is_loaded()) {168return type()->as_ObjectType()->exact_type();169}170return NULL;171}172173ciType* LoadIndexed::exact_type() const {174ciType* array_type = array()->exact_type();175if (array_type != NULL) {176assert(array_type->is_array_klass(), "what else?");177ciArrayKlass* ak = (ciArrayKlass*)array_type;178179if (ak->element_type()->is_instance_klass()) {180ciInstanceKlass* ik = (ciInstanceKlass*)ak->element_type();181if (ik->is_loaded() && ik->is_final()) {182return ik;183}184}185}186return Instruction::exact_type();187}188189190ciType* LoadIndexed::declared_type() const {191ciType* array_type = array()->declared_type();192if (array_type == NULL || !array_type->is_loaded()) {193return NULL;194}195assert(array_type->is_array_klass(), "what else?");196ciArrayKlass* ak = (ciArrayKlass*)array_type;197return ak->element_type();198}199200201ciType* LoadField::declared_type() const {202return field()->type();203}204205206ciType* NewTypeArray::exact_type() const {207return ciTypeArrayKlass::make(elt_type());208}209210ciType* NewObjectArray::exact_type() const {211return ciObjArrayKlass::make(klass());212}213214ciType* NewArray::declared_type() const {215return exact_type();216}217218ciType* NewInstance::exact_type() const {219return klass();220}221222ciType* NewInstance::declared_type() const {223return exact_type();224}225226ciType* CheckCast::declared_type() const {227return klass();228}229230// Implementation of ArithmeticOp231232bool ArithmeticOp::is_commutative() const {233switch (op()) {234case Bytecodes::_iadd: // fall through235case Bytecodes::_ladd: // fall through236case Bytecodes::_fadd: // fall through237case Bytecodes::_dadd: // fall through238case Bytecodes::_imul: // fall through239case Bytecodes::_lmul: // fall through240case Bytecodes::_fmul: // fall through241case Bytecodes::_dmul: return true;242default : return false;243}244}245246247bool ArithmeticOp::can_trap() const {248switch (op()) {249case Bytecodes::_idiv: // fall through250case Bytecodes::_ldiv: // fall through251case Bytecodes::_irem: // fall through252case Bytecodes::_lrem: return true;253default : return false;254}255}256257258// Implementation of LogicOp259260bool LogicOp::is_commutative() const {261#ifdef ASSERT262switch (op()) {263case Bytecodes::_iand: // fall through264case Bytecodes::_land: // fall through265case Bytecodes::_ior : // fall through266case Bytecodes::_lor : // fall through267case Bytecodes::_ixor: // fall through268case Bytecodes::_lxor: break;269default : ShouldNotReachHere(); break;270}271#endif272// all LogicOps are commutative273return true;274}275276277// Implementation of IfOp278279bool IfOp::is_commutative() const {280return cond() == eql || cond() == neq;281}282283284// Implementation of StateSplit285286void StateSplit::substitute(BlockList& list, BlockBegin* old_block, BlockBegin* new_block) {287NOT_PRODUCT(bool assigned = false;)288for (int i = 0; i < list.length(); i++) {289BlockBegin** b = list.adr_at(i);290if (*b == old_block) {291*b = new_block;292NOT_PRODUCT(assigned = true;)293}294}295assert(assigned == true, "should have assigned at least once");296}297298299IRScope* StateSplit::scope() const {300return _state->scope();301}302303304void StateSplit::state_values_do(ValueVisitor* f) {305Instruction::state_values_do(f);306if (state() != NULL) state()->values_do(f);307}308309310void BlockBegin::state_values_do(ValueVisitor* f) {311StateSplit::state_values_do(f);312313if (is_set(BlockBegin::exception_entry_flag)) {314for (int i = 0; i < number_of_exception_states(); i++) {315exception_state_at(i)->values_do(f);316}317}318}319320321// Implementation of Invoke322323324Invoke::Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args,325ciMethod* target, ValueStack* state_before)326: StateSplit(result_type, state_before)327, _code(code)328, _recv(recv)329, _args(args)330, _target(target)331{332set_flag(TargetIsLoadedFlag, target->is_loaded());333set_flag(TargetIsFinalFlag, target_is_loaded() && target->is_final_method());334335assert(args != NULL, "args must exist");336#ifdef ASSERT337AssertValues assert_value;338values_do(&assert_value);339#endif340341// provide an initial guess of signature size.342_signature = new BasicTypeList(number_of_arguments() + (has_receiver() ? 1 : 0));343if (has_receiver()) {344_signature->append(as_BasicType(receiver()->type()));345}346for (int i = 0; i < number_of_arguments(); i++) {347ValueType* t = argument_at(i)->type();348BasicType bt = as_BasicType(t);349_signature->append(bt);350}351}352353354void Invoke::state_values_do(ValueVisitor* f) {355StateSplit::state_values_do(f);356if (state_before() != NULL) state_before()->values_do(f);357if (state() != NULL) state()->values_do(f);358}359360ciType* Invoke::declared_type() const {361ciSignature* declared_signature = state()->scope()->method()->get_declared_signature_at_bci(state()->bci());362ciType *t = declared_signature->return_type();363assert(t->basic_type() != T_VOID, "need return value of void method?");364return t;365}366367// Implementation of Contant368intx Constant::hash() const {369if (state_before() == NULL) {370switch (type()->tag()) {371case intTag:372return HASH2(name(), type()->as_IntConstant()->value());373case addressTag:374return HASH2(name(), type()->as_AddressConstant()->value());375case longTag:376{377jlong temp = type()->as_LongConstant()->value();378return HASH3(name(), high(temp), low(temp));379}380case floatTag:381return HASH2(name(), jint_cast(type()->as_FloatConstant()->value()));382case doubleTag:383{384jlong temp = jlong_cast(type()->as_DoubleConstant()->value());385return HASH3(name(), high(temp), low(temp));386}387case objectTag:388assert(type()->as_ObjectType()->is_loaded(), "can't handle unloaded values");389return HASH2(name(), type()->as_ObjectType()->constant_value());390case metaDataTag:391assert(type()->as_MetadataType()->is_loaded(), "can't handle unloaded values");392return HASH2(name(), type()->as_MetadataType()->constant_value());393default:394ShouldNotReachHere();395}396}397return 0;398}399400bool Constant::is_equal(Value v) const {401if (v->as_Constant() == NULL) return false;402403switch (type()->tag()) {404case intTag:405{406IntConstant* t1 = type()->as_IntConstant();407IntConstant* t2 = v->type()->as_IntConstant();408return (t1 != NULL && t2 != NULL &&409t1->value() == t2->value());410}411case longTag:412{413LongConstant* t1 = type()->as_LongConstant();414LongConstant* t2 = v->type()->as_LongConstant();415return (t1 != NULL && t2 != NULL &&416t1->value() == t2->value());417}418case floatTag:419{420FloatConstant* t1 = type()->as_FloatConstant();421FloatConstant* t2 = v->type()->as_FloatConstant();422return (t1 != NULL && t2 != NULL &&423jint_cast(t1->value()) == jint_cast(t2->value()));424}425case doubleTag:426{427DoubleConstant* t1 = type()->as_DoubleConstant();428DoubleConstant* t2 = v->type()->as_DoubleConstant();429return (t1 != NULL && t2 != NULL &&430jlong_cast(t1->value()) == jlong_cast(t2->value()));431}432case objectTag:433{434ObjectType* t1 = type()->as_ObjectType();435ObjectType* t2 = v->type()->as_ObjectType();436return (t1 != NULL && t2 != NULL &&437t1->is_loaded() && t2->is_loaded() &&438t1->constant_value() == t2->constant_value());439}440case metaDataTag:441{442MetadataType* t1 = type()->as_MetadataType();443MetadataType* t2 = v->type()->as_MetadataType();444return (t1 != NULL && t2 != NULL &&445t1->is_loaded() && t2->is_loaded() &&446t1->constant_value() == t2->constant_value());447}448default:449return false;450}451}452453Constant::CompareResult Constant::compare(Instruction::Condition cond, Value right) const {454Constant* rc = right->as_Constant();455// other is not a constant456if (rc == NULL) return not_comparable;457458ValueType* lt = type();459ValueType* rt = rc->type();460// different types461if (lt->base() != rt->base()) return not_comparable;462switch (lt->tag()) {463case intTag: {464int x = lt->as_IntConstant()->value();465int y = rt->as_IntConstant()->value();466switch (cond) {467case If::eql: return x == y ? cond_true : cond_false;468case If::neq: return x != y ? cond_true : cond_false;469case If::lss: return x < y ? cond_true : cond_false;470case If::leq: return x <= y ? cond_true : cond_false;471case If::gtr: return x > y ? cond_true : cond_false;472case If::geq: return x >= y ? cond_true : cond_false;473default : break;474}475break;476}477case longTag: {478jlong x = lt->as_LongConstant()->value();479jlong y = rt->as_LongConstant()->value();480switch (cond) {481case If::eql: return x == y ? cond_true : cond_false;482case If::neq: return x != y ? cond_true : cond_false;483case If::lss: return x < y ? cond_true : cond_false;484case If::leq: return x <= y ? cond_true : cond_false;485case If::gtr: return x > y ? cond_true : cond_false;486case If::geq: return x >= y ? cond_true : cond_false;487default : break;488}489break;490}491case objectTag: {492ciObject* xvalue = lt->as_ObjectType()->constant_value();493ciObject* yvalue = rt->as_ObjectType()->constant_value();494assert(xvalue != NULL && yvalue != NULL, "not constants");495if (xvalue->is_loaded() && yvalue->is_loaded()) {496switch (cond) {497case If::eql: return xvalue == yvalue ? cond_true : cond_false;498case If::neq: return xvalue != yvalue ? cond_true : cond_false;499default : break;500}501}502break;503}504case metaDataTag: {505ciMetadata* xvalue = lt->as_MetadataType()->constant_value();506ciMetadata* yvalue = rt->as_MetadataType()->constant_value();507assert(xvalue != NULL && yvalue != NULL, "not constants");508if (xvalue->is_loaded() && yvalue->is_loaded()) {509switch (cond) {510case If::eql: return xvalue == yvalue ? cond_true : cond_false;511case If::neq: return xvalue != yvalue ? cond_true : cond_false;512default : break;513}514}515break;516}517default:518break;519}520return not_comparable;521}522523524// Implementation of BlockBegin525526void BlockBegin::set_end(BlockEnd* end) {527assert(end != NULL, "should not reset block end to NULL");528if (end == _end) {529return;530}531clear_end();532533// Set the new end534_end = end;535536_successors.clear();537// Now reset successors list based on BlockEnd538for (int i = 0; i < end->number_of_sux(); i++) {539BlockBegin* sux = end->sux_at(i);540_successors.append(sux);541sux->_predecessors.append(this);542}543_end->set_begin(this);544}545546547void BlockBegin::clear_end() {548// Must make the predecessors/successors match up with the549// BlockEnd's notion.550if (_end != NULL) {551// disconnect from the old end552_end->set_begin(NULL);553554// disconnect this block from it's current successors555for (int i = 0; i < _successors.length(); i++) {556_successors.at(i)->remove_predecessor(this);557}558_end = NULL;559}560}561562563void BlockBegin::disconnect_edge(BlockBegin* from, BlockBegin* to) {564// disconnect any edges between from and to565#ifndef PRODUCT566if (PrintIR && Verbose) {567tty->print_cr("Disconnected edge B%d -> B%d", from->block_id(), to->block_id());568}569#endif570for (int s = 0; s < from->number_of_sux();) {571BlockBegin* sux = from->sux_at(s);572if (sux == to) {573int index = sux->_predecessors.find(from);574if (index >= 0) {575sux->_predecessors.remove_at(index);576}577from->_successors.remove_at(s);578} else {579s++;580}581}582}583584585void BlockBegin::disconnect_from_graph() {586// disconnect this block from all other blocks587for (int p = 0; p < number_of_preds(); p++) {588pred_at(p)->remove_successor(this);589}590for (int s = 0; s < number_of_sux(); s++) {591sux_at(s)->remove_predecessor(this);592}593}594595void BlockBegin::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {596// modify predecessors before substituting successors597for (int i = 0; i < number_of_sux(); i++) {598if (sux_at(i) == old_sux) {599// remove old predecessor before adding new predecessor600// otherwise there is a dead predecessor in the list601new_sux->remove_predecessor(old_sux);602new_sux->add_predecessor(this);603}604}605old_sux->remove_predecessor(this);606end()->substitute_sux(old_sux, new_sux);607}608609610611// In general it is not possible to calculate a value for the field "depth_first_number"612// of the inserted block, without recomputing the values of the other blocks613// in the CFG. Therefore the value of "depth_first_number" in BlockBegin becomes meaningless.614BlockBegin* BlockBegin::insert_block_between(BlockBegin* sux) {615int bci = sux->bci();616// critical edge splitting may introduce a goto after a if and array617// bound check elimination may insert a predicate between the if and618// goto. The bci of the goto can't be the one of the if otherwise619// the state and bci are inconsistent and a deoptimization triggered620// by the predicate would lead to incorrect execution/a crash.621BlockBegin* new_sux = new BlockBegin(bci);622623// mark this block (special treatment when block order is computed)624new_sux->set(critical_edge_split_flag);625626// This goto is not a safepoint.627Goto* e = new Goto(sux, false);628new_sux->set_next(e, bci);629new_sux->set_end(e);630// setup states631ValueStack* s = end()->state();632new_sux->set_state(s->copy(s->kind(), bci));633e->set_state(s->copy(s->kind(), bci));634assert(new_sux->state()->locals_size() == s->locals_size(), "local size mismatch!");635assert(new_sux->state()->stack_size() == s->stack_size(), "stack size mismatch!");636assert(new_sux->state()->locks_size() == s->locks_size(), "locks size mismatch!");637638// link predecessor to new block639end()->substitute_sux(sux, new_sux);640641// The ordering needs to be the same, so remove the link that the642// set_end call above added and substitute the new_sux for this643// block.644sux->remove_predecessor(new_sux);645646// the successor could be the target of a switch so it might have647// multiple copies of this predecessor, so substitute the new_sux648// for the first and delete the rest.649bool assigned = false;650BlockList& list = sux->_predecessors;651for (int i = 0; i < list.length(); i++) {652BlockBegin** b = list.adr_at(i);653if (*b == this) {654if (assigned) {655list.remove_at(i);656// reprocess this index657i--;658} else {659assigned = true;660*b = new_sux;661}662// link the new block back to it's predecessors.663new_sux->add_predecessor(this);664}665}666assert(assigned == true, "should have assigned at least once");667return new_sux;668}669670671void BlockBegin::remove_successor(BlockBegin* pred) {672int idx;673while ((idx = _successors.find(pred)) >= 0) {674_successors.remove_at(idx);675}676}677678679void BlockBegin::add_predecessor(BlockBegin* pred) {680_predecessors.append(pred);681}682683684void BlockBegin::remove_predecessor(BlockBegin* pred) {685int idx;686while ((idx = _predecessors.find(pred)) >= 0) {687_predecessors.remove_at(idx);688}689}690691692void BlockBegin::add_exception_handler(BlockBegin* b) {693assert(b != NULL && (b->is_set(exception_entry_flag)), "exception handler must exist");694// add only if not in the list already695if (!_exception_handlers.contains(b)) _exception_handlers.append(b);696}697698int BlockBegin::add_exception_state(ValueStack* state) {699assert(is_set(exception_entry_flag), "only for xhandlers");700if (_exception_states == NULL) {701_exception_states = new ValueStackStack(4);702}703_exception_states->append(state);704return _exception_states->length() - 1;705}706707708void BlockBegin::iterate_preorder(boolArray& mark, BlockClosure* closure) {709if (!mark.at(block_id())) {710mark.at_put(block_id(), true);711closure->block_do(this);712BlockEnd* e = end(); // must do this after block_do because block_do may change it!713{ for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_preorder(mark, closure); }714{ for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_preorder(mark, closure); }715}716}717718719void BlockBegin::iterate_postorder(boolArray& mark, BlockClosure* closure) {720if (!mark.at(block_id())) {721mark.at_put(block_id(), true);722BlockEnd* e = end();723{ for (int i = number_of_exception_handlers() - 1; i >= 0; i--) exception_handler_at(i)->iterate_postorder(mark, closure); }724{ for (int i = e->number_of_sux () - 1; i >= 0; i--) e->sux_at (i)->iterate_postorder(mark, closure); }725closure->block_do(this);726}727}728729730void BlockBegin::iterate_preorder(BlockClosure* closure) {731int mark_len = number_of_blocks();732boolArray mark(mark_len, mark_len, false);733iterate_preorder(mark, closure);734}735736737void BlockBegin::iterate_postorder(BlockClosure* closure) {738int mark_len = number_of_blocks();739boolArray mark(mark_len, mark_len, false);740iterate_postorder(mark, closure);741}742743744void BlockBegin::block_values_do(ValueVisitor* f) {745for (Instruction* n = this; n != NULL; n = n->next()) n->values_do(f);746}747748749#ifndef PRODUCT750#define TRACE_PHI(code) if (PrintPhiFunctions) { code; }751#else752#define TRACE_PHI(coce)753#endif754755756bool BlockBegin::try_merge(ValueStack* new_state) {757TRACE_PHI(tty->print_cr("********** try_merge for block B%d", block_id()));758759// local variables used for state iteration760int index;761Value new_value, existing_value;762763ValueStack* existing_state = state();764if (existing_state == NULL) {765TRACE_PHI(tty->print_cr("first call of try_merge for this block"));766767if (is_set(BlockBegin::was_visited_flag)) {768// this actually happens for complicated jsr/ret structures769return false; // BAILOUT in caller770}771772// copy state because it is altered773new_state = new_state->copy(ValueStack::BlockBeginState, bci());774775// Use method liveness to invalidate dead locals776MethodLivenessResult liveness = new_state->scope()->method()->liveness_at_bci(bci());777if (liveness.is_valid()) {778assert((int)liveness.size() == new_state->locals_size(), "error in use of liveness");779780for_each_local_value(new_state, index, new_value) {781if (!liveness.at(index) || new_value->type()->is_illegal()) {782new_state->invalidate_local(index);783TRACE_PHI(tty->print_cr("invalidating dead local %d", index));784}785}786}787788if (is_set(BlockBegin::parser_loop_header_flag)) {789TRACE_PHI(tty->print_cr("loop header block, initializing phi functions"));790791for_each_stack_value(new_state, index, new_value) {792new_state->setup_phi_for_stack(this, index);793TRACE_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));794}795796BitMap& requires_phi_function = new_state->scope()->requires_phi_function();797798for_each_local_value(new_state, index, new_value) {799bool requires_phi = requires_phi_function.at(index) || (new_value->type()->is_double_word() && requires_phi_function.at(index + 1));800if (requires_phi || !SelectivePhiFunctions) {801new_state->setup_phi_for_local(this, index);802TRACE_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));803}804}805}806807// initialize state of block808set_state(new_state);809810} else if (existing_state->is_same(new_state)) {811TRACE_PHI(tty->print_cr("exisiting state found"));812813assert(existing_state->scope() == new_state->scope(), "not matching");814assert(existing_state->locals_size() == new_state->locals_size(), "not matching");815assert(existing_state->stack_size() == new_state->stack_size(), "not matching");816817if (is_set(BlockBegin::was_visited_flag)) {818TRACE_PHI(tty->print_cr("loop header block, phis must be present"));819820if (!is_set(BlockBegin::parser_loop_header_flag)) {821// this actually happens for complicated jsr/ret structures822return false; // BAILOUT in caller823}824825for_each_local_value(existing_state, index, existing_value) {826Value new_value = new_state->local_at(index);827if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) {828Phi* existing_phi = existing_value->as_Phi();829if (existing_phi == NULL) {830return false; // BAILOUT in caller831}832// Invalidate the phi function here. This case is very rare except for833// JVMTI capability "can_access_local_variables".834// In really rare cases we will bail out in LIRGenerator::move_to_phi.835existing_phi->make_illegal();836existing_state->invalidate_local(index);837TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index));838}839}840841#ifdef ASSERT842// check that all necessary phi functions are present843for_each_stack_value(existing_state, index, existing_value) {844assert(existing_value->as_Phi() != NULL && existing_value->as_Phi()->block() == this, "phi function required");845}846for_each_local_value(existing_state, index, existing_value) {847assert(existing_value == new_state->local_at(index) || (existing_value->as_Phi() != NULL && existing_value->as_Phi()->as_Phi()->block() == this), "phi function required");848}849#endif850851} else {852TRACE_PHI(tty->print_cr("creating phi functions on demand"));853854// create necessary phi functions for stack855for_each_stack_value(existing_state, index, existing_value) {856Value new_value = new_state->stack_at(index);857Phi* existing_phi = existing_value->as_Phi();858859if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) {860existing_state->setup_phi_for_stack(this, index);861TRACE_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));862}863}864865// create necessary phi functions for locals866for_each_local_value(existing_state, index, existing_value) {867Value new_value = new_state->local_at(index);868Phi* existing_phi = existing_value->as_Phi();869870if (new_value == NULL || new_value->type()->tag() != existing_value->type()->tag()) {871existing_state->invalidate_local(index);872TRACE_PHI(tty->print_cr("invalidating local %d because of type mismatch", index));873} else if (new_value != existing_value && (existing_phi == NULL || existing_phi->block() != this)) {874existing_state->setup_phi_for_local(this, index);875TRACE_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));876}877}878}879880assert(existing_state->caller_state() == new_state->caller_state(), "caller states must be equal");881882} else {883assert(false, "stack or locks not matching (invalid bytecodes)");884return false;885}886887TRACE_PHI(tty->print_cr("********** try_merge for block B%d successful", block_id()));888889return true;890}891892893#ifndef PRODUCT894void BlockBegin::print_block() {895InstructionPrinter ip;896print_block(ip, false);897}898899900void BlockBegin::print_block(InstructionPrinter& ip, bool live_only) {901ip.print_instr(this); tty->cr();902ip.print_stack(this->state()); tty->cr();903ip.print_inline_level(this);904ip.print_head();905for (Instruction* n = next(); n != NULL; n = n->next()) {906if (!live_only || n->is_pinned() || n->use_count() > 0) {907ip.print_line(n);908}909}910tty->cr();911}912#endif // PRODUCT913914915// Implementation of BlockList916917void BlockList::iterate_forward (BlockClosure* closure) {918const int l = length();919for (int i = 0; i < l; i++) closure->block_do(at(i));920}921922923void BlockList::iterate_backward(BlockClosure* closure) {924for (int i = length() - 1; i >= 0; i--) closure->block_do(at(i));925}926927928void BlockList::blocks_do(void f(BlockBegin*)) {929for (int i = length() - 1; i >= 0; i--) f(at(i));930}931932933void BlockList::values_do(ValueVisitor* f) {934for (int i = length() - 1; i >= 0; i--) at(i)->block_values_do(f);935}936937938#ifndef PRODUCT939void BlockList::print(bool cfg_only, bool live_only) {940InstructionPrinter ip;941for (int i = 0; i < length(); i++) {942BlockBegin* block = at(i);943if (cfg_only) {944ip.print_instr(block); tty->cr();945} else {946block->print_block(ip, live_only);947}948}949}950#endif // PRODUCT951952953// Implementation of BlockEnd954955void BlockEnd::set_begin(BlockBegin* begin) {956BlockList* sux = NULL;957if (begin != NULL) {958sux = begin->successors();959} else if (this->begin() != NULL) {960// copy our sux list961BlockList* sux = new BlockList(this->begin()->number_of_sux());962for (int i = 0; i < this->begin()->number_of_sux(); i++) {963sux->append(this->begin()->sux_at(i));964}965}966_sux = sux;967}968969970void BlockEnd::substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux) {971substitute(*_sux, old_sux, new_sux);972}973974975// Implementation of Phi976977// Normal phi functions take their operands from the last instruction of the978// predecessor. Special handling is needed for xhanlder entries because there979// the state of arbitrary instructions are needed.980981Value Phi::operand_at(int i) const {982ValueStack* state;983if (_block->is_set(BlockBegin::exception_entry_flag)) {984state = _block->exception_state_at(i);985} else {986state = _block->pred_at(i)->end()->state();987}988assert(state != NULL, "");989990if (is_local()) {991return state->local_at(local_index());992} else {993return state->stack_at(stack_index());994}995}996997998int Phi::operand_count() const {999if (_block->is_set(BlockBegin::exception_entry_flag)) {1000return _block->number_of_exception_states();1001} else {1002return _block->number_of_preds();1003}1004}10051006#ifdef ASSERT1007// Constructor of Assert1008Assert::Assert(Value x, Condition cond, bool unordered_is_true, Value y) : Instruction(illegalType)1009, _x(x)1010, _cond(cond)1011, _y(y)1012{1013set_flag(UnorderedIsTrueFlag, unordered_is_true);1014assert(x->type()->tag() == y->type()->tag(), "types must match");1015pin();10161017stringStream strStream;1018Compilation::current()->method()->print_name(&strStream);10191020stringStream strStream1;1021InstructionPrinter ip1(1, &strStream1);1022ip1.print_instr(x);10231024stringStream strStream2;1025InstructionPrinter ip2(1, &strStream2);1026ip2.print_instr(y);10271028stringStream ss;1029ss.print("Assertion %s %s %s in method %s", strStream1.as_string(), ip2.cond_name(cond), strStream2.as_string(), strStream.as_string());10301031_message = ss.as_string();1032}1033#endif10341035void RangeCheckPredicate::check_state() {1036assert(state()->kind() != ValueStack::EmptyExceptionState && state()->kind() != ValueStack::ExceptionState, "will deopt with empty state");1037}10381039void ProfileInvoke::state_values_do(ValueVisitor* f) {1040if (state() != NULL) state()->values_do(f);1041}104210431044