src/share/vm/ci/ciTypeFlow.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/ci/ciTypeFlow.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,949 @@
     1.4 +/*
     1.5 + * Copyright (c) 2000, 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_CI_CITYPEFLOW_HPP
    1.29 +#define SHARE_VM_CI_CITYPEFLOW_HPP
    1.30 +
    1.31 +#ifdef COMPILER2
    1.32 +#include "ci/ciEnv.hpp"
    1.33 +#include "ci/ciKlass.hpp"
    1.34 +#include "ci/ciMethodBlocks.hpp"
    1.35 +#endif
    1.36 +#ifdef SHARK
    1.37 +#include "ci/ciEnv.hpp"
    1.38 +#include "ci/ciKlass.hpp"
    1.39 +#include "ci/ciMethodBlocks.hpp"
    1.40 +#include "shark/shark_globals.hpp"
    1.41 +#endif
    1.42 +
    1.43 +
    1.44 +class ciTypeFlow : public ResourceObj {
    1.45 +private:
    1.46 +  ciEnv*    _env;
    1.47 +  ciMethod* _method;
    1.48 +  ciMethodBlocks* _methodBlocks;
    1.49 +  int       _osr_bci;
    1.50 +
    1.51 +  // information cached from the method:
    1.52 +  int _max_locals;
    1.53 +  int _max_stack;
    1.54 +  int _code_size;
    1.55 +  bool      _has_irreducible_entry;
    1.56 +
    1.57 +  const char* _failure_reason;
    1.58 +
    1.59 +public:
    1.60 +  class StateVector;
    1.61 +  class Loop;
    1.62 +  class Block;
    1.63 +
    1.64 +  // Build a type flow analyzer
    1.65 +  // Do an OSR analysis if osr_bci >= 0.
    1.66 +  ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci = InvocationEntryBci);
    1.67 +
    1.68 +  // Accessors
    1.69 +  ciMethod* method() const     { return _method; }
    1.70 +  ciEnv*    env()              { return _env; }
    1.71 +  Arena*    arena()            { return _env->arena(); }
    1.72 +  bool      is_osr_flow() const{ return _osr_bci != InvocationEntryBci; }
    1.73 +  int       start_bci() const  { return is_osr_flow()? _osr_bci: 0; }
    1.74 +  int       max_locals() const { return _max_locals; }
    1.75 +  int       max_stack() const  { return _max_stack; }
    1.76 +  int       max_cells() const  { return _max_locals + _max_stack; }
    1.77 +  int       code_size() const  { return _code_size; }
    1.78 +  bool      has_irreducible_entry() const { return _has_irreducible_entry; }
    1.79 +
    1.80 +  // Represents information about an "active" jsr call.  This
    1.81 +  // class represents a call to the routine at some entry address
    1.82 +  // with some distinct return address.
    1.83 +  class JsrRecord : public ResourceObj {
    1.84 +  private:
    1.85 +    int _entry_address;
    1.86 +    int _return_address;
    1.87 +  public:
    1.88 +    JsrRecord(int entry_address, int return_address) {
    1.89 +      _entry_address = entry_address;
    1.90 +      _return_address = return_address;
    1.91 +    }
    1.92 +
    1.93 +    int entry_address() const  { return _entry_address; }
    1.94 +    int return_address() const { return _return_address; }
    1.95 +
    1.96 +    void print_on(outputStream* st) const {
    1.97 +#ifndef PRODUCT
    1.98 +      st->print("%d->%d", entry_address(), return_address());
    1.99 +#endif
   1.100 +    }
   1.101 +  };
   1.102 +
   1.103 +  // A JsrSet represents some set of JsrRecords.  This class
   1.104 +  // is used to record a set of all jsr routines which we permit
   1.105 +  // execution to return (ret) from.
   1.106 +  //
   1.107 +  // During abstract interpretation, JsrSets are used to determine
   1.108 +  // whether two paths which reach a given block are unique, and
   1.109 +  // should be cloned apart, or are compatible, and should merge
   1.110 +  // together.
   1.111 +  //
   1.112 +  // Note that different amounts of effort can be expended determining
   1.113 +  // if paths are compatible.  <DISCUSSION>
   1.114 +  class JsrSet : public ResourceObj {
   1.115 +  private:
   1.116 +    GrowableArray<JsrRecord*>* _set;
   1.117 +
   1.118 +    JsrRecord* record_at(int i) {
   1.119 +      return _set->at(i);
   1.120 +    }
   1.121 +
   1.122 +    // Insert the given JsrRecord into the JsrSet, maintaining the order
   1.123 +    // of the set and replacing any element with the same entry address.
   1.124 +    void insert_jsr_record(JsrRecord* record);
   1.125 +
   1.126 +    // Remove the JsrRecord with the given return address from the JsrSet.
   1.127 +    void remove_jsr_record(int return_address);
   1.128 +
   1.129 +  public:
   1.130 +    JsrSet(Arena* arena, int default_len = 4);
   1.131 +
   1.132 +    // Copy this JsrSet.
   1.133 +    void copy_into(JsrSet* jsrs);
   1.134 +
   1.135 +    // Is this JsrSet compatible with some other JsrSet?
   1.136 +    bool is_compatible_with(JsrSet* other);
   1.137 +
   1.138 +    // Apply the effect of a single bytecode to the JsrSet.
   1.139 +    void apply_control(ciTypeFlow* analyzer,
   1.140 +                       ciBytecodeStream* str,
   1.141 +                       StateVector* state);
   1.142 +
   1.143 +    // What is the cardinality of this set?
   1.144 +    int size() const { return _set->length(); }
   1.145 +
   1.146 +    void print_on(outputStream* st) const PRODUCT_RETURN;
   1.147 +  };
   1.148 +
   1.149 +  class LocalSet VALUE_OBJ_CLASS_SPEC {
   1.150 +  private:
   1.151 +    enum Constants { max = 63 };
   1.152 +    uint64_t _bits;
   1.153 +  public:
   1.154 +    LocalSet() : _bits(0) {}
   1.155 +    void add(uint32_t i)        { if (i < (uint32_t)max) _bits |=  (1LL << i); }
   1.156 +    void add(LocalSet* ls)      { _bits |= ls->_bits; }
   1.157 +    bool test(uint32_t i) const { return i < (uint32_t)max ? (_bits>>i)&1U : true; }
   1.158 +    void clear()                { _bits = 0; }
   1.159 +    void print_on(outputStream* st, int limit) const  PRODUCT_RETURN;
   1.160 +  };
   1.161 +
   1.162 +  // Used as a combined index for locals and temps
   1.163 +  enum Cell {
   1.164 +    Cell_0, Cell_max = INT_MAX
   1.165 +  };
   1.166 +
   1.167 +  // A StateVector summarizes the type information at some
   1.168 +  // point in the program
   1.169 +  class StateVector : public ResourceObj {
   1.170 +  private:
   1.171 +    ciType**    _types;
   1.172 +    int         _stack_size;
   1.173 +    int         _monitor_count;
   1.174 +    ciTypeFlow* _outer;
   1.175 +
   1.176 +    int         _trap_bci;
   1.177 +    int         _trap_index;
   1.178 +
   1.179 +    LocalSet    _def_locals;  // For entire block
   1.180 +
   1.181 +    static ciType* type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer);
   1.182 +
   1.183 +  public:
   1.184 +    // Special elements in our type lattice.
   1.185 +    enum {
   1.186 +      T_TOP     = T_VOID,      // why not?
   1.187 +      T_BOTTOM  = T_CONFLICT,
   1.188 +      T_LONG2   = T_SHORT,     // 2nd word of T_LONG
   1.189 +      T_DOUBLE2 = T_CHAR,      // 2nd word of T_DOUBLE
   1.190 +      T_NULL    = T_BYTE       // for now.
   1.191 +    };
   1.192 +    static ciType* top_type()    { return ciType::make((BasicType)T_TOP); }
   1.193 +    static ciType* bottom_type() { return ciType::make((BasicType)T_BOTTOM); }
   1.194 +    static ciType* long2_type()  { return ciType::make((BasicType)T_LONG2); }
   1.195 +    static ciType* double2_type(){ return ciType::make((BasicType)T_DOUBLE2); }
   1.196 +    static ciType* null_type()   { return ciType::make((BasicType)T_NULL); }
   1.197 +
   1.198 +    static ciType* half_type(ciType* t) {
   1.199 +      switch (t->basic_type()) {
   1.200 +      case T_LONG:    return long2_type();
   1.201 +      case T_DOUBLE:  return double2_type();
   1.202 +      default:        ShouldNotReachHere(); return NULL;
   1.203 +      }
   1.204 +    }
   1.205 +
   1.206 +    // The meet operation for our type lattice.
   1.207 +    ciType* type_meet(ciType* t1, ciType* t2) {
   1.208 +      return type_meet_internal(t1, t2, outer());
   1.209 +    }
   1.210 +
   1.211 +    // Accessors
   1.212 +    ciTypeFlow* outer() const          { return _outer; }
   1.213 +
   1.214 +    int         stack_size() const     { return _stack_size; }
   1.215 +    void    set_stack_size(int ss)     { _stack_size = ss; }
   1.216 +
   1.217 +    int         monitor_count() const  { return _monitor_count; }
   1.218 +    void    set_monitor_count(int mc)  { _monitor_count = mc; }
   1.219 +
   1.220 +    LocalSet* def_locals() { return &_def_locals; }
   1.221 +    const LocalSet* def_locals() const { return &_def_locals; }
   1.222 +
   1.223 +    static Cell start_cell()           { return (Cell)0; }
   1.224 +    static Cell next_cell(Cell c)      { return (Cell)(((int)c) + 1); }
   1.225 +    Cell        limit_cell() const {
   1.226 +      return (Cell)(outer()->max_locals() + stack_size());
   1.227 +    }
   1.228 +
   1.229 +    // Cell creation
   1.230 +    Cell      local(int lnum) const {
   1.231 +      assert(lnum < outer()->max_locals(), "index check");
   1.232 +      return (Cell)(lnum);
   1.233 +    }
   1.234 +
   1.235 +    Cell      stack(int snum) const {
   1.236 +      assert(snum < stack_size(), "index check");
   1.237 +      return (Cell)(outer()->max_locals() + snum);
   1.238 +    }
   1.239 +
   1.240 +    Cell      tos() const { return stack(stack_size()-1); }
   1.241 +
   1.242 +    // For external use only:
   1.243 +    ciType* local_type_at(int i) const { return type_at(local(i)); }
   1.244 +    ciType* stack_type_at(int i) const { return type_at(stack(i)); }
   1.245 +
   1.246 +    // Accessors for the type of some Cell c
   1.247 +    ciType*   type_at(Cell c) const {
   1.248 +      assert(start_cell() <= c && c < limit_cell(), "out of bounds");
   1.249 +      return _types[c];
   1.250 +    }
   1.251 +
   1.252 +    void      set_type_at(Cell c, ciType* type) {
   1.253 +      assert(start_cell() <= c && c < limit_cell(), "out of bounds");
   1.254 +      _types[c] = type;
   1.255 +    }
   1.256 +
   1.257 +    // Top-of-stack operations.
   1.258 +    void      set_type_at_tos(ciType* type) { set_type_at(tos(), type); }
   1.259 +    ciType*   type_at_tos() const           { return type_at(tos()); }
   1.260 +
   1.261 +    void      push(ciType* type) {
   1.262 +      _stack_size++;
   1.263 +      set_type_at_tos(type);
   1.264 +    }
   1.265 +    void      pop() {
   1.266 +      debug_only(set_type_at_tos(bottom_type()));
   1.267 +      _stack_size--;
   1.268 +    }
   1.269 +    ciType*   pop_value() {
   1.270 +      ciType* t = type_at_tos();
   1.271 +      pop();
   1.272 +      return t;
   1.273 +    }
   1.274 +
   1.275 +    // Convenience operations.
   1.276 +    bool      is_reference(ciType* type) const {
   1.277 +      return type == null_type() || !type->is_primitive_type();
   1.278 +    }
   1.279 +    bool      is_int(ciType* type) const {
   1.280 +      return type->basic_type() == T_INT;
   1.281 +    }
   1.282 +    bool      is_long(ciType* type) const {
   1.283 +      return type->basic_type() == T_LONG;
   1.284 +    }
   1.285 +    bool      is_float(ciType* type) const {
   1.286 +      return type->basic_type() == T_FLOAT;
   1.287 +    }
   1.288 +    bool      is_double(ciType* type) const {
   1.289 +      return type->basic_type() == T_DOUBLE;
   1.290 +    }
   1.291 +
   1.292 +    void store_to_local(int lnum) {
   1.293 +      _def_locals.add((uint) lnum);
   1.294 +    }
   1.295 +
   1.296 +    void      push_translate(ciType* type);
   1.297 +
   1.298 +    void      push_int() {
   1.299 +      push(ciType::make(T_INT));
   1.300 +    }
   1.301 +    void      pop_int() {
   1.302 +      assert(is_int(type_at_tos()), "must be integer");
   1.303 +      pop();
   1.304 +    }
   1.305 +    void      check_int(Cell c) {
   1.306 +      assert(is_int(type_at(c)), "must be integer");
   1.307 +    }
   1.308 +    void      push_double() {
   1.309 +      push(ciType::make(T_DOUBLE));
   1.310 +      push(double2_type());
   1.311 +    }
   1.312 +    void      pop_double() {
   1.313 +      assert(type_at_tos() == double2_type(), "must be 2nd half");
   1.314 +      pop();
   1.315 +      assert(is_double(type_at_tos()), "must be double");
   1.316 +      pop();
   1.317 +    }
   1.318 +    void      push_float() {
   1.319 +      push(ciType::make(T_FLOAT));
   1.320 +    }
   1.321 +    void      pop_float() {
   1.322 +      assert(is_float(type_at_tos()), "must be float");
   1.323 +      pop();
   1.324 +    }
   1.325 +    void      push_long() {
   1.326 +      push(ciType::make(T_LONG));
   1.327 +      push(long2_type());
   1.328 +    }
   1.329 +    void      pop_long() {
   1.330 +      assert(type_at_tos() == long2_type(), "must be 2nd half");
   1.331 +      pop();
   1.332 +      assert(is_long(type_at_tos()), "must be long");
   1.333 +      pop();
   1.334 +    }
   1.335 +    void      push_object(ciKlass* klass) {
   1.336 +      push(klass);
   1.337 +    }
   1.338 +    void      pop_object() {
   1.339 +      assert(is_reference(type_at_tos()), "must be reference type");
   1.340 +      pop();
   1.341 +    }
   1.342 +    void      pop_array() {
   1.343 +      assert(type_at_tos() == null_type() ||
   1.344 +             type_at_tos()->is_array_klass(), "must be array type");
   1.345 +      pop();
   1.346 +    }
   1.347 +    // pop_objArray and pop_typeArray narrow the tos to ciObjArrayKlass
   1.348 +    // or ciTypeArrayKlass (resp.).  In the rare case that an explicit
   1.349 +    // null is popped from the stack, we return NULL.  Caller beware.
   1.350 +    ciObjArrayKlass* pop_objArray() {
   1.351 +      ciType* array = pop_value();
   1.352 +      if (array == null_type())  return NULL;
   1.353 +      assert(array->is_obj_array_klass(), "must be object array type");
   1.354 +      return array->as_obj_array_klass();
   1.355 +    }
   1.356 +    ciTypeArrayKlass* pop_typeArray() {
   1.357 +      ciType* array = pop_value();
   1.358 +      if (array == null_type())  return NULL;
   1.359 +      assert(array->is_type_array_klass(), "must be prim array type");
   1.360 +      return array->as_type_array_klass();
   1.361 +    }
   1.362 +    void      push_null() {
   1.363 +      push(null_type());
   1.364 +    }
   1.365 +    void      do_null_assert(ciKlass* unloaded_klass);
   1.366 +
   1.367 +    // Helper convenience routines.
   1.368 +    void do_aaload(ciBytecodeStream* str);
   1.369 +    void do_checkcast(ciBytecodeStream* str);
   1.370 +    void do_getfield(ciBytecodeStream* str);
   1.371 +    void do_getstatic(ciBytecodeStream* str);
   1.372 +    void do_invoke(ciBytecodeStream* str, bool has_receiver);
   1.373 +    void do_jsr(ciBytecodeStream* str);
   1.374 +    void do_ldc(ciBytecodeStream* str);
   1.375 +    void do_multianewarray(ciBytecodeStream* str);
   1.376 +    void do_new(ciBytecodeStream* str);
   1.377 +    void do_newarray(ciBytecodeStream* str);
   1.378 +    void do_putfield(ciBytecodeStream* str);
   1.379 +    void do_putstatic(ciBytecodeStream* str);
   1.380 +    void do_ret(ciBytecodeStream* str);
   1.381 +
   1.382 +    void overwrite_local_double_long(int index) {
   1.383 +      // Invalidate the previous local if it contains first half of
   1.384 +      // a double or long value since it's seconf half is being overwritten.
   1.385 +      int prev_index = index - 1;
   1.386 +      if (prev_index >= 0 &&
   1.387 +          (is_double(type_at(local(prev_index))) ||
   1.388 +           is_long(type_at(local(prev_index))))) {
   1.389 +        set_type_at(local(prev_index), bottom_type());
   1.390 +      }
   1.391 +    }
   1.392 +
   1.393 +    void load_local_object(int index) {
   1.394 +      ciType* type = type_at(local(index));
   1.395 +      assert(is_reference(type), "must be reference type");
   1.396 +      push(type);
   1.397 +    }
   1.398 +    void store_local_object(int index) {
   1.399 +      ciType* type = pop_value();
   1.400 +      assert(is_reference(type) || type->is_return_address(),
   1.401 +             "must be reference type or return address");
   1.402 +      overwrite_local_double_long(index);
   1.403 +      set_type_at(local(index), type);
   1.404 +      store_to_local(index);
   1.405 +    }
   1.406 +
   1.407 +    void load_local_double(int index) {
   1.408 +      ciType* type = type_at(local(index));
   1.409 +      ciType* type2 = type_at(local(index+1));
   1.410 +      assert(is_double(type), "must be double type");
   1.411 +      assert(type2 == double2_type(), "must be 2nd half");
   1.412 +      push(type);
   1.413 +      push(double2_type());
   1.414 +    }
   1.415 +    void store_local_double(int index) {
   1.416 +      ciType* type2 = pop_value();
   1.417 +      ciType* type = pop_value();
   1.418 +      assert(is_double(type), "must be double");
   1.419 +      assert(type2 == double2_type(), "must be 2nd half");
   1.420 +      overwrite_local_double_long(index);
   1.421 +      set_type_at(local(index), type);
   1.422 +      set_type_at(local(index+1), type2);
   1.423 +      store_to_local(index);
   1.424 +      store_to_local(index+1);
   1.425 +    }
   1.426 +
   1.427 +    void load_local_float(int index) {
   1.428 +      ciType* type = type_at(local(index));
   1.429 +      assert(is_float(type), "must be float type");
   1.430 +      push(type);
   1.431 +    }
   1.432 +    void store_local_float(int index) {
   1.433 +      ciType* type = pop_value();
   1.434 +      assert(is_float(type), "must be float type");
   1.435 +      overwrite_local_double_long(index);
   1.436 +      set_type_at(local(index), type);
   1.437 +      store_to_local(index);
   1.438 +    }
   1.439 +
   1.440 +    void load_local_int(int index) {
   1.441 +      ciType* type = type_at(local(index));
   1.442 +      assert(is_int(type), "must be int type");
   1.443 +      push(type);
   1.444 +    }
   1.445 +    void store_local_int(int index) {
   1.446 +      ciType* type = pop_value();
   1.447 +      assert(is_int(type), "must be int type");
   1.448 +      overwrite_local_double_long(index);
   1.449 +      set_type_at(local(index), type);
   1.450 +      store_to_local(index);
   1.451 +    }
   1.452 +
   1.453 +    void load_local_long(int index) {
   1.454 +      ciType* type = type_at(local(index));
   1.455 +      ciType* type2 = type_at(local(index+1));
   1.456 +      assert(is_long(type), "must be long type");
   1.457 +      assert(type2 == long2_type(), "must be 2nd half");
   1.458 +      push(type);
   1.459 +      push(long2_type());
   1.460 +    }
   1.461 +    void store_local_long(int index) {
   1.462 +      ciType* type2 = pop_value();
   1.463 +      ciType* type = pop_value();
   1.464 +      assert(is_long(type), "must be long");
   1.465 +      assert(type2 == long2_type(), "must be 2nd half");
   1.466 +      overwrite_local_double_long(index);
   1.467 +      set_type_at(local(index), type);
   1.468 +      set_type_at(local(index+1), type2);
   1.469 +      store_to_local(index);
   1.470 +      store_to_local(index+1);
   1.471 +    }
   1.472 +
   1.473 +    // Stop interpretation of this path with a trap.
   1.474 +    void trap(ciBytecodeStream* str, ciKlass* klass, int index);
   1.475 +
   1.476 +  public:
   1.477 +    StateVector(ciTypeFlow* outer);
   1.478 +
   1.479 +    // Copy our value into some other StateVector
   1.480 +    void copy_into(StateVector* copy) const;
   1.481 +
   1.482 +    // Meets this StateVector with another, destructively modifying this
   1.483 +    // one.  Returns true if any modification takes place.
   1.484 +    bool meet(const StateVector* incoming);
   1.485 +
   1.486 +    // Ditto, except that the incoming state is coming from an exception.
   1.487 +    bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming);
   1.488 +
   1.489 +    // Apply the effect of one bytecode to this StateVector
   1.490 +    bool apply_one_bytecode(ciBytecodeStream* stream);
   1.491 +
   1.492 +    // What is the bci of the trap?
   1.493 +    int  trap_bci() { return _trap_bci; }
   1.494 +
   1.495 +    // What is the index associated with the trap?
   1.496 +    int  trap_index() { return _trap_index; }
   1.497 +
   1.498 +    void print_cell_on(outputStream* st, Cell c) const PRODUCT_RETURN;
   1.499 +    void print_on(outputStream* st) const              PRODUCT_RETURN;
   1.500 +  };
   1.501 +
   1.502 +  // Parameter for "find_block" calls:
   1.503 +  // Describes the difference between a public and backedge copy.
   1.504 +  enum CreateOption {
   1.505 +    create_public_copy,
   1.506 +    create_backedge_copy,
   1.507 +    no_create
   1.508 +  };
   1.509 +
   1.510 +  // Successor iterator
   1.511 +  class SuccIter : public StackObj {
   1.512 +  private:
   1.513 +    Block* _pred;
   1.514 +    int    _index;
   1.515 +    Block* _succ;
   1.516 +  public:
   1.517 +    SuccIter()                        : _pred(NULL), _index(-1), _succ(NULL) {}
   1.518 +    SuccIter(Block* pred)             : _pred(pred), _index(-1), _succ(NULL) { next(); }
   1.519 +    int    index()     { return _index; }
   1.520 +    Block* pred()      { return _pred; }           // Return predecessor
   1.521 +    bool   done()      { return _index < 0; }      // Finished?
   1.522 +    Block* succ()      { return _succ; }           // Return current successor
   1.523 +    void   next();                                 // Advance
   1.524 +    void   set_succ(Block* succ);                  // Update current successor
   1.525 +    bool   is_normal_ctrl() { return index() < _pred->successors()->length(); }
   1.526 +  };
   1.527 +
   1.528 +  // A basic block
   1.529 +  class Block : public ResourceObj {
   1.530 +  private:
   1.531 +    ciBlock*                          _ciblock;
   1.532 +    GrowableArray<Block*>*           _exceptions;
   1.533 +    GrowableArray<ciInstanceKlass*>* _exc_klasses;
   1.534 +    GrowableArray<Block*>*           _successors;
   1.535 +    StateVector*                     _state;
   1.536 +    JsrSet*                          _jsrs;
   1.537 +
   1.538 +    int                              _trap_bci;
   1.539 +    int                              _trap_index;
   1.540 +
   1.541 +    // pre_order, assigned at first visit. Used as block ID and "visited" tag
   1.542 +    int                              _pre_order;
   1.543 +
   1.544 +    // A post-order, used to compute the reverse post order (RPO) provided to the client
   1.545 +    int                              _post_order;  // used to compute rpo
   1.546 +
   1.547 +    // Has this block been cloned for a loop backedge?
   1.548 +    bool                             _backedge_copy;
   1.549 +
   1.550 +    // This block is entry to irreducible loop.
   1.551 +    bool                             _irreducible_entry;
   1.552 +
   1.553 +    // This block has monitor entry point.
   1.554 +    bool                             _has_monitorenter;
   1.555 +
   1.556 +    // A pointer used for our internal work list
   1.557 +    bool                             _on_work_list;      // on the work list
   1.558 +    Block*                           _next;
   1.559 +    Block*                           _rpo_next;          // Reverse post order list
   1.560 +
   1.561 +    // Loop info
   1.562 +    Loop*                            _loop;              // nearest loop
   1.563 +
   1.564 +    ciBlock*     ciblock() const     { return _ciblock; }
   1.565 +    StateVector* state() const     { return _state; }
   1.566 +
   1.567 +    // Compute the exceptional successors and types for this Block.
   1.568 +    void compute_exceptions();
   1.569 +
   1.570 +  public:
   1.571 +    // constructors
   1.572 +    Block(ciTypeFlow* outer, ciBlock* ciblk, JsrSet* jsrs);
   1.573 +
   1.574 +    void set_trap(int trap_bci, int trap_index) {
   1.575 +      _trap_bci = trap_bci;
   1.576 +      _trap_index = trap_index;
   1.577 +      assert(has_trap(), "");
   1.578 +    }
   1.579 +    bool has_trap()   const  { return _trap_bci != -1; }
   1.580 +    int  trap_bci()   const  { assert(has_trap(), ""); return _trap_bci; }
   1.581 +    int  trap_index() const  { assert(has_trap(), ""); return _trap_index; }
   1.582 +
   1.583 +    // accessors
   1.584 +    ciTypeFlow* outer() const { return state()->outer(); }
   1.585 +    int start() const         { return _ciblock->start_bci(); }
   1.586 +    int limit() const         { return _ciblock->limit_bci(); }
   1.587 +    int control() const       { return _ciblock->control_bci(); }
   1.588 +    JsrSet* jsrs() const      { return _jsrs; }
   1.589 +
   1.590 +    bool    is_backedge_copy() const       { return _backedge_copy; }
   1.591 +    void   set_backedge_copy(bool z);
   1.592 +    int        backedge_copy_count() const { return outer()->backedge_copy_count(ciblock()->index(), _jsrs); }
   1.593 +
   1.594 +    // access to entry state
   1.595 +    int     stack_size() const         { return _state->stack_size(); }
   1.596 +    int     monitor_count() const      { return _state->monitor_count(); }
   1.597 +    ciType* local_type_at(int i) const { return _state->local_type_at(i); }
   1.598 +    ciType* stack_type_at(int i) const { return _state->stack_type_at(i); }
   1.599 +
   1.600 +    // Data flow on locals
   1.601 +    bool is_invariant_local(uint v) const {
   1.602 +      assert(is_loop_head(), "only loop heads");
   1.603 +      // Find outermost loop with same loop head
   1.604 +      Loop* lp = loop();
   1.605 +      while (lp->parent() != NULL) {
   1.606 +        if (lp->parent()->head() != lp->head()) break;
   1.607 +        lp = lp->parent();
   1.608 +      }
   1.609 +      return !lp->def_locals()->test(v);
   1.610 +    }
   1.611 +    LocalSet* def_locals() { return _state->def_locals(); }
   1.612 +    const LocalSet* def_locals() const { return _state->def_locals(); }
   1.613 +
   1.614 +    // Get the successors for this Block.
   1.615 +    GrowableArray<Block*>* successors(ciBytecodeStream* str,
   1.616 +                                      StateVector* state,
   1.617 +                                      JsrSet* jsrs);
   1.618 +    GrowableArray<Block*>* successors() {
   1.619 +      assert(_successors != NULL, "must be filled in");
   1.620 +      return _successors;
   1.621 +    }
   1.622 +
   1.623 +    // Get the exceptional successors for this Block.
   1.624 +    GrowableArray<Block*>* exceptions() {
   1.625 +      if (_exceptions == NULL) {
   1.626 +        compute_exceptions();
   1.627 +      }
   1.628 +      return _exceptions;
   1.629 +    }
   1.630 +
   1.631 +    // Get the exception klasses corresponding to the
   1.632 +    // exceptional successors for this Block.
   1.633 +    GrowableArray<ciInstanceKlass*>* exc_klasses() {
   1.634 +      if (_exc_klasses == NULL) {
   1.635 +        compute_exceptions();
   1.636 +      }
   1.637 +      return _exc_klasses;
   1.638 +    }
   1.639 +
   1.640 +    // Is this Block compatible with a given JsrSet?
   1.641 +    bool is_compatible_with(JsrSet* other) {
   1.642 +      return _jsrs->is_compatible_with(other);
   1.643 +    }
   1.644 +
   1.645 +    // Copy the value of our state vector into another.
   1.646 +    void copy_state_into(StateVector* copy) const {
   1.647 +      _state->copy_into(copy);
   1.648 +    }
   1.649 +
   1.650 +    // Copy the value of our JsrSet into another
   1.651 +    void copy_jsrs_into(JsrSet* copy) const {
   1.652 +      _jsrs->copy_into(copy);
   1.653 +    }
   1.654 +
   1.655 +    // Meets the start state of this block with another state, destructively
   1.656 +    // modifying this one.  Returns true if any modification takes place.
   1.657 +    bool meet(const StateVector* incoming) {
   1.658 +      return state()->meet(incoming);
   1.659 +    }
   1.660 +
   1.661 +    // Ditto, except that the incoming state is coming from an
   1.662 +    // exception path.  This means the stack is replaced by the
   1.663 +    // appropriate exception type.
   1.664 +    bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming) {
   1.665 +      return state()->meet_exception(exc, incoming);
   1.666 +    }
   1.667 +
   1.668 +    // Work list manipulation
   1.669 +    void   set_next(Block* block) { _next = block; }
   1.670 +    Block* next() const           { return _next; }
   1.671 +
   1.672 +    void   set_on_work_list(bool c) { _on_work_list = c; }
   1.673 +    bool   is_on_work_list() const  { return _on_work_list; }
   1.674 +
   1.675 +    bool   has_pre_order() const  { return _pre_order >= 0; }
   1.676 +    void   set_pre_order(int po)  { assert(!has_pre_order(), ""); _pre_order = po; }
   1.677 +    int    pre_order() const      { assert(has_pre_order(), ""); return _pre_order; }
   1.678 +    void   set_next_pre_order()   { set_pre_order(outer()->inc_next_pre_order()); }
   1.679 +    bool   is_start() const       { return _pre_order == outer()->start_block_num(); }
   1.680 +
   1.681 +    // Reverse post order
   1.682 +    void   df_init();
   1.683 +    bool   has_post_order() const { return _post_order >= 0; }
   1.684 +    void   set_post_order(int po) { assert(!has_post_order() && po >= 0, ""); _post_order = po; }
   1.685 +    void   reset_post_order(int o){ _post_order = o; }
   1.686 +    int    post_order() const     { assert(has_post_order(), ""); return _post_order; }
   1.687 +
   1.688 +    bool   has_rpo() const        { return has_post_order() && outer()->have_block_count(); }
   1.689 +    int    rpo() const            { assert(has_rpo(), ""); return outer()->block_count() - post_order() - 1; }
   1.690 +    void   set_rpo_next(Block* b) { _rpo_next = b; }
   1.691 +    Block* rpo_next()             { return _rpo_next; }
   1.692 +
   1.693 +    // Loops
   1.694 +    Loop*  loop() const                  { return _loop; }
   1.695 +    void   set_loop(Loop* lp)            { _loop = lp; }
   1.696 +    bool   is_loop_head() const          { return _loop && _loop->head() == this; }
   1.697 +    void   set_irreducible_entry(bool c) { _irreducible_entry = c; }
   1.698 +    bool   is_irreducible_entry() const  { return _irreducible_entry; }
   1.699 +    void   set_has_monitorenter()        { _has_monitorenter = true; }
   1.700 +    bool   has_monitorenter() const      { return _has_monitorenter; }
   1.701 +    bool   is_visited() const            { return has_pre_order(); }
   1.702 +    bool   is_post_visited() const       { return has_post_order(); }
   1.703 +    bool   is_clonable_exit(Loop* lp);
   1.704 +    Block* looping_succ(Loop* lp);       // Successor inside of loop
   1.705 +    bool   is_single_entry_loop_head() const {
   1.706 +      if (!is_loop_head()) return false;
   1.707 +      for (Loop* lp = loop(); lp != NULL && lp->head() == this; lp = lp->parent())
   1.708 +        if (lp->is_irreducible()) return false;
   1.709 +      return true;
   1.710 +    }
   1.711 +
   1.712 +    void   print_value_on(outputStream* st) const PRODUCT_RETURN;
   1.713 +    void   print_on(outputStream* st) const       PRODUCT_RETURN;
   1.714 +  };
   1.715 +
   1.716 +  // Loop
   1.717 +  class Loop : public ResourceObj {
   1.718 +  private:
   1.719 +    Loop* _parent;
   1.720 +    Loop* _sibling;  // List of siblings, null terminated
   1.721 +    Loop* _child;    // Head of child list threaded thru sibling pointer
   1.722 +    Block* _head;    // Head of loop
   1.723 +    Block* _tail;    // Tail of loop
   1.724 +    bool   _irreducible;
   1.725 +    LocalSet _def_locals;
   1.726 +
   1.727 +  public:
   1.728 +    Loop(Block* head, Block* tail) :
   1.729 +      _head(head),   _tail(tail),
   1.730 +      _parent(NULL), _sibling(NULL), _child(NULL),
   1.731 +      _irreducible(false), _def_locals() {}
   1.732 +
   1.733 +    Loop* parent()  const { return _parent; }
   1.734 +    Loop* sibling() const { return _sibling; }
   1.735 +    Loop* child()   const { return _child; }
   1.736 +    Block* head()   const { return _head; }
   1.737 +    Block* tail()   const { return _tail; }
   1.738 +    void set_parent(Loop* p)  { _parent = p; }
   1.739 +    void set_sibling(Loop* s) { _sibling = s; }
   1.740 +    void set_child(Loop* c)   { _child = c; }
   1.741 +    void set_head(Block* hd)  { _head = hd; }
   1.742 +    void set_tail(Block* tl)  { _tail = tl; }
   1.743 +
   1.744 +    int depth() const;              // nesting depth
   1.745 +
   1.746 +    // Returns true if lp is a nested loop or us.
   1.747 +    bool contains(Loop* lp) const;
   1.748 +    bool contains(Block* blk) const { return contains(blk->loop()); }
   1.749 +
   1.750 +    // Data flow on locals
   1.751 +    LocalSet* def_locals() { return &_def_locals; }
   1.752 +    const LocalSet* def_locals() const { return &_def_locals; }
   1.753 +
   1.754 +    // Merge the branch lp into this branch, sorting on the loop head
   1.755 +    // pre_orders. Returns the new branch.
   1.756 +    Loop* sorted_merge(Loop* lp);
   1.757 +
   1.758 +    // Mark non-single entry to loop
   1.759 +    void set_irreducible(Block* entry) {
   1.760 +      _irreducible = true;
   1.761 +      entry->set_irreducible_entry(true);
   1.762 +    }
   1.763 +    bool is_irreducible() const { return _irreducible; }
   1.764 +
   1.765 +    bool is_root() const { return _tail->pre_order() == max_jint; }
   1.766 +
   1.767 +    void print(outputStream* st = tty, int indent = 0) const PRODUCT_RETURN;
   1.768 +  };
   1.769 +
   1.770 +  // Postorder iteration over the loop tree.
   1.771 +  class PostorderLoops : public StackObj {
   1.772 +  private:
   1.773 +    Loop* _root;
   1.774 +    Loop* _current;
   1.775 +  public:
   1.776 +    PostorderLoops(Loop* root) : _root(root), _current(root) {
   1.777 +      while (_current->child() != NULL) {
   1.778 +        _current = _current->child();
   1.779 +      }
   1.780 +    }
   1.781 +    bool done() { return _current == NULL; }  // Finished iterating?
   1.782 +    void next();                            // Advance to next loop
   1.783 +    Loop* current() { return _current; }      // Return current loop.
   1.784 +  };
   1.785 +
   1.786 +  // Preorder iteration over the loop tree.
   1.787 +  class PreorderLoops : public StackObj {
   1.788 +  private:
   1.789 +    Loop* _root;
   1.790 +    Loop* _current;
   1.791 +  public:
   1.792 +    PreorderLoops(Loop* root) : _root(root), _current(root) {}
   1.793 +    bool done() { return _current == NULL; }  // Finished iterating?
   1.794 +    void next();                            // Advance to next loop
   1.795 +    Loop* current() { return _current; }      // Return current loop.
   1.796 +  };
   1.797 +
   1.798 +  // Standard indexes of successors, for various bytecodes.
   1.799 +  enum {
   1.800 +    FALL_THROUGH   = 0,  // normal control
   1.801 +    IF_NOT_TAKEN   = 0,  // the not-taken branch of an if (i.e., fall-through)
   1.802 +    IF_TAKEN       = 1,  // the taken branch of an if
   1.803 +    GOTO_TARGET    = 0,  // unique successor for goto, jsr, or ret
   1.804 +    SWITCH_DEFAULT = 0,  // default branch of a switch
   1.805 +    SWITCH_CASES   = 1   // first index for any non-default switch branches
   1.806 +    // Unlike in other blocks, the successors of a switch are listed uniquely.
   1.807 +  };
   1.808 +
   1.809 +private:
   1.810 +  // A mapping from pre_order to Blocks.  This array is created
   1.811 +  // only at the end of the flow.
   1.812 +  Block** _block_map;
   1.813 +
   1.814 +  // For each ciBlock index, a list of Blocks which share this ciBlock.
   1.815 +  GrowableArray<Block*>** _idx_to_blocklist;
   1.816 +  // count of ciBlocks
   1.817 +  int _ciblock_count;
   1.818 +
   1.819 +  // Tells if a given instruction is able to generate an exception edge.
   1.820 +  bool can_trap(ciBytecodeStream& str);
   1.821 +
   1.822 +  // Clone the loop heads. Returns true if any cloning occurred.
   1.823 +  bool clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set);
   1.824 +
   1.825 +  // Clone lp's head and replace tail's successors with clone.
   1.826 +  Block* clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set);
   1.827 +
   1.828 +public:
   1.829 +  // Return the block beginning at bci which has a JsrSet compatible
   1.830 +  // with jsrs.
   1.831 +  Block* block_at(int bci, JsrSet* set, CreateOption option = create_public_copy);
   1.832 +
   1.833 +  // block factory
   1.834 +  Block* get_block_for(int ciBlockIndex, JsrSet* jsrs, CreateOption option = create_public_copy);
   1.835 +
   1.836 +  // How many of the blocks have the backedge_copy bit set?
   1.837 +  int backedge_copy_count(int ciBlockIndex, JsrSet* jsrs) const;
   1.838 +
   1.839 +  // Return an existing block containing bci which has a JsrSet compatible
   1.840 +  // with jsrs, or NULL if there is none.
   1.841 +  Block* existing_block_at(int bci, JsrSet* set) { return block_at(bci, set, no_create); }
   1.842 +
   1.843 +  // Tell whether the flow analysis has encountered an error of some sort.
   1.844 +  bool failing() { return env()->failing() || _failure_reason != NULL; }
   1.845 +
   1.846 +  // Reason this compilation is failing, such as "too many basic blocks".
   1.847 +  const char* failure_reason() { return _failure_reason; }
   1.848 +
   1.849 +  // Note a failure.
   1.850 +  void record_failure(const char* reason);
   1.851 +
   1.852 +  // Return the block of a given pre-order number.
   1.853 +  int have_block_count() const      { return _block_map != NULL; }
   1.854 +  int block_count() const           { assert(have_block_count(), "");
   1.855 +                                      return _next_pre_order; }
   1.856 +  Block* pre_order_at(int po) const { assert(0 <= po && po < block_count(), "out of bounds");
   1.857 +                                      return _block_map[po]; }
   1.858 +  Block* start_block() const        { return pre_order_at(start_block_num()); }
   1.859 +  int start_block_num() const       { return 0; }
   1.860 +  Block* rpo_at(int rpo) const      { assert(0 <= rpo && rpo < block_count(), "out of bounds");
   1.861 +                                      return _block_map[rpo]; }
   1.862 +  int next_pre_order()              { return _next_pre_order; }
   1.863 +  int inc_next_pre_order()          { return _next_pre_order++; }
   1.864 +
   1.865 +private:
   1.866 +  // A work list used during flow analysis.
   1.867 +  Block* _work_list;
   1.868 +
   1.869 +  // List of blocks in reverse post order
   1.870 +  Block* _rpo_list;
   1.871 +
   1.872 +  // Next Block::_pre_order.  After mapping, doubles as block_count.
   1.873 +  int _next_pre_order;
   1.874 +
   1.875 +  // Are there more blocks on the work list?
   1.876 +  bool work_list_empty() { return _work_list == NULL; }
   1.877 +
   1.878 +  // Get the next basic block from our work list.
   1.879 +  Block* work_list_next();
   1.880 +
   1.881 +  // Add a basic block to our work list.
   1.882 +  void add_to_work_list(Block* block);
   1.883 +
   1.884 +  // Prepend a basic block to rpo list.
   1.885 +  void prepend_to_rpo_list(Block* blk) {
   1.886 +    blk->set_rpo_next(_rpo_list);
   1.887 +    _rpo_list = blk;
   1.888 +  }
   1.889 +
   1.890 +  // Root of the loop tree
   1.891 +  Loop* _loop_tree_root;
   1.892 +
   1.893 +  // State used for make_jsr_record
   1.894 +  int _jsr_count;
   1.895 +  GrowableArray<JsrRecord*>* _jsr_records;
   1.896 +
   1.897 +public:
   1.898 +  // Make a JsrRecord for a given (entry, return) pair, if such a record
   1.899 +  // does not already exist.
   1.900 +  JsrRecord* make_jsr_record(int entry_address, int return_address);
   1.901 +
   1.902 +  void  set_loop_tree_root(Loop* ltr) { _loop_tree_root = ltr; }
   1.903 +  Loop* loop_tree_root()              { return _loop_tree_root; }
   1.904 +
   1.905 +private:
   1.906 +  // Get the initial state for start_bci:
   1.907 +  const StateVector* get_start_state();
   1.908 +
   1.909 +  // Merge the current state into all exceptional successors at the
   1.910 +  // current point in the code.
   1.911 +  void flow_exceptions(GrowableArray<Block*>* exceptions,
   1.912 +                       GrowableArray<ciInstanceKlass*>* exc_klasses,
   1.913 +                       StateVector* state);
   1.914 +
   1.915 +  // Merge the current state into all successors at the current point
   1.916 +  // in the code.
   1.917 +  void flow_successors(GrowableArray<Block*>* successors,
   1.918 +                       StateVector* state);
   1.919 +
   1.920 +  // Interpret the effects of the bytecodes on the incoming state
   1.921 +  // vector of a basic block.  Push the changed state to succeeding
   1.922 +  // basic blocks.
   1.923 +  void flow_block(Block* block,
   1.924 +                  StateVector* scratch_state,
   1.925 +                  JsrSet* scratch_jsrs);
   1.926 +
   1.927 +  // Perform the type flow analysis, creating and cloning Blocks as
   1.928 +  // necessary.
   1.929 +  void flow_types();
   1.930 +
   1.931 +  // Perform the depth first type flow analysis. Helper for flow_types.
   1.932 +  void df_flow_types(Block* start,
   1.933 +                     bool do_flow,
   1.934 +                     StateVector* temp_vector,
   1.935 +                     JsrSet* temp_set);
   1.936 +
   1.937 +  // Incrementally build loop tree.
   1.938 +  void build_loop_tree(Block* blk);
   1.939 +
   1.940 +  // Create the block map, which indexes blocks in pre_order.
   1.941 +  void map_blocks();
   1.942 +
   1.943 +public:
   1.944 +  // Perform type inference flow analysis.
   1.945 +  void do_flow();
   1.946 +
   1.947 +  void print_on(outputStream* st) const PRODUCT_RETURN;
   1.948 +
   1.949 +  void rpo_print_on(outputStream* st) const PRODUCT_RETURN;
   1.950 +};
   1.951 +
   1.952 +#endif // SHARE_VM_CI_CITYPEFLOW_HPP

mercurial