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