1 /*
   2  * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciConstant.hpp"
  27 #include "ci/ciField.hpp"

  28 #include "ci/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 "opto/node.hpp"
  42 #include "runtime/deoptimization.hpp"
  43 #include "utilities/growableArray.hpp"
  44 
  45 // ciTypeFlow::JsrSet
  46 //
  47 // A JsrSet represents some set of JsrRecords.  This class
  48 // is used to record a set of all jsr routines which we permit
  49 // execution to return (ret) from.
  50 //
  51 // During abstract interpretation, JsrSets are used to determine
  52 // whether two paths which reach a given block are unique, and
  53 // should be cloned apart, or are compatible, and should merge
  54 // together.
  55 
  56 // ------------------------------------------------------------------
  57 // ciTypeFlow::JsrSet::JsrSet
  58 
  59 // Allocate growable array storage in Arena.
  60 ciTypeFlow::JsrSet::JsrSet(Arena* arena, int default_len) : _set(arena, default_len, 0, nullptr) {
  61   assert(arena != nullptr, "invariant");
  62 }
  63 
  64 // Allocate growable array storage in current ResourceArea.
  65 ciTypeFlow::JsrSet::JsrSet(int default_len) : _set(default_len, 0, nullptr) {}
  66 
  67 // ------------------------------------------------------------------
  68 // ciTypeFlow::JsrSet::copy_into
  69 void ciTypeFlow::JsrSet::copy_into(JsrSet* jsrs) {
  70   int len = size();
  71   jsrs->_set.clear();
  72   for (int i = 0; i < len; i++) {
  73     jsrs->_set.append(_set.at(i));
  74   }
  75 }
  76 
  77 // ------------------------------------------------------------------
  78 // ciTypeFlow::JsrSet::is_compatible_with
  79 //
  80 // !!!! MISGIVINGS ABOUT THIS... disregard
  81 //
  82 // Is this JsrSet compatible with some other JsrSet?
  83 //
  84 // In set-theoretic terms, a JsrSet can be viewed as a partial function
  85 // from entry addresses to return addresses.  Two JsrSets A and B are
  86 // compatible iff
  87 //
  88 //   For any x,
  89 //   A(x) defined and B(x) defined implies A(x) == B(x)
  90 //
  91 // Less formally, two JsrSets are compatible when they have identical
  92 // return addresses for any entry addresses they share in common.
  93 bool ciTypeFlow::JsrSet::is_compatible_with(JsrSet* other) {
  94   // Walk through both sets in parallel.  If the same entry address
  95   // appears in both sets, then the return address must match for
  96   // the sets to be compatible.
  97   int size1 = size();
  98   int size2 = other->size();
  99 
 100   // Special case.  If nothing is on the jsr stack, then there can
 101   // be no ret.
 102   if (size2 == 0) {
 103     return true;
 104   } else if (size1 != size2) {
 105     return false;
 106   } else {
 107     for (int i = 0; i < size1; i++) {
 108       JsrRecord* record1 = record_at(i);
 109       JsrRecord* record2 = other->record_at(i);
 110       if (record1->entry_address() != record2->entry_address() ||
 111           record1->return_address() != record2->return_address()) {
 112         return false;
 113       }
 114     }
 115     return true;
 116   }
 117 
 118 #if 0
 119   int pos1 = 0;
 120   int pos2 = 0;
 121   int size1 = size();
 122   int size2 = other->size();
 123   while (pos1 < size1 && pos2 < size2) {
 124     JsrRecord* record1 = record_at(pos1);
 125     JsrRecord* record2 = other->record_at(pos2);
 126     int entry1 = record1->entry_address();
 127     int entry2 = record2->entry_address();
 128     if (entry1 < entry2) {
 129       pos1++;
 130     } else if (entry1 > entry2) {
 131       pos2++;
 132     } else {
 133       if (record1->return_address() == record2->return_address()) {
 134         pos1++;
 135         pos2++;
 136       } else {
 137         // These two JsrSets are incompatible.
 138         return false;
 139       }
 140     }
 141   }
 142   // The two JsrSets agree.
 143   return true;
 144 #endif
 145 }
 146 
 147 // ------------------------------------------------------------------
 148 // ciTypeFlow::JsrSet::insert_jsr_record
 149 //
 150 // Insert the given JsrRecord into the JsrSet, maintaining the order
 151 // of the set and replacing any element with the same entry address.
 152 void ciTypeFlow::JsrSet::insert_jsr_record(JsrRecord* record) {
 153   int len = size();
 154   int entry = record->entry_address();
 155   int pos = 0;
 156   for ( ; pos < len; pos++) {
 157     JsrRecord* current = record_at(pos);
 158     if (entry == current->entry_address()) {
 159       // Stomp over this entry.
 160       _set.at_put(pos, record);
 161       assert(size() == len, "must be same size");
 162       return;
 163     } else if (entry < current->entry_address()) {
 164       break;
 165     }
 166   }
 167 
 168   // Insert the record into the list.
 169   JsrRecord* swap = record;
 170   JsrRecord* temp = nullptr;
 171   for ( ; pos < len; pos++) {
 172     temp = _set.at(pos);
 173     _set.at_put(pos, swap);
 174     swap = temp;
 175   }
 176   _set.append(swap);
 177   assert(size() == len+1, "must be larger");
 178 }
 179 
 180 // ------------------------------------------------------------------
 181 // ciTypeFlow::JsrSet::remove_jsr_record
 182 //
 183 // Remove the JsrRecord with the given return address from the JsrSet.
 184 void ciTypeFlow::JsrSet::remove_jsr_record(int return_address) {
 185   int len = size();
 186   for (int i = 0; i < len; i++) {
 187     if (record_at(i)->return_address() == return_address) {
 188       // We have found the proper entry.  Remove it from the
 189       // JsrSet and exit.
 190       for (int j = i + 1; j < len ; j++) {
 191         _set.at_put(j - 1, _set.at(j));
 192       }
 193       _set.trunc_to(len - 1);
 194       assert(size() == len-1, "must be smaller");
 195       return;
 196     }
 197   }
 198   assert(false, "verify: returning from invalid subroutine");
 199 }
 200 
 201 // ------------------------------------------------------------------
 202 // ciTypeFlow::JsrSet::apply_control
 203 //
 204 // Apply the effect of a control-flow bytecode on the JsrSet.  The
 205 // only bytecodes that modify the JsrSet are jsr and ret.
 206 void ciTypeFlow::JsrSet::apply_control(ciTypeFlow* analyzer,
 207                                        ciBytecodeStream* str,
 208                                        ciTypeFlow::StateVector* state) {
 209   Bytecodes::Code code = str->cur_bc();
 210   if (code == Bytecodes::_jsr) {
 211     JsrRecord* record =
 212       analyzer->make_jsr_record(str->get_dest(), str->next_bci());
 213     insert_jsr_record(record);
 214   } else if (code == Bytecodes::_jsr_w) {
 215     JsrRecord* record =
 216       analyzer->make_jsr_record(str->get_far_dest(), str->next_bci());
 217     insert_jsr_record(record);
 218   } else if (code == Bytecodes::_ret) {
 219     Cell local = state->local(str->get_index());
 220     ciType* return_address = state->type_at(local);
 221     assert(return_address->is_return_address(), "verify: wrong type");
 222     if (size() == 0) {
 223       // Ret-state underflow:  Hit a ret w/o any previous jsrs.  Bail out.
 224       // This can happen when a loop is inside a finally clause (4614060).
 225       analyzer->record_failure("OSR in finally clause");
 226       return;
 227     }
 228     remove_jsr_record(return_address->as_return_address()->bci());
 229   }
 230 }
 231 
 232 #ifndef PRODUCT
 233 // ------------------------------------------------------------------
 234 // ciTypeFlow::JsrSet::print_on
 235 void ciTypeFlow::JsrSet::print_on(outputStream* st) const {
 236   st->print("{ ");
 237   int num_elements = size();
 238   if (num_elements > 0) {
 239     int i = 0;
 240     for( ; i < num_elements - 1; i++) {
 241       _set.at(i)->print_on(st);
 242       st->print(", ");
 243     }
 244     _set.at(i)->print_on(st);
 245     st->print(" ");
 246   }
 247   st->print("}");
 248 }
 249 #endif
 250 
 251 // ciTypeFlow::StateVector
 252 //
 253 // A StateVector summarizes the type information at some point in
 254 // the program.
 255 
 256 // ------------------------------------------------------------------
 257 // ciTypeFlow::StateVector::type_meet
 258 //
 259 // Meet two types.
 260 //
 261 // The semi-lattice of types use by this analysis are modeled on those
 262 // of the verifier.  The lattice is as follows:
 263 //
 264 //        top_type() >= all non-extremal types >= bottom_type
 265 //                             and
 266 //   Every primitive type is comparable only with itself.  The meet of
 267 //   reference types is determined by their kind: instance class,
 268 //   interface, or array class.  The meet of two types of the same
 269 //   kind is their least common ancestor.  The meet of two types of
 270 //   different kinds is always java.lang.Object.
 271 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
 272   assert(t1 != t2, "checked in caller");
 273   if (t1->equals(top_type())) {
 274     return t2;
 275   } else if (t2->equals(top_type())) {
 276     return t1;
 277   } else if (t1->is_primitive_type() || t2->is_primitive_type()) {










 278     // Special case null_type.  null_type meet any reference type T
 279     // is T.  null_type meet null_type is null_type.
 280     if (t1->equals(null_type())) {
 281       if (!t2->is_primitive_type() || t2->equals(null_type())) {
 282         return t2;
 283       }
 284     } else if (t2->equals(null_type())) {
 285       if (!t1->is_primitive_type()) {
 286         return t1;
 287       }
 288     }
 289 
 290     // At least one of the two types is a non-top primitive type.
 291     // The other type is not equal to it.  Fall to bottom.
 292     return bottom_type();
 293   } else {
 294     // Both types are non-top non-primitive types.  That is,
 295     // both types are either instanceKlasses or arrayKlasses.
 296     ciKlass* object_klass = analyzer->env()->Object_klass();
 297     ciKlass* k1 = t1->as_klass();
 298     ciKlass* k2 = t2->as_klass();
 299     if (k1->equals(object_klass) || k2->equals(object_klass)) {
 300       return object_klass;
 301     } else if (!k1->is_loaded() || !k2->is_loaded()) {
 302       // Unloaded classes fall to java.lang.Object at a merge.
 303       return object_klass;
 304     } else if (k1->is_interface() != k2->is_interface()) {
 305       // When an interface meets a non-interface, we get Object;
 306       // This is what the verifier does.
 307       return object_klass;
 308     } else if (k1->is_array_klass() || k2->is_array_klass()) {
 309       // When an array meets a non-array, we get Object.
 310       // When objArray meets typeArray, we also get Object.
 311       // And when typeArray meets different typeArray, we again get Object.
 312       // But when objArray meets objArray, we look carefully at element types.
 313       if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) {
 314         // Meet the element types, then construct the corresponding array type.
 315         ciKlass* elem1 = k1->as_obj_array_klass()->element_klass();
 316         ciKlass* elem2 = k2->as_obj_array_klass()->element_klass();
 317         ciKlass* elem  = type_meet_internal(elem1, elem2, analyzer)->as_klass();
 318         // Do an easy shortcut if one type is a super of the other.
 319         if (elem == elem1) {
 320           assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK");
 321           return k1;
 322         } else if (elem == elem2) {
 323           assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK");
 324           return k2;
 325         } else {
 326           return ciObjArrayKlass::make(elem);
 327         }

 328       } else {
 329         return object_klass;
 330       }
 331     } else {
 332       // Must be two plain old instance klasses.
 333       assert(k1->is_instance_klass(), "previous cases handle non-instances");
 334       assert(k2->is_instance_klass(), "previous cases handle non-instances");
 335       return k1->least_common_ancestor(k2);





 336     }

 337   }
 338 }
 339 
 340 
 341 // ------------------------------------------------------------------
 342 // ciTypeFlow::StateVector::StateVector
 343 //
 344 // Build a new state vector
 345 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
 346   _outer = analyzer;
 347   _stack_size = -1;
 348   _monitor_count = -1;
 349   // Allocate the _types array
 350   int max_cells = analyzer->max_cells();
 351   _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
 352   for (int i=0; i<max_cells; i++) {
 353     _types[i] = top_type();
 354   }
 355   _trap_bci = -1;
 356   _trap_index = 0;
 357   _def_locals.clear();
 358 }
 359 
 360 
 361 // ------------------------------------------------------------------
 362 // ciTypeFlow::get_start_state
 363 //
 364 // Set this vector to the method entry state.
 365 const ciTypeFlow::StateVector* ciTypeFlow::get_start_state() {
 366   StateVector* state = new StateVector(this);
 367   if (is_osr_flow()) {
 368     ciTypeFlow* non_osr_flow = method()->get_flow_analysis();
 369     if (non_osr_flow->failing()) {
 370       record_failure(non_osr_flow->failure_reason());
 371       return nullptr;
 372     }
 373     JsrSet* jsrs = new JsrSet(4);
 374     Block* non_osr_block = non_osr_flow->existing_block_at(start_bci(), jsrs);
 375     if (non_osr_block == nullptr) {
 376       record_failure("cannot reach OSR point");
 377       return nullptr;
 378     }
 379     // load up the non-OSR state at this point
 380     non_osr_block->copy_state_into(state);
 381     int non_osr_start = non_osr_block->start();
 382     if (non_osr_start != start_bci()) {
 383       // must flow forward from it
 384       if (CITraceTypeFlow) {
 385         tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
 386       }
 387       Block* block = block_at(non_osr_start, jsrs);
 388       assert(block->limit() == start_bci(), "must flow forward to start");
 389       flow_block(block, state, jsrs);
 390     }
 391     return state;
 392     // Note:  The code below would be an incorrect for an OSR flow,
 393     // even if it were possible for an OSR entry point to be at bci zero.
 394   }
 395   // "Push" the method signature into the first few locals.
 396   state->set_stack_size(-max_locals());
 397   if (!method()->is_static()) {
 398     state->push(method()->holder());





 399     assert(state->tos() == state->local(0), "");
 400   }
 401   for (ciSignatureStream str(method()->signature());
 402        !str.at_return_type();
 403        str.next()) {
 404     state->push_translate(str.type());
 405   }
 406   // Set the rest of the locals to bottom.
 407   assert(state->stack_size() <= 0, "stack size should not be strictly positive");
 408   while (state->stack_size() < 0) {
 409     state->push(state->bottom_type());
 410   }
 411   // Lock an object, if necessary.
 412   state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
 413   return state;
 414 }
 415 
 416 // ------------------------------------------------------------------
 417 // ciTypeFlow::StateVector::copy_into
 418 //
 419 // Copy our value into some other StateVector
 420 void ciTypeFlow::StateVector::copy_into(ciTypeFlow::StateVector* copy)
 421 const {
 422   copy->set_stack_size(stack_size());
 423   copy->set_monitor_count(monitor_count());
 424   Cell limit = limit_cell();
 425   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 426     copy->set_type_at(c, type_at(c));
 427   }
 428 }
 429 
 430 // ------------------------------------------------------------------
 431 // ciTypeFlow::StateVector::meet
 432 //
 433 // Meets this StateVector with another, destructively modifying this
 434 // one.  Returns true if any modification takes place.
 435 bool ciTypeFlow::StateVector::meet(const ciTypeFlow::StateVector* incoming) {
 436   if (monitor_count() == -1) {
 437     set_monitor_count(incoming->monitor_count());
 438   }
 439   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
 440 
 441   if (stack_size() == -1) {
 442     set_stack_size(incoming->stack_size());
 443     Cell limit = limit_cell();
 444     #ifdef ASSERT
 445     { for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 446         assert(type_at(c) == top_type(), "");
 447     } }
 448     #endif
 449     // Make a simple copy of the incoming state.
 450     for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 451       set_type_at(c, incoming->type_at(c));
 452     }
 453     return true;  // it is always different the first time
 454   }
 455 #ifdef ASSERT
 456   if (stack_size() != incoming->stack_size()) {
 457     _outer->method()->print_codes();
 458     tty->print_cr("!!!! Stack size conflict");
 459     tty->print_cr("Current state:");
 460     print_on(tty);
 461     tty->print_cr("Incoming state:");
 462     ((StateVector*)incoming)->print_on(tty);
 463   }
 464 #endif
 465   assert(stack_size() == incoming->stack_size(), "sanity");
 466 
 467   bool different = false;
 468   Cell limit = limit_cell();
 469   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 470     ciType* t1 = type_at(c);
 471     ciType* t2 = incoming->type_at(c);
 472     if (!t1->equals(t2)) {
 473       ciType* new_type = type_meet(t1, t2);
 474       if (!t1->equals(new_type)) {
 475         set_type_at(c, new_type);
 476         different = true;
 477       }
 478     }
 479   }
 480   return different;
 481 }
 482 
 483 // ------------------------------------------------------------------
 484 // ciTypeFlow::StateVector::meet_exception
 485 //
 486 // Meets this StateVector with another, destructively modifying this
 487 // one.  The incoming state is coming via an exception.  Returns true
 488 // if any modification takes place.
 489 bool ciTypeFlow::StateVector::meet_exception(ciInstanceKlass* exc,
 490                                      const ciTypeFlow::StateVector* incoming) {
 491   if (monitor_count() == -1) {
 492     set_monitor_count(incoming->monitor_count());
 493   }
 494   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
 495 
 496   if (stack_size() == -1) {
 497     set_stack_size(1);
 498   }
 499 
 500   assert(stack_size() ==  1, "must have one-element stack");
 501 
 502   bool different = false;
 503 
 504   // Meet locals from incoming array.
 505   Cell limit = local(_outer->max_locals()-1);
 506   for (Cell c = start_cell(); c <= limit; c = next_cell(c)) {
 507     ciType* t1 = type_at(c);
 508     ciType* t2 = incoming->type_at(c);
 509     if (!t1->equals(t2)) {
 510       ciType* new_type = type_meet(t1, t2);
 511       if (!t1->equals(new_type)) {
 512         set_type_at(c, new_type);
 513         different = true;
 514       }
 515     }
 516   }
 517 
 518   // Handle stack separately.  When an exception occurs, the
 519   // only stack entry is the exception instance.
 520   ciType* tos_type = type_at_tos();
 521   if (!tos_type->equals(exc)) {
 522     ciType* new_type = type_meet(tos_type, exc);
 523     if (!tos_type->equals(new_type)) {
 524       set_type_at_tos(new_type);
 525       different = true;
 526     }
 527   }
 528 
 529   return different;
 530 }
 531 
 532 // ------------------------------------------------------------------
 533 // ciTypeFlow::StateVector::push_translate
 534 void ciTypeFlow::StateVector::push_translate(ciType* type) {
 535   BasicType basic_type = type->basic_type();
 536   if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
 537       basic_type == T_BYTE    || basic_type == T_SHORT) {
 538     push_int();
 539   } else {
 540     push(type);
 541     if (type->is_two_word()) {
 542       push(half_type(type));
 543     }
 544   }
 545 }
 546 
 547 // ------------------------------------------------------------------
 548 // ciTypeFlow::StateVector::do_aaload
 549 void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
 550   pop_int();
 551   ciObjArrayKlass* array_klass = pop_objArray();
 552   if (array_klass == nullptr) {
 553     // Did aaload on a null reference; push a null and ignore the exception.
 554     // This instruction will never continue normally.  All we have to do
 555     // is report a value that will meet correctly with any downstream
 556     // reference types on paths that will truly be executed.  This null type
 557     // meets with any reference type to yield that same reference type.
 558     // (The compiler will generate an unconditional exception here.)
 559     push(null_type());
 560     return;
 561   }
 562   if (!array_klass->is_loaded()) {
 563     // Only fails for some -Xcomp runs
 564     trap(str, array_klass,
 565          Deoptimization::make_trap_request
 566          (Deoptimization::Reason_unloaded,
 567           Deoptimization::Action_reinterpret));
 568     return;
 569   }
 570   ciKlass* element_klass = array_klass->element_klass();
 571   if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
 572     Untested("unloaded array element class in ciTypeFlow");
 573     trap(str, element_klass,
 574          Deoptimization::make_trap_request
 575          (Deoptimization::Reason_unloaded,
 576           Deoptimization::Action_reinterpret));
 577   } else {
 578     push_object(element_klass);
 579   }
 580 }
 581 
 582 
 583 // ------------------------------------------------------------------
 584 // ciTypeFlow::StateVector::do_checkcast
 585 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
 586   bool will_link;
 587   ciKlass* klass = str->get_klass(will_link);
 588   if (!will_link) {
 589     // VM's interpreter will not load 'klass' if object is null.
 590     // Type flow after this block may still be needed in two situations:
 591     // 1) C2 uses do_null_assert() and continues compilation for later blocks
 592     // 2) C2 does an OSR compile in a later block (see bug 4778368).
 593     pop_object();
 594     do_null_assert(klass);
 595   } else {
 596     pop_object();






 597     push_object(klass);
 598   }
 599 }
 600 
 601 // ------------------------------------------------------------------
 602 // ciTypeFlow::StateVector::do_getfield
 603 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
 604   // could add assert here for type of object.
 605   pop_object();
 606   do_getstatic(str);
 607 }
 608 
 609 // ------------------------------------------------------------------
 610 // ciTypeFlow::StateVector::do_getstatic
 611 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
 612   bool will_link;
 613   ciField* field = str->get_field(will_link);
 614   if (!will_link) {
 615     trap(str, field->holder(), str->get_field_holder_index());
 616   } else {
 617     ciType* field_type = field->type();
 618     if (!field_type->is_loaded()) {









 619       // Normally, we need the field's type to be loaded if we are to
 620       // do anything interesting with its value.
 621       // We used to do this:  trap(str, str->get_field_signature_index());
 622       //
 623       // There is one good reason not to trap here.  Execution can
 624       // get past this "getfield" or "getstatic" if the value of
 625       // the field is null.  As long as the value is null, the class
 626       // does not need to be loaded!  The compiler must assume that
 627       // the value of the unloaded class reference is null; if the code
 628       // ever sees a non-null value, loading has occurred.
 629       //
 630       // This actually happens often enough to be annoying.  If the
 631       // compiler throws an uncommon trap at this bytecode, you can
 632       // get an endless loop of recompilations, when all the code
 633       // needs to do is load a series of null values.  Also, a trap
 634       // here can make an OSR entry point unreachable, triggering the
 635       // assert on non_osr_block in ciTypeFlow::get_start_state.
 636       // (See bug 4379915.)
 637       do_null_assert(field_type->as_klass());
 638     } else {



 639       push_translate(field_type);
 640     }
 641   }
 642 }
 643 
 644 // ------------------------------------------------------------------
 645 // ciTypeFlow::StateVector::do_invoke
 646 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
 647                                         bool has_receiver) {
 648   bool will_link;
 649   ciSignature* declared_signature = nullptr;
 650   ciMethod* callee = str->get_method(will_link, &declared_signature);
 651   assert(declared_signature != nullptr, "cannot be null");
 652   if (!will_link) {
 653     // We weren't able to find the method.
 654     if (str->cur_bc() == Bytecodes::_invokedynamic) {
 655       trap(str, nullptr,
 656            Deoptimization::make_trap_request
 657            (Deoptimization::Reason_uninitialized,
 658             Deoptimization::Action_reinterpret));
 659     } else {
 660       ciKlass* unloaded_holder = callee->holder();
 661       trap(str, unloaded_holder, str->get_method_holder_index());
 662     }
 663   } else {
 664     // We are using the declared signature here because it might be
 665     // different from the callee signature (Cf. invokedynamic and
 666     // invokehandle).
 667     ciSignatureStream sigstr(declared_signature);
 668     const int arg_size = declared_signature->size();
 669     const int stack_base = stack_size() - arg_size;
 670     int i = 0;
 671     for( ; !sigstr.at_return_type(); sigstr.next()) {
 672       ciType* type = sigstr.type();
 673       ciType* stack_type = type_at(stack(stack_base + i++));
 674       // Do I want to check this type?
 675       // assert(stack_type->is_subtype_of(type), "bad type for field value");
 676       if (type->is_two_word()) {
 677         ciType* stack_type2 = type_at(stack(stack_base + i++));
 678         assert(stack_type2->equals(half_type(type)), "must be 2nd half");
 679       }
 680     }
 681     assert(arg_size == i, "must match");
 682     for (int j = 0; j < arg_size; j++) {
 683       pop();
 684     }
 685     if (has_receiver) {
 686       // Check this?
 687       pop_object();
 688     }
 689     assert(!sigstr.is_done(), "must have return type");
 690     ciType* return_type = sigstr.type();
 691     if (!return_type->is_void()) {
 692       if (!return_type->is_loaded()) {
 693         // As in do_getstatic(), generally speaking, we need the return type to
 694         // be loaded if we are to do anything interesting with its value.
 695         // We used to do this:  trap(str, str->get_method_signature_index());
 696         //
 697         // We do not trap here since execution can get past this invoke if
 698         // the return value is null.  As long as the value is null, the class
 699         // does not need to be loaded!  The compiler must assume that
 700         // the value of the unloaded class reference is null; if the code
 701         // ever sees a non-null value, loading has occurred.
 702         //
 703         // See do_getstatic() for similar explanation, as well as bug 4684993.
 704         do_null_assert(return_type->as_klass());










 705       } else {
 706         push_translate(return_type);
 707       }
 708     }
 709   }
 710 }
 711 
 712 // ------------------------------------------------------------------
 713 // ciTypeFlow::StateVector::do_jsr
 714 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
 715   push(ciReturnAddress::make(str->next_bci()));
 716 }
 717 
 718 // ------------------------------------------------------------------
 719 // ciTypeFlow::StateVector::do_ldc
 720 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
 721   if (str->is_in_error()) {
 722     trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unhandled,
 723                                                          Deoptimization::Action_none));
 724     return;
 725   }
 726   ciConstant con = str->get_constant();
 727   if (con.is_valid()) {
 728     int cp_index = str->get_constant_pool_index();
 729     if (!con.is_loaded()) {
 730       trap(str, nullptr, Deoptimization::make_trap_request(Deoptimization::Reason_unloaded,
 731                                                            Deoptimization::Action_reinterpret,
 732                                                            cp_index));
 733       return;
 734     }
 735     BasicType basic_type = str->get_basic_type_for_constant_at(cp_index);
 736     if (is_reference_type(basic_type)) {
 737       ciObject* obj = con.as_object();
 738       if (obj->is_null_object()) {
 739         push_null();
 740       } else {
 741         assert(obj->is_instance() || obj->is_array(), "must be java_mirror of klass");
 742         push_object(obj->klass());




 743       }
 744     } else {
 745       assert(basic_type == con.basic_type() || con.basic_type() == T_OBJECT,
 746              "not a boxed form: %s vs %s", type2name(basic_type), type2name(con.basic_type()));
 747       push_translate(ciType::make(basic_type));
 748     }
 749   } else {
 750     // OutOfMemoryError in the CI while loading a String constant.
 751     push_null();
 752     outer()->record_failure("ldc did not link");
 753   }
 754 }
 755 
 756 // ------------------------------------------------------------------
 757 // ciTypeFlow::StateVector::do_multianewarray
 758 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
 759   int dimensions = str->get_dimensions();
 760   bool will_link;
 761   ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
 762   if (!will_link) {
 763     trap(str, array_klass, str->get_klass_index());
 764   } else {
 765     for (int i = 0; i < dimensions; i++) {
 766       pop_int();
 767     }
 768     push_object(array_klass);
 769   }
 770 }
 771 
 772 // ------------------------------------------------------------------
 773 // ciTypeFlow::StateVector::do_new
 774 void ciTypeFlow::StateVector::do_new(ciBytecodeStream* str) {
 775   bool will_link;
 776   ciKlass* klass = str->get_klass(will_link);
 777   if (!will_link || str->is_unresolved_klass()) {
 778     trap(str, klass, str->get_klass_index());
 779   } else {
 780     push_object(klass);
 781   }
 782 }
 783 
 784 // ------------------------------------------------------------------
 785 // ciTypeFlow::StateVector::do_newarray
 786 void ciTypeFlow::StateVector::do_newarray(ciBytecodeStream* str) {
 787   pop_int();
 788   ciKlass* klass = ciTypeArrayKlass::make((BasicType)str->get_index());
 789   push_object(klass);
 790 }
 791 
 792 // ------------------------------------------------------------------
 793 // ciTypeFlow::StateVector::do_putfield
 794 void ciTypeFlow::StateVector::do_putfield(ciBytecodeStream* str) {
 795   do_putstatic(str);
 796   if (_trap_bci != -1)  return;  // unloaded field holder, etc.
 797   // could add assert here for type of object.
 798   pop_object();
 799 }
 800 
 801 // ------------------------------------------------------------------
 802 // ciTypeFlow::StateVector::do_putstatic
 803 void ciTypeFlow::StateVector::do_putstatic(ciBytecodeStream* str) {
 804   bool will_link;
 805   ciField* field = str->get_field(will_link);
 806   if (!will_link) {
 807     trap(str, field->holder(), str->get_field_holder_index());
 808   } else {
 809     ciType* field_type = field->type();
 810     ciType* type = pop_value();
 811     // Do I want to check this type?
 812     //      assert(type->is_subtype_of(field_type), "bad type for field value");
 813     if (field_type->is_two_word()) {
 814       ciType* type2 = pop_value();
 815       assert(type2->is_two_word(), "must be 2nd half");
 816       assert(type == half_type(type2), "must be 2nd half");
 817     }
 818   }
 819 }
 820 
 821 // ------------------------------------------------------------------
 822 // ciTypeFlow::StateVector::do_ret
 823 void ciTypeFlow::StateVector::do_ret(ciBytecodeStream* str) {
 824   Cell index = local(str->get_index());
 825 
 826   ciType* address = type_at(index);
 827   assert(address->is_return_address(), "bad return address");
 828   set_type_at(index, bottom_type());
 829 }
 830 
 831 // ------------------------------------------------------------------
 832 // ciTypeFlow::StateVector::trap
 833 //
 834 // Stop interpretation of this path with a trap.
 835 void ciTypeFlow::StateVector::trap(ciBytecodeStream* str, ciKlass* klass, int index) {
 836   _trap_bci = str->cur_bci();
 837   _trap_index = index;
 838 
 839   // Log information about this trap:
 840   CompileLog* log = outer()->env()->log();
 841   if (log != nullptr) {
 842     int mid = log->identify(outer()->method());
 843     int kid = (klass == nullptr)? -1: log->identify(klass);
 844     log->begin_elem("uncommon_trap method='%d' bci='%d'", mid, str->cur_bci());
 845     char buf[100];
 846     log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf),
 847                                                           index));
 848     if (kid >= 0)
 849       log->print(" klass='%d'", kid);
 850     log->end_elem();
 851   }
 852 }
 853 
 854 // ------------------------------------------------------------------
 855 // ciTypeFlow::StateVector::do_null_assert
 856 // Corresponds to graphKit::do_null_assert.
 857 void ciTypeFlow::StateVector::do_null_assert(ciKlass* unloaded_klass) {
 858   if (unloaded_klass->is_loaded()) {
 859     // We failed to link, but we can still compute with this class,
 860     // since it is loaded somewhere.  The compiler will uncommon_trap
 861     // if the object is not null, but the typeflow pass can not assume
 862     // that the object will be null, otherwise it may incorrectly tell
 863     // the parser that an object is known to be null. 4761344, 4807707
 864     push_object(unloaded_klass);
 865   } else {
 866     // The class is not loaded anywhere.  It is safe to model the
 867     // null in the typestates, because we can compile in a null check
 868     // which will deoptimize us if someone manages to load the
 869     // class later.
 870     push_null();
 871   }
 872 }
 873 
 874 
 875 // ------------------------------------------------------------------
 876 // ciTypeFlow::StateVector::apply_one_bytecode
 877 //
 878 // Apply the effect of one bytecode to this StateVector
 879 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
 880   _trap_bci = -1;
 881   _trap_index = 0;
 882 
 883   if (CITraceTypeFlow) {
 884     tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
 885                   Bytecodes::name(str->cur_bc()));
 886   }
 887 
 888   switch(str->cur_bc()) {
 889   case Bytecodes::_aaload: do_aaload(str);                       break;
 890 
 891   case Bytecodes::_aastore:
 892     {
 893       pop_object();
 894       pop_int();
 895       pop_objArray();
 896       break;
 897     }
 898   case Bytecodes::_aconst_null:
 899     {
 900       push_null();
 901       break;
 902     }
 903   case Bytecodes::_aload:   load_local_object(str->get_index());    break;
 904   case Bytecodes::_aload_0: load_local_object(0);                   break;
 905   case Bytecodes::_aload_1: load_local_object(1);                   break;
 906   case Bytecodes::_aload_2: load_local_object(2);                   break;
 907   case Bytecodes::_aload_3: load_local_object(3);                   break;
 908 
 909   case Bytecodes::_anewarray:
 910     {
 911       pop_int();
 912       bool will_link;
 913       ciKlass* element_klass = str->get_klass(will_link);
 914       if (!will_link) {
 915         trap(str, element_klass, str->get_klass_index());
 916       } else {
 917         push_object(ciObjArrayKlass::make(element_klass));
 918       }
 919       break;
 920     }
 921   case Bytecodes::_areturn:
 922   case Bytecodes::_ifnonnull:
 923   case Bytecodes::_ifnull:
 924     {
 925       pop_object();
 926       break;
 927     }
 928   case Bytecodes::_monitorenter:
 929     {
 930       pop_object();
 931       set_monitor_count(monitor_count() + 1);
 932       break;
 933     }
 934   case Bytecodes::_monitorexit:
 935     {
 936       pop_object();
 937       assert(monitor_count() > 0, "must be a monitor to exit from");
 938       set_monitor_count(monitor_count() - 1);
 939       break;
 940     }
 941   case Bytecodes::_arraylength:
 942     {
 943       pop_array();
 944       push_int();
 945       break;
 946     }
 947   case Bytecodes::_astore:   store_local_object(str->get_index());  break;
 948   case Bytecodes::_astore_0: store_local_object(0);                 break;
 949   case Bytecodes::_astore_1: store_local_object(1);                 break;
 950   case Bytecodes::_astore_2: store_local_object(2);                 break;
 951   case Bytecodes::_astore_3: store_local_object(3);                 break;
 952 
 953   case Bytecodes::_athrow:
 954     {
 955       NEEDS_CLEANUP;
 956       pop_object();
 957       break;
 958     }
 959   case Bytecodes::_baload:
 960   case Bytecodes::_caload:
 961   case Bytecodes::_iaload:
 962   case Bytecodes::_saload:
 963     {
 964       pop_int();
 965       ciTypeArrayKlass* array_klass = pop_typeArray();
 966       // Put assert here for right type?
 967       push_int();
 968       break;
 969     }
 970   case Bytecodes::_bastore:
 971   case Bytecodes::_castore:
 972   case Bytecodes::_iastore:
 973   case Bytecodes::_sastore:
 974     {
 975       pop_int();
 976       pop_int();
 977       pop_typeArray();
 978       // assert here?
 979       break;
 980     }
 981   case Bytecodes::_bipush:
 982   case Bytecodes::_iconst_m1:
 983   case Bytecodes::_iconst_0:
 984   case Bytecodes::_iconst_1:
 985   case Bytecodes::_iconst_2:
 986   case Bytecodes::_iconst_3:
 987   case Bytecodes::_iconst_4:
 988   case Bytecodes::_iconst_5:
 989   case Bytecodes::_sipush:
 990     {
 991       push_int();
 992       break;
 993     }
 994   case Bytecodes::_checkcast: do_checkcast(str);                  break;
 995 
 996   case Bytecodes::_d2f:
 997     {
 998       pop_double();
 999       push_float();
1000       break;
1001     }
1002   case Bytecodes::_d2i:
1003     {
1004       pop_double();
1005       push_int();
1006       break;
1007     }
1008   case Bytecodes::_d2l:
1009     {
1010       pop_double();
1011       push_long();
1012       break;
1013     }
1014   case Bytecodes::_dadd:
1015   case Bytecodes::_ddiv:
1016   case Bytecodes::_dmul:
1017   case Bytecodes::_drem:
1018   case Bytecodes::_dsub:
1019     {
1020       pop_double();
1021       pop_double();
1022       push_double();
1023       break;
1024     }
1025   case Bytecodes::_daload:
1026     {
1027       pop_int();
1028       ciTypeArrayKlass* array_klass = pop_typeArray();
1029       // Put assert here for right type?
1030       push_double();
1031       break;
1032     }
1033   case Bytecodes::_dastore:
1034     {
1035       pop_double();
1036       pop_int();
1037       pop_typeArray();
1038       // assert here?
1039       break;
1040     }
1041   case Bytecodes::_dcmpg:
1042   case Bytecodes::_dcmpl:
1043     {
1044       pop_double();
1045       pop_double();
1046       push_int();
1047       break;
1048     }
1049   case Bytecodes::_dconst_0:
1050   case Bytecodes::_dconst_1:
1051     {
1052       push_double();
1053       break;
1054     }
1055   case Bytecodes::_dload:   load_local_double(str->get_index());    break;
1056   case Bytecodes::_dload_0: load_local_double(0);                   break;
1057   case Bytecodes::_dload_1: load_local_double(1);                   break;
1058   case Bytecodes::_dload_2: load_local_double(2);                   break;
1059   case Bytecodes::_dload_3: load_local_double(3);                   break;
1060 
1061   case Bytecodes::_dneg:
1062     {
1063       pop_double();
1064       push_double();
1065       break;
1066     }
1067   case Bytecodes::_dreturn:
1068     {
1069       pop_double();
1070       break;
1071     }
1072   case Bytecodes::_dstore:   store_local_double(str->get_index());  break;
1073   case Bytecodes::_dstore_0: store_local_double(0);                 break;
1074   case Bytecodes::_dstore_1: store_local_double(1);                 break;
1075   case Bytecodes::_dstore_2: store_local_double(2);                 break;
1076   case Bytecodes::_dstore_3: store_local_double(3);                 break;
1077 
1078   case Bytecodes::_dup:
1079     {
1080       push(type_at_tos());
1081       break;
1082     }
1083   case Bytecodes::_dup_x1:
1084     {
1085       ciType* value1 = pop_value();
1086       ciType* value2 = pop_value();
1087       push(value1);
1088       push(value2);
1089       push(value1);
1090       break;
1091     }
1092   case Bytecodes::_dup_x2:
1093     {
1094       ciType* value1 = pop_value();
1095       ciType* value2 = pop_value();
1096       ciType* value3 = pop_value();
1097       push(value1);
1098       push(value3);
1099       push(value2);
1100       push(value1);
1101       break;
1102     }
1103   case Bytecodes::_dup2:
1104     {
1105       ciType* value1 = pop_value();
1106       ciType* value2 = pop_value();
1107       push(value2);
1108       push(value1);
1109       push(value2);
1110       push(value1);
1111       break;
1112     }
1113   case Bytecodes::_dup2_x1:
1114     {
1115       ciType* value1 = pop_value();
1116       ciType* value2 = pop_value();
1117       ciType* value3 = pop_value();
1118       push(value2);
1119       push(value1);
1120       push(value3);
1121       push(value2);
1122       push(value1);
1123       break;
1124     }
1125   case Bytecodes::_dup2_x2:
1126     {
1127       ciType* value1 = pop_value();
1128       ciType* value2 = pop_value();
1129       ciType* value3 = pop_value();
1130       ciType* value4 = pop_value();
1131       push(value2);
1132       push(value1);
1133       push(value4);
1134       push(value3);
1135       push(value2);
1136       push(value1);
1137       break;
1138     }
1139   case Bytecodes::_f2d:
1140     {
1141       pop_float();
1142       push_double();
1143       break;
1144     }
1145   case Bytecodes::_f2i:
1146     {
1147       pop_float();
1148       push_int();
1149       break;
1150     }
1151   case Bytecodes::_f2l:
1152     {
1153       pop_float();
1154       push_long();
1155       break;
1156     }
1157   case Bytecodes::_fadd:
1158   case Bytecodes::_fdiv:
1159   case Bytecodes::_fmul:
1160   case Bytecodes::_frem:
1161   case Bytecodes::_fsub:
1162     {
1163       pop_float();
1164       pop_float();
1165       push_float();
1166       break;
1167     }
1168   case Bytecodes::_faload:
1169     {
1170       pop_int();
1171       ciTypeArrayKlass* array_klass = pop_typeArray();
1172       // Put assert here.
1173       push_float();
1174       break;
1175     }
1176   case Bytecodes::_fastore:
1177     {
1178       pop_float();
1179       pop_int();
1180       ciTypeArrayKlass* array_klass = pop_typeArray();
1181       // Put assert here.
1182       break;
1183     }
1184   case Bytecodes::_fcmpg:
1185   case Bytecodes::_fcmpl:
1186     {
1187       pop_float();
1188       pop_float();
1189       push_int();
1190       break;
1191     }
1192   case Bytecodes::_fconst_0:
1193   case Bytecodes::_fconst_1:
1194   case Bytecodes::_fconst_2:
1195     {
1196       push_float();
1197       break;
1198     }
1199   case Bytecodes::_fload:   load_local_float(str->get_index());     break;
1200   case Bytecodes::_fload_0: load_local_float(0);                    break;
1201   case Bytecodes::_fload_1: load_local_float(1);                    break;
1202   case Bytecodes::_fload_2: load_local_float(2);                    break;
1203   case Bytecodes::_fload_3: load_local_float(3);                    break;
1204 
1205   case Bytecodes::_fneg:
1206     {
1207       pop_float();
1208       push_float();
1209       break;
1210     }
1211   case Bytecodes::_freturn:
1212     {
1213       pop_float();
1214       break;
1215     }
1216   case Bytecodes::_fstore:    store_local_float(str->get_index());   break;
1217   case Bytecodes::_fstore_0:  store_local_float(0);                  break;
1218   case Bytecodes::_fstore_1:  store_local_float(1);                  break;
1219   case Bytecodes::_fstore_2:  store_local_float(2);                  break;
1220   case Bytecodes::_fstore_3:  store_local_float(3);                  break;
1221 
1222   case Bytecodes::_getfield:  do_getfield(str);                      break;
1223   case Bytecodes::_getstatic: do_getstatic(str);                     break;
1224 
1225   case Bytecodes::_goto:
1226   case Bytecodes::_goto_w:
1227   case Bytecodes::_nop:
1228   case Bytecodes::_return:
1229     {
1230       // do nothing.
1231       break;
1232     }
1233   case Bytecodes::_i2b:
1234   case Bytecodes::_i2c:
1235   case Bytecodes::_i2s:
1236   case Bytecodes::_ineg:
1237     {
1238       pop_int();
1239       push_int();
1240       break;
1241     }
1242   case Bytecodes::_i2d:
1243     {
1244       pop_int();
1245       push_double();
1246       break;
1247     }
1248   case Bytecodes::_i2f:
1249     {
1250       pop_int();
1251       push_float();
1252       break;
1253     }
1254   case Bytecodes::_i2l:
1255     {
1256       pop_int();
1257       push_long();
1258       break;
1259     }
1260   case Bytecodes::_iadd:
1261   case Bytecodes::_iand:
1262   case Bytecodes::_idiv:
1263   case Bytecodes::_imul:
1264   case Bytecodes::_ior:
1265   case Bytecodes::_irem:
1266   case Bytecodes::_ishl:
1267   case Bytecodes::_ishr:
1268   case Bytecodes::_isub:
1269   case Bytecodes::_iushr:
1270   case Bytecodes::_ixor:
1271     {
1272       pop_int();
1273       pop_int();
1274       push_int();
1275       break;
1276     }
1277   case Bytecodes::_if_acmpeq:
1278   case Bytecodes::_if_acmpne:
1279     {
1280       pop_object();
1281       pop_object();
1282       break;
1283     }
1284   case Bytecodes::_if_icmpeq:
1285   case Bytecodes::_if_icmpge:
1286   case Bytecodes::_if_icmpgt:
1287   case Bytecodes::_if_icmple:
1288   case Bytecodes::_if_icmplt:
1289   case Bytecodes::_if_icmpne:
1290     {
1291       pop_int();
1292       pop_int();
1293       break;
1294     }
1295   case Bytecodes::_ifeq:
1296   case Bytecodes::_ifle:
1297   case Bytecodes::_iflt:
1298   case Bytecodes::_ifge:
1299   case Bytecodes::_ifgt:
1300   case Bytecodes::_ifne:
1301   case Bytecodes::_ireturn:
1302   case Bytecodes::_lookupswitch:
1303   case Bytecodes::_tableswitch:
1304     {
1305       pop_int();
1306       break;
1307     }
1308   case Bytecodes::_iinc:
1309     {
1310       int lnum = str->get_index();
1311       check_int(local(lnum));
1312       store_to_local(lnum);
1313       break;
1314     }
1315   case Bytecodes::_iload:   load_local_int(str->get_index()); break;
1316   case Bytecodes::_iload_0: load_local_int(0);                      break;
1317   case Bytecodes::_iload_1: load_local_int(1);                      break;
1318   case Bytecodes::_iload_2: load_local_int(2);                      break;
1319   case Bytecodes::_iload_3: load_local_int(3);                      break;
1320 
1321   case Bytecodes::_instanceof:
1322     {
1323       // Check for uncommon trap:
1324       do_checkcast(str);
1325       pop_object();
1326       push_int();
1327       break;
1328     }
1329   case Bytecodes::_invokeinterface: do_invoke(str, true);           break;
1330   case Bytecodes::_invokespecial:   do_invoke(str, true);           break;
1331   case Bytecodes::_invokestatic:    do_invoke(str, false);          break;
1332   case Bytecodes::_invokevirtual:   do_invoke(str, true);           break;
1333   case Bytecodes::_invokedynamic:   do_invoke(str, false);          break;
1334 
1335   case Bytecodes::_istore:   store_local_int(str->get_index());     break;
1336   case Bytecodes::_istore_0: store_local_int(0);                    break;
1337   case Bytecodes::_istore_1: store_local_int(1);                    break;
1338   case Bytecodes::_istore_2: store_local_int(2);                    break;
1339   case Bytecodes::_istore_3: store_local_int(3);                    break;
1340 
1341   case Bytecodes::_jsr:
1342   case Bytecodes::_jsr_w: do_jsr(str);                              break;
1343 
1344   case Bytecodes::_l2d:
1345     {
1346       pop_long();
1347       push_double();
1348       break;
1349     }
1350   case Bytecodes::_l2f:
1351     {
1352       pop_long();
1353       push_float();
1354       break;
1355     }
1356   case Bytecodes::_l2i:
1357     {
1358       pop_long();
1359       push_int();
1360       break;
1361     }
1362   case Bytecodes::_ladd:
1363   case Bytecodes::_land:
1364   case Bytecodes::_ldiv:
1365   case Bytecodes::_lmul:
1366   case Bytecodes::_lor:
1367   case Bytecodes::_lrem:
1368   case Bytecodes::_lsub:
1369   case Bytecodes::_lxor:
1370     {
1371       pop_long();
1372       pop_long();
1373       push_long();
1374       break;
1375     }
1376   case Bytecodes::_laload:
1377     {
1378       pop_int();
1379       ciTypeArrayKlass* array_klass = pop_typeArray();
1380       // Put assert here for right type?
1381       push_long();
1382       break;
1383     }
1384   case Bytecodes::_lastore:
1385     {
1386       pop_long();
1387       pop_int();
1388       pop_typeArray();
1389       // assert here?
1390       break;
1391     }
1392   case Bytecodes::_lcmp:
1393     {
1394       pop_long();
1395       pop_long();
1396       push_int();
1397       break;
1398     }
1399   case Bytecodes::_lconst_0:
1400   case Bytecodes::_lconst_1:
1401     {
1402       push_long();
1403       break;
1404     }
1405   case Bytecodes::_ldc:
1406   case Bytecodes::_ldc_w:
1407   case Bytecodes::_ldc2_w:
1408     {
1409       do_ldc(str);
1410       break;
1411     }
1412 
1413   case Bytecodes::_lload:   load_local_long(str->get_index());      break;
1414   case Bytecodes::_lload_0: load_local_long(0);                     break;
1415   case Bytecodes::_lload_1: load_local_long(1);                     break;
1416   case Bytecodes::_lload_2: load_local_long(2);                     break;
1417   case Bytecodes::_lload_3: load_local_long(3);                     break;
1418 
1419   case Bytecodes::_lneg:
1420     {
1421       pop_long();
1422       push_long();
1423       break;
1424     }
1425   case Bytecodes::_lreturn:
1426     {
1427       pop_long();
1428       break;
1429     }
1430   case Bytecodes::_lshl:
1431   case Bytecodes::_lshr:
1432   case Bytecodes::_lushr:
1433     {
1434       pop_int();
1435       pop_long();
1436       push_long();
1437       break;
1438     }
1439   case Bytecodes::_lstore:   store_local_long(str->get_index());    break;
1440   case Bytecodes::_lstore_0: store_local_long(0);                   break;
1441   case Bytecodes::_lstore_1: store_local_long(1);                   break;
1442   case Bytecodes::_lstore_2: store_local_long(2);                   break;
1443   case Bytecodes::_lstore_3: store_local_long(3);                   break;
1444 
1445   case Bytecodes::_multianewarray: do_multianewarray(str);          break;
1446 
1447   case Bytecodes::_new:      do_new(str);                           break;
1448 
1449   case Bytecodes::_newarray: do_newarray(str);                      break;
1450 
1451   case Bytecodes::_pop:
1452     {
1453       pop();
1454       break;
1455     }
1456   case Bytecodes::_pop2:
1457     {
1458       pop();
1459       pop();
1460       break;
1461     }
1462 
1463   case Bytecodes::_putfield:       do_putfield(str);                 break;
1464   case Bytecodes::_putstatic:      do_putstatic(str);                break;
1465 
1466   case Bytecodes::_ret: do_ret(str);                                 break;
1467 
1468   case Bytecodes::_swap:
1469     {
1470       ciType* value1 = pop_value();
1471       ciType* value2 = pop_value();
1472       push(value1);
1473       push(value2);
1474       break;
1475     }

1476   case Bytecodes::_wide:
1477   default:
1478     {
1479       // The iterator should skip this.
1480       ShouldNotReachHere();
1481       break;
1482     }
1483   }
1484 
1485   if (CITraceTypeFlow) {
1486     print_on(tty);
1487   }
1488 
1489   return (_trap_bci != -1);
1490 }
1491 
1492 #ifndef PRODUCT
1493 // ------------------------------------------------------------------
1494 // ciTypeFlow::StateVector::print_cell_on
1495 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1496   ciType* type = type_at(c);
1497   if (type == top_type()) {
1498     st->print("top");
1499   } else if (type == bottom_type()) {
1500     st->print("bottom");
1501   } else if (type == null_type()) {
1502     st->print("null");
1503   } else if (type == long2_type()) {
1504     st->print("long2");
1505   } else if (type == double2_type()) {
1506     st->print("double2");
1507   } else if (is_int(type)) {
1508     st->print("int");
1509   } else if (is_long(type)) {
1510     st->print("long");
1511   } else if (is_float(type)) {
1512     st->print("float");
1513   } else if (is_double(type)) {
1514     st->print("double");
1515   } else if (type->is_return_address()) {
1516     st->print("address(%d)", type->as_return_address()->bci());
1517   } else {
1518     if (type->is_klass()) {
1519       type->as_klass()->name()->print_symbol_on(st);
1520     } else {
1521       st->print("UNEXPECTED TYPE");
1522       type->print();
1523     }
1524   }
1525 }
1526 
1527 // ------------------------------------------------------------------
1528 // ciTypeFlow::StateVector::print_on
1529 void ciTypeFlow::StateVector::print_on(outputStream* st) const {
1530   int num_locals   = _outer->max_locals();
1531   int num_stack    = stack_size();
1532   int num_monitors = monitor_count();
1533   st->print_cr("  State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors);
1534   if (num_stack >= 0) {
1535     int i;
1536     for (i = 0; i < num_locals; i++) {
1537       st->print("    local %2d : ", i);
1538       print_cell_on(st, local(i));
1539       st->cr();
1540     }
1541     for (i = 0; i < num_stack; i++) {
1542       st->print("    stack %2d : ", i);
1543       print_cell_on(st, stack(i));
1544       st->cr();
1545     }
1546   }
1547 }
1548 #endif
1549 
1550 
1551 // ------------------------------------------------------------------
1552 // ciTypeFlow::SuccIter::next
1553 //
1554 void ciTypeFlow::SuccIter::next() {
1555   int succ_ct = _pred->successors()->length();
1556   int next = _index + 1;
1557   if (next < succ_ct) {
1558     _index = next;
1559     _succ = _pred->successors()->at(next);
1560     return;
1561   }
1562   for (int i = next - succ_ct; i < _pred->exceptions()->length(); i++) {
1563     // Do not compile any code for unloaded exception types.
1564     // Following compiler passes are responsible for doing this also.
1565     ciInstanceKlass* exception_klass = _pred->exc_klasses()->at(i);
1566     if (exception_klass->is_loaded()) {
1567       _index = next;
1568       _succ = _pred->exceptions()->at(i);
1569       return;
1570     }
1571     next++;
1572   }
1573   _index = -1;
1574   _succ = nullptr;
1575 }
1576 
1577 // ------------------------------------------------------------------
1578 // ciTypeFlow::SuccIter::set_succ
1579 //
1580 void ciTypeFlow::SuccIter::set_succ(Block* succ) {
1581   int succ_ct = _pred->successors()->length();
1582   if (_index < succ_ct) {
1583     _pred->successors()->at_put(_index, succ);
1584   } else {
1585     int idx = _index - succ_ct;
1586     _pred->exceptions()->at_put(idx, succ);
1587   }
1588 }
1589 
1590 // ciTypeFlow::Block
1591 //
1592 // A basic block.
1593 
1594 // ------------------------------------------------------------------
1595 // ciTypeFlow::Block::Block
1596 ciTypeFlow::Block::Block(ciTypeFlow* outer,
1597                          ciBlock *ciblk,
1598                          ciTypeFlow::JsrSet* jsrs) : _predecessors(outer->arena(), 1, 0, nullptr) {
1599   _ciblock = ciblk;
1600   _exceptions = nullptr;
1601   _exc_klasses = nullptr;
1602   _successors = nullptr;
1603   _state = new (outer->arena()) StateVector(outer);
1604   JsrSet* new_jsrs =
1605     new (outer->arena()) JsrSet(outer->arena(), jsrs->size());
1606   jsrs->copy_into(new_jsrs);
1607   _jsrs = new_jsrs;
1608   _next = nullptr;
1609   _on_work_list = false;
1610   _backedge_copy = false;
1611   _has_monitorenter = false;
1612   _trap_bci = -1;
1613   _trap_index = 0;
1614   df_init();
1615 
1616   if (CITraceTypeFlow) {
1617     tty->print_cr(">> Created new block");
1618     print_on(tty);
1619   }
1620 
1621   assert(this->outer() == outer, "outer link set up");
1622   assert(!outer->have_block_count(), "must not have mapped blocks yet");
1623 }
1624 
1625 // ------------------------------------------------------------------
1626 // ciTypeFlow::Block::df_init
1627 void ciTypeFlow::Block::df_init() {
1628   _pre_order = -1; assert(!has_pre_order(), "");
1629   _post_order = -1; assert(!has_post_order(), "");
1630   _loop = nullptr;
1631   _irreducible_loop_head = false;
1632   _irreducible_loop_secondary_entry = false;
1633   _rpo_next = nullptr;
1634 }
1635 
1636 // ------------------------------------------------------------------
1637 // ciTypeFlow::Block::successors
1638 //
1639 // Get the successors for this Block.
1640 GrowableArray<ciTypeFlow::Block*>*
1641 ciTypeFlow::Block::successors(ciBytecodeStream* str,
1642                               ciTypeFlow::StateVector* state,
1643                               ciTypeFlow::JsrSet* jsrs) {
1644   if (_successors == nullptr) {
1645     if (CITraceTypeFlow) {
1646       tty->print(">> Computing successors for block ");
1647       print_value_on(tty);
1648       tty->cr();
1649     }
1650 
1651     ciTypeFlow* analyzer = outer();
1652     Arena* arena = analyzer->arena();
1653     Block* block = nullptr;
1654     bool has_successor = !has_trap() &&
1655                          (control() != ciBlock::fall_through_bci || limit() < analyzer->code_size());
1656     if (!has_successor) {
1657       _successors =
1658         new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1659       // No successors
1660     } else if (control() == ciBlock::fall_through_bci) {
1661       assert(str->cur_bci() == limit(), "bad block end");
1662       // This block simply falls through to the next.
1663       _successors =
1664         new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1665 
1666       Block* block = analyzer->block_at(limit(), _jsrs);
1667       assert(_successors->length() == FALL_THROUGH, "");
1668       _successors->append(block);
1669     } else {
1670       int current_bci = str->cur_bci();
1671       int next_bci = str->next_bci();
1672       int branch_bci = -1;
1673       Block* target = nullptr;
1674       assert(str->next_bci() == limit(), "bad block end");
1675       // This block is not a simple fall-though.  Interpret
1676       // the current bytecode to find our successors.
1677       switch (str->cur_bc()) {
1678       case Bytecodes::_ifeq:         case Bytecodes::_ifne:
1679       case Bytecodes::_iflt:         case Bytecodes::_ifge:
1680       case Bytecodes::_ifgt:         case Bytecodes::_ifle:
1681       case Bytecodes::_if_icmpeq:    case Bytecodes::_if_icmpne:
1682       case Bytecodes::_if_icmplt:    case Bytecodes::_if_icmpge:
1683       case Bytecodes::_if_icmpgt:    case Bytecodes::_if_icmple:
1684       case Bytecodes::_if_acmpeq:    case Bytecodes::_if_acmpne:
1685       case Bytecodes::_ifnull:       case Bytecodes::_ifnonnull:
1686         // Our successors are the branch target and the next bci.
1687         branch_bci = str->get_dest();
1688         _successors =
1689           new (arena) GrowableArray<Block*>(arena, 2, 0, nullptr);
1690         assert(_successors->length() == IF_NOT_TAKEN, "");
1691         _successors->append(analyzer->block_at(next_bci, jsrs));
1692         assert(_successors->length() == IF_TAKEN, "");
1693         _successors->append(analyzer->block_at(branch_bci, jsrs));
1694         break;
1695 
1696       case Bytecodes::_goto:
1697         branch_bci = str->get_dest();
1698         _successors =
1699           new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1700         assert(_successors->length() == GOTO_TARGET, "");
1701         _successors->append(analyzer->block_at(branch_bci, jsrs));
1702         break;
1703 
1704       case Bytecodes::_jsr:
1705         branch_bci = str->get_dest();
1706         _successors =
1707           new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1708         assert(_successors->length() == GOTO_TARGET, "");
1709         _successors->append(analyzer->block_at(branch_bci, jsrs));
1710         break;
1711 
1712       case Bytecodes::_goto_w:
1713       case Bytecodes::_jsr_w:
1714         _successors =
1715           new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1716         assert(_successors->length() == GOTO_TARGET, "");
1717         _successors->append(analyzer->block_at(str->get_far_dest(), jsrs));
1718         break;
1719 
1720       case Bytecodes::_tableswitch:  {
1721         Bytecode_tableswitch tableswitch(str);
1722 
1723         int len = tableswitch.length();
1724         _successors =
1725           new (arena) GrowableArray<Block*>(arena, len+1, 0, nullptr);
1726         int bci = current_bci + tableswitch.default_offset();
1727         Block* block = analyzer->block_at(bci, jsrs);
1728         assert(_successors->length() == SWITCH_DEFAULT, "");
1729         _successors->append(block);
1730         while (--len >= 0) {
1731           int bci = current_bci + tableswitch.dest_offset_at(len);
1732           block = analyzer->block_at(bci, jsrs);
1733           assert(_successors->length() >= SWITCH_CASES, "");
1734           _successors->append_if_missing(block);
1735         }
1736         break;
1737       }
1738 
1739       case Bytecodes::_lookupswitch: {
1740         Bytecode_lookupswitch lookupswitch(str);
1741 
1742         int npairs = lookupswitch.number_of_pairs();
1743         _successors =
1744           new (arena) GrowableArray<Block*>(arena, npairs+1, 0, nullptr);
1745         int bci = current_bci + lookupswitch.default_offset();
1746         Block* block = analyzer->block_at(bci, jsrs);
1747         assert(_successors->length() == SWITCH_DEFAULT, "");
1748         _successors->append(block);
1749         while(--npairs >= 0) {
1750           LookupswitchPair pair = lookupswitch.pair_at(npairs);
1751           int bci = current_bci + pair.offset();
1752           Block* block = analyzer->block_at(bci, jsrs);
1753           assert(_successors->length() >= SWITCH_CASES, "");
1754           _successors->append_if_missing(block);
1755         }
1756         break;
1757       }
1758 
1759       case Bytecodes::_athrow:     case Bytecodes::_ireturn:
1760       case Bytecodes::_lreturn:    case Bytecodes::_freturn:
1761       case Bytecodes::_dreturn:    case Bytecodes::_areturn:



1762       case Bytecodes::_return:
1763         _successors =
1764           new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1765         // No successors
1766         break;
1767 
1768       case Bytecodes::_ret: {
1769         _successors =
1770           new (arena) GrowableArray<Block*>(arena, 1, 0, nullptr);
1771 
1772         Cell local = state->local(str->get_index());
1773         ciType* return_address = state->type_at(local);
1774         assert(return_address->is_return_address(), "verify: wrong type");
1775         int bci = return_address->as_return_address()->bci();
1776         assert(_successors->length() == GOTO_TARGET, "");
1777         _successors->append(analyzer->block_at(bci, jsrs));
1778         break;
1779       }
1780 
1781       case Bytecodes::_wide:
1782       default:
1783         ShouldNotReachHere();
1784         break;
1785       }
1786     }
1787 
1788     // Set predecessor information
1789     for (int i = 0; i < _successors->length(); i++) {
1790       Block* block = _successors->at(i);
1791       block->predecessors()->append(this);
1792     }
1793   }
1794   return _successors;
1795 }
1796 
1797 // ------------------------------------------------------------------
1798 // ciTypeFlow::Block:compute_exceptions
1799 //
1800 // Compute the exceptional successors and types for this Block.
1801 void ciTypeFlow::Block::compute_exceptions() {
1802   assert(_exceptions == nullptr && _exc_klasses == nullptr, "repeat");
1803 
1804   if (CITraceTypeFlow) {
1805     tty->print(">> Computing exceptions for block ");
1806     print_value_on(tty);
1807     tty->cr();
1808   }
1809 
1810   ciTypeFlow* analyzer = outer();
1811   Arena* arena = analyzer->arena();
1812 
1813   // Any bci in the block will do.
1814   ciExceptionHandlerStream str(analyzer->method(), start());
1815 
1816   // Allocate our growable arrays.
1817   int exc_count = str.count();
1818   _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, nullptr);
1819   _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count,
1820                                                              0, nullptr);
1821 
1822   for ( ; !str.is_done(); str.next()) {
1823     ciExceptionHandler* handler = str.handler();
1824     int bci = handler->handler_bci();
1825     ciInstanceKlass* klass = nullptr;
1826     if (bci == -1) {
1827       // There is no catch all.  It is possible to exit the method.
1828       break;
1829     }
1830     if (handler->is_catch_all()) {
1831       klass = analyzer->env()->Throwable_klass();
1832     } else {
1833       klass = handler->catch_klass();
1834     }
1835     Block* block = analyzer->block_at(bci, _jsrs);
1836     _exceptions->append(block);
1837     block->predecessors()->append(this);
1838     _exc_klasses->append(klass);
1839   }
1840 }
1841 
1842 // ------------------------------------------------------------------
1843 // ciTypeFlow::Block::set_backedge_copy
1844 // Use this only to make a pre-existing public block into a backedge copy.
1845 void ciTypeFlow::Block::set_backedge_copy(bool z) {
1846   assert(z || (z == is_backedge_copy()), "cannot make a backedge copy public");
1847   _backedge_copy = z;
1848 }
1849 
1850 // Analogous to PhaseIdealLoop::is_in_irreducible_loop
1851 bool ciTypeFlow::Block::is_in_irreducible_loop() const {
1852   if (!outer()->has_irreducible_entry()) {
1853     return false; // No irreducible loop in method.
1854   }
1855   Loop* lp = loop(); // Innermost loop containing block.
1856   if (lp == nullptr) {
1857     assert(!is_post_visited(), "must have enclosing loop once post-visited");
1858     return false; // Not yet processed, so we do not know, yet.
1859   }
1860   // Walk all the way up the loop-tree, search for an irreducible loop.
1861   do {
1862     if (lp->is_irreducible()) {
1863       return true; // We are in irreducible loop.
1864     }
1865     if (lp->head()->pre_order() == 0) {
1866       return false; // Found root loop, terminate.
1867     }
1868     lp = lp->parent();
1869   } while (lp != nullptr);
1870   // We have "lp->parent() == nullptr", which happens only for infinite loops,
1871   // where no parent is attached to the loop. We did not find any irreducible
1872   // loop from this block out to lp. Thus lp only has one entry, and no exit
1873   // (it is infinite and reducible). We can always rewrite an infinite loop
1874   // that is nested inside other loops:
1875   // while(condition) { infinite_loop; }
1876   // with an equivalent program where the infinite loop is an outermost loop
1877   // that is not nested in any loop:
1878   // while(condition) { break; } infinite_loop;
1879   // Thus, we can understand lp as an outermost loop, and can terminate and
1880   // conclude: this block is in no irreducible loop.
1881   return false;
1882 }
1883 
1884 // ------------------------------------------------------------------
1885 // ciTypeFlow::Block::is_clonable_exit
1886 //
1887 // At most 2 normal successors, one of which continues looping,
1888 // and all exceptional successors must exit.
1889 bool ciTypeFlow::Block::is_clonable_exit(ciTypeFlow::Loop* lp) {
1890   int normal_cnt  = 0;
1891   int in_loop_cnt = 0;
1892   for (SuccIter iter(this); !iter.done(); iter.next()) {
1893     Block* succ = iter.succ();
1894     if (iter.is_normal_ctrl()) {
1895       if (++normal_cnt > 2) return false;
1896       if (lp->contains(succ->loop())) {
1897         if (++in_loop_cnt > 1) return false;
1898       }
1899     } else {
1900       if (lp->contains(succ->loop())) return false;
1901     }
1902   }
1903   return in_loop_cnt == 1;
1904 }
1905 
1906 // ------------------------------------------------------------------
1907 // ciTypeFlow::Block::looping_succ
1908 //
1909 ciTypeFlow::Block* ciTypeFlow::Block::looping_succ(ciTypeFlow::Loop* lp) {
1910   assert(successors()->length() <= 2, "at most 2 normal successors");
1911   for (SuccIter iter(this); !iter.done(); iter.next()) {
1912     Block* succ = iter.succ();
1913     if (lp->contains(succ->loop())) {
1914       return succ;
1915     }
1916   }
1917   return nullptr;
1918 }
1919 
1920 #ifndef PRODUCT
1921 // ------------------------------------------------------------------
1922 // ciTypeFlow::Block::print_value_on
1923 void ciTypeFlow::Block::print_value_on(outputStream* st) const {
1924   if (has_pre_order()) st->print("#%-2d ", pre_order());
1925   if (has_rpo())       st->print("rpo#%-2d ", rpo());
1926   st->print("[%d - %d)", start(), limit());
1927   if (is_loop_head()) st->print(" lphd");
1928   if (is_in_irreducible_loop()) st->print(" in_irred");
1929   if (is_irreducible_loop_head()) st->print(" irred_head");
1930   if (is_irreducible_loop_secondary_entry()) st->print(" irred_entry");
1931   if (_jsrs->size() > 0) { st->print("/");  _jsrs->print_on(st); }
1932   if (is_backedge_copy())  st->print("/backedge_copy");
1933 }
1934 
1935 // ------------------------------------------------------------------
1936 // ciTypeFlow::Block::print_on
1937 void ciTypeFlow::Block::print_on(outputStream* st) const {
1938   if ((Verbose || WizardMode) && (limit() >= 0)) {
1939     // Don't print 'dummy' blocks (i.e. blocks with limit() '-1')
1940     outer()->method()->print_codes_on(start(), limit(), st);
1941   }
1942   st->print_cr("  ====================================================  ");
1943   st->print ("  ");
1944   print_value_on(st);
1945   st->print(" Stored locals: "); def_locals()->print_on(st, outer()->method()->max_locals()); tty->cr();
1946   if (loop() && loop()->parent() != nullptr) {
1947     st->print(" loops:");
1948     Loop* lp = loop();
1949     do {
1950       st->print(" %d<-%d", lp->head()->pre_order(),lp->tail()->pre_order());
1951       if (lp->is_irreducible()) st->print("(ir)");
1952       lp = lp->parent();
1953     } while (lp->parent() != nullptr);
1954   }
1955   st->cr();
1956   _state->print_on(st);
1957   if (_successors == nullptr) {
1958     st->print_cr("  No successor information");
1959   } else {
1960     int num_successors = _successors->length();
1961     st->print_cr("  Successors : %d", num_successors);
1962     for (int i = 0; i < num_successors; i++) {
1963       Block* successor = _successors->at(i);
1964       st->print("    ");
1965       successor->print_value_on(st);
1966       st->cr();
1967     }
1968   }
1969   if (_predecessors.is_empty()) {
1970     st->print_cr("  No predecessor information");
1971   } else {
1972     int num_predecessors = _predecessors.length();
1973     st->print_cr("  Predecessors : %d", num_predecessors);
1974     for (int i = 0; i < num_predecessors; i++) {
1975       Block* predecessor = _predecessors.at(i);
1976       st->print("    ");
1977       predecessor->print_value_on(st);
1978       st->cr();
1979     }
1980   }
1981   if (_exceptions == nullptr) {
1982     st->print_cr("  No exception information");
1983   } else {
1984     int num_exceptions = _exceptions->length();
1985     st->print_cr("  Exceptions : %d", num_exceptions);
1986     for (int i = 0; i < num_exceptions; i++) {
1987       Block* exc_succ = _exceptions->at(i);
1988       ciInstanceKlass* exc_klass = _exc_klasses->at(i);
1989       st->print("    ");
1990       exc_succ->print_value_on(st);
1991       st->print(" -- ");
1992       exc_klass->name()->print_symbol_on(st);
1993       st->cr();
1994     }
1995   }
1996   if (has_trap()) {
1997     st->print_cr("  Traps on %d with trap index %d", trap_bci(), trap_index());
1998   }
1999   st->print_cr("  ====================================================  ");
2000 }
2001 #endif
2002 
2003 #ifndef PRODUCT
2004 // ------------------------------------------------------------------
2005 // ciTypeFlow::LocalSet::print_on
2006 void ciTypeFlow::LocalSet::print_on(outputStream* st, int limit) const {
2007   st->print("{");
2008   for (int i = 0; i < max; i++) {
2009     if (test(i)) st->print(" %d", i);
2010   }
2011   if (limit > max) {
2012     st->print(" %d..%d ", max, limit);
2013   }
2014   st->print(" }");
2015 }
2016 #endif
2017 
2018 // ciTypeFlow
2019 //
2020 // This is a pass over the bytecodes which computes the following:
2021 //   basic block structure
2022 //   interpreter type-states (a la the verifier)
2023 
2024 // ------------------------------------------------------------------
2025 // ciTypeFlow::ciTypeFlow
2026 ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) {
2027   _env = env;
2028   _method = method;
2029   _has_irreducible_entry = false;
2030   _osr_bci = osr_bci;
2031   _failure_reason = nullptr;
2032   assert(0 <= start_bci() && start_bci() < code_size() , "correct osr_bci argument: 0 <= %d < %d", start_bci(), code_size());
2033   _work_list = nullptr;
2034 
2035   int ciblock_count = _method->get_method_blocks()->num_blocks();
2036   _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, ciblock_count);
2037   for (int i = 0; i < ciblock_count; i++) {
2038     _idx_to_blocklist[i] = nullptr;
2039   }
2040   _block_map = nullptr;  // until all blocks are seen
2041   _jsr_records = nullptr;
2042 }
2043 
2044 // ------------------------------------------------------------------
2045 // ciTypeFlow::work_list_next
2046 //
2047 // Get the next basic block from our work list.
2048 ciTypeFlow::Block* ciTypeFlow::work_list_next() {
2049   assert(!work_list_empty(), "work list must not be empty");
2050   Block* next_block = _work_list;
2051   _work_list = next_block->next();
2052   next_block->set_next(nullptr);
2053   next_block->set_on_work_list(false);
2054   return next_block;
2055 }
2056 
2057 // ------------------------------------------------------------------
2058 // ciTypeFlow::add_to_work_list
2059 //
2060 // Add a basic block to our work list.
2061 // List is sorted by decreasing postorder sort (same as increasing RPO)
2062 void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) {
2063   assert(!block->is_on_work_list(), "must not already be on work list");
2064 
2065   if (CITraceTypeFlow) {
2066     tty->print(">> Adding block ");
2067     block->print_value_on(tty);
2068     tty->print_cr(" to the work list : ");
2069   }
2070 
2071   block->set_on_work_list(true);
2072 
2073   // decreasing post order sort
2074 
2075   Block* prev = nullptr;
2076   Block* current = _work_list;
2077   int po = block->post_order();
2078   while (current != nullptr) {
2079     if (!current->has_post_order() || po > current->post_order())
2080       break;
2081     prev = current;
2082     current = current->next();
2083   }
2084   if (prev == nullptr) {
2085     block->set_next(_work_list);
2086     _work_list = block;
2087   } else {
2088     block->set_next(current);
2089     prev->set_next(block);
2090   }
2091 
2092   if (CITraceTypeFlow) {
2093     tty->cr();
2094   }
2095 }
2096 
2097 // ------------------------------------------------------------------
2098 // ciTypeFlow::block_at
2099 //
2100 // Return the block beginning at bci which has a JsrSet compatible
2101 // with jsrs.
2102 ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2103   // First find the right ciBlock.
2104   if (CITraceTypeFlow) {
2105     tty->print(">> Requesting block for %d/", bci);
2106     jsrs->print_on(tty);
2107     tty->cr();
2108   }
2109 
2110   ciBlock* ciblk = _method->get_method_blocks()->block_containing(bci);
2111   assert(ciblk->start_bci() == bci, "bad ciBlock boundaries");
2112   Block* block = get_block_for(ciblk->index(), jsrs, option);
2113 
2114   assert(block == nullptr? (option == no_create): block->is_backedge_copy() == (option == create_backedge_copy), "create option consistent with result");
2115 
2116   if (CITraceTypeFlow) {
2117     if (block != nullptr) {
2118       tty->print(">> Found block ");
2119       block->print_value_on(tty);
2120       tty->cr();
2121     } else {
2122       tty->print_cr(">> No such block.");
2123     }
2124   }
2125 
2126   return block;
2127 }
2128 
2129 // ------------------------------------------------------------------
2130 // ciTypeFlow::make_jsr_record
2131 //
2132 // Make a JsrRecord for a given (entry, return) pair, if such a record
2133 // does not already exist.
2134 ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address,
2135                                                    int return_address) {
2136   if (_jsr_records == nullptr) {
2137     _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(),
2138                                                            2,
2139                                                            0,
2140                                                            nullptr);
2141   }
2142   JsrRecord* record = nullptr;
2143   int len = _jsr_records->length();
2144   for (int i = 0; i < len; i++) {
2145     JsrRecord* record = _jsr_records->at(i);
2146     if (record->entry_address() == entry_address &&
2147         record->return_address() == return_address) {
2148       return record;
2149     }
2150   }
2151 
2152   record = new (arena()) JsrRecord(entry_address, return_address);
2153   _jsr_records->append(record);
2154   return record;
2155 }
2156 
2157 // ------------------------------------------------------------------
2158 // ciTypeFlow::flow_exceptions
2159 //
2160 // Merge the current state into all exceptional successors at the
2161 // current point in the code.
2162 void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions,
2163                                  GrowableArray<ciInstanceKlass*>* exc_klasses,
2164                                  ciTypeFlow::StateVector* state) {
2165   int len = exceptions->length();
2166   assert(exc_klasses->length() == len, "must have same length");
2167   for (int i = 0; i < len; i++) {
2168     Block* block = exceptions->at(i);
2169     ciInstanceKlass* exception_klass = exc_klasses->at(i);
2170 
2171     if (!exception_klass->is_loaded()) {
2172       // Do not compile any code for unloaded exception types.
2173       // Following compiler passes are responsible for doing this also.
2174       continue;
2175     }
2176 
2177     if (block->meet_exception(exception_klass, state)) {
2178       // Block was modified and has PO.  Add it to the work list.
2179       if (block->has_post_order() &&
2180           !block->is_on_work_list()) {
2181         add_to_work_list(block);
2182       }
2183     }
2184   }
2185 }
2186 
2187 // ------------------------------------------------------------------
2188 // ciTypeFlow::flow_successors
2189 //
2190 // Merge the current state into all successors at the current point
2191 // in the code.
2192 void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors,
2193                                  ciTypeFlow::StateVector* state) {
2194   int len = successors->length();
2195   for (int i = 0; i < len; i++) {
2196     Block* block = successors->at(i);
2197     if (block->meet(state)) {
2198       // Block was modified and has PO.  Add it to the work list.
2199       if (block->has_post_order() &&
2200           !block->is_on_work_list()) {
2201         add_to_work_list(block);
2202       }
2203     }
2204   }
2205 }
2206 
2207 // ------------------------------------------------------------------
2208 // ciTypeFlow::can_trap
2209 //
2210 // Tells if a given instruction is able to generate an exception edge.
2211 bool ciTypeFlow::can_trap(ciBytecodeStream& str) {
2212   // Cf. GenerateOopMap::do_exception_edge.
2213   if (!Bytecodes::can_trap(str.cur_bc()))  return false;
2214 
2215   switch (str.cur_bc()) {
2216     case Bytecodes::_ldc:
2217     case Bytecodes::_ldc_w:
2218     case Bytecodes::_ldc2_w:
2219       return str.is_in_error() || !str.get_constant().is_loaded();
2220 
2221     case Bytecodes::_aload_0:
2222       // These bytecodes can trap for rewriting.  We need to assume that
2223       // they do not throw exceptions to make the monitor analysis work.
2224       return false;
2225 
2226     case Bytecodes::_ireturn:
2227     case Bytecodes::_lreturn:
2228     case Bytecodes::_freturn:
2229     case Bytecodes::_dreturn:
2230     case Bytecodes::_areturn:
2231     case Bytecodes::_return:
2232       // We can assume the monitor stack is empty in this analysis.
2233       return false;
2234 
2235     case Bytecodes::_monitorexit:
2236       // We can assume monitors are matched in this analysis.
2237       return false;
2238 
2239     default:
2240       return true;
2241   }
2242 }
2243 
2244 // ------------------------------------------------------------------
2245 // ciTypeFlow::clone_loop_heads
2246 //
2247 // Clone the loop heads
2248 bool ciTypeFlow::clone_loop_heads(StateVector* temp_vector, JsrSet* temp_set) {
2249   bool rslt = false;
2250   for (PreorderLoops iter(loop_tree_root()); !iter.done(); iter.next()) {
2251     Loop* lp = iter.current();
2252     Block* head = lp->head();
2253     if (lp == loop_tree_root() ||
2254         lp->is_irreducible() ||
2255         !head->is_clonable_exit(lp))
2256       continue;
2257 
2258     // Avoid BoxLock merge.
2259     if (EliminateNestedLocks && head->has_monitorenter())
2260       continue;
2261 
2262     // check not already cloned
2263     if (head->backedge_copy_count() != 0)
2264       continue;
2265 
2266     // Don't clone head of OSR loop to get correct types in start block.
2267     if (is_osr_flow() && head->start() == start_bci())
2268       continue;
2269 
2270     // check _no_ shared head below us
2271     Loop* ch;
2272     for (ch = lp->child(); ch != nullptr && ch->head() != head; ch = ch->sibling());
2273     if (ch != nullptr)
2274       continue;
2275 
2276     // Clone head
2277     Block* new_head = head->looping_succ(lp);
2278     Block* clone = clone_loop_head(lp, temp_vector, temp_set);
2279     // Update lp's info
2280     clone->set_loop(lp);
2281     lp->set_head(new_head);
2282     lp->set_tail(clone);
2283     // And move original head into outer loop
2284     head->set_loop(lp->parent());
2285 
2286     rslt = true;
2287   }
2288   return rslt;
2289 }
2290 
2291 // ------------------------------------------------------------------
2292 // ciTypeFlow::clone_loop_head
2293 //
2294 // Clone lp's head and replace tail's successors with clone.
2295 //
2296 //  |
2297 //  v
2298 // head <-> body
2299 //  |
2300 //  v
2301 // exit
2302 //
2303 // new_head
2304 //
2305 //  |
2306 //  v
2307 // head ----------\
2308 //  |             |
2309 //  |             v
2310 //  |  clone <-> body
2311 //  |    |
2312 //  | /--/
2313 //  | |
2314 //  v v
2315 // exit
2316 //
2317 ciTypeFlow::Block* ciTypeFlow::clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) {
2318   Block* head = lp->head();
2319   Block* tail = lp->tail();
2320   if (CITraceTypeFlow) {
2321     tty->print(">> Requesting clone of loop head "); head->print_value_on(tty);
2322     tty->print("  for predecessor ");                tail->print_value_on(tty);
2323     tty->cr();
2324   }
2325   Block* clone = block_at(head->start(), head->jsrs(), create_backedge_copy);
2326   assert(clone->backedge_copy_count() == 1, "one backedge copy for all back edges");
2327 
2328   assert(!clone->has_pre_order(), "just created");
2329   clone->set_next_pre_order();
2330 
2331   // Accumulate profiled count for all backedges that share this loop's head
2332   int total_count = lp->profiled_count();
2333   for (Loop* lp1 = lp->parent(); lp1 != nullptr; lp1 = lp1->parent()) {
2334     for (Loop* lp2 = lp1; lp2 != nullptr; lp2 = lp2->sibling()) {
2335       if (lp2->head() == head && !lp2->tail()->is_backedge_copy()) {
2336         total_count += lp2->profiled_count();
2337       }
2338     }
2339   }
2340   // Have the most frequent ones branch to the clone instead
2341   int count = 0;
2342   int loops_with_shared_head = 0;
2343   Block* latest_tail = tail;
2344   bool done = false;
2345   for (Loop* lp1 = lp; lp1 != nullptr && !done; lp1 = lp1->parent()) {
2346     for (Loop* lp2 = lp1; lp2 != nullptr && !done; lp2 = lp2->sibling()) {
2347       if (lp2->head() == head && !lp2->tail()->is_backedge_copy()) {
2348         count += lp2->profiled_count();
2349         if (lp2->tail()->post_order() < latest_tail->post_order()) {
2350           latest_tail = lp2->tail();
2351         }
2352         loops_with_shared_head++;
2353         for (SuccIter iter(lp2->tail()); !iter.done(); iter.next()) {
2354           if (iter.succ() == head) {
2355             iter.set_succ(clone);
2356             // Update predecessor information
2357             head->predecessors()->remove(lp2->tail());
2358             clone->predecessors()->append(lp2->tail());
2359           }
2360         }
2361         flow_block(lp2->tail(), temp_vector, temp_set);
2362         if (lp2->head() == lp2->tail()) {
2363           // For self-loops, clone->head becomes clone->clone
2364           flow_block(clone, temp_vector, temp_set);
2365           for (SuccIter iter(clone); !iter.done(); iter.next()) {
2366             if (iter.succ() == lp2->head()) {
2367               iter.set_succ(clone);
2368               // Update predecessor information
2369               lp2->head()->predecessors()->remove(clone);
2370               clone->predecessors()->append(clone);
2371               break;
2372             }
2373           }
2374         }
2375         if (total_count == 0 || count > (total_count * .9)) {
2376           done = true;
2377         }
2378       }
2379     }
2380   }
2381   assert(loops_with_shared_head >= 1, "at least one new");
2382   clone->set_rpo_next(latest_tail->rpo_next());
2383   latest_tail->set_rpo_next(clone);
2384   flow_block(clone, temp_vector, temp_set);
2385 
2386   return clone;
2387 }
2388 
2389 // ------------------------------------------------------------------
2390 // ciTypeFlow::flow_block
2391 //
2392 // Interpret the effects of the bytecodes on the incoming state
2393 // vector of a basic block.  Push the changed state to succeeding
2394 // basic blocks.
2395 void ciTypeFlow::flow_block(ciTypeFlow::Block* block,
2396                             ciTypeFlow::StateVector* state,
2397                             ciTypeFlow::JsrSet* jsrs) {
2398   if (CITraceTypeFlow) {
2399     tty->print("\n>> ANALYZING BLOCK : ");
2400     tty->cr();
2401     block->print_on(tty);
2402   }
2403   assert(block->has_pre_order(), "pre-order is assigned before 1st flow");
2404 
2405   int start = block->start();
2406   int limit = block->limit();
2407   int control = block->control();
2408   if (control != ciBlock::fall_through_bci) {
2409     limit = control;
2410   }
2411 
2412   // Grab the state from the current block.
2413   block->copy_state_into(state);
2414   state->def_locals()->clear();
2415 
2416   GrowableArray<Block*>*           exceptions = block->exceptions();
2417   GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses();
2418   bool has_exceptions = exceptions->length() > 0;
2419 
2420   bool exceptions_used = false;
2421 
2422   ciBytecodeStream str(method());
2423   str.reset_to_bci(start);
2424   Bytecodes::Code code;
2425   while ((code = str.next()) != ciBytecodeStream::EOBC() &&
2426          str.cur_bci() < limit) {
2427     // Check for exceptional control flow from this point.
2428     if (has_exceptions && can_trap(str)) {
2429       flow_exceptions(exceptions, exc_klasses, state);
2430       exceptions_used = true;
2431     }
2432     // Apply the effects of the current bytecode to our state.
2433     bool res = state->apply_one_bytecode(&str);
2434 
2435     // Watch for bailouts.
2436     if (failing())  return;
2437 
2438     if (str.cur_bc() == Bytecodes::_monitorenter) {
2439       block->set_has_monitorenter();
2440     }
2441 
2442     if (res) {
2443 
2444       // We have encountered a trap.  Record it in this block.
2445       block->set_trap(state->trap_bci(), state->trap_index());
2446 
2447       if (CITraceTypeFlow) {
2448         tty->print_cr(">> Found trap");
2449         block->print_on(tty);
2450       }
2451 
2452       // Save set of locals defined in this block
2453       block->def_locals()->add(state->def_locals());
2454 
2455       // Record (no) successors.
2456       block->successors(&str, state, jsrs);
2457 
2458       assert(!has_exceptions || exceptions_used, "Not removing exceptions");
2459 
2460       // Discontinue interpretation of this Block.
2461       return;
2462     }
2463   }
2464 
2465   GrowableArray<Block*>* successors = nullptr;
2466   if (control != ciBlock::fall_through_bci) {
2467     // Check for exceptional control flow from this point.
2468     if (has_exceptions && can_trap(str)) {
2469       flow_exceptions(exceptions, exc_klasses, state);
2470       exceptions_used = true;
2471     }
2472 
2473     // Fix the JsrSet to reflect effect of the bytecode.
2474     block->copy_jsrs_into(jsrs);
2475     jsrs->apply_control(this, &str, state);
2476 
2477     // Find successor edges based on old state and new JsrSet.
2478     successors = block->successors(&str, state, jsrs);
2479 
2480     // Apply the control changes to the state.
2481     state->apply_one_bytecode(&str);
2482   } else {
2483     // Fall through control
2484     successors = block->successors(&str, nullptr, nullptr);
2485   }
2486 
2487   // Save set of locals defined in this block
2488   block->def_locals()->add(state->def_locals());
2489 
2490   // Remove untaken exception paths
2491   if (!exceptions_used)
2492     exceptions->clear();
2493 
2494   // Pass our state to successors.
2495   flow_successors(successors, state);
2496 }
2497 
2498 // ------------------------------------------------------------------
2499 // ciTypeFlow::PreOrderLoops::next
2500 //
2501 // Advance to next loop tree using a preorder, left-to-right traversal.
2502 void ciTypeFlow::PreorderLoops::next() {
2503   assert(!done(), "must not be done.");
2504   if (_current->child() != nullptr) {
2505     _current = _current->child();
2506   } else if (_current->sibling() != nullptr) {
2507     _current = _current->sibling();
2508   } else {
2509     while (_current != _root && _current->sibling() == nullptr) {
2510       _current = _current->parent();
2511     }
2512     if (_current == _root) {
2513       _current = nullptr;
2514       assert(done(), "must be done.");
2515     } else {
2516       assert(_current->sibling() != nullptr, "must be more to do");
2517       _current = _current->sibling();
2518     }
2519   }
2520 }
2521 
2522 // If the tail is a branch to the head, retrieve how many times that path was taken from profiling
2523 int ciTypeFlow::Loop::profiled_count() {
2524   if (_profiled_count >= 0) {
2525     return _profiled_count;
2526   }
2527   ciMethodData* methodData = outer()->method()->method_data();
2528   if (!methodData->is_mature()) {
2529     _profiled_count = 0;
2530     return 0;
2531   }
2532   ciTypeFlow::Block* tail = this->tail();
2533   if (tail->control() == -1 || tail->has_trap()) {
2534     _profiled_count = 0;
2535     return 0;
2536   }
2537 
2538   ciProfileData* data = methodData->bci_to_data(tail->control());
2539 
2540   if (data == nullptr || !data->is_JumpData()) {
2541     _profiled_count = 0;
2542     return 0;
2543   }
2544 
2545   ciBytecodeStream iter(outer()->method());
2546   iter.reset_to_bci(tail->control());
2547 
2548   bool is_an_if = false;
2549   bool wide = false;
2550   Bytecodes::Code bc = iter.next();
2551   switch (bc) {
2552     case Bytecodes::_ifeq:
2553     case Bytecodes::_ifne:
2554     case Bytecodes::_iflt:
2555     case Bytecodes::_ifge:
2556     case Bytecodes::_ifgt:
2557     case Bytecodes::_ifle:
2558     case Bytecodes::_if_icmpeq:
2559     case Bytecodes::_if_icmpne:
2560     case Bytecodes::_if_icmplt:
2561     case Bytecodes::_if_icmpge:
2562     case Bytecodes::_if_icmpgt:
2563     case Bytecodes::_if_icmple:
2564     case Bytecodes::_if_acmpeq:
2565     case Bytecodes::_if_acmpne:
2566     case Bytecodes::_ifnull:
2567     case Bytecodes::_ifnonnull:
2568       is_an_if = true;
2569       break;
2570     case Bytecodes::_goto_w:
2571     case Bytecodes::_jsr_w:
2572       wide = true;
2573       break;
2574     case Bytecodes::_goto:
2575     case Bytecodes::_jsr:
2576       break;
2577     default:
2578       fatal(" invalid bytecode: %s", Bytecodes::name(iter.cur_bc()));
2579   }
2580 
2581   GrowableArray<ciTypeFlow::Block*>* succs = tail->successors();
2582 
2583   if (!is_an_if) {
2584     assert(((wide ? iter.get_far_dest() : iter.get_dest()) == head()->start()) == (succs->at(ciTypeFlow::GOTO_TARGET) == head()), "branch should lead to loop head");
2585     if (succs->at(ciTypeFlow::GOTO_TARGET) == head()) {
2586       _profiled_count = outer()->method()->scale_count(data->as_JumpData()->taken());
2587       return _profiled_count;
2588     }
2589   } else {
2590     assert((iter.get_dest() == head()->start()) == (succs->at(ciTypeFlow::IF_TAKEN) == head()), "bytecode and CFG not consistent");
2591     assert((tail->limit() == head()->start()) == (succs->at(ciTypeFlow::IF_NOT_TAKEN) == head()), "bytecode and CFG not consistent");
2592     if (succs->at(ciTypeFlow::IF_TAKEN) == head()) {
2593       _profiled_count = outer()->method()->scale_count(data->as_JumpData()->taken());
2594       return _profiled_count;
2595     } else if (succs->at(ciTypeFlow::IF_NOT_TAKEN) == head()) {
2596       _profiled_count = outer()->method()->scale_count(data->as_BranchData()->not_taken());
2597       return _profiled_count;
2598     }
2599   }
2600 
2601   _profiled_count = 0;
2602   return _profiled_count;
2603 }
2604 
2605 bool ciTypeFlow::Loop::at_insertion_point(Loop* lp, Loop* current) {
2606   int lp_pre_order = lp->head()->pre_order();
2607   if (current->head()->pre_order() < lp_pre_order) {
2608     return true;
2609   } else if (current->head()->pre_order() > lp_pre_order) {
2610     return false;
2611   }
2612   // In the case of a shared head, make the most frequent head/tail (as reported by profiling) the inner loop
2613   if (current->head() == lp->head()) {
2614     int lp_count = lp->profiled_count();
2615     int current_count = current->profiled_count();
2616     if (current_count < lp_count) {
2617       return true;
2618     } else if (current_count > lp_count) {
2619       return false;
2620     }
2621   }
2622   if (current->tail()->pre_order() > lp->tail()->pre_order()) {
2623     return true;
2624   }
2625   return false;
2626 }
2627 
2628 // ------------------------------------------------------------------
2629 // ciTypeFlow::Loop::sorted_merge
2630 //
2631 // Merge the branch lp into this branch, sorting on the loop head
2632 // pre_orders. Returns the leaf of the merged branch.
2633 // Child and sibling pointers will be setup later.
2634 // Sort is (looking from leaf towards the root)
2635 //  descending on primary key: loop head's pre_order, and
2636 //  ascending  on secondary key: loop tail's pre_order.
2637 ciTypeFlow::Loop* ciTypeFlow::Loop::sorted_merge(Loop* lp) {
2638   Loop* leaf = this;
2639   Loop* prev = nullptr;
2640   Loop* current = leaf;
2641   while (lp != nullptr) {
2642     int lp_pre_order = lp->head()->pre_order();
2643     // Find insertion point for "lp"
2644     while (current != nullptr) {
2645       if (current == lp) {
2646         return leaf; // Already in list
2647       }
2648       if (at_insertion_point(lp, current)) {
2649         break;
2650       }
2651       prev = current;
2652       current = current->parent();
2653     }
2654     Loop* next_lp = lp->parent(); // Save future list of items to insert
2655     // Insert lp before current
2656     lp->set_parent(current);
2657     if (prev != nullptr) {
2658       prev->set_parent(lp);
2659     } else {
2660       leaf = lp;
2661     }
2662     prev = lp;     // Inserted item is new prev[ious]
2663     lp = next_lp;  // Next item to insert
2664   }
2665   return leaf;
2666 }
2667 
2668 // ------------------------------------------------------------------
2669 // ciTypeFlow::build_loop_tree
2670 //
2671 // Incrementally build loop tree.
2672 void ciTypeFlow::build_loop_tree(Block* blk) {
2673   assert(!blk->is_post_visited(), "precondition");
2674   Loop* innermost = nullptr; // merge of loop tree branches over all successors
2675 
2676   for (SuccIter iter(blk); !iter.done(); iter.next()) {
2677     Loop*  lp   = nullptr;
2678     Block* succ = iter.succ();
2679     if (!succ->is_post_visited()) {
2680       // Found backedge since predecessor post visited, but successor is not
2681       assert(succ->pre_order() <= blk->pre_order(), "should be backedge");
2682 
2683       // Create a LoopNode to mark this loop.
2684       lp = new (arena()) Loop(succ, blk);
2685       if (succ->loop() == nullptr)
2686         succ->set_loop(lp);
2687       // succ->loop will be updated to innermost loop on a later call, when blk==succ
2688 
2689     } else {  // Nested loop
2690       lp = succ->loop();
2691 
2692       // If succ is loop head, find outer loop.
2693       while (lp != nullptr && lp->head() == succ) {
2694         lp = lp->parent();
2695       }
2696       if (lp == nullptr) {
2697         // Infinite loop, it's parent is the root
2698         lp = loop_tree_root();
2699       }
2700     }
2701 
2702     // Check for irreducible loop.
2703     // Successor has already been visited. If the successor's loop head
2704     // has already been post-visited, then this is another entry into the loop.
2705     while (lp->head()->is_post_visited() && lp != loop_tree_root()) {
2706       _has_irreducible_entry = true;
2707       lp->set_irreducible(succ);
2708       if (!succ->is_on_work_list()) {
2709         // Assume irreducible entries need more data flow
2710         add_to_work_list(succ);
2711       }
2712       Loop* plp = lp->parent();
2713       if (plp == nullptr) {
2714         // This only happens for some irreducible cases.  The parent
2715         // will be updated during a later pass.
2716         break;
2717       }
2718       lp = plp;
2719     }
2720 
2721     // Merge loop tree branch for all successors.
2722     innermost = innermost == nullptr ? lp : innermost->sorted_merge(lp);
2723 
2724   } // end loop
2725 
2726   if (innermost == nullptr) {
2727     assert(blk->successors()->length() == 0, "CFG exit");
2728     blk->set_loop(loop_tree_root());
2729   } else if (innermost->head() == blk) {
2730     // If loop header, complete the tree pointers
2731     if (blk->loop() != innermost) {
2732 #ifdef ASSERT
2733       assert(blk->loop()->head() == innermost->head(), "same head");
2734       Loop* dl;
2735       for (dl = innermost; dl != nullptr && dl != blk->loop(); dl = dl->parent());
2736       assert(dl == blk->loop(), "blk->loop() already in innermost list");
2737 #endif
2738       blk->set_loop(innermost);
2739     }
2740     innermost->def_locals()->add(blk->def_locals());
2741     Loop* l = innermost;
2742     Loop* p = l->parent();
2743     while (p && l->head() == blk) {
2744       l->set_sibling(p->child());  // Put self on parents 'next child'
2745       p->set_child(l);             // Make self the first child of parent
2746       p->def_locals()->add(l->def_locals());
2747       l = p;                       // Walk up the parent chain
2748       p = l->parent();
2749     }
2750   } else {
2751     blk->set_loop(innermost);
2752     innermost->def_locals()->add(blk->def_locals());
2753   }
2754 }
2755 
2756 // ------------------------------------------------------------------
2757 // ciTypeFlow::Loop::contains
2758 //
2759 // Returns true if lp is nested loop.
2760 bool ciTypeFlow::Loop::contains(ciTypeFlow::Loop* lp) const {
2761   assert(lp != nullptr, "");
2762   if (this == lp || head() == lp->head()) return true;
2763   int depth1 = depth();
2764   int depth2 = lp->depth();
2765   if (depth1 > depth2)
2766     return false;
2767   while (depth1 < depth2) {
2768     depth2--;
2769     lp = lp->parent();
2770   }
2771   return this == lp;
2772 }
2773 
2774 // ------------------------------------------------------------------
2775 // ciTypeFlow::Loop::depth
2776 //
2777 // Loop depth
2778 int ciTypeFlow::Loop::depth() const {
2779   int dp = 0;
2780   for (Loop* lp = this->parent(); lp != nullptr; lp = lp->parent())
2781     dp++;
2782   return dp;
2783 }
2784 
2785 #ifndef PRODUCT
2786 // ------------------------------------------------------------------
2787 // ciTypeFlow::Loop::print
2788 void ciTypeFlow::Loop::print(outputStream* st, int indent) const {
2789   for (int i = 0; i < indent; i++) st->print(" ");
2790   st->print("%d<-%d %s",
2791             is_root() ? 0 : this->head()->pre_order(),
2792             is_root() ? 0 : this->tail()->pre_order(),
2793             is_irreducible()?" irr":"");
2794   st->print(" defs: ");
2795   def_locals()->print_on(st, _head->outer()->method()->max_locals());
2796   st->cr();
2797   for (Loop* ch = child(); ch != nullptr; ch = ch->sibling())
2798     ch->print(st, indent+2);
2799 }
2800 #endif
2801 
2802 // ------------------------------------------------------------------
2803 // ciTypeFlow::df_flow_types
2804 //
2805 // Perform the depth first type flow analysis. Helper for flow_types.
2806 void ciTypeFlow::df_flow_types(Block* start,
2807                                bool do_flow,
2808                                StateVector* temp_vector,
2809                                JsrSet* temp_set) {
2810   int dft_len = 100;
2811   GrowableArray<Block*> stk(dft_len);
2812 
2813   ciBlock* dummy = _method->get_method_blocks()->make_dummy_block();
2814   JsrSet* root_set = new JsrSet(0);
2815   Block* root_head = new (arena()) Block(this, dummy, root_set);
2816   Block* root_tail = new (arena()) Block(this, dummy, root_set);
2817   root_head->set_pre_order(0);
2818   root_head->set_post_order(0);
2819   root_tail->set_pre_order(max_jint);
2820   root_tail->set_post_order(max_jint);
2821   set_loop_tree_root(new (arena()) Loop(root_head, root_tail));
2822 
2823   stk.push(start);
2824 
2825   _next_pre_order = 0;  // initialize pre_order counter
2826   _rpo_list = nullptr;
2827   int next_po = 0;      // initialize post_order counter
2828 
2829   // Compute RPO and the control flow graph
2830   int size;
2831   while ((size = stk.length()) > 0) {
2832     Block* blk = stk.top(); // Leave node on stack
2833     if (!blk->is_visited()) {
2834       // forward arc in graph
2835       assert (!blk->has_pre_order(), "");
2836       blk->set_next_pre_order();
2837 
2838       if (_next_pre_order >= (int)Compile::current()->max_node_limit() / 2) {
2839         // Too many basic blocks.  Bail out.
2840         // This can happen when try/finally constructs are nested to depth N,
2841         // and there is O(2**N) cloning of jsr bodies.  See bug 4697245!
2842         // "MaxNodeLimit / 2" is used because probably the parser will
2843         // generate at least twice that many nodes and bail out.
2844         record_failure("too many basic blocks");
2845         return;
2846       }
2847       if (do_flow) {
2848         flow_block(blk, temp_vector, temp_set);
2849         if (failing()) return; // Watch for bailouts.
2850       }
2851     } else if (!blk->is_post_visited()) {
2852       // cross or back arc
2853       for (SuccIter iter(blk); !iter.done(); iter.next()) {
2854         Block* succ = iter.succ();
2855         if (!succ->is_visited()) {
2856           stk.push(succ);
2857         }
2858       }
2859       if (stk.length() == size) {
2860         // There were no additional children, post visit node now
2861         stk.pop(); // Remove node from stack
2862 
2863         build_loop_tree(blk);
2864         blk->set_post_order(next_po++);   // Assign post order
2865         prepend_to_rpo_list(blk);
2866         assert(blk->is_post_visited(), "");
2867 
2868         if (blk->is_loop_head() && !blk->is_on_work_list()) {
2869           // Assume loop heads need more data flow
2870           add_to_work_list(blk);
2871         }
2872       }
2873     } else {
2874       stk.pop(); // Remove post-visited node from stack
2875     }
2876   }
2877 }
2878 
2879 // ------------------------------------------------------------------
2880 // ciTypeFlow::flow_types
2881 //
2882 // Perform the type flow analysis, creating and cloning Blocks as
2883 // necessary.
2884 void ciTypeFlow::flow_types() {
2885   ResourceMark rm;
2886   StateVector* temp_vector = new StateVector(this);
2887   JsrSet* temp_set = new JsrSet(4);
2888 
2889   // Create the method entry block.
2890   Block* start = block_at(start_bci(), temp_set);
2891 
2892   // Load the initial state into it.
2893   const StateVector* start_state = get_start_state();
2894   if (failing())  return;
2895   start->meet(start_state);
2896 
2897   // Depth first visit
2898   df_flow_types(start, true /*do flow*/, temp_vector, temp_set);
2899 
2900   if (failing())  return;
2901   assert(_rpo_list == start, "must be start");
2902 
2903   // Any loops found?
2904   if (loop_tree_root()->child() != nullptr &&
2905       env()->comp_level() >= CompLevel_full_optimization) {
2906       // Loop optimizations are not performed on Tier1 compiles.
2907 
2908     bool changed = clone_loop_heads(temp_vector, temp_set);
2909 
2910     // If some loop heads were cloned, recompute postorder and loop tree
2911     if (changed) {
2912       loop_tree_root()->set_child(nullptr);
2913       for (Block* blk = _rpo_list; blk != nullptr;) {
2914         Block* next = blk->rpo_next();
2915         blk->df_init();
2916         blk = next;
2917       }
2918       df_flow_types(start, false /*no flow*/, temp_vector, temp_set);
2919     }
2920   }
2921 
2922   if (CITraceTypeFlow) {
2923     tty->print_cr("\nLoop tree");
2924     loop_tree_root()->print();
2925   }
2926 
2927   // Continue flow analysis until fixed point reached
2928 
2929   debug_only(int max_block = _next_pre_order;)
2930 
2931   while (!work_list_empty()) {
2932     Block* blk = work_list_next();
2933     assert (blk->has_post_order(), "post order assigned above");
2934 
2935     flow_block(blk, temp_vector, temp_set);
2936 
2937     assert (max_block == _next_pre_order, "no new blocks");
2938     assert (!failing(), "no more bailouts");
2939   }
2940 }
2941 
2942 // ------------------------------------------------------------------
2943 // ciTypeFlow::map_blocks
2944 //
2945 // Create the block map, which indexes blocks in reverse post-order.
2946 void ciTypeFlow::map_blocks() {
2947   assert(_block_map == nullptr, "single initialization");
2948   int block_ct = _next_pre_order;
2949   _block_map = NEW_ARENA_ARRAY(arena(), Block*, block_ct);
2950   assert(block_ct == block_count(), "");
2951 
2952   Block* blk = _rpo_list;
2953   for (int m = 0; m < block_ct; m++) {
2954     int rpo = blk->rpo();
2955     assert(rpo == m, "should be sequential");
2956     _block_map[rpo] = blk;
2957     blk = blk->rpo_next();
2958   }
2959   assert(blk == nullptr, "should be done");
2960 
2961   for (int j = 0; j < block_ct; j++) {
2962     assert(_block_map[j] != nullptr, "must not drop any blocks");
2963     Block* block = _block_map[j];
2964     // Remove dead blocks from successor lists:
2965     for (int e = 0; e <= 1; e++) {
2966       GrowableArray<Block*>* l = e? block->exceptions(): block->successors();
2967       for (int k = 0; k < l->length(); k++) {
2968         Block* s = l->at(k);
2969         if (!s->has_post_order()) {
2970           if (CITraceTypeFlow) {
2971             tty->print("Removing dead %s successor of #%d: ", (e? "exceptional":  "normal"), block->pre_order());
2972             s->print_value_on(tty);
2973             tty->cr();
2974           }
2975           l->remove(s);
2976           --k;
2977         }
2978       }
2979     }
2980   }
2981 }
2982 
2983 // ------------------------------------------------------------------
2984 // ciTypeFlow::get_block_for
2985 //
2986 // Find a block with this ciBlock which has a compatible JsrSet.
2987 // If no such block exists, create it, unless the option is no_create.
2988 // If the option is create_backedge_copy, always create a fresh backedge copy.
2989 ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2990   Arena* a = arena();
2991   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
2992   if (blocks == nullptr) {
2993     // Query only?
2994     if (option == no_create)  return nullptr;
2995 
2996     // Allocate the growable array.
2997     blocks = new (a) GrowableArray<Block*>(a, 4, 0, nullptr);
2998     _idx_to_blocklist[ciBlockIndex] = blocks;
2999   }
3000 
3001   if (option != create_backedge_copy) {
3002     int len = blocks->length();
3003     for (int i = 0; i < len; i++) {
3004       Block* block = blocks->at(i);
3005       if (!block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
3006         return block;
3007       }
3008     }
3009   }
3010 
3011   // Query only?
3012   if (option == no_create)  return nullptr;
3013 
3014   // We did not find a compatible block.  Create one.
3015   Block* new_block = new (a) Block(this, _method->get_method_blocks()->block(ciBlockIndex), jsrs);
3016   if (option == create_backedge_copy)  new_block->set_backedge_copy(true);
3017   blocks->append(new_block);
3018   return new_block;
3019 }
3020 
3021 // ------------------------------------------------------------------
3022 // ciTypeFlow::backedge_copy_count
3023 //
3024 int ciTypeFlow::backedge_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const {
3025   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
3026 
3027   if (blocks == nullptr) {
3028     return 0;
3029   }
3030 
3031   int count = 0;
3032   int len = blocks->length();
3033   for (int i = 0; i < len; i++) {
3034     Block* block = blocks->at(i);
3035     if (block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
3036       count++;
3037     }
3038   }
3039 
3040   return count;
3041 }
3042 
3043 // ------------------------------------------------------------------
3044 // ciTypeFlow::do_flow
3045 //
3046 // Perform type inference flow analysis.
3047 void ciTypeFlow::do_flow() {
3048   if (CITraceTypeFlow) {
3049     tty->print_cr("\nPerforming flow analysis on method");
3050     method()->print();
3051     if (is_osr_flow())  tty->print(" at OSR bci %d", start_bci());
3052     tty->cr();
3053     method()->print_codes();
3054   }
3055   if (CITraceTypeFlow) {
3056     tty->print_cr("Initial CI Blocks");
3057     print_on(tty);
3058   }
3059   flow_types();
3060   // Watch for bailouts.
3061   if (failing()) {
3062     return;
3063   }
3064 
3065   map_blocks();
3066 
3067   if (CIPrintTypeFlow || CITraceTypeFlow) {
3068     rpo_print_on(tty);
3069   }
3070 }
3071 
3072 // ------------------------------------------------------------------
3073 // ciTypeFlow::is_dominated_by
3074 //
3075 // Determine if the instruction at bci is dominated by the instruction at dom_bci.
3076 bool ciTypeFlow::is_dominated_by(int bci, int dom_bci) {
3077   assert(!method()->has_jsrs(), "jsrs are not supported");
3078 
3079   ResourceMark rm;
3080   JsrSet* jsrs = new ciTypeFlow::JsrSet();
3081   int        index = _method->get_method_blocks()->block_containing(bci)->index();
3082   int    dom_index = _method->get_method_blocks()->block_containing(dom_bci)->index();
3083   Block*     block = get_block_for(index, jsrs, ciTypeFlow::no_create);
3084   Block* dom_block = get_block_for(dom_index, jsrs, ciTypeFlow::no_create);
3085 
3086   // Start block dominates all other blocks
3087   if (start_block()->rpo() == dom_block->rpo()) {
3088     return true;
3089   }
3090 
3091   // Dominated[i] is true if block i is dominated by dom_block
3092   int num_blocks = block_count();
3093   bool* dominated = NEW_RESOURCE_ARRAY(bool, num_blocks);
3094   for (int i = 0; i < num_blocks; ++i) {
3095     dominated[i] = true;
3096   }
3097   dominated[start_block()->rpo()] = false;
3098 
3099   // Iterative dominator algorithm
3100   bool changed = true;
3101   while (changed) {
3102     changed = false;
3103     // Use reverse postorder iteration
3104     for (Block* blk = _rpo_list; blk != nullptr; blk = blk->rpo_next()) {
3105       if (blk->is_start()) {
3106         // Ignore start block
3107         continue;
3108       }
3109       // The block is dominated if it is the dominating block
3110       // itself or if all predecessors are dominated.
3111       int index = blk->rpo();
3112       bool dom = (index == dom_block->rpo());
3113       if (!dom) {
3114         // Check if all predecessors are dominated
3115         dom = true;
3116         for (int i = 0; i < blk->predecessors()->length(); ++i) {
3117           Block* pred = blk->predecessors()->at(i);
3118           if (!dominated[pred->rpo()]) {
3119             dom = false;
3120             break;
3121           }
3122         }
3123       }
3124       // Update dominator information
3125       if (dominated[index] != dom) {
3126         changed = true;
3127         dominated[index] = dom;
3128       }
3129     }
3130   }
3131   // block dominated by dom_block?
3132   return dominated[block->rpo()];
3133 }
3134 
3135 // ------------------------------------------------------------------
3136 // ciTypeFlow::record_failure()
3137 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
3138 // This is required because there is not a 1-1 relation between the ciEnv and
3139 // the TypeFlow passes within a compilation task.  For example, if the compiler
3140 // is considering inlining a method, it will request a TypeFlow.  If that fails,
3141 // the compilation as a whole may continue without the inlining.  Some TypeFlow
3142 // requests are not optional; if they fail the requestor is responsible for
3143 // copying the failure reason up to the ciEnv.  (See Parse::Parse.)
3144 void ciTypeFlow::record_failure(const char* reason) {
3145   if (env()->log() != nullptr) {
3146     env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
3147   }
3148   if (_failure_reason == nullptr) {
3149     // Record the first failure reason.
3150     _failure_reason = reason;
3151   }
3152 }
3153 





