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