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