1 /*
   2  * Copyright (c) 2000, 2012, 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_VM_CI_CITYPEFLOW_HPP
  26 #define SHARE_VM_CI_CITYPEFLOW_HPP
  27 
  28 #ifdef COMPILER2
  29 #include "ci/ciEnv.hpp"
  30 #include "ci/ciKlass.hpp"
  31 #include "ci/ciMethodBlocks.hpp"
  32 #endif
  33 
  34 
  35 class ciTypeFlow : public ResourceObj {
  36 private:
  37   ciEnv*    _env;
  38   ciMethod* _method;
  39   ciMethodBlocks* _methodBlocks;
  40   int       _osr_bci;
  41 
  42   // information cached from the method:
  43   int _max_locals;
  44   int _max_stack;
  45   int _code_size;
  46   bool      _has_irreducible_entry;
  47 
  48   const char* _failure_reason;
  49 
  50 public:
  51   class StateVector;
  52   class Loop;
  53   class Block;
  54 
  55   // Build a type flow analyzer
  56   // Do an OSR analysis if osr_bci >= 0.
  57   ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci = InvocationEntryBci);
  58 
  59   // Accessors
  60   ciMethod* method() const     { return _method; }
  61   ciEnv*    env()              { return _env; }
  62   Arena*    arena()            { return _env->arena(); }
  63   bool      is_osr_flow() const{ return _osr_bci != InvocationEntryBci; }
  64   int       start_bci() const  { return is_osr_flow()? _osr_bci: 0; }
  65   int       max_locals() const { return _max_locals; }
  66   int       max_stack() const  { return _max_stack; }
  67   int       max_cells() const  { return _max_locals + _max_stack; }
  68   int       code_size() const  { return _code_size; }
  69   bool      has_irreducible_entry() const { return _has_irreducible_entry; }
  70 
  71   // Represents information about an "active" jsr call.  This
  72   // class represents a call to the routine at some entry address
  73   // with some distinct return address.
  74   class JsrRecord : public ResourceObj {
  75   private:
  76     int _entry_address;
  77     int _return_address;
  78   public:
  79     JsrRecord(int entry_address, int return_address) {
  80       _entry_address = entry_address;
  81       _return_address = return_address;
  82     }
  83 
  84     int entry_address() const  { return _entry_address; }
  85     int return_address() const { return _return_address; }
  86 
  87     void print_on(outputStream* st) const {
  88 #ifndef PRODUCT
  89       st->print("%d->%d", entry_address(), return_address());
  90 #endif
  91     }
  92   };
  93 
  94   // A JsrSet represents some set of JsrRecords.  This class
  95   // is used to record a set of all jsr routines which we permit
  96   // execution to return (ret) from.
  97   //
  98   // During abstract interpretation, JsrSets are used to determine
  99   // whether two paths which reach a given block are unique, and
 100   // should be cloned apart, or are compatible, and should merge
 101   // together.
 102   //
 103   // Note that different amounts of effort can be expended determining
 104   // if paths are compatible.  <DISCUSSION>
 105   class JsrSet : public ResourceObj {
 106   private:
 107     GrowableArray<JsrRecord*>* _set;
 108 
 109     JsrRecord* record_at(int i) {
 110       return _set->at(i);
 111     }
 112 
 113     // Insert the given JsrRecord into the JsrSet, maintaining the order
 114     // of the set and replacing any element with the same entry address.
 115     void insert_jsr_record(JsrRecord* record);
 116 
 117     // Remove the JsrRecord with the given return address from the JsrSet.
 118     void remove_jsr_record(int return_address);
 119 
 120   public:
 121     JsrSet(Arena* arena, int default_len = 4);
 122 
 123     // Copy this JsrSet.
 124     void copy_into(JsrSet* jsrs);
 125 
 126     // Is this JsrSet compatible with some other JsrSet?
 127     bool is_compatible_with(JsrSet* other);
 128 
 129     // Apply the effect of a single bytecode to the JsrSet.
 130     void apply_control(ciTypeFlow* analyzer,
 131                        ciBytecodeStream* str,
 132                        StateVector* state);
 133 
 134     // What is the cardinality of this set?
 135     int size() const { return _set->length(); }
 136 
 137     void print_on(outputStream* st) const PRODUCT_RETURN;
 138   };
 139 
 140   class LocalSet VALUE_OBJ_CLASS_SPEC {
 141   private:
 142     enum Constants { max = 63 };
 143     uint64_t _bits;
 144   public:
 145     LocalSet() : _bits(0) {}
 146     void add(uint32_t i)        { if (i < (uint32_t)max) _bits |=  (1LL << i); }
 147     void add(LocalSet* ls)      { _bits |= ls->_bits; }
 148     bool test(uint32_t i) const { return i < (uint32_t)max ? (_bits>>i)&1U : true; }
 149     void clear()                { _bits = 0; }
 150     void print_on(outputStream* st, int limit) const  PRODUCT_RETURN;
 151   };
 152 
 153   // Used as a combined index for locals and temps
 154   enum Cell {
 155     Cell_0, Cell_max = INT_MAX
 156   };
 157 
 158   // A StateVector summarizes the type information at some
 159   // point in the program
 160   class StateVector : public ResourceObj {
 161   private:
 162     ciType**    _types;
 163     int         _stack_size;
 164     int         _monitor_count;
 165     ciTypeFlow* _outer;
 166 
 167     int         _trap_bci;
 168     int         _trap_index;
 169 
 170     LocalSet    _def_locals;  // For entire block
 171 
 172     static ciType* type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer);
 173 
 174   public:
 175     // Special elements in our type lattice.
 176     enum {
 177       T_TOP     = T_VOID,      // why not?
 178       T_BOTTOM  = T_CONFLICT,
 179       T_LONG2   = T_SHORT,     // 2nd word of T_LONG
 180       T_DOUBLE2 = T_CHAR,      // 2nd word of T_DOUBLE
 181       T_NULL    = T_BYTE       // for now.
 182     };
 183     static ciType* top_type()    { return ciType::make((BasicType)T_TOP); }
 184     static ciType* bottom_type() { return ciType::make((BasicType)T_BOTTOM); }
 185     static ciType* long2_type()  { return ciType::make((BasicType)T_LONG2); }
 186     static ciType* double2_type(){ return ciType::make((BasicType)T_DOUBLE2); }
 187     static ciType* null_type()   { return ciType::make((BasicType)T_NULL); }
 188 
 189     static ciType* half_type(ciType* t) {
 190       switch (t->basic_type()) {
 191       case T_LONG:    return long2_type();
 192       case T_DOUBLE:  return double2_type();
 193       default:        ShouldNotReachHere(); return NULL;
 194       }
 195     }
 196 
 197     // The meet operation for our type lattice.
 198     ciType* type_meet(ciType* t1, ciType* t2) {
 199       return type_meet_internal(t1, t2, outer());
 200     }
 201 
 202     // Accessors
 203     ciTypeFlow* outer() const          { return _outer; }
 204 
 205     int         stack_size() const     { return _stack_size; }
 206     void    set_stack_size(int ss)     { _stack_size = ss; }
 207 
 208     int         monitor_count() const  { return _monitor_count; }
 209     void    set_monitor_count(int mc)  { _monitor_count = mc; }
 210 
 211     LocalSet* def_locals() { return &_def_locals; }
 212     const LocalSet* def_locals() const { return &_def_locals; }
 213 
 214     static Cell start_cell()           { return (Cell)0; }
 215     static Cell next_cell(Cell c)      { return (Cell)(((int)c) + 1); }
 216     Cell        limit_cell() const {
 217       return (Cell)(outer()->max_locals() + stack_size());
 218     }
 219 
 220     // Cell creation
 221     Cell      local(int lnum) const {
 222       assert(lnum < outer()->max_locals(), "index check");
 223       return (Cell)(lnum);
 224     }
 225 
 226     Cell      stack(int snum) const {
 227       assert(snum < stack_size(), "index check");
 228       return (Cell)(outer()->max_locals() + snum);
 229     }
 230 
 231     Cell      tos() const { return stack(stack_size()-1); }
 232 
 233     // For external use only:
 234     ciType* local_type_at(int i) const { return type_at(local(i)); }
 235     ciType* stack_type_at(int i) const { return type_at(stack(i)); }
 236 
 237     // Accessors for the type of some Cell c
 238     ciType*   type_at(Cell c) const {
 239       assert(start_cell() <= c && c < limit_cell(), "out of bounds");
 240       return _types[c];
 241     }
 242 
 243     void      set_type_at(Cell c, ciType* type) {
 244       assert(start_cell() <= c && c < limit_cell(), "out of bounds");
 245       _types[c] = type;
 246     }
 247 
 248     // Top-of-stack operations.
 249     void      set_type_at_tos(ciType* type) { set_type_at(tos(), type); }
 250     ciType*   type_at_tos() const           { return type_at(tos()); }
 251 
 252     void      push(ciType* type) {
 253       _stack_size++;
 254       set_type_at_tos(type);
 255     }
 256     void      pop() {
 257       debug_only(set_type_at_tos(bottom_type()));
 258       _stack_size--;
 259     }
 260     ciType*   pop_value() {
 261       ciType* t = type_at_tos();
 262       pop();
 263       return t;
 264     }
 265 
 266     // Convenience operations.
 267     bool      is_reference(ciType* type) const {
 268       return type == null_type() || !type->is_primitive_type();
 269     }
 270     bool      is_int(ciType* type) const {
 271       return type->basic_type() == T_INT;
 272     }
 273     bool      is_long(ciType* type) const {
 274       return type->basic_type() == T_LONG;
 275     }
 276     bool      is_float(ciType* type) const {
 277       return type->basic_type() == T_FLOAT;
 278     }
 279     bool      is_double(ciType* type) const {
 280       return type->basic_type() == T_DOUBLE;
 281     }
 282 
 283     void store_to_local(int lnum) {
 284       _def_locals.add((uint) lnum);
 285     }
 286 
 287     void      push_translate(ciType* type);
 288 
 289     void      push_int() {
 290       push(ciType::make(T_INT));
 291     }
 292     void      pop_int() {
 293       assert(is_int(type_at_tos()), "must be integer");
 294       pop();
 295     }
 296     void      check_int(Cell c) {
 297       assert(is_int(type_at(c)), "must be integer");
 298     }
 299     void      push_double() {
 300       push(ciType::make(T_DOUBLE));
 301       push(double2_type());
 302     }
 303     void      pop_double() {
 304       assert(type_at_tos() == double2_type(), "must be 2nd half");
 305       pop();
 306       assert(is_double(type_at_tos()), "must be double");
 307       pop();
 308     }
 309     void      push_float() {
 310       push(ciType::make(T_FLOAT));
 311     }
 312     void      pop_float() {
 313       assert(is_float(type_at_tos()), "must be float");
 314       pop();
 315     }
 316     void      push_long() {
 317       push(ciType::make(T_LONG));
 318       push(long2_type());
 319     }
 320     void      pop_long() {
 321       assert(type_at_tos() == long2_type(), "must be 2nd half");
 322       pop();
 323       assert(is_long(type_at_tos()), "must be long");
 324       pop();
 325     }
 326     void      push_object(ciKlass* klass) {
 327       push(klass);
 328     }
 329     void      pop_object() {
 330       assert(is_reference(type_at_tos()), "must be reference type");
 331       pop();
 332     }
 333     void      pop_array() {
 334       assert(type_at_tos() == null_type() ||
 335              type_at_tos()->is_array_klass(), "must be array type");
 336       pop();
 337     }
 338     // pop_objArray and pop_typeArray narrow the tos to ciObjArrayKlass
 339     // or ciTypeArrayKlass (resp.).  In the rare case that an explicit
 340     // null is popped from the stack, we return NULL.  Caller beware.
 341     ciObjArrayKlass* pop_objArray() {
 342       ciType* array = pop_value();
 343       if (array == null_type())  return NULL;
 344       assert(array->is_obj_array_klass(), "must be object array type");
 345       return array->as_obj_array_klass();
 346     }
 347     ciTypeArrayKlass* pop_typeArray() {
 348       ciType* array = pop_value();
 349       if (array == null_type())  return NULL;
 350       assert(array->is_type_array_klass(), "must be prim array type");
 351       return array->as_type_array_klass();
 352     }
 353     void      push_null() {
 354       push(null_type());
 355     }
 356     void      do_null_assert(ciKlass* unloaded_klass);
 357 
 358     // Helper convenience routines.
 359     void do_aaload(ciBytecodeStream* str);
 360     void do_checkcast(ciBytecodeStream* str);
 361     void do_getfield(ciBytecodeStream* str);
 362     void do_getstatic(ciBytecodeStream* str);
 363     void do_invoke(ciBytecodeStream* str, bool has_receiver);
 364     void do_jsr(ciBytecodeStream* str);
 365     void do_ldc(ciBytecodeStream* str);
 366     void do_multianewarray(ciBytecodeStream* str);
 367     void do_new(ciBytecodeStream* str);
 368     void do_newarray(ciBytecodeStream* str);
 369     void do_putfield(ciBytecodeStream* str);
 370     void do_putstatic(ciBytecodeStream* str);
 371     void do_ret(ciBytecodeStream* str);
 372 
 373     void overwrite_local_double_long(int index) {
 374       // Invalidate the previous local if it contains first half of
 375       // a double or long value since it's seconf half is being overwritten.
 376       int prev_index = index - 1;
 377       if (prev_index >= 0 &&
 378           (is_double(type_at(local(prev_index))) ||
 379            is_long(type_at(local(prev_index))))) {
 380         set_type_at(local(prev_index), bottom_type());
 381       }
 382     }
 383 
 384     void load_local_object(int index) {
 385       ciType* type = type_at(local(index));
 386       assert(is_reference(type), "must be reference type");
 387       push(type);
 388     }
 389     void store_local_object(int index) {
 390       ciType* type = pop_value();
 391       assert(is_reference(type) || type->is_return_address(),
 392              "must be reference type or return address");
 393       overwrite_local_double_long(index);
 394       set_type_at(local(index), type);
 395       store_to_local(index);
 396     }
 397 
 398     void load_local_double(int index) {
 399       ciType* type = type_at(local(index));
 400       ciType* type2 = type_at(local(index+1));
 401       assert(is_double(type), "must be double type");
 402       assert(type2 == double2_type(), "must be 2nd half");
 403       push(type);
 404       push(double2_type());
 405     }
 406     void store_local_double(int index) {
 407       ciType* type2 = pop_value();
 408       ciType* type = pop_value();
 409       assert(is_double(type), "must be double");
 410       assert(type2 == double2_type(), "must be 2nd half");
 411       overwrite_local_double_long(index);
 412       set_type_at(local(index), type);
 413       set_type_at(local(index+1), type2);
 414       store_to_local(index);
 415       store_to_local(index+1);
 416     }
 417 
 418     void load_local_float(int index) {
 419       ciType* type = type_at(local(index));
 420       assert(is_float(type), "must be float type");
 421       push(type);
 422     }
 423     void store_local_float(int index) {
 424       ciType* type = pop_value();
 425       assert(is_float(type), "must be float type");
 426       overwrite_local_double_long(index);
 427       set_type_at(local(index), type);
 428       store_to_local(index);
 429     }
 430 
 431     void load_local_int(int index) {
 432       ciType* type = type_at(local(index));
 433       assert(is_int(type), "must be int type");
 434       push(type);
 435     }
 436     void store_local_int(int index) {
 437       ciType* type = pop_value();
 438       assert(is_int(type), "must be int type");
 439       overwrite_local_double_long(index);
 440       set_type_at(local(index), type);
 441       store_to_local(index);
 442     }
 443 
 444     void load_local_long(int index) {
 445       ciType* type = type_at(local(index));
 446       ciType* type2 = type_at(local(index+1));
 447       assert(is_long(type), "must be long type");
 448       assert(type2 == long2_type(), "must be 2nd half");
 449       push(type);
 450       push(long2_type());
 451     }
 452     void store_local_long(int index) {
 453       ciType* type2 = pop_value();
 454       ciType* type = pop_value();
 455       assert(is_long(type), "must be long");
 456       assert(type2 == long2_type(), "must be 2nd half");
 457       overwrite_local_double_long(index);
 458       set_type_at(local(index), type);
 459       set_type_at(local(index+1), type2);
 460       store_to_local(index);
 461       store_to_local(index+1);
 462     }
 463 
 464     // Stop interpretation of this path with a trap.
 465     void trap(ciBytecodeStream* str, ciKlass* klass, int index);
 466 
 467   public:
 468     StateVector(ciTypeFlow* outer);
 469 
 470     // Copy our value into some other StateVector
 471     void copy_into(StateVector* copy) const;
 472 
 473     // Meets this StateVector with another, destructively modifying this
 474     // one.  Returns true if any modification takes place.
 475     bool meet(const StateVector* incoming);
 476 
 477     // Ditto, except that the incoming state is coming from an exception.
 478     bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming);
 479 
 480     // Apply the effect of one bytecode to this StateVector
 481     bool apply_one_bytecode(ciBytecodeStream* stream);
 482 
 483     // What is the bci of the trap?
 484     int  trap_bci() { return _trap_bci; }
 485 
 486     // What is the index associated with the trap?
 487     int  trap_index() { return _trap_index; }
 488 
 489     void print_cell_on(outputStream* st, Cell c) const PRODUCT_RETURN;
 490     void print_on(outputStream* st) const              PRODUCT_RETURN;
 491   };
 492 
 493   // Parameter for "find_block" calls:
 494   // Describes the difference between a public and backedge copy.
 495   enum CreateOption {
 496     create_public_copy,
 497     create_backedge_copy,
 498     no_create
 499   };
 500 
 501   // Successor iterator
 502   class SuccIter : public StackObj {
 503   private:
 504     Block* _pred;
 505     int    _index;
 506     Block* _succ;
 507   public:
 508     SuccIter()                        : _pred(NULL), _index(-1), _succ(NULL) {}
 509     SuccIter(Block* pred)             : _pred(pred), _index(-1), _succ(NULL) { next(); }
 510     int    index()     { return _index; }
 511     Block* pred()      { return _pred; }           // Return predecessor
 512     bool   done()      { return _index < 0; }      // Finished?
 513     Block* succ()      { return _succ; }           // Return current successor
 514     void   next();                                 // Advance
 515     void   set_succ(Block* succ);                  // Update current successor
 516     bool   is_normal_ctrl() { return index() < _pred->successors()->length(); }
 517   };
 518 
 519   // A basic block
 520   class Block : public ResourceObj {
 521   private:
 522     ciBlock*                          _ciblock;
 523     GrowableArray<Block*>*           _exceptions;
 524     GrowableArray<ciInstanceKlass*>* _exc_klasses;
 525     GrowableArray<Block*>*           _successors;
 526     GrowableArray<Block*>*           _predecessors;
 527     StateVector*                     _state;
 528     JsrSet*                          _jsrs;
 529 
 530     int                              _trap_bci;
 531     int                              _trap_index;
 532 
 533     // pre_order, assigned at first visit. Used as block ID and "visited" tag
 534     int                              _pre_order;
 535 
 536     // A post-order, used to compute the reverse post order (RPO) provided to the client
 537     int                              _post_order;  // used to compute rpo
 538 
 539     // Has this block been cloned for a loop backedge?
 540     bool                             _backedge_copy;
 541 
 542     // This block is entry to irreducible loop.
 543     bool                             _irreducible_entry;
 544 
 545     // This block has monitor entry point.
 546     bool                             _has_monitorenter;
 547 
 548     // A pointer used for our internal work list
 549     bool                             _on_work_list;      // on the work list
 550     Block*                           _next;
 551     Block*                           _rpo_next;          // Reverse post order list
 552 
 553     // Loop info
 554     Loop*                            _loop;              // nearest loop
 555 
 556     ciBlock*     ciblock() const     { return _ciblock; }
 557     StateVector* state() const     { return _state; }
 558 
 559     // Compute the exceptional successors and types for this Block.
 560     void compute_exceptions();
 561 
 562   public:
 563     // constructors
 564     Block(ciTypeFlow* outer, ciBlock* ciblk, JsrSet* jsrs);
 565 
 566     void set_trap(int trap_bci, int trap_index) {
 567       _trap_bci = trap_bci;
 568       _trap_index = trap_index;
 569       assert(has_trap(), "");
 570     }
 571     bool has_trap()   const  { return _trap_bci != -1; }
 572     int  trap_bci()   const  { assert(has_trap(), ""); return _trap_bci; }
 573     int  trap_index() const  { assert(has_trap(), ""); return _trap_index; }
 574 
 575     // accessors
 576     ciTypeFlow* outer() const { return state()->outer(); }
 577     int start() const         { return _ciblock->start_bci(); }
 578     int limit() const         { return _ciblock->limit_bci(); }
 579     int control() const       { return _ciblock->control_bci(); }
 580     JsrSet* jsrs() const      { return _jsrs; }
 581 
 582     bool    is_backedge_copy() const       { return _backedge_copy; }
 583     void   set_backedge_copy(bool z);
 584     int        backedge_copy_count() const { return outer()->backedge_copy_count(ciblock()->index(), _jsrs); }
 585 
 586     // access to entry state
 587     int     stack_size() const         { return _state->stack_size(); }
 588     int     monitor_count() const      { return _state->monitor_count(); }
 589     ciType* local_type_at(int i) const { return _state->local_type_at(i); }
 590     ciType* stack_type_at(int i) const { return _state->stack_type_at(i); }
 591 
 592     // Data flow on locals
 593     bool is_invariant_local(uint v) const {
 594       assert(is_loop_head(), "only loop heads");
 595       // Find outermost loop with same loop head
 596       Loop* lp = loop();
 597       while (lp->parent() != NULL) {
 598         if (lp->parent()->head() != lp->head()) break;
 599         lp = lp->parent();
 600       }
 601       return !lp->def_locals()->test(v);
 602     }
 603     LocalSet* def_locals() { return _state->def_locals(); }
 604     const LocalSet* def_locals() const { return _state->def_locals(); }
 605 
 606     // Get the successors for this Block.
 607     GrowableArray<Block*>* successors(ciBytecodeStream* str,
 608                                       StateVector* state,
 609                                       JsrSet* jsrs);
 610     GrowableArray<Block*>* successors() {
 611       assert(_successors != NULL, "must be filled in");
 612       return _successors;
 613     }
 614 
 615     // Predecessors of this block (including exception edges)
 616     GrowableArray<Block*>* predecessors() {
 617       assert(_predecessors != NULL, "must be filled in");
 618       return _predecessors;
 619     }
 620 
 621     // Get the exceptional successors for this Block.
 622     GrowableArray<Block*>* exceptions() {
 623       if (_exceptions == NULL) {
 624         compute_exceptions();
 625       }
 626       return _exceptions;
 627     }
 628 
 629     // Get the exception klasses corresponding to the
 630     // exceptional successors for this Block.
 631     GrowableArray<ciInstanceKlass*>* exc_klasses() {
 632       if (_exc_klasses == NULL) {
 633         compute_exceptions();
 634       }
 635       return _exc_klasses;
 636     }
 637 
 638     // Is this Block compatible with a given JsrSet?
 639     bool is_compatible_with(JsrSet* other) {
 640       return _jsrs->is_compatible_with(other);
 641     }
 642 
 643     // Copy the value of our state vector into another.
 644     void copy_state_into(StateVector* copy) const {
 645       _state->copy_into(copy);
 646     }
 647 
 648     // Copy the value of our JsrSet into another
 649     void copy_jsrs_into(JsrSet* copy) const {
 650       _jsrs->copy_into(copy);
 651     }
 652 
 653     // Meets the start state of this block with another state, destructively
 654     // modifying this one.  Returns true if any modification takes place.
 655     bool meet(const StateVector* incoming) {
 656       return state()->meet(incoming);
 657     }
 658 
 659     // Ditto, except that the incoming state is coming from an
 660     // exception path.  This means the stack is replaced by the
 661     // appropriate exception type.
 662     bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming) {
 663       return state()->meet_exception(exc, incoming);
 664     }
 665 
 666     // Work list manipulation
 667     void   set_next(Block* block) { _next = block; }
 668     Block* next() const           { return _next; }
 669 
 670     void   set_on_work_list(bool c) { _on_work_list = c; }
 671     bool   is_on_work_list() const  { return _on_work_list; }
 672 
 673     bool   has_pre_order() const  { return _pre_order >= 0; }
 674     void   set_pre_order(int po)  { assert(!has_pre_order(), ""); _pre_order = po; }
 675     int    pre_order() const      { assert(has_pre_order(), ""); return _pre_order; }
 676     void   set_next_pre_order()   { set_pre_order(outer()->inc_next_pre_order()); }
 677     bool   is_start() const       { return _pre_order == outer()->start_block_num(); }
 678 
 679     // Reverse post order
 680     void   df_init();
 681     bool   has_post_order() const { return _post_order >= 0; }
 682     void   set_post_order(int po) { assert(!has_post_order() && po >= 0, ""); _post_order = po; }
 683     void   reset_post_order(int o){ _post_order = o; }
 684     int    post_order() const     { assert(has_post_order(), ""); return _post_order; }
 685 
 686     bool   has_rpo() const        { return has_post_order() && outer()->have_block_count(); }
 687     int    rpo() const            { assert(has_rpo(), ""); return outer()->block_count() - post_order() - 1; }
 688     void   set_rpo_next(Block* b) { _rpo_next = b; }
 689     Block* rpo_next()             { return _rpo_next; }
 690 
 691     // Loops
 692     Loop*  loop() const                  { return _loop; }
 693     void   set_loop(Loop* lp)            { _loop = lp; }
 694     bool   is_loop_head() const          { return _loop && _loop->head() == this; }
 695     void   set_irreducible_entry(bool c) { _irreducible_entry = c; }
 696     bool   is_irreducible_entry() const  { return _irreducible_entry; }
 697     void   set_has_monitorenter()        { _has_monitorenter = true; }
 698     bool   has_monitorenter() const      { return _has_monitorenter; }
 699     bool   is_visited() const            { return has_pre_order(); }
 700     bool   is_post_visited() const       { return has_post_order(); }
 701     bool   is_clonable_exit(Loop* lp);
 702     Block* looping_succ(Loop* lp);       // Successor inside of loop
 703     bool   is_single_entry_loop_head() const {
 704       if (!is_loop_head()) return false;
 705       for (Loop* lp = loop(); lp != NULL && lp->head() == this; lp = lp->parent())
 706         if (lp->is_irreducible()) return false;
 707       return true;
 708     }
 709 
 710     void   print_value_on(outputStream* st) const PRODUCT_RETURN;
 711     void   print_on(outputStream* st) const       PRODUCT_RETURN;
 712   };
 713 
 714   // Loop
 715   class Loop : public ResourceObj {
 716   private:
 717     Loop* _parent;
 718     Loop* _sibling;  // List of siblings, null terminated
 719     Loop* _child;    // Head of child list threaded thru sibling pointer
 720     Block* _head;    // Head of loop
 721     Block* _tail;    // Tail of loop
 722     bool   _irreducible;
 723     LocalSet _def_locals;
 724 
 725   public:
 726     Loop(Block* head, Block* tail) :
 727       _head(head),   _tail(tail),
 728       _parent(NULL), _sibling(NULL), _child(NULL),
 729       _irreducible(false), _def_locals() {}
 730 
 731     Loop* parent()  const { return _parent; }
 732     Loop* sibling() const { return _sibling; }
 733     Loop* child()   const { return _child; }
 734     Block* head()   const { return _head; }
 735     Block* tail()   const { return _tail; }
 736     void set_parent(Loop* p)  { _parent = p; }
 737     void set_sibling(Loop* s) { _sibling = s; }
 738     void set_child(Loop* c)   { _child = c; }
 739     void set_head(Block* hd)  { _head = hd; }
 740     void set_tail(Block* tl)  { _tail = tl; }
 741 
 742     int depth() const;              // nesting depth
 743 
 744     // Returns true if lp is a nested loop or us.
 745     bool contains(Loop* lp) const;
 746     bool contains(Block* blk) const { return contains(blk->loop()); }
 747 
 748     // Data flow on locals
 749     LocalSet* def_locals() { return &_def_locals; }
 750     const LocalSet* def_locals() const { return &_def_locals; }
 751 
 752     // Merge the branch lp into this branch, sorting on the loop head
 753     // pre_orders. Returns the new branch.
 754     Loop* sorted_merge(Loop* lp);
 755 
 756     // Mark non-single entry to loop
 757     void set_irreducible(Block* entry) {
 758       _irreducible = true;
 759       entry->set_irreducible_entry(true);
 760     }
 761     bool is_irreducible() const { return _irreducible; }
 762 
 763     bool is_root() const { return _tail->pre_order() == max_jint; }
 764 
 765     void print(outputStream* st = tty, int indent = 0) const PRODUCT_RETURN;
 766   };
 767 
 768   // Postorder iteration over the loop tree.
 769   class PostorderLoops : public StackObj {
 770   private:
 771     Loop* _root;
 772     Loop* _current;
 773   public:
 774     PostorderLoops(Loop* root) : _root(root), _current(root) {
 775       while (_current->child() != NULL) {
 776         _current = _current->child();
 777       }
 778     }
 779     bool done() { return _current == NULL; }  // Finished iterating?
 780     void next();                            // Advance to next loop
 781     Loop* current() { return _current; }      // Return current loop.
 782   };
 783 
 784   // Preorder iteration over the loop tree.
 785   class PreorderLoops : public StackObj {
 786   private:
 787     Loop* _root;
 788     Loop* _current;
 789   public:
 790     PreorderLoops(Loop* root) : _root(root), _current(root) {}
 791     bool done() { return _current == NULL; }  // Finished iterating?
 792     void next();                            // Advance to next loop
 793     Loop* current() { return _current; }      // Return current loop.
 794   };
 795 
 796   // Standard indexes of successors, for various bytecodes.
 797   enum {
 798     FALL_THROUGH   = 0,  // normal control
 799     IF_NOT_TAKEN   = 0,  // the not-taken branch of an if (i.e., fall-through)
 800     IF_TAKEN       = 1,  // the taken branch of an if
 801     GOTO_TARGET    = 0,  // unique successor for goto, jsr, or ret
 802     SWITCH_DEFAULT = 0,  // default branch of a switch
 803     SWITCH_CASES   = 1   // first index for any non-default switch branches
 804     // Unlike in other blocks, the successors of a switch are listed uniquely.
 805   };
 806 
 807 private:
 808   // A mapping from pre_order to Blocks.  This array is created
 809   // only at the end of the flow.
 810   Block** _block_map;
 811 
 812   // For each ciBlock index, a list of Blocks which share this ciBlock.
 813   GrowableArray<Block*>** _idx_to_blocklist;
 814   // count of ciBlocks
 815   int _ciblock_count;
 816 
 817   // Tells if a given instruction is able to generate an exception edge.
 818   bool can_trap(ciBytecodeStream& str);
 819 
 820   // Clone the loop heads. Returns true if any cloning occurred.
 821   bool clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set);
 822 
 823   // Clone lp's head and replace tail's successors with clone.
 824   Block* clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set);
 825 
 826 public:
 827   // Return the block beginning at bci which has a JsrSet compatible
 828   // with jsrs.
 829   Block* block_at(int bci, JsrSet* set, CreateOption option = create_public_copy);
 830 
 831   // block factory
 832   Block* get_block_for(int ciBlockIndex, JsrSet* jsrs, CreateOption option = create_public_copy);
 833 
 834   // How many of the blocks have the backedge_copy bit set?
 835   int backedge_copy_count(int ciBlockIndex, JsrSet* jsrs) const;
 836 
 837   // Return an existing block containing bci which has a JsrSet compatible
 838   // with jsrs, or NULL if there is none.
 839   Block* existing_block_at(int bci, JsrSet* set) { return block_at(bci, set, no_create); }
 840 
 841   // Tell whether the flow analysis has encountered an error of some sort.
 842   bool failing() { return env()->failing() || _failure_reason != NULL; }
 843 
 844   // Reason this compilation is failing, such as "too many basic blocks".
 845   const char* failure_reason() { return _failure_reason; }
 846 
 847   // Note a failure.
 848   void record_failure(const char* reason);
 849 
 850   // Return the block of a given pre-order number.
 851   int have_block_count() const      { return _block_map != NULL; }
 852   int block_count() const           { assert(have_block_count(), "");
 853                                       return _next_pre_order; }
 854   Block* pre_order_at(int po) const { assert(0 <= po && po < block_count(), "out of bounds");
 855                                       return _block_map[po]; }
 856   Block* start_block() const        { return pre_order_at(start_block_num()); }
 857   int start_block_num() const       { return 0; }
 858   Block* rpo_at(int rpo) const      { assert(0 <= rpo && rpo < block_count(), "out of bounds");
 859                                       return _block_map[rpo]; }
 860   int next_pre_order()              { return _next_pre_order; }
 861   int inc_next_pre_order()          { return _next_pre_order++; }
 862 
 863 private:
 864   // A work list used during flow analysis.
 865   Block* _work_list;
 866 
 867   // List of blocks in reverse post order
 868   Block* _rpo_list;
 869 
 870   // Next Block::_pre_order.  After mapping, doubles as block_count.
 871   int _next_pre_order;
 872 
 873   // Are there more blocks on the work list?
 874   bool work_list_empty() { return _work_list == NULL; }
 875 
 876   // Get the next basic block from our work list.
 877   Block* work_list_next();
 878 
 879   // Add a basic block to our work list.
 880   void add_to_work_list(Block* block);
 881 
 882   // Prepend a basic block to rpo list.
 883   void prepend_to_rpo_list(Block* blk) {
 884     blk->set_rpo_next(_rpo_list);
 885     _rpo_list = blk;
 886   }
 887 
 888   // Root of the loop tree
 889   Loop* _loop_tree_root;
 890 
 891   // State used for make_jsr_record
 892   int _jsr_count;
 893   GrowableArray<JsrRecord*>* _jsr_records;
 894 
 895 public:
 896   // Make a JsrRecord for a given (entry, return) pair, if such a record
 897   // does not already exist.
 898   JsrRecord* make_jsr_record(int entry_address, int return_address);
 899 
 900   void  set_loop_tree_root(Loop* ltr) { _loop_tree_root = ltr; }
 901   Loop* loop_tree_root()              { return _loop_tree_root; }
 902 
 903 private:
 904   // Get the initial state for start_bci:
 905   const StateVector* get_start_state();
 906 
 907   // Merge the current state into all exceptional successors at the
 908   // current point in the code.
 909   void flow_exceptions(GrowableArray<Block*>* exceptions,
 910                        GrowableArray<ciInstanceKlass*>* exc_klasses,
 911                        StateVector* state);
 912 
 913   // Merge the current state into all successors at the current point
 914   // in the code.
 915   void flow_successors(GrowableArray<Block*>* successors,
 916                        StateVector* state);
 917 
 918   // Interpret the effects of the bytecodes on the incoming state
 919   // vector of a basic block.  Push the changed state to succeeding
 920   // basic blocks.
 921   void flow_block(Block* block,
 922                   StateVector* scratch_state,
 923                   JsrSet* scratch_jsrs);
 924 
 925   // Perform the type flow analysis, creating and cloning Blocks as
 926   // necessary.
 927   void flow_types();
 928 
 929   // Perform the depth first type flow analysis. Helper for flow_types.
 930   void df_flow_types(Block* start,
 931                      bool do_flow,
 932                      StateVector* temp_vector,
 933                      JsrSet* temp_set);
 934 
 935   // Incrementally build loop tree.
 936   void build_loop_tree(Block* blk);
 937 
 938   // Create the block map, which indexes blocks in pre_order.
 939   void map_blocks();
 940 
 941 public:
 942   // Perform type inference flow analysis.
 943   void do_flow();
 944 
 945   // Determine if bci is dominated by dom_bci
 946   bool is_dominated_by(int bci, int dom_bci);
 947 
 948   void print_on(outputStream* st) const PRODUCT_RETURN;
 949 
 950   void rpo_print_on(outputStream* st) const PRODUCT_RETURN;
 951 };
 952 
 953 #endif // SHARE_VM_CI_CITYPEFLOW_HPP