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