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