src/share/vm/c1/c1_ValueStack.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 4153
b9a9ed0f8eeb
parent 0
f90c822e73f8
child 10015
eb7ce841ccec
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_C1_C1_VALUESTACK_HPP
aoqi@0 26 #define SHARE_VM_C1_C1_VALUESTACK_HPP
aoqi@0 27
aoqi@0 28 #include "c1/c1_Instruction.hpp"
aoqi@0 29
aoqi@0 30 class ValueStack: public CompilationResourceObj {
aoqi@0 31 public:
aoqi@0 32 enum Kind {
aoqi@0 33 Parsing, // During abstract interpretation in GraphBuilder
aoqi@0 34 CallerState, // Caller state when inlining
aoqi@0 35 StateBefore, // Before before execution of instruction
aoqi@0 36 StateAfter, // After execution of instruction
aoqi@0 37 ExceptionState, // Exception handling of instruction
aoqi@0 38 EmptyExceptionState, // Exception handling of instructions not covered by an xhandler
aoqi@0 39 BlockBeginState // State of BlockBegin instruction with phi functions of this block
aoqi@0 40 };
aoqi@0 41
aoqi@0 42 private:
aoqi@0 43 IRScope* _scope; // the enclosing scope
aoqi@0 44 ValueStack* _caller_state;
aoqi@0 45 int _bci;
aoqi@0 46 Kind _kind;
aoqi@0 47
aoqi@0 48 Values _locals; // the locals
aoqi@0 49 Values _stack; // the expression stack
aoqi@0 50 Values _locks; // the monitor stack (holding the locked values)
aoqi@0 51
aoqi@0 52 Value check(ValueTag tag, Value t) {
aoqi@0 53 assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond");
aoqi@0 54 return t;
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 Value check(ValueTag tag, Value t, Value h) {
aoqi@0 58 assert(h == NULL, "hi-word of doubleword value must be NULL");
aoqi@0 59 return check(tag, t);
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 // helper routine
aoqi@0 63 static void apply(Values list, ValueVisitor* f);
aoqi@0 64
aoqi@0 65 // for simplified copying
aoqi@0 66 ValueStack(ValueStack* copy_from, Kind kind, int bci);
aoqi@0 67
aoqi@0 68 public:
aoqi@0 69 // creation
aoqi@0 70 ValueStack(IRScope* scope, ValueStack* caller_state);
aoqi@0 71
aoqi@0 72 ValueStack* copy() { return new ValueStack(this, _kind, _bci); }
aoqi@0 73 ValueStack* copy(Kind new_kind, int new_bci) { return new ValueStack(this, new_kind, new_bci); }
aoqi@0 74 ValueStack* copy_for_parsing() { return new ValueStack(this, Parsing, -99); }
aoqi@0 75
aoqi@0 76 void set_caller_state(ValueStack* s) {
aoqi@0 77 assert(kind() == EmptyExceptionState ||
aoqi@0 78 (Compilation::current()->env()->jvmti_can_access_local_variables() && kind() == ExceptionState),
aoqi@0 79 "only EmptyExceptionStates can be modified");
aoqi@0 80 _caller_state = s;
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 bool is_same(ValueStack* s); // returns true if this & s's types match (w/o checking locals)
aoqi@0 84
aoqi@0 85 // accessors
aoqi@0 86 IRScope* scope() const { return _scope; }
aoqi@0 87 ValueStack* caller_state() const { return _caller_state; }
aoqi@0 88 int bci() const { return _bci; }
aoqi@0 89 Kind kind() const { return _kind; }
aoqi@0 90
aoqi@0 91 int locals_size() const { return _locals.length(); }
aoqi@0 92 int stack_size() const { return _stack.length(); }
aoqi@0 93 int locks_size() const { return _locks.length(); }
aoqi@0 94 bool stack_is_empty() const { return _stack.is_empty(); }
aoqi@0 95 bool no_active_locks() const { return _locks.is_empty(); }
aoqi@0 96 int total_locks_size() const;
aoqi@0 97
aoqi@0 98 // locals access
aoqi@0 99 void clear_locals(); // sets all locals to NULL;
aoqi@0 100
aoqi@0 101 void invalidate_local(int i) {
aoqi@0 102 assert(_locals.at(i)->type()->is_single_word() ||
aoqi@0 103 _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
aoqi@0 104 _locals.at_put(i, NULL);
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 Value local_at(int i) const {
aoqi@0 108 Value x = _locals.at(i);
aoqi@0 109 assert(x == NULL || x->type()->is_single_word() ||
aoqi@0 110 _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
aoqi@0 111 return x;
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 void store_local(int i, Value x) {
aoqi@0 115 // When overwriting local i, check if i - 1 was the start of a
aoqi@0 116 // double word local and kill it.
aoqi@0 117 if (i > 0) {
aoqi@0 118 Value prev = _locals.at(i - 1);
aoqi@0 119 if (prev != NULL && prev->type()->is_double_word()) {
aoqi@0 120 _locals.at_put(i - 1, NULL);
aoqi@0 121 }
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 _locals.at_put(i, x);
aoqi@0 125 if (x->type()->is_double_word()) {
aoqi@0 126 // hi-word of doubleword value is always NULL
aoqi@0 127 _locals.at_put(i + 1, NULL);
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 // stack access
aoqi@0 132 Value stack_at(int i) const {
aoqi@0 133 Value x = _stack.at(i);
aoqi@0 134 assert(x->type()->is_single_word() ||
aoqi@0 135 _stack.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
aoqi@0 136 return x;
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 Value stack_at_inc(int& i) const {
aoqi@0 140 Value x = stack_at(i);
aoqi@0 141 i += x->type()->size();
aoqi@0 142 return x;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 void stack_at_put(int i, Value x) {
aoqi@0 146 _stack.at_put(i, x);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 // pinning support
aoqi@0 150 void pin_stack_for_linear_scan();
aoqi@0 151
aoqi@0 152 // iteration
aoqi@0 153 void values_do(ValueVisitor* f);
aoqi@0 154
aoqi@0 155 // untyped manipulation (for dup_x1, etc.)
aoqi@0 156 void truncate_stack(int size) { _stack.trunc_to(size); }
aoqi@0 157 void raw_push(Value t) { _stack.push(t); }
aoqi@0 158 Value raw_pop() { return _stack.pop(); }
aoqi@0 159
aoqi@0 160 // typed manipulation
aoqi@0 161 void ipush(Value t) { _stack.push(check(intTag , t)); }
aoqi@0 162 void fpush(Value t) { _stack.push(check(floatTag , t)); }
aoqi@0 163 void apush(Value t) { _stack.push(check(objectTag , t)); }
aoqi@0 164 void rpush(Value t) { _stack.push(check(addressTag, t)); }
aoqi@0 165 void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(NULL); }
aoqi@0 166 void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(NULL); }
aoqi@0 167
aoqi@0 168 void push(ValueType* type, Value t) {
aoqi@0 169 switch (type->tag()) {
aoqi@0 170 case intTag : ipush(t); return;
aoqi@0 171 case longTag : lpush(t); return;
aoqi@0 172 case floatTag : fpush(t); return;
aoqi@0 173 case doubleTag : dpush(t); return;
aoqi@0 174 case objectTag : apush(t); return;
aoqi@0 175 case addressTag: rpush(t); return;
aoqi@0 176 }
aoqi@0 177 ShouldNotReachHere();
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 Value ipop() { return check(intTag , _stack.pop()); }
aoqi@0 181 Value fpop() { return check(floatTag , _stack.pop()); }
aoqi@0 182 Value apop() { return check(objectTag , _stack.pop()); }
aoqi@0 183 Value rpop() { return check(addressTag, _stack.pop()); }
aoqi@0 184 Value lpop() { Value h = _stack.pop(); return check(longTag , _stack.pop(), h); }
aoqi@0 185 Value dpop() { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); }
aoqi@0 186
aoqi@0 187 Value pop(ValueType* type) {
aoqi@0 188 switch (type->tag()) {
aoqi@0 189 case intTag : return ipop();
aoqi@0 190 case longTag : return lpop();
aoqi@0 191 case floatTag : return fpop();
aoqi@0 192 case doubleTag : return dpop();
aoqi@0 193 case objectTag : return apop();
aoqi@0 194 case addressTag: return rpop();
aoqi@0 195 }
aoqi@0 196 ShouldNotReachHere();
aoqi@0 197 return NULL;
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 Values* pop_arguments(int argument_size);
aoqi@0 201
aoqi@0 202 // locks access
aoqi@0 203 int lock (Value obj);
aoqi@0 204 int unlock();
aoqi@0 205 Value lock_at(int i) const { return _locks.at(i); }
aoqi@0 206
aoqi@0 207 // SSA form IR support
aoqi@0 208 void setup_phi_for_stack(BlockBegin* b, int index);
aoqi@0 209 void setup_phi_for_local(BlockBegin* b, int index);
aoqi@0 210
aoqi@0 211 // debugging
aoqi@0 212 void print() PRODUCT_RETURN;
aoqi@0 213 void verify() PRODUCT_RETURN;
aoqi@0 214 };
aoqi@0 215
aoqi@0 216
aoqi@0 217
aoqi@0 218 // Macro definitions for simple iteration of stack and local values of a ValueStack
aoqi@0 219 // The macros can be used like a for-loop. All variables (state, index and value)
aoqi@0 220 // must be defined before the loop.
aoqi@0 221 // When states are nested because of inlining, the stack of the innermost state
aoqi@0 222 // cumulates also the stack of the nested states. In contrast, the locals of all
aoqi@0 223 // states must be iterated each.
aoqi@0 224 // Use the following code pattern to iterate all stack values and all nested local values:
aoqi@0 225 //
aoqi@0 226 // ValueStack* state = ... // state that is iterated
aoqi@0 227 // int index; // current loop index (overwritten in loop)
aoqi@0 228 // Value value; // value at current loop index (overwritten in loop)
aoqi@0 229 //
aoqi@0 230 // for_each_stack_value(state, index, value {
aoqi@0 231 // do something with value and index
aoqi@0 232 // }
aoqi@0 233 //
aoqi@0 234 // for_each_state(state) {
aoqi@0 235 // for_each_local_value(state, index, value) {
aoqi@0 236 // do something with value and index
aoqi@0 237 // }
aoqi@0 238 // }
aoqi@0 239 // as an invariant, state is NULL now
aoqi@0 240
aoqi@0 241
aoqi@0 242 // construct a unique variable name with the line number where the macro is used
aoqi@0 243 #define temp_var3(x) temp__ ## x
aoqi@0 244 #define temp_var2(x) temp_var3(x)
aoqi@0 245 #define temp_var temp_var2(__LINE__)
aoqi@0 246
aoqi@0 247 #define for_each_state(state) \
aoqi@0 248 for (; state != NULL; state = state->caller_state())
aoqi@0 249
aoqi@0 250 #define for_each_local_value(state, index, value) \
aoqi@0 251 int temp_var = state->locals_size(); \
aoqi@0 252 for (index = 0; \
aoqi@0 253 index < temp_var && (value = state->local_at(index), true); \
aoqi@0 254 index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size())) \
aoqi@0 255 if (value != NULL)
aoqi@0 256
aoqi@0 257
aoqi@0 258 #define for_each_stack_value(state, index, value) \
aoqi@0 259 int temp_var = state->stack_size(); \
aoqi@0 260 for (index = 0; \
aoqi@0 261 index < temp_var && (value = state->stack_at(index), true); \
aoqi@0 262 index += value->type()->size())
aoqi@0 263
aoqi@0 264
aoqi@0 265 #define for_each_lock_value(state, index, value) \
aoqi@0 266 int temp_var = state->locks_size(); \
aoqi@0 267 for (index = 0; \
aoqi@0 268 index < temp_var && (value = state->lock_at(index), true); \
aoqi@0 269 index++) \
aoqi@0 270 if (value != NULL)
aoqi@0 271
aoqi@0 272
aoqi@0 273 // Macro definition for simple iteration of all state values of a ValueStack
aoqi@0 274 // Because the code cannot be executed in a single loop, the code must be passed
aoqi@0 275 // as a macro parameter.
aoqi@0 276 // Use the following code pattern to iterate all stack values and all nested local values:
aoqi@0 277 //
aoqi@0 278 // ValueStack* state = ... // state that is iterated
aoqi@0 279 // for_each_state_value(state, value,
aoqi@0 280 // do something with value (note that this is a macro parameter)
aoqi@0 281 // );
aoqi@0 282
aoqi@0 283 #define for_each_state_value(v_state, v_value, v_code) \
aoqi@0 284 { \
aoqi@0 285 int cur_index; \
aoqi@0 286 ValueStack* cur_state = v_state; \
aoqi@0 287 Value v_value; \
aoqi@0 288 for_each_state(cur_state) { \
aoqi@0 289 { \
aoqi@0 290 for_each_local_value(cur_state, cur_index, v_value) { \
aoqi@0 291 v_code; \
aoqi@0 292 } \
aoqi@0 293 } \
aoqi@0 294 { \
aoqi@0 295 for_each_stack_value(cur_state, cur_index, v_value) { \
aoqi@0 296 v_code; \
aoqi@0 297 } \
aoqi@0 298 } \
aoqi@0 299 } \
aoqi@0 300 }
aoqi@0 301
aoqi@0 302
aoqi@0 303 // Macro definition for simple iteration of all phif functions of a block, i.e all
aoqi@0 304 // phi functions of the ValueStack where the block matches.
aoqi@0 305 // Use the following code pattern to iterate all phi functions of a block:
aoqi@0 306 //
aoqi@0 307 // BlockBegin* block = ... // block that is iterated
aoqi@0 308 // for_each_phi_function(block, phi,
aoqi@0 309 // do something with the phi function phi (note that this is a macro parameter)
aoqi@0 310 // );
aoqi@0 311
aoqi@0 312 #define for_each_phi_fun(v_block, v_phi, v_code) \
aoqi@0 313 { \
aoqi@0 314 int cur_index; \
aoqi@0 315 ValueStack* cur_state = v_block->state(); \
aoqi@0 316 Value value; \
aoqi@0 317 { \
aoqi@0 318 for_each_stack_value(cur_state, cur_index, value) { \
aoqi@0 319 Phi* v_phi = value->as_Phi(); \
aoqi@0 320 if (v_phi != NULL && v_phi->block() == v_block) { \
aoqi@0 321 v_code; \
aoqi@0 322 } \
aoqi@0 323 } \
aoqi@0 324 } \
aoqi@0 325 { \
aoqi@0 326 for_each_local_value(cur_state, cur_index, value) { \
aoqi@0 327 Phi* v_phi = value->as_Phi(); \
aoqi@0 328 if (v_phi != NULL && v_phi->block() == v_block) { \
aoqi@0 329 v_code; \
aoqi@0 330 } \
aoqi@0 331 } \
aoqi@0 332 } \
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 #endif // SHARE_VM_C1_C1_VALUESTACK_HPP

mercurial