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 #include "incls/_precompiled.incl"
26 #include "incls/_c1_ValueStack.cpp.incl"
29 // Implementation of ValueStack
31 ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)
32 : _scope(scope)
33 , _caller_state(caller_state)
34 , _bci(-99)
35 , _kind(Parsing)
36 , _locals(scope->method()->max_locals(), NULL)
37 , _stack(scope->method()->max_stack())
38 , _locks()
39 {
40 verify();
41 }
44 ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci)
45 : _scope(copy_from->scope())
46 , _caller_state(copy_from->caller_state())
47 , _bci(bci)
48 , _kind(kind)
49 , _locals()
50 , _stack()
51 , _locks(copy_from->locks_size())
52 {
53 assert(kind != EmptyExceptionState || !Compilation::current()->env()->jvmti_can_access_local_variables(), "need locals");
54 if (kind != EmptyExceptionState) {
55 // only allocate space if we need to copy the locals-array
56 _locals = Values(copy_from->locals_size());
57 _locals.appendAll(©_from->_locals);
58 }
60 if (kind != ExceptionState && kind != EmptyExceptionState) {
61 if (kind == Parsing) {
62 // stack will be modified, so reserve enough space to avoid resizing
63 _stack = Values(scope()->method()->max_stack());
64 } else {
65 // stack will not be modified, so do not waste space
66 _stack = Values(copy_from->stack_size());
67 }
68 _stack.appendAll(©_from->_stack);
69 }
71 _locks.appendAll(©_from->_locks);
73 verify();
74 }
77 bool ValueStack::is_same(ValueStack* s) {
78 if (scope() != s->scope()) return false;
79 if (caller_state() != s->caller_state()) return false;
81 if (locals_size() != s->locals_size()) return false;
82 if (stack_size() != s->stack_size()) return false;
83 if (locks_size() != s->locks_size()) return false;
85 // compare each stack element with the corresponding stack element of s
86 int index;
87 Value value;
88 for_each_stack_value(this, index, value) {
89 if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;
90 }
91 for_each_lock_value(this, index, value) {
92 if (value != s->lock_at(index)) return false;
93 }
94 return true;
95 }
97 void ValueStack::clear_locals() {
98 for (int i = _locals.length() - 1; i >= 0; i--) {
99 _locals.at_put(i, NULL);
100 }
101 }
104 void ValueStack::pin_stack_for_linear_scan() {
105 for_each_state_value(this, v,
106 if (v->as_Constant() == NULL && v->as_Local() == NULL) {
107 v->pin(Instruction::PinStackForStateSplit);
108 }
109 );
110 }
113 // apply function to all values of a list; factored out from values_do(f)
114 void ValueStack::apply(Values list, ValueVisitor* f) {
115 for (int i = 0; i < list.length(); i++) {
116 Value* va = list.adr_at(i);
117 Value v0 = *va;
118 if (v0 != NULL && !v0->type()->is_illegal()) {
119 f->visit(va);
120 #ifdef ASSERT
121 Value v1 = *va;
122 assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
123 assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
124 #endif
125 if (v0->type()->is_double_word()) i++;
126 }
127 }
128 }
131 void ValueStack::values_do(ValueVisitor* f) {
132 ValueStack* state = this;
133 for_each_state(state) {
134 apply(state->_locals, f);
135 apply(state->_stack, f);
136 apply(state->_locks, f);
137 }
138 }
141 Values* ValueStack::pop_arguments(int argument_size) {
142 assert(stack_size() >= argument_size, "stack too small or too many arguments");
143 int base = stack_size() - argument_size;
144 Values* args = new Values(argument_size);
145 for (int i = base; i < stack_size();) args->push(stack_at_inc(i));
146 truncate_stack(base);
147 return args;
148 }
151 int ValueStack::total_locks_size() const {
152 int num_locks = 0;
153 const ValueStack* state = this;
154 for_each_state(state) {
155 num_locks += state->locks_size();
156 }
157 return num_locks;
158 }
160 int ValueStack::lock(Value obj) {
161 _locks.push(obj);
162 int num_locks = total_locks_size();
163 scope()->set_min_number_of_locks(num_locks);
164 return num_locks - 1;
165 }
168 int ValueStack::unlock() {
169 _locks.pop();
170 return total_locks_size();
171 }
174 void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {
175 assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created");
177 ValueType* t = stack_at(index)->type();
178 Value phi = new Phi(t, b, -index - 1);
179 _stack[index] = phi;
181 assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL");
182 }
184 void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {
185 assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created");
187 ValueType* t = local_at(index)->type();
188 Value phi = new Phi(t, b, index);
189 store_local(index, phi);
190 }
192 #ifndef PRODUCT
194 void ValueStack::print() {
195 scope()->method()->print_name();
196 if (stack_is_empty()) {
197 tty->print_cr("empty stack");
198 } else {
199 InstructionPrinter ip;
200 for (int i = 0; i < stack_size();) {
201 Value t = stack_at_inc(i);
202 tty->print("%2d ", i);
203 tty->print("%c%d ", t->type()->tchar(), t->id());
204 ip.print_instr(t);
205 tty->cr();
206 }
207 }
208 if (!no_active_locks()) {
209 InstructionPrinter ip;
210 for (int i = 0; i < locks_size(); i++) {
211 Value t = lock_at(i);
212 tty->print("lock %2d ", i);
213 if (t == NULL) {
214 tty->print("this");
215 } else {
216 tty->print("%c%d ", t->type()->tchar(), t->id());
217 ip.print_instr(t);
218 }
219 tty->cr();
220 }
221 }
222 if (locals_size() > 0) {
223 InstructionPrinter ip;
224 for (int i = 0; i < locals_size();) {
225 Value l = _locals[i];
226 tty->print("local %d ", i);
227 if (l == NULL) {
228 tty->print("null");
229 i ++;
230 } else {
231 tty->print("%c%d ", l->type()->tchar(), l->id());
232 ip.print_instr(l);
233 if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;
234 }
235 tty->cr();
236 }
237 }
239 if (caller_state() != NULL) {
240 caller_state()->print();
241 }
242 }
245 void ValueStack::verify() {
246 assert(scope() != NULL, "scope must exist");
247 if (caller_state() != NULL) {
248 assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");
249 caller_state()->verify();
250 }
252 if (kind() == Parsing) {
253 assert(bci() == -99, "bci not defined during parsing");
254 } else {
255 assert(bci() >= -1, "bci out of range");
256 assert(bci() < scope()->method()->code_size(), "bci out of range");
257 assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");
258 assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");
259 }
261 int i;
262 for (i = 0; i < stack_size(); i++) {
263 Value v = _stack.at(i);
264 if (v == NULL) {
265 assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack");
266 } else if (v->type()->is_double_word()) {
267 assert(_stack.at(i + 1) == NULL, "hi-word must be NULL");
268 }
269 }
271 for (i = 0; i < locals_size(); i++) {
272 Value v = _locals.at(i);
273 if (v != NULL && v->type()->is_double_word()) {
274 assert(_locals.at(i + 1) == NULL, "hi-word must be NULL");
275 }
276 }
278 for_each_state_value(this, v,
279 assert(v != NULL, "just test if state-iteration succeeds");
280 );
281 }
282 #endif // PRODUCT