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