src/share/vm/c1/c1_ValueStack.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/c1/c1_ValueStack.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,335 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_C1_C1_VALUESTACK_HPP
    1.29 +#define SHARE_VM_C1_C1_VALUESTACK_HPP
    1.30 +
    1.31 +#include "c1/c1_Instruction.hpp"
    1.32 +
    1.33 +class ValueStack: public CompilationResourceObj {
    1.34 + public:
    1.35 +  enum Kind {
    1.36 +    Parsing,             // During abstract interpretation in GraphBuilder
    1.37 +    CallerState,         // Caller state when inlining
    1.38 +    StateBefore,         // Before before execution of instruction
    1.39 +    StateAfter,          // After execution of instruction
    1.40 +    ExceptionState,      // Exception handling of instruction
    1.41 +    EmptyExceptionState, // Exception handling of instructions not covered by an xhandler
    1.42 +    BlockBeginState      // State of BlockBegin instruction with phi functions of this block
    1.43 +  };
    1.44 +
    1.45 + private:
    1.46 +  IRScope* _scope;                               // the enclosing scope
    1.47 +  ValueStack* _caller_state;
    1.48 +  int      _bci;
    1.49 +  Kind     _kind;
    1.50 +
    1.51 +  Values   _locals;                              // the locals
    1.52 +  Values   _stack;                               // the expression stack
    1.53 +  Values   _locks;                               // the monitor stack (holding the locked values)
    1.54 +
    1.55 +  Value check(ValueTag tag, Value t) {
    1.56 +    assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond");
    1.57 +    return t;
    1.58 +  }
    1.59 +
    1.60 +  Value check(ValueTag tag, Value t, Value h) {
    1.61 +    assert(h == NULL, "hi-word of doubleword value must be NULL");
    1.62 +    return check(tag, t);
    1.63 +  }
    1.64 +
    1.65 +  // helper routine
    1.66 +  static void apply(Values list, ValueVisitor* f);
    1.67 +
    1.68 +  // for simplified copying
    1.69 +  ValueStack(ValueStack* copy_from, Kind kind, int bci);
    1.70 +
    1.71 + public:
    1.72 +  // creation
    1.73 +  ValueStack(IRScope* scope, ValueStack* caller_state);
    1.74 +
    1.75 +  ValueStack* copy()                             { return new ValueStack(this, _kind, _bci); }
    1.76 +  ValueStack* copy(Kind new_kind, int new_bci)   { return new ValueStack(this, new_kind, new_bci); }
    1.77 +  ValueStack* copy_for_parsing()                 { return new ValueStack(this, Parsing, -99); }
    1.78 +
    1.79 +  void set_caller_state(ValueStack* s)           {
    1.80 +    assert(kind() == EmptyExceptionState ||
    1.81 +           (Compilation::current()->env()->jvmti_can_access_local_variables() && kind() == ExceptionState),
    1.82 +           "only EmptyExceptionStates can be modified");
    1.83 +    _caller_state = s;
    1.84 +  }
    1.85 +
    1.86 +  bool is_same(ValueStack* s);                   // returns true if this & s's types match (w/o checking locals)
    1.87 +
    1.88 +  // accessors
    1.89 +  IRScope* scope() const                         { return _scope; }
    1.90 +  ValueStack* caller_state() const               { return _caller_state; }
    1.91 +  int bci() const                                { return _bci; }
    1.92 +  Kind kind() const                              { return _kind; }
    1.93 +
    1.94 +  int locals_size() const                        { return _locals.length(); }
    1.95 +  int stack_size() const                         { return _stack.length(); }
    1.96 +  int locks_size() const                         { return _locks.length(); }
    1.97 +  bool stack_is_empty() const                    { return _stack.is_empty(); }
    1.98 +  bool no_active_locks() const                   { return _locks.is_empty(); }
    1.99 +  int total_locks_size() const;
   1.100 +
   1.101 +  // locals access
   1.102 +  void clear_locals();                           // sets all locals to NULL;
   1.103 +
   1.104 +  void invalidate_local(int i) {
   1.105 +    assert(_locals.at(i)->type()->is_single_word() ||
   1.106 +           _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
   1.107 +    _locals.at_put(i, NULL);
   1.108 +  }
   1.109 +
   1.110 +  Value local_at(int i) const {
   1.111 +    Value x = _locals.at(i);
   1.112 +    assert(x == NULL || x->type()->is_single_word() ||
   1.113 +           _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
   1.114 +    return x;
   1.115 +  }
   1.116 +
   1.117 +  void store_local(int i, Value x) {
   1.118 +    // When overwriting local i, check if i - 1 was the start of a
   1.119 +    // double word local and kill it.
   1.120 +    if (i > 0) {
   1.121 +      Value prev = _locals.at(i - 1);
   1.122 +      if (prev != NULL && prev->type()->is_double_word()) {
   1.123 +        _locals.at_put(i - 1, NULL);
   1.124 +      }
   1.125 +    }
   1.126 +
   1.127 +    _locals.at_put(i, x);
   1.128 +    if (x->type()->is_double_word()) {
   1.129 +      // hi-word of doubleword value is always NULL
   1.130 +      _locals.at_put(i + 1, NULL);
   1.131 +    }
   1.132 +  }
   1.133 +
   1.134 +  // stack access
   1.135 +  Value stack_at(int i) const {
   1.136 +    Value x = _stack.at(i);
   1.137 +    assert(x->type()->is_single_word() ||
   1.138 +           _stack.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
   1.139 +    return x;
   1.140 +  }
   1.141 +
   1.142 +  Value stack_at_inc(int& i) const {
   1.143 +    Value x = stack_at(i);
   1.144 +    i += x->type()->size();
   1.145 +    return x;
   1.146 +  }
   1.147 +
   1.148 +  void stack_at_put(int i, Value x) {
   1.149 +    _stack.at_put(i, x);
   1.150 +  }
   1.151 +
   1.152 +  // pinning support
   1.153 +  void pin_stack_for_linear_scan();
   1.154 +
   1.155 +  // iteration
   1.156 +  void values_do(ValueVisitor* f);
   1.157 +
   1.158 +  // untyped manipulation (for dup_x1, etc.)
   1.159 +  void truncate_stack(int size)                  { _stack.trunc_to(size); }
   1.160 +  void raw_push(Value t)                         { _stack.push(t); }
   1.161 +  Value raw_pop()                                { return _stack.pop(); }
   1.162 +
   1.163 +  // typed manipulation
   1.164 +  void ipush(Value t)                            { _stack.push(check(intTag    , t)); }
   1.165 +  void fpush(Value t)                            { _stack.push(check(floatTag  , t)); }
   1.166 +  void apush(Value t)                            { _stack.push(check(objectTag , t)); }
   1.167 +  void rpush(Value t)                            { _stack.push(check(addressTag, t)); }
   1.168 +  void lpush(Value t)                            { _stack.push(check(longTag   , t)); _stack.push(NULL); }
   1.169 +  void dpush(Value t)                            { _stack.push(check(doubleTag , t)); _stack.push(NULL); }
   1.170 +
   1.171 +  void push(ValueType* type, Value t) {
   1.172 +    switch (type->tag()) {
   1.173 +      case intTag    : ipush(t); return;
   1.174 +      case longTag   : lpush(t); return;
   1.175 +      case floatTag  : fpush(t); return;
   1.176 +      case doubleTag : dpush(t); return;
   1.177 +      case objectTag : apush(t); return;
   1.178 +      case addressTag: rpush(t); return;
   1.179 +    }
   1.180 +    ShouldNotReachHere();
   1.181 +  }
   1.182 +
   1.183 +  Value ipop()                                   { return check(intTag    , _stack.pop()); }
   1.184 +  Value fpop()                                   { return check(floatTag  , _stack.pop()); }
   1.185 +  Value apop()                                   { return check(objectTag , _stack.pop()); }
   1.186 +  Value rpop()                                   { return check(addressTag, _stack.pop()); }
   1.187 +  Value lpop()                                   { Value h = _stack.pop(); return check(longTag  , _stack.pop(), h); }
   1.188 +  Value dpop()                                   { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); }
   1.189 +
   1.190 +  Value pop(ValueType* type) {
   1.191 +    switch (type->tag()) {
   1.192 +      case intTag    : return ipop();
   1.193 +      case longTag   : return lpop();
   1.194 +      case floatTag  : return fpop();
   1.195 +      case doubleTag : return dpop();
   1.196 +      case objectTag : return apop();
   1.197 +      case addressTag: return rpop();
   1.198 +    }
   1.199 +    ShouldNotReachHere();
   1.200 +    return NULL;
   1.201 +  }
   1.202 +
   1.203 +  Values* pop_arguments(int argument_size);
   1.204 +
   1.205 +  // locks access
   1.206 +  int lock  (Value obj);
   1.207 +  int unlock();
   1.208 +  Value lock_at(int i) const                     { return _locks.at(i); }
   1.209 +
   1.210 +  // SSA form IR support
   1.211 +  void setup_phi_for_stack(BlockBegin* b, int index);
   1.212 +  void setup_phi_for_local(BlockBegin* b, int index);
   1.213 +
   1.214 +  // debugging
   1.215 +  void print()  PRODUCT_RETURN;
   1.216 +  void verify() PRODUCT_RETURN;
   1.217 +};
   1.218 +
   1.219 +
   1.220 +
   1.221 +// Macro definitions for simple iteration of stack and local values of a ValueStack
   1.222 +// The macros can be used like a for-loop. All variables (state, index and value)
   1.223 +// must be defined before the loop.
   1.224 +// When states are nested because of inlining, the stack of the innermost state
   1.225 +// cumulates also the stack of the nested states. In contrast, the locals of all
   1.226 +// states must be iterated each.
   1.227 +// Use the following code pattern to iterate all stack values and all nested local values:
   1.228 +//
   1.229 +// ValueStack* state = ...   // state that is iterated
   1.230 +// int index;                // current loop index (overwritten in loop)
   1.231 +// Value value;              // value at current loop index (overwritten in loop)
   1.232 +//
   1.233 +// for_each_stack_value(state, index, value {
   1.234 +//   do something with value and index
   1.235 +// }
   1.236 +//
   1.237 +// for_each_state(state) {
   1.238 +//   for_each_local_value(state, index, value) {
   1.239 +//     do something with value and index
   1.240 +//   }
   1.241 +// }
   1.242 +// as an invariant, state is NULL now
   1.243 +
   1.244 +
   1.245 +// construct a unique variable name with the line number where the macro is used
   1.246 +#define temp_var3(x) temp__ ## x
   1.247 +#define temp_var2(x) temp_var3(x)
   1.248 +#define temp_var     temp_var2(__LINE__)
   1.249 +
   1.250 +#define for_each_state(state)  \
   1.251 +  for (; state != NULL; state = state->caller_state())
   1.252 +
   1.253 +#define for_each_local_value(state, index, value)                                              \
   1.254 +  int temp_var = state->locals_size();                                                         \
   1.255 +  for (index = 0;                                                                              \
   1.256 +       index < temp_var && (value = state->local_at(index), true);                             \
   1.257 +       index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size()))    \
   1.258 +    if (value != NULL)
   1.259 +
   1.260 +
   1.261 +#define for_each_stack_value(state, index, value)                                              \
   1.262 +  int temp_var = state->stack_size();                                                          \
   1.263 +  for (index = 0;                                                                              \
   1.264 +       index < temp_var && (value = state->stack_at(index), true);                             \
   1.265 +       index += value->type()->size())
   1.266 +
   1.267 +
   1.268 +#define for_each_lock_value(state, index, value)                                               \
   1.269 +  int temp_var = state->locks_size();                                                          \
   1.270 +  for (index = 0;                                                                              \
   1.271 +       index < temp_var && (value = state->lock_at(index), true);                              \
   1.272 +       index++)                                                                                \
   1.273 +    if (value != NULL)
   1.274 +
   1.275 +
   1.276 +// Macro definition for simple iteration of all state values of a ValueStack
   1.277 +// Because the code cannot be executed in a single loop, the code must be passed
   1.278 +// as a macro parameter.
   1.279 +// Use the following code pattern to iterate all stack values and all nested local values:
   1.280 +//
   1.281 +// ValueStack* state = ...   // state that is iterated
   1.282 +// for_each_state_value(state, value,
   1.283 +//   do something with value (note that this is a macro parameter)
   1.284 +// );
   1.285 +
   1.286 +#define for_each_state_value(v_state, v_value, v_code)                                         \
   1.287 +{                                                                                              \
   1.288 +  int cur_index;                                                                               \
   1.289 +  ValueStack* cur_state = v_state;                                                             \
   1.290 +  Value v_value;                                                                               \
   1.291 +  for_each_state(cur_state) {                                                                  \
   1.292 +    {                                                                                            \
   1.293 +      for_each_local_value(cur_state, cur_index, v_value) {                                      \
   1.294 +        v_code;                                                                                  \
   1.295 +      }                                                                                          \
   1.296 +    }                                                                                          \
   1.297 +    {                                                                                            \
   1.298 +      for_each_stack_value(cur_state, cur_index, v_value) {                                      \
   1.299 +        v_code;                                                                                  \
   1.300 +      }                                                                                          \
   1.301 +    }                                                                                            \
   1.302 +  }                                                                                            \
   1.303 +}
   1.304 +
   1.305 +
   1.306 +// Macro definition for simple iteration of all phif functions of a block, i.e all
   1.307 +// phi functions of the ValueStack where the block matches.
   1.308 +// Use the following code pattern to iterate all phi functions of a block:
   1.309 +//
   1.310 +// BlockBegin* block = ...   // block that is iterated
   1.311 +// for_each_phi_function(block, phi,
   1.312 +//   do something with the phi function phi (note that this is a macro parameter)
   1.313 +// );
   1.314 +
   1.315 +#define for_each_phi_fun(v_block, v_phi, v_code)                                               \
   1.316 +{                                                                                              \
   1.317 +  int cur_index;                                                                               \
   1.318 +  ValueStack* cur_state = v_block->state();                                                    \
   1.319 +  Value value;                                                                                 \
   1.320 +  {                                                                                            \
   1.321 +    for_each_stack_value(cur_state, cur_index, value) {                                        \
   1.322 +      Phi* v_phi = value->as_Phi();                                                      \
   1.323 +      if (v_phi != NULL && v_phi->block() == v_block) {                                        \
   1.324 +        v_code;                                                                                \
   1.325 +      }                                                                                        \
   1.326 +    }                                                                                          \
   1.327 +  }                                                                                            \
   1.328 +  {                                                                                            \
   1.329 +    for_each_local_value(cur_state, cur_index, value) {                                        \
   1.330 +      Phi* v_phi = value->as_Phi();                                                      \
   1.331 +      if (v_phi != NULL && v_phi->block() == v_block) {                                        \
   1.332 +        v_code;                                                                                \
   1.333 +      }                                                                                        \
   1.334 +    }                                                                                          \
   1.335 +  }                                                                                            \
   1.336 +}
   1.337 +
   1.338 +#endif // SHARE_VM_C1_C1_VALUESTACK_HPP

mercurial