1 /*
   2  * Copyright (c) 2000, 2010, 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 #include "incls/_precompiled.incl"
  26 #include "incls/_ciTypeFlow.cpp.incl"
  27 
  28 // ciTypeFlow::JsrSet
  29 //
  30 // A JsrSet represents some set of JsrRecords.  This class
  31 // is used to record a set of all jsr routines which we permit
  32 // execution to return (ret) from.
  33 //
  34 // During abstract interpretation, JsrSets are used to determine
  35 // whether two paths which reach a given block are unique, and
  36 // should be cloned apart, or are compatible, and should merge
  37 // together.
  38 
  39 // ------------------------------------------------------------------
  40 // ciTypeFlow::JsrSet::JsrSet
  41 ciTypeFlow::JsrSet::JsrSet(Arena* arena, int default_len) {
  42   if (arena != NULL) {
  43     // Allocate growable array in Arena.
  44     _set = new (arena) GrowableArray<JsrRecord*>(arena, default_len, 0, NULL);
  45   } else {
  46     // Allocate growable array in current ResourceArea.
  47     _set = new GrowableArray<JsrRecord*>(4, 0, NULL, false);
  48   }
  49 }
  50 
  51 // ------------------------------------------------------------------
  52 // ciTypeFlow::JsrSet::copy_into
  53 void ciTypeFlow::JsrSet::copy_into(JsrSet* jsrs) {
  54   int len = size();
  55   jsrs->_set->clear();
  56   for (int i = 0; i < len; i++) {
  57     jsrs->_set->append(_set->at(i));
  58   }
  59 }
  60 
  61 // ------------------------------------------------------------------
  62 // ciTypeFlow::JsrSet::is_compatible_with
  63 //
  64 // !!!! MISGIVINGS ABOUT THIS... disregard
  65 //
  66 // Is this JsrSet compatible with some other JsrSet?
  67 //
  68 // In set-theoretic terms, a JsrSet can be viewed as a partial function
  69 // from entry addresses to return addresses.  Two JsrSets A and B are
  70 // compatible iff
  71 //
  72 //   For any x,
  73 //   A(x) defined and B(x) defined implies A(x) == B(x)
  74 //
  75 // Less formally, two JsrSets are compatible when they have identical
  76 // return addresses for any entry addresses they share in common.
  77 bool ciTypeFlow::JsrSet::is_compatible_with(JsrSet* other) {
  78   // Walk through both sets in parallel.  If the same entry address
  79   // appears in both sets, then the return address must match for
  80   // the sets to be compatible.
  81   int size1 = size();
  82   int size2 = other->size();
  83 
  84   // Special case.  If nothing is on the jsr stack, then there can
  85   // be no ret.
  86   if (size2 == 0) {
  87     return true;
  88   } else if (size1 != size2) {
  89     return false;
  90   } else {
  91     for (int i = 0; i < size1; i++) {
  92       JsrRecord* record1 = record_at(i);
  93       JsrRecord* record2 = other->record_at(i);
  94       if (record1->entry_address() != record2->entry_address() ||
  95           record1->return_address() != record2->return_address()) {
  96         return false;
  97       }
  98     }
  99     return true;
 100   }
 101 
 102 #if 0
 103   int pos1 = 0;
 104   int pos2 = 0;
 105   int size1 = size();
 106   int size2 = other->size();
 107   while (pos1 < size1 && pos2 < size2) {
 108     JsrRecord* record1 = record_at(pos1);
 109     JsrRecord* record2 = other->record_at(pos2);
 110     int entry1 = record1->entry_address();
 111     int entry2 = record2->entry_address();
 112     if (entry1 < entry2) {
 113       pos1++;
 114     } else if (entry1 > entry2) {
 115       pos2++;
 116     } else {
 117       if (record1->return_address() == record2->return_address()) {
 118         pos1++;
 119         pos2++;
 120       } else {
 121         // These two JsrSets are incompatible.
 122         return false;
 123       }
 124     }
 125   }
 126   // The two JsrSets agree.
 127   return true;
 128 #endif
 129 }
 130 
 131 // ------------------------------------------------------------------
 132 // ciTypeFlow::JsrSet::insert_jsr_record
 133 //
 134 // Insert the given JsrRecord into the JsrSet, maintaining the order
 135 // of the set and replacing any element with the same entry address.
 136 void ciTypeFlow::JsrSet::insert_jsr_record(JsrRecord* record) {
 137   int len = size();
 138   int entry = record->entry_address();
 139   int pos = 0;
 140   for ( ; pos < len; pos++) {
 141     JsrRecord* current = record_at(pos);
 142     if (entry == current->entry_address()) {
 143       // Stomp over this entry.
 144       _set->at_put(pos, record);
 145       assert(size() == len, "must be same size");
 146       return;
 147     } else if (entry < current->entry_address()) {
 148       break;
 149     }
 150   }
 151 
 152   // Insert the record into the list.
 153   JsrRecord* swap = record;
 154   JsrRecord* temp = NULL;
 155   for ( ; pos < len; pos++) {
 156     temp = _set->at(pos);
 157     _set->at_put(pos, swap);
 158     swap = temp;
 159   }
 160   _set->append(swap);
 161   assert(size() == len+1, "must be larger");
 162 }
 163 
 164 // ------------------------------------------------------------------
 165 // ciTypeFlow::JsrSet::remove_jsr_record
 166 //
 167 // Remove the JsrRecord with the given return address from the JsrSet.
 168 void ciTypeFlow::JsrSet::remove_jsr_record(int return_address) {
 169   int len = size();
 170   for (int i = 0; i < len; i++) {
 171     if (record_at(i)->return_address() == return_address) {
 172       // We have found the proper entry.  Remove it from the
 173       // JsrSet and exit.
 174       for (int j = i+1; j < len ; j++) {
 175         _set->at_put(j-1, _set->at(j));
 176       }
 177       _set->trunc_to(len-1);
 178       assert(size() == len-1, "must be smaller");
 179       return;
 180     }
 181   }
 182   assert(false, "verify: returning from invalid subroutine");
 183 }
 184 
 185 // ------------------------------------------------------------------
 186 // ciTypeFlow::JsrSet::apply_control
 187 //
 188 // Apply the effect of a control-flow bytecode on the JsrSet.  The
 189 // only bytecodes that modify the JsrSet are jsr and ret.
 190 void ciTypeFlow::JsrSet::apply_control(ciTypeFlow* analyzer,
 191                                        ciBytecodeStream* str,
 192                                        ciTypeFlow::StateVector* state) {
 193   Bytecodes::Code code = str->cur_bc();
 194   if (code == Bytecodes::_jsr) {
 195     JsrRecord* record =
 196       analyzer->make_jsr_record(str->get_dest(), str->next_bci());
 197     insert_jsr_record(record);
 198   } else if (code == Bytecodes::_jsr_w) {
 199     JsrRecord* record =
 200       analyzer->make_jsr_record(str->get_far_dest(), str->next_bci());
 201     insert_jsr_record(record);
 202   } else if (code == Bytecodes::_ret) {
 203     Cell local = state->local(str->get_index());
 204     ciType* return_address = state->type_at(local);
 205     assert(return_address->is_return_address(), "verify: wrong type");
 206     if (size() == 0) {
 207       // Ret-state underflow:  Hit a ret w/o any previous jsrs.  Bail out.
 208       // This can happen when a loop is inside a finally clause (4614060).
 209       analyzer->record_failure("OSR in finally clause");
 210       return;
 211     }
 212     remove_jsr_record(return_address->as_return_address()->bci());
 213   }
 214 }
 215 
 216 #ifndef PRODUCT
 217 // ------------------------------------------------------------------
 218 // ciTypeFlow::JsrSet::print_on
 219 void ciTypeFlow::JsrSet::print_on(outputStream* st) const {
 220   st->print("{ ");
 221   int num_elements = size();
 222   if (num_elements > 0) {
 223     int i = 0;
 224     for( ; i < num_elements - 1; i++) {
 225       _set->at(i)->print_on(st);
 226       st->print(", ");
 227     }
 228     _set->at(i)->print_on(st);
 229     st->print(" ");
 230   }
 231   st->print("}");
 232 }
 233 #endif
 234 
 235 // ciTypeFlow::StateVector
 236 //
 237 // A StateVector summarizes the type information at some point in
 238 // the program.
 239 
 240 // ------------------------------------------------------------------
 241 // ciTypeFlow::StateVector::type_meet
 242 //
 243 // Meet two types.
 244 //
 245 // The semi-lattice of types use by this analysis are modeled on those
 246 // of the verifier.  The lattice is as follows:
 247 //
 248 //        top_type() >= all non-extremal types >= bottom_type
 249 //                             and
 250 //   Every primitive type is comparable only with itself.  The meet of
 251 //   reference types is determined by their kind: instance class,
 252 //   interface, or array class.  The meet of two types of the same
 253 //   kind is their least common ancestor.  The meet of two types of
 254 //   different kinds is always java.lang.Object.
 255 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
 256   assert(t1 != t2, "checked in caller");
 257   if (t1->equals(top_type())) {
 258     return t2;
 259   } else if (t2->equals(top_type())) {
 260     return t1;
 261   } else if (t1->is_primitive_type() || t2->is_primitive_type()) {
 262     // Special case null_type.  null_type meet any reference type T
 263     // is T.  null_type meet null_type is null_type.
 264     if (t1->equals(null_type())) {
 265       if (!t2->is_primitive_type() || t2->equals(null_type())) {
 266         return t2;
 267       }
 268     } else if (t2->equals(null_type())) {
 269       if (!t1->is_primitive_type()) {
 270         return t1;
 271       }
 272     }
 273 
 274     // At least one of the two types is a non-top primitive type.
 275     // The other type is not equal to it.  Fall to bottom.
 276     return bottom_type();
 277   } else {
 278     // Both types are non-top non-primitive types.  That is,
 279     // both types are either instanceKlasses or arrayKlasses.
 280     ciKlass* object_klass = analyzer->env()->Object_klass();
 281     ciKlass* k1 = t1->as_klass();
 282     ciKlass* k2 = t2->as_klass();
 283     if (k1->equals(object_klass) || k2->equals(object_klass)) {
 284       return object_klass;
 285     } else if (!k1->is_loaded() || !k2->is_loaded()) {
 286       // Unloaded classes fall to java.lang.Object at a merge.
 287       return object_klass;
 288     } else if (k1->is_interface() != k2->is_interface()) {
 289       // When an interface meets a non-interface, we get Object;
 290       // This is what the verifier does.
 291       return object_klass;
 292     } else if (k1->is_array_klass() || k2->is_array_klass()) {
 293       // When an array meets a non-array, we get Object.
 294       // When objArray meets typeArray, we also get Object.
 295       // And when typeArray meets different typeArray, we again get Object.
 296       // But when objArray meets objArray, we look carefully at element types.
 297       if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) {
 298         // Meet the element types, then construct the corresponding array type.
 299         ciKlass* elem1 = k1->as_obj_array_klass()->element_klass();
 300         ciKlass* elem2 = k2->as_obj_array_klass()->element_klass();
 301         ciKlass* elem  = type_meet_internal(elem1, elem2, analyzer)->as_klass();
 302         // Do an easy shortcut if one type is a super of the other.
 303         if (elem == elem1) {
 304           assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK");
 305           return k1;
 306         } else if (elem == elem2) {
 307           assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK");
 308           return k2;
 309         } else {
 310           return ciObjArrayKlass::make(elem);
 311         }
 312       } else {
 313         return object_klass;
 314       }
 315     } else {
 316       // Must be two plain old instance klasses.
 317       assert(k1->is_instance_klass(), "previous cases handle non-instances");
 318       assert(k2->is_instance_klass(), "previous cases handle non-instances");
 319       return k1->least_common_ancestor(k2);
 320     }
 321   }
 322 }
 323 
 324 
 325 // ------------------------------------------------------------------
 326 // ciTypeFlow::StateVector::StateVector
 327 //
 328 // Build a new state vector
 329 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
 330   _outer = analyzer;
 331   _stack_size = -1;
 332   _monitor_count = -1;
 333   // Allocate the _types array
 334   int max_cells = analyzer->max_cells();
 335   _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
 336   for (int i=0; i<max_cells; i++) {
 337     _types[i] = top_type();
 338   }
 339   _trap_bci = -1;
 340   _trap_index = 0;
 341   _def_locals.clear();
 342 }
 343 
 344 
 345 // ------------------------------------------------------------------
 346 // ciTypeFlow::get_start_state
 347 //
 348 // Set this vector to the method entry state.
 349 const ciTypeFlow::StateVector* ciTypeFlow::get_start_state() {
 350   StateVector* state = new StateVector(this);
 351   if (is_osr_flow()) {
 352     ciTypeFlow* non_osr_flow = method()->get_flow_analysis();
 353     if (non_osr_flow->failing()) {
 354       record_failure(non_osr_flow->failure_reason());
 355       return NULL;
 356     }
 357     JsrSet* jsrs = new JsrSet(NULL, 16);
 358     Block* non_osr_block = non_osr_flow->existing_block_at(start_bci(), jsrs);
 359     if (non_osr_block == NULL) {
 360       record_failure("cannot reach OSR point");
 361       return NULL;
 362     }
 363     // load up the non-OSR state at this point
 364     non_osr_block->copy_state_into(state);
 365     int non_osr_start = non_osr_block->start();
 366     if (non_osr_start != start_bci()) {
 367       // must flow forward from it
 368       if (CITraceTypeFlow) {
 369         tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
 370       }
 371       Block* block = block_at(non_osr_start, jsrs);
 372       assert(block->limit() == start_bci(), "must flow forward to start");
 373       flow_block(block, state, jsrs);
 374     }
 375     return state;
 376     // Note:  The code below would be an incorrect for an OSR flow,
 377     // even if it were possible for an OSR entry point to be at bci zero.
 378   }
 379   // "Push" the method signature into the first few locals.
 380   state->set_stack_size(-max_locals());
 381   if (!method()->is_static()) {
 382     state->push(method()->holder());
 383     assert(state->tos() == state->local(0), "");
 384   }
 385   for (ciSignatureStream str(method()->signature());
 386        !str.at_return_type();
 387        str.next()) {
 388     state->push_translate(str.type());
 389   }
 390   // Set the rest of the locals to bottom.
 391   Cell cell = state->next_cell(state->tos());
 392   state->set_stack_size(0);
 393   int limit = state->limit_cell();
 394   for (; cell < limit; cell = state->next_cell(cell)) {
 395     state->set_type_at(cell, state->bottom_type());
 396   }
 397   // Lock an object, if necessary.
 398   state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
 399   return state;
 400 }
 401 
 402 // ------------------------------------------------------------------
 403 // ciTypeFlow::StateVector::copy_into
 404 //
 405 // Copy our value into some other StateVector
 406 void ciTypeFlow::StateVector::copy_into(ciTypeFlow::StateVector* copy)
 407 const {
 408   copy->set_stack_size(stack_size());
 409   copy->set_monitor_count(monitor_count());
 410   Cell limit = limit_cell();
 411   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 412     copy->set_type_at(c, type_at(c));
 413   }
 414 }
 415 
 416 // ------------------------------------------------------------------
 417 // ciTypeFlow::StateVector::meet
 418 //
 419 // Meets this StateVector with another, destructively modifying this
 420 // one.  Returns true if any modification takes place.
 421 bool ciTypeFlow::StateVector::meet(const ciTypeFlow::StateVector* incoming) {
 422   if (monitor_count() == -1) {
 423     set_monitor_count(incoming->monitor_count());
 424   }
 425   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
 426 
 427   if (stack_size() == -1) {
 428     set_stack_size(incoming->stack_size());
 429     Cell limit = limit_cell();
 430     #ifdef ASSERT
 431     { for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 432         assert(type_at(c) == top_type(), "");
 433     } }
 434     #endif
 435     // Make a simple copy of the incoming state.
 436     for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 437       set_type_at(c, incoming->type_at(c));
 438     }
 439     return true;  // it is always different the first time
 440   }
 441 #ifdef ASSERT
 442   if (stack_size() != incoming->stack_size()) {
 443     _outer->method()->print_codes();
 444     tty->print_cr("!!!! Stack size conflict");
 445     tty->print_cr("Current state:");
 446     print_on(tty);
 447     tty->print_cr("Incoming state:");
 448     ((StateVector*)incoming)->print_on(tty);
 449   }
 450 #endif
 451   assert(stack_size() == incoming->stack_size(), "sanity");
 452 
 453   bool different = false;
 454   Cell limit = limit_cell();
 455   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 456     ciType* t1 = type_at(c);
 457     ciType* t2 = incoming->type_at(c);
 458     if (!t1->equals(t2)) {
 459       ciType* new_type = type_meet(t1, t2);
 460       if (!t1->equals(new_type)) {
 461         set_type_at(c, new_type);
 462         different = true;
 463       }
 464     }
 465   }
 466   return different;
 467 }
 468 
 469 // ------------------------------------------------------------------
 470 // ciTypeFlow::StateVector::meet_exception
 471 //
 472 // Meets this StateVector with another, destructively modifying this
 473 // one.  The incoming state is coming via an exception.  Returns true
 474 // if any modification takes place.
 475 bool ciTypeFlow::StateVector::meet_exception(ciInstanceKlass* exc,
 476                                      const ciTypeFlow::StateVector* incoming) {
 477   if (monitor_count() == -1) {
 478     set_monitor_count(incoming->monitor_count());
 479   }
 480   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
 481 
 482   if (stack_size() == -1) {
 483     set_stack_size(1);
 484   }
 485 
 486   assert(stack_size() ==  1, "must have one-element stack");
 487 
 488   bool different = false;
 489 
 490   // Meet locals from incoming array.
 491   Cell limit = local(_outer->max_locals()-1);
 492   for (Cell c = start_cell(); c <= limit; c = next_cell(c)) {
 493     ciType* t1 = type_at(c);
 494     ciType* t2 = incoming->type_at(c);
 495     if (!t1->equals(t2)) {
 496       ciType* new_type = type_meet(t1, t2);
 497       if (!t1->equals(new_type)) {
 498         set_type_at(c, new_type);
 499         different = true;
 500       }
 501     }
 502   }
 503 
 504   // Handle stack separately.  When an exception occurs, the
 505   // only stack entry is the exception instance.
 506   ciType* tos_type = type_at_tos();
 507   if (!tos_type->equals(exc)) {
 508     ciType* new_type = type_meet(tos_type, exc);
 509     if (!tos_type->equals(new_type)) {
 510       set_type_at_tos(new_type);
 511       different = true;
 512     }
 513   }
 514 
 515   return different;
 516 }
 517 
 518 // ------------------------------------------------------------------
 519 // ciTypeFlow::StateVector::push_translate
 520 void ciTypeFlow::StateVector::push_translate(ciType* type) {
 521   BasicType basic_type = type->basic_type();
 522   if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
 523       basic_type == T_BYTE    || basic_type == T_SHORT) {
 524     push_int();
 525   } else {
 526     push(type);
 527     if (type->is_two_word()) {
 528       push(half_type(type));
 529     }
 530   }
 531 }
 532 
 533 // ------------------------------------------------------------------
 534 // ciTypeFlow::StateVector::do_aaload
 535 void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
 536   pop_int();
 537   ciObjArrayKlass* array_klass = pop_objArray();
 538   if (array_klass == NULL) {
 539     // Did aaload on a null reference; push a null and ignore the exception.
 540     // This instruction will never continue normally.  All we have to do
 541     // is report a value that will meet correctly with any downstream
 542     // reference types on paths that will truly be executed.  This null type
 543     // meets with any reference type to yield that same reference type.
 544     // (The compiler will generate an unconditional exception here.)
 545     push(null_type());
 546     return;
 547   }
 548   if (!array_klass->is_loaded()) {
 549     // Only fails for some -Xcomp runs
 550     trap(str, array_klass,
 551          Deoptimization::make_trap_request
 552          (Deoptimization::Reason_unloaded,
 553           Deoptimization::Action_reinterpret));
 554     return;
 555   }
 556   ciKlass* element_klass = array_klass->element_klass();
 557   if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
 558     Untested("unloaded array element class in ciTypeFlow");
 559     trap(str, element_klass,
 560          Deoptimization::make_trap_request
 561          (Deoptimization::Reason_unloaded,
 562           Deoptimization::Action_reinterpret));
 563   } else {
 564     push_object(element_klass);
 565   }
 566 }
 567 
 568 
 569 // ------------------------------------------------------------------
 570 // ciTypeFlow::StateVector::do_checkcast
 571 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
 572   bool will_link;
 573   ciKlass* klass = str->get_klass(will_link);
 574   if (!will_link) {
 575     // VM's interpreter will not load 'klass' if object is NULL.
 576     // Type flow after this block may still be needed in two situations:
 577     // 1) C2 uses do_null_assert() and continues compilation for later blocks
 578     // 2) C2 does an OSR compile in a later block (see bug 4778368).
 579     pop_object();
 580     do_null_assert(klass);
 581   } else {
 582     pop_object();
 583     push_object(klass);
 584   }
 585 }
 586 
 587 // ------------------------------------------------------------------
 588 // ciTypeFlow::StateVector::do_getfield
 589 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
 590   // could add assert here for type of object.
 591   pop_object();
 592   do_getstatic(str);
 593 }
 594 
 595 // ------------------------------------------------------------------
 596 // ciTypeFlow::StateVector::do_getstatic
 597 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
 598   bool will_link;
 599   ciField* field = str->get_field(will_link);
 600   if (!will_link) {
 601     trap(str, field->holder(), str->get_field_holder_index());
 602   } else {
 603     ciType* field_type = field->type();
 604     if (!field_type->is_loaded()) {
 605       // Normally, we need the field's type to be loaded if we are to
 606       // do anything interesting with its value.
 607       // We used to do this:  trap(str, str->get_field_signature_index());
 608       //
 609       // There is one good reason not to trap here.  Execution can
 610       // get past this "getfield" or "getstatic" if the value of
 611       // the field is null.  As long as the value is null, the class
 612       // does not need to be loaded!  The compiler must assume that
 613       // the value of the unloaded class reference is null; if the code
 614       // ever sees a non-null value, loading has occurred.
 615       //
 616       // This actually happens often enough to be annoying.  If the
 617       // compiler throws an uncommon trap at this bytecode, you can
 618       // get an endless loop of recompilations, when all the code
 619       // needs to do is load a series of null values.  Also, a trap
 620       // here can make an OSR entry point unreachable, triggering the
 621       // assert on non_osr_block in ciTypeFlow::get_start_state.
 622       // (See bug 4379915.)
 623       do_null_assert(field_type->as_klass());
 624     } else {
 625       push_translate(field_type);
 626     }
 627   }
 628 }
 629 
 630 // ------------------------------------------------------------------
 631 // ciTypeFlow::StateVector::do_invoke
 632 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
 633                                         bool has_receiver) {
 634   bool will_link;
 635   ciMethod* method = str->get_method(will_link);
 636   if (!will_link) {
 637     // We weren't able to find the method.
 638     if (str->cur_bc() == Bytecodes::_invokedynamic) {
 639       trap(str, NULL,
 640            Deoptimization::make_trap_request
 641            (Deoptimization::Reason_uninitialized,
 642             Deoptimization::Action_reinterpret));
 643     } else {
 644       ciKlass* unloaded_holder = method->holder();
 645       trap(str, unloaded_holder, str->get_method_holder_index());
 646     }
 647   } else {
 648     ciSignature* signature = method->signature();
 649     ciSignatureStream sigstr(signature);
 650     int arg_size = signature->size();
 651     int stack_base = stack_size() - arg_size;
 652     int i = 0;
 653     for( ; !sigstr.at_return_type(); sigstr.next()) {
 654       ciType* type = sigstr.type();
 655       ciType* stack_type = type_at(stack(stack_base + i++));
 656       // Do I want to check this type?
 657       // assert(stack_type->is_subtype_of(type), "bad type for field value");
 658       if (type->is_two_word()) {
 659         ciType* stack_type2 = type_at(stack(stack_base + i++));
 660         assert(stack_type2->equals(half_type(type)), "must be 2nd half");
 661       }
 662     }
 663     assert(arg_size == i, "must match");
 664     for (int j = 0; j < arg_size; j++) {
 665       pop();
 666     }
 667     if (has_receiver) {
 668       // Check this?
 669       pop_object();
 670     }
 671     assert(!sigstr.is_done(), "must have return type");
 672     ciType* return_type = sigstr.type();
 673     if (!return_type->is_void()) {
 674       if (!return_type->is_loaded()) {
 675         // As in do_getstatic(), generally speaking, we need the return type to
 676         // be loaded if we are to do anything interesting with its value.
 677         // We used to do this:  trap(str, str->get_method_signature_index());
 678         //
 679         // We do not trap here since execution can get past this invoke if
 680         // the return value is null.  As long as the value is null, the class
 681         // does not need to be loaded!  The compiler must assume that
 682         // the value of the unloaded class reference is null; if the code
 683         // ever sees a non-null value, loading has occurred.
 684         //
 685         // See do_getstatic() for similar explanation, as well as bug 4684993.
 686         do_null_assert(return_type->as_klass());
 687       } else {
 688         push_translate(return_type);
 689       }
 690     }
 691   }
 692 }
 693 
 694 // ------------------------------------------------------------------
 695 // ciTypeFlow::StateVector::do_jsr
 696 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
 697   push(ciReturnAddress::make(str->next_bci()));
 698 }
 699 
 700 // ------------------------------------------------------------------
 701 // ciTypeFlow::StateVector::do_ldc
 702 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
 703   ciConstant con = str->get_constant();
 704   BasicType basic_type = con.basic_type();
 705   if (basic_type == T_ILLEGAL) {
 706     // OutOfMemoryError in the CI while loading constant
 707     push_null();
 708     outer()->record_failure("ldc did not link");
 709     return;
 710   }
 711   if (basic_type == T_OBJECT || basic_type == T_ARRAY) {
 712     ciObject* obj = con.as_object();
 713     if (obj->is_null_object()) {
 714       push_null();
 715     } else {
 716       assert(!obj->is_klass(), "must be java_mirror of klass");
 717       push_object(obj->klass());
 718     }
 719   } else {
 720     push_translate(ciType::make(basic_type));
 721   }
 722 }
 723 
 724 // ------------------------------------------------------------------
 725 // ciTypeFlow::StateVector::do_multianewarray
 726 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
 727   int dimensions = str->get_dimensions();
 728   bool will_link;
 729   ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
 730   if (!will_link) {
 731     trap(str, array_klass, str->get_klass_index());
 732   } else {
 733     for (int i = 0; i < dimensions; i++) {
 734       pop_int();
 735     }
 736     push_object(array_klass);
 737   }
 738 }
 739 
 740 // ------------------------------------------------------------------
 741 // ciTypeFlow::StateVector::do_new
 742 void ciTypeFlow::StateVector::do_new(ciBytecodeStream* str) {
 743   bool will_link;
 744   ciKlass* klass = str->get_klass(will_link);
 745   if (!will_link || str->is_unresolved_klass()) {
 746     trap(str, klass, str->get_klass_index());
 747   } else {
 748     push_object(klass);
 749   }
 750 }
 751 
 752 // ------------------------------------------------------------------
 753 // ciTypeFlow::StateVector::do_newarray
 754 void ciTypeFlow::StateVector::do_newarray(ciBytecodeStream* str) {
 755   pop_int();
 756   ciKlass* klass = ciTypeArrayKlass::make((BasicType)str->get_index());
 757   push_object(klass);
 758 }
 759 
 760 // ------------------------------------------------------------------
 761 // ciTypeFlow::StateVector::do_putfield
 762 void ciTypeFlow::StateVector::do_putfield(ciBytecodeStream* str) {
 763   do_putstatic(str);
 764   if (_trap_bci != -1)  return;  // unloaded field holder, etc.
 765   // could add assert here for type of object.
 766   pop_object();
 767 }
 768 
 769 // ------------------------------------------------------------------
 770 // ciTypeFlow::StateVector::do_putstatic
 771 void ciTypeFlow::StateVector::do_putstatic(ciBytecodeStream* str) {
 772   bool will_link;
 773   ciField* field = str->get_field(will_link);
 774   if (!will_link) {
 775     trap(str, field->holder(), str->get_field_holder_index());
 776   } else {
 777     ciType* field_type = field->type();
 778     ciType* type = pop_value();
 779     // Do I want to check this type?
 780     //      assert(type->is_subtype_of(field_type), "bad type for field value");
 781     if (field_type->is_two_word()) {
 782       ciType* type2 = pop_value();
 783       assert(type2->is_two_word(), "must be 2nd half");
 784       assert(type == half_type(type2), "must be 2nd half");
 785     }
 786   }
 787 }
 788 
 789 // ------------------------------------------------------------------
 790 // ciTypeFlow::StateVector::do_ret
 791 void ciTypeFlow::StateVector::do_ret(ciBytecodeStream* str) {
 792   Cell index = local(str->get_index());
 793 
 794   ciType* address = type_at(index);
 795   assert(address->is_return_address(), "bad return address");
 796   set_type_at(index, bottom_type());
 797 }
 798 
 799 // ------------------------------------------------------------------
 800 // ciTypeFlow::StateVector::trap
 801 //
 802 // Stop interpretation of this path with a trap.
 803 void ciTypeFlow::StateVector::trap(ciBytecodeStream* str, ciKlass* klass, int index) {
 804   _trap_bci = str->cur_bci();
 805   _trap_index = index;
 806 
 807   // Log information about this trap:
 808   CompileLog* log = outer()->env()->log();
 809   if (log != NULL) {
 810     int mid = log->identify(outer()->method());
 811     int kid = (klass == NULL)? -1: log->identify(klass);
 812     log->begin_elem("uncommon_trap method='%d' bci='%d'", mid, str->cur_bci());
 813     char buf[100];
 814     log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf),
 815                                                           index));
 816     if (kid >= 0)
 817       log->print(" klass='%d'", kid);
 818     log->end_elem();
 819   }
 820 }
 821 
 822 // ------------------------------------------------------------------
 823 // ciTypeFlow::StateVector::do_null_assert
 824 // Corresponds to graphKit::do_null_assert.
 825 void ciTypeFlow::StateVector::do_null_assert(ciKlass* unloaded_klass) {
 826   if (unloaded_klass->is_loaded()) {
 827     // We failed to link, but we can still compute with this class,
 828     // since it is loaded somewhere.  The compiler will uncommon_trap
 829     // if the object is not null, but the typeflow pass can not assume
 830     // that the object will be null, otherwise it may incorrectly tell
 831     // the parser that an object is known to be null. 4761344, 4807707
 832     push_object(unloaded_klass);
 833   } else {
 834     // The class is not loaded anywhere.  It is safe to model the
 835     // null in the typestates, because we can compile in a null check
 836     // which will deoptimize us if someone manages to load the
 837     // class later.
 838     push_null();
 839   }
 840 }
 841 
 842 
 843 // ------------------------------------------------------------------
 844 // ciTypeFlow::StateVector::apply_one_bytecode
 845 //
 846 // Apply the effect of one bytecode to this StateVector
 847 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
 848   _trap_bci = -1;
 849   _trap_index = 0;
 850 
 851   if (CITraceTypeFlow) {
 852     tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
 853                   Bytecodes::name(str->cur_bc()));
 854   }
 855 
 856   switch(str->cur_bc()) {
 857   case Bytecodes::_aaload: do_aaload(str);                       break;
 858 
 859   case Bytecodes::_aastore:
 860     {
 861       pop_object();
 862       pop_int();
 863       pop_objArray();
 864       break;
 865     }
 866   case Bytecodes::_aconst_null:
 867     {
 868       push_null();
 869       break;
 870     }
 871   case Bytecodes::_aload:   load_local_object(str->get_index());    break;
 872   case Bytecodes::_aload_0: load_local_object(0);                   break;
 873   case Bytecodes::_aload_1: load_local_object(1);                   break;
 874   case Bytecodes::_aload_2: load_local_object(2);                   break;
 875   case Bytecodes::_aload_3: load_local_object(3);                   break;
 876 
 877   case Bytecodes::_anewarray:
 878     {
 879       pop_int();
 880       bool will_link;
 881       ciKlass* element_klass = str->get_klass(will_link);
 882       if (!will_link) {
 883         trap(str, element_klass, str->get_klass_index());
 884       } else {
 885         push_object(ciObjArrayKlass::make(element_klass));
 886       }
 887       break;
 888     }
 889   case Bytecodes::_areturn:
 890   case Bytecodes::_ifnonnull:
 891   case Bytecodes::_ifnull:
 892     {
 893       pop_object();
 894       break;
 895     }
 896   case Bytecodes::_monitorenter:
 897     {
 898       pop_object();
 899       set_monitor_count(monitor_count() + 1);
 900       break;
 901     }
 902   case Bytecodes::_monitorexit:
 903     {
 904       pop_object();
 905       assert(monitor_count() > 0, "must be a monitor to exit from");
 906       set_monitor_count(monitor_count() - 1);
 907       break;
 908     }
 909   case Bytecodes::_arraylength:
 910     {
 911       pop_array();
 912       push_int();
 913       break;
 914     }
 915   case Bytecodes::_astore:   store_local_object(str->get_index());  break;
 916   case Bytecodes::_astore_0: store_local_object(0);                 break;
 917   case Bytecodes::_astore_1: store_local_object(1);                 break;
 918   case Bytecodes::_astore_2: store_local_object(2);                 break;
 919   case Bytecodes::_astore_3: store_local_object(3);                 break;
 920 
 921   case Bytecodes::_athrow:
 922     {
 923       NEEDS_CLEANUP;
 924       pop_object();
 925       break;
 926     }
 927   case Bytecodes::_baload:
 928   case Bytecodes::_caload:
 929   case Bytecodes::_iaload:
 930   case Bytecodes::_saload:
 931     {
 932       pop_int();
 933       ciTypeArrayKlass* array_klass = pop_typeArray();
 934       // Put assert here for right type?
 935       push_int();
 936       break;
 937     }
 938   case Bytecodes::_bastore:
 939   case Bytecodes::_castore:
 940   case Bytecodes::_iastore:
 941   case Bytecodes::_sastore:
 942     {
 943       pop_int();
 944       pop_int();
 945       pop_typeArray();
 946       // assert here?
 947       break;
 948     }
 949   case Bytecodes::_bipush:
 950   case Bytecodes::_iconst_m1:
 951   case Bytecodes::_iconst_0:
 952   case Bytecodes::_iconst_1:
 953   case Bytecodes::_iconst_2:
 954   case Bytecodes::_iconst_3:
 955   case Bytecodes::_iconst_4:
 956   case Bytecodes::_iconst_5:
 957   case Bytecodes::_sipush:
 958     {
 959       push_int();
 960       break;
 961     }
 962   case Bytecodes::_checkcast: do_checkcast(str);                  break;
 963 
 964   case Bytecodes::_d2f:
 965     {
 966       pop_double();
 967       push_float();
 968       break;
 969     }
 970   case Bytecodes::_d2i:
 971     {
 972       pop_double();
 973       push_int();
 974       break;
 975     }
 976   case Bytecodes::_d2l:
 977     {
 978       pop_double();
 979       push_long();
 980       break;
 981     }
 982   case Bytecodes::_dadd:
 983   case Bytecodes::_ddiv:
 984   case Bytecodes::_dmul:
 985   case Bytecodes::_drem:
 986   case Bytecodes::_dsub:
 987     {
 988       pop_double();
 989       pop_double();
 990       push_double();
 991       break;
 992     }
 993   case Bytecodes::_daload:
 994     {
 995       pop_int();
 996       ciTypeArrayKlass* array_klass = pop_typeArray();
 997       // Put assert here for right type?
 998       push_double();
 999       break;
1000     }
1001   case Bytecodes::_dastore:
1002     {
1003       pop_double();
1004       pop_int();
1005       pop_typeArray();
1006       // assert here?
1007       break;
1008     }
1009   case Bytecodes::_dcmpg:
1010   case Bytecodes::_dcmpl:
1011     {
1012       pop_double();
1013       pop_double();
1014       push_int();
1015       break;
1016     }
1017   case Bytecodes::_dconst_0:
1018   case Bytecodes::_dconst_1:
1019     {
1020       push_double();
1021       break;
1022     }
1023   case Bytecodes::_dload:   load_local_double(str->get_index());    break;
1024   case Bytecodes::_dload_0: load_local_double(0);                   break;
1025   case Bytecodes::_dload_1: load_local_double(1);                   break;
1026   case Bytecodes::_dload_2: load_local_double(2);                   break;
1027   case Bytecodes::_dload_3: load_local_double(3);                   break;
1028 
1029   case Bytecodes::_dneg:
1030     {
1031       pop_double();
1032       push_double();
1033       break;
1034     }
1035   case Bytecodes::_dreturn:
1036     {
1037       pop_double();
1038       break;
1039     }
1040   case Bytecodes::_dstore:   store_local_double(str->get_index());  break;
1041   case Bytecodes::_dstore_0: store_local_double(0);                 break;
1042   case Bytecodes::_dstore_1: store_local_double(1);                 break;
1043   case Bytecodes::_dstore_2: store_local_double(2);                 break;
1044   case Bytecodes::_dstore_3: store_local_double(3);                 break;
1045 
1046   case Bytecodes::_dup:
1047     {
1048       push(type_at_tos());
1049       break;
1050     }
1051   case Bytecodes::_dup_x1:
1052     {
1053       ciType* value1 = pop_value();
1054       ciType* value2 = pop_value();
1055       push(value1);
1056       push(value2);
1057       push(value1);
1058       break;
1059     }
1060   case Bytecodes::_dup_x2:
1061     {
1062       ciType* value1 = pop_value();
1063       ciType* value2 = pop_value();
1064       ciType* value3 = pop_value();
1065       push(value1);
1066       push(value3);
1067       push(value2);
1068       push(value1);
1069       break;
1070     }
1071   case Bytecodes::_dup2:
1072     {
1073       ciType* value1 = pop_value();
1074       ciType* value2 = pop_value();
1075       push(value2);
1076       push(value1);
1077       push(value2);
1078       push(value1);
1079       break;
1080     }
1081   case Bytecodes::_dup2_x1:
1082     {
1083       ciType* value1 = pop_value();
1084       ciType* value2 = pop_value();
1085       ciType* value3 = pop_value();
1086       push(value2);
1087       push(value1);
1088       push(value3);
1089       push(value2);
1090       push(value1);
1091       break;
1092     }
1093   case Bytecodes::_dup2_x2:
1094     {
1095       ciType* value1 = pop_value();
1096       ciType* value2 = pop_value();
1097       ciType* value3 = pop_value();
1098       ciType* value4 = pop_value();
1099       push(value2);
1100       push(value1);
1101       push(value4);
1102       push(value3);
1103       push(value2);
1104       push(value1);
1105       break;
1106     }
1107   case Bytecodes::_f2d:
1108     {
1109       pop_float();
1110       push_double();
1111       break;
1112     }
1113   case Bytecodes::_f2i:
1114     {
1115       pop_float();
1116       push_int();
1117       break;
1118     }
1119   case Bytecodes::_f2l:
1120     {
1121       pop_float();
1122       push_long();
1123       break;
1124     }
1125   case Bytecodes::_fadd:
1126   case Bytecodes::_fdiv:
1127   case Bytecodes::_fmul:
1128   case Bytecodes::_frem:
1129   case Bytecodes::_fsub:
1130     {
1131       pop_float();
1132       pop_float();
1133       push_float();
1134       break;
1135     }
1136   case Bytecodes::_faload:
1137     {
1138       pop_int();
1139       ciTypeArrayKlass* array_klass = pop_typeArray();
1140       // Put assert here.
1141       push_float();
1142       break;
1143     }
1144   case Bytecodes::_fastore:
1145     {
1146       pop_float();
1147       pop_int();
1148       ciTypeArrayKlass* array_klass = pop_typeArray();
1149       // Put assert here.
1150       break;
1151     }
1152   case Bytecodes::_fcmpg:
1153   case Bytecodes::_fcmpl:
1154     {
1155       pop_float();
1156       pop_float();
1157       push_int();
1158       break;
1159     }
1160   case Bytecodes::_fconst_0:
1161   case Bytecodes::_fconst_1:
1162   case Bytecodes::_fconst_2:
1163     {
1164       push_float();
1165       break;
1166     }
1167   case Bytecodes::_fload:   load_local_float(str->get_index());     break;
1168   case Bytecodes::_fload_0: load_local_float(0);                    break;
1169   case Bytecodes::_fload_1: load_local_float(1);                    break;
1170   case Bytecodes::_fload_2: load_local_float(2);                    break;
1171   case Bytecodes::_fload_3: load_local_float(3);                    break;
1172 
1173   case Bytecodes::_fneg:
1174     {
1175       pop_float();
1176       push_float();
1177       break;
1178     }
1179   case Bytecodes::_freturn:
1180     {
1181       pop_float();
1182       break;
1183     }
1184   case Bytecodes::_fstore:    store_local_float(str->get_index());   break;
1185   case Bytecodes::_fstore_0:  store_local_float(0);                  break;
1186   case Bytecodes::_fstore_1:  store_local_float(1);                  break;
1187   case Bytecodes::_fstore_2:  store_local_float(2);                  break;
1188   case Bytecodes::_fstore_3:  store_local_float(3);                  break;
1189 
1190   case Bytecodes::_getfield:  do_getfield(str);                      break;
1191   case Bytecodes::_getstatic: do_getstatic(str);                     break;
1192 
1193   case Bytecodes::_goto:
1194   case Bytecodes::_goto_w:
1195   case Bytecodes::_nop:
1196   case Bytecodes::_return:
1197     {
1198       // do nothing.
1199       break;
1200     }
1201   case Bytecodes::_i2b:
1202   case Bytecodes::_i2c:
1203   case Bytecodes::_i2s:
1204   case Bytecodes::_ineg:
1205     {
1206       pop_int();
1207       push_int();
1208       break;
1209     }
1210   case Bytecodes::_i2d:
1211     {
1212       pop_int();
1213       push_double();
1214       break;
1215     }
1216   case Bytecodes::_i2f:
1217     {
1218       pop_int();
1219       push_float();
1220       break;
1221     }
1222   case Bytecodes::_i2l:
1223     {
1224       pop_int();
1225       push_long();
1226       break;
1227     }
1228   case Bytecodes::_iadd:
1229   case Bytecodes::_iand:
1230   case Bytecodes::_idiv:
1231   case Bytecodes::_imul:
1232   case Bytecodes::_ior:
1233   case Bytecodes::_irem:
1234   case Bytecodes::_ishl:
1235   case Bytecodes::_ishr:
1236   case Bytecodes::_isub:
1237   case Bytecodes::_iushr:
1238   case Bytecodes::_ixor:
1239     {
1240       pop_int();
1241       pop_int();
1242       push_int();
1243       break;
1244     }
1245   case Bytecodes::_if_acmpeq:
1246   case Bytecodes::_if_acmpne:
1247     {
1248       pop_object();
1249       pop_object();
1250       break;
1251     }
1252   case Bytecodes::_if_icmpeq:
1253   case Bytecodes::_if_icmpge:
1254   case Bytecodes::_if_icmpgt:
1255   case Bytecodes::_if_icmple:
1256   case Bytecodes::_if_icmplt:
1257   case Bytecodes::_if_icmpne:
1258     {
1259       pop_int();
1260       pop_int();
1261       break;
1262     }
1263   case Bytecodes::_ifeq:
1264   case Bytecodes::_ifle:
1265   case Bytecodes::_iflt:
1266   case Bytecodes::_ifge:
1267   case Bytecodes::_ifgt:
1268   case Bytecodes::_ifne:
1269   case Bytecodes::_ireturn:
1270   case Bytecodes::_lookupswitch:
1271   case Bytecodes::_tableswitch:
1272     {
1273       pop_int();
1274       break;
1275     }
1276   case Bytecodes::_iinc:
1277     {
1278       int lnum = str->get_index();
1279       check_int(local(lnum));
1280       store_to_local(lnum);
1281       break;
1282     }
1283   case Bytecodes::_iload:   load_local_int(str->get_index()); break;
1284   case Bytecodes::_iload_0: load_local_int(0);                      break;
1285   case Bytecodes::_iload_1: load_local_int(1);                      break;
1286   case Bytecodes::_iload_2: load_local_int(2);                      break;
1287   case Bytecodes::_iload_3: load_local_int(3);                      break;
1288 
1289   case Bytecodes::_instanceof:
1290     {
1291       // Check for uncommon trap:
1292       do_checkcast(str);
1293       pop_object();
1294       push_int();
1295       break;
1296     }
1297   case Bytecodes::_invokeinterface: do_invoke(str, true);           break;
1298   case Bytecodes::_invokespecial:   do_invoke(str, true);           break;
1299   case Bytecodes::_invokestatic:    do_invoke(str, false);          break;
1300   case Bytecodes::_invokevirtual:   do_invoke(str, true);           break;
1301   case Bytecodes::_invokedynamic:   do_invoke(str, false);          break;
1302 
1303   case Bytecodes::_istore:   store_local_int(str->get_index());     break;
1304   case Bytecodes::_istore_0: store_local_int(0);                    break;
1305   case Bytecodes::_istore_1: store_local_int(1);                    break;
1306   case Bytecodes::_istore_2: store_local_int(2);                    break;
1307   case Bytecodes::_istore_3: store_local_int(3);                    break;
1308 
1309   case Bytecodes::_jsr:
1310   case Bytecodes::_jsr_w: do_jsr(str);                              break;
1311 
1312   case Bytecodes::_l2d:
1313     {
1314       pop_long();
1315       push_double();
1316       break;
1317     }
1318   case Bytecodes::_l2f:
1319     {
1320       pop_long();
1321       push_float();
1322       break;
1323     }
1324   case Bytecodes::_l2i:
1325     {
1326       pop_long();
1327       push_int();
1328       break;
1329     }
1330   case Bytecodes::_ladd:
1331   case Bytecodes::_land:
1332   case Bytecodes::_ldiv:
1333   case Bytecodes::_lmul:
1334   case Bytecodes::_lor:
1335   case Bytecodes::_lrem:
1336   case Bytecodes::_lsub:
1337   case Bytecodes::_lxor:
1338     {
1339       pop_long();
1340       pop_long();
1341       push_long();
1342       break;
1343     }
1344   case Bytecodes::_laload:
1345     {
1346       pop_int();
1347       ciTypeArrayKlass* array_klass = pop_typeArray();
1348       // Put assert here for right type?
1349       push_long();
1350       break;
1351     }
1352   case Bytecodes::_lastore:
1353     {
1354       pop_long();
1355       pop_int();
1356       pop_typeArray();
1357       // assert here?
1358       break;
1359     }
1360   case Bytecodes::_lcmp:
1361     {
1362       pop_long();
1363       pop_long();
1364       push_int();
1365       break;
1366     }
1367   case Bytecodes::_lconst_0:
1368   case Bytecodes::_lconst_1:
1369     {
1370       push_long();
1371       break;
1372     }
1373   case Bytecodes::_ldc:
1374   case Bytecodes::_ldc_w:
1375   case Bytecodes::_ldc2_w:
1376     {
1377       do_ldc(str);
1378       break;
1379     }
1380 
1381   case Bytecodes::_lload:   load_local_long(str->get_index());      break;
1382   case Bytecodes::_lload_0: load_local_long(0);                     break;
1383   case Bytecodes::_lload_1: load_local_long(1);                     break;
1384   case Bytecodes::_lload_2: load_local_long(2);                     break;
1385   case Bytecodes::_lload_3: load_local_long(3);                     break;
1386 
1387   case Bytecodes::_lneg:
1388     {
1389       pop_long();
1390       push_long();
1391       break;
1392     }
1393   case Bytecodes::_lreturn:
1394     {
1395       pop_long();
1396       break;
1397     }
1398   case Bytecodes::_lshl:
1399   case Bytecodes::_lshr:
1400   case Bytecodes::_lushr:
1401     {
1402       pop_int();
1403       pop_long();
1404       push_long();
1405       break;
1406     }
1407   case Bytecodes::_lstore:   store_local_long(str->get_index());    break;
1408   case Bytecodes::_lstore_0: store_local_long(0);                   break;
1409   case Bytecodes::_lstore_1: store_local_long(1);                   break;
1410   case Bytecodes::_lstore_2: store_local_long(2);                   break;
1411   case Bytecodes::_lstore_3: store_local_long(3);                   break;
1412 
1413   case Bytecodes::_multianewarray: do_multianewarray(str);          break;
1414 
1415   case Bytecodes::_new:      do_new(str);                           break;
1416 
1417   case Bytecodes::_newarray: do_newarray(str);                      break;
1418 
1419   case Bytecodes::_pop:
1420     {
1421       pop();
1422       break;
1423     }
1424   case Bytecodes::_pop2:
1425     {
1426       pop();
1427       pop();
1428       break;
1429     }
1430 
1431   case Bytecodes::_putfield:       do_putfield(str);                 break;
1432   case Bytecodes::_putstatic:      do_putstatic(str);                break;
1433 
1434   case Bytecodes::_ret: do_ret(str);                                 break;
1435 
1436   case Bytecodes::_swap:
1437     {
1438       ciType* value1 = pop_value();
1439       ciType* value2 = pop_value();
1440       push(value1);
1441       push(value2);
1442       break;
1443     }
1444   case Bytecodes::_wide:
1445   default:
1446     {
1447       // The iterator should skip this.
1448       ShouldNotReachHere();
1449       break;
1450     }
1451   }
1452 
1453   if (CITraceTypeFlow) {
1454     print_on(tty);
1455   }
1456 
1457   return (_trap_bci != -1);
1458 }
1459 
1460 #ifndef PRODUCT
1461 // ------------------------------------------------------------------
1462 // ciTypeFlow::StateVector::print_cell_on
1463 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1464   ciType* type = type_at(c);
1465   if (type == top_type()) {
1466     st->print("top");
1467   } else if (type == bottom_type()) {
1468     st->print("bottom");
1469   } else if (type == null_type()) {
1470     st->print("null");
1471   } else if (type == long2_type()) {
1472     st->print("long2");
1473   } else if (type == double2_type()) {
1474     st->print("double2");
1475   } else if (is_int(type)) {
1476     st->print("int");
1477   } else if (is_long(type)) {
1478     st->print("long");
1479   } else if (is_float(type)) {
1480     st->print("float");
1481   } else if (is_double(type)) {
1482     st->print("double");
1483   } else if (type->is_return_address()) {
1484     st->print("address(%d)", type->as_return_address()->bci());
1485   } else {
1486     if (type->is_klass()) {
1487       type->as_klass()->name()->print_symbol_on(st);
1488     } else {
1489       st->print("UNEXPECTED TYPE");
1490       type->print();
1491     }
1492   }
1493 }
1494 
1495 // ------------------------------------------------------------------
1496 // ciTypeFlow::StateVector::print_on
1497 void ciTypeFlow::StateVector::print_on(outputStream* st) const {
1498   int num_locals   = _outer->max_locals();
1499   int num_stack    = stack_size();
1500   int num_monitors = monitor_count();
1501   st->print_cr("  State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors);
1502   if (num_stack >= 0) {
1503     int i;
1504     for (i = 0; i < num_locals; i++) {
1505       st->print("    local %2d : ", i);
1506       print_cell_on(st, local(i));
1507       st->cr();
1508     }
1509     for (i = 0; i < num_stack; i++) {
1510       st->print("    stack %2d : ", i);
1511       print_cell_on(st, stack(i));
1512       st->cr();
1513     }
1514   }
1515 }
1516 #endif
1517 
1518 
1519 // ------------------------------------------------------------------
1520 // ciTypeFlow::SuccIter::next
1521 //
1522 void ciTypeFlow::SuccIter::next() {
1523   int succ_ct = _pred->successors()->length();
1524   int next = _index + 1;
1525   if (next < succ_ct) {
1526     _index = next;
1527     _succ = _pred->successors()->at(next);
1528     return;
1529   }
1530   for (int i = next - succ_ct; i < _pred->exceptions()->length(); i++) {
1531     // Do not compile any code for unloaded exception types.
1532     // Following compiler passes are responsible for doing this also.
1533     ciInstanceKlass* exception_klass = _pred->exc_klasses()->at(i);
1534     if (exception_klass->is_loaded()) {
1535       _index = next;
1536       _succ = _pred->exceptions()->at(i);
1537       return;
1538     }
1539     next++;
1540   }
1541   _index = -1;
1542   _succ = NULL;
1543 }
1544 
1545 // ------------------------------------------------------------------
1546 // ciTypeFlow::SuccIter::set_succ
1547 //
1548 void ciTypeFlow::SuccIter::set_succ(Block* succ) {
1549   int succ_ct = _pred->successors()->length();
1550   if (_index < succ_ct) {
1551     _pred->successors()->at_put(_index, succ);
1552   } else {
1553     int idx = _index - succ_ct;
1554     _pred->exceptions()->at_put(idx, succ);
1555   }
1556 }
1557 
1558 // ciTypeFlow::Block
1559 //
1560 // A basic block.
1561 
1562 // ------------------------------------------------------------------
1563 // ciTypeFlow::Block::Block
1564 ciTypeFlow::Block::Block(ciTypeFlow* outer,
1565                          ciBlock *ciblk,
1566                          ciTypeFlow::JsrSet* jsrs) {
1567   _ciblock = ciblk;
1568   _exceptions = NULL;
1569   _exc_klasses = NULL;
1570   _successors = NULL;
1571   _state = new (outer->arena()) StateVector(outer);
1572   JsrSet* new_jsrs =
1573     new (outer->arena()) JsrSet(outer->arena(), jsrs->size());
1574   jsrs->copy_into(new_jsrs);
1575   _jsrs = new_jsrs;
1576   _next = NULL;
1577   _on_work_list = false;
1578   _backedge_copy = false;
1579   _exception_entry = false;
1580   _trap_bci = -1;
1581   _trap_index = 0;
1582   df_init();
1583 
1584   if (CITraceTypeFlow) {
1585     tty->print_cr(">> Created new block");
1586     print_on(tty);
1587   }
1588 
1589   assert(this->outer() == outer, "outer link set up");
1590   assert(!outer->have_block_count(), "must not have mapped blocks yet");
1591 }
1592 
1593 // ------------------------------------------------------------------
1594 // ciTypeFlow::Block::df_init
1595 void ciTypeFlow::Block::df_init() {
1596   _pre_order = -1; assert(!has_pre_order(), "");
1597   _post_order = -1; assert(!has_post_order(), "");
1598   _loop = NULL;
1599   _irreducible_entry = false;
1600   _rpo_next = NULL;
1601 }
1602 
1603 // ------------------------------------------------------------------
1604 // ciTypeFlow::Block::successors
1605 //
1606 // Get the successors for this Block.
1607 GrowableArray<ciTypeFlow::Block*>*
1608 ciTypeFlow::Block::successors(ciBytecodeStream* str,
1609                               ciTypeFlow::StateVector* state,
1610                               ciTypeFlow::JsrSet* jsrs) {
1611   if (_successors == NULL) {
1612     if (CITraceTypeFlow) {
1613       tty->print(">> Computing successors for block ");
1614       print_value_on(tty);
1615       tty->cr();
1616     }
1617 
1618     ciTypeFlow* analyzer = outer();
1619     Arena* arena = analyzer->arena();
1620     Block* block = NULL;
1621     bool has_successor = !has_trap() &&
1622                          (control() != ciBlock::fall_through_bci || limit() < analyzer->code_size());
1623     if (!has_successor) {
1624       _successors =
1625         new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1626       // No successors
1627     } else if (control() == ciBlock::fall_through_bci) {
1628       assert(str->cur_bci() == limit(), "bad block end");
1629       // This block simply falls through to the next.
1630       _successors =
1631         new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1632 
1633       Block* block = analyzer->block_at(limit(), _jsrs);
1634       assert(_successors->length() == FALL_THROUGH, "");
1635       _successors->append(block);
1636     } else {
1637       int current_bci = str->cur_bci();
1638       int next_bci = str->next_bci();
1639       int branch_bci = -1;
1640       Block* target = NULL;
1641       assert(str->next_bci() == limit(), "bad block end");
1642       // This block is not a simple fall-though.  Interpret
1643       // the current bytecode to find our successors.
1644       switch (str->cur_bc()) {
1645       case Bytecodes::_ifeq:         case Bytecodes::_ifne:
1646       case Bytecodes::_iflt:         case Bytecodes::_ifge:
1647       case Bytecodes::_ifgt:         case Bytecodes::_ifle:
1648       case Bytecodes::_if_icmpeq:    case Bytecodes::_if_icmpne:
1649       case Bytecodes::_if_icmplt:    case Bytecodes::_if_icmpge:
1650       case Bytecodes::_if_icmpgt:    case Bytecodes::_if_icmple:
1651       case Bytecodes::_if_acmpeq:    case Bytecodes::_if_acmpne:
1652       case Bytecodes::_ifnull:       case Bytecodes::_ifnonnull:
1653         // Our successors are the branch target and the next bci.
1654         branch_bci = str->get_dest();
1655         _successors =
1656           new (arena) GrowableArray<Block*>(arena, 2, 0, NULL);
1657         assert(_successors->length() == IF_NOT_TAKEN, "");
1658         _successors->append(analyzer->block_at(next_bci, jsrs));
1659         assert(_successors->length() == IF_TAKEN, "");
1660         _successors->append(analyzer->block_at(branch_bci, jsrs));
1661         break;
1662 
1663       case Bytecodes::_goto:
1664         branch_bci = str->get_dest();
1665         _successors =
1666           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1667         assert(_successors->length() == GOTO_TARGET, "");
1668         _successors->append(analyzer->block_at(branch_bci, jsrs));
1669         break;
1670 
1671       case Bytecodes::_jsr:
1672         branch_bci = str->get_dest();
1673         _successors =
1674           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1675         assert(_successors->length() == GOTO_TARGET, "");
1676         _successors->append(analyzer->block_at(branch_bci, jsrs));
1677         break;
1678 
1679       case Bytecodes::_goto_w:
1680       case Bytecodes::_jsr_w:
1681         _successors =
1682           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1683         assert(_successors->length() == GOTO_TARGET, "");
1684         _successors->append(analyzer->block_at(str->get_far_dest(), jsrs));
1685         break;
1686 
1687       case Bytecodes::_tableswitch:  {
1688         Bytecode_tableswitch *tableswitch =
1689           Bytecode_tableswitch_at(str->cur_bcp());
1690 
1691         int len = tableswitch->length();
1692         _successors =
1693           new (arena) GrowableArray<Block*>(arena, len+1, 0, NULL);
1694         int bci = current_bci + tableswitch->default_offset();
1695         Block* block = analyzer->block_at(bci, jsrs);
1696         assert(_successors->length() == SWITCH_DEFAULT, "");
1697         _successors->append(block);
1698         while (--len >= 0) {
1699           int bci = current_bci + tableswitch->dest_offset_at(len);
1700           block = analyzer->block_at(bci, jsrs);
1701           assert(_successors->length() >= SWITCH_CASES, "");
1702           _successors->append_if_missing(block);
1703         }
1704         break;
1705       }
1706 
1707       case Bytecodes::_lookupswitch: {
1708         Bytecode_lookupswitch *lookupswitch =
1709           Bytecode_lookupswitch_at(str->cur_bcp());
1710 
1711         int npairs = lookupswitch->number_of_pairs();
1712         _successors =
1713           new (arena) GrowableArray<Block*>(arena, npairs+1, 0, NULL);
1714         int bci = current_bci + lookupswitch->default_offset();
1715         Block* block = analyzer->block_at(bci, jsrs);
1716         assert(_successors->length() == SWITCH_DEFAULT, "");
1717         _successors->append(block);
1718         while(--npairs >= 0) {
1719           LookupswitchPair *pair = lookupswitch->pair_at(npairs);
1720           int bci = current_bci + pair->offset();
1721           Block* block = analyzer->block_at(bci, jsrs);
1722           assert(_successors->length() >= SWITCH_CASES, "");
1723           _successors->append_if_missing(block);
1724         }
1725         break;
1726       }
1727 
1728       case Bytecodes::_athrow:     case Bytecodes::_ireturn:
1729       case Bytecodes::_lreturn:    case Bytecodes::_freturn:
1730       case Bytecodes::_dreturn:    case Bytecodes::_areturn:
1731       case Bytecodes::_return:
1732         _successors =
1733           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1734         // No successors
1735         break;
1736 
1737       case Bytecodes::_ret: {
1738         _successors =
1739           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1740 
1741         Cell local = state->local(str->get_index());
1742         ciType* return_address = state->type_at(local);
1743         assert(return_address->is_return_address(), "verify: wrong type");
1744         int bci = return_address->as_return_address()->bci();
1745         assert(_successors->length() == GOTO_TARGET, "");
1746         _successors->append(analyzer->block_at(bci, jsrs));
1747         break;
1748       }
1749 
1750       case Bytecodes::_wide:
1751       default:
1752         ShouldNotReachHere();
1753         break;
1754       }
1755     }
1756   }
1757   return _successors;
1758 }
1759 
1760 // ------------------------------------------------------------------
1761 // ciTypeFlow::Block:compute_exceptions
1762 //
1763 // Compute the exceptional successors and types for this Block.
1764 void ciTypeFlow::Block::compute_exceptions() {
1765   assert(_exceptions == NULL && _exc_klasses == NULL, "repeat");
1766 
1767   if (CITraceTypeFlow) {
1768     tty->print(">> Computing exceptions for block ");
1769     print_value_on(tty);
1770     tty->cr();
1771   }
1772 
1773   ciTypeFlow* analyzer = outer();
1774   Arena* arena = analyzer->arena();
1775 
1776   // Any bci in the block will do.
1777   ciExceptionHandlerStream str(analyzer->method(), start());
1778 
1779   // Allocate our growable arrays.
1780   int exc_count = str.count();
1781   _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, NULL);
1782   _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count,
1783                                                              0, NULL);
1784 
1785   for ( ; !str.is_done(); str.next()) {
1786     ciExceptionHandler* handler = str.handler();
1787     int bci = handler->handler_bci();
1788     ciInstanceKlass* klass = NULL;
1789     if (bci == -1) {
1790       // There is no catch all.  It is possible to exit the method.
1791       break;
1792     }
1793     if (handler->is_catch_all()) {
1794       klass = analyzer->env()->Throwable_klass();
1795     } else {
1796       klass = handler->catch_klass();
1797     }
1798     _exceptions->append(analyzer->block_at(bci, _jsrs));
1799     _exc_klasses->append(klass);
1800   }
1801 }
1802 
1803 // ------------------------------------------------------------------
1804 // ciTypeFlow::Block::set_backedge_copy
1805 // Use this only to make a pre-existing public block into a backedge copy.
1806 void ciTypeFlow::Block::set_backedge_copy(bool z) {
1807   assert(z || (z == is_backedge_copy()), "cannot make a backedge copy public");
1808   _backedge_copy = z;
1809 }
1810 
1811 // ------------------------------------------------------------------
1812 // ciTypeFlow::Block::is_clonable_exit
1813 //
1814 // At most 2 normal successors, one of which continues looping,
1815 // and all exceptional successors must exit.
1816 bool ciTypeFlow::Block::is_clonable_exit(ciTypeFlow::Loop* lp) {
1817   int normal_cnt  = 0;
1818   int in_loop_cnt = 0;
1819   for (SuccIter iter(this); !iter.done(); iter.next()) {
1820     Block* succ = iter.succ();
1821     if (iter.is_normal_ctrl()) {
1822       if (++normal_cnt > 2) return false;
1823       if (lp->contains(succ->loop())) {
1824         if (++in_loop_cnt > 1) return false;
1825       }
1826     } else {
1827       if (lp->contains(succ->loop())) return false;
1828     }
1829   }
1830   return in_loop_cnt == 1;
1831 }
1832 
1833 // ------------------------------------------------------------------
1834 // ciTypeFlow::Block::looping_succ
1835 //
1836 ciTypeFlow::Block* ciTypeFlow::Block::looping_succ(ciTypeFlow::Loop* lp) {
1837   assert(successors()->length() <= 2, "at most 2 normal successors");
1838   for (SuccIter iter(this); !iter.done(); iter.next()) {
1839     Block* succ = iter.succ();
1840     if (lp->contains(succ->loop())) {
1841       return succ;
1842     }
1843   }
1844   return NULL;
1845 }
1846 
1847 #ifndef PRODUCT
1848 // ------------------------------------------------------------------
1849 // ciTypeFlow::Block::print_value_on
1850 void ciTypeFlow::Block::print_value_on(outputStream* st) const {
1851   if (has_pre_order()) st->print("#%-2d ", pre_order());
1852   if (has_rpo())       st->print("rpo#%-2d ", rpo());
1853   st->print("[%d - %d)", start(), limit());
1854   if (is_loop_head()) st->print(" lphd");
1855   if (is_irreducible_entry()) st->print(" irred");
1856   if (_jsrs->size() > 0) { st->print("/");  _jsrs->print_on(st); }
1857   if (is_backedge_copy())  st->print("/backedge_copy");
1858 }
1859 
1860 // ------------------------------------------------------------------
1861 // ciTypeFlow::Block::print_on
1862 void ciTypeFlow::Block::print_on(outputStream* st) const {
1863   if ((Verbose || WizardMode)) {
1864     outer()->method()->print_codes_on(start(), limit(), st);
1865   }
1866   st->print_cr("  ====================================================  ");
1867   st->print ("  ");
1868   print_value_on(st);
1869   st->print(" Stored locals: "); def_locals()->print_on(st, outer()->method()->max_locals()); tty->cr();
1870   if (loop() && loop()->parent() != NULL) {
1871     st->print(" loops:");
1872     Loop* lp = loop();
1873     do {
1874       st->print(" %d<-%d", lp->head()->pre_order(),lp->tail()->pre_order());
1875       if (lp->is_irreducible()) st->print("(ir)");
1876       lp = lp->parent();
1877     } while (lp->parent() != NULL);
1878   }
1879   st->cr();
1880   _state->print_on(st);
1881   if (_successors == NULL) {
1882     st->print_cr("  No successor information");
1883   } else {
1884     int num_successors = _successors->length();
1885     st->print_cr("  Successors : %d", num_successors);
1886     for (int i = 0; i < num_successors; i++) {
1887       Block* successor = _successors->at(i);
1888       st->print("    ");
1889       successor->print_value_on(st);
1890       st->cr();
1891     }
1892   }
1893   if (_exceptions == NULL) {
1894     st->print_cr("  No exception information");
1895   } else {
1896     int num_exceptions = _exceptions->length();
1897     st->print_cr("  Exceptions : %d", num_exceptions);
1898     for (int i = 0; i < num_exceptions; i++) {
1899       Block* exc_succ = _exceptions->at(i);
1900       ciInstanceKlass* exc_klass = _exc_klasses->at(i);
1901       st->print("    ");
1902       exc_succ->print_value_on(st);
1903       st->print(" -- ");
1904       exc_klass->name()->print_symbol_on(st);
1905       st->cr();
1906     }
1907   }
1908   if (has_trap()) {
1909     st->print_cr("  Traps on %d with trap index %d", trap_bci(), trap_index());
1910   }
1911   st->print_cr("  ====================================================  ");
1912 }
1913 #endif
1914 
1915 #ifndef PRODUCT
1916 // ------------------------------------------------------------------
1917 // ciTypeFlow::LocalSet::print_on
1918 void ciTypeFlow::LocalSet::print_on(outputStream* st, int limit) const {
1919   st->print("{");
1920   for (int i = 0; i < max; i++) {
1921     if (test(i)) st->print(" %d", i);
1922   }
1923   if (limit > max) {
1924     st->print(" %d..%d ", max, limit);
1925   }
1926   st->print(" }");
1927 }
1928 #endif
1929 
1930 // ciTypeFlow
1931 //
1932 // This is a pass over the bytecodes which computes the following:
1933 //   basic block structure
1934 //   interpreter type-states (a la the verifier)
1935 
1936 // ------------------------------------------------------------------
1937 // ciTypeFlow::ciTypeFlow
1938 ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) {
1939   _env = env;
1940   _method = method;
1941   _methodBlocks = method->get_method_blocks();
1942   _max_locals = method->max_locals();
1943   _max_stack = method->max_stack();
1944   _code_size = method->code_size();
1945   _has_irreducible_entry = false;
1946   _osr_bci = osr_bci;
1947   _failure_reason = NULL;
1948   assert(0 <= start_bci() && start_bci() < code_size() , err_msg("correct osr_bci argument: 0 <= %d < %d", start_bci(), code_size()));
1949   _work_list = NULL;
1950 
1951   _ciblock_count = _methodBlocks->num_blocks();
1952   _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, _ciblock_count);
1953   for (int i = 0; i < _ciblock_count; i++) {
1954     _idx_to_blocklist[i] = NULL;
1955   }
1956   _block_map = NULL;  // until all blocks are seen
1957   _jsr_count = 0;
1958   _jsr_records = NULL;
1959 }
1960 
1961 // ------------------------------------------------------------------
1962 // ciTypeFlow::work_list_next
1963 //
1964 // Get the next basic block from our work list.
1965 ciTypeFlow::Block* ciTypeFlow::work_list_next() {
1966   assert(!work_list_empty(), "work list must not be empty");
1967   Block* next_block = _work_list;
1968   _work_list = next_block->next();
1969   next_block->set_next(NULL);
1970   next_block->set_on_work_list(false);
1971   return next_block;
1972 }
1973 
1974 // ------------------------------------------------------------------
1975 // ciTypeFlow::add_to_work_list
1976 //
1977 // Add a basic block to our work list.
1978 // List is sorted by decreasing postorder sort (same as increasing RPO)
1979 void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) {
1980   assert(!block->is_on_work_list(), "must not already be on work list");
1981 
1982   if (CITraceTypeFlow) {
1983     tty->print(">> Adding block ");
1984     block->print_value_on(tty);
1985     tty->print_cr(" to the work list : ");
1986   }
1987 
1988   block->set_on_work_list(true);
1989 
1990   // decreasing post order sort
1991 
1992   Block* prev = NULL;
1993   Block* current = _work_list;
1994   int po = block->post_order();
1995   while (current != NULL) {
1996     if (!current->has_post_order() || po > current->post_order())
1997       break;
1998     prev = current;
1999     current = current->next();
2000   }
2001   if (prev == NULL) {
2002     block->set_next(_work_list);
2003     _work_list = block;
2004   } else {
2005     block->set_next(current);
2006     prev->set_next(block);
2007   }
2008 
2009   if (CITraceTypeFlow) {
2010     tty->cr();
2011   }
2012 }
2013 
2014 // ------------------------------------------------------------------
2015 // ciTypeFlow::block_at
2016 //
2017 // Return the block beginning at bci which has a JsrSet compatible
2018 // with jsrs.
2019 ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2020   // First find the right ciBlock.
2021   if (CITraceTypeFlow) {
2022     tty->print(">> Requesting block for %d/", bci);
2023     jsrs->print_on(tty);
2024     tty->cr();
2025   }
2026 
2027   ciBlock* ciblk = _methodBlocks->block_containing(bci);
2028   assert(ciblk->start_bci() == bci, "bad ciBlock boundaries");
2029   Block* block = get_block_for(ciblk->index(), jsrs, option);
2030 
2031   assert(block == NULL? (option == no_create): block->is_backedge_copy() == (option == create_backedge_copy), "create option consistent with result");
2032 
2033   if (CITraceTypeFlow) {
2034     if (block != NULL) {
2035       tty->print(">> Found block ");
2036       block->print_value_on(tty);
2037       tty->cr();
2038     } else {
2039       tty->print_cr(">> No such block.");
2040     }
2041   }
2042 
2043   return block;
2044 }
2045 
2046 // ------------------------------------------------------------------
2047 // ciTypeFlow::make_jsr_record
2048 //
2049 // Make a JsrRecord for a given (entry, return) pair, if such a record
2050 // does not already exist.
2051 ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address,
2052                                                    int return_address) {
2053   if (_jsr_records == NULL) {
2054     _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(),
2055                                                            _jsr_count,
2056                                                            0,
2057                                                            NULL);
2058   }
2059   JsrRecord* record = NULL;
2060   int len = _jsr_records->length();
2061   for (int i = 0; i < len; i++) {
2062     JsrRecord* record = _jsr_records->at(i);
2063     if (record->entry_address() == entry_address &&
2064         record->return_address() == return_address) {
2065       return record;
2066     }
2067   }
2068 
2069   record = new (arena()) JsrRecord(entry_address, return_address);
2070   _jsr_records->append(record);
2071   return record;
2072 }
2073 
2074 // ------------------------------------------------------------------
2075 // ciTypeFlow::flow_exceptions
2076 //
2077 // Merge the current state into all exceptional successors at the
2078 // current point in the code.
2079 void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions,
2080                                  GrowableArray<ciInstanceKlass*>* exc_klasses,
2081                                  ciTypeFlow::StateVector* state) {
2082   int len = exceptions->length();
2083   assert(exc_klasses->length() == len, "must have same length");
2084   for (int i = 0; i < len; i++) {
2085     Block* block = exceptions->at(i);
2086     ciInstanceKlass* exception_klass = exc_klasses->at(i);
2087 
2088     if (!exception_klass->is_loaded()) {
2089       // Do not compile any code for unloaded exception types.
2090       // Following compiler passes are responsible for doing this also.
2091       continue;
2092     }
2093 
2094     if (block->meet_exception(exception_klass, state)) {
2095       // Block was modified and has PO.  Add it to the work list.
2096       if (block->has_post_order() &&
2097           !block->is_on_work_list()) {
2098         add_to_work_list(block);
2099       }
2100     }
2101   }
2102 }
2103 
2104 // ------------------------------------------------------------------
2105 // ciTypeFlow::flow_successors
2106 //
2107 // Merge the current state into all successors at the current point
2108 // in the code.
2109 void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors,
2110                                  ciTypeFlow::StateVector* state) {
2111   int len = successors->length();
2112   for (int i = 0; i < len; i++) {
2113     Block* block = successors->at(i);
2114     if (block->meet(state)) {
2115       // Block was modified and has PO.  Add it to the work list.
2116       if (block->has_post_order() &&
2117           !block->is_on_work_list()) {
2118         add_to_work_list(block);
2119       }
2120     }
2121   }
2122 }
2123 
2124 // ------------------------------------------------------------------
2125 // ciTypeFlow::can_trap
2126 //
2127 // Tells if a given instruction is able to generate an exception edge.
2128 bool ciTypeFlow::can_trap(ciBytecodeStream& str) {
2129   // Cf. GenerateOopMap::do_exception_edge.
2130   if (!Bytecodes::can_trap(str.cur_bc()))  return false;
2131 
2132   switch (str.cur_bc()) {
2133     // %%% FIXME: ldc of Class can generate an exception
2134     case Bytecodes::_ldc:
2135     case Bytecodes::_ldc_w:
2136     case Bytecodes::_ldc2_w:
2137     case Bytecodes::_aload_0:
2138       // These bytecodes can trap for rewriting.  We need to assume that
2139       // they do not throw exceptions to make the monitor analysis work.
2140       return false;
2141 
2142     case Bytecodes::_ireturn:
2143     case Bytecodes::_lreturn:
2144     case Bytecodes::_freturn:
2145     case Bytecodes::_dreturn:
2146     case Bytecodes::_areturn:
2147     case Bytecodes::_return:
2148       // We can assume the monitor stack is empty in this analysis.
2149       return false;
2150 
2151     case Bytecodes::_monitorexit:
2152       // We can assume monitors are matched in this analysis.
2153       return false;
2154   }
2155 
2156   return true;
2157 }
2158 
2159 // ------------------------------------------------------------------
2160 // ciTypeFlow::clone_loop_heads
2161 //
2162 // Clone the loop heads
2163 bool ciTypeFlow::clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) {
2164   bool rslt = false;
2165   for (PreorderLoops iter(loop_tree_root()); !iter.done(); iter.next()) {
2166     lp = iter.current();
2167     Block* head = lp->head();
2168     if (lp == loop_tree_root() ||
2169         lp->is_irreducible() ||
2170         !head->is_clonable_exit(lp))
2171       continue;
2172 
2173     // check not already cloned
2174     if (head->backedge_copy_count() != 0)
2175       continue;
2176 
2177     // check _no_ shared head below us
2178     Loop* ch;
2179     for (ch = lp->child(); ch != NULL && ch->head() != head; ch = ch->sibling());
2180     if (ch != NULL)
2181       continue;
2182 
2183     // Clone head
2184     Block* new_head = head->looping_succ(lp);
2185     Block* clone = clone_loop_head(lp, temp_vector, temp_set);
2186     // Update lp's info
2187     clone->set_loop(lp);
2188     lp->set_head(new_head);
2189     lp->set_tail(clone);
2190     // And move original head into outer loop
2191     head->set_loop(lp->parent());
2192 
2193     rslt = true;
2194   }
2195   return rslt;
2196 }
2197 
2198 // ------------------------------------------------------------------
2199 // ciTypeFlow::clone_loop_head
2200 //
2201 // Clone lp's head and replace tail's successors with clone.
2202 //
2203 //  |
2204 //  v
2205 // head <-> body
2206 //  |
2207 //  v
2208 // exit
2209 //
2210 // new_head
2211 //
2212 //  |
2213 //  v
2214 // head ----------\
2215 //  |             |
2216 //  |             v
2217 //  |  clone <-> body
2218 //  |    |
2219 //  | /--/
2220 //  | |
2221 //  v v
2222 // exit
2223 //
2224 ciTypeFlow::Block* ciTypeFlow::clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) {
2225   Block* head = lp->head();
2226   Block* tail = lp->tail();
2227   if (CITraceTypeFlow) {
2228     tty->print(">> Requesting clone of loop head "); head->print_value_on(tty);
2229     tty->print("  for predecessor ");                tail->print_value_on(tty);
2230     tty->cr();
2231   }
2232   Block* clone = block_at(head->start(), head->jsrs(), create_backedge_copy);
2233   assert(clone->backedge_copy_count() == 1, "one backedge copy for all back edges");
2234 
2235   assert(!clone->has_pre_order(), "just created");
2236   clone->set_next_pre_order();
2237 
2238   // Insert clone after (orig) tail in reverse post order
2239   clone->set_rpo_next(tail->rpo_next());
2240   tail->set_rpo_next(clone);
2241 
2242   // tail->head becomes tail->clone
2243   for (SuccIter iter(tail); !iter.done(); iter.next()) {
2244     if (iter.succ() == head) {
2245       iter.set_succ(clone);
2246     }
2247   }
2248   flow_block(tail, temp_vector, temp_set);
2249   if (head == tail) {
2250     // For self-loops, clone->head becomes clone->clone
2251     flow_block(clone, temp_vector, temp_set);
2252     for (SuccIter iter(clone); !iter.done(); iter.next()) {
2253       if (iter.succ() == head) {
2254         iter.set_succ(clone);
2255         break;
2256       }
2257     }
2258   }
2259   flow_block(clone, temp_vector, temp_set);
2260 
2261   return clone;
2262 }
2263 
2264 // ------------------------------------------------------------------
2265 // ciTypeFlow::flow_block
2266 //
2267 // Interpret the effects of the bytecodes on the incoming state
2268 // vector of a basic block.  Push the changed state to succeeding
2269 // basic blocks.
2270 void ciTypeFlow::flow_block(ciTypeFlow::Block* block,
2271                             ciTypeFlow::StateVector* state,
2272                             ciTypeFlow::JsrSet* jsrs) {
2273   if (CITraceTypeFlow) {
2274     tty->print("\n>> ANALYZING BLOCK : ");
2275     tty->cr();
2276     block->print_on(tty);
2277   }
2278   assert(block->has_pre_order(), "pre-order is assigned before 1st flow");
2279 
2280   int start = block->start();
2281   int limit = block->limit();
2282   int control = block->control();
2283   if (control != ciBlock::fall_through_bci) {
2284     limit = control;
2285   }
2286 
2287   // Grab the state from the current block.
2288   block->copy_state_into(state);
2289   state->def_locals()->clear();
2290 
2291   GrowableArray<Block*>*           exceptions = block->exceptions();
2292   GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses();
2293   bool has_exceptions = exceptions->length() > 0;
2294 
2295   bool exceptions_used = false;
2296 
2297   ciBytecodeStream str(method());
2298   str.reset_to_bci(start);
2299   Bytecodes::Code code;
2300   while ((code = str.next()) != ciBytecodeStream::EOBC() &&
2301          str.cur_bci() < limit) {
2302     // Check for exceptional control flow from this point.
2303     if (has_exceptions && can_trap(str)) {
2304       flow_exceptions(exceptions, exc_klasses, state);
2305       exceptions_used = true;
2306     }
2307     // Apply the effects of the current bytecode to our state.
2308     bool res = state->apply_one_bytecode(&str);
2309 
2310     // Watch for bailouts.
2311     if (failing())  return;
2312 
2313     if (res) {
2314 
2315       // We have encountered a trap.  Record it in this block.
2316       block->set_trap(state->trap_bci(), state->trap_index());
2317 
2318       if (CITraceTypeFlow) {
2319         tty->print_cr(">> Found trap");
2320         block->print_on(tty);
2321       }
2322 
2323       // Save set of locals defined in this block
2324       block->def_locals()->add(state->def_locals());
2325 
2326       // Record (no) successors.
2327       block->successors(&str, state, jsrs);
2328 
2329       assert(!has_exceptions || exceptions_used, "Not removing exceptions");
2330 
2331       // Discontinue interpretation of this Block.
2332       return;
2333     }
2334   }
2335 
2336   GrowableArray<Block*>* successors = NULL;
2337   if (control != ciBlock::fall_through_bci) {
2338     // Check for exceptional control flow from this point.
2339     if (has_exceptions && can_trap(str)) {
2340       flow_exceptions(exceptions, exc_klasses, state);
2341       exceptions_used = true;
2342     }
2343 
2344     // Fix the JsrSet to reflect effect of the bytecode.
2345     block->copy_jsrs_into(jsrs);
2346     jsrs->apply_control(this, &str, state);
2347 
2348     // Find successor edges based on old state and new JsrSet.
2349     successors = block->successors(&str, state, jsrs);
2350 
2351     // Apply the control changes to the state.
2352     state->apply_one_bytecode(&str);
2353   } else {
2354     // Fall through control
2355     successors = block->successors(&str, NULL, NULL);
2356   }
2357 
2358   // Save set of locals defined in this block
2359   block->def_locals()->add(state->def_locals());
2360 
2361   // Remove untaken exception paths
2362   if (!exceptions_used)
2363     exceptions->clear();
2364 
2365   // Pass our state to successors.
2366   flow_successors(successors, state);
2367 }
2368 
2369 // ------------------------------------------------------------------
2370 // ciTypeFlow::PostOrderLoops::next
2371 //
2372 // Advance to next loop tree using a postorder, left-to-right traversal.
2373 void ciTypeFlow::PostorderLoops::next() {
2374   assert(!done(), "must not be done.");
2375   if (_current->sibling() != NULL) {
2376     _current = _current->sibling();
2377     while (_current->child() != NULL) {
2378       _current = _current->child();
2379     }
2380   } else {
2381     _current = _current->parent();
2382   }
2383 }
2384 
2385 // ------------------------------------------------------------------
2386 // ciTypeFlow::PreOrderLoops::next
2387 //
2388 // Advance to next loop tree using a preorder, left-to-right traversal.
2389 void ciTypeFlow::PreorderLoops::next() {
2390   assert(!done(), "must not be done.");
2391   if (_current->child() != NULL) {
2392     _current = _current->child();
2393   } else if (_current->sibling() != NULL) {
2394     _current = _current->sibling();
2395   } else {
2396     while (_current != _root && _current->sibling() == NULL) {
2397       _current = _current->parent();
2398     }
2399     if (_current == _root) {
2400       _current = NULL;
2401       assert(done(), "must be done.");
2402     } else {
2403       assert(_current->sibling() != NULL, "must be more to do");
2404       _current = _current->sibling();
2405     }
2406   }
2407 }
2408 
2409 // ------------------------------------------------------------------
2410 // ciTypeFlow::Loop::sorted_merge
2411 //
2412 // Merge the branch lp into this branch, sorting on the loop head
2413 // pre_orders. Returns the leaf of the merged branch.
2414 // Child and sibling pointers will be setup later.
2415 // Sort is (looking from leaf towards the root)
2416 //  descending on primary key: loop head's pre_order, and
2417 //  ascending  on secondary key: loop tail's pre_order.
2418 ciTypeFlow::Loop* ciTypeFlow::Loop::sorted_merge(Loop* lp) {
2419   Loop* leaf = this;
2420   Loop* prev = NULL;
2421   Loop* current = leaf;
2422   while (lp != NULL) {
2423     int lp_pre_order = lp->head()->pre_order();
2424     // Find insertion point for "lp"
2425     while (current != NULL) {
2426       if (current == lp)
2427         return leaf; // Already in list
2428       if (current->head()->pre_order() < lp_pre_order)
2429         break;
2430       if (current->head()->pre_order() == lp_pre_order &&
2431           current->tail()->pre_order() > lp->tail()->pre_order()) {
2432         break;
2433       }
2434       prev = current;
2435       current = current->parent();
2436     }
2437     Loop* next_lp = lp->parent(); // Save future list of items to insert
2438     // Insert lp before current
2439     lp->set_parent(current);
2440     if (prev != NULL) {
2441       prev->set_parent(lp);
2442     } else {
2443       leaf = lp;
2444     }
2445     prev = lp;     // Inserted item is new prev[ious]
2446     lp = next_lp;  // Next item to insert
2447   }
2448   return leaf;
2449 }
2450 
2451 // ------------------------------------------------------------------
2452 // ciTypeFlow::build_loop_tree
2453 //
2454 // Incrementally build loop tree.
2455 void ciTypeFlow::build_loop_tree(Block* blk) {
2456   assert(!blk->is_post_visited(), "precondition");
2457   Loop* innermost = NULL; // merge of loop tree branches over all successors
2458 
2459   for (SuccIter iter(blk); !iter.done(); iter.next()) {
2460     Loop*  lp   = NULL;
2461     Block* succ = iter.succ();
2462     if (!succ->is_post_visited()) {
2463       // Found backedge since predecessor post visited, but successor is not
2464       assert(succ->pre_order() <= blk->pre_order(), "should be backedge");
2465 
2466       // Create a LoopNode to mark this loop.
2467       lp = new (arena()) Loop(succ, blk);
2468       if (succ->loop() == NULL)
2469         succ->set_loop(lp);
2470       // succ->loop will be updated to innermost loop on a later call, when blk==succ
2471 
2472     } else {  // Nested loop
2473       lp = succ->loop();
2474 
2475       // If succ is loop head, find outer loop.
2476       while (lp != NULL && lp->head() == succ) {
2477         lp = lp->parent();
2478       }
2479       if (lp == NULL) {
2480         // Infinite loop, it's parent is the root
2481         lp = loop_tree_root();
2482       }
2483     }
2484 
2485     // Check for irreducible loop.
2486     // Successor has already been visited. If the successor's loop head
2487     // has already been post-visited, then this is another entry into the loop.
2488     while (lp->head()->is_post_visited() && lp != loop_tree_root()) {
2489       _has_irreducible_entry = true;
2490       lp->set_irreducible(succ);
2491       if (!succ->is_on_work_list()) {
2492         // Assume irreducible entries need more data flow
2493         add_to_work_list(succ);
2494       }
2495       Loop* plp = lp->parent();
2496       if (plp == NULL) {
2497         // This only happens for some irreducible cases.  The parent
2498         // will be updated during a later pass.
2499         break;
2500       }
2501       lp = plp;
2502     }
2503 
2504     // Merge loop tree branch for all successors.
2505     innermost = innermost == NULL ? lp : innermost->sorted_merge(lp);
2506 
2507   } // end loop
2508 
2509   if (innermost == NULL) {
2510     assert(blk->successors()->length() == 0, "CFG exit");
2511     blk->set_loop(loop_tree_root());
2512   } else if (innermost->head() == blk) {
2513     // If loop header, complete the tree pointers
2514     if (blk->loop() != innermost) {
2515 #if ASSERT
2516       assert(blk->loop()->head() == innermost->head(), "same head");
2517       Loop* dl;
2518       for (dl = innermost; dl != NULL && dl != blk->loop(); dl = dl->parent());
2519       assert(dl == blk->loop(), "blk->loop() already in innermost list");
2520 #endif
2521       blk->set_loop(innermost);
2522     }
2523     innermost->def_locals()->add(blk->def_locals());
2524     Loop* l = innermost;
2525     Loop* p = l->parent();
2526     while (p && l->head() == blk) {
2527       l->set_sibling(p->child());  // Put self on parents 'next child'
2528       p->set_child(l);             // Make self the first child of parent
2529       p->def_locals()->add(l->def_locals());
2530       l = p;                       // Walk up the parent chain
2531       p = l->parent();
2532     }
2533   } else {
2534     blk->set_loop(innermost);
2535     innermost->def_locals()->add(blk->def_locals());
2536   }
2537 }
2538 
2539 // ------------------------------------------------------------------
2540 // ciTypeFlow::Loop::contains
2541 //
2542 // Returns true if lp is nested loop.
2543 bool ciTypeFlow::Loop::contains(ciTypeFlow::Loop* lp) const {
2544   assert(lp != NULL, "");
2545   if (this == lp || head() == lp->head()) return true;
2546   int depth1 = depth();
2547   int depth2 = lp->depth();
2548   if (depth1 > depth2)
2549     return false;
2550   while (depth1 < depth2) {
2551     depth2--;
2552     lp = lp->parent();
2553   }
2554   return this == lp;
2555 }
2556 
2557 // ------------------------------------------------------------------
2558 // ciTypeFlow::Loop::depth
2559 //
2560 // Loop depth
2561 int ciTypeFlow::Loop::depth() const {
2562   int dp = 0;
2563   for (Loop* lp = this->parent(); lp != NULL; lp = lp->parent())
2564     dp++;
2565   return dp;
2566 }
2567 
2568 #ifndef PRODUCT
2569 // ------------------------------------------------------------------
2570 // ciTypeFlow::Loop::print
2571 void ciTypeFlow::Loop::print(outputStream* st, int indent) const {
2572   for (int i = 0; i < indent; i++) st->print(" ");
2573   st->print("%d<-%d %s",
2574             is_root() ? 0 : this->head()->pre_order(),
2575             is_root() ? 0 : this->tail()->pre_order(),
2576             is_irreducible()?" irr":"");
2577   st->print(" defs: ");
2578   def_locals()->print_on(st, _head->outer()->method()->max_locals());
2579   st->cr();
2580   for (Loop* ch = child(); ch != NULL; ch = ch->sibling())
2581     ch->print(st, indent+2);
2582 }
2583 #endif
2584 
2585 // ------------------------------------------------------------------
2586 // ciTypeFlow::df_flow_types
2587 //
2588 // Perform the depth first type flow analysis. Helper for flow_types.
2589 void ciTypeFlow::df_flow_types(Block* start,
2590                                bool do_flow,
2591                                StateVector* temp_vector,
2592                                JsrSet* temp_set) {
2593   int dft_len = 100;
2594   GrowableArray<Block*> stk(dft_len);
2595 
2596   ciBlock* dummy = _methodBlocks->make_dummy_block();
2597   JsrSet* root_set = new JsrSet(NULL, 0);
2598   Block* root_head = new (arena()) Block(this, dummy, root_set);
2599   Block* root_tail = new (arena()) Block(this, dummy, root_set);
2600   root_head->set_pre_order(0);
2601   root_head->set_post_order(0);
2602   root_tail->set_pre_order(max_jint);
2603   root_tail->set_post_order(max_jint);
2604   set_loop_tree_root(new (arena()) Loop(root_head, root_tail));
2605 
2606   stk.push(start);
2607 
2608   _next_pre_order = 0;  // initialize pre_order counter
2609   _rpo_list = NULL;
2610   int next_po = 0;      // initialize post_order counter
2611 
2612   // Compute RPO and the control flow graph
2613   int size;
2614   while ((size = stk.length()) > 0) {
2615     Block* blk = stk.top(); // Leave node on stack
2616     if (!blk->is_visited()) {
2617       // forward arc in graph
2618       assert (!blk->has_pre_order(), "");
2619       blk->set_next_pre_order();
2620 
2621       if (_next_pre_order >= MaxNodeLimit / 2) {
2622         // Too many basic blocks.  Bail out.
2623         // This can happen when try/finally constructs are nested to depth N,
2624         // and there is O(2**N) cloning of jsr bodies.  See bug 4697245!
2625         // "MaxNodeLimit / 2" is used because probably the parser will
2626         // generate at least twice that many nodes and bail out.
2627         record_failure("too many basic blocks");
2628         return;
2629       }
2630       if (do_flow) {
2631         flow_block(blk, temp_vector, temp_set);
2632         if (failing()) return; // Watch for bailouts.
2633       }
2634     } else if (!blk->is_post_visited()) {
2635       // cross or back arc
2636       for (SuccIter iter(blk); !iter.done(); iter.next()) {
2637         Block* succ = iter.succ();
2638         if (!succ->is_visited()) {
2639           stk.push(succ);
2640         }
2641       }
2642       if (stk.length() == size) {
2643         // There were no additional children, post visit node now
2644         stk.pop(); // Remove node from stack
2645 
2646         build_loop_tree(blk);
2647         blk->set_post_order(next_po++);   // Assign post order
2648         prepend_to_rpo_list(blk);
2649         assert(blk->is_post_visited(), "");
2650 
2651         if (blk->is_loop_head() && !blk->is_on_work_list()) {
2652           // Assume loop heads need more data flow
2653           add_to_work_list(blk);
2654         }
2655       }
2656     } else {
2657       stk.pop(); // Remove post-visited node from stack
2658     }
2659   }
2660 }
2661 
2662 // ------------------------------------------------------------------
2663 // ciTypeFlow::flow_types
2664 //
2665 // Perform the type flow analysis, creating and cloning Blocks as
2666 // necessary.
2667 void ciTypeFlow::flow_types() {
2668   ResourceMark rm;
2669   StateVector* temp_vector = new StateVector(this);
2670   JsrSet* temp_set = new JsrSet(NULL, 16);
2671 
2672   // Create the method entry block.
2673   Block* start = block_at(start_bci(), temp_set);
2674 
2675   // Load the initial state into it.
2676   const StateVector* start_state = get_start_state();
2677   if (failing())  return;
2678   start->meet(start_state);
2679 
2680   // Depth first visit
2681   df_flow_types(start, true /*do flow*/, temp_vector, temp_set);
2682 
2683   if (failing())  return;
2684   assert(_rpo_list == start, "must be start");
2685 
2686   // Any loops found?
2687   if (loop_tree_root()->child() != NULL &&
2688       env()->comp_level() >= CompLevel_full_optimization) {
2689       // Loop optimizations are not performed on Tier1 compiles.
2690 
2691     bool changed = clone_loop_heads(loop_tree_root(), temp_vector, temp_set);
2692 
2693     // If some loop heads were cloned, recompute postorder and loop tree
2694     if (changed) {
2695       loop_tree_root()->set_child(NULL);
2696       for (Block* blk = _rpo_list; blk != NULL;) {
2697         Block* next = blk->rpo_next();
2698         blk->df_init();
2699         blk = next;
2700       }
2701       df_flow_types(start, false /*no flow*/, temp_vector, temp_set);
2702     }
2703   }
2704 
2705   if (CITraceTypeFlow) {
2706     tty->print_cr("\nLoop tree");
2707     loop_tree_root()->print();
2708   }
2709 
2710   // Continue flow analysis until fixed point reached
2711 
2712   debug_only(int max_block = _next_pre_order;)
2713 
2714   while (!work_list_empty()) {
2715     Block* blk = work_list_next();
2716     assert (blk->has_post_order(), "post order assigned above");
2717 
2718     flow_block(blk, temp_vector, temp_set);
2719 
2720     assert (max_block == _next_pre_order, "no new blocks");
2721     assert (!failing(), "no more bailouts");
2722   }
2723 }
2724 
2725 // ------------------------------------------------------------------
2726 // ciTypeFlow::map_blocks
2727 //
2728 // Create the block map, which indexes blocks in reverse post-order.
2729 void ciTypeFlow::map_blocks() {
2730   assert(_block_map == NULL, "single initialization");
2731   int block_ct = _next_pre_order;
2732   _block_map = NEW_ARENA_ARRAY(arena(), Block*, block_ct);
2733   assert(block_ct == block_count(), "");
2734 
2735   Block* blk = _rpo_list;
2736   for (int m = 0; m < block_ct; m++) {
2737     int rpo = blk->rpo();
2738     assert(rpo == m, "should be sequential");
2739     _block_map[rpo] = blk;
2740     blk = blk->rpo_next();
2741   }
2742   assert(blk == NULL, "should be done");
2743 
2744   for (int j = 0; j < block_ct; j++) {
2745     assert(_block_map[j] != NULL, "must not drop any blocks");
2746     Block* block = _block_map[j];
2747     // Remove dead blocks from successor lists:
2748     for (int e = 0; e <= 1; e++) {
2749       GrowableArray<Block*>* l = e? block->exceptions(): block->successors();
2750       for (int k = 0; k < l->length(); k++) {
2751         Block* s = l->at(k);
2752         if (!s->has_post_order()) {
2753           if (CITraceTypeFlow) {
2754             tty->print("Removing dead %s successor of #%d: ", (e? "exceptional":  "normal"), block->pre_order());
2755             s->print_value_on(tty);
2756             tty->cr();
2757           }
2758           l->remove(s);
2759           --k;
2760         }
2761       }
2762     }
2763   }
2764 }
2765 
2766 // ------------------------------------------------------------------
2767 // ciTypeFlow::get_block_for
2768 //
2769 // Find a block with this ciBlock which has a compatible JsrSet.
2770 // If no such block exists, create it, unless the option is no_create.
2771 // If the option is create_backedge_copy, always create a fresh backedge copy.
2772 ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2773   Arena* a = arena();
2774   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
2775   if (blocks == NULL) {
2776     // Query only?
2777     if (option == no_create)  return NULL;
2778 
2779     // Allocate the growable array.
2780     blocks = new (a) GrowableArray<Block*>(a, 4, 0, NULL);
2781     _idx_to_blocklist[ciBlockIndex] = blocks;
2782   }
2783 
2784   if (option != create_backedge_copy) {
2785     int len = blocks->length();
2786     for (int i = 0; i < len; i++) {
2787       Block* block = blocks->at(i);
2788       if (!block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
2789         return block;
2790       }
2791     }
2792   }
2793 
2794   // Query only?
2795   if (option == no_create)  return NULL;
2796 
2797   // We did not find a compatible block.  Create one.
2798   Block* new_block = new (a) Block(this, _methodBlocks->block(ciBlockIndex), jsrs);
2799   if (option == create_backedge_copy)  new_block->set_backedge_copy(true);
2800   blocks->append(new_block);
2801   return new_block;
2802 }
2803 
2804 // ------------------------------------------------------------------
2805 // ciTypeFlow::backedge_copy_count
2806 //
2807 int ciTypeFlow::backedge_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const {
2808   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
2809 
2810   if (blocks == NULL) {
2811     return 0;
2812   }
2813 
2814   int count = 0;
2815   int len = blocks->length();
2816   for (int i = 0; i < len; i++) {
2817     Block* block = blocks->at(i);
2818     if (block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
2819       count++;
2820     }
2821   }
2822 
2823   return count;
2824 }
2825 
2826 // ------------------------------------------------------------------
2827 // ciTypeFlow::do_flow
2828 //
2829 // Perform type inference flow analysis.
2830 void ciTypeFlow::do_flow() {
2831   if (CITraceTypeFlow) {
2832     tty->print_cr("\nPerforming flow analysis on method");
2833     method()->print();
2834     if (is_osr_flow())  tty->print(" at OSR bci %d", start_bci());
2835     tty->cr();
2836     method()->print_codes();
2837   }
2838   if (CITraceTypeFlow) {
2839     tty->print_cr("Initial CI Blocks");
2840     print_on(tty);
2841   }
2842   flow_types();
2843   // Watch for bailouts.
2844   if (failing()) {
2845     return;
2846   }
2847 
2848   map_blocks();
2849 
2850   if (CIPrintTypeFlow || CITraceTypeFlow) {
2851     rpo_print_on(tty);
2852   }
2853 }
2854 
2855 // ------------------------------------------------------------------
2856 // ciTypeFlow::record_failure()
2857 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
2858 // This is required because there is not a 1-1 relation between the ciEnv and
2859 // the TypeFlow passes within a compilation task.  For example, if the compiler
2860 // is considering inlining a method, it will request a TypeFlow.  If that fails,
2861 // the compilation as a whole may continue without the inlining.  Some TypeFlow
2862 // requests are not optional; if they fail the requestor is responsible for
2863 // copying the failure reason up to the ciEnv.  (See Parse::Parse.)
2864 void ciTypeFlow::record_failure(const char* reason) {
2865   if (env()->log() != NULL) {
2866     env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
2867   }
2868   if (_failure_reason == NULL) {
2869     // Record the first failure reason.
2870     _failure_reason = reason;
2871   }
2872 }
2873 
2874 #ifndef PRODUCT
2875 // ------------------------------------------------------------------
2876 // ciTypeFlow::print_on
2877 void ciTypeFlow::print_on(outputStream* st) const {
2878   // Walk through CI blocks
2879   st->print_cr("********************************************************");
2880   st->print   ("TypeFlow for ");
2881   method()->name()->print_symbol_on(st);
2882   int limit_bci = code_size();
2883   st->print_cr("  %d bytes", limit_bci);
2884   ciMethodBlocks  *mblks = _methodBlocks;
2885   ciBlock* current = NULL;
2886   for (int bci = 0; bci < limit_bci; bci++) {
2887     ciBlock* blk = mblks->block_containing(bci);
2888     if (blk != NULL && blk != current) {
2889       current = blk;
2890       current->print_on(st);
2891 
2892       GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()];
2893       int num_blocks = (blocks == NULL) ? 0 : blocks->length();
2894 
2895       if (num_blocks == 0) {
2896         st->print_cr("  No Blocks");
2897       } else {
2898         for (int i = 0; i < num_blocks; i++) {
2899           Block* block = blocks->at(i);
2900           block->print_on(st);
2901         }
2902       }
2903       st->print_cr("--------------------------------------------------------");
2904       st->cr();
2905     }
2906   }
2907   st->print_cr("********************************************************");
2908   st->cr();
2909 }
2910 
2911 void ciTypeFlow::rpo_print_on(outputStream* st) const {
2912   st->print_cr("********************************************************");
2913   st->print   ("TypeFlow for ");
2914   method()->name()->print_symbol_on(st);
2915   int limit_bci = code_size();
2916   st->print_cr("  %d bytes", limit_bci);
2917   for (Block* blk = _rpo_list; blk != NULL; blk = blk->rpo_next()) {
2918     blk->print_on(st);
2919     st->print_cr("--------------------------------------------------------");
2920     st->cr();
2921   }
2922   st->print_cr("********************************************************");
2923   st->cr();
2924 }
2925 #endif