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 #ifndef SHARE_C1_C1_VALUESTACK_HPP
 26 #define SHARE_C1_C1_VALUESTACK_HPP
 27 
 28 #include "c1/c1_Instruction.hpp"
 29 
 30 class ValueStack: public CompilationResourceObj {
 31  public:
 32   enum Kind {
 33     Parsing,             // During abstract interpretation in GraphBuilder
 34     CallerState,         // Caller state when inlining
 35     StateBefore,         // Before before execution of instruction
 36     StateAfter,          // After execution of instruction
 37     // Exception states for an instruction.
 38     // Dead stack items or locals may be invalidated or cleared/removed.
 39     // Locals are retained if needed for JVMTI.
 40     // "empty" exception states are used when there is no handler,
 41     // and invalidate the locals.
 42     // "leaf" exception states clear the stack.
 43     // "caller" exception states are used for the parent/caller,
 44     // and invalidate the stack.
 45     ExceptionState,      // Exception state for leaf with handler, stack cleared
 46     EmptyExceptionState, // Exception state for leaf w/o handler, stack cleared, locals invalidated
 47     CallerExceptionState, // Exception state for parent with handler, stack invalidated
 48     CallerEmptyExceptionState, // Exception state for parent w/o handler, stack+locals invalidated
 49     BlockBeginState      // State of BlockBegin instruction with phi functions of this block
 50   };
 51 
 52  private:
 53   IRScope* _scope;                               // the enclosing scope
 54   ValueStack* _caller_state;
 55   int      _bci;
 56   Kind     _kind;
 57   bool     _should_reexecute;
 58 
 59   Values   _locals;                              // the locals
 60   Values   _stack;                               // the expression stack
 61   Values*  _locks;                               // the monitor stack (holding the locked values)
 62   bool     _force_reexecute;                     // force the reexecute flag on, used for patching stub
 63 
 64   Value check(ValueTag tag, Value t) {
 65     assert(tag == t->type()->tag() || (tag == objectTag && t->type()->tag() == addressTag), "types must correspond");
 66     return t;
 67   }
 68 
 69   Value check(ValueTag tag, Value t, Value h) {
 70     assert(h == nullptr, "hi-word of doubleword value must be null");
 71     return check(tag, t);
 72   }
 73 
 74   // helper routine
 75   static void apply(const Values& list, ValueVisitor* f);
 76 
 77   // for simplified copying
 78   ValueStack(ValueStack* copy_from, Kind kind, int bci, bool reexecute);
 79 
 80   int locals_size_for_copy(Kind kind) const;
 81   int stack_size_for_copy(Kind kind) const;
 82  public:
 83   // creation
 84   ValueStack(IRScope* scope, ValueStack* caller_state);
 85 
 86   ValueStack* copy()                             { return new ValueStack(this, _kind, _bci, _should_reexecute); }
 87   ValueStack* copy(Kind new_kind, int new_bci)   { return new ValueStack(this, new_kind, new_bci, _should_reexecute); }
 88   ValueStack* copy_for_parsing()                 { return new ValueStack(this, Parsing, -99, false); }
 89 
 90   // Used when no exception handler is found
 91   static Kind empty_exception_kind(bool caller = false) {
 92     return Compilation::current()->env()->should_retain_local_variables() ?
 93       (caller ? CallerExceptionState : ExceptionState) : // retain locals
 94       (caller ? CallerEmptyExceptionState : EmptyExceptionState);   // clear locals
 95   }
 96 
 97   void set_caller_state(ValueStack* s)           {
 98     assert(kind() == empty_exception_kind(false) || kind() == empty_exception_kind(true),
 99            "only empty exception states can be modified");
100     _caller_state = s;
101   }
102 
103   bool is_same(ValueStack* s);                   // returns true if this & s's types match (w/o checking locals)
104 
105   // accessors
106   IRScope* scope() const                         { return _scope; }
107   ValueStack* caller_state() const               { return _caller_state; }
108   int bci() const                                { return _bci; }
109   Kind kind() const                              { return _kind; }
110   bool should_reexecute() const                  { return _should_reexecute; }
111   void set_should_reexecute(bool reexec)         { _should_reexecute = reexec; }
112 
113   int locals_size() const                        { return _locals.length(); }
114   int stack_size() const                         { return _stack.length(); }
115   int locks_size() const                         { return _locks == nullptr ? 0 : _locks->length(); }
116   bool stack_is_empty() const                    { return _stack.is_empty(); }
117   bool no_active_locks() const                   { return _locks == nullptr || _locks->is_empty(); }
118   int total_locks_size() const;
119 
120   // locals access
121   void clear_locals();                           // sets all locals to null;
122 
123   void invalidate_local(int i) {
124     assert(!_locals.at(i)->type()->is_double_word() ||
125            _locals.at(i + 1) == nullptr, "hi-word of doubleword value must be null");
126     _locals.at_put(i, nullptr);
127   }
128 
129   Value local_at(int i) const {
130     Value x = _locals.at(i);
131     assert(x == nullptr || !x->type()->is_double_word() ||
132            _locals.at(i + 1) == nullptr, "hi-word of doubleword value must be null");
133     return x;
134   }
135 
136   void store_local(int i, Value x) {
137     // When overwriting local i, check if i - 1 was the start of a
138     // double word local and kill it.
139     if (i > 0) {
140       Value prev = _locals.at(i - 1);
141       if (prev != nullptr && prev->type()->is_double_word()) {
142         _locals.at_put(i - 1, nullptr);
143       }
144     }
145 
146     _locals.at_put(i, x);
147     if (x->type()->is_double_word()) {
148       // hi-word of doubleword value is always null
149       _locals.at_put(i + 1, nullptr);
150     }
151   }
152 
153   // stack access
154   Value stack_at(int i) const {
155     Value x = _stack.at(i);
156     assert(x == nullptr || !x->type()->is_double_word() ||
157            _stack.at(i + 1) == nullptr, "hi-word of doubleword value must be null");
158     return x;
159   }
160 
161   Value stack_at_inc(int& i) const {
162     Value x = stack_at(i);
163     i += ((x == nullptr) ? 1 : x->type()->size());
164     return x;
165   }
166 
167   void stack_at_put(int i, Value x) {
168     _stack.at_put(i, x);
169   }
170 
171   // pinning support
172   void pin_stack_for_linear_scan();
173 
174   // iteration
175   void values_do(ValueVisitor* f);
176 
177   // untyped manipulation (for dup_x1, etc.)
178   void truncate_stack(int size)                  { _stack.trunc_to(size); }
179   void raw_push(Value t)                         { _stack.push(t); }
180   Value raw_pop()                                { return _stack.pop(); }
181 
182   // typed manipulation
183   void ipush(Value t)                            { _stack.push(check(intTag    , t)); }
184   void fpush(Value t)                            { _stack.push(check(floatTag  , t)); }
185   void apush(Value t)                            { _stack.push(check(objectTag , t)); }
186   void rpush(Value t)                            { _stack.push(check(addressTag, t)); }
187   void lpush(Value t)                            { _stack.push(check(longTag   , t)); _stack.push(nullptr); }
188   void dpush(Value t)                            { _stack.push(check(doubleTag , t)); _stack.push(nullptr); }
189 
190   void push(ValueType* type, Value t) {
191     switch (type->tag()) {
192       case intTag    : ipush(t); return;
193       case longTag   : lpush(t); return;
194       case floatTag  : fpush(t); return;
195       case doubleTag : dpush(t); return;
196       case objectTag : apush(t); return;
197       case addressTag: rpush(t); return;
198       default        : ShouldNotReachHere(); return;
199     }
200   }
201 
202   Value ipop()                                   { return check(intTag    , _stack.pop()); }
203   Value fpop()                                   { return check(floatTag  , _stack.pop()); }
204   Value apop()                                   { return check(objectTag , _stack.pop()); }
205   Value rpop()                                   { return check(addressTag, _stack.pop()); }
206   Value lpop()                                   { Value h = _stack.pop(); return check(longTag  , _stack.pop(), h); }
207   Value dpop()                                   { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); }
208 
209   Value pop(ValueType* type) {
210     switch (type->tag()) {
211       case intTag    : return ipop();
212       case longTag   : return lpop();
213       case floatTag  : return fpop();
214       case doubleTag : return dpop();
215       case objectTag : return apop();
216       case addressTag: return rpop();
217       default        : ShouldNotReachHere(); return nullptr;
218     }
219   }
220 
221   Values* pop_arguments(int argument_size);
222 
223   // locks access
224   int lock  (Value obj);
225   int unlock();
226   Value lock_at(int i) const                     { return _locks->at(i); }
227 
228   // SSA form IR support
229   void setup_phi_for_stack(BlockBegin* b, int index);
230   void setup_phi_for_local(BlockBegin* b, int index);
231 
232   bool force_reexecute() const         { return _force_reexecute; }
233   void set_force_reexecute()           { _force_reexecute = true; }
234 
235   // debugging
236   void print()  PRODUCT_RETURN;
237   void verify() PRODUCT_RETURN;
238 };
239 
240 
241 
242 // Macro definitions for simple iteration of stack and local values of a ValueStack
243 // The macros can be used like a for-loop. All variables (state, index and value)
244 // must be defined before the loop.
245 // When states are nested because of inlining, the stack of the innermost state
246 // cumulates also the stack of the nested states. In contrast, the locals of all
247 // states must be iterated each.
248 // Use the following code pattern to iterate all stack values and all nested local values:
249 //
250 // ValueStack* state = ...   // state that is iterated
251 // int index;                // current loop index (overwritten in loop)
252 // Value value;              // value at current loop index (overwritten in loop)
253 //
254 // for_each_stack_value(state, index, value {
255 //   do something with value and index
256 // }
257 //
258 // for_each_state(state) {
259 //   for_each_local_value(state, index, value) {
260 //     do something with value and index
261 //   }
262 // }
263 // as an invariant, state is null now
264 
265 
266 // construct a unique variable name with the line number where the macro is used
267 #define temp_var3(x) temp__ ## x
268 #define temp_var2(x) temp_var3(x)
269 #define temp_var     temp_var2(__LINE__)
270 
271 #define for_each_state(state)  \
272   for (; state != nullptr; state = state->caller_state())
273 
274 #define for_each_local_value(state, index, value)                                              \
275   int temp_var = state->locals_size();                                                         \
276   for (index = 0;                                                                              \
277        index < temp_var && (value = state->local_at(index), true);                             \
278        index += (value == nullptr || value->type()->is_illegal() ? 1 : value->type()->size())) \
279     if (value != nullptr)
280 
281 
282 #define for_each_stack_value(state, index, value)                                              \
283   int temp_var = state->stack_size();                                                          \
284   for (index = 0;                                                                              \
285        index < temp_var && (value = state->stack_at(index), true);                             \
286        index += (value == nullptr ? 1 : value->type()->size()))                                \
287     if (value != nullptr)
288 
289 
290 #define for_each_lock_value(state, index, value)                                               \
291   int temp_var = state->locks_size();                                                          \
292   for (index = 0;                                                                              \
293        index < temp_var && (value = state->lock_at(index), true);                              \
294        index++)                                                                                \
295     if (value != nullptr)
296 
297 
298 // Macro definition for simple iteration of all state values of a ValueStack
299 // Because the code cannot be executed in a single loop, the code must be passed
300 // as a macro parameter.
301 // Use the following code pattern to iterate all stack values and all nested local values:
302 //
303 // ValueStack* state = ...   // state that is iterated
304 // for_each_state_value(state, value,
305 //   do something with value (note that this is a macro parameter)
306 // );
307 
308 #define for_each_state_value(v_state, v_value, v_code)                                         \
309 {                                                                                              \
310   int cur_index;                                                                               \
311   ValueStack* cur_state = v_state;                                                             \
312   Value v_value;                                                                               \
313   for_each_state(cur_state) {                                                                  \
314     {                                                                                            \
315       for_each_local_value(cur_state, cur_index, v_value) {                                      \
316         v_code;                                                                                  \
317       }                                                                                          \
318     }                                                                                          \
319     {                                                                                            \
320       for_each_stack_value(cur_state, cur_index, v_value) {                                      \
321         v_code;                                                                                  \
322       }                                                                                          \
323     }                                                                                            \
324   }                                                                                            \
325 }
326 
327 
328 // Macro definition for simple iteration of all phi functions of a block, i.e all
329 // phi functions of the ValueStack where the block matches.
330 // Use the following code pattern to iterate all phi functions of a block:
331 //
332 // BlockBegin* block = ...   // block that is iterated
333 // for_each_phi_function(block, phi,
334 //   do something with the phi function phi (note that this is a macro parameter)
335 // );
336 
337 #define for_each_phi_fun(v_block, v_phi, v_code)                                               \
338 {                                                                                              \
339   int cur_index;                                                                               \
340   ValueStack* cur_state = v_block->state();                                                    \
341   Value value;                                                                                 \
342   {                                                                                            \
343     for_each_stack_value(cur_state, cur_index, value) {                                        \
344       Phi* v_phi = value->as_Phi();                                                            \
345       if (v_phi != nullptr && v_phi->block() == v_block) {                                     \
346         v_code;                                                                                \
347       }                                                                                        \
348     }                                                                                          \
349   }                                                                                            \
350   {                                                                                            \
351     for_each_local_value(cur_state, cur_index, value) {                                        \
352       Phi* v_phi = value->as_Phi();                                                            \
353       if (v_phi != nullptr && v_phi->block() == v_block) {                                     \
354         v_code;                                                                                \
355       }                                                                                        \
356     }                                                                                          \
357   }                                                                                            \
358 }
359 
360 #endif // SHARE_C1_C1_VALUESTACK_HPP