src/share/vm/ci/ciTypeFlow.cpp

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 905
ad8c8ca4ab0f
child 1101
ebebd376f657
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

     1 /*
     2  * Copyright 2000-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_ciTypeFlow.cpp.incl"
    28 // ciTypeFlow::JsrSet
    29 //
    30 // A JsrSet represents some set of JsrRecords.  This class
    31 // is used to record a set of all jsr routines which we permit
    32 // execution to return (ret) from.
    33 //
    34 // During abstract interpretation, JsrSets are used to determine
    35 // whether two paths which reach a given block are unique, and
    36 // should be cloned apart, or are compatible, and should merge
    37 // together.
    39 // ------------------------------------------------------------------
    40 // ciTypeFlow::JsrSet::JsrSet
    41 ciTypeFlow::JsrSet::JsrSet(Arena* arena, int default_len) {
    42   if (arena != NULL) {
    43     // Allocate growable array in Arena.
    44     _set = new (arena) GrowableArray<JsrRecord*>(arena, default_len, 0, NULL);
    45   } else {
    46     // Allocate growable array in current ResourceArea.
    47     _set = new GrowableArray<JsrRecord*>(4, 0, NULL, false);
    48   }
    49 }
    51 // ------------------------------------------------------------------
    52 // ciTypeFlow::JsrSet::copy_into
    53 void ciTypeFlow::JsrSet::copy_into(JsrSet* jsrs) {
    54   int len = size();
    55   jsrs->_set->clear();
    56   for (int i = 0; i < len; i++) {
    57     jsrs->_set->append(_set->at(i));
    58   }
    59 }
    61 // ------------------------------------------------------------------
    62 // ciTypeFlow::JsrSet::is_compatible_with
    63 //
    64 // !!!! MISGIVINGS ABOUT THIS... disregard
    65 //
    66 // Is this JsrSet compatible with some other JsrSet?
    67 //
    68 // In set-theoretic terms, a JsrSet can be viewed as a partial function
    69 // from entry addresses to return addresses.  Two JsrSets A and B are
    70 // compatible iff
    71 //
    72 //   For any x,
    73 //   A(x) defined and B(x) defined implies A(x) == B(x)
    74 //
    75 // Less formally, two JsrSets are compatible when they have identical
    76 // return addresses for any entry addresses they share in common.
    77 bool ciTypeFlow::JsrSet::is_compatible_with(JsrSet* other) {
    78   // Walk through both sets in parallel.  If the same entry address
    79   // appears in both sets, then the return address must match for
    80   // the sets to be compatible.
    81   int size1 = size();
    82   int size2 = other->size();
    84   // Special case.  If nothing is on the jsr stack, then there can
    85   // be no ret.
    86   if (size2 == 0) {
    87     return true;
    88   } else if (size1 != size2) {
    89     return false;
    90   } else {
    91     for (int i = 0; i < size1; i++) {
    92       JsrRecord* record1 = record_at(i);
    93       JsrRecord* record2 = other->record_at(i);
    94       if (record1->entry_address() != record2->entry_address() ||
    95           record1->return_address() != record2->return_address()) {
    96         return false;
    97       }
    98     }
    99     return true;
   100   }
   102 #if 0
   103   int pos1 = 0;
   104   int pos2 = 0;
   105   int size1 = size();
   106   int size2 = other->size();
   107   while (pos1 < size1 && pos2 < size2) {
   108     JsrRecord* record1 = record_at(pos1);
   109     JsrRecord* record2 = other->record_at(pos2);
   110     int entry1 = record1->entry_address();
   111     int entry2 = record2->entry_address();
   112     if (entry1 < entry2) {
   113       pos1++;
   114     } else if (entry1 > entry2) {
   115       pos2++;
   116     } else {
   117       if (record1->return_address() == record2->return_address()) {
   118         pos1++;
   119         pos2++;
   120       } else {
   121         // These two JsrSets are incompatible.
   122         return false;
   123       }
   124     }
   125   }
   126   // The two JsrSets agree.
   127   return true;
   128 #endif
   129 }
   131 // ------------------------------------------------------------------
   132 // ciTypeFlow::JsrSet::insert_jsr_record
   133 //
   134 // Insert the given JsrRecord into the JsrSet, maintaining the order
   135 // of the set and replacing any element with the same entry address.
   136 void ciTypeFlow::JsrSet::insert_jsr_record(JsrRecord* record) {
   137   int len = size();
   138   int entry = record->entry_address();
   139   int pos = 0;
   140   for ( ; pos < len; pos++) {
   141     JsrRecord* current = record_at(pos);
   142     if (entry == current->entry_address()) {
   143       // Stomp over this entry.
   144       _set->at_put(pos, record);
   145       assert(size() == len, "must be same size");
   146       return;
   147     } else if (entry < current->entry_address()) {
   148       break;
   149     }
   150   }
   152   // Insert the record into the list.
   153   JsrRecord* swap = record;
   154   JsrRecord* temp = NULL;
   155   for ( ; pos < len; pos++) {
   156     temp = _set->at(pos);
   157     _set->at_put(pos, swap);
   158     swap = temp;
   159   }
   160   _set->append(swap);
   161   assert(size() == len+1, "must be larger");
   162 }
   164 // ------------------------------------------------------------------
   165 // ciTypeFlow::JsrSet::remove_jsr_record
   166 //
   167 // Remove the JsrRecord with the given return address from the JsrSet.
   168 void ciTypeFlow::JsrSet::remove_jsr_record(int return_address) {
   169   int len = size();
   170   for (int i = 0; i < len; i++) {
   171     if (record_at(i)->return_address() == return_address) {
   172       // We have found the proper entry.  Remove it from the
   173       // JsrSet and exit.
   174       for (int j = i+1; j < len ; j++) {
   175         _set->at_put(j-1, _set->at(j));
   176       }
   177       _set->trunc_to(len-1);
   178       assert(size() == len-1, "must be smaller");
   179       return;
   180     }
   181   }
   182   assert(false, "verify: returning from invalid subroutine");
   183 }
   185 // ------------------------------------------------------------------
   186 // ciTypeFlow::JsrSet::apply_control
   187 //
   188 // Apply the effect of a control-flow bytecode on the JsrSet.  The
   189 // only bytecodes that modify the JsrSet are jsr and ret.
   190 void ciTypeFlow::JsrSet::apply_control(ciTypeFlow* analyzer,
   191                                        ciBytecodeStream* str,
   192                                        ciTypeFlow::StateVector* state) {
   193   Bytecodes::Code code = str->cur_bc();
   194   if (code == Bytecodes::_jsr) {
   195     JsrRecord* record =
   196       analyzer->make_jsr_record(str->get_dest(), str->next_bci());
   197     insert_jsr_record(record);
   198   } else if (code == Bytecodes::_jsr_w) {
   199     JsrRecord* record =
   200       analyzer->make_jsr_record(str->get_far_dest(), str->next_bci());
   201     insert_jsr_record(record);
   202   } else if (code == Bytecodes::_ret) {
   203     Cell local = state->local(str->get_index());
   204     ciType* return_address = state->type_at(local);
   205     assert(return_address->is_return_address(), "verify: wrong type");
   206     if (size() == 0) {
   207       // Ret-state underflow:  Hit a ret w/o any previous jsrs.  Bail out.
   208       // This can happen when a loop is inside a finally clause (4614060).
   209       analyzer->record_failure("OSR in finally clause");
   210       return;
   211     }
   212     remove_jsr_record(return_address->as_return_address()->bci());
   213   }
   214 }
   216 #ifndef PRODUCT
   217 // ------------------------------------------------------------------
   218 // ciTypeFlow::JsrSet::print_on
   219 void ciTypeFlow::JsrSet::print_on(outputStream* st) const {
   220   st->print("{ ");
   221   int num_elements = size();
   222   if (num_elements > 0) {
   223     int i = 0;
   224     for( ; i < num_elements - 1; i++) {
   225       _set->at(i)->print_on(st);
   226       st->print(", ");
   227     }
   228     _set->at(i)->print_on(st);
   229     st->print(" ");
   230   }
   231   st->print("}");
   232 }
   233 #endif
   235 // ciTypeFlow::StateVector
   236 //
   237 // A StateVector summarizes the type information at some point in
   238 // the program.
   240 // ------------------------------------------------------------------
   241 // ciTypeFlow::StateVector::type_meet
   242 //
   243 // Meet two types.
   244 //
   245 // The semi-lattice of types use by this analysis are modeled on those
   246 // of the verifier.  The lattice is as follows:
   247 //
   248 //        top_type() >= all non-extremal types >= bottom_type
   249 //                             and
   250 //   Every primitive type is comparable only with itself.  The meet of
   251 //   reference types is determined by their kind: instance class,
   252 //   interface, or array class.  The meet of two types of the same
   253 //   kind is their least common ancestor.  The meet of two types of
   254 //   different kinds is always java.lang.Object.
   255 ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
   256   assert(t1 != t2, "checked in caller");
   257   if (t1->equals(top_type())) {
   258     return t2;
   259   } else if (t2->equals(top_type())) {
   260     return t1;
   261   } else if (t1->is_primitive_type() || t2->is_primitive_type()) {
   262     // Special case null_type.  null_type meet any reference type T
   263     // is T.  null_type meet null_type is null_type.
   264     if (t1->equals(null_type())) {
   265       if (!t2->is_primitive_type() || t2->equals(null_type())) {
   266         return t2;
   267       }
   268     } else if (t2->equals(null_type())) {
   269       if (!t1->is_primitive_type()) {
   270         return t1;
   271       }
   272     }
   274     // At least one of the two types is a non-top primitive type.
   275     // The other type is not equal to it.  Fall to bottom.
   276     return bottom_type();
   277   } else {
   278     // Both types are non-top non-primitive types.  That is,
   279     // both types are either instanceKlasses or arrayKlasses.
   280     ciKlass* object_klass = analyzer->env()->Object_klass();
   281     ciKlass* k1 = t1->as_klass();
   282     ciKlass* k2 = t2->as_klass();
   283     if (k1->equals(object_klass) || k2->equals(object_klass)) {
   284       return object_klass;
   285     } else if (!k1->is_loaded() || !k2->is_loaded()) {
   286       // Unloaded classes fall to java.lang.Object at a merge.
   287       return object_klass;
   288     } else if (k1->is_interface() != k2->is_interface()) {
   289       // When an interface meets a non-interface, we get Object;
   290       // This is what the verifier does.
   291       return object_klass;
   292     } else if (k1->is_array_klass() || k2->is_array_klass()) {
   293       // When an array meets a non-array, we get Object.
   294       // When objArray meets typeArray, we also get Object.
   295       // And when typeArray meets different typeArray, we again get Object.
   296       // But when objArray meets objArray, we look carefully at element types.
   297       if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) {
   298         // Meet the element types, then construct the corresponding array type.
   299         ciKlass* elem1 = k1->as_obj_array_klass()->element_klass();
   300         ciKlass* elem2 = k2->as_obj_array_klass()->element_klass();
   301         ciKlass* elem  = type_meet_internal(elem1, elem2, analyzer)->as_klass();
   302         // Do an easy shortcut if one type is a super of the other.
   303         if (elem == elem1) {
   304           assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK");
   305           return k1;
   306         } else if (elem == elem2) {
   307           assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK");
   308           return k2;
   309         } else {
   310           return ciObjArrayKlass::make(elem);
   311         }
   312       } else {
   313         return object_klass;
   314       }
   315     } else {
   316       // Must be two plain old instance klasses.
   317       assert(k1->is_instance_klass(), "previous cases handle non-instances");
   318       assert(k2->is_instance_klass(), "previous cases handle non-instances");
   319       return k1->least_common_ancestor(k2);
   320     }
   321   }
   322 }
   325 // ------------------------------------------------------------------
   326 // ciTypeFlow::StateVector::StateVector
   327 //
   328 // Build a new state vector
   329 ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
   330   _outer = analyzer;
   331   _stack_size = -1;
   332   _monitor_count = -1;
   333   // Allocate the _types array
   334   int max_cells = analyzer->max_cells();
   335   _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
   336   for (int i=0; i<max_cells; i++) {
   337     _types[i] = top_type();
   338   }
   339   _trap_bci = -1;
   340   _trap_index = 0;
   341   _def_locals.clear();
   342 }
   345 // ------------------------------------------------------------------
   346 // ciTypeFlow::get_start_state
   347 //
   348 // Set this vector to the method entry state.
   349 const ciTypeFlow::StateVector* ciTypeFlow::get_start_state() {
   350   StateVector* state = new StateVector(this);
   351   if (is_osr_flow()) {
   352     ciTypeFlow* non_osr_flow = method()->get_flow_analysis();
   353     if (non_osr_flow->failing()) {
   354       record_failure(non_osr_flow->failure_reason());
   355       return NULL;
   356     }
   357     JsrSet* jsrs = new JsrSet(NULL, 16);
   358     Block* non_osr_block = non_osr_flow->existing_block_at(start_bci(), jsrs);
   359     if (non_osr_block == NULL) {
   360       record_failure("cannot reach OSR point");
   361       return NULL;
   362     }
   363     // load up the non-OSR state at this point
   364     non_osr_block->copy_state_into(state);
   365     int non_osr_start = non_osr_block->start();
   366     if (non_osr_start != start_bci()) {
   367       // must flow forward from it
   368       if (CITraceTypeFlow) {
   369         tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
   370       }
   371       Block* block = block_at(non_osr_start, jsrs);
   372       assert(block->limit() == start_bci(), "must flow forward to start");
   373       flow_block(block, state, jsrs);
   374     }
   375     return state;
   376     // Note:  The code below would be an incorrect for an OSR flow,
   377     // even if it were possible for an OSR entry point to be at bci zero.
   378   }
   379   // "Push" the method signature into the first few locals.
   380   state->set_stack_size(-max_locals());
   381   if (!method()->is_static()) {
   382     state->push(method()->holder());
   383     assert(state->tos() == state->local(0), "");
   384   }
   385   for (ciSignatureStream str(method()->signature());
   386        !str.at_return_type();
   387        str.next()) {
   388     state->push_translate(str.type());
   389   }
   390   // Set the rest of the locals to bottom.
   391   Cell cell = state->next_cell(state->tos());
   392   state->set_stack_size(0);
   393   int limit = state->limit_cell();
   394   for (; cell < limit; cell = state->next_cell(cell)) {
   395     state->set_type_at(cell, state->bottom_type());
   396   }
   397   // Lock an object, if necessary.
   398   state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
   399   return state;
   400 }
   402 // ------------------------------------------------------------------
   403 // ciTypeFlow::StateVector::copy_into
   404 //
   405 // Copy our value into some other StateVector
   406 void ciTypeFlow::StateVector::copy_into(ciTypeFlow::StateVector* copy)
   407 const {
   408   copy->set_stack_size(stack_size());
   409   copy->set_monitor_count(monitor_count());
   410   Cell limit = limit_cell();
   411   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   412     copy->set_type_at(c, type_at(c));
   413   }
   414 }
   416 // ------------------------------------------------------------------
   417 // ciTypeFlow::StateVector::meet
   418 //
   419 // Meets this StateVector with another, destructively modifying this
   420 // one.  Returns true if any modification takes place.
   421 bool ciTypeFlow::StateVector::meet(const ciTypeFlow::StateVector* incoming) {
   422   if (monitor_count() == -1) {
   423     set_monitor_count(incoming->monitor_count());
   424   }
   425   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
   427   if (stack_size() == -1) {
   428     set_stack_size(incoming->stack_size());
   429     Cell limit = limit_cell();
   430     #ifdef ASSERT
   431     { for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   432         assert(type_at(c) == top_type(), "");
   433     } }
   434     #endif
   435     // Make a simple copy of the incoming state.
   436     for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   437       set_type_at(c, incoming->type_at(c));
   438     }
   439     return true;  // it is always different the first time
   440   }
   441 #ifdef ASSERT
   442   if (stack_size() != incoming->stack_size()) {
   443     _outer->method()->print_codes();
   444     tty->print_cr("!!!! Stack size conflict");
   445     tty->print_cr("Current state:");
   446     print_on(tty);
   447     tty->print_cr("Incoming state:");
   448     ((StateVector*)incoming)->print_on(tty);
   449   }
   450 #endif
   451   assert(stack_size() == incoming->stack_size(), "sanity");
   453   bool different = false;
   454   Cell limit = limit_cell();
   455   for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   456     ciType* t1 = type_at(c);
   457     ciType* t2 = incoming->type_at(c);
   458     if (!t1->equals(t2)) {
   459       ciType* new_type = type_meet(t1, t2);
   460       if (!t1->equals(new_type)) {
   461         set_type_at(c, new_type);
   462         different = true;
   463       }
   464     }
   465   }
   466   return different;
   467 }
   469 // ------------------------------------------------------------------
   470 // ciTypeFlow::StateVector::meet_exception
   471 //
   472 // Meets this StateVector with another, destructively modifying this
   473 // one.  The incoming state is coming via an exception.  Returns true
   474 // if any modification takes place.
   475 bool ciTypeFlow::StateVector::meet_exception(ciInstanceKlass* exc,
   476                                      const ciTypeFlow::StateVector* incoming) {
   477   if (monitor_count() == -1) {
   478     set_monitor_count(incoming->monitor_count());
   479   }
   480   assert(monitor_count() == incoming->monitor_count(), "monitors must match");
   482   if (stack_size() == -1) {
   483     set_stack_size(1);
   484   }
   486   assert(stack_size() ==  1, "must have one-element stack");
   488   bool different = false;
   490   // Meet locals from incoming array.
   491   Cell limit = local(_outer->max_locals()-1);
   492   for (Cell c = start_cell(); c <= limit; c = next_cell(c)) {
   493     ciType* t1 = type_at(c);
   494     ciType* t2 = incoming->type_at(c);
   495     if (!t1->equals(t2)) {
   496       ciType* new_type = type_meet(t1, t2);
   497       if (!t1->equals(new_type)) {
   498         set_type_at(c, new_type);
   499         different = true;
   500       }
   501     }
   502   }
   504   // Handle stack separately.  When an exception occurs, the
   505   // only stack entry is the exception instance.
   506   ciType* tos_type = type_at_tos();
   507   if (!tos_type->equals(exc)) {
   508     ciType* new_type = type_meet(tos_type, exc);
   509     if (!tos_type->equals(new_type)) {
   510       set_type_at_tos(new_type);
   511       different = true;
   512     }
   513   }
   515   return different;
   516 }
   518 // ------------------------------------------------------------------
   519 // ciTypeFlow::StateVector::push_translate
   520 void ciTypeFlow::StateVector::push_translate(ciType* type) {
   521   BasicType basic_type = type->basic_type();
   522   if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
   523       basic_type == T_BYTE    || basic_type == T_SHORT) {
   524     push_int();
   525   } else {
   526     push(type);
   527     if (type->is_two_word()) {
   528       push(half_type(type));
   529     }
   530   }
   531 }
   533 // ------------------------------------------------------------------
   534 // ciTypeFlow::StateVector::do_aaload
   535 void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
   536   pop_int();
   537   ciObjArrayKlass* array_klass = pop_objArray();
   538   if (array_klass == NULL) {
   539     // Did aaload on a null reference; push a null and ignore the exception.
   540     // This instruction will never continue normally.  All we have to do
   541     // is report a value that will meet correctly with any downstream
   542     // reference types on paths that will truly be executed.  This null type
   543     // meets with any reference type to yield that same reference type.
   544     // (The compiler will generate an unconditional exception here.)
   545     push(null_type());
   546     return;
   547   }
   548   if (!array_klass->is_loaded()) {
   549     // Only fails for some -Xcomp runs
   550     trap(str, array_klass,
   551          Deoptimization::make_trap_request
   552          (Deoptimization::Reason_unloaded,
   553           Deoptimization::Action_reinterpret));
   554     return;
   555   }
   556   ciKlass* element_klass = array_klass->element_klass();
   557   if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
   558     Untested("unloaded array element class in ciTypeFlow");
   559     trap(str, element_klass,
   560          Deoptimization::make_trap_request
   561          (Deoptimization::Reason_unloaded,
   562           Deoptimization::Action_reinterpret));
   563   } else {
   564     push_object(element_klass);
   565   }
   566 }
   569 // ------------------------------------------------------------------
   570 // ciTypeFlow::StateVector::do_checkcast
   571 void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
   572   bool will_link;
   573   ciKlass* klass = str->get_klass(will_link);
   574   if (!will_link) {
   575     // VM's interpreter will not load 'klass' if object is NULL.
   576     // Type flow after this block may still be needed in two situations:
   577     // 1) C2 uses do_null_assert() and continues compilation for later blocks
   578     // 2) C2 does an OSR compile in a later block (see bug 4778368).
   579     pop_object();
   580     do_null_assert(klass);
   581   } else {
   582     pop_object();
   583     push_object(klass);
   584   }
   585 }
   587 // ------------------------------------------------------------------
   588 // ciTypeFlow::StateVector::do_getfield
   589 void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
   590   // could add assert here for type of object.
   591   pop_object();
   592   do_getstatic(str);
   593 }
   595 // ------------------------------------------------------------------
   596 // ciTypeFlow::StateVector::do_getstatic
   597 void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
   598   bool will_link;
   599   ciField* field = str->get_field(will_link);
   600   if (!will_link) {
   601     trap(str, field->holder(), str->get_field_holder_index());
   602   } else {
   603     ciType* field_type = field->type();
   604     if (!field_type->is_loaded()) {
   605       // Normally, we need the field's type to be loaded if we are to
   606       // do anything interesting with its value.
   607       // We used to do this:  trap(str, str->get_field_signature_index());
   608       //
   609       // There is one good reason not to trap here.  Execution can
   610       // get past this "getfield" or "getstatic" if the value of
   611       // the field is null.  As long as the value is null, the class
   612       // does not need to be loaded!  The compiler must assume that
   613       // the value of the unloaded class reference is null; if the code
   614       // ever sees a non-null value, loading has occurred.
   615       //
   616       // This actually happens often enough to be annoying.  If the
   617       // compiler throws an uncommon trap at this bytecode, you can
   618       // get an endless loop of recompilations, when all the code
   619       // needs to do is load a series of null values.  Also, a trap
   620       // here can make an OSR entry point unreachable, triggering the
   621       // assert on non_osr_block in ciTypeFlow::get_start_state.
   622       // (See bug 4379915.)
   623       do_null_assert(field_type->as_klass());
   624     } else {
   625       push_translate(field_type);
   626     }
   627   }
   628 }
   630 // ------------------------------------------------------------------
   631 // ciTypeFlow::StateVector::do_invoke
   632 void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
   633                                         bool has_receiver) {
   634   bool will_link;
   635   ciMethod* method = str->get_method(will_link);
   636   if (!will_link) {
   637     // We weren't able to find the method.
   638     ciKlass* unloaded_holder = method->holder();
   639     trap(str, unloaded_holder, str->get_method_holder_index());
   640   } else {
   641     ciSignature* signature = method->signature();
   642     ciSignatureStream sigstr(signature);
   643     int arg_size = signature->size();
   644     int stack_base = stack_size() - arg_size;
   645     int i = 0;
   646     for( ; !sigstr.at_return_type(); sigstr.next()) {
   647       ciType* type = sigstr.type();
   648       ciType* stack_type = type_at(stack(stack_base + i++));
   649       // Do I want to check this type?
   650       // assert(stack_type->is_subtype_of(type), "bad type for field value");
   651       if (type->is_two_word()) {
   652         ciType* stack_type2 = type_at(stack(stack_base + i++));
   653         assert(stack_type2->equals(half_type(type)), "must be 2nd half");
   654       }
   655     }
   656     assert(arg_size == i, "must match");
   657     for (int j = 0; j < arg_size; j++) {
   658       pop();
   659     }
   660     if (has_receiver) {
   661       // Check this?
   662       pop_object();
   663     }
   664     assert(!sigstr.is_done(), "must have return type");
   665     ciType* return_type = sigstr.type();
   666     if (!return_type->is_void()) {
   667       if (!return_type->is_loaded()) {
   668         // As in do_getstatic(), generally speaking, we need the return type to
   669         // be loaded if we are to do anything interesting with its value.
   670         // We used to do this:  trap(str, str->get_method_signature_index());
   671         //
   672         // We do not trap here since execution can get past this invoke if
   673         // the return value is null.  As long as the value is null, the class
   674         // does not need to be loaded!  The compiler must assume that
   675         // the value of the unloaded class reference is null; if the code
   676         // ever sees a non-null value, loading has occurred.
   677         //
   678         // See do_getstatic() for similar explanation, as well as bug 4684993.
   679         do_null_assert(return_type->as_klass());
   680       } else {
   681         push_translate(return_type);
   682       }
   683     }
   684   }
   685 }
   687 // ------------------------------------------------------------------
   688 // ciTypeFlow::StateVector::do_jsr
   689 void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
   690   push(ciReturnAddress::make(str->next_bci()));
   691 }
   693 // ------------------------------------------------------------------
   694 // ciTypeFlow::StateVector::do_ldc
   695 void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
   696   ciConstant con = str->get_constant();
   697   BasicType basic_type = con.basic_type();
   698   if (basic_type == T_ILLEGAL) {
   699     // OutOfMemoryError in the CI while loading constant
   700     push_null();
   701     outer()->record_failure("ldc did not link");
   702     return;
   703   }
   704   if (basic_type == T_OBJECT || basic_type == T_ARRAY) {
   705     ciObject* obj = con.as_object();
   706     if (obj->is_null_object()) {
   707       push_null();
   708     } else if (obj->is_klass()) {
   709       // The type of ldc <class> is java.lang.Class
   710       push_object(outer()->env()->Class_klass());
   711     } else {
   712       push_object(obj->klass());
   713     }
   714   } else {
   715     push_translate(ciType::make(basic_type));
   716   }
   717 }
   719 // ------------------------------------------------------------------
   720 // ciTypeFlow::StateVector::do_multianewarray
   721 void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
   722   int dimensions = str->get_dimensions();
   723   bool will_link;
   724   ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
   725   if (!will_link) {
   726     trap(str, array_klass, str->get_klass_index());
   727   } else {
   728     for (int i = 0; i < dimensions; i++) {
   729       pop_int();
   730     }
   731     push_object(array_klass);
   732   }
   733 }
   735 // ------------------------------------------------------------------
   736 // ciTypeFlow::StateVector::do_new
   737 void ciTypeFlow::StateVector::do_new(ciBytecodeStream* str) {
   738   bool will_link;
   739   ciKlass* klass = str->get_klass(will_link);
   740   if (!will_link || str->is_unresolved_klass()) {
   741     trap(str, klass, str->get_klass_index());
   742   } else {
   743     push_object(klass);
   744   }
   745 }
   747 // ------------------------------------------------------------------
   748 // ciTypeFlow::StateVector::do_newarray
   749 void ciTypeFlow::StateVector::do_newarray(ciBytecodeStream* str) {
   750   pop_int();
   751   ciKlass* klass = ciTypeArrayKlass::make((BasicType)str->get_index());
   752   push_object(klass);
   753 }
   755 // ------------------------------------------------------------------
   756 // ciTypeFlow::StateVector::do_putfield
   757 void ciTypeFlow::StateVector::do_putfield(ciBytecodeStream* str) {
   758   do_putstatic(str);
   759   if (_trap_bci != -1)  return;  // unloaded field holder, etc.
   760   // could add assert here for type of object.
   761   pop_object();
   762 }
   764 // ------------------------------------------------------------------
   765 // ciTypeFlow::StateVector::do_putstatic
   766 void ciTypeFlow::StateVector::do_putstatic(ciBytecodeStream* str) {
   767   bool will_link;
   768   ciField* field = str->get_field(will_link);
   769   if (!will_link) {
   770     trap(str, field->holder(), str->get_field_holder_index());
   771   } else {
   772     ciType* field_type = field->type();
   773     ciType* type = pop_value();
   774     // Do I want to check this type?
   775     //      assert(type->is_subtype_of(field_type), "bad type for field value");
   776     if (field_type->is_two_word()) {
   777       ciType* type2 = pop_value();
   778       assert(type2->is_two_word(), "must be 2nd half");
   779       assert(type == half_type(type2), "must be 2nd half");
   780     }
   781   }
   782 }
   784 // ------------------------------------------------------------------
   785 // ciTypeFlow::StateVector::do_ret
   786 void ciTypeFlow::StateVector::do_ret(ciBytecodeStream* str) {
   787   Cell index = local(str->get_index());
   789   ciType* address = type_at(index);
   790   assert(address->is_return_address(), "bad return address");
   791   set_type_at(index, bottom_type());
   792 }
   794 // ------------------------------------------------------------------
   795 // ciTypeFlow::StateVector::trap
   796 //
   797 // Stop interpretation of this path with a trap.
   798 void ciTypeFlow::StateVector::trap(ciBytecodeStream* str, ciKlass* klass, int index) {
   799   _trap_bci = str->cur_bci();
   800   _trap_index = index;
   802   // Log information about this trap:
   803   CompileLog* log = outer()->env()->log();
   804   if (log != NULL) {
   805     int mid = log->identify(outer()->method());
   806     int kid = (klass == NULL)? -1: log->identify(klass);
   807     log->begin_elem("uncommon_trap method='%d' bci='%d'", mid, str->cur_bci());
   808     char buf[100];
   809     log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf),
   810                                                           index));
   811     if (kid >= 0)
   812       log->print(" klass='%d'", kid);
   813     log->end_elem();
   814   }
   815 }
   817 // ------------------------------------------------------------------
   818 // ciTypeFlow::StateVector::do_null_assert
   819 // Corresponds to graphKit::do_null_assert.
   820 void ciTypeFlow::StateVector::do_null_assert(ciKlass* unloaded_klass) {
   821   if (unloaded_klass->is_loaded()) {
   822     // We failed to link, but we can still compute with this class,
   823     // since it is loaded somewhere.  The compiler will uncommon_trap
   824     // if the object is not null, but the typeflow pass can not assume
   825     // that the object will be null, otherwise it may incorrectly tell
   826     // the parser that an object is known to be null. 4761344, 4807707
   827     push_object(unloaded_klass);
   828   } else {
   829     // The class is not loaded anywhere.  It is safe to model the
   830     // null in the typestates, because we can compile in a null check
   831     // which will deoptimize us if someone manages to load the
   832     // class later.
   833     push_null();
   834   }
   835 }
   838 // ------------------------------------------------------------------
   839 // ciTypeFlow::StateVector::apply_one_bytecode
   840 //
   841 // Apply the effect of one bytecode to this StateVector
   842 bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
   843   _trap_bci = -1;
   844   _trap_index = 0;
   846   if (CITraceTypeFlow) {
   847     tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
   848                   Bytecodes::name(str->cur_bc()));
   849   }
   851   switch(str->cur_bc()) {
   852   case Bytecodes::_aaload: do_aaload(str);                       break;
   854   case Bytecodes::_aastore:
   855     {
   856       pop_object();
   857       pop_int();
   858       pop_objArray();
   859       break;
   860     }
   861   case Bytecodes::_aconst_null:
   862     {
   863       push_null();
   864       break;
   865     }
   866   case Bytecodes::_aload:   load_local_object(str->get_index());    break;
   867   case Bytecodes::_aload_0: load_local_object(0);                   break;
   868   case Bytecodes::_aload_1: load_local_object(1);                   break;
   869   case Bytecodes::_aload_2: load_local_object(2);                   break;
   870   case Bytecodes::_aload_3: load_local_object(3);                   break;
   872   case Bytecodes::_anewarray:
   873     {
   874       pop_int();
   875       bool will_link;
   876       ciKlass* element_klass = str->get_klass(will_link);
   877       if (!will_link) {
   878         trap(str, element_klass, str->get_klass_index());
   879       } else {
   880         push_object(ciObjArrayKlass::make(element_klass));
   881       }
   882       break;
   883     }
   884   case Bytecodes::_areturn:
   885   case Bytecodes::_ifnonnull:
   886   case Bytecodes::_ifnull:
   887     {
   888       pop_object();
   889       break;
   890     }
   891   case Bytecodes::_monitorenter:
   892     {
   893       pop_object();
   894       set_monitor_count(monitor_count() + 1);
   895       break;
   896     }
   897   case Bytecodes::_monitorexit:
   898     {
   899       pop_object();
   900       assert(monitor_count() > 0, "must be a monitor to exit from");
   901       set_monitor_count(monitor_count() - 1);
   902       break;
   903     }
   904   case Bytecodes::_arraylength:
   905     {
   906       pop_array();
   907       push_int();
   908       break;
   909     }
   910   case Bytecodes::_astore:   store_local_object(str->get_index());  break;
   911   case Bytecodes::_astore_0: store_local_object(0);                 break;
   912   case Bytecodes::_astore_1: store_local_object(1);                 break;
   913   case Bytecodes::_astore_2: store_local_object(2);                 break;
   914   case Bytecodes::_astore_3: store_local_object(3);                 break;
   916   case Bytecodes::_athrow:
   917     {
   918       NEEDS_CLEANUP;
   919       pop_object();
   920       break;
   921     }
   922   case Bytecodes::_baload:
   923   case Bytecodes::_caload:
   924   case Bytecodes::_iaload:
   925   case Bytecodes::_saload:
   926     {
   927       pop_int();
   928       ciTypeArrayKlass* array_klass = pop_typeArray();
   929       // Put assert here for right type?
   930       push_int();
   931       break;
   932     }
   933   case Bytecodes::_bastore:
   934   case Bytecodes::_castore:
   935   case Bytecodes::_iastore:
   936   case Bytecodes::_sastore:
   937     {
   938       pop_int();
   939       pop_int();
   940       pop_typeArray();
   941       // assert here?
   942       break;
   943     }
   944   case Bytecodes::_bipush:
   945   case Bytecodes::_iconst_m1:
   946   case Bytecodes::_iconst_0:
   947   case Bytecodes::_iconst_1:
   948   case Bytecodes::_iconst_2:
   949   case Bytecodes::_iconst_3:
   950   case Bytecodes::_iconst_4:
   951   case Bytecodes::_iconst_5:
   952   case Bytecodes::_sipush:
   953     {
   954       push_int();
   955       break;
   956     }
   957   case Bytecodes::_checkcast: do_checkcast(str);                  break;
   959   case Bytecodes::_d2f:
   960     {
   961       pop_double();
   962       push_float();
   963       break;
   964     }
   965   case Bytecodes::_d2i:
   966     {
   967       pop_double();
   968       push_int();
   969       break;
   970     }
   971   case Bytecodes::_d2l:
   972     {
   973       pop_double();
   974       push_long();
   975       break;
   976     }
   977   case Bytecodes::_dadd:
   978   case Bytecodes::_ddiv:
   979   case Bytecodes::_dmul:
   980   case Bytecodes::_drem:
   981   case Bytecodes::_dsub:
   982     {
   983       pop_double();
   984       pop_double();
   985       push_double();
   986       break;
   987     }
   988   case Bytecodes::_daload:
   989     {
   990       pop_int();
   991       ciTypeArrayKlass* array_klass = pop_typeArray();
   992       // Put assert here for right type?
   993       push_double();
   994       break;
   995     }
   996   case Bytecodes::_dastore:
   997     {
   998       pop_double();
   999       pop_int();
  1000       pop_typeArray();
  1001       // assert here?
  1002       break;
  1004   case Bytecodes::_dcmpg:
  1005   case Bytecodes::_dcmpl:
  1007       pop_double();
  1008       pop_double();
  1009       push_int();
  1010       break;
  1012   case Bytecodes::_dconst_0:
  1013   case Bytecodes::_dconst_1:
  1015       push_double();
  1016       break;
  1018   case Bytecodes::_dload:   load_local_double(str->get_index());    break;
  1019   case Bytecodes::_dload_0: load_local_double(0);                   break;
  1020   case Bytecodes::_dload_1: load_local_double(1);                   break;
  1021   case Bytecodes::_dload_2: load_local_double(2);                   break;
  1022   case Bytecodes::_dload_3: load_local_double(3);                   break;
  1024   case Bytecodes::_dneg:
  1026       pop_double();
  1027       push_double();
  1028       break;
  1030   case Bytecodes::_dreturn:
  1032       pop_double();
  1033       break;
  1035   case Bytecodes::_dstore:   store_local_double(str->get_index());  break;
  1036   case Bytecodes::_dstore_0: store_local_double(0);                 break;
  1037   case Bytecodes::_dstore_1: store_local_double(1);                 break;
  1038   case Bytecodes::_dstore_2: store_local_double(2);                 break;
  1039   case Bytecodes::_dstore_3: store_local_double(3);                 break;
  1041   case Bytecodes::_dup:
  1043       push(type_at_tos());
  1044       break;
  1046   case Bytecodes::_dup_x1:
  1048       ciType* value1 = pop_value();
  1049       ciType* value2 = pop_value();
  1050       push(value1);
  1051       push(value2);
  1052       push(value1);
  1053       break;
  1055   case Bytecodes::_dup_x2:
  1057       ciType* value1 = pop_value();
  1058       ciType* value2 = pop_value();
  1059       ciType* value3 = pop_value();
  1060       push(value1);
  1061       push(value3);
  1062       push(value2);
  1063       push(value1);
  1064       break;
  1066   case Bytecodes::_dup2:
  1068       ciType* value1 = pop_value();
  1069       ciType* value2 = pop_value();
  1070       push(value2);
  1071       push(value1);
  1072       push(value2);
  1073       push(value1);
  1074       break;
  1076   case Bytecodes::_dup2_x1:
  1078       ciType* value1 = pop_value();
  1079       ciType* value2 = pop_value();
  1080       ciType* value3 = pop_value();
  1081       push(value2);
  1082       push(value1);
  1083       push(value3);
  1084       push(value2);
  1085       push(value1);
  1086       break;
  1088   case Bytecodes::_dup2_x2:
  1090       ciType* value1 = pop_value();
  1091       ciType* value2 = pop_value();
  1092       ciType* value3 = pop_value();
  1093       ciType* value4 = pop_value();
  1094       push(value2);
  1095       push(value1);
  1096       push(value4);
  1097       push(value3);
  1098       push(value2);
  1099       push(value1);
  1100       break;
  1102   case Bytecodes::_f2d:
  1104       pop_float();
  1105       push_double();
  1106       break;
  1108   case Bytecodes::_f2i:
  1110       pop_float();
  1111       push_int();
  1112       break;
  1114   case Bytecodes::_f2l:
  1116       pop_float();
  1117       push_long();
  1118       break;
  1120   case Bytecodes::_fadd:
  1121   case Bytecodes::_fdiv:
  1122   case Bytecodes::_fmul:
  1123   case Bytecodes::_frem:
  1124   case Bytecodes::_fsub:
  1126       pop_float();
  1127       pop_float();
  1128       push_float();
  1129       break;
  1131   case Bytecodes::_faload:
  1133       pop_int();
  1134       ciTypeArrayKlass* array_klass = pop_typeArray();
  1135       // Put assert here.
  1136       push_float();
  1137       break;
  1139   case Bytecodes::_fastore:
  1141       pop_float();
  1142       pop_int();
  1143       ciTypeArrayKlass* array_klass = pop_typeArray();
  1144       // Put assert here.
  1145       break;
  1147   case Bytecodes::_fcmpg:
  1148   case Bytecodes::_fcmpl:
  1150       pop_float();
  1151       pop_float();
  1152       push_int();
  1153       break;
  1155   case Bytecodes::_fconst_0:
  1156   case Bytecodes::_fconst_1:
  1157   case Bytecodes::_fconst_2:
  1159       push_float();
  1160       break;
  1162   case Bytecodes::_fload:   load_local_float(str->get_index());     break;
  1163   case Bytecodes::_fload_0: load_local_float(0);                    break;
  1164   case Bytecodes::_fload_1: load_local_float(1);                    break;
  1165   case Bytecodes::_fload_2: load_local_float(2);                    break;
  1166   case Bytecodes::_fload_3: load_local_float(3);                    break;
  1168   case Bytecodes::_fneg:
  1170       pop_float();
  1171       push_float();
  1172       break;
  1174   case Bytecodes::_freturn:
  1176       pop_float();
  1177       break;
  1179   case Bytecodes::_fstore:    store_local_float(str->get_index());   break;
  1180   case Bytecodes::_fstore_0:  store_local_float(0);                  break;
  1181   case Bytecodes::_fstore_1:  store_local_float(1);                  break;
  1182   case Bytecodes::_fstore_2:  store_local_float(2);                  break;
  1183   case Bytecodes::_fstore_3:  store_local_float(3);                  break;
  1185   case Bytecodes::_getfield:  do_getfield(str);                      break;
  1186   case Bytecodes::_getstatic: do_getstatic(str);                     break;
  1188   case Bytecodes::_goto:
  1189   case Bytecodes::_goto_w:
  1190   case Bytecodes::_nop:
  1191   case Bytecodes::_return:
  1193       // do nothing.
  1194       break;
  1196   case Bytecodes::_i2b:
  1197   case Bytecodes::_i2c:
  1198   case Bytecodes::_i2s:
  1199   case Bytecodes::_ineg:
  1201       pop_int();
  1202       push_int();
  1203       break;
  1205   case Bytecodes::_i2d:
  1207       pop_int();
  1208       push_double();
  1209       break;
  1211   case Bytecodes::_i2f:
  1213       pop_int();
  1214       push_float();
  1215       break;
  1217   case Bytecodes::_i2l:
  1219       pop_int();
  1220       push_long();
  1221       break;
  1223   case Bytecodes::_iadd:
  1224   case Bytecodes::_iand:
  1225   case Bytecodes::_idiv:
  1226   case Bytecodes::_imul:
  1227   case Bytecodes::_ior:
  1228   case Bytecodes::_irem:
  1229   case Bytecodes::_ishl:
  1230   case Bytecodes::_ishr:
  1231   case Bytecodes::_isub:
  1232   case Bytecodes::_iushr:
  1233   case Bytecodes::_ixor:
  1235       pop_int();
  1236       pop_int();
  1237       push_int();
  1238       break;
  1240   case Bytecodes::_if_acmpeq:
  1241   case Bytecodes::_if_acmpne:
  1243       pop_object();
  1244       pop_object();
  1245       break;
  1247   case Bytecodes::_if_icmpeq:
  1248   case Bytecodes::_if_icmpge:
  1249   case Bytecodes::_if_icmpgt:
  1250   case Bytecodes::_if_icmple:
  1251   case Bytecodes::_if_icmplt:
  1252   case Bytecodes::_if_icmpne:
  1254       pop_int();
  1255       pop_int();
  1256       break;
  1258   case Bytecodes::_ifeq:
  1259   case Bytecodes::_ifle:
  1260   case Bytecodes::_iflt:
  1261   case Bytecodes::_ifge:
  1262   case Bytecodes::_ifgt:
  1263   case Bytecodes::_ifne:
  1264   case Bytecodes::_ireturn:
  1265   case Bytecodes::_lookupswitch:
  1266   case Bytecodes::_tableswitch:
  1268       pop_int();
  1269       break;
  1271   case Bytecodes::_iinc:
  1273       int lnum = str->get_index();
  1274       check_int(local(lnum));
  1275       store_to_local(lnum);
  1276       break;
  1278   case Bytecodes::_iload:   load_local_int(str->get_index()); break;
  1279   case Bytecodes::_iload_0: load_local_int(0);                      break;
  1280   case Bytecodes::_iload_1: load_local_int(1);                      break;
  1281   case Bytecodes::_iload_2: load_local_int(2);                      break;
  1282   case Bytecodes::_iload_3: load_local_int(3);                      break;
  1284   case Bytecodes::_instanceof:
  1286       // Check for uncommon trap:
  1287       do_checkcast(str);
  1288       pop_object();
  1289       push_int();
  1290       break;
  1292   case Bytecodes::_invokeinterface: do_invoke(str, true);           break;
  1293   case Bytecodes::_invokespecial:   do_invoke(str, true);           break;
  1294   case Bytecodes::_invokestatic:    do_invoke(str, false);          break;
  1296   case Bytecodes::_invokevirtual:   do_invoke(str, true);           break;
  1298   case Bytecodes::_istore:   store_local_int(str->get_index());     break;
  1299   case Bytecodes::_istore_0: store_local_int(0);                    break;
  1300   case Bytecodes::_istore_1: store_local_int(1);                    break;
  1301   case Bytecodes::_istore_2: store_local_int(2);                    break;
  1302   case Bytecodes::_istore_3: store_local_int(3);                    break;
  1304   case Bytecodes::_jsr:
  1305   case Bytecodes::_jsr_w: do_jsr(str);                              break;
  1307   case Bytecodes::_l2d:
  1309       pop_long();
  1310       push_double();
  1311       break;
  1313   case Bytecodes::_l2f:
  1315       pop_long();
  1316       push_float();
  1317       break;
  1319   case Bytecodes::_l2i:
  1321       pop_long();
  1322       push_int();
  1323       break;
  1325   case Bytecodes::_ladd:
  1326   case Bytecodes::_land:
  1327   case Bytecodes::_ldiv:
  1328   case Bytecodes::_lmul:
  1329   case Bytecodes::_lor:
  1330   case Bytecodes::_lrem:
  1331   case Bytecodes::_lsub:
  1332   case Bytecodes::_lxor:
  1334       pop_long();
  1335       pop_long();
  1336       push_long();
  1337       break;
  1339   case Bytecodes::_laload:
  1341       pop_int();
  1342       ciTypeArrayKlass* array_klass = pop_typeArray();
  1343       // Put assert here for right type?
  1344       push_long();
  1345       break;
  1347   case Bytecodes::_lastore:
  1349       pop_long();
  1350       pop_int();
  1351       pop_typeArray();
  1352       // assert here?
  1353       break;
  1355   case Bytecodes::_lcmp:
  1357       pop_long();
  1358       pop_long();
  1359       push_int();
  1360       break;
  1362   case Bytecodes::_lconst_0:
  1363   case Bytecodes::_lconst_1:
  1365       push_long();
  1366       break;
  1368   case Bytecodes::_ldc:
  1369   case Bytecodes::_ldc_w:
  1370   case Bytecodes::_ldc2_w:
  1372       do_ldc(str);
  1373       break;
  1376   case Bytecodes::_lload:   load_local_long(str->get_index());      break;
  1377   case Bytecodes::_lload_0: load_local_long(0);                     break;
  1378   case Bytecodes::_lload_1: load_local_long(1);                     break;
  1379   case Bytecodes::_lload_2: load_local_long(2);                     break;
  1380   case Bytecodes::_lload_3: load_local_long(3);                     break;
  1382   case Bytecodes::_lneg:
  1384       pop_long();
  1385       push_long();
  1386       break;
  1388   case Bytecodes::_lreturn:
  1390       pop_long();
  1391       break;
  1393   case Bytecodes::_lshl:
  1394   case Bytecodes::_lshr:
  1395   case Bytecodes::_lushr:
  1397       pop_int();
  1398       pop_long();
  1399       push_long();
  1400       break;
  1402   case Bytecodes::_lstore:   store_local_long(str->get_index());    break;
  1403   case Bytecodes::_lstore_0: store_local_long(0);                   break;
  1404   case Bytecodes::_lstore_1: store_local_long(1);                   break;
  1405   case Bytecodes::_lstore_2: store_local_long(2);                   break;
  1406   case Bytecodes::_lstore_3: store_local_long(3);                   break;
  1408   case Bytecodes::_multianewarray: do_multianewarray(str);          break;
  1410   case Bytecodes::_new:      do_new(str);                           break;
  1412   case Bytecodes::_newarray: do_newarray(str);                      break;
  1414   case Bytecodes::_pop:
  1416       pop();
  1417       break;
  1419   case Bytecodes::_pop2:
  1421       pop();
  1422       pop();
  1423       break;
  1426   case Bytecodes::_putfield:       do_putfield(str);                 break;
  1427   case Bytecodes::_putstatic:      do_putstatic(str);                break;
  1429   case Bytecodes::_ret: do_ret(str);                                 break;
  1431   case Bytecodes::_swap:
  1433       ciType* value1 = pop_value();
  1434       ciType* value2 = pop_value();
  1435       push(value1);
  1436       push(value2);
  1437       break;
  1439   case Bytecodes::_wide:
  1440   default:
  1442       // The iterator should skip this.
  1443       ShouldNotReachHere();
  1444       break;
  1448   if (CITraceTypeFlow) {
  1449     print_on(tty);
  1452   return (_trap_bci != -1);
  1455 #ifndef PRODUCT
  1456 // ------------------------------------------------------------------
  1457 // ciTypeFlow::StateVector::print_cell_on
  1458 void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
  1459   ciType* type = type_at(c);
  1460   if (type == top_type()) {
  1461     st->print("top");
  1462   } else if (type == bottom_type()) {
  1463     st->print("bottom");
  1464   } else if (type == null_type()) {
  1465     st->print("null");
  1466   } else if (type == long2_type()) {
  1467     st->print("long2");
  1468   } else if (type == double2_type()) {
  1469     st->print("double2");
  1470   } else if (is_int(type)) {
  1471     st->print("int");
  1472   } else if (is_long(type)) {
  1473     st->print("long");
  1474   } else if (is_float(type)) {
  1475     st->print("float");
  1476   } else if (is_double(type)) {
  1477     st->print("double");
  1478   } else if (type->is_return_address()) {
  1479     st->print("address(%d)", type->as_return_address()->bci());
  1480   } else {
  1481     if (type->is_klass()) {
  1482       type->as_klass()->name()->print_symbol_on(st);
  1483     } else {
  1484       st->print("UNEXPECTED TYPE");
  1485       type->print();
  1490 // ------------------------------------------------------------------
  1491 // ciTypeFlow::StateVector::print_on
  1492 void ciTypeFlow::StateVector::print_on(outputStream* st) const {
  1493   int num_locals   = _outer->max_locals();
  1494   int num_stack    = stack_size();
  1495   int num_monitors = monitor_count();
  1496   st->print_cr("  State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors);
  1497   if (num_stack >= 0) {
  1498     int i;
  1499     for (i = 0; i < num_locals; i++) {
  1500       st->print("    local %2d : ", i);
  1501       print_cell_on(st, local(i));
  1502       st->cr();
  1504     for (i = 0; i < num_stack; i++) {
  1505       st->print("    stack %2d : ", i);
  1506       print_cell_on(st, stack(i));
  1507       st->cr();
  1511 #endif
  1514 // ------------------------------------------------------------------
  1515 // ciTypeFlow::SuccIter::next
  1516 //
  1517 void ciTypeFlow::SuccIter::next() {
  1518   int succ_ct = _pred->successors()->length();
  1519   int next = _index + 1;
  1520   if (next < succ_ct) {
  1521     _index = next;
  1522     _succ = _pred->successors()->at(next);
  1523     return;
  1525   for (int i = next - succ_ct; i < _pred->exceptions()->length(); i++) {
  1526     // Do not compile any code for unloaded exception types.
  1527     // Following compiler passes are responsible for doing this also.
  1528     ciInstanceKlass* exception_klass = _pred->exc_klasses()->at(i);
  1529     if (exception_klass->is_loaded()) {
  1530       _index = next;
  1531       _succ = _pred->exceptions()->at(i);
  1532       return;
  1534     next++;
  1536   _index = -1;
  1537   _succ = NULL;
  1540 // ------------------------------------------------------------------
  1541 // ciTypeFlow::SuccIter::set_succ
  1542 //
  1543 void ciTypeFlow::SuccIter::set_succ(Block* succ) {
  1544   int succ_ct = _pred->successors()->length();
  1545   if (_index < succ_ct) {
  1546     _pred->successors()->at_put(_index, succ);
  1547   } else {
  1548     int idx = _index - succ_ct;
  1549     _pred->exceptions()->at_put(idx, succ);
  1553 // ciTypeFlow::Block
  1554 //
  1555 // A basic block.
  1557 // ------------------------------------------------------------------
  1558 // ciTypeFlow::Block::Block
  1559 ciTypeFlow::Block::Block(ciTypeFlow* outer,
  1560                          ciBlock *ciblk,
  1561                          ciTypeFlow::JsrSet* jsrs) {
  1562   _ciblock = ciblk;
  1563   _exceptions = NULL;
  1564   _exc_klasses = NULL;
  1565   _successors = NULL;
  1566   _state = new (outer->arena()) StateVector(outer);
  1567   JsrSet* new_jsrs =
  1568     new (outer->arena()) JsrSet(outer->arena(), jsrs->size());
  1569   jsrs->copy_into(new_jsrs);
  1570   _jsrs = new_jsrs;
  1571   _next = NULL;
  1572   _on_work_list = false;
  1573   _backedge_copy = false;
  1574   _exception_entry = false;
  1575   _trap_bci = -1;
  1576   _trap_index = 0;
  1577   df_init();
  1579   if (CITraceTypeFlow) {
  1580     tty->print_cr(">> Created new block");
  1581     print_on(tty);
  1584   assert(this->outer() == outer, "outer link set up");
  1585   assert(!outer->have_block_count(), "must not have mapped blocks yet");
  1588 // ------------------------------------------------------------------
  1589 // ciTypeFlow::Block::df_init
  1590 void ciTypeFlow::Block::df_init() {
  1591   _pre_order = -1; assert(!has_pre_order(), "");
  1592   _post_order = -1; assert(!has_post_order(), "");
  1593   _loop = NULL;
  1594   _irreducible_entry = false;
  1595   _rpo_next = NULL;
  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();
  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);
  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         _successors =
  1651           new (arena) GrowableArray<Block*>(arena, 2, 0, NULL);
  1652         assert(_successors->length() == IF_NOT_TAKEN, "");
  1653         _successors->append(analyzer->block_at(next_bci, jsrs));
  1654         assert(_successors->length() == IF_TAKEN, "");
  1655         _successors->append(analyzer->block_at(branch_bci, jsrs));
  1656         break;
  1658       case Bytecodes::_goto:
  1659         branch_bci = str->get_dest();
  1660         _successors =
  1661           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1662         assert(_successors->length() == GOTO_TARGET, "");
  1663         _successors->append(analyzer->block_at(branch_bci, jsrs));
  1664         break;
  1666       case Bytecodes::_jsr:
  1667         branch_bci = str->get_dest();
  1668         _successors =
  1669           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1670         assert(_successors->length() == GOTO_TARGET, "");
  1671         _successors->append(analyzer->block_at(branch_bci, jsrs));
  1672         break;
  1674       case Bytecodes::_goto_w:
  1675       case Bytecodes::_jsr_w:
  1676         _successors =
  1677           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1678         assert(_successors->length() == GOTO_TARGET, "");
  1679         _successors->append(analyzer->block_at(str->get_far_dest(), jsrs));
  1680         break;
  1682       case Bytecodes::_tableswitch:  {
  1683         Bytecode_tableswitch *tableswitch =
  1684           Bytecode_tableswitch_at(str->cur_bcp());
  1686         int len = tableswitch->length();
  1687         _successors =
  1688           new (arena) GrowableArray<Block*>(arena, len+1, 0, NULL);
  1689         int bci = current_bci + tableswitch->default_offset();
  1690         Block* block = analyzer->block_at(bci, jsrs);
  1691         assert(_successors->length() == SWITCH_DEFAULT, "");
  1692         _successors->append(block);
  1693         while (--len >= 0) {
  1694           int bci = current_bci + tableswitch->dest_offset_at(len);
  1695           block = analyzer->block_at(bci, jsrs);
  1696           assert(_successors->length() >= SWITCH_CASES, "");
  1697           _successors->append_if_missing(block);
  1699         break;
  1702       case Bytecodes::_lookupswitch: {
  1703         Bytecode_lookupswitch *lookupswitch =
  1704           Bytecode_lookupswitch_at(str->cur_bcp());
  1706         int npairs = lookupswitch->number_of_pairs();
  1707         _successors =
  1708           new (arena) GrowableArray<Block*>(arena, npairs+1, 0, NULL);
  1709         int bci = current_bci + lookupswitch->default_offset();
  1710         Block* block = analyzer->block_at(bci, jsrs);
  1711         assert(_successors->length() == SWITCH_DEFAULT, "");
  1712         _successors->append(block);
  1713         while(--npairs >= 0) {
  1714           LookupswitchPair *pair = lookupswitch->pair_at(npairs);
  1715           int bci = current_bci + pair->offset();
  1716           Block* block = analyzer->block_at(bci, jsrs);
  1717           assert(_successors->length() >= SWITCH_CASES, "");
  1718           _successors->append_if_missing(block);
  1720         break;
  1723       case Bytecodes::_athrow:     case Bytecodes::_ireturn:
  1724       case Bytecodes::_lreturn:    case Bytecodes::_freturn:
  1725       case Bytecodes::_dreturn:    case Bytecodes::_areturn:
  1726       case Bytecodes::_return:
  1727         _successors =
  1728           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1729         // No successors
  1730         break;
  1732       case Bytecodes::_ret: {
  1733         _successors =
  1734           new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1736         Cell local = state->local(str->get_index());
  1737         ciType* return_address = state->type_at(local);
  1738         assert(return_address->is_return_address(), "verify: wrong type");
  1739         int bci = return_address->as_return_address()->bci();
  1740         assert(_successors->length() == GOTO_TARGET, "");
  1741         _successors->append(analyzer->block_at(bci, jsrs));
  1742         break;
  1745       case Bytecodes::_wide:
  1746       default:
  1747         ShouldNotReachHere();
  1748         break;
  1752   return _successors;
  1755 // ------------------------------------------------------------------
  1756 // ciTypeFlow::Block:compute_exceptions
  1757 //
  1758 // Compute the exceptional successors and types for this Block.
  1759 void ciTypeFlow::Block::compute_exceptions() {
  1760   assert(_exceptions == NULL && _exc_klasses == NULL, "repeat");
  1762   if (CITraceTypeFlow) {
  1763     tty->print(">> Computing exceptions for block ");
  1764     print_value_on(tty);
  1765     tty->cr();
  1768   ciTypeFlow* analyzer = outer();
  1769   Arena* arena = analyzer->arena();
  1771   // Any bci in the block will do.
  1772   ciExceptionHandlerStream str(analyzer->method(), start());
  1774   // Allocate our growable arrays.
  1775   int exc_count = str.count();
  1776   _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, NULL);
  1777   _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count,
  1778                                                              0, NULL);
  1780   for ( ; !str.is_done(); str.next()) {
  1781     ciExceptionHandler* handler = str.handler();
  1782     int bci = handler->handler_bci();
  1783     ciInstanceKlass* klass = NULL;
  1784     if (bci == -1) {
  1785       // There is no catch all.  It is possible to exit the method.
  1786       break;
  1788     if (handler->is_catch_all()) {
  1789       klass = analyzer->env()->Throwable_klass();
  1790     } else {
  1791       klass = handler->catch_klass();
  1793     _exceptions->append(analyzer->block_at(bci, _jsrs));
  1794     _exc_klasses->append(klass);
  1798 // ------------------------------------------------------------------
  1799 // ciTypeFlow::Block::set_backedge_copy
  1800 // Use this only to make a pre-existing public block into a backedge copy.
  1801 void ciTypeFlow::Block::set_backedge_copy(bool z) {
  1802   assert(z || (z == is_backedge_copy()), "cannot make a backedge copy public");
  1803   _backedge_copy = z;
  1806 // ------------------------------------------------------------------
  1807 // ciTypeFlow::Block::is_clonable_exit
  1808 //
  1809 // At most 2 normal successors, one of which continues looping,
  1810 // and all exceptional successors must exit.
  1811 bool ciTypeFlow::Block::is_clonable_exit(ciTypeFlow::Loop* lp) {
  1812   int normal_cnt  = 0;
  1813   int in_loop_cnt = 0;
  1814   for (SuccIter iter(this); !iter.done(); iter.next()) {
  1815     Block* succ = iter.succ();
  1816     if (iter.is_normal_ctrl()) {
  1817       if (++normal_cnt > 2) return false;
  1818       if (lp->contains(succ->loop())) {
  1819         if (++in_loop_cnt > 1) return false;
  1821     } else {
  1822       if (lp->contains(succ->loop())) return false;
  1825   return in_loop_cnt == 1;
  1828 // ------------------------------------------------------------------
  1829 // ciTypeFlow::Block::looping_succ
  1830 //
  1831 ciTypeFlow::Block* ciTypeFlow::Block::looping_succ(ciTypeFlow::Loop* lp) {
  1832   assert(successors()->length() <= 2, "at most 2 normal successors");
  1833   for (SuccIter iter(this); !iter.done(); iter.next()) {
  1834     Block* succ = iter.succ();
  1835     if (lp->contains(succ->loop())) {
  1836       return succ;
  1839   return NULL;
  1842 #ifndef PRODUCT
  1843 // ------------------------------------------------------------------
  1844 // ciTypeFlow::Block::print_value_on
  1845 void ciTypeFlow::Block::print_value_on(outputStream* st) const {
  1846   if (has_pre_order()) st->print("#%-2d ", pre_order());
  1847   if (has_rpo())       st->print("rpo#%-2d ", rpo());
  1848   st->print("[%d - %d)", start(), limit());
  1849   if (is_loop_head()) st->print(" lphd");
  1850   if (is_irreducible_entry()) st->print(" irred");
  1851   if (_jsrs->size() > 0) { st->print("/");  _jsrs->print_on(st); }
  1852   if (is_backedge_copy())  st->print("/backedge_copy");
  1855 // ------------------------------------------------------------------
  1856 // ciTypeFlow::Block::print_on
  1857 void ciTypeFlow::Block::print_on(outputStream* st) const {
  1858   if ((Verbose || WizardMode)) {
  1859     outer()->method()->print_codes_on(start(), limit(), st);
  1861   st->print_cr("  ====================================================  ");
  1862   st->print ("  ");
  1863   print_value_on(st);
  1864   st->print(" Stored locals: "); def_locals()->print_on(st, outer()->method()->max_locals()); tty->cr();
  1865   if (loop() && loop()->parent() != NULL) {
  1866     st->print(" loops:");
  1867     Loop* lp = loop();
  1868     do {
  1869       st->print(" %d<-%d", lp->head()->pre_order(),lp->tail()->pre_order());
  1870       if (lp->is_irreducible()) st->print("(ir)");
  1871       lp = lp->parent();
  1872     } while (lp->parent() != NULL);
  1874   st->cr();
  1875   _state->print_on(st);
  1876   if (_successors == NULL) {
  1877     st->print_cr("  No successor information");
  1878   } else {
  1879     int num_successors = _successors->length();
  1880     st->print_cr("  Successors : %d", num_successors);
  1881     for (int i = 0; i < num_successors; i++) {
  1882       Block* successor = _successors->at(i);
  1883       st->print("    ");
  1884       successor->print_value_on(st);
  1885       st->cr();
  1888   if (_exceptions == NULL) {
  1889     st->print_cr("  No exception information");
  1890   } else {
  1891     int num_exceptions = _exceptions->length();
  1892     st->print_cr("  Exceptions : %d", num_exceptions);
  1893     for (int i = 0; i < num_exceptions; i++) {
  1894       Block* exc_succ = _exceptions->at(i);
  1895       ciInstanceKlass* exc_klass = _exc_klasses->at(i);
  1896       st->print("    ");
  1897       exc_succ->print_value_on(st);
  1898       st->print(" -- ");
  1899       exc_klass->name()->print_symbol_on(st);
  1900       st->cr();
  1903   if (has_trap()) {
  1904     st->print_cr("  Traps on %d with trap index %d", trap_bci(), trap_index());
  1906   st->print_cr("  ====================================================  ");
  1908 #endif
  1910 #ifndef PRODUCT
  1911 // ------------------------------------------------------------------
  1912 // ciTypeFlow::LocalSet::print_on
  1913 void ciTypeFlow::LocalSet::print_on(outputStream* st, int limit) const {
  1914   st->print("{");
  1915   for (int i = 0; i < max; i++) {
  1916     if (test(i)) st->print(" %d", i);
  1918   if (limit > max) {
  1919     st->print(" %d..%d ", max, limit);
  1921   st->print(" }");
  1923 #endif
  1925 // ciTypeFlow
  1926 //
  1927 // This is a pass over the bytecodes which computes the following:
  1928 //   basic block structure
  1929 //   interpreter type-states (a la the verifier)
  1931 // ------------------------------------------------------------------
  1932 // ciTypeFlow::ciTypeFlow
  1933 ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) {
  1934   _env = env;
  1935   _method = method;
  1936   _methodBlocks = method->get_method_blocks();
  1937   _max_locals = method->max_locals();
  1938   _max_stack = method->max_stack();
  1939   _code_size = method->code_size();
  1940   _has_irreducible_entry = false;
  1941   _osr_bci = osr_bci;
  1942   _failure_reason = NULL;
  1943   assert(start_bci() >= 0 && start_bci() < code_size() , "correct osr_bci argument");
  1944   _work_list = NULL;
  1946   _ciblock_count = _methodBlocks->num_blocks();
  1947   _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, _ciblock_count);
  1948   for (int i = 0; i < _ciblock_count; i++) {
  1949     _idx_to_blocklist[i] = NULL;
  1951   _block_map = NULL;  // until all blocks are seen
  1952   _jsr_count = 0;
  1953   _jsr_records = NULL;
  1956 // ------------------------------------------------------------------
  1957 // ciTypeFlow::work_list_next
  1958 //
  1959 // Get the next basic block from our work list.
  1960 ciTypeFlow::Block* ciTypeFlow::work_list_next() {
  1961   assert(!work_list_empty(), "work list must not be empty");
  1962   Block* next_block = _work_list;
  1963   _work_list = next_block->next();
  1964   next_block->set_next(NULL);
  1965   next_block->set_on_work_list(false);
  1966   return next_block;
  1969 // ------------------------------------------------------------------
  1970 // ciTypeFlow::add_to_work_list
  1971 //
  1972 // Add a basic block to our work list.
  1973 // List is sorted by decreasing postorder sort (same as increasing RPO)
  1974 void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) {
  1975   assert(!block->is_on_work_list(), "must not already be on work list");
  1977   if (CITraceTypeFlow) {
  1978     tty->print(">> Adding block ");
  1979     block->print_value_on(tty);
  1980     tty->print_cr(" to the work list : ");
  1983   block->set_on_work_list(true);
  1985   // decreasing post order sort
  1987   Block* prev = NULL;
  1988   Block* current = _work_list;
  1989   int po = block->post_order();
  1990   while (current != NULL) {
  1991     if (!current->has_post_order() || po > current->post_order())
  1992       break;
  1993     prev = current;
  1994     current = current->next();
  1996   if (prev == NULL) {
  1997     block->set_next(_work_list);
  1998     _work_list = block;
  1999   } else {
  2000     block->set_next(current);
  2001     prev->set_next(block);
  2004   if (CITraceTypeFlow) {
  2005     tty->cr();
  2009 // ------------------------------------------------------------------
  2010 // ciTypeFlow::block_at
  2011 //
  2012 // Return the block beginning at bci which has a JsrSet compatible
  2013 // with jsrs.
  2014 ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
  2015   // First find the right ciBlock.
  2016   if (CITraceTypeFlow) {
  2017     tty->print(">> Requesting block for %d/", bci);
  2018     jsrs->print_on(tty);
  2019     tty->cr();
  2022   ciBlock* ciblk = _methodBlocks->block_containing(bci);
  2023   assert(ciblk->start_bci() == bci, "bad ciBlock boundaries");
  2024   Block* block = get_block_for(ciblk->index(), jsrs, option);
  2026   assert(block == NULL? (option == no_create): block->is_backedge_copy() == (option == create_backedge_copy), "create option consistent with result");
  2028   if (CITraceTypeFlow) {
  2029     if (block != NULL) {
  2030       tty->print(">> Found block ");
  2031       block->print_value_on(tty);
  2032       tty->cr();
  2033     } else {
  2034       tty->print_cr(">> No such block.");
  2038   return block;
  2041 // ------------------------------------------------------------------
  2042 // ciTypeFlow::make_jsr_record
  2043 //
  2044 // Make a JsrRecord for a given (entry, return) pair, if such a record
  2045 // does not already exist.
  2046 ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address,
  2047                                                    int return_address) {
  2048   if (_jsr_records == NULL) {
  2049     _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(),
  2050                                                            _jsr_count,
  2051                                                            0,
  2052                                                            NULL);
  2054   JsrRecord* record = NULL;
  2055   int len = _jsr_records->length();
  2056   for (int i = 0; i < len; i++) {
  2057     JsrRecord* record = _jsr_records->at(i);
  2058     if (record->entry_address() == entry_address &&
  2059         record->return_address() == return_address) {
  2060       return record;
  2064   record = new (arena()) JsrRecord(entry_address, return_address);
  2065   _jsr_records->append(record);
  2066   return record;
  2069 // ------------------------------------------------------------------
  2070 // ciTypeFlow::flow_exceptions
  2071 //
  2072 // Merge the current state into all exceptional successors at the
  2073 // current point in the code.
  2074 void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions,
  2075                                  GrowableArray<ciInstanceKlass*>* exc_klasses,
  2076                                  ciTypeFlow::StateVector* state) {
  2077   int len = exceptions->length();
  2078   assert(exc_klasses->length() == len, "must have same length");
  2079   for (int i = 0; i < len; i++) {
  2080     Block* block = exceptions->at(i);
  2081     ciInstanceKlass* exception_klass = exc_klasses->at(i);
  2083     if (!exception_klass->is_loaded()) {
  2084       // Do not compile any code for unloaded exception types.
  2085       // Following compiler passes are responsible for doing this also.
  2086       continue;
  2089     if (block->meet_exception(exception_klass, state)) {
  2090       // Block was modified and has PO.  Add it to the work list.
  2091       if (block->has_post_order() &&
  2092           !block->is_on_work_list()) {
  2093         add_to_work_list(block);
  2099 // ------------------------------------------------------------------
  2100 // ciTypeFlow::flow_successors
  2101 //
  2102 // Merge the current state into all successors at the current point
  2103 // in the code.
  2104 void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors,
  2105                                  ciTypeFlow::StateVector* state) {
  2106   int len = successors->length();
  2107   for (int i = 0; i < len; i++) {
  2108     Block* block = successors->at(i);
  2109     if (block->meet(state)) {
  2110       // Block was modified and has PO.  Add it to the work list.
  2111       if (block->has_post_order() &&
  2112           !block->is_on_work_list()) {
  2113         add_to_work_list(block);
  2119 // ------------------------------------------------------------------
  2120 // ciTypeFlow::can_trap
  2121 //
  2122 // Tells if a given instruction is able to generate an exception edge.
  2123 bool ciTypeFlow::can_trap(ciBytecodeStream& str) {
  2124   // Cf. GenerateOopMap::do_exception_edge.
  2125   if (!Bytecodes::can_trap(str.cur_bc()))  return false;
  2127   switch (str.cur_bc()) {
  2128     case Bytecodes::_ldc:
  2129     case Bytecodes::_ldc_w:
  2130     case Bytecodes::_ldc2_w:
  2131     case Bytecodes::_aload_0:
  2132       // These bytecodes can trap for rewriting.  We need to assume that
  2133       // they do not throw exceptions to make the monitor analysis work.
  2134       return false;
  2136     case Bytecodes::_ireturn:
  2137     case Bytecodes::_lreturn:
  2138     case Bytecodes::_freturn:
  2139     case Bytecodes::_dreturn:
  2140     case Bytecodes::_areturn:
  2141     case Bytecodes::_return:
  2142       // We can assume the monitor stack is empty in this analysis.
  2143       return false;
  2145     case Bytecodes::_monitorexit:
  2146       // We can assume monitors are matched in this analysis.
  2147       return false;
  2150   return true;
  2153 // ------------------------------------------------------------------
  2154 // ciTypeFlow::clone_loop_heads
  2155 //
  2156 // Clone the loop heads
  2157 bool ciTypeFlow::clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) {
  2158   bool rslt = false;
  2159   for (PreorderLoops iter(loop_tree_root()); !iter.done(); iter.next()) {
  2160     lp = iter.current();
  2161     Block* head = lp->head();
  2162     if (lp == loop_tree_root() ||
  2163         lp->is_irreducible() ||
  2164         !head->is_clonable_exit(lp))
  2165       continue;
  2167     // check not already cloned
  2168     if (head->backedge_copy_count() != 0)
  2169       continue;
  2171     // check _no_ shared head below us
  2172     Loop* ch;
  2173     for (ch = lp->child(); ch != NULL && ch->head() != head; ch = ch->sibling());
  2174     if (ch != NULL)
  2175       continue;
  2177     // Clone head
  2178     Block* new_head = head->looping_succ(lp);
  2179     Block* clone = clone_loop_head(lp, temp_vector, temp_set);
  2180     // Update lp's info
  2181     clone->set_loop(lp);
  2182     lp->set_head(new_head);
  2183     lp->set_tail(clone);
  2184     // And move original head into outer loop
  2185     head->set_loop(lp->parent());
  2187     rslt = true;
  2189   return rslt;
  2192 // ------------------------------------------------------------------
  2193 // ciTypeFlow::clone_loop_head
  2194 //
  2195 // Clone lp's head and replace tail's successors with clone.
  2196 //
  2197 //  |
  2198 //  v
  2199 // head <-> body
  2200 //  |
  2201 //  v
  2202 // exit
  2203 //
  2204 // new_head
  2205 //
  2206 //  |
  2207 //  v
  2208 // head ----------\
  2209 //  |             |
  2210 //  |             v
  2211 //  |  clone <-> body
  2212 //  |    |
  2213 //  | /--/
  2214 //  | |
  2215 //  v v
  2216 // exit
  2217 //
  2218 ciTypeFlow::Block* ciTypeFlow::clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set) {
  2219   Block* head = lp->head();
  2220   Block* tail = lp->tail();
  2221   if (CITraceTypeFlow) {
  2222     tty->print(">> Requesting clone of loop head "); head->print_value_on(tty);
  2223     tty->print("  for predecessor ");                tail->print_value_on(tty);
  2224     tty->cr();
  2226   Block* clone = block_at(head->start(), head->jsrs(), create_backedge_copy);
  2227   assert(clone->backedge_copy_count() == 1, "one backedge copy for all back edges");
  2229   assert(!clone->has_pre_order(), "just created");
  2230   clone->set_next_pre_order();
  2232   // Insert clone after (orig) tail in reverse post order
  2233   clone->set_rpo_next(tail->rpo_next());
  2234   tail->set_rpo_next(clone);
  2236   // tail->head becomes tail->clone
  2237   for (SuccIter iter(tail); !iter.done(); iter.next()) {
  2238     if (iter.succ() == head) {
  2239       iter.set_succ(clone);
  2240       break;
  2243   flow_block(tail, temp_vector, temp_set);
  2244   if (head == tail) {
  2245     // For self-loops, clone->head becomes clone->clone
  2246     flow_block(clone, temp_vector, temp_set);
  2247     for (SuccIter iter(clone); !iter.done(); iter.next()) {
  2248       if (iter.succ() == head) {
  2249         iter.set_succ(clone);
  2250         break;
  2254   flow_block(clone, temp_vector, temp_set);
  2256   return clone;
  2259 // ------------------------------------------------------------------
  2260 // ciTypeFlow::flow_block
  2261 //
  2262 // Interpret the effects of the bytecodes on the incoming state
  2263 // vector of a basic block.  Push the changed state to succeeding
  2264 // basic blocks.
  2265 void ciTypeFlow::flow_block(ciTypeFlow::Block* block,
  2266                             ciTypeFlow::StateVector* state,
  2267                             ciTypeFlow::JsrSet* jsrs) {
  2268   if (CITraceTypeFlow) {
  2269     tty->print("\n>> ANALYZING BLOCK : ");
  2270     tty->cr();
  2271     block->print_on(tty);
  2273   assert(block->has_pre_order(), "pre-order is assigned before 1st flow");
  2275   int start = block->start();
  2276   int limit = block->limit();
  2277   int control = block->control();
  2278   if (control != ciBlock::fall_through_bci) {
  2279     limit = control;
  2282   // Grab the state from the current block.
  2283   block->copy_state_into(state);
  2284   state->def_locals()->clear();
  2286   GrowableArray<Block*>*           exceptions = block->exceptions();
  2287   GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses();
  2288   bool has_exceptions = exceptions->length() > 0;
  2290   bool exceptions_used = false;
  2292   ciBytecodeStream str(method());
  2293   str.reset_to_bci(start);
  2294   Bytecodes::Code code;
  2295   while ((code = str.next()) != ciBytecodeStream::EOBC() &&
  2296          str.cur_bci() < limit) {
  2297     // Check for exceptional control flow from this point.
  2298     if (has_exceptions && can_trap(str)) {
  2299       flow_exceptions(exceptions, exc_klasses, state);
  2300       exceptions_used = true;
  2302     // Apply the effects of the current bytecode to our state.
  2303     bool res = state->apply_one_bytecode(&str);
  2305     // Watch for bailouts.
  2306     if (failing())  return;
  2308     if (res) {
  2310       // We have encountered a trap.  Record it in this block.
  2311       block->set_trap(state->trap_bci(), state->trap_index());
  2313       if (CITraceTypeFlow) {
  2314         tty->print_cr(">> Found trap");
  2315         block->print_on(tty);
  2318       // Save set of locals defined in this block
  2319       block->def_locals()->add(state->def_locals());
  2321       // Record (no) successors.
  2322       block->successors(&str, state, jsrs);
  2324       assert(!has_exceptions || exceptions_used, "Not removing exceptions");
  2326       // Discontinue interpretation of this Block.
  2327       return;
  2331   GrowableArray<Block*>* successors = NULL;
  2332   if (control != ciBlock::fall_through_bci) {
  2333     // Check for exceptional control flow from this point.
  2334     if (has_exceptions && can_trap(str)) {
  2335       flow_exceptions(exceptions, exc_klasses, state);
  2336       exceptions_used = true;
  2339     // Fix the JsrSet to reflect effect of the bytecode.
  2340     block->copy_jsrs_into(jsrs);
  2341     jsrs->apply_control(this, &str, state);
  2343     // Find successor edges based on old state and new JsrSet.
  2344     successors = block->successors(&str, state, jsrs);
  2346     // Apply the control changes to the state.
  2347     state->apply_one_bytecode(&str);
  2348   } else {
  2349     // Fall through control
  2350     successors = block->successors(&str, NULL, NULL);
  2353   // Save set of locals defined in this block
  2354   block->def_locals()->add(state->def_locals());
  2356   // Remove untaken exception paths
  2357   if (!exceptions_used)
  2358     exceptions->clear();
  2360   // Pass our state to successors.
  2361   flow_successors(successors, state);
  2364 // ------------------------------------------------------------------
  2365 // ciTypeFlow::PostOrderLoops::next
  2366 //
  2367 // Advance to next loop tree using a postorder, left-to-right traversal.
  2368 void ciTypeFlow::PostorderLoops::next() {
  2369   assert(!done(), "must not be done.");
  2370   if (_current->sibling() != NULL) {
  2371     _current = _current->sibling();
  2372     while (_current->child() != NULL) {
  2373       _current = _current->child();
  2375   } else {
  2376     _current = _current->parent();
  2380 // ------------------------------------------------------------------
  2381 // ciTypeFlow::PreOrderLoops::next
  2382 //
  2383 // Advance to next loop tree using a preorder, left-to-right traversal.
  2384 void ciTypeFlow::PreorderLoops::next() {
  2385   assert(!done(), "must not be done.");
  2386   if (_current->child() != NULL) {
  2387     _current = _current->child();
  2388   } else if (_current->sibling() != NULL) {
  2389     _current = _current->sibling();
  2390   } else {
  2391     while (_current != _root && _current->sibling() == NULL) {
  2392       _current = _current->parent();
  2394     if (_current == _root) {
  2395       _current = NULL;
  2396       assert(done(), "must be done.");
  2397     } else {
  2398       assert(_current->sibling() != NULL, "must be more to do");
  2399       _current = _current->sibling();
  2404 // ------------------------------------------------------------------
  2405 // ciTypeFlow::Loop::sorted_merge
  2406 //
  2407 // Merge the branch lp into this branch, sorting on the loop head
  2408 // pre_orders. Returns the leaf of the merged branch.
  2409 // Child and sibling pointers will be setup later.
  2410 // Sort is (looking from leaf towards the root)
  2411 //  descending on primary key: loop head's pre_order, and
  2412 //  ascending  on secondary key: loop tail's pre_order.
  2413 ciTypeFlow::Loop* ciTypeFlow::Loop::sorted_merge(Loop* lp) {
  2414   Loop* leaf = this;
  2415   Loop* prev = NULL;
  2416   Loop* current = leaf;
  2417   while (lp != NULL) {
  2418     int lp_pre_order = lp->head()->pre_order();
  2419     // Find insertion point for "lp"
  2420     while (current != NULL) {
  2421       if (current == lp)
  2422         return leaf; // Already in list
  2423       if (current->head()->pre_order() < lp_pre_order)
  2424         break;
  2425       if (current->head()->pre_order() == lp_pre_order &&
  2426           current->tail()->pre_order() > lp->tail()->pre_order()) {
  2427         break;
  2429       prev = current;
  2430       current = current->parent();
  2432     Loop* next_lp = lp->parent(); // Save future list of items to insert
  2433     // Insert lp before current
  2434     lp->set_parent(current);
  2435     if (prev != NULL) {
  2436       prev->set_parent(lp);
  2437     } else {
  2438       leaf = lp;
  2440     prev = lp;     // Inserted item is new prev[ious]
  2441     lp = next_lp;  // Next item to insert
  2443   return leaf;
  2446 // ------------------------------------------------------------------
  2447 // ciTypeFlow::build_loop_tree
  2448 //
  2449 // Incrementally build loop tree.
  2450 void ciTypeFlow::build_loop_tree(Block* blk) {
  2451   assert(!blk->is_post_visited(), "precondition");
  2452   Loop* innermost = NULL; // merge of loop tree branches over all successors
  2454   for (SuccIter iter(blk); !iter.done(); iter.next()) {
  2455     Loop*  lp   = NULL;
  2456     Block* succ = iter.succ();
  2457     if (!succ->is_post_visited()) {
  2458       // Found backedge since predecessor post visited, but successor is not
  2459       assert(succ->pre_order() <= blk->pre_order(), "should be backedge");
  2461       // Create a LoopNode to mark this loop.
  2462       lp = new (arena()) Loop(succ, blk);
  2463       if (succ->loop() == NULL)
  2464         succ->set_loop(lp);
  2465       // succ->loop will be updated to innermost loop on a later call, when blk==succ
  2467     } else {  // Nested loop
  2468       lp = succ->loop();
  2470       // If succ is loop head, find outer loop.
  2471       while (lp != NULL && lp->head() == succ) {
  2472         lp = lp->parent();
  2474       if (lp == NULL) {
  2475         // Infinite loop, it's parent is the root
  2476         lp = loop_tree_root();
  2480     // Check for irreducible loop.
  2481     // Successor has already been visited. If the successor's loop head
  2482     // has already been post-visited, then this is another entry into the loop.
  2483     while (lp->head()->is_post_visited() && lp != loop_tree_root()) {
  2484       _has_irreducible_entry = true;
  2485       lp->set_irreducible(succ);
  2486       if (!succ->is_on_work_list()) {
  2487         // Assume irreducible entries need more data flow
  2488         add_to_work_list(succ);
  2490       lp = lp->parent();
  2491       assert(lp != NULL, "nested loop must have parent by now");
  2494     // Merge loop tree branch for all successors.
  2495     innermost = innermost == NULL ? lp : innermost->sorted_merge(lp);
  2497   } // end loop
  2499   if (innermost == NULL) {
  2500     assert(blk->successors()->length() == 0, "CFG exit");
  2501     blk->set_loop(loop_tree_root());
  2502   } else if (innermost->head() == blk) {
  2503     // If loop header, complete the tree pointers
  2504     if (blk->loop() != innermost) {
  2505 #if ASSERT
  2506       assert(blk->loop()->head() == innermost->head(), "same head");
  2507       Loop* dl;
  2508       for (dl = innermost; dl != NULL && dl != blk->loop(); dl = dl->parent());
  2509       assert(dl == blk->loop(), "blk->loop() already in innermost list");
  2510 #endif
  2511       blk->set_loop(innermost);
  2513     innermost->def_locals()->add(blk->def_locals());
  2514     Loop* l = innermost;
  2515     Loop* p = l->parent();
  2516     while (p && l->head() == blk) {
  2517       l->set_sibling(p->child());  // Put self on parents 'next child'
  2518       p->set_child(l);             // Make self the first child of parent
  2519       p->def_locals()->add(l->def_locals());
  2520       l = p;                       // Walk up the parent chain
  2521       p = l->parent();
  2523   } else {
  2524     blk->set_loop(innermost);
  2525     innermost->def_locals()->add(blk->def_locals());
  2529 // ------------------------------------------------------------------
  2530 // ciTypeFlow::Loop::contains
  2531 //
  2532 // Returns true if lp is nested loop.
  2533 bool ciTypeFlow::Loop::contains(ciTypeFlow::Loop* lp) const {
  2534   assert(lp != NULL, "");
  2535   if (this == lp || head() == lp->head()) return true;
  2536   int depth1 = depth();
  2537   int depth2 = lp->depth();
  2538   if (depth1 > depth2)
  2539     return false;
  2540   while (depth1 < depth2) {
  2541     depth2--;
  2542     lp = lp->parent();
  2544   return this == lp;
  2547 // ------------------------------------------------------------------
  2548 // ciTypeFlow::Loop::depth
  2549 //
  2550 // Loop depth
  2551 int ciTypeFlow::Loop::depth() const {
  2552   int dp = 0;
  2553   for (Loop* lp = this->parent(); lp != NULL; lp = lp->parent())
  2554     dp++;
  2555   return dp;
  2558 #ifndef PRODUCT
  2559 // ------------------------------------------------------------------
  2560 // ciTypeFlow::Loop::print
  2561 void ciTypeFlow::Loop::print(outputStream* st, int indent) const {
  2562   for (int i = 0; i < indent; i++) st->print(" ");
  2563   st->print("%d<-%d %s",
  2564             is_root() ? 0 : this->head()->pre_order(),
  2565             is_root() ? 0 : this->tail()->pre_order(),
  2566             is_irreducible()?" irr":"");
  2567   st->print(" defs: ");
  2568   def_locals()->print_on(st, _head->outer()->method()->max_locals());
  2569   st->cr();
  2570   for (Loop* ch = child(); ch != NULL; ch = ch->sibling())
  2571     ch->print(st, indent+2);
  2573 #endif
  2575 // ------------------------------------------------------------------
  2576 // ciTypeFlow::df_flow_types
  2577 //
  2578 // Perform the depth first type flow analysis. Helper for flow_types.
  2579 void ciTypeFlow::df_flow_types(Block* start,
  2580                                bool do_flow,
  2581                                StateVector* temp_vector,
  2582                                JsrSet* temp_set) {
  2583   int dft_len = 100;
  2584   GrowableArray<Block*> stk(arena(), dft_len, 0, NULL);
  2586   ciBlock* dummy = _methodBlocks->make_dummy_block();
  2587   JsrSet* root_set = new JsrSet(NULL, 0);
  2588   Block* root_head = new (arena()) Block(this, dummy, root_set);
  2589   Block* root_tail = new (arena()) Block(this, dummy, root_set);
  2590   root_head->set_pre_order(0);
  2591   root_head->set_post_order(0);
  2592   root_tail->set_pre_order(max_jint);
  2593   root_tail->set_post_order(max_jint);
  2594   set_loop_tree_root(new (arena()) Loop(root_head, root_tail));
  2596   stk.push(start);
  2598   _next_pre_order = 0;  // initialize pre_order counter
  2599   _rpo_list = NULL;
  2600   int next_po = 0;      // initialize post_order counter
  2602   // Compute RPO and the control flow graph
  2603   int size;
  2604   while ((size = stk.length()) > 0) {
  2605     Block* blk = stk.top(); // Leave node on stack
  2606     if (!blk->is_visited()) {
  2607       // forward arc in graph
  2608       assert (!blk->has_pre_order(), "");
  2609       blk->set_next_pre_order();
  2611       if (_next_pre_order >= MaxNodeLimit / 2) {
  2612         // Too many basic blocks.  Bail out.
  2613         // This can happen when try/finally constructs are nested to depth N,
  2614         // and there is O(2**N) cloning of jsr bodies.  See bug 4697245!
  2615         // "MaxNodeLimit / 2" is used because probably the parser will
  2616         // generate at least twice that many nodes and bail out.
  2617         record_failure("too many basic blocks");
  2618         return;
  2620       if (do_flow) {
  2621         flow_block(blk, temp_vector, temp_set);
  2622         if (failing()) return; // Watch for bailouts.
  2624     } else if (!blk->is_post_visited()) {
  2625       // cross or back arc
  2626       for (SuccIter iter(blk); !iter.done(); iter.next()) {
  2627         Block* succ = iter.succ();
  2628         if (!succ->is_visited()) {
  2629           stk.push(succ);
  2632       if (stk.length() == size) {
  2633         // There were no additional children, post visit node now
  2634         stk.pop(); // Remove node from stack
  2636         build_loop_tree(blk);
  2637         blk->set_post_order(next_po++);   // Assign post order
  2638         prepend_to_rpo_list(blk);
  2639         assert(blk->is_post_visited(), "");
  2641         if (blk->is_loop_head() && !blk->is_on_work_list()) {
  2642           // Assume loop heads need more data flow
  2643           add_to_work_list(blk);
  2646     } else {
  2647       stk.pop(); // Remove post-visited node from stack
  2652 // ------------------------------------------------------------------
  2653 // ciTypeFlow::flow_types
  2654 //
  2655 // Perform the type flow analysis, creating and cloning Blocks as
  2656 // necessary.
  2657 void ciTypeFlow::flow_types() {
  2658   ResourceMark rm;
  2659   StateVector* temp_vector = new StateVector(this);
  2660   JsrSet* temp_set = new JsrSet(NULL, 16);
  2662   // Create the method entry block.
  2663   Block* start = block_at(start_bci(), temp_set);
  2665   // Load the initial state into it.
  2666   const StateVector* start_state = get_start_state();
  2667   if (failing())  return;
  2668   start->meet(start_state);
  2670   // Depth first visit
  2671   df_flow_types(start, true /*do flow*/, temp_vector, temp_set);
  2673   if (failing())  return;
  2674   assert(_rpo_list == start, "must be start");
  2676   // Any loops found?
  2677   if (loop_tree_root()->child() != NULL &&
  2678       env()->comp_level() >= CompLevel_full_optimization) {
  2679       // Loop optimizations are not performed on Tier1 compiles.
  2681     bool changed = clone_loop_heads(loop_tree_root(), temp_vector, temp_set);
  2683     // If some loop heads were cloned, recompute postorder and loop tree
  2684     if (changed) {
  2685       loop_tree_root()->set_child(NULL);
  2686       for (Block* blk = _rpo_list; blk != NULL;) {
  2687         Block* next = blk->rpo_next();
  2688         blk->df_init();
  2689         blk = next;
  2691       df_flow_types(start, false /*no flow*/, temp_vector, temp_set);
  2695   if (CITraceTypeFlow) {
  2696     tty->print_cr("\nLoop tree");
  2697     loop_tree_root()->print();
  2700   // Continue flow analysis until fixed point reached
  2702   debug_only(int max_block = _next_pre_order;)
  2704   while (!work_list_empty()) {
  2705     Block* blk = work_list_next();
  2706     assert (blk->has_post_order(), "post order assigned above");
  2708     flow_block(blk, temp_vector, temp_set);
  2710     assert (max_block == _next_pre_order, "no new blocks");
  2711     assert (!failing(), "no more bailouts");
  2715 // ------------------------------------------------------------------
  2716 // ciTypeFlow::map_blocks
  2717 //
  2718 // Create the block map, which indexes blocks in reverse post-order.
  2719 void ciTypeFlow::map_blocks() {
  2720   assert(_block_map == NULL, "single initialization");
  2721   int block_ct = _next_pre_order;
  2722   _block_map = NEW_ARENA_ARRAY(arena(), Block*, block_ct);
  2723   assert(block_ct == block_count(), "");
  2725   Block* blk = _rpo_list;
  2726   for (int m = 0; m < block_ct; m++) {
  2727     int rpo = blk->rpo();
  2728     assert(rpo == m, "should be sequential");
  2729     _block_map[rpo] = blk;
  2730     blk = blk->rpo_next();
  2732   assert(blk == NULL, "should be done");
  2734   for (int j = 0; j < block_ct; j++) {
  2735     assert(_block_map[j] != NULL, "must not drop any blocks");
  2736     Block* block = _block_map[j];
  2737     // Remove dead blocks from successor lists:
  2738     for (int e = 0; e <= 1; e++) {
  2739       GrowableArray<Block*>* l = e? block->exceptions(): block->successors();
  2740       for (int k = 0; k < l->length(); k++) {
  2741         Block* s = l->at(k);
  2742         if (!s->has_post_order()) {
  2743           if (CITraceTypeFlow) {
  2744             tty->print("Removing dead %s successor of #%d: ", (e? "exceptional":  "normal"), block->pre_order());
  2745             s->print_value_on(tty);
  2746             tty->cr();
  2748           l->remove(s);
  2749           --k;
  2756 // ------------------------------------------------------------------
  2757 // ciTypeFlow::get_block_for
  2758 //
  2759 // Find a block with this ciBlock which has a compatible JsrSet.
  2760 // If no such block exists, create it, unless the option is no_create.
  2761 // If the option is create_backedge_copy, always create a fresh backedge copy.
  2762 ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
  2763   Arena* a = arena();
  2764   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
  2765   if (blocks == NULL) {
  2766     // Query only?
  2767     if (option == no_create)  return NULL;
  2769     // Allocate the growable array.
  2770     blocks = new (a) GrowableArray<Block*>(a, 4, 0, NULL);
  2771     _idx_to_blocklist[ciBlockIndex] = blocks;
  2774   if (option != create_backedge_copy) {
  2775     int len = blocks->length();
  2776     for (int i = 0; i < len; i++) {
  2777       Block* block = blocks->at(i);
  2778       if (!block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
  2779         return block;
  2784   // Query only?
  2785   if (option == no_create)  return NULL;
  2787   // We did not find a compatible block.  Create one.
  2788   Block* new_block = new (a) Block(this, _methodBlocks->block(ciBlockIndex), jsrs);
  2789   if (option == create_backedge_copy)  new_block->set_backedge_copy(true);
  2790   blocks->append(new_block);
  2791   return new_block;
  2794 // ------------------------------------------------------------------
  2795 // ciTypeFlow::backedge_copy_count
  2796 //
  2797 int ciTypeFlow::backedge_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const {
  2798   GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
  2800   if (blocks == NULL) {
  2801     return 0;
  2804   int count = 0;
  2805   int len = blocks->length();
  2806   for (int i = 0; i < len; i++) {
  2807     Block* block = blocks->at(i);
  2808     if (block->is_backedge_copy() && block->is_compatible_with(jsrs)) {
  2809       count++;
  2813   return count;
  2816 // ------------------------------------------------------------------
  2817 // ciTypeFlow::do_flow
  2818 //
  2819 // Perform type inference flow analysis.
  2820 void ciTypeFlow::do_flow() {
  2821   if (CITraceTypeFlow) {
  2822     tty->print_cr("\nPerforming flow analysis on method");
  2823     method()->print();
  2824     if (is_osr_flow())  tty->print(" at OSR bci %d", start_bci());
  2825     tty->cr();
  2826     method()->print_codes();
  2828   if (CITraceTypeFlow) {
  2829     tty->print_cr("Initial CI Blocks");
  2830     print_on(tty);
  2832   flow_types();
  2833   // Watch for bailouts.
  2834   if (failing()) {
  2835     return;
  2838   map_blocks();
  2840   if (CIPrintTypeFlow || CITraceTypeFlow) {
  2841     rpo_print_on(tty);
  2845 // ------------------------------------------------------------------
  2846 // ciTypeFlow::record_failure()
  2847 // The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
  2848 // This is required because there is not a 1-1 relation between the ciEnv and
  2849 // the TypeFlow passes within a compilation task.  For example, if the compiler
  2850 // is considering inlining a method, it will request a TypeFlow.  If that fails,
  2851 // the compilation as a whole may continue without the inlining.  Some TypeFlow
  2852 // requests are not optional; if they fail the requestor is responsible for
  2853 // copying the failure reason up to the ciEnv.  (See Parse::Parse.)
  2854 void ciTypeFlow::record_failure(const char* reason) {
  2855   if (env()->log() != NULL) {
  2856     env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
  2858   if (_failure_reason == NULL) {
  2859     // Record the first failure reason.
  2860     _failure_reason = reason;
  2864 #ifndef PRODUCT
  2865 // ------------------------------------------------------------------
  2866 // ciTypeFlow::print_on
  2867 void ciTypeFlow::print_on(outputStream* st) const {
  2868   // Walk through CI blocks
  2869   st->print_cr("********************************************************");
  2870   st->print   ("TypeFlow for ");
  2871   method()->name()->print_symbol_on(st);
  2872   int limit_bci = code_size();
  2873   st->print_cr("  %d bytes", limit_bci);
  2874   ciMethodBlocks  *mblks = _methodBlocks;
  2875   ciBlock* current = NULL;
  2876   for (int bci = 0; bci < limit_bci; bci++) {
  2877     ciBlock* blk = mblks->block_containing(bci);
  2878     if (blk != NULL && blk != current) {
  2879       current = blk;
  2880       current->print_on(st);
  2882       GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()];
  2883       int num_blocks = (blocks == NULL) ? 0 : blocks->length();
  2885       if (num_blocks == 0) {
  2886         st->print_cr("  No Blocks");
  2887       } else {
  2888         for (int i = 0; i < num_blocks; i++) {
  2889           Block* block = blocks->at(i);
  2890           block->print_on(st);
  2893       st->print_cr("--------------------------------------------------------");
  2894       st->cr();
  2897   st->print_cr("********************************************************");
  2898   st->cr();
  2901 void ciTypeFlow::rpo_print_on(outputStream* st) const {
  2902   st->print_cr("********************************************************");
  2903   st->print   ("TypeFlow for ");
  2904   method()->name()->print_symbol_on(st);
  2905   int limit_bci = code_size();
  2906   st->print_cr("  %d bytes", limit_bci);
  2907   for (Block* blk = _rpo_list; blk != NULL; blk = blk->rpo_next()) {
  2908     blk->print_on(st);
  2909     st->print_cr("--------------------------------------------------------");
  2910     st->cr();
  2912   st->print_cr("********************************************************");
  2913   st->cr();
  2915 #endif

mercurial