1 /*
2 * Copyright (c) 1999, 2025, 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
25 #include "c1/c1_InstructionPrinter.hpp"
26 #include "c1/c1_IR.hpp"
27 #include "c1/c1_ValueStack.hpp"
28
29
30 // Implementation of ValueStack
31
32 ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)
33 : _scope(scope)
34 , _caller_state(caller_state)
35 , _bci(-99)
36 , _kind(Parsing)
37 , _should_reexecute(false)
38 , _locals(scope->method()->max_locals(), scope->method()->max_locals(), nullptr)
39 , _stack(scope->method()->max_stack())
40 , _locks(nullptr)
41 , _force_reexecute(false)
42 {
43 verify();
44 }
45
46 ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci, bool reexecute)
47 : _scope(copy_from->scope())
48 , _caller_state(copy_from->caller_state())
49 , _bci(bci)
50 , _kind(kind)
51 , _should_reexecute(reexecute)
52 , _locals(copy_from->locals_size_for_copy(kind))
53 , _stack(copy_from->stack_size_for_copy(kind))
54 , _locks(copy_from->locks_size() == 0 ? nullptr : new Values(copy_from->locks_size()))
55 , _force_reexecute(false)
56 {
57 switch (kind) {
58 case EmptyExceptionState:
59 case CallerEmptyExceptionState:
60 assert(!Compilation::current()->env()->should_retain_local_variables(), "need locals");
61 // set to all nulls, like clear_locals()
62 for (int i = 0; i < copy_from->locals_size(); ++i) {
63 _locals.append(nullptr);
64 }
65 break;
66 default:
67 _locals.appendAll(©_from->_locals);
68 }
69
70 switch (kind) {
71 case ExceptionState:
72 case EmptyExceptionState:
73 assert(stack_size() == 0, "fix stack_size_for_copy");
74 break;
75 case CallerExceptionState:
76 case CallerEmptyExceptionState:
77 // set to all nulls
78 for (int i = 0; i < copy_from->stack_size(); ++i) {
79 _stack.append(nullptr);
80 }
81 break;
82 default:
83 _stack.appendAll(©_from->_stack);
84 }
85
86 if (copy_from->locks_size() > 0) {
87 _locks->appendAll(copy_from->_locks);
88 }
89
90 verify();
91 }
92
93 int ValueStack::locals_size_for_copy(Kind kind) const {
94 return locals_size();
95 }
96
97 int ValueStack::stack_size_for_copy(Kind kind) const {
98 if (kind != ExceptionState && kind != EmptyExceptionState) {
99 if (kind == Parsing) {
100 // stack will be modified, so reserve enough space to avoid resizing
101 return scope()->method()->max_stack();
102 } else {
103 // stack will not be modified, so do not waste space
104 return stack_size();
105 }
106 }
107 return 0;
108 }
109
110 bool ValueStack::is_same(ValueStack* s) {
111 if (scope() != s->scope()) return false;
112 if (caller_state() != s->caller_state()) return false;
113
114 if (locals_size() != s->locals_size()) return false;
115 if (stack_size() != s->stack_size()) return false;
116 if (locks_size() != s->locks_size()) return false;
117
118 // compare each stack element with the corresponding stack element of s
119 int index;
120 Value value;
121 for_each_stack_value(this, index, value) {
122 if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;
123 }
124 for (int i = 0; i < locks_size(); i++) {
125 value = lock_at(i);
126 if (value != nullptr && value != s->lock_at(i)) {
127 return false;
128 }
129 }
130 return true;
131 }
132
133 void ValueStack::clear_locals() {
134 for (int i = _locals.length() - 1; i >= 0; i--) {
135 _locals.at_put(i, nullptr);
136 }
137 }
138
139
140 void ValueStack::pin_stack_for_linear_scan() {
141 for_each_state_value(this, v,
142 if (v->as_Constant() == nullptr && v->as_Local() == nullptr) {
143 v->pin(Instruction::PinStackForStateSplit);
144 }
145 );
146 }
147
148
149 // apply function to all values of a list; factored out from values_do(f)
150 void ValueStack::apply(const Values& list, ValueVisitor* f) {
151 for (int i = 0; i < list.length(); i++) {
152 Value* va = list.adr_at(i);
153 Value v0 = *va;
154 if (v0 != nullptr && !v0->type()->is_illegal()) {
155 f->visit(va);
156 #ifdef ASSERT
157 Value v1 = *va;
158 assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
159 assert(!v1->type()->is_double_word() || list.at(i + 1) == nullptr, "hi-word of doubleword value must be null");
160 #endif
161 if (v0->type()->is_double_word()) i++;
162 }
163 }
164 }
165
166
167 void ValueStack::values_do(ValueVisitor* f) {
168 ValueStack* state = this;
169 for_each_state(state) {
170 apply(state->_locals, f);
171 apply(state->_stack, f);
172 if (state->_locks != nullptr) {
173 apply(*state->_locks, f);
174 }
175 }
176 }
177
178
179 Values* ValueStack::pop_arguments(int argument_size) {
180 assert(stack_size() >= argument_size, "stack too small or too many arguments");
181 int base = stack_size() - argument_size;
182 Values* args = new Values(argument_size);
183 for (int i = base; i < stack_size();) args->push(stack_at_inc(i));
184 truncate_stack(base);
185 return args;
186 }
187
188
189 int ValueStack::total_locks_size() const {
190 int num_locks = 0;
191 const ValueStack* state = this;
192 for_each_state(state) {
193 num_locks += state->locks_size();
194 }
195 return num_locks;
196 }
197
198 int ValueStack::lock(Value obj) {
199 if (_locks == nullptr) {
200 _locks = new Values();
201 }
202 _locks->push(obj);
203 int num_locks = total_locks_size();
204 scope()->set_min_number_of_locks(num_locks);
205 return num_locks - 1;
206 }
207
208
209 int ValueStack::unlock() {
210 assert(locks_size() > 0, "sanity");
211 _locks->pop();
212 return total_locks_size();
213 }
214
215 void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {
216 assert(stack_at(index)->as_Phi() == nullptr || stack_at(index)->as_Phi()->block() != b, "phi function already created");
217
218 ValueType* t = stack_at(index)->type();
219 Value phi = new Phi(t, b, -index - 1);
220 _stack.at_put(index, phi);
221
222 assert(!t->is_double_word() || _stack.at(index + 1) == nullptr, "hi-word of doubleword value must be null");
223 }
224
225 void ValueStack::setup_phi_for_local(BlockBegin* b, int index) {
226 assert(local_at(index)->as_Phi() == nullptr || local_at(index)->as_Phi()->block() != b, "phi function already created");
227
228 ValueType* t = local_at(index)->type();
229 Value phi = new Phi(t, b, index);
230 store_local(index, phi);
231 }
232
233 #ifndef PRODUCT
234
235 void ValueStack::print() {
236 scope()->method()->print_name();
237 tty->cr();
238 if (stack_is_empty()) {
239 tty->print_cr("empty stack");
240 } else {
241 InstructionPrinter ip;
242 for (int i = 0; i < stack_size();) {
243 tty->print("stack %d ", i);
244 Value t = stack_at_inc(i);
245 if (t == nullptr) {
246 tty->print("null");
247 } else {
248 tty->print("%2d ", i);
249 tty->print("%c%d ", t->type()->tchar(), t->id());
250 ip.print_instr(t);
251 }
252 tty->cr();
253 }
254 }
255 if (!no_active_locks()) {
256 InstructionPrinter ip;
257 for (int i = 0; i < locks_size(); i++) {
258 Value t = lock_at(i);
259 tty->print("lock %2d ", i);
260 if (t == nullptr) {
261 tty->print("this");
262 } else {
263 tty->print("%c%d ", t->type()->tchar(), t->id());
264 ip.print_instr(t);
265 }
266 tty->cr();
267 }
268 }
269 if (locals_size() > 0) {
270 InstructionPrinter ip;
271 for (int i = 0; i < locals_size();) {
272 Value l = _locals.at(i);
273 tty->print("local %d ", i);
274 if (l == nullptr) {
275 tty->print("null");
276 i ++;
277 } else {
278 tty->print("%c%d ", l->type()->tchar(), l->id());
279 ip.print_instr(l);
280 if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;
281 }
282 tty->cr();
283 }
284 }
285
286 if (caller_state() != nullptr) {
287 caller_state()->print();
288 }
289 }
290
291
292 void ValueStack::verify() {
293 assert(scope() != nullptr, "scope must exist");
294 if (caller_state() != nullptr) {
295 assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");
296 caller_state()->verify();
297 }
298
299 if (kind() == Parsing) {
300 assert(bci() == -99, "bci not defined during parsing");
301 } else {
302 assert(bci() >= -1, "bci out of range");
303 assert(bci() < scope()->method()->code_size(), "bci out of range");
304 assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");
305 assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");
306 }
307
308 int i;
309 for (i = 0; i < stack_size(); i++) {
310 Value v = _stack.at(i);
311 if (kind() == empty_exception_kind(true /* caller */)) {
312 assert(v == nullptr, "should be empty");
313 } else if (v == nullptr) {
314 assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are null on stack");
315 } else if (v->type()->is_double_word()) {
316 assert(_stack.at(i + 1) == nullptr, "hi-word must be null");
317 }
318 }
319
320 for (i = 0; i < locals_size(); i++) {
321 Value v = _locals.at(i);
322 if (kind() == EmptyExceptionState) {
323 assert(v == nullptr, "should be empty");
324 } else if (v != nullptr && v->type()->is_double_word()) {
325 assert(_locals.at(i + 1) == nullptr, "hi-word must be null");
326 }
327 }
328
329 for_each_state_value(this, v,
330 assert(v != nullptr, "just test if state-iteration succeeds");
331 );
332 }
333 #endif // PRODUCT