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