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