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