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>
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 */
25 class ValueStack: public CompilationResourceObj {
26 public:
27 enum Kind {
28 Parsing, // During abstract interpretation in GraphBuilder
29 CallerState, // Caller state when inlining
30 StateBefore, // Before before execution of instruction
31 StateAfter, // After execution of instruction
32 ExceptionState, // Exception handling of instruction
33 EmptyExceptionState, // Exception handling of instructions not covered by an xhandler
34 BlockBeginState // State of BlockBegin instruction with phi functions of this block
35 };
37 private:
38 IRScope* _scope; // the enclosing scope
39 ValueStack* _caller_state;
40 int _bci;
41 Kind _kind;
43 Values _locals; // the locals
44 Values _stack; // the expression stack
45 Values _locks; // the monitor stack (holding the locked values)
47 Value check(ValueTag tag, Value t) {
48 assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond");
49 return t;
50 }
52 Value check(ValueTag tag, Value t, Value h) {
53 assert(h == NULL, "hi-word of doubleword value must be NULL");
54 return check(tag, t);
55 }
57 // helper routine
58 static void apply(Values list, ValueVisitor* f);
60 // for simplified copying
61 ValueStack(ValueStack* copy_from, Kind kind, int bci);
63 public:
64 // creation
65 ValueStack(IRScope* scope, ValueStack* caller_state);
67 ValueStack* copy() { return new ValueStack(this, _kind, _bci); }
68 ValueStack* copy(Kind new_kind, int new_bci) { return new ValueStack(this, new_kind, new_bci); }
69 ValueStack* copy_for_parsing() { return new ValueStack(this, Parsing, -99); }
71 void set_caller_state(ValueStack* s) { assert(kind() == EmptyExceptionState, "only EmptyExceptionStates can be modified"); _caller_state = s; }
73 bool is_same(ValueStack* s); // returns true if this & s's types match (w/o checking locals)
75 // accessors
76 IRScope* scope() const { return _scope; }
77 ValueStack* caller_state() const { return _caller_state; }
78 int bci() const { return _bci; }
79 Kind kind() const { return _kind; }
81 int locals_size() const { return _locals.length(); }
82 int stack_size() const { return _stack.length(); }
83 int locks_size() const { return _locks.length(); }
84 bool stack_is_empty() const { return _stack.is_empty(); }
85 bool no_active_locks() const { return _locks.is_empty(); }
86 int total_locks_size() const;
88 // locals access
89 void clear_locals(); // sets all locals to NULL;
91 void invalidate_local(int i) {
92 assert(_locals.at(i)->type()->is_single_word() ||
93 _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
94 _locals.at_put(i, NULL);
95 }
97 Value local_at(int i) const {
98 Value x = _locals.at(i);
99 assert(x == NULL || x->type()->is_single_word() ||
100 _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
101 return x;
102 }
104 void store_local(int i, Value x) {
105 // When overwriting local i, check if i - 1 was the start of a
106 // double word local and kill it.
107 if (i > 0) {
108 Value prev = _locals.at(i - 1);
109 if (prev != NULL && prev->type()->is_double_word()) {
110 _locals.at_put(i - 1, NULL);
111 }
112 }
114 _locals.at_put(i, x);
115 if (x->type()->is_double_word()) {
116 // hi-word of doubleword value is always NULL
117 _locals.at_put(i + 1, NULL);
118 }
119 }
121 // stack access
122 Value stack_at(int i) const {
123 Value x = _stack.at(i);
124 assert(x->type()->is_single_word() ||
125 _stack.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
126 return x;
127 }
129 Value stack_at_inc(int& i) const {
130 Value x = stack_at(i);
131 i += x->type()->size();
132 return x;
133 }
135 // pinning support
136 void pin_stack_for_linear_scan();
138 // iteration
139 void values_do(ValueVisitor* f);
141 // untyped manipulation (for dup_x1, etc.)
142 void truncate_stack(int size) { _stack.trunc_to(size); }
143 void raw_push(Value t) { _stack.push(t); }
144 Value raw_pop() { return _stack.pop(); }
146 // typed manipulation
147 void ipush(Value t) { _stack.push(check(intTag , t)); }
148 void fpush(Value t) { _stack.push(check(floatTag , t)); }
149 void apush(Value t) { _stack.push(check(objectTag , t)); }
150 void rpush(Value t) { _stack.push(check(addressTag, t)); }
151 void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(NULL); }
152 void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(NULL); }
154 void push(ValueType* type, Value t) {
155 switch (type->tag()) {
156 case intTag : ipush(t); return;
157 case longTag : lpush(t); return;
158 case floatTag : fpush(t); return;
159 case doubleTag : dpush(t); return;
160 case objectTag : apush(t); return;
161 case addressTag: rpush(t); return;
162 }
163 ShouldNotReachHere();
164 }
166 Value ipop() { return check(intTag , _stack.pop()); }
167 Value fpop() { return check(floatTag , _stack.pop()); }
168 Value apop() { return check(objectTag , _stack.pop()); }
169 Value rpop() { return check(addressTag, _stack.pop()); }
170 Value lpop() { Value h = _stack.pop(); return check(longTag , _stack.pop(), h); }
171 Value dpop() { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); }
173 Value pop(ValueType* type) {
174 switch (type->tag()) {
175 case intTag : return ipop();
176 case longTag : return lpop();
177 case floatTag : return fpop();
178 case doubleTag : return dpop();
179 case objectTag : return apop();
180 case addressTag: return rpop();
181 }
182 ShouldNotReachHere();
183 return NULL;
184 }
186 Values* pop_arguments(int argument_size);
188 // locks access
189 int lock (Value obj);
190 int unlock();
191 Value lock_at(int i) const { return _locks.at(i); }
193 // SSA form IR support
194 void setup_phi_for_stack(BlockBegin* b, int index);
195 void setup_phi_for_local(BlockBegin* b, int index);
197 // debugging
198 void print() PRODUCT_RETURN;
199 void verify() PRODUCT_RETURN;
200 };
204 // Macro definitions for simple iteration of stack and local values of a ValueStack
205 // The macros can be used like a for-loop. All variables (state, index and value)
206 // must be defined before the loop.
207 // When states are nested because of inlining, the stack of the innermost state
208 // cumulates also the stack of the nested states. In contrast, the locals of all
209 // states must be iterated each.
210 // Use the following code pattern to iterate all stack values and all nested local values:
211 //
212 // ValueStack* state = ... // state that is iterated
213 // int index; // current loop index (overwritten in loop)
214 // Value value; // value at current loop index (overwritten in loop)
215 //
216 // for_each_stack_value(state, index, value {
217 // do something with value and index
218 // }
219 //
220 // for_each_state(state) {
221 // for_each_local_value(state, index, value) {
222 // do something with value and index
223 // }
224 // }
225 // as an invariant, state is NULL now
228 // construct a unique variable name with the line number where the macro is used
229 #define temp_var3(x) temp__ ## x
230 #define temp_var2(x) temp_var3(x)
231 #define temp_var temp_var2(__LINE__)
233 #define for_each_state(state) \
234 for (; state != NULL; state = state->caller_state())
236 #define for_each_local_value(state, index, value) \
237 int temp_var = state->locals_size(); \
238 for (index = 0; \
239 index < temp_var && (value = state->local_at(index), true); \
240 index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size())) \
241 if (value != NULL)
244 #define for_each_stack_value(state, index, value) \
245 int temp_var = state->stack_size(); \
246 for (index = 0; \
247 index < temp_var && (value = state->stack_at(index), true); \
248 index += value->type()->size())
251 #define for_each_lock_value(state, index, value) \
252 int temp_var = state->locks_size(); \
253 for (index = 0; \
254 index < temp_var && (value = state->lock_at(index), true); \
255 index++) \
256 if (value != NULL)
259 // Macro definition for simple iteration of all state values of a ValueStack
260 // Because the code cannot be executed in a single loop, the code must be passed
261 // as a macro parameter.
262 // Use the following code pattern to iterate all stack values and all nested local values:
263 //
264 // ValueStack* state = ... // state that is iterated
265 // for_each_state_value(state, value,
266 // do something with value (note that this is a macro parameter)
267 // );
269 #define for_each_state_value(v_state, v_value, v_code) \
270 { \
271 int cur_index; \
272 ValueStack* cur_state = v_state; \
273 Value v_value; \
274 for_each_state(cur_state) { \
275 { \
276 for_each_local_value(cur_state, cur_index, v_value) { \
277 v_code; \
278 } \
279 } \
280 { \
281 for_each_stack_value(cur_state, cur_index, v_value) { \
282 v_code; \
283 } \
284 } \
285 } \
286 }
289 // Macro definition for simple iteration of all phif functions of a block, i.e all
290 // phi functions of the ValueStack where the block matches.
291 // Use the following code pattern to iterate all phi functions of a block:
292 //
293 // BlockBegin* block = ... // block that is iterated
294 // for_each_phi_function(block, phi,
295 // do something with the phi function phi (note that this is a macro parameter)
296 // );
298 #define for_each_phi_fun(v_block, v_phi, v_code) \
299 { \
300 int cur_index; \
301 ValueStack* cur_state = v_block->state(); \
302 Value value; \
303 { \
304 for_each_stack_value(cur_state, cur_index, value) { \
305 Phi* v_phi = value->as_Phi(); \
306 if (v_phi != NULL && v_phi->block() == v_block) { \
307 v_code; \
308 } \
309 } \
310 } \
311 { \
312 for_each_local_value(cur_state, cur_index, value) { \
313 Phi* v_phi = value->as_Phi(); \
314 if (v_phi != NULL && v_phi->block() == v_block) { \
315 v_code; \
316 } \
317 } \
318 } \
319 }