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