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