3154 #ifndef PRODUCT
3155 void ciTypeFlow::print() const       { print_on(tty); }
3156 
3157 // ------------------------------------------------------------------
3158 // ciTypeFlow::print_on
3159 void ciTypeFlow::print_on(outputStream* st) const {
3160   // Walk through CI blocks
3161   st->print_cr("********************************************************");
3162   st->print   ("TypeFlow for ");
3163   method()->name()->print_symbol_on(st);
3164   int limit_bci = code_size();
3165   st->print_cr("  %d bytes", limit_bci);
3166   ciMethodBlocks* mblks = _method->get_method_blocks();
3167   ciBlock* current = nullptr;
3168   for (int bci = 0; bci < limit_bci; bci++) {
3169     ciBlock* blk = mblks->block_containing(bci);
3170     if (blk != nullptr && blk != current) {
3171       current = blk;
3172       current->print_on(st);
3173 
3174       GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()];
3175       int num_blocks = (blocks == nullptr) ? 0 : blocks->length();
3176 
3177       if (num_blocks == 0) {
3178         st->print_cr("  No Blocks");
3179       } else {
3180         for (int i = 0; i < num_blocks; i++) {
3181           Block* block = blocks->at(i);
3182           block->print_on(st);
3183         }
3184       }
3185       st->print_cr("--------------------------------------------------------");
3186       st->cr();
3187     }
3188   }
3189   st->print_cr("********************************************************");
3190   st->cr();
3191 }
3192 
3193 void ciTypeFlow::rpo_print_on(outputStream* st) const {
3194   st->print_cr("********************************************************");
3195   st->print   ("TypeFlow for ");
3196   method()->name()->print_symbol_on(st);
3197   int limit_bci = code_size();
3198   st->print_cr("  %d bytes", limit_bci);
3199   for (Block* blk = _rpo_list; blk != nullptr; blk = blk->rpo_next()) {
3200     blk->print_on(st);
3201     st->print_cr("--------------------------------------------------------");
3202     st->cr();
3203   }
3204   st->print_cr("********************************************************");
3205   st->cr();
3206 }
3207 #endif
--- EOF ---