twisti@2047: /* coleenp@4037: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. twisti@2047: * Copyright 2008, 2009 Red Hat, Inc. twisti@2047: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. twisti@2047: * twisti@2047: * This code is free software; you can redistribute it and/or modify it twisti@2047: * under the terms of the GNU General Public License version 2 only, as twisti@2047: * published by the Free Software Foundation. twisti@2047: * twisti@2047: * This code is distributed in the hope that it will be useful, but WITHOUT twisti@2047: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or twisti@2047: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License twisti@2047: * version 2 for more details (a copy is included in the LICENSE file that twisti@2047: * accompanied this code). twisti@2047: * twisti@2047: * You should have received a copy of the GNU General Public License version twisti@2047: * 2 along with this work; if not, write to the Free Software Foundation, twisti@2047: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. twisti@2047: * twisti@2047: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA twisti@2047: * or visit www.oracle.com if you need additional information or have any twisti@2047: * questions. twisti@2047: * twisti@2047: */ twisti@2047: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "ci/ciType.hpp" stefank@2314: #include "ci/ciTypeFlow.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "shark/sharkBuilder.hpp" stefank@2314: #include "shark/sharkCacheDecache.hpp" stefank@2314: #include "shark/sharkState.hpp" stefank@2314: #include "shark/sharkTopLevelBlock.hpp" stefank@2314: #include "shark/sharkType.hpp" stefank@2314: #include "shark/sharkValue.hpp" twisti@2047: twisti@2047: using namespace llvm; twisti@2047: twisti@2047: void SharkState::initialize(const SharkState *state) { twisti@2047: _locals = NEW_RESOURCE_ARRAY(SharkValue*, max_locals()); twisti@2047: _stack = NEW_RESOURCE_ARRAY(SharkValue*, max_stack()); twisti@2047: twisti@2047: NOT_PRODUCT(memset(_locals, 23, max_locals() * sizeof(SharkValue *))); twisti@2047: NOT_PRODUCT(memset(_stack, 23, max_stack() * sizeof(SharkValue *))); twisti@2047: _sp = _stack; twisti@2047: twisti@2047: if (state) { twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: SharkValue *value = state->local(i); twisti@2047: if (value) twisti@2047: value = value->clone(); twisti@2047: set_local(i, value); twisti@2047: } twisti@2047: twisti@2047: for (int i = state->stack_depth() - 1; i >= 0; i--) { twisti@2047: SharkValue *value = state->stack(i); twisti@2047: if (value) twisti@2047: value = value->clone(); twisti@2047: push(value); twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: set_num_monitors(state ? state->num_monitors() : 0); twisti@2047: } twisti@2047: twisti@2047: bool SharkState::equal_to(SharkState *other) { twisti@2047: if (target() != other->target()) twisti@2047: return false; twisti@2047: twisti@2047: if (method() != other->method()) twisti@2047: return false; twisti@2047: twisti@2047: if (oop_tmp() != other->oop_tmp()) twisti@2047: return false; twisti@2047: twisti@2047: if (max_locals() != other->max_locals()) twisti@2047: return false; twisti@2047: twisti@2047: if (stack_depth() != other->stack_depth()) twisti@2047: return false; twisti@2047: twisti@2047: if (num_monitors() != other->num_monitors()) twisti@2047: return false; twisti@2047: twisti@2047: if (has_safepointed() != other->has_safepointed()) twisti@2047: return false; twisti@2047: twisti@2047: // Local variables twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: SharkValue *value = local(i); twisti@2047: SharkValue *other_value = other->local(i); twisti@2047: twisti@2047: if (value == NULL) { twisti@2047: if (other_value != NULL) twisti@2047: return false; twisti@2047: } twisti@2047: else { twisti@2047: if (other_value == NULL) twisti@2047: return false; twisti@2047: twisti@2047: if (!value->equal_to(other_value)) twisti@2047: return false; twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: // Expression stack twisti@2047: for (int i = 0; i < stack_depth(); i++) { twisti@2047: SharkValue *value = stack(i); twisti@2047: SharkValue *other_value = other->stack(i); twisti@2047: twisti@2047: if (value == NULL) { twisti@2047: if (other_value != NULL) twisti@2047: return false; twisti@2047: } twisti@2047: else { twisti@2047: if (other_value == NULL) twisti@2047: return false; twisti@2047: twisti@2047: if (!value->equal_to(other_value)) twisti@2047: return false; twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: return true; twisti@2047: } twisti@2047: twisti@2047: void SharkState::merge(SharkState* other, twisti@2047: BasicBlock* other_block, twisti@2047: BasicBlock* this_block) { twisti@2047: // Method twisti@2047: Value *this_method = this->method(); twisti@2047: Value *other_method = other->method(); twisti@2047: if (this_method != other_method) { twisti@4314: PHINode *phi = builder()->CreatePHI(SharkType::Method_type(), 0, "method"); twisti@2047: phi->addIncoming(this_method, this_block); twisti@2047: phi->addIncoming(other_method, other_block); twisti@2047: set_method(phi); twisti@2047: } twisti@2047: twisti@2047: // Temporary oop slot twisti@2047: Value *this_oop_tmp = this->oop_tmp(); twisti@2047: Value *other_oop_tmp = other->oop_tmp(); twisti@2047: if (this_oop_tmp != other_oop_tmp) { twisti@2047: assert(this_oop_tmp && other_oop_tmp, "can't merge NULL with non-NULL"); twisti@4314: PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "oop_tmp"); twisti@2047: phi->addIncoming(this_oop_tmp, this_block); twisti@2047: phi->addIncoming(other_oop_tmp, other_block); twisti@2047: set_oop_tmp(phi); twisti@2047: } twisti@2047: twisti@2047: // Monitors twisti@2047: assert(this->num_monitors() == other->num_monitors(), "should be"); twisti@2047: twisti@2047: // Local variables twisti@2047: assert(this->max_locals() == other->max_locals(), "should be"); twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: SharkValue *this_value = this->local(i); twisti@2047: SharkValue *other_value = other->local(i); twisti@2047: assert((this_value == NULL) == (other_value == NULL), "should be"); twisti@2047: if (this_value != NULL) { twisti@2047: char name[18]; twisti@2047: snprintf(name, sizeof(name), "local_%d_", i); twisti@2047: set_local(i, this_value->merge( twisti@2047: builder(), other_value, other_block, this_block, name)); twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: // Expression stack twisti@2047: assert(this->stack_depth() == other->stack_depth(), "should be"); twisti@2047: for (int i = 0; i < stack_depth(); i++) { twisti@2047: SharkValue *this_value = this->stack(i); twisti@2047: SharkValue *other_value = other->stack(i); twisti@2047: assert((this_value == NULL) == (other_value == NULL), "should be"); twisti@2047: if (this_value != NULL) { twisti@2047: char name[18]; twisti@2047: snprintf(name, sizeof(name), "stack_%d_", i); twisti@2047: set_stack(i, this_value->merge( twisti@2047: builder(), other_value, other_block, this_block, name)); twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: // Safepointed status twisti@2047: set_has_safepointed(this->has_safepointed() && other->has_safepointed()); twisti@2047: } twisti@2047: twisti@2047: void SharkState::replace_all(SharkValue* old_value, SharkValue* new_value) { twisti@2047: // Local variables twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: if (local(i) == old_value) twisti@2047: set_local(i, new_value); twisti@2047: } twisti@2047: twisti@2047: // Expression stack twisti@2047: for (int i = 0; i < stack_depth(); i++) { twisti@2047: if (stack(i) == old_value) twisti@2047: set_stack(i, new_value); twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: SharkNormalEntryState::SharkNormalEntryState(SharkTopLevelBlock* block, twisti@2047: Value* method) twisti@2047: : SharkState(block) { twisti@2047: assert(!block->stack_depth_at_entry(), "entry block shouldn't have stack"); twisti@2047: twisti@2047: // Local variables twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: ciType *type = block->local_type_at_entry(i); twisti@2047: twisti@2047: SharkValue *value = NULL; twisti@2047: switch (type->basic_type()) { twisti@2047: case T_INT: twisti@2047: case T_LONG: twisti@2047: case T_FLOAT: twisti@2047: case T_DOUBLE: twisti@2047: case T_OBJECT: twisti@2047: case T_ARRAY: twisti@2047: if (i >= arg_size()) { twisti@2047: ShouldNotReachHere(); twisti@2047: } twisti@2047: value = SharkValue::create_generic(type, NULL, i == 0 && !is_static()); twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_NULL: twisti@2047: value = SharkValue::null(); twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_BOTTOM: twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_LONG2: twisti@2047: case ciTypeFlow::StateVector::T_DOUBLE2: twisti@2047: break; twisti@2047: twisti@2047: default: twisti@2047: ShouldNotReachHere(); twisti@2047: } twisti@2047: set_local(i, value); twisti@2047: } twisti@2047: SharkNormalEntryCacher(block->function(), method).scan(this); twisti@2047: } twisti@2047: twisti@2047: SharkOSREntryState::SharkOSREntryState(SharkTopLevelBlock* block, twisti@2047: Value* method, twisti@2047: Value* osr_buf) twisti@2047: : SharkState(block) { twisti@4314: assert(block->stack_depth_at_entry() == 0, "entry block shouldn't have stack"); twisti@2047: set_num_monitors(block->ciblock()->monitor_count()); twisti@2047: twisti@2047: // Local variables twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: ciType *type = block->local_type_at_entry(i); twisti@2047: twisti@2047: SharkValue *value = NULL; twisti@2047: switch (type->basic_type()) { twisti@2047: case T_INT: twisti@2047: case T_LONG: twisti@2047: case T_FLOAT: twisti@2047: case T_DOUBLE: twisti@2047: case T_OBJECT: twisti@2047: case T_ARRAY: twisti@2047: value = SharkValue::create_generic(type, NULL, false); twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_NULL: twisti@2047: value = SharkValue::null(); twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_BOTTOM: twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_LONG2: twisti@2047: case ciTypeFlow::StateVector::T_DOUBLE2: twisti@2047: break; twisti@2047: twisti@2047: default: twisti@2047: ShouldNotReachHere(); twisti@2047: } twisti@2047: set_local(i, value); twisti@2047: } twisti@2047: SharkOSREntryCacher(block->function(), method, osr_buf).scan(this); twisti@2047: } twisti@2047: twisti@2047: SharkPHIState::SharkPHIState(SharkTopLevelBlock* block) twisti@2047: : SharkState(block), _block(block) { twisti@2047: BasicBlock *saved_insert_point = builder()->GetInsertBlock(); twisti@2047: builder()->SetInsertPoint(block->entry_block()); twisti@2047: char name[18]; twisti@2047: twisti@2047: // Method twisti@4314: set_method(builder()->CreatePHI(SharkType::Method_type(), 0, "method")); twisti@2047: twisti@2047: // Local variables twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: ciType *type = block->local_type_at_entry(i); twisti@2047: if (type->basic_type() == (BasicType) ciTypeFlow::StateVector::T_NULL) { twisti@2047: // XXX we could do all kinds of clever stuff here twisti@2047: type = ciType::make(T_OBJECT); // XXX what about T_ARRAY? twisti@2047: } twisti@2047: twisti@2047: SharkValue *value = NULL; twisti@2047: switch (type->basic_type()) { twisti@2047: case T_INT: twisti@2047: case T_LONG: twisti@2047: case T_FLOAT: twisti@2047: case T_DOUBLE: twisti@2047: case T_OBJECT: twisti@2047: case T_ARRAY: twisti@2047: snprintf(name, sizeof(name), "local_%d_", i); twisti@2047: value = SharkValue::create_phi( twisti@4314: type, builder()->CreatePHI(SharkType::to_stackType(type), 0, name)); twisti@2047: break; twisti@2047: twisti@2047: case T_ADDRESS: twisti@2047: value = SharkValue::address_constant(type->as_return_address()->bci()); twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_BOTTOM: twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_LONG2: twisti@2047: case ciTypeFlow::StateVector::T_DOUBLE2: twisti@2047: break; twisti@2047: twisti@2047: default: twisti@2047: ShouldNotReachHere(); twisti@2047: } twisti@2047: set_local(i, value); twisti@2047: } twisti@2047: twisti@2047: // Expression stack twisti@2047: for (int i = 0; i < block->stack_depth_at_entry(); i++) { twisti@2047: ciType *type = block->stack_type_at_entry(i); twisti@2047: if (type->basic_type() == (BasicType) ciTypeFlow::StateVector::T_NULL) { twisti@2047: // XXX we could do all kinds of clever stuff here twisti@2047: type = ciType::make(T_OBJECT); // XXX what about T_ARRAY? twisti@2047: } twisti@2047: twisti@2047: SharkValue *value = NULL; twisti@2047: switch (type->basic_type()) { twisti@2047: case T_INT: twisti@2047: case T_LONG: twisti@2047: case T_FLOAT: twisti@2047: case T_DOUBLE: twisti@2047: case T_OBJECT: twisti@2047: case T_ARRAY: twisti@2047: snprintf(name, sizeof(name), "stack_%d_", i); twisti@2047: value = SharkValue::create_phi( twisti@4314: type, builder()->CreatePHI(SharkType::to_stackType(type), 0, name)); twisti@2047: break; twisti@2047: twisti@2047: case T_ADDRESS: twisti@2047: value = SharkValue::address_constant(type->as_return_address()->bci()); twisti@2047: break; twisti@2047: twisti@2047: case ciTypeFlow::StateVector::T_LONG2: twisti@2047: case ciTypeFlow::StateVector::T_DOUBLE2: twisti@2047: break; twisti@2047: twisti@2047: default: twisti@2047: ShouldNotReachHere(); twisti@2047: } twisti@2047: push(value); twisti@2047: } twisti@2047: twisti@2047: // Monitors twisti@2047: set_num_monitors(block->ciblock()->monitor_count()); twisti@2047: twisti@2047: builder()->SetInsertPoint(saved_insert_point); twisti@2047: } twisti@2047: twisti@2047: void SharkPHIState::add_incoming(SharkState* incoming_state) { twisti@2047: BasicBlock *predecessor = builder()->GetInsertBlock(); twisti@2047: twisti@2047: // Method twisti@2047: ((PHINode *) method())->addIncoming(incoming_state->method(), predecessor); twisti@2047: twisti@2047: // Local variables twisti@2047: for (int i = 0; i < max_locals(); i++) { twisti@2047: if (local(i) != NULL) twisti@2047: local(i)->addIncoming(incoming_state->local(i), predecessor); twisti@2047: } twisti@2047: twisti@2047: // Expression stack twisti@2047: int stack_depth = block()->stack_depth_at_entry(); twisti@2047: assert(stack_depth == incoming_state->stack_depth(), "should be"); twisti@2047: for (int i = 0; i < stack_depth; i++) { twisti@2047: assert((stack(i) == NULL) == (incoming_state->stack(i) == NULL), "oops"); twisti@2047: if (stack(i)) twisti@2047: stack(i)->addIncoming(incoming_state->stack(i), predecessor); twisti@2047: } twisti@2047: twisti@2047: // Monitors twisti@2047: assert(num_monitors() == incoming_state->num_monitors(), "should be"); twisti@2047: twisti@2047: // Temporary oop slot twisti@2047: assert(oop_tmp() == incoming_state->oop_tmp(), "should be"); twisti@2047: }