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