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