Path: blob/master/src/hotspot/share/c1/c1_ValueStack.hpp
40931 views
/*1* Copyright (c) 1999, 2019, 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#ifndef SHARE_C1_C1_VALUESTACK_HPP25#define SHARE_C1_C1_VALUESTACK_HPP2627#include "c1/c1_Instruction.hpp"2829class ValueStack: public CompilationResourceObj {30public:31enum Kind {32Parsing, // During abstract interpretation in GraphBuilder33CallerState, // Caller state when inlining34StateBefore, // Before before execution of instruction35StateAfter, // After execution of instruction36ExceptionState, // Exception handling of instruction37EmptyExceptionState, // Exception handling of instructions not covered by an xhandler38BlockBeginState // State of BlockBegin instruction with phi functions of this block39};4041private:42IRScope* _scope; // the enclosing scope43ValueStack* _caller_state;44int _bci;45Kind _kind;4647Values _locals; // the locals48Values _stack; // the expression stack49Values* _locks; // the monitor stack (holding the locked values)5051Value check(ValueTag tag, Value t) {52assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond");53return t;54}5556Value check(ValueTag tag, Value t, Value h) {57assert(h == NULL, "hi-word of doubleword value must be NULL");58return check(tag, t);59}6061// helper routine62static void apply(const Values& list, ValueVisitor* f);6364// for simplified copying65ValueStack(ValueStack* copy_from, Kind kind, int bci);6667int locals_size_for_copy(Kind kind) const;68int stack_size_for_copy(Kind kind) const;69public:70// creation71ValueStack(IRScope* scope, ValueStack* caller_state);7273ValueStack* copy() { return new ValueStack(this, _kind, _bci); }74ValueStack* copy(Kind new_kind, int new_bci) { return new ValueStack(this, new_kind, new_bci); }75ValueStack* copy_for_parsing() { return new ValueStack(this, Parsing, -99); }7677void set_caller_state(ValueStack* s) {78assert(kind() == EmptyExceptionState ||79(Compilation::current()->env()->should_retain_local_variables() && kind() == ExceptionState),80"only EmptyExceptionStates can be modified");81_caller_state = s;82}8384bool is_same(ValueStack* s); // returns true if this & s's types match (w/o checking locals)8586// accessors87IRScope* scope() const { return _scope; }88ValueStack* caller_state() const { return _caller_state; }89int bci() const { return _bci; }90Kind kind() const { return _kind; }9192int locals_size() const { return _locals.length(); }93int stack_size() const { return _stack.length(); }94int locks_size() const { return _locks == NULL ? 0 : _locks->length(); }95bool stack_is_empty() const { return _stack.is_empty(); }96bool no_active_locks() const { return _locks == NULL || _locks->is_empty(); }97int total_locks_size() const;9899// locals access100void clear_locals(); // sets all locals to NULL;101102void invalidate_local(int i) {103assert(!_locals.at(i)->type()->is_double_word() ||104_locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");105_locals.at_put(i, NULL);106}107108Value local_at(int i) const {109Value x = _locals.at(i);110assert(x == NULL || !x->type()->is_double_word() ||111_locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");112return x;113}114115void store_local(int i, Value x) {116// When overwriting local i, check if i - 1 was the start of a117// double word local and kill it.118if (i > 0) {119Value prev = _locals.at(i - 1);120if (prev != NULL && prev->type()->is_double_word()) {121_locals.at_put(i - 1, NULL);122}123}124125_locals.at_put(i, x);126if (x->type()->is_double_word()) {127// hi-word of doubleword value is always NULL128_locals.at_put(i + 1, NULL);129}130}131132// stack access133Value stack_at(int i) const {134Value x = _stack.at(i);135assert(!x->type()->is_double_word() ||136_stack.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");137return x;138}139140Value stack_at_inc(int& i) const {141Value x = stack_at(i);142i += x->type()->size();143return x;144}145146void stack_at_put(int i, Value x) {147_stack.at_put(i, x);148}149150// pinning support151void pin_stack_for_linear_scan();152153// iteration154void values_do(ValueVisitor* f);155156// untyped manipulation (for dup_x1, etc.)157void truncate_stack(int size) { _stack.trunc_to(size); }158void raw_push(Value t) { _stack.push(t); }159Value raw_pop() { return _stack.pop(); }160161// typed manipulation162void ipush(Value t) { _stack.push(check(intTag , t)); }163void fpush(Value t) { _stack.push(check(floatTag , t)); }164void apush(Value t) { _stack.push(check(objectTag , t)); }165void rpush(Value t) { _stack.push(check(addressTag, t)); }166void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(NULL); }167void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(NULL); }168169void push(ValueType* type, Value t) {170switch (type->tag()) {171case intTag : ipush(t); return;172case longTag : lpush(t); return;173case floatTag : fpush(t); return;174case doubleTag : dpush(t); return;175case objectTag : apush(t); return;176case addressTag: rpush(t); return;177default : ShouldNotReachHere(); return;178}179}180181Value ipop() { return check(intTag , _stack.pop()); }182Value fpop() { return check(floatTag , _stack.pop()); }183Value apop() { return check(objectTag , _stack.pop()); }184Value rpop() { return check(addressTag, _stack.pop()); }185Value lpop() { Value h = _stack.pop(); return check(longTag , _stack.pop(), h); }186Value dpop() { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); }187188Value pop(ValueType* type) {189switch (type->tag()) {190case intTag : return ipop();191case longTag : return lpop();192case floatTag : return fpop();193case doubleTag : return dpop();194case objectTag : return apop();195case addressTag: return rpop();196default : ShouldNotReachHere(); return NULL;197}198}199200Values* pop_arguments(int argument_size);201202// locks access203int lock (Value obj);204int unlock();205Value lock_at(int i) const { return _locks->at(i); }206207// SSA form IR support208void setup_phi_for_stack(BlockBegin* b, int index);209void setup_phi_for_local(BlockBegin* b, int index);210211// debugging212void print() PRODUCT_RETURN;213void verify() PRODUCT_RETURN;214};215216217218// Macro definitions for simple iteration of stack and local values of a ValueStack219// The macros can be used like a for-loop. All variables (state, index and value)220// must be defined before the loop.221// When states are nested because of inlining, the stack of the innermost state222// cumulates also the stack of the nested states. In contrast, the locals of all223// states must be iterated each.224// Use the following code pattern to iterate all stack values and all nested local values:225//226// ValueStack* state = ... // state that is iterated227// int index; // current loop index (overwritten in loop)228// Value value; // value at current loop index (overwritten in loop)229//230// for_each_stack_value(state, index, value {231// do something with value and index232// }233//234// for_each_state(state) {235// for_each_local_value(state, index, value) {236// do something with value and index237// }238// }239// as an invariant, state is NULL now240241242// construct a unique variable name with the line number where the macro is used243#define temp_var3(x) temp__ ## x244#define temp_var2(x) temp_var3(x)245#define temp_var temp_var2(__LINE__)246247#define for_each_state(state) \248for (; state != NULL; state = state->caller_state())249250#define for_each_local_value(state, index, value) \251int temp_var = state->locals_size(); \252for (index = 0; \253index < temp_var && (value = state->local_at(index), true); \254index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size())) \255if (value != NULL)256257258#define for_each_stack_value(state, index, value) \259int temp_var = state->stack_size(); \260for (index = 0; \261index < temp_var && (value = state->stack_at(index), true); \262index += value->type()->size())263264265#define for_each_lock_value(state, index, value) \266int temp_var = state->locks_size(); \267for (index = 0; \268index < temp_var && (value = state->lock_at(index), true); \269index++) \270if (value != NULL)271272273// Macro definition for simple iteration of all state values of a ValueStack274// Because the code cannot be executed in a single loop, the code must be passed275// as a macro parameter.276// Use the following code pattern to iterate all stack values and all nested local values:277//278// ValueStack* state = ... // state that is iterated279// for_each_state_value(state, value,280// do something with value (note that this is a macro parameter)281// );282283#define for_each_state_value(v_state, v_value, v_code) \284{ \285int cur_index; \286ValueStack* cur_state = v_state; \287Value v_value; \288for_each_state(cur_state) { \289{ \290for_each_local_value(cur_state, cur_index, v_value) { \291v_code; \292} \293} \294{ \295for_each_stack_value(cur_state, cur_index, v_value) { \296v_code; \297} \298} \299} \300}301302303// Macro definition for simple iteration of all phi functions of a block, i.e all304// phi functions of the ValueStack where the block matches.305// Use the following code pattern to iterate all phi functions of a block:306//307// BlockBegin* block = ... // block that is iterated308// for_each_phi_function(block, phi,309// do something with the phi function phi (note that this is a macro parameter)310// );311312#define for_each_phi_fun(v_block, v_phi, v_code) \313{ \314int cur_index; \315ValueStack* cur_state = v_block->state(); \316Value value; \317{ \318for_each_stack_value(cur_state, cur_index, value) { \319Phi* v_phi = value->as_Phi(); \320if (v_phi != NULL && v_phi->block() == v_block) { \321v_code; \322} \323} \324} \325{ \326for_each_local_value(cur_state, cur_index, value) { \327Phi* v_phi = value->as_Phi(); \328if (v_phi != NULL && v_phi->block() == v_block) { \329v_code; \330} \331} \332} \333}334335#endif // SHARE_C1_C1_VALUESTACK_HPP336337338