1 /*
  2  * Copyright (c) 1999, 2024, 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 "precompiled.hpp"
 26 #include "c1/c1_IR.hpp"
 27 #include "c1/c1_InstructionPrinter.hpp"
 28 #include "c1/c1_ValueStack.hpp"
 29 
 30 
 31 // Implementation of ValueStack
 32 
 33 ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)
 34 : _scope(scope)
 35 , _caller_state(caller_state)
 36 , _bci(-99)
 37 , _kind(Parsing)
 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)
 47   : _scope(copy_from->scope())
 48   , _caller_state(copy_from->caller_state())
 49   , _bci(bci)
 50   , _kind(kind)
 51   , _locals(copy_from->locals_size_for_copy(kind))
 52   , _stack(copy_from->stack_size_for_copy(kind))
 53   , _locks(copy_from->locks_size() == 0 ? nullptr : new Values(copy_from->locks_size()))
 54   , _force_reexecute(false)
 55 {
 56   switch (kind) {
 57   case EmptyExceptionState:
 58   case CallerEmptyExceptionState:
 59     assert(!Compilation::current()->env()->should_retain_local_variables(), "need locals");
 60     // set to all nulls, like clear_locals()
 61     for (int i = 0; i < copy_from->locals_size(); ++i) {
 62       _locals.append(nullptr);
 63     }
 64     break;
 65   default:
 66     _locals.appendAll(&copy_from->_locals);
 67   }
 68 
 69   switch (kind) {
 70   case ExceptionState:
 71   case EmptyExceptionState:
 72     assert(stack_size() == 0, "fix stack_size_for_copy");
 73     break;
 74   case CallerExceptionState:
 75   case CallerEmptyExceptionState:
 76     // set to all nulls
 77     for (int i = 0; i < copy_from->stack_size(); ++i) {
 78       _stack.append(nullptr);
 79     }
 80     break;
 81   default:
 82     _stack.appendAll(&copy_from->_stack);
 83   }
 84 
 85   if (copy_from->locks_size() > 0) {
 86     _locks->appendAll(copy_from->_locks);
 87   }
 88 
 89   verify();
 90 }
 91 
 92 int ValueStack::locals_size_for_copy(Kind kind) const {
 93   return locals_size();
 94 }
 95 
 96 int ValueStack::stack_size_for_copy(Kind kind) const {
 97   if (kind != ExceptionState && kind != EmptyExceptionState) {
 98     if (kind == Parsing) {
 99       // stack will be modified, so reserve enough space to avoid resizing
100       return scope()->method()->max_stack();
101     } else {
102       // stack will not be modified, so do not waste space
103       return stack_size();
104     }
105   }
106   return 0;
107 }
108 
109 bool ValueStack::is_same(ValueStack* s) {
110   if (scope() != s->scope()) return false;
111   if (caller_state() != s->caller_state()) return false;
112 
113   if (locals_size() != s->locals_size()) return false;
114   if (stack_size() != s->stack_size()) return false;
115   if (locks_size() != s->locks_size()) return false;
116 
117   // compare each stack element with the corresponding stack element of s
118   int index;
119   Value value;
120   for_each_stack_value(this, index, value) {
121     if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;
122   }
123   for (int i = 0; i < locks_size(); i++) {
124     value = lock_at(i);
125     if (value != nullptr && value != s->lock_at(i)) {
126       return false;
127     }
128   }
129   return true;
130 }
131 
132 void ValueStack::clear_locals() {
133   for (int i = _locals.length() - 1; i >= 0; i--) {
134     _locals.at_put(i, nullptr);
135   }
136 }
137 
138 
139 void ValueStack::pin_stack_for_linear_scan() {
140   for_each_state_value(this, v,
141     if (v->as_Constant() == nullptr && v->as_Local() == nullptr) {
142       v->pin(Instruction::PinStackForStateSplit);
143     }
144   );
145 }
146 
147 
148 // apply function to all values of a list; factored out from values_do(f)
149 void ValueStack::apply(const Values& list, ValueVisitor* f) {
150   for (int i = 0; i < list.length(); i++) {
151     Value* va = list.adr_at(i);
152     Value v0 = *va;
153     if (v0 != nullptr && !v0->type()->is_illegal()) {
154       f->visit(va);
155 #ifdef ASSERT
156       Value v1 = *va;
157       assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
158       assert(!v1->type()->is_double_word() || list.at(i + 1) == nullptr, "hi-word of doubleword value must be null");
159 #endif
160       if (v0->type()->is_double_word()) i++;
161     }
162   }
163 }
164 
165 
166 void ValueStack::values_do(ValueVisitor* f) {
167   ValueStack* state = this;
168   for_each_state(state) {
169     apply(state->_locals, f);
170     apply(state->_stack, f);
171     if (state->_locks != nullptr) {
172       apply(*state->_locks, f);
173     }
174   }
175 }
176 
177 
178 Values* ValueStack::pop_arguments(int argument_size) {
179   assert(stack_size() >= argument_size, "stack too small or too many arguments");
180   int base = stack_size() - argument_size;
181   Values* args = new Values(argument_size);
182   for (int i = base; i < stack_size();) args->push(stack_at_inc(i));
183   truncate_stack(base);
184   return args;
185 }
186 
187 
188 int ValueStack::total_locks_size() const {
189   int num_locks = 0;
190   const ValueStack* state = this;
191   for_each_state(state) {
192     num_locks += state->locks_size();
193   }
194   return num_locks;
195 }
196 
197 int ValueStack::lock(Value obj) {
198   if (_locks == nullptr) {
199     _locks = new Values();
200   }
201   _locks->push(obj);
202   int num_locks = total_locks_size();
203   scope()->set_min_number_of_locks(num_locks);
204   return num_locks - 1;
205 }
206 
207 
208 int ValueStack::unlock() {
209   assert(locks_size() > 0, "sanity");
210   _locks->pop();
211   return total_locks_size();
212 }
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