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