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