1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)ciTypeFlow.cpp       1.47 07/09/28 10:23:20 JVM"
   3 #endif
   4 /*
   5  * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 #include "incls/_precompiled.incl"
  29 #include "incls/_ciTypeFlow.cpp.incl"
  30 
  31 // ciTypeFlow::JsrSet
  32 //
  33 // A JsrSet represents some set of JsrRecords.  This class
  34 // is used to record a set of all jsr routines which we permit
  35 // execution to return (ret) from.
  36 //
  37 // During abstract interpretation, JsrSets are used to determine
  38 // whether two paths which reach a given block are unique, and
  39 // should be cloned apart, or are compatible, and should merge
  40 // together.
  41 
  42 // ------------------------------------------------------------------
  43 // ciTypeFlow::JsrSet::JsrSet
  44 ciTypeFlow::JsrSet::JsrSet(Arena* arena, int default_len) {
  45   if (arena != NULL) {
  46     // Allocate growable array in Arena.
  47     _set = new (arena) GrowableArray<JsrRecord*>(arena, default_len, 0, NULL);
  48   } else {
  49     // Allocate growable array in current ResourceArea.
  50     _set = new GrowableArray<JsrRecord*>(4, 0, NULL, false);
  51   }
  52 }
  53 
  54 // ------------------------------------------------------------------
  55 // ciTypeFlow::JsrSet::copy_into
  56 void ciTypeFlow::JsrSet::copy_into(JsrSet* jsrs) {
  57   int len = size();
  58   jsrs->_set->clear();
  59   for (int i = 0; i < len; i++) {
  60     jsrs->_set->append(_set->at(i));
  61   }
  62 }
  63 
  64 // ------------------------------------------------------------------
  65 // ciTypeFlow::JsrSet::is_compatible_with
  66 //
  67 // !!!! MISGIVINGS ABOUT THIS... disregard
  68 //
  69 // Is this JsrSet compatible with some other JsrSet?
  70 //
  71 // In set-theoretic terms, a JsrSet can be viewed as a partial function
  72 // from entry addresses to return addresses.  Two JsrSets A and B are
  73 // compatible iff
  74 //
  75 //   For any x,
  76 //   A(x) defined and B(x) defined implies A(x) == B(x)
  77 //
  78 // Less formally, two JsrSets are compatible when they have identical
  79 // return addresses for any entry addresses they share in common.
  80 bool ciTypeFlow::JsrSet::is_compatible_with(JsrSet* other) {
  81   // Walk through both sets in parallel.  If the same entry address
  82   // appears in both sets, then the return address must match for
  83   // the sets to be compatible.
  84   int size1 = size();
  85   int size2 = other->size();
  86   
  87   // Special case.  If nothing is on the jsr stack, then there can
  88   // be no ret.
  89   if (size2 == 0) {
  90     return true;
  91   } else if (size1 != size2) {
  92     return false;
  93   } else {
  94     for (int i = 0; i < size1; i++) {
  95       JsrRecord* record1 = record_at(i);
  96       JsrRecord* record2 = other->record_at(i);
  97       if (record1->entry_address() != record2->entry_address() ||
  98           record1->return_address() != record2->return_address()) {
  99         return false;
 100       }
 101     }
 102     return true;
 103   }
 104 
 105 #if 0
 106   int pos1 = 0;
 107   int pos2 = 0;
 108   int size1 = size();
 109   int size2 = other->size();
 110   while (pos1 < size1 && pos2 < size2) {
 111     JsrRecord* record1 = record_at(pos1);
 112     JsrRecord* record2 = other->record_at(pos2);
 113     int entry1 = record1->entry_address();
 114     int entry2 = record2->entry_address();
 115     if (entry1 < entry2) {
 116       pos1++;
 117     } else if (entry1 > entry2) {
 118       pos2++;
 119     } else {
 120       if (record1->return_address() == record2->return_address()) {
 121         pos1++;
 122         pos2++;
 123       } else {
 124         // These two JsrSets are incompatible.
 125         return false;
 126       }
 127     }
 128   }
 129   // The two JsrSets agree.
 130   return true;
 131 #endif
 132 }
 133 
 134 // ------------------------------------------------------------------
 135 // ciTypeFlow::JsrSet::insert_jsr_record
 136 //
 137 // Insert the given JsrRecord into the JsrSet, maintaining the order
 138 // of the set and replacing any element with the same entry address.
 139 void ciTypeFlow::JsrSet::insert_jsr_record(JsrRecord* record) {
 140   int len = size();
 141   int entry = record->entry_address();
 142   int pos = 0;
 143   for ( ; pos < len; pos++) {
 144     JsrRecord* current = record_at(pos);
 145     if (entry == current->entry_address()) {
 146       // Stomp over this entry.
 147       _set->at_put(pos, record);
 148       assert(size() == len, "must be same size");
 149       return;
 150     } else if (entry < current->entry_address()) {
 151       break;
 152     }
 153   }   
 154 
 155   // Insert the record into the list.
 156   JsrRecord* swap = record;
 157   JsrRecord* temp = NULL;
 158   for ( ; pos < len; pos++) {
 159     temp = _set->at(pos);
 160     _set->at_put(pos, swap);
 161     swap = temp;
 162   }
 163   _set->append(swap);
 164   assert(size() == len+1, "must be larger");
 165 }
 166 
 167 // ------------------------------------------------------------------
 168 // ciTypeFlow::JsrSet::remove_jsr_record
 169 //
 170 // Remove the JsrRecord with the given return address from the JsrSet.
 171 void ciTypeFlow::JsrSet::remove_jsr_record(int return_address) {
 172   int len = size();
 173   for (int i = 0; i < len; i++) {
 174     if (record_at(i)->return_address() == return_address) {
 175       // We have found the proper entry.  Remove it from the
 176       // JsrSet and exit.
 177       for (int j = i+1; j < len ; j++) {
 178         _set->at_put(j-1, _set->at(j));
 179       }
 180       _set->trunc_to(len-1);
 181       assert(size() == len-1, "must be smaller");
 182       return;
 183     }
 184   }
 185   assert(false, "verify: returning from invalid subroutine");
 186 }
 187 
 188 // ------------------------------------------------------------------
 189 // ciTypeFlow::JsrSet::apply_control
 190 //
 191 // Apply the effect of a control-flow bytecode on the JsrSet.  The
 192 // only bytecodes that modify the JsrSet are jsr and ret.
 193 void ciTypeFlow::JsrSet::apply_control(ciTypeFlow* analyzer,
 194                                        ciBytecodeStream* str,
 195                                        ciTypeFlow::StateVector* state) {
 196   Bytecodes::Code code = str->cur_bc();
 197   if (code == Bytecodes::_jsr) {
 198     JsrRecord* record =
 199       analyzer->make_jsr_record(str->get_dest(), str->next_bci());
 200     insert_jsr_record(record);
 201   } else if (code == Bytecodes::_jsr_w) {
 202     JsrRecord* record =
 203       analyzer->make_jsr_record(str->get_far_dest(), str->next_bci());
 204     insert_jsr_record(record);
 205   } else if (code == Bytecodes::_ret) {
 206     Cell local = state->local(str->get_index());
 207     ciType* return_address = state->type_at(local);
 208     assert(return_address->is_return_address(), "verify: wrong type");
 209     if (size() == 0) {
 210       // Ret-state underflow:  Hit a ret w/o any previous jsrs.  Bail out.
 211       // This can happen when a loop is inside a finally clause (4614060).
 212       analyzer->record_failure("OSR in finally clause");
 213       return;
 214     }
 215     remove_jsr_record(return_address->as_return_address()->bci());
 216   }
 217 }
 218 
 219 #ifndef PRODUCT
 220 // ------------------------------------------------------------------
 221 // ciTypeFlow::JsrSet::print_on
 222 void ciTypeFlow::JsrSet::print_on(outputStream* st) const {
 223   st->print("{ ");
 224   int num_elements = size();
 225   if (num_elements > 0) {
 226     int i = 0;
 227     for( ; i < num_elements - 1; i++) {
 228       _set->at(i)->print_on(st);
 229       st->print(", ");
 230     }
 231     _set->at(i)->print_on(st);
 232     st->print(" ");
 233   }
 234   st->print("}");
 235 }
 236 #endif
 237 
 238 // ciTypeFlow::StateVector
 239 //
 240 // A StateVector summarizes the type information at some point in
 241 // the program.
 242 
 243 // ------------------------------------------------------------------
 244 // ciTypeFlow::StateVector::type_meet
 245 //
 246 // Meet two types.
 247 //
 248 // The semi-lattice of types use by this analysis are modeled on those
 249 // of the verifier.  The lattice is as follows:
 250 //
 251 //        top_type() >= all non-extremal types >= bottom_type
 252 //                             and
 253 //   Every primitive type is comparable only with itself.  The meet of
 254 //   reference types is determined by their kind: instance class,
 255 //   interface, or array class.  The meet of two types of the same
 256 //   kind is their least common ancestor.  The meet of two types of
 257 //   different kinds is always java.lang.Object.
 258 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
 259   assert(t1 != t2, "checked in caller");
 260   if (t1->equals(top_type())) {
 261     return t2;
 262   } else if (t2->equals(top_type())) {
 263     return t1;
 264   } else if (t1->is_primitive_type() || t2->is_primitive_type()) {
 265     // Special case null_type.  null_type meet any reference type T
 266     // is T.  null_type meet null_type is null_type.
 267     if (t1->equals(null_type())) {
 268       if (!t2->is_primitive_type() || t2->equals(null_type())) {
 269         return t2;
 270       }
 271     } else if (t2->equals(null_type())) {
 272       if (!t1->is_primitive_type()) {
 273         return t1;
 274       }
 275     }
 276           
 277     // At least one of the two types is a non-top primitive type.
 278     // The other type is not equal to it.  Fall to bottom.
 279     return bottom_type();
 280   } else {
 281     // Both types are non-top non-primitive types.  That is,
 282     // both types are either instanceKlasses or arrayKlasses.
 283     ciKlass* object_klass = analyzer->env()->Object_klass();
 284     ciKlass* k1 = t1->as_klass();
 285     ciKlass* k2 = t2->as_klass();
 286     if (k1->equals(object_klass) || k2->equals(object_klass)) {
 287       return object_klass;
 288     } else if (!k1->is_loaded() || !k2->is_loaded()) {
 289       // Unloaded classes fall to java.lang.Object at a merge.
 290       return object_klass;
 291     } else if (k1->is_interface() != k2->is_interface()) {
 292       // When an interface meets a non-interface, we get Object;
 293       // This is what the verifier does.
 294       return object_klass;
 295     } else if (k1->is_array_klass() || k2->is_array_klass()) {
 296       // When an array meets a non-array, we get Object.
 297       // When objArray meets typeArray, we also get Object.
 298       // And when typeArray meets different typeArray, we again get Object.
 299       // But when objArray meets objArray, we look carefully at element types.
 300       if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) {
 301         // Meet the element types, then construct the corresponding array type.
 302         ciKlass* elem1 = k1->as_obj_array_klass()->element_klass();
 303         ciKlass* elem2 = k2->as_obj_array_klass()->element_klass();
 304         ciKlass* elem  = type_meet_internal(elem1, elem2, analyzer)->as_klass();
 305         // Do an easy shortcut if one type is a super of the other.
 306         if (elem == elem1) {
 307           assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK");
 308           return k1;
 309         } else if (elem == elem2) {
 310           assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK");
 311           return k2;
 312         } else {
 313           return ciObjArrayKlass::make(elem);
 314         }
 315       } else {
 316         return object_klass;
 317       }
 318     } else {
 319       // Must be two plain old instance klasses.
 320       assert(k1->is_instance_klass(), "previous cases handle non-instances");
 321       assert(k2->is_instance_klass(), "previous cases handle non-instances");
 322       return k1->least_common_ancestor(k2);
 323     }
 324   }
 325 }
 326       
 327 
 328 // ------------------------------------------------------------------
 329 // ciTypeFlow::StateVector::StateVector
 330 //
 331 // Build a new state vector
 332 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
 333   _outer = analyzer;
 334   _stack_size = -1;
 335   _monitor_count = -1;
 336   // Allocate the _types array
 337   int max_cells = analyzer->max_cells();
 338   _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
 339   for (int i=0; i<max_cells; i++) {
 340     _types[i] = top_type();
 341   }
 342   _trap_bci = -1;
 343   _trap_index = 0;
 344 }
 345 
 346 // ------------------------------------------------------------------
 347 // ciTypeFlow::get_start_state
 348 //
 349 // Set this vector to the method entry state.
 350 const ciTypeFlow::StateVector* ciTypeFlow::get_start_state() {
 351   StateVector* state = new StateVector(this);
 352   if (is_osr_flow()) {
 353     ciTypeFlow* non_osr_flow = method()->get_flow_analysis();
 354     if (non_osr_flow->failing()) {
 355       record_failure(non_osr_flow->failure_reason());
 356       return NULL;
 357     }
 358     JsrSet* jsrs = new JsrSet(NULL, 16);
 359     Block* non_osr_block = non_osr_flow->existing_block_at(start_bci(), jsrs);
 360     if (non_osr_block == NULL) {
 361       record_failure("cannot reach OSR point");
 362       return NULL;
 363     }
 364     // load up the non-OSR state at this point
 365     non_osr_block->copy_state_into(state);
 366     int non_osr_start = non_osr_block->start();
 367     if (non_osr_start != start_bci()) {
 368       // must flow forward from it
 369       if (CITraceTypeFlow) {
 370         tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
 371       }
 372       Block* block = block_at(non_osr_start, jsrs);
 373       assert(block->limit() == start_bci(), "must flow forward to start");
 374       flow_block(block, state, jsrs);
 375     }
 376     return state;
 377     // Note:  The code below would be an incorrect for an OSR flow,
 378     // even if it were possible for an OSR entry point to be at bci zero.
 379   }
 380   // "Push" the method signature into the first few locals.
 381   state->set_stack_size(-max_locals());
 382   if (!method()->is_static()) {
 383     state->push(method()->holder());
 384     assert(state->tos() == state->local(0), "");
 385   }
 386   for (ciSignatureStream str(method()->signature());
 387        !str.at_return_type();
 388        str.next()) {
 389     state->push_translate(str.type());
 390   }
 391   // Set the rest of the locals to bottom.
 392   Cell cell = state->next_cell(state->tos());
 393   state->set_stack_size(0);
 394   int limit = state->limit_cell();
 395   for (; cell < limit; cell = state->next_cell(cell)) {
 396     state->set_type_at(cell, state->bottom_type());
 397   }
 398   // Lock an object, if necessary.
 399   state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
 400   return state;
 401 }
 402 
 403 // ------------------------------------------------------------------
 404 // ciTypeFlow::StateVector::copy_into
 405 //
 406 // Copy our value into some other StateVector
 407 void ciTypeFlow::StateVector::copy_into(ciTypeFlow::StateVector* copy)
 408 const {
 409   copy->set_stack_size(stack_size());
 410   copy->set_monitor_count(monitor_count());
 411   Cell limit = limit_cell();
 412   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 413     copy->set_type_at(c, type_at(c));
 414   }
 415 }
 416 
 417 // ------------------------------------------------------------------
 418 // ciTypeFlow::StateVector::meet
 419 //
 420 // Meets this StateVector with another, destructively modifying this
 421 // one.  Returns true if any modification takes place.
 422 bool ciTypeFlow::StateVector::meet(const ciTypeFlow::StateVector* incoming) {
 423   if (monitor_count() == -1) {
 424     set_monitor_count(incoming->monitor_count());
 425   }
 426   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
 427 
 428   if (stack_size() == -1) {
 429     set_stack_size(incoming->stack_size());
 430     Cell limit = limit_cell();
 431     #ifdef ASSERT
 432     { for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 433         assert(type_at(c) == top_type(), "");
 434     } }
 435     #endif
 436     // Make a simple copy of the incoming state.
 437     for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 438       set_type_at(c, incoming->type_at(c));
 439     }
 440     return true;  // it is always different the first time
 441   }
 442 #ifdef ASSERT
 443   if (stack_size() != incoming->stack_size()) {
 444     _outer->method()->print_codes();
 445     tty->print_cr("!!!! Stack size conflict");
 446     tty->print_cr("Current state:");
 447     print_on(tty);
 448     tty->print_cr("Incoming state:");
 449     ((StateVector*)incoming)->print_on(tty);
 450   }
 451 #endif
 452   assert(stack_size() == incoming->stack_size(), "sanity");
 453 
 454   bool different = false;
 455   Cell limit = limit_cell();
 456   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
 457     ciType* t1 = type_at(c);
 458     ciType* t2 = incoming->type_at(c);
 459     if (!t1->equals(t2)) {
 460       ciType* new_type = type_meet(t1, t2);
 461       if (!t1->equals(new_type)) {
 462         set_type_at(c, new_type);
 463         different = true;
 464       }
 465     }
 466   }
 467   return different;
 468 }
 469 
 470 // ------------------------------------------------------------------
 471 // ciTypeFlow::StateVector::meet_exception
 472 //
 473 // Meets this StateVector with another, destructively modifying this
 474 // one.  The incoming state is coming via an exception.  Returns true
 475 // if any modification takes place.
 476 bool ciTypeFlow::StateVector::meet_exception(ciInstanceKlass* exc,
 477                                      const ciTypeFlow::StateVector* incoming) {
 478   if (monitor_count() == -1) {
 479     set_monitor_count(incoming->monitor_count());
 480   }
 481   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
 482 
 483   if (stack_size() == -1) {
 484     set_stack_size(1);
 485   }
 486 
 487   assert(stack_size() ==  1, "must have one-element stack");
 488 
 489   bool different = false;
 490 
 491   // Meet locals from incoming array.
 492   Cell limit = local(_outer->max_locals()-1);
 493   for (Cell c = start_cell(); c <= limit; c = next_cell(c)) {
 494     ciType* t1 = type_at(c);
 495     ciType* t2 = incoming->type_at(c);
 496     if (!t1->equals(t2)) {
 497       ciType* new_type = type_meet(t1, t2);
 498       if (!t1->equals(new_type)) {
 499         set_type_at(c, new_type);
 500         different = true;
 501       }
 502     }
 503   }
 504   
 505   // Handle stack separately.  When an exception occurs, the
 506   // only stack entry is the exception instance.
 507   ciType* tos_type = type_at_tos();
 508   if (!tos_type->equals(exc)) {
 509     ciType* new_type = type_meet(tos_type, exc);
 510     if (!tos_type->equals(new_type)) {
 511       set_type_at_tos(new_type);
 512       different = true;
 513     }
 514   }
 515   
 516   return different;
 517 }
 518 
 519 // ------------------------------------------------------------------
 520 // ciTypeFlow::StateVector::push_translate
 521 void ciTypeFlow::StateVector::push_translate(ciType* type) {
 522   BasicType basic_type = type->basic_type();
 523   if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
 524       basic_type == T_BYTE    || basic_type == T_SHORT) {
 525     push_int();
 526   } else {
 527     push(type);
 528     if (type->is_two_word()) {
 529       push(half_type(type));
 530     }
 531   }
 532 }
 533 
 534 // ------------------------------------------------------------------
 535 // ciTypeFlow::StateVector::do_aaload
 536 void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
 537   pop_int();
 538   ciObjArrayKlass* array_klass = pop_objArray();
 539   if (array_klass == NULL) {
 540     // Did aaload on a null reference; push a null and ignore the exception.
 541     // This instruction will never continue normally.  All we have to do
 542     // is report a value that will meet correctly with any downstream
 543     // reference types on paths that will truly be executed.  This null type
 544     // meets with any reference type to yield that same reference type.
 545     // (The compiler will generate an unconditonal exception here.)
 546     push(null_type());
 547     return;
 548   }
 549   if (!array_klass->is_loaded()) {
 550     // Only fails for some -Xcomp runs
 551     trap(str, array_klass,
 552          Deoptimization::make_trap_request
 553          (Deoptimization::Reason_unloaded,
 554           Deoptimization::Action_reinterpret));
 555     return;
 556   }
 557   ciKlass* element_klass = array_klass->element_klass();
 558   if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
 559     Untested("unloaded array element class in ciTypeFlow");
 560     trap(str, element_klass,
 561          Deoptimization::make_trap_request
 562          (Deoptimization::Reason_unloaded,
 563           Deoptimization::Action_reinterpret));
 564   } else {
 565     push_object(element_klass);
 566   }
 567 }
 568 
 569 
 570 // ------------------------------------------------------------------
 571 // ciTypeFlow::StateVector::do_checkcast
 572 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
 573   bool will_link;
 574   ciKlass* klass = str->get_klass(will_link);
 575   if (!will_link) {
 576     // VM's interpreter will not load 'klass' if object is NULL.
 577     // Type flow after this block may still be needed in two situations:
 578     // 1) C2 uses do_null_assert() and continues compilation for later blocks
 579     // 2) C2 does an OSR compile in a later block (see bug 4778368).
 580     pop_object();
 581     do_null_assert(klass);
 582   } else {
 583     pop_object();
 584     push_object(klass);
 585   }
 586 }
 587 
 588 // ------------------------------------------------------------------
 589 // ciTypeFlow::StateVector::do_getfield
 590 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
 591   // could add assert here for type of object.
 592   pop_object();
 593   do_getstatic(str);
 594 }
 595 
 596 // ------------------------------------------------------------------
 597 // ciTypeFlow::StateVector::do_getstatic
 598 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
 599   bool will_link;
 600   ciField* field = str->get_field(will_link);
 601   if (!will_link) {
 602     trap(str, field->holder(), str->get_field_holder_index());
 603   } else {
 604     ciType* field_type = field->type();
 605     if (!field_type->is_loaded()) {
 606       // Normally, we need the field's type to be loaded if we are to
 607       // do anything interesting with its value.
 608       // We used to do this:  trap(str, str->get_field_signature_index());
 609       //
 610       // There is one good reason not to trap here.  Execution can
 611       // get past this "getfield" or "getstatic" if the value of
 612       // the field is null.  As long as the value is null, the class
 613       // does not need to be loaded!  The compiler must assume that
 614       // the value of the unloaded class reference is null; if the code
 615       // ever sees a non-null value, loading has occurred.
 616       //
 617       // This actually happens often enough to be annoying.  If the
 618       // compiler throws an uncommon trap at this bytecode, you can
 619       // get an endless loop of recompilations, when all the code
 620       // needs to do is load a series of null values.  Also, a trap
 621       // here can make an OSR entry point unreachable, triggering the
 622       // assert on non_osr_block in ciTypeFlow::get_start_state.
 623       // (See bug 4379915.)
 624       do_null_assert(field_type->as_klass());
 625     } else {
 626       push_translate(field_type);
 627     }
 628   }    
 629 }
 630 
 631 // ------------------------------------------------------------------
 632 // ciTypeFlow::StateVector::do_invoke
 633 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
 634                                         bool has_receiver) {
 635   bool will_link;
 636   ciMethod* method = str->get_method(will_link);
 637   if (!will_link) {
 638     // We weren't able to find the method.
 639     ciKlass* unloaded_holder = method->holder();
 640     trap(str, unloaded_holder, str->get_method_holder_index());
 641   } else {
 642     ciSignature* signature = method->signature();
 643     ciSignatureStream sigstr(signature);
 644     int arg_size = signature->size();
 645     int stack_base = stack_size() - arg_size;
 646     int i = 0;
 647     for( ; !sigstr.at_return_type(); sigstr.next()) {
 648       ciType* type = sigstr.type();
 649       ciType* stack_type = type_at(stack(stack_base + i++));
 650       // Do I want to check this type?
 651       // assert(stack_type->is_subtype_of(type), "bad type for field value");
 652       if (type->is_two_word()) {
 653         ciType* stack_type2 = type_at(stack(stack_base + i++));
 654         assert(stack_type2->equals(half_type(type)), "must be 2nd half");
 655       }
 656     }
 657     assert(arg_size == i, "must match");
 658     for (int j = 0; j < arg_size; j++) {
 659       pop();
 660     }
 661     if (has_receiver) {
 662       // Check this?
 663       pop_object();
 664     }
 665     assert(!sigstr.is_done(), "must have return type");
 666     ciType* return_type = sigstr.type();
 667     if (!return_type->is_void()) {
 668       if (!return_type->is_loaded()) {
 669         // As in do_getstatic(), generally speaking, we need the return type to 
 670         // be loaded if we are to do anything interesting with its value.
 671         // We used to do this:  trap(str, str->get_method_signature_index());
 672         //
 673         // We do not trap here since execution can get past this invoke if 
 674         // the return value is null.  As long as the value is null, the class
 675         // does not need to be loaded!  The compiler must assume that
 676         // the value of the unloaded class reference is null; if the code
 677         // ever sees a non-null value, loading has occurred.
 678         //
 679         // See do_getstatic() for similar explanation, as well as bug 4684993.
 680         do_null_assert(return_type->as_klass());
 681       } else {
 682         push_translate(return_type);
 683       }
 684     }
 685   }
 686 }
 687 
 688 // ------------------------------------------------------------------
 689 // ciTypeFlow::StateVector::do_jsr
 690 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
 691   push(ciReturnAddress::make(str->next_bci()));
 692 }
 693 
 694 // ------------------------------------------------------------------
 695 // ciTypeFlow::StateVector::do_ldc
 696 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
 697   ciConstant con = str->get_constant();
 698   BasicType basic_type = con.basic_type();
 699   if (basic_type == T_ILLEGAL) {
 700     // OutOfMemoryError in the CI while loading constant
 701     push_null();
 702     outer()->record_failure("ldc did not link");
 703     return;
 704   }
 705   if (basic_type == T_OBJECT || basic_type == T_ARRAY) {
 706     ciObject* obj = con.as_object();
 707     if (obj->is_null_object()) {
 708       push_null();
 709     } else if (obj->is_klass()) {
 710       // The type of ldc <class> is java.lang.Class
 711       push_object(outer()->env()->Class_klass());
 712     } else {
 713       push_object(obj->klass());
 714     }
 715   } else {
 716     push_translate(ciType::make(basic_type));
 717   }
 718 }
 719 
 720 // ------------------------------------------------------------------
 721 // ciTypeFlow::StateVector::do_multianewarray
 722 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
 723   int dimensions = str->get_dimensions();
 724   bool will_link;
 725   ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
 726   if (!will_link) {
 727     trap(str, array_klass, str->get_klass_index());
 728   } else {
 729     for (int i = 0; i < dimensions; i++) {
 730       pop_int();
 731     }
 732     push_object(array_klass);
 733   }
 734 }
 735 
 736 // ------------------------------------------------------------------
 737 // ciTypeFlow::StateVector::do_new
 738 void ciTypeFlow::StateVector::do_new(ciBytecodeStream* str) {
 739   bool will_link;
 740   ciKlass* klass = str->get_klass(will_link);
 741   if (!will_link) {
 742     trap(str, klass, str->get_klass_index());
 743   } else {
 744     push_object(klass);
 745   }
 746 }
 747 
 748 // ------------------------------------------------------------------
 749 // ciTypeFlow::StateVector::do_newarray
 750 void ciTypeFlow::StateVector::do_newarray(ciBytecodeStream* str) {
 751   pop_int();
 752   ciKlass* klass = ciTypeArrayKlass::make((BasicType)str->get_index());
 753   push_object(klass);
 754 }
 755 
 756 // ------------------------------------------------------------------
 757 // ciTypeFlow::StateVector::do_putfield
 758 void ciTypeFlow::StateVector::do_putfield(ciBytecodeStream* str) {
 759   do_putstatic(str);
 760   if (_trap_bci != -1)  return;  // unloaded field holder, etc.
 761   // could add assert here for type of object.
 762   pop_object();
 763 }
 764 
 765 // ------------------------------------------------------------------
 766 // ciTypeFlow::StateVector::do_putstatic
 767 void ciTypeFlow::StateVector::do_putstatic(ciBytecodeStream* str) {
 768   bool will_link;
 769   ciField* field = str->get_field(will_link);
 770   if (!will_link) {
 771     trap(str, field->holder(), str->get_field_holder_index());
 772   } else {
 773     ciType* field_type = field->type();
 774     ciType* type = pop_value();
 775     // Do I want to check this type?
 776     //      assert(type->is_subtype_of(field_type), "bad type for field value");
 777     if (field_type->is_two_word()) {
 778       ciType* type2 = pop_value();
 779       assert(type2->is_two_word(), "must be 2nd half");
 780       assert(type == half_type(type2), "must be 2nd half");
 781     }
 782   }    
 783 }
 784 
 785 // ------------------------------------------------------------------
 786 // ciTypeFlow::StateVector::do_ret
 787 void ciTypeFlow::StateVector::do_ret(ciBytecodeStream* str) {
 788   Cell index = local(str->get_index());
 789   
 790   ciType* address = type_at(index);
 791   assert(address->is_return_address(), "bad return address");
 792   set_type_at(index, bottom_type());
 793 }
 794 
 795 // ------------------------------------------------------------------
 796 // ciTypeFlow::StateVector::trap
 797 //
 798 // Stop interpretation of this path with a trap.
 799 void ciTypeFlow::StateVector::trap(ciBytecodeStream* str, ciKlass* klass, int index) {
 800   _trap_bci = str->cur_bci();
 801   _trap_index = index;
 802 
 803   // Log information about this trap:
 804   CompileLog* log = outer()->env()->log();
 805   if (log != NULL) {
 806     int mid = log->identify(outer()->method());
 807     int kid = (klass == NULL)? -1: log->identify(klass);
 808     log->begin_elem("uncommon_trap method='%d' bci='%d'", mid, str->cur_bci());
 809     char buf[100];
 810     log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf),
 811                                                           index));
 812     if (kid >= 0)
 813       log->print(" klass='%d'", kid);
 814     log->end_elem();
 815   }
 816 }
 817 
 818 // ------------------------------------------------------------------
 819 // ciTypeFlow::StateVector::do_null_assert
 820 // Corresponds to graphKit::do_null_assert.
 821 void ciTypeFlow::StateVector::do_null_assert(ciKlass* unloaded_klass) {
 822   if (unloaded_klass->is_loaded()) {
 823     // We failed to link, but we can still compute with this class,
 824     // since it is loaded somewhere.  The compiler will uncommon_trap
 825     // if the object is not null, but the typeflow pass can not assume
 826     // that the object will be null, otherwise it may incorrectly tell
 827     // the parser that an object is known to be null. 4761344, 4807707
 828     push_object(unloaded_klass);
 829   } else {
 830     // The class is not loaded anywhere.  It is safe to model the
 831     // null in the typestates, because we can compile in a null check
 832     // which will deoptimize us if someone manages to load the
 833     // class later.
 834     push_null();
 835   }
 836 } 
 837 
 838 
 839 // ------------------------------------------------------------------
 840 // ciTypeFlow::StateVector::apply_one_bytecode
 841 //
 842 // Apply the effect of one bytecode to this StateVector
 843 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
 844   _trap_bci = -1;
 845   _trap_index = 0;
 846 
 847   if (CITraceTypeFlow) {
 848     tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
 849                   Bytecodes::name(str->cur_bc()));
 850   }
 851 
 852   switch(str->cur_bc()) {
 853   case Bytecodes::_aaload: do_aaload(str);                       break;
 854 
 855   case Bytecodes::_aastore:
 856     {
 857       pop_object();
 858       pop_int();
 859       pop_objArray();
 860       break;
 861     }
 862   case Bytecodes::_aconst_null:
 863     {
 864       push_null();
 865       break;
 866     }
 867   case Bytecodes::_aload:   load_local_object(str->get_index());    break;
 868   case Bytecodes::_aload_0: load_local_object(0);                   break;
 869   case Bytecodes::_aload_1: load_local_object(1);                   break;
 870   case Bytecodes::_aload_2: load_local_object(2);                   break;
 871   case Bytecodes::_aload_3: load_local_object(3);                   break;
 872 
 873   case Bytecodes::_anewarray:
 874     {
 875       pop_int();
 876       bool will_link;
 877       ciKlass* element_klass = str->get_klass(will_link);
 878       if (!will_link) {
 879         trap(str, element_klass, str->get_klass_index());
 880       } else {
 881         push_object(ciObjArrayKlass::make(element_klass));
 882       }
 883       break;
 884     }
 885   case Bytecodes::_areturn:
 886   case Bytecodes::_ifnonnull:
 887   case Bytecodes::_ifnull:
 888     {
 889       pop_object();
 890       break;
 891     }
 892   case Bytecodes::_monitorenter:
 893     {
 894       pop_object();
 895       set_monitor_count(monitor_count() + 1);
 896       break;
 897     }
 898   case Bytecodes::_monitorexit:
 899     {
 900       pop_object();
 901       assert(monitor_count() > 0, "must be a monitor to exit from");
 902       set_monitor_count(monitor_count() - 1);
 903       break;
 904     }
 905   case Bytecodes::_arraylength:
 906     {
 907       pop_array();
 908       push_int();
 909       break;
 910     }
 911   case Bytecodes::_astore:   store_local_object(str->get_index());  break;
 912   case Bytecodes::_astore_0: store_local_object(0);                 break;
 913   case Bytecodes::_astore_1: store_local_object(1);                 break;
 914   case Bytecodes::_astore_2: store_local_object(2);                 break;
 915   case Bytecodes::_astore_3: store_local_object(3);                 break;
 916 
 917   case Bytecodes::_athrow:
 918     {
 919       NEEDS_CLEANUP;
 920       pop_object();
 921       break;
 922     }
 923   case Bytecodes::_baload:
 924   case Bytecodes::_caload:
 925   case Bytecodes::_iaload:
 926   case Bytecodes::_saload:
 927     {
 928       pop_int();
 929       ciTypeArrayKlass* array_klass = pop_typeArray();
 930       // Put assert here for right type?
 931       push_int();
 932       break;
 933     }
 934   case Bytecodes::_bastore:
 935   case Bytecodes::_castore:
 936   case Bytecodes::_iastore:
 937   case Bytecodes::_sastore:
 938     {
 939       pop_int();
 940       pop_int();
 941       pop_typeArray();
 942       // assert here?
 943       break;
 944     }
 945   case Bytecodes::_bipush:
 946   case Bytecodes::_iconst_m1:
 947   case Bytecodes::_iconst_0:
 948   case Bytecodes::_iconst_1:
 949   case Bytecodes::_iconst_2:
 950   case Bytecodes::_iconst_3:
 951   case Bytecodes::_iconst_4:
 952   case Bytecodes::_iconst_5:
 953   case Bytecodes::_sipush:
 954     {
 955       push_int();
 956       break;
 957     }
 958   case Bytecodes::_checkcast: do_checkcast(str);                  break;
 959 
 960   case Bytecodes::_d2f:
 961     {
 962       pop_double();
 963       push_float();
 964       break;
 965     }
 966   case Bytecodes::_d2i:
 967     {
 968       pop_double();
 969       push_int();
 970       break;
 971     }
 972   case Bytecodes::_d2l:
 973     {
 974       pop_double();
 975       push_long();
 976       break;
 977     }
 978   case Bytecodes::_dadd:
 979   case Bytecodes::_ddiv:
 980   case Bytecodes::_dmul:
 981   case Bytecodes::_drem:
 982   case Bytecodes::_dsub:
 983     {
 984       pop_double();
 985       pop_double();
 986       push_double();
 987       break;
 988     }
 989   case Bytecodes::_daload:
 990     {
 991       pop_int();
 992       ciTypeArrayKlass* array_klass = pop_typeArray();
 993       // Put assert here for right type?
 994       push_double();
 995       break;
 996     }
 997   case Bytecodes::_dastore:
 998     {
 999       pop_double();
1000       pop_int();
1001       pop_typeArray();
1002       // assert here?
1003       break;
1004     }
1005   case Bytecodes::_dcmpg:
1006   case Bytecodes::_dcmpl:
1007     {
1008       pop_double();
1009       pop_double();
1010       push_int();
1011       break;
1012     }
1013   case Bytecodes::_dconst_0:
1014   case Bytecodes::_dconst_1:
1015     {
1016       push_double();
1017       break;
1018     }
1019   case Bytecodes::_dload:   load_local_double(str->get_index());    break;
1020   case Bytecodes::_dload_0: load_local_double(0);                   break;
1021   case Bytecodes::_dload_1: load_local_double(1);                   break;
1022   case Bytecodes::_dload_2: load_local_double(2);                   break;
1023   case Bytecodes::_dload_3: load_local_double(3);                   break;
1024 
1025   case Bytecodes::_dneg:
1026     {
1027       pop_double();
1028       push_double();
1029       break;
1030     }
1031   case Bytecodes::_dreturn:
1032     {
1033       pop_double();
1034       break;
1035     }
1036   case Bytecodes::_dstore:   store_local_double(str->get_index());  break;
1037   case Bytecodes::_dstore_0: store_local_double(0);                 break;
1038   case Bytecodes::_dstore_1: store_local_double(1);                 break;
1039   case Bytecodes::_dstore_2: store_local_double(2);                 break;
1040   case Bytecodes::_dstore_3: store_local_double(3);                 break;
1041 
1042   case Bytecodes::_dup:
1043     {
1044       push(type_at_tos());
1045       break;
1046     }
1047   case Bytecodes::_dup_x1:
1048     {
1049       ciType* value1 = pop_value();
1050       ciType* value2 = pop_value();
1051       push(value1);
1052       push(value2);
1053       push(value1);
1054       break;
1055     }
1056   case Bytecodes::_dup_x2:
1057     {
1058       ciType* value1 = pop_value();
1059       ciType* value2 = pop_value();
1060       ciType* value3 = pop_value();
1061       push(value1);
1062       push(value3);
1063       push(value2);
1064       push(value1);
1065       break;
1066     }
1067   case Bytecodes::_dup2:
1068     {
1069       ciType* value1 = pop_value();
1070       ciType* value2 = pop_value();
1071       push(value2);
1072       push(value1);
1073       push(value2);
1074       push(value1);
1075       break;
1076     }
1077   case Bytecodes::_dup2_x1:
1078     {
1079       ciType* value1 = pop_value();
1080       ciType* value2 = pop_value();
1081       ciType* value3 = pop_value();
1082       push(value2);
1083       push(value1);
1084       push(value3);
1085       push(value2);
1086       push(value1);
1087       break;
1088     }
1089   case Bytecodes::_dup2_x2:
1090     {
1091       ciType* value1 = pop_value();
1092       ciType* value2 = pop_value();
1093       ciType* value3 = pop_value();
1094       ciType* value4 = pop_value();
1095       push(value2);
1096       push(value1);
1097       push(value4);
1098       push(value3);
1099       push(value2);
1100       push(value1);
1101       break;
1102     }
1103   case Bytecodes::_f2d:
1104     {
1105       pop_float();
1106       push_double();
1107       break;
1108     }
1109   case Bytecodes::_f2i:
1110     {
1111       pop_float();
1112       push_int();
1113       break;
1114     }
1115   case Bytecodes::_f2l:
1116     {
1117       pop_float();
1118       push_long();
1119       break;
1120     }
1121   case Bytecodes::_fadd:
1122   case Bytecodes::_fdiv:
1123   case Bytecodes::_fmul:
1124   case Bytecodes::_frem:
1125   case Bytecodes::_fsub:
1126     {
1127       pop_float();
1128       pop_float();
1129       push_float();
1130       break;
1131     }
1132   case Bytecodes::_faload:
1133     {
1134       pop_int();
1135       ciTypeArrayKlass* array_klass = pop_typeArray();
1136       // Put assert here.
1137       push_float();
1138       break;
1139     }
1140   case Bytecodes::_fastore:
1141     {
1142       pop_float();
1143       pop_int();
1144       ciTypeArrayKlass* array_klass = pop_typeArray();
1145       // Put assert here.
1146       break;
1147     }
1148   case Bytecodes::_fcmpg:
1149   case Bytecodes::_fcmpl:
1150     {
1151       pop_float();
1152       pop_float();
1153       push_int();
1154       break;
1155     }
1156   case Bytecodes::_fconst_0:
1157   case Bytecodes::_fconst_1:
1158   case Bytecodes::_fconst_2:
1159     {
1160       push_float();
1161       break;
1162     }
1163   case Bytecodes::_fload:   load_local_float(str->get_index());     break;
1164   case Bytecodes::_fload_0: load_local_float(0);                    break;
1165   case Bytecodes::_fload_1: load_local_float(1);                    break;
1166   case Bytecodes::_fload_2: load_local_float(2);                    break;
1167   case Bytecodes::_fload_3: load_local_float(3);                    break;
1168 
1169   case Bytecodes::_fneg:
1170     {
1171       pop_float();
1172       push_float();
1173       break;
1174     }
1175   case Bytecodes::_freturn:
1176     {
1177       pop_float();
1178       break;
1179     }
1180   case Bytecodes::_fstore:    store_local_float(str->get_index());   break;
1181   case Bytecodes::_fstore_0:  store_local_float(0);                  break;
1182   case Bytecodes::_fstore_1:  store_local_float(1);                  break;
1183   case Bytecodes::_fstore_2:  store_local_float(2);                  break;
1184   case Bytecodes::_fstore_3:  store_local_float(3);                  break;
1185 
1186   case Bytecodes::_getfield:  do_getfield(str);                      break;
1187   case Bytecodes::_getstatic: do_getstatic(str);                     break;
1188     
1189   case Bytecodes::_goto:
1190   case Bytecodes::_goto_w:
1191   case Bytecodes::_nop:
1192   case Bytecodes::_return:
1193     {
1194       // do nothing.
1195       break;
1196     }
1197   case Bytecodes::_i2b:
1198   case Bytecodes::_i2c:
1199   case Bytecodes::_i2s:
1200   case Bytecodes::_ineg:
1201     {
1202       pop_int();
1203       push_int();
1204       break;
1205     }
1206   case Bytecodes::_i2d:
1207     {
1208       pop_int();
1209       push_double();
1210       break;
1211     }
1212   case Bytecodes::_i2f:
1213     {
1214       pop_int();
1215       push_float();
1216       break;
1217     }
1218   case Bytecodes::_i2l:
1219     {
1220       pop_int();
1221       push_long();
1222       break;
1223     }
1224   case Bytecodes::_iadd:
1225   case Bytecodes::_iand:
1226   case Bytecodes::_idiv:
1227   case Bytecodes::_imul:
1228   case Bytecodes::_ior:
1229   case Bytecodes::_irem:
1230   case Bytecodes::_ishl:
1231   case Bytecodes::_ishr:
1232   case Bytecodes::_isub:
1233   case Bytecodes::_iushr:
1234   case Bytecodes::_ixor:
1235     {
1236       pop_int();
1237       pop_int();
1238       push_int();
1239       break;
1240     }
1241   case Bytecodes::_if_acmpeq:
1242   case Bytecodes::_if_acmpne:
1243     {
1244       pop_object();
1245       pop_object();
1246       break;
1247     }
1248   case Bytecodes::_if_icmpeq:
1249   case Bytecodes::_if_icmpge:
1250   case Bytecodes::_if_icmpgt:
1251   case Bytecodes::_if_icmple:
1252   case Bytecodes::_if_icmplt:
1253   case Bytecodes::_if_icmpne:
1254     {
1255       pop_int();
1256       pop_int();
1257       break;
1258     }
1259   case Bytecodes::_ifeq:
1260   case Bytecodes::_ifle:
1261   case Bytecodes::_iflt:
1262   case Bytecodes::_ifge:
1263   case Bytecodes::_ifgt:
1264   case Bytecodes::_ifne:
1265   case Bytecodes::_ireturn:
1266   case Bytecodes::_lookupswitch:
1267   case Bytecodes::_tableswitch:
1268     {
1269       pop_int();
1270       break;
1271     }
1272   case Bytecodes::_iinc:
1273     {
1274       check_int(local(str->get_index()));
1275       break;
1276     }
1277   case Bytecodes::_iload:   load_local_int(str->get_index()); break;
1278   case Bytecodes::_iload_0: load_local_int(0);                      break;
1279   case Bytecodes::_iload_1: load_local_int(1);                      break;
1280   case Bytecodes::_iload_2: load_local_int(2);                      break;
1281   case Bytecodes::_iload_3: load_local_int(3);                      break;
1282 
1283   case Bytecodes::_instanceof:
1284     {
1285       // Check for uncommon trap:
1286       do_checkcast(str);
1287       pop_object();
1288       push_int();
1289       break;
1290     }
1291   case Bytecodes::_invokeinterface: do_invoke(str, true);           break;
1292   case Bytecodes::_invokespecial:   do_invoke(str, true);           break;
1293   case Bytecodes::_invokestatic:    do_invoke(str, false);          break;
1294 
1295   case Bytecodes::_invokevirtual:   do_invoke(str, true);           break;
1296 
1297   case Bytecodes::_istore:   store_local_int(str->get_index());     break;
1298   case Bytecodes::_istore_0: store_local_int(0);                    break;
1299   case Bytecodes::_istore_1: store_local_int(1);                    break;
1300   case Bytecodes::_istore_2: store_local_int(2);                    break;
1301   case Bytecodes::_istore_3: store_local_int(3);                    break;
1302 
1303   case Bytecodes::_jsr:
1304   case Bytecodes::_jsr_w: do_jsr(str);                              break;
1305 
1306   case Bytecodes::_l2d:
1307     {
1308       pop_long();
1309       push_double();
1310       break;
1311     }
1312   case Bytecodes::_l2f:
1313     {
1314       pop_long();
1315       push_float();
1316       break;
1317     }
1318   case Bytecodes::_l2i:
1319     {
1320       pop_long();
1321       push_int();
1322       break;
1323     }
1324   case Bytecodes::_ladd:
1325   case Bytecodes::_land:
1326   case Bytecodes::_ldiv:
1327   case Bytecodes::_lmul:
1328   case Bytecodes::_lor:
1329   case Bytecodes::_lrem:
1330   case Bytecodes::_lsub:
1331   case Bytecodes::_lxor:
1332     {
1333       pop_long();
1334       pop_long();
1335       push_long();
1336       break;
1337     }
1338   case Bytecodes::_laload:
1339     {
1340       pop_int();
1341       ciTypeArrayKlass* array_klass = pop_typeArray();
1342       // Put assert here for right type?
1343       push_long();
1344       break;
1345     }
1346   case Bytecodes::_lastore:
1347     {
1348       pop_long();
1349       pop_int();
1350       pop_typeArray();
1351       // assert here?
1352       break;
1353     }
1354   case Bytecodes::_lcmp:
1355     {
1356       pop_long();
1357       pop_long();
1358       push_int();
1359       break;
1360     }
1361   case Bytecodes::_lconst_0:
1362   case Bytecodes::_lconst_1:
1363     {
1364       push_long();
1365       break;
1366     }
1367   case Bytecodes::_ldc:
1368   case Bytecodes::_ldc_w:
1369   case Bytecodes::_ldc2_w:
1370     {
1371       do_ldc(str);
1372       break;
1373     }
1374 
1375   case Bytecodes::_lload:   load_local_long(str->get_index());      break;
1376   case Bytecodes::_lload_0: load_local_long(0);                     break;
1377   case Bytecodes::_lload_1: load_local_long(1);                     break;
1378   case Bytecodes::_lload_2: load_local_long(2);                     break;
1379   case Bytecodes::_lload_3: load_local_long(3);                     break;
1380     
1381   case Bytecodes::_lneg:
1382     {
1383       pop_long();
1384       push_long();
1385       break;
1386     }
1387   case Bytecodes::_lreturn:
1388     {
1389       pop_long();
1390       break;
1391     }
1392   case Bytecodes::_lshl:
1393   case Bytecodes::_lshr:
1394   case Bytecodes::_lushr:
1395     {
1396       pop_int();
1397       pop_long();
1398       push_long();
1399       break;
1400     }
1401   case Bytecodes::_lstore:   store_local_long(str->get_index());    break;
1402   case Bytecodes::_lstore_0: store_local_long(0);                   break;
1403   case Bytecodes::_lstore_1: store_local_long(1);                   break;
1404   case Bytecodes::_lstore_2: store_local_long(2);                   break;
1405   case Bytecodes::_lstore_3: store_local_long(3);                   break;
1406 
1407   case Bytecodes::_multianewarray: do_multianewarray(str);          break;
1408 
1409   case Bytecodes::_new:      do_new(str);                           break;
1410 
1411   case Bytecodes::_newarray: do_newarray(str);                      break;
1412     
1413   case Bytecodes::_pop:
1414     {
1415       pop();
1416       break;
1417     }
1418   case Bytecodes::_pop2:
1419     {
1420       pop();
1421       pop();
1422       break;
1423     }
1424 
1425   case Bytecodes::_putfield:       do_putfield(str);                 break;
1426   case Bytecodes::_putstatic:      do_putstatic(str);                break;
1427   
1428   case Bytecodes::_ret: do_ret(str);                                 break;
1429 
1430   case Bytecodes::_swap:
1431     {
1432       ciType* value1 = pop_value();
1433       ciType* value2 = pop_value();
1434       push(value1);
1435       push(value2);
1436       break;
1437     }
1438   case Bytecodes::_wide:
1439   default:
1440     {
1441       // The iterator should skip this.
1442       ShouldNotReachHere();
1443       break;
1444     }
1445   }
1446 
1447   if (CITraceTypeFlow) {
1448     print_on(tty);
1449   }
1450 
1451   return (_trap_bci != -1);
1452 }
1453 
1454 #ifndef PRODUCT
1455 // ------------------------------------------------------------------
1456 // ciTypeFlow::StateVector::print_cell_on
1457 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
1458   ciType* type = type_at(c);
1459   if (type == top_type()) {
1460     st->print("top");
1461   } else if (type == bottom_type()) {
1462     st->print("bottom");
1463   } else if (type == null_type()) {
1464     st->print("null");
1465   } else if (type == long2_type()) {
1466     st->print("long2");
1467   } else if (type == double2_type()) {
1468     st->print("double2");
1469   } else if (is_int(type)) {
1470     st->print("int");
1471   } else if (is_long(type)) {
1472     st->print("long");
1473   } else if (is_float(type)) {
1474     st->print("float");
1475   } else if (is_double(type)) {
1476     st->print("double");
1477   } else if (type->is_return_address()) {
1478     st->print("address(%d)", type->as_return_address()->bci());
1479   } else {
1480     if (type->is_klass()) {
1481       type->as_klass()->name()->print_symbol_on(st);
1482     } else {
1483       st->print("UNEXPECTED TYPE");
1484       type->print();
1485     }
1486   }
1487 }
1488 
1489 // ------------------------------------------------------------------
1490 // ciTypeFlow::StateVector::print_on
1491 void ciTypeFlow::StateVector::print_on(outputStream* st) const {
1492   int num_locals   = _outer->max_locals();
1493   int num_stack    = stack_size();
1494   int num_monitors = monitor_count();
1495   st->print_cr("  State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors);
1496   if (num_stack >= 0) {
1497     int i;
1498     for (i = 0; i < num_locals; i++) {
1499       st->print("    local %2d : ", i);
1500       print_cell_on(st, local(i));
1501       st->cr();
1502     }
1503     for (i = 0; i < num_stack; i++) {
1504       st->print("    stack %2d : ", i);
1505       print_cell_on(st, stack(i));
1506       st->cr();
1507     }
1508   }
1509 }
1510 #endif 
1511 
1512 // ciTypeFlow::Block
1513 //
1514 // A basic block.
1515 
1516 // ------------------------------------------------------------------
1517 // ciTypeFlow::Block::Block
1518 ciTypeFlow::Block::Block(ciTypeFlow* outer,
1519                          ciBlock *ciblk,
1520                          ciTypeFlow::JsrSet* jsrs) {
1521   _ciblock = ciblk;
1522   _exceptions = NULL;
1523   _exc_klasses = NULL;
1524   _successors = NULL;
1525   _state = new (outer->arena()) StateVector(outer);
1526   JsrSet* new_jsrs =
1527     new (outer->arena()) JsrSet(outer->arena(), jsrs->size());
1528   jsrs->copy_into(new_jsrs);
1529   _jsrs = new_jsrs;
1530   _next = NULL;
1531   _on_work_list = false;
1532   _pre_order = -1; assert(!has_pre_order(), "");
1533   _private_copy = false;
1534   _trap_bci = -1;
1535   _trap_index = 0;
1536 
1537   if (CITraceTypeFlow) {
1538     tty->print_cr(">> Created new block");
1539     print_on(tty);
1540   }
1541 
1542   assert(this->outer() == outer, "outer link set up");
1543   assert(!outer->have_block_count(), "must not have mapped blocks yet");
1544 }
1545 
1546 // ------------------------------------------------------------------
1547 // ciTypeFlow::Block::clone_loop_head
1548 //
1549 ciTypeFlow::Block*
1550 ciTypeFlow::Block::clone_loop_head(ciTypeFlow* analyzer,
1551                                    int branch_bci,
1552                                    ciTypeFlow::Block* target,
1553                                    ciTypeFlow::JsrSet* jsrs) {
1554   // Loop optimizations are not performed on Tier1 compiles. Do nothing.
1555   if (analyzer->env()->comp_level() < CompLevel_full_optimization) {
1556     return target;
1557   }
1558 
1559   // The current block ends with a branch.
1560   //
1561   // If the target block appears to be the test-clause of a for loop, and
1562   // it is not too large, and it has not yet been cloned, clone it.
1563   // The pre-existing copy becomes the private clone used only by
1564   // the initial iteration of the loop.  (We know we are simulating
1565   // the initial iteration right now, since we have never calculated
1566   // successors before for this block.)
1567   
1568   if (branch_bci <= start()
1569       && (target->limit() - target->start()) <= CICloneLoopTestLimit
1570       && target->private_copy_count() == 0) {
1571     // Setting the private_copy bit ensures that the target block cannot be
1572     // reached by any other paths, such as fall-in from the loop body.
1573     // The private copy will be accessible only on successor lists 
1574     // created up to this point.
1575     target->set_private_copy(true);
1576     if (CITraceTypeFlow) {
1577       tty->print(">> Cloning a test-clause block ");
1578       print_value_on(tty);
1579       tty->cr();
1580     }
1581     // If the target is the current block, then later on a new copy of the 
1582     // target block will be created when its bytecodes are reached by 
1583     // an alternate path. (This is the case for loops with the loop
1584     // head at the bci-wise bottom of the loop, as with pre-1.4.2 javac.)
1585     //
1586     // Otherwise, duplicate the target block now and use it immediately.
1587     // (The case for loops with the loop head at the bci-wise top of the
1588     // loop, as with 1.4.2 javac.)
1589     //
1590     // In either case, the new copy of the block will remain public.
1591     if (target != this) {
1592       target = analyzer->block_at(branch_bci, jsrs);
1593     }
1594   }
1595   return target;
1596 }
1597 
1598 // ------------------------------------------------------------------
1599 // ciTypeFlow::Block::successors
1600 //
1601 // Get the successors for this Block.
1602 GrowableArray<ciTypeFlow::Block*>*
1603 ciTypeFlow::Block::successors(ciBytecodeStream* str,
1604                               ciTypeFlow::StateVector* state,
1605                               ciTypeFlow::JsrSet* jsrs) {
1606   if (_successors == NULL) {
1607     if (CITraceTypeFlow) {
1608       tty->print(">> Computing successors for block ");
1609       print_value_on(tty);
1610       tty->cr();
1611     }
1612     
1613     ciTypeFlow* analyzer = outer();
1614     Arena* arena = analyzer->arena();
1615     Block* block = NULL;
1616     bool has_successor = !has_trap() &&
1617                          (control() != ciBlock::fall_through_bci || limit() < analyzer->code_size());
1618     if (!has_successor) {
1619       _successors =
1620         new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1621       // No successors
1622     } else if (control() == ciBlock::fall_through_bci) {
1623       assert(str->cur_bci() == limit(), "bad block end");
1624       // This block simply falls through to the next.
1625       _successors =
1626         new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1627 
1628       Block* block = analyzer->block_at(limit(), _jsrs);
1629       assert(_successors->length() == FALL_THROUGH, "");
1630       _successors->append(block);
1631     } else {
1632       int current_bci = str->cur_bci();
1633       int next_bci = str->next_bci();
1634       int branch_bci = -1;
1635       Block* target = NULL;
1636       assert(str->next_bci() == limit(), "bad block end");
1637       // This block is not a simple fall-though.  Interpret
1638       // the current bytecode to find our successors.
1639       switch (str->cur_bc()) {
1640       case Bytecodes::_ifeq:         case Bytecodes::_ifne:
1641       case Bytecodes::_iflt:         case Bytecodes::_ifge:
1642       case Bytecodes::_ifgt:         case Bytecodes::_ifle:
1643       case Bytecodes::_if_icmpeq:    case Bytecodes::_if_icmpne:
1644       case Bytecodes::_if_icmplt:    case Bytecodes::_if_icmpge:
1645       case Bytecodes::_if_icmpgt:    case Bytecodes::_if_icmple:
1646       case Bytecodes::_if_acmpeq:    case Bytecodes::_if_acmpne:
1647       case Bytecodes::_ifnull:       case Bytecodes::_ifnonnull:
1648         // Our successors are the branch target and the next bci.
1649         branch_bci = str->get_dest();
1650         clone_loop_head(analyzer, branch_bci, this, jsrs);
1651         _successors =
1652           new (arena) GrowableArray<Block*>(arena, 2, 0, NULL);
1653         assert(_successors->length() == IF_NOT_TAKEN, "");
1654         _successors->append(analyzer->block_at(next_bci, jsrs));
1655         assert(_successors->length() == IF_TAKEN, "");
1656         _successors->append(analyzer->block_at(branch_bci, jsrs));
1657         break;
1658         
1659       case Bytecodes::_goto:
1660         branch_bci = str->get_dest();
1661         _successors =
1662           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1663         assert(_successors->length() == GOTO_TARGET, "");
1664         target = analyzer->block_at(branch_bci, jsrs);
1665         // If the target block has not been visited yet, and looks like
1666         // a two-way branch, attempt to clone it if it is a loop head.
1667         if (target->_successors != NULL
1668             && target->_successors->length() == (IF_TAKEN + 1)) {
1669           target = clone_loop_head(analyzer, branch_bci, target, jsrs);
1670         }
1671         _successors->append(target);
1672         break;
1673 
1674       case Bytecodes::_jsr:
1675         branch_bci = str->get_dest();
1676         _successors =
1677           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1678         assert(_successors->length() == GOTO_TARGET, "");
1679         _successors->append(analyzer->block_at(branch_bci, jsrs));
1680         break;
1681 
1682       case Bytecodes::_goto_w:         
1683       case Bytecodes::_jsr_w:
1684         _successors =
1685           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1686         assert(_successors->length() == GOTO_TARGET, "");
1687         _successors->append(analyzer->block_at(str->get_far_dest(), jsrs));
1688         break;
1689 
1690       case Bytecodes::_tableswitch:  {
1691         Bytecode_tableswitch *tableswitch =
1692           Bytecode_tableswitch_at(str->cur_bcp());
1693         
1694         int len = tableswitch->length();        
1695         _successors =
1696           new (arena) GrowableArray<Block*>(arena, len+1, 0, NULL);
1697         int bci = current_bci + tableswitch->default_offset();
1698         Block* block = analyzer->block_at(bci, jsrs);
1699         assert(_successors->length() == SWITCH_DEFAULT, "");
1700         _successors->append(block);
1701         while (--len >= 0) {
1702           int bci = current_bci + tableswitch->dest_offset_at(len);
1703           block = analyzer->block_at(bci, jsrs);
1704           assert(_successors->length() >= SWITCH_CASES, "");
1705           _successors->append_if_missing(block);
1706         }
1707         break; 
1708       }
1709       
1710       case Bytecodes::_lookupswitch: {
1711         Bytecode_lookupswitch *lookupswitch =
1712           Bytecode_lookupswitch_at(str->cur_bcp());
1713           
1714         int npairs = lookupswitch->number_of_pairs(); 
1715         _successors =
1716           new (arena) GrowableArray<Block*>(arena, npairs+1, 0, NULL);
1717         int bci = current_bci + lookupswitch->default_offset();
1718         Block* block = analyzer->block_at(bci, jsrs);
1719         assert(_successors->length() == SWITCH_DEFAULT, "");
1720         _successors->append(block);
1721         while(--npairs >= 0) {
1722           LookupswitchPair *pair = lookupswitch->pair_at(npairs);
1723           int bci = current_bci + pair->offset();
1724           Block* block = analyzer->block_at(bci, jsrs);
1725           assert(_successors->length() >= SWITCH_CASES, "");
1726           _successors->append_if_missing(block);
1727         }
1728         break; 
1729       }
1730 
1731       case Bytecodes::_athrow:     case Bytecodes::_ireturn:
1732       case Bytecodes::_lreturn:    case Bytecodes::_freturn:
1733       case Bytecodes::_dreturn:    case Bytecodes::_areturn:
1734       case Bytecodes::_return:
1735         _successors =
1736           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1737         // No successors
1738         break;
1739 
1740       case Bytecodes::_ret: {
1741         _successors =
1742           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
1743 
1744         Cell local = state->local(str->get_index());
1745         ciType* return_address = state->type_at(local);
1746         assert(return_address->is_return_address(), "verify: wrong type");
1747         int bci = return_address->as_return_address()->bci();
1748         assert(_successors->length() == GOTO_TARGET, "");
1749         _successors->append(analyzer->block_at(bci, jsrs));
1750         break;
1751       }
1752 
1753       case Bytecodes::_wide:           
1754       default:                 
1755         ShouldNotReachHere();
1756         break;
1757       }
1758     }
1759   }
1760   return _successors;
1761 }
1762 
1763 // ------------------------------------------------------------------
1764 // ciTypeFlow::Block:compute_exceptions
1765 //
1766 // Compute the exceptional successors and types for this Block.
1767 void ciTypeFlow::Block::compute_exceptions() {
1768   assert(_exceptions == NULL && _exc_klasses == NULL, "repeat");
1769 
1770   if (CITraceTypeFlow) {
1771     tty->print(">> Computing exceptions for block ");
1772     print_value_on(tty);
1773     tty->cr();
1774   }
1775   
1776   ciTypeFlow* analyzer = outer();
1777   Arena* arena = analyzer->arena();
1778   
1779   // Any bci in the block will do.
1780   ciExceptionHandlerStream str(analyzer->method(), start());
1781 
1782   // Allocate our growable arrays.
1783   int exc_count = str.count();
1784   _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, NULL);
1785   _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count,
1786                                                              0, NULL);
1787 
1788   for ( ; !str.is_done(); str.next()) {
1789     ciExceptionHandler* handler = str.handler();
1790     int bci = handler->handler_bci();
1791     ciInstanceKlass* klass = NULL;
1792     if (bci == -1) {
1793       // There is no catch all.  It is possible to exit the method.
1794       break;
1795     }
1796     if (handler->is_catch_all()) {
1797       klass = analyzer->env()->Throwable_klass();
1798     } else {
1799       klass = handler->catch_klass();
1800     }
1801     _exceptions->append(analyzer->block_at(bci, _jsrs));
1802     _exc_klasses->append(klass);
1803   }
1804 }
1805 
1806 // ------------------------------------------------------------------
1807 // ciTypeFlow::Block::is_simpler_than
1808 //
1809 // A relation used to order our work list.  We work on a block earlier
1810 // if it has a smaller jsr stack or it occurs earlier in the program
1811 // text.
1812 //
1813 // Note: maybe we should redo this functionality to make blocks
1814 // which correspond to exceptions lower priority.
1815 bool ciTypeFlow::Block::is_simpler_than(ciTypeFlow::Block* other) {
1816   if (other == NULL) {
1817     return true;
1818   } else {
1819     int size1 = _jsrs->size();
1820     int size2 = other->_jsrs->size();
1821     if (size1 < size2) {
1822       return true;
1823     } else if (size2 < size1) {
1824       return false;
1825     } else {
1826 #if 0
1827       if (size1 > 0) {
1828         int r1 = _jsrs->record_at(0)->return_address();
1829         int r2 = _jsrs->record_at(0)->return_address();
1830         if (r1 < r2) {
1831           return true;
1832         } else if (r2 < r1) {
1833           return false;
1834         } else {
1835           int e1 = _jsrs->record_at(0)->return_address();
1836           int e2 = _jsrs->record_at(0)->return_address();
1837           if (e1 < e2) {
1838             return true;
1839           } else if (e2 < e1) {
1840             return false;
1841           }
1842         }
1843       }
1844 #endif
1845       return (start() <= other->start()); 
1846     }
1847   }
1848 }
1849 
1850 // ------------------------------------------------------------------
1851 // ciTypeFlow::Block::set_private_copy
1852 // Use this only to make a pre-existing public block into a private copy.
1853 void ciTypeFlow::Block::set_private_copy(bool z) {
1854   assert(z || (z == is_private_copy()), "cannot make a private copy public");
1855   _private_copy = z;
1856 }
1857 
1858 #ifndef PRODUCT
1859 // ------------------------------------------------------------------
1860 // ciTypeFlow::Block::print_value_on
1861 void ciTypeFlow::Block::print_value_on(outputStream* st) const {
1862   if (has_pre_order())  st->print("#%-2d ", pre_order());
1863   st->print("[%d - %d)", start(), limit());
1864   if (_jsrs->size() > 0) { st->print("/");  _jsrs->print_on(st); }
1865   if (is_private_copy())  st->print("/private_copy");
1866 }
1867 
1868 // ------------------------------------------------------------------
1869 // ciTypeFlow::Block::print_on
1870 void ciTypeFlow::Block::print_on(outputStream* st) const {
1871   if ((Verbose || WizardMode)) {
1872     outer()->method()->print_codes_on(start(), limit(), st);
1873   }
1874   st->print_cr("  ====================================================  ");
1875   st->print ("  ");
1876   print_value_on(st);
1877   st->cr();
1878   _state->print_on(st);
1879   if (_successors == NULL) {
1880     st->print_cr("  No successor information");
1881   } else {
1882     int num_successors = _successors->length();
1883     st->print_cr("  Successors : %d", num_successors);
1884     for (int i = 0; i < num_successors; i++) {
1885       Block* successor = _successors->at(i);
1886       st->print("    ");
1887       successor->print_value_on(st);
1888       st->cr();
1889     }
1890   }
1891   if (_exceptions == NULL) {
1892     st->print_cr("  No exception information");
1893   } else {
1894     int num_exceptions = _exceptions->length();
1895     st->print_cr("  Exceptions : %d", num_exceptions);
1896     for (int i = 0; i < num_exceptions; i++) {
1897       Block* exc_succ = _exceptions->at(i);
1898       ciInstanceKlass* exc_klass = _exc_klasses->at(i);
1899       st->print("    ");
1900       exc_succ->print_value_on(st);
1901       st->print(" -- ");
1902       exc_klass->name()->print_symbol_on(st);
1903       st->cr();
1904     }
1905   }
1906   if (has_trap()) {
1907     st->print_cr("  Traps on %d with trap index %d", trap_bci(), trap_index());
1908   }
1909   st->print_cr("  ====================================================  ");
1910 }
1911 #endif
1912 
1913 // ciTypeFlow
1914 //
1915 // This is a pass over the bytecodes which computes the following:
1916 //   basic block structure
1917 //   interpreter type-states (a la the verifier)
1918 
1919 // ------------------------------------------------------------------
1920 // ciTypeFlow::ciTypeFlow
1921 ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) {
1922   _env = env;
1923   _method = method;
1924   _methodBlocks = method->get_method_blocks();
1925   _max_locals = method->max_locals();
1926   _max_stack = method->max_stack();
1927   _code_size = method->code_size();
1928   _osr_bci = osr_bci;
1929   _failure_reason = NULL;
1930   assert(start_bci() >= 0 && start_bci() < code_size() , "correct osr_bci argument");
1931 
1932   _work_list = NULL;
1933   _next_pre_order = 0;
1934 
1935   _ciblock_count = _methodBlocks->num_blocks();
1936   _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, _ciblock_count);
1937   for (int i = 0; i < _ciblock_count; i++) {
1938     _idx_to_blocklist[i] = NULL;
1939   }
1940   _block_map = NULL;  // until all blocks are seen
1941   _jsr_count = 0;
1942   _jsr_records = NULL;
1943 }
1944 
1945 // ------------------------------------------------------------------
1946 // ciTypeFlow::work_list_next
1947 //
1948 // Get the next basic block from our work list.
1949 ciTypeFlow::Block* ciTypeFlow::work_list_next() {
1950   assert(!work_list_empty(), "work list must not be empty");
1951   Block* next_block = _work_list;
1952   _work_list = next_block->next();
1953   next_block->set_next(NULL);
1954   next_block->set_on_work_list(false);
1955   if (!next_block->has_pre_order()) {
1956     // Assign "pre_order" as each new block is taken from the work list.
1957     // This number may be used by following phases to order block visits.
1958     assert(!have_block_count(), "must not have mapped blocks yet")
1959     next_block->set_pre_order(_next_pre_order++);
1960   }
1961   return next_block;
1962 }
1963 
1964 // ------------------------------------------------------------------
1965 // ciTypeFlow::add_to_work_list
1966 //
1967 // Add a basic block to our work list.
1968 void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) {
1969   assert(!block->is_on_work_list(), "must not already be on work list");
1970 
1971   if (CITraceTypeFlow) {
1972     tty->print(">> Adding block%s ", block->has_pre_order() ? " (again)" : "");
1973     block->print_value_on(tty);
1974     tty->print_cr(" to the work list : ");
1975   }
1976 
1977   block->set_on_work_list(true);
1978   if (block->is_simpler_than(_work_list)) {
1979     block->set_next(_work_list);
1980     _work_list = block;
1981   } else {
1982     Block *temp = _work_list;
1983     while (!block->is_simpler_than(temp->next())) {
1984       if (CITraceTypeFlow) {
1985         tty->print(".");
1986       }
1987       temp = temp->next();
1988     }
1989     block->set_next(temp->next());
1990     temp->set_next(block);
1991   }
1992   if (CITraceTypeFlow) {
1993     tty->cr();
1994   }
1995 }
1996 
1997 // ------------------------------------------------------------------
1998 // ciTypeFlow::block_at
1999 //
2000 // Return the block beginning at bci which has a JsrSet compatible
2001 // with jsrs.
2002 ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2003   // First find the right ciBlock.
2004   if (CITraceTypeFlow) {
2005     tty->print(">> Requesting block for %d/", bci);
2006     jsrs->print_on(tty);
2007     tty->cr();
2008   }
2009 
2010   ciBlock* ciblk = _methodBlocks->block_containing(bci);
2011   assert(ciblk->start_bci() == bci, "bad ciBlock boundaries");
2012   Block* block = get_block_for(ciblk->index(), jsrs, option);
2013 
2014   assert(block == NULL? (option == no_create): block->is_private_copy() == (option == create_private_copy), "create option consistent with result");
2015 
2016   if (CITraceTypeFlow) {
2017     if (block != NULL) {
2018       tty->print(">> Found block ");
2019       block->print_value_on(tty);
2020       tty->cr();
2021     } else {
2022       tty->print_cr(">> No such block.");
2023     }
2024   }
2025   
2026   return block;
2027 }
2028  
2029 // ------------------------------------------------------------------
2030 // ciTypeFlow::make_jsr_record
2031 //
2032 // Make a JsrRecord for a given (entry, return) pair, if such a record
2033 // does not already exist.
2034 ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address,
2035                                                    int return_address) {
2036   if (_jsr_records == NULL) {
2037     _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(),
2038                                                            _jsr_count,
2039                                                            0,
2040                                                            NULL);
2041   }
2042   JsrRecord* record = NULL;
2043   int len = _jsr_records->length();
2044   for (int i = 0; i < len; i++) {
2045     JsrRecord* record = _jsr_records->at(i);
2046     if (record->entry_address() == entry_address &&
2047         record->return_address() == return_address) {
2048       return record;
2049     }
2050   }
2051   
2052   record = new (arena()) JsrRecord(entry_address, return_address);
2053   _jsr_records->append(record);
2054   return record;
2055 }
2056  
2057 // ------------------------------------------------------------------
2058 // ciTypeFlow::flow_exceptions
2059 //
2060 // Merge the current state into all exceptional successors at the
2061 // current point in the code.
2062 void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions,
2063                                  GrowableArray<ciInstanceKlass*>* exc_klasses,
2064                                  ciTypeFlow::StateVector* state) {
2065   int len = exceptions->length();
2066   assert(exc_klasses->length() == len, "must have same length");
2067   for (int i = 0; i < len; i++) {
2068     Block* block = exceptions->at(i);
2069     ciInstanceKlass* exception_klass = exc_klasses->at(i);
2070 
2071     if (!exception_klass->is_loaded()) {
2072       // Do not compile any code for unloaded exception types.
2073       // Following compiler passes are responsible for doing this also.
2074       continue;
2075     }
2076 
2077     if (block->meet_exception(exception_klass, state)) {
2078       // Block was modified.  Add it to the work list.
2079       if (!block->is_on_work_list()) {
2080         add_to_work_list(block);
2081       }
2082     }
2083   }    
2084 }
2085 
2086 // ------------------------------------------------------------------
2087 // ciTypeFlow::flow_successors
2088 //
2089 // Merge the current state into all successors at the current point
2090 // in the code.
2091 void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors,
2092                                  ciTypeFlow::StateVector* state) {
2093   int len = successors->length();
2094   for (int i = 0; i < len; i++) {
2095     Block* block = successors->at(i);
2096     if (block->meet(state)) {
2097       // Block was modified.  Add it to the work list.
2098       if (!block->is_on_work_list()) {
2099         add_to_work_list(block);
2100       }
2101     }
2102   }
2103 }
2104 
2105 // ------------------------------------------------------------------
2106 // ciTypeFlow::can_trap
2107 //
2108 // Tells if a given instruction is able to generate an exception edge.
2109 bool ciTypeFlow::can_trap(ciBytecodeStream& str) {
2110   // Cf. GenerateOopMap::do_exception_edge.
2111   if (!Bytecodes::can_trap(str.cur_bc()))  return false;
2112 
2113   switch (str.cur_bc()) {
2114     case Bytecodes::_ldc:
2115     case Bytecodes::_ldc_w:
2116     case Bytecodes::_ldc2_w:
2117     case Bytecodes::_aload_0:
2118       // These bytecodes can trap for rewriting.  We need to assume that
2119       // they do not throw exceptions to make the monitor analysis work.
2120       return false;
2121 
2122     case Bytecodes::_ireturn:
2123     case Bytecodes::_lreturn:
2124     case Bytecodes::_freturn:
2125     case Bytecodes::_dreturn:
2126     case Bytecodes::_areturn:
2127     case Bytecodes::_return:
2128       // We can assume the monitor stack is empty in this analysis.
2129       return false;
2130 
2131     case Bytecodes::_monitorexit:
2132       // We can assume monitors are matched in this analysis.
2133       return false;
2134   }
2135 
2136   return true;
2137 }
2138 
2139 
2140 // ------------------------------------------------------------------
2141 // ciTypeFlow::flow_block
2142 //
2143 // Interpret the effects of the bytecodes on the incoming state
2144 // vector of a basic block.  Push the changed state to succeeding
2145 // basic blocks.
2146 void ciTypeFlow::flow_block(ciTypeFlow::Block* block,
2147                             ciTypeFlow::StateVector* state,
2148                             ciTypeFlow::JsrSet* jsrs) {
2149   if (CITraceTypeFlow) {
2150     tty->print("\n>> ANALYZING BLOCK : ");
2151     tty->cr();
2152     block->print_on(tty);
2153   }
2154   assert(block->has_pre_order(), "pre-order is assigned before 1st flow");
2155   
2156   int start = block->start();
2157   int limit = block->limit();
2158   int control = block->control();
2159   if (control != ciBlock::fall_through_bci) {
2160     limit = control;
2161   }
2162 
2163   // Grab the state from the current block.
2164   block->copy_state_into(state);
2165 
2166   GrowableArray<Block*>*           exceptions = block->exceptions();
2167   GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses();
2168   bool has_exceptions = exceptions->length() > 0;
2169 
2170   ciBytecodeStream str(method());
2171   str.reset_to_bci(start);
2172   Bytecodes::Code code;
2173   while ((code = str.next()) != ciBytecodeStream::EOBC() &&
2174          str.cur_bci() < limit) {
2175     // Check for exceptional control flow from this point.
2176     if (has_exceptions && can_trap(str)) {
2177       flow_exceptions(exceptions, exc_klasses, state);
2178     }
2179     // Apply the effects of the current bytecode to our state.
2180     bool res = state->apply_one_bytecode(&str);
2181 
2182     // Watch for bailouts.
2183     if (failing())  return;
2184 
2185     if (res) {
2186 
2187       // We have encountered a trap.  Record it in this block.
2188       block->set_trap(state->trap_bci(), state->trap_index());
2189       
2190       if (CITraceTypeFlow) {
2191         tty->print_cr(">> Found trap");
2192         block->print_on(tty);
2193       }
2194 
2195       // Record (no) successors.
2196       block->successors(&str, state, jsrs);
2197 
2198       // Discontinue interpretation of this Block.
2199       return;
2200     }
2201   }
2202 
2203   GrowableArray<Block*>* successors = NULL;
2204   if (control != ciBlock::fall_through_bci) {
2205     // Check for exceptional control flow from this point.
2206     if (has_exceptions && can_trap(str)) {
2207       flow_exceptions(exceptions, exc_klasses, state);
2208     }
2209 
2210     // Fix the JsrSet to reflect effect of the bytecode.
2211     block->copy_jsrs_into(jsrs);
2212     jsrs->apply_control(this, &str, state);
2213 
2214     // Find successor edges based on old state and new JsrSet.
2215     successors = block->successors(&str, state, jsrs);
2216 
2217     // Apply the control changes to the state.
2218     state->apply_one_bytecode(&str);
2219   } else {
2220     // Fall through control
2221     successors = block->successors(&str, NULL, NULL);
2222   }
2223 
2224   // Pass our state to successors.
2225   flow_successors(successors, state);
2226 }
2227 
2228 // ------------------------------------------------------------------
2229 // ciTypeFlow::flow_types
2230 //
2231 // Perform the type flow analysis, creating and cloning Blocks as
2232 // necessary.
2233 void ciTypeFlow::flow_types() {
2234   ResourceMark rm;
2235   StateVector* temp_vector = new StateVector(this);
2236   JsrSet* temp_set = new JsrSet(NULL, 16);
2237 
2238   // Create the method entry block.
2239   Block* block = block_at(start_bci(), temp_set);
2240   block->set_pre_order(_next_pre_order++);
2241   assert(block->is_start(), "start block must have order #0");
2242 
2243   // Load the initial state into it.
2244   const StateVector* start_state = get_start_state();
2245   if (failing())  return;
2246   block->meet(start_state);
2247   add_to_work_list(block);
2248 
2249   // Trickle away.
2250   while (!work_list_empty()) {
2251     Block* block = work_list_next();
2252     flow_block(block, temp_vector, temp_set);
2253 
2254 
2255     // NodeCountCutoff is the number of nodes at which the parser
2256     // will bail out.  Probably if we already have lots of BBs,
2257     // the parser will generate at least twice that many nodes and bail out.
2258     // Therefore, this is a conservatively large limit at which to
2259     // bail out in the pre-parse typeflow pass.
2260     int block_limit = MaxNodeLimit / 2;
2261 
2262     if (_next_pre_order >= block_limit) {
2263       // Too many basic blocks.  Bail out.
2264       //
2265       // This can happen when try/finally constructs are nested to depth N,
2266       // and there is O(2**N) cloning of jsr bodies.  See bug 4697245!
2267       record_failure("too many basic blocks");
2268       return;
2269     }
2270 
2271     // Watch for bailouts.
2272     if (failing())  return;
2273   }
2274 }
2275 
2276 // ------------------------------------------------------------------
2277 // ciTypeFlow::map_blocks
2278 //
2279 // Create the block map, which indexes blocks in pre_order.
2280 void ciTypeFlow::map_blocks() {
2281   assert(_block_map == NULL, "single initialization");
2282   int pre_order_limit = _next_pre_order;
2283   _block_map = NEW_ARENA_ARRAY(arena(), Block*, pre_order_limit);
2284   assert(pre_order_limit == block_count(), "");
2285   int po;
2286   for (po = 0; po < pre_order_limit; po++) {
2287     debug_only(_block_map[po] = NULL);
2288   }
2289   ciMethodBlocks *mblks = _methodBlocks;
2290   ciBlock* current = NULL;
2291   int limit_bci = code_size();
2292   for (int bci = 0; bci < limit_bci; bci++) {
2293     ciBlock* ciblk = mblks->block_containing(bci);
2294     if (ciblk != NULL && ciblk != current) {
2295       current = ciblk;
2296       int curidx = ciblk->index();
2297       int block_count = (_idx_to_blocklist[curidx] == NULL) ? 0 : _idx_to_blocklist[curidx]->length();
2298       for (int i = 0; i < block_count; i++) {
2299         Block* block = _idx_to_blocklist[curidx]->at(i);
2300         if (!block->has_pre_order())  continue;
2301         int po = block->pre_order();
2302         assert(_block_map[po] == NULL, "unique ref to block");
2303         assert(0 <= po && po < pre_order_limit, "");
2304         _block_map[po] = block;
2305       }
2306     }
2307   }
2308   for (po = 0; po < pre_order_limit; po++) {
2309     assert(_block_map[po] != NULL, "must not drop any blocks");
2310     Block* block = _block_map[po];
2311     // Remove dead blocks from successor lists:
2312     for (int e = 0; e <= 1; e++) {
2313       GrowableArray<Block*>* l = e? block->exceptions(): block->successors();
2314       for (int i = 0; i < l->length(); i++) {
2315         Block* s = l->at(i);
2316         if (!s->has_pre_order()) {
2317           if (CITraceTypeFlow) {
2318             tty->print("Removing dead %s successor of #%d: ", (e? "exceptional":  "normal"), block->pre_order());
2319             s->print_value_on(tty);
2320             tty->cr();
2321           }
2322           l->remove(s);
2323           --i;
2324         }
2325       }
2326     }
2327   }
2328 }
2329 
2330 // ------------------------------------------------------------------
2331 // ciTypeFlow::get_block_for
2332 //
2333 // Find a block with this ciBlock which has a compatible JsrSet.
2334 // If no such block exists, create it, unless the option is no_create.
2335 // If the option is create_private_copy, always create a fresh private copy.
2336 ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
2337   Arena* a = arena();
2338   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
2339   if (blocks == NULL) {
2340     // Query only?
2341     if (option == no_create)  return NULL;
2342 
2343     // Allocate the growable array.
2344     blocks = new (a) GrowableArray<Block*>(a, 4, 0, NULL);
2345     _idx_to_blocklist[ciBlockIndex] = blocks;
2346   }
2347 
2348   if (option != create_private_copy) {
2349     int len = blocks->length();
2350     for (int i = 0; i < len; i++) {
2351       Block* block = blocks->at(i);
2352       if (!block->is_private_copy() && block->is_compatible_with(jsrs)) {
2353         return block;
2354       }
2355     }
2356   }
2357 
2358   // Query only?
2359   if (option == no_create)  return NULL;
2360 
2361   // We did not find a compatible block.  Create one.
2362   Block* new_block = new (a) Block(this, _methodBlocks->block(ciBlockIndex), jsrs);
2363   if (option == create_private_copy)  new_block->set_private_copy(true);
2364   blocks->append(new_block);
2365   return new_block;
2366 }
2367 
2368 // ------------------------------------------------------------------
2369 // ciTypeFlow::private_copy_count
2370 //
2371 int ciTypeFlow::private_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const {
2372   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
2373 
2374   if (blocks == NULL) {
2375     return 0;
2376   }
2377 
2378   int count = 0;
2379   int len = blocks->length();
2380   for (int i = 0; i < len; i++) {
2381     Block* block = blocks->at(i);
2382     if (block->is_private_copy() && block->is_compatible_with(jsrs)) {
2383       count++;
2384     }
2385   }
2386 
2387   return count;
2388 }
2389 
2390 // ------------------------------------------------------------------
2391 // ciTypeFlow::do_flow
2392 //
2393 // Perform type inference flow analysis.
2394 void ciTypeFlow::do_flow() {
2395   if (CITraceTypeFlow) {
2396     tty->print_cr("\nPerforming flow analysis on method");
2397     method()->print();
2398     if (is_osr_flow())  tty->print(" at OSR bci %d", start_bci());
2399     tty->cr();
2400     method()->print_codes();
2401   }
2402   if (CITraceTypeFlow) {
2403     tty->print_cr("Initial CI Blocks");
2404     print_on(tty);
2405   }
2406   flow_types();
2407   // Watch for bailouts.
2408   if (failing()) {
2409     return;
2410   }
2411   if (CIPrintTypeFlow || CITraceTypeFlow) {
2412     print_on(tty);
2413   }
2414   map_blocks();
2415 }
2416 
2417 // ------------------------------------------------------------------
2418 // ciTypeFlow::record_failure()
2419 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
2420 // This is required because there is not a 1-1 relation between the ciEnv and
2421 // the TypeFlow passes within a compilation task.  For example, if the compiler
2422 // is considering inlining a method, it will request a TypeFlow.  If that fails,
2423 // the compilation as a whole may continue without the inlining.  Some TypeFlow
2424 // requests are not optional; if they fail the requestor is responsible for
2425 // copying the failure reason up to the ciEnv.  (See Parse::Parse.)
2426 void ciTypeFlow::record_failure(const char* reason) {
2427   if (env()->log() != NULL) {
2428     env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
2429   }
2430   if (_failure_reason == NULL) {
2431     // Record the first failure reason.
2432     _failure_reason = reason;
2433   }
2434 }
2435 
2436 #ifndef PRODUCT
2437 // ------------------------------------------------------------------
2438 // ciTypeFlow::print_on
2439 void ciTypeFlow::print_on(outputStream* st) const {
2440   // Walk through CI blocks
2441   st->print_cr("********************************************************");
2442   st->print   ("TypeFlow for ");
2443   method()->name()->print_symbol_on(st);
2444   int limit_bci = code_size();
2445   st->print_cr("  %d bytes", limit_bci);
2446   ciMethodBlocks  *mblks = _methodBlocks;
2447   ciBlock* current = NULL;
2448   for (int bci = 0; bci < limit_bci; bci++) {
2449     ciBlock* blk = mblks->block_containing(bci);
2450     if (blk != NULL && blk != current) {
2451       current = blk;
2452       current->print_on(st);
2453 
2454       GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()];
2455       int num_blocks = (blocks == NULL) ? 0 : blocks->length();
2456 
2457       if (num_blocks == 0) {
2458         st->print_cr("  No Blocks");
2459       } else {
2460         for (int i = 0; i < num_blocks; i++) {
2461           Block* block = blocks->at(i);
2462           block->print_on(st);
2463         }
2464       }
2465       st->print_cr("--------------------------------------------------------");
2466       st->cr();
2467     }
2468   }
2469   st->print_cr("********************************************************");
2470   st->cr();
2471 }
2472 #endif