src/share/vm/ci/ciTypeFlow.cpp

changeset 435
a61af66fc99e
child 802
194b8e3a2fc4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/ci/ciTypeFlow.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,2469 @@
     1.4 +/*
     1.5 + * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "incls/_precompiled.incl"
    1.29 +#include "incls/_ciTypeFlow.cpp.incl"
    1.30 +
    1.31 +// ciTypeFlow::JsrSet
    1.32 +//
    1.33 +// A JsrSet represents some set of JsrRecords.  This class
    1.34 +// is used to record a set of all jsr routines which we permit
    1.35 +// execution to return (ret) from.
    1.36 +//
    1.37 +// During abstract interpretation, JsrSets are used to determine
    1.38 +// whether two paths which reach a given block are unique, and
    1.39 +// should be cloned apart, or are compatible, and should merge
    1.40 +// together.
    1.41 +
    1.42 +// ------------------------------------------------------------------
    1.43 +// ciTypeFlow::JsrSet::JsrSet
    1.44 +ciTypeFlow::JsrSet::JsrSet(Arena* arena, int default_len) {
    1.45 +  if (arena != NULL) {
    1.46 +    // Allocate growable array in Arena.
    1.47 +    _set = new (arena) GrowableArray<JsrRecord*>(arena, default_len, 0, NULL);
    1.48 +  } else {
    1.49 +    // Allocate growable array in current ResourceArea.
    1.50 +    _set = new GrowableArray<JsrRecord*>(4, 0, NULL, false);
    1.51 +  }
    1.52 +}
    1.53 +
    1.54 +// ------------------------------------------------------------------
    1.55 +// ciTypeFlow::JsrSet::copy_into
    1.56 +void ciTypeFlow::JsrSet::copy_into(JsrSet* jsrs) {
    1.57 +  int len = size();
    1.58 +  jsrs->_set->clear();
    1.59 +  for (int i = 0; i < len; i++) {
    1.60 +    jsrs->_set->append(_set->at(i));
    1.61 +  }
    1.62 +}
    1.63 +
    1.64 +// ------------------------------------------------------------------
    1.65 +// ciTypeFlow::JsrSet::is_compatible_with
    1.66 +//
    1.67 +// !!!! MISGIVINGS ABOUT THIS... disregard
    1.68 +//
    1.69 +// Is this JsrSet compatible with some other JsrSet?
    1.70 +//
    1.71 +// In set-theoretic terms, a JsrSet can be viewed as a partial function
    1.72 +// from entry addresses to return addresses.  Two JsrSets A and B are
    1.73 +// compatible iff
    1.74 +//
    1.75 +//   For any x,
    1.76 +//   A(x) defined and B(x) defined implies A(x) == B(x)
    1.77 +//
    1.78 +// Less formally, two JsrSets are compatible when they have identical
    1.79 +// return addresses for any entry addresses they share in common.
    1.80 +bool ciTypeFlow::JsrSet::is_compatible_with(JsrSet* other) {
    1.81 +  // Walk through both sets in parallel.  If the same entry address
    1.82 +  // appears in both sets, then the return address must match for
    1.83 +  // the sets to be compatible.
    1.84 +  int size1 = size();
    1.85 +  int size2 = other->size();
    1.86 +
    1.87 +  // Special case.  If nothing is on the jsr stack, then there can
    1.88 +  // be no ret.
    1.89 +  if (size2 == 0) {
    1.90 +    return true;
    1.91 +  } else if (size1 != size2) {
    1.92 +    return false;
    1.93 +  } else {
    1.94 +    for (int i = 0; i < size1; i++) {
    1.95 +      JsrRecord* record1 = record_at(i);
    1.96 +      JsrRecord* record2 = other->record_at(i);
    1.97 +      if (record1->entry_address() != record2->entry_address() ||
    1.98 +          record1->return_address() != record2->return_address()) {
    1.99 +        return false;
   1.100 +      }
   1.101 +    }
   1.102 +    return true;
   1.103 +  }
   1.104 +
   1.105 +#if 0
   1.106 +  int pos1 = 0;
   1.107 +  int pos2 = 0;
   1.108 +  int size1 = size();
   1.109 +  int size2 = other->size();
   1.110 +  while (pos1 < size1 && pos2 < size2) {
   1.111 +    JsrRecord* record1 = record_at(pos1);
   1.112 +    JsrRecord* record2 = other->record_at(pos2);
   1.113 +    int entry1 = record1->entry_address();
   1.114 +    int entry2 = record2->entry_address();
   1.115 +    if (entry1 < entry2) {
   1.116 +      pos1++;
   1.117 +    } else if (entry1 > entry2) {
   1.118 +      pos2++;
   1.119 +    } else {
   1.120 +      if (record1->return_address() == record2->return_address()) {
   1.121 +        pos1++;
   1.122 +        pos2++;
   1.123 +      } else {
   1.124 +        // These two JsrSets are incompatible.
   1.125 +        return false;
   1.126 +      }
   1.127 +    }
   1.128 +  }
   1.129 +  // The two JsrSets agree.
   1.130 +  return true;
   1.131 +#endif
   1.132 +}
   1.133 +
   1.134 +// ------------------------------------------------------------------
   1.135 +// ciTypeFlow::JsrSet::insert_jsr_record
   1.136 +//
   1.137 +// Insert the given JsrRecord into the JsrSet, maintaining the order
   1.138 +// of the set and replacing any element with the same entry address.
   1.139 +void ciTypeFlow::JsrSet::insert_jsr_record(JsrRecord* record) {
   1.140 +  int len = size();
   1.141 +  int entry = record->entry_address();
   1.142 +  int pos = 0;
   1.143 +  for ( ; pos < len; pos++) {
   1.144 +    JsrRecord* current = record_at(pos);
   1.145 +    if (entry == current->entry_address()) {
   1.146 +      // Stomp over this entry.
   1.147 +      _set->at_put(pos, record);
   1.148 +      assert(size() == len, "must be same size");
   1.149 +      return;
   1.150 +    } else if (entry < current->entry_address()) {
   1.151 +      break;
   1.152 +    }
   1.153 +  }
   1.154 +
   1.155 +  // Insert the record into the list.
   1.156 +  JsrRecord* swap = record;
   1.157 +  JsrRecord* temp = NULL;
   1.158 +  for ( ; pos < len; pos++) {
   1.159 +    temp = _set->at(pos);
   1.160 +    _set->at_put(pos, swap);
   1.161 +    swap = temp;
   1.162 +  }
   1.163 +  _set->append(swap);
   1.164 +  assert(size() == len+1, "must be larger");
   1.165 +}
   1.166 +
   1.167 +// ------------------------------------------------------------------
   1.168 +// ciTypeFlow::JsrSet::remove_jsr_record
   1.169 +//
   1.170 +// Remove the JsrRecord with the given return address from the JsrSet.
   1.171 +void ciTypeFlow::JsrSet::remove_jsr_record(int return_address) {
   1.172 +  int len = size();
   1.173 +  for (int i = 0; i < len; i++) {
   1.174 +    if (record_at(i)->return_address() == return_address) {
   1.175 +      // We have found the proper entry.  Remove it from the
   1.176 +      // JsrSet and exit.
   1.177 +      for (int j = i+1; j < len ; j++) {
   1.178 +        _set->at_put(j-1, _set->at(j));
   1.179 +      }
   1.180 +      _set->trunc_to(len-1);
   1.181 +      assert(size() == len-1, "must be smaller");
   1.182 +      return;
   1.183 +    }
   1.184 +  }
   1.185 +  assert(false, "verify: returning from invalid subroutine");
   1.186 +}
   1.187 +
   1.188 +// ------------------------------------------------------------------
   1.189 +// ciTypeFlow::JsrSet::apply_control
   1.190 +//
   1.191 +// Apply the effect of a control-flow bytecode on the JsrSet.  The
   1.192 +// only bytecodes that modify the JsrSet are jsr and ret.
   1.193 +void ciTypeFlow::JsrSet::apply_control(ciTypeFlow* analyzer,
   1.194 +                                       ciBytecodeStream* str,
   1.195 +                                       ciTypeFlow::StateVector* state) {
   1.196 +  Bytecodes::Code code = str->cur_bc();
   1.197 +  if (code == Bytecodes::_jsr) {
   1.198 +    JsrRecord* record =
   1.199 +      analyzer->make_jsr_record(str->get_dest(), str->next_bci());
   1.200 +    insert_jsr_record(record);
   1.201 +  } else if (code == Bytecodes::_jsr_w) {
   1.202 +    JsrRecord* record =
   1.203 +      analyzer->make_jsr_record(str->get_far_dest(), str->next_bci());
   1.204 +    insert_jsr_record(record);
   1.205 +  } else if (code == Bytecodes::_ret) {
   1.206 +    Cell local = state->local(str->get_index());
   1.207 +    ciType* return_address = state->type_at(local);
   1.208 +    assert(return_address->is_return_address(), "verify: wrong type");
   1.209 +    if (size() == 0) {
   1.210 +      // Ret-state underflow:  Hit a ret w/o any previous jsrs.  Bail out.
   1.211 +      // This can happen when a loop is inside a finally clause (4614060).
   1.212 +      analyzer->record_failure("OSR in finally clause");
   1.213 +      return;
   1.214 +    }
   1.215 +    remove_jsr_record(return_address->as_return_address()->bci());
   1.216 +  }
   1.217 +}
   1.218 +
   1.219 +#ifndef PRODUCT
   1.220 +// ------------------------------------------------------------------
   1.221 +// ciTypeFlow::JsrSet::print_on
   1.222 +void ciTypeFlow::JsrSet::print_on(outputStream* st) const {
   1.223 +  st->print("{ ");
   1.224 +  int num_elements = size();
   1.225 +  if (num_elements > 0) {
   1.226 +    int i = 0;
   1.227 +    for( ; i < num_elements - 1; i++) {
   1.228 +      _set->at(i)->print_on(st);
   1.229 +      st->print(", ");
   1.230 +    }
   1.231 +    _set->at(i)->print_on(st);
   1.232 +    st->print(" ");
   1.233 +  }
   1.234 +  st->print("}");
   1.235 +}
   1.236 +#endif
   1.237 +
   1.238 +// ciTypeFlow::StateVector
   1.239 +//
   1.240 +// A StateVector summarizes the type information at some point in
   1.241 +// the program.
   1.242 +
   1.243 +// ------------------------------------------------------------------
   1.244 +// ciTypeFlow::StateVector::type_meet
   1.245 +//
   1.246 +// Meet two types.
   1.247 +//
   1.248 +// The semi-lattice of types use by this analysis are modeled on those
   1.249 +// of the verifier.  The lattice is as follows:
   1.250 +//
   1.251 +//        top_type() >= all non-extremal types >= bottom_type
   1.252 +//                             and
   1.253 +//   Every primitive type is comparable only with itself.  The meet of
   1.254 +//   reference types is determined by their kind: instance class,
   1.255 +//   interface, or array class.  The meet of two types of the same
   1.256 +//   kind is their least common ancestor.  The meet of two types of
   1.257 +//   different kinds is always java.lang.Object.
   1.258 +ciType* ciTypeFlow::StateVector::type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer) {
   1.259 +  assert(t1 != t2, "checked in caller");
   1.260 +  if (t1->equals(top_type())) {
   1.261 +    return t2;
   1.262 +  } else if (t2->equals(top_type())) {
   1.263 +    return t1;
   1.264 +  } else if (t1->is_primitive_type() || t2->is_primitive_type()) {
   1.265 +    // Special case null_type.  null_type meet any reference type T
   1.266 +    // is T.  null_type meet null_type is null_type.
   1.267 +    if (t1->equals(null_type())) {
   1.268 +      if (!t2->is_primitive_type() || t2->equals(null_type())) {
   1.269 +        return t2;
   1.270 +      }
   1.271 +    } else if (t2->equals(null_type())) {
   1.272 +      if (!t1->is_primitive_type()) {
   1.273 +        return t1;
   1.274 +      }
   1.275 +    }
   1.276 +
   1.277 +    // At least one of the two types is a non-top primitive type.
   1.278 +    // The other type is not equal to it.  Fall to bottom.
   1.279 +    return bottom_type();
   1.280 +  } else {
   1.281 +    // Both types are non-top non-primitive types.  That is,
   1.282 +    // both types are either instanceKlasses or arrayKlasses.
   1.283 +    ciKlass* object_klass = analyzer->env()->Object_klass();
   1.284 +    ciKlass* k1 = t1->as_klass();
   1.285 +    ciKlass* k2 = t2->as_klass();
   1.286 +    if (k1->equals(object_klass) || k2->equals(object_klass)) {
   1.287 +      return object_klass;
   1.288 +    } else if (!k1->is_loaded() || !k2->is_loaded()) {
   1.289 +      // Unloaded classes fall to java.lang.Object at a merge.
   1.290 +      return object_klass;
   1.291 +    } else if (k1->is_interface() != k2->is_interface()) {
   1.292 +      // When an interface meets a non-interface, we get Object;
   1.293 +      // This is what the verifier does.
   1.294 +      return object_klass;
   1.295 +    } else if (k1->is_array_klass() || k2->is_array_klass()) {
   1.296 +      // When an array meets a non-array, we get Object.
   1.297 +      // When objArray meets typeArray, we also get Object.
   1.298 +      // And when typeArray meets different typeArray, we again get Object.
   1.299 +      // But when objArray meets objArray, we look carefully at element types.
   1.300 +      if (k1->is_obj_array_klass() && k2->is_obj_array_klass()) {
   1.301 +        // Meet the element types, then construct the corresponding array type.
   1.302 +        ciKlass* elem1 = k1->as_obj_array_klass()->element_klass();
   1.303 +        ciKlass* elem2 = k2->as_obj_array_klass()->element_klass();
   1.304 +        ciKlass* elem  = type_meet_internal(elem1, elem2, analyzer)->as_klass();
   1.305 +        // Do an easy shortcut if one type is a super of the other.
   1.306 +        if (elem == elem1) {
   1.307 +          assert(k1 == ciObjArrayKlass::make(elem), "shortcut is OK");
   1.308 +          return k1;
   1.309 +        } else if (elem == elem2) {
   1.310 +          assert(k2 == ciObjArrayKlass::make(elem), "shortcut is OK");
   1.311 +          return k2;
   1.312 +        } else {
   1.313 +          return ciObjArrayKlass::make(elem);
   1.314 +        }
   1.315 +      } else {
   1.316 +        return object_klass;
   1.317 +      }
   1.318 +    } else {
   1.319 +      // Must be two plain old instance klasses.
   1.320 +      assert(k1->is_instance_klass(), "previous cases handle non-instances");
   1.321 +      assert(k2->is_instance_klass(), "previous cases handle non-instances");
   1.322 +      return k1->least_common_ancestor(k2);
   1.323 +    }
   1.324 +  }
   1.325 +}
   1.326 +
   1.327 +
   1.328 +// ------------------------------------------------------------------
   1.329 +// ciTypeFlow::StateVector::StateVector
   1.330 +//
   1.331 +// Build a new state vector
   1.332 +ciTypeFlow::StateVector::StateVector(ciTypeFlow* analyzer) {
   1.333 +  _outer = analyzer;
   1.334 +  _stack_size = -1;
   1.335 +  _monitor_count = -1;
   1.336 +  // Allocate the _types array
   1.337 +  int max_cells = analyzer->max_cells();
   1.338 +  _types = (ciType**)analyzer->arena()->Amalloc(sizeof(ciType*) * max_cells);
   1.339 +  for (int i=0; i<max_cells; i++) {
   1.340 +    _types[i] = top_type();
   1.341 +  }
   1.342 +  _trap_bci = -1;
   1.343 +  _trap_index = 0;
   1.344 +}
   1.345 +
   1.346 +// ------------------------------------------------------------------
   1.347 +// ciTypeFlow::get_start_state
   1.348 +//
   1.349 +// Set this vector to the method entry state.
   1.350 +const ciTypeFlow::StateVector* ciTypeFlow::get_start_state() {
   1.351 +  StateVector* state = new StateVector(this);
   1.352 +  if (is_osr_flow()) {
   1.353 +    ciTypeFlow* non_osr_flow = method()->get_flow_analysis();
   1.354 +    if (non_osr_flow->failing()) {
   1.355 +      record_failure(non_osr_flow->failure_reason());
   1.356 +      return NULL;
   1.357 +    }
   1.358 +    JsrSet* jsrs = new JsrSet(NULL, 16);
   1.359 +    Block* non_osr_block = non_osr_flow->existing_block_at(start_bci(), jsrs);
   1.360 +    if (non_osr_block == NULL) {
   1.361 +      record_failure("cannot reach OSR point");
   1.362 +      return NULL;
   1.363 +    }
   1.364 +    // load up the non-OSR state at this point
   1.365 +    non_osr_block->copy_state_into(state);
   1.366 +    int non_osr_start = non_osr_block->start();
   1.367 +    if (non_osr_start != start_bci()) {
   1.368 +      // must flow forward from it
   1.369 +      if (CITraceTypeFlow) {
   1.370 +        tty->print_cr(">> Interpreting pre-OSR block %d:", non_osr_start);
   1.371 +      }
   1.372 +      Block* block = block_at(non_osr_start, jsrs);
   1.373 +      assert(block->limit() == start_bci(), "must flow forward to start");
   1.374 +      flow_block(block, state, jsrs);
   1.375 +    }
   1.376 +    return state;
   1.377 +    // Note:  The code below would be an incorrect for an OSR flow,
   1.378 +    // even if it were possible for an OSR entry point to be at bci zero.
   1.379 +  }
   1.380 +  // "Push" the method signature into the first few locals.
   1.381 +  state->set_stack_size(-max_locals());
   1.382 +  if (!method()->is_static()) {
   1.383 +    state->push(method()->holder());
   1.384 +    assert(state->tos() == state->local(0), "");
   1.385 +  }
   1.386 +  for (ciSignatureStream str(method()->signature());
   1.387 +       !str.at_return_type();
   1.388 +       str.next()) {
   1.389 +    state->push_translate(str.type());
   1.390 +  }
   1.391 +  // Set the rest of the locals to bottom.
   1.392 +  Cell cell = state->next_cell(state->tos());
   1.393 +  state->set_stack_size(0);
   1.394 +  int limit = state->limit_cell();
   1.395 +  for (; cell < limit; cell = state->next_cell(cell)) {
   1.396 +    state->set_type_at(cell, state->bottom_type());
   1.397 +  }
   1.398 +  // Lock an object, if necessary.
   1.399 +  state->set_monitor_count(method()->is_synchronized() ? 1 : 0);
   1.400 +  return state;
   1.401 +}
   1.402 +
   1.403 +// ------------------------------------------------------------------
   1.404 +// ciTypeFlow::StateVector::copy_into
   1.405 +//
   1.406 +// Copy our value into some other StateVector
   1.407 +void ciTypeFlow::StateVector::copy_into(ciTypeFlow::StateVector* copy)
   1.408 +const {
   1.409 +  copy->set_stack_size(stack_size());
   1.410 +  copy->set_monitor_count(monitor_count());
   1.411 +  Cell limit = limit_cell();
   1.412 +  for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   1.413 +    copy->set_type_at(c, type_at(c));
   1.414 +  }
   1.415 +}
   1.416 +
   1.417 +// ------------------------------------------------------------------
   1.418 +// ciTypeFlow::StateVector::meet
   1.419 +//
   1.420 +// Meets this StateVector with another, destructively modifying this
   1.421 +// one.  Returns true if any modification takes place.
   1.422 +bool ciTypeFlow::StateVector::meet(const ciTypeFlow::StateVector* incoming) {
   1.423 +  if (monitor_count() == -1) {
   1.424 +    set_monitor_count(incoming->monitor_count());
   1.425 +  }
   1.426 +  assert(monitor_count() == incoming->monitor_count(), "monitors must match");
   1.427 +
   1.428 +  if (stack_size() == -1) {
   1.429 +    set_stack_size(incoming->stack_size());
   1.430 +    Cell limit = limit_cell();
   1.431 +    #ifdef ASSERT
   1.432 +    { for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   1.433 +        assert(type_at(c) == top_type(), "");
   1.434 +    } }
   1.435 +    #endif
   1.436 +    // Make a simple copy of the incoming state.
   1.437 +    for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   1.438 +      set_type_at(c, incoming->type_at(c));
   1.439 +    }
   1.440 +    return true;  // it is always different the first time
   1.441 +  }
   1.442 +#ifdef ASSERT
   1.443 +  if (stack_size() != incoming->stack_size()) {
   1.444 +    _outer->method()->print_codes();
   1.445 +    tty->print_cr("!!!! Stack size conflict");
   1.446 +    tty->print_cr("Current state:");
   1.447 +    print_on(tty);
   1.448 +    tty->print_cr("Incoming state:");
   1.449 +    ((StateVector*)incoming)->print_on(tty);
   1.450 +  }
   1.451 +#endif
   1.452 +  assert(stack_size() == incoming->stack_size(), "sanity");
   1.453 +
   1.454 +  bool different = false;
   1.455 +  Cell limit = limit_cell();
   1.456 +  for (Cell c = start_cell(); c < limit; c = next_cell(c)) {
   1.457 +    ciType* t1 = type_at(c);
   1.458 +    ciType* t2 = incoming->type_at(c);
   1.459 +    if (!t1->equals(t2)) {
   1.460 +      ciType* new_type = type_meet(t1, t2);
   1.461 +      if (!t1->equals(new_type)) {
   1.462 +        set_type_at(c, new_type);
   1.463 +        different = true;
   1.464 +      }
   1.465 +    }
   1.466 +  }
   1.467 +  return different;
   1.468 +}
   1.469 +
   1.470 +// ------------------------------------------------------------------
   1.471 +// ciTypeFlow::StateVector::meet_exception
   1.472 +//
   1.473 +// Meets this StateVector with another, destructively modifying this
   1.474 +// one.  The incoming state is coming via an exception.  Returns true
   1.475 +// if any modification takes place.
   1.476 +bool ciTypeFlow::StateVector::meet_exception(ciInstanceKlass* exc,
   1.477 +                                     const ciTypeFlow::StateVector* incoming) {
   1.478 +  if (monitor_count() == -1) {
   1.479 +    set_monitor_count(incoming->monitor_count());
   1.480 +  }
   1.481 +  assert(monitor_count() == incoming->monitor_count(), "monitors must match");
   1.482 +
   1.483 +  if (stack_size() == -1) {
   1.484 +    set_stack_size(1);
   1.485 +  }
   1.486 +
   1.487 +  assert(stack_size() ==  1, "must have one-element stack");
   1.488 +
   1.489 +  bool different = false;
   1.490 +
   1.491 +  // Meet locals from incoming array.
   1.492 +  Cell limit = local(_outer->max_locals()-1);
   1.493 +  for (Cell c = start_cell(); c <= limit; c = next_cell(c)) {
   1.494 +    ciType* t1 = type_at(c);
   1.495 +    ciType* t2 = incoming->type_at(c);
   1.496 +    if (!t1->equals(t2)) {
   1.497 +      ciType* new_type = type_meet(t1, t2);
   1.498 +      if (!t1->equals(new_type)) {
   1.499 +        set_type_at(c, new_type);
   1.500 +        different = true;
   1.501 +      }
   1.502 +    }
   1.503 +  }
   1.504 +
   1.505 +  // Handle stack separately.  When an exception occurs, the
   1.506 +  // only stack entry is the exception instance.
   1.507 +  ciType* tos_type = type_at_tos();
   1.508 +  if (!tos_type->equals(exc)) {
   1.509 +    ciType* new_type = type_meet(tos_type, exc);
   1.510 +    if (!tos_type->equals(new_type)) {
   1.511 +      set_type_at_tos(new_type);
   1.512 +      different = true;
   1.513 +    }
   1.514 +  }
   1.515 +
   1.516 +  return different;
   1.517 +}
   1.518 +
   1.519 +// ------------------------------------------------------------------
   1.520 +// ciTypeFlow::StateVector::push_translate
   1.521 +void ciTypeFlow::StateVector::push_translate(ciType* type) {
   1.522 +  BasicType basic_type = type->basic_type();
   1.523 +  if (basic_type == T_BOOLEAN || basic_type == T_CHAR ||
   1.524 +      basic_type == T_BYTE    || basic_type == T_SHORT) {
   1.525 +    push_int();
   1.526 +  } else {
   1.527 +    push(type);
   1.528 +    if (type->is_two_word()) {
   1.529 +      push(half_type(type));
   1.530 +    }
   1.531 +  }
   1.532 +}
   1.533 +
   1.534 +// ------------------------------------------------------------------
   1.535 +// ciTypeFlow::StateVector::do_aaload
   1.536 +void ciTypeFlow::StateVector::do_aaload(ciBytecodeStream* str) {
   1.537 +  pop_int();
   1.538 +  ciObjArrayKlass* array_klass = pop_objArray();
   1.539 +  if (array_klass == NULL) {
   1.540 +    // Did aaload on a null reference; push a null and ignore the exception.
   1.541 +    // This instruction will never continue normally.  All we have to do
   1.542 +    // is report a value that will meet correctly with any downstream
   1.543 +    // reference types on paths that will truly be executed.  This null type
   1.544 +    // meets with any reference type to yield that same reference type.
   1.545 +    // (The compiler will generate an unconditonal exception here.)
   1.546 +    push(null_type());
   1.547 +    return;
   1.548 +  }
   1.549 +  if (!array_klass->is_loaded()) {
   1.550 +    // Only fails for some -Xcomp runs
   1.551 +    trap(str, array_klass,
   1.552 +         Deoptimization::make_trap_request
   1.553 +         (Deoptimization::Reason_unloaded,
   1.554 +          Deoptimization::Action_reinterpret));
   1.555 +    return;
   1.556 +  }
   1.557 +  ciKlass* element_klass = array_klass->element_klass();
   1.558 +  if (!element_klass->is_loaded() && element_klass->is_instance_klass()) {
   1.559 +    Untested("unloaded array element class in ciTypeFlow");
   1.560 +    trap(str, element_klass,
   1.561 +         Deoptimization::make_trap_request
   1.562 +         (Deoptimization::Reason_unloaded,
   1.563 +          Deoptimization::Action_reinterpret));
   1.564 +  } else {
   1.565 +    push_object(element_klass);
   1.566 +  }
   1.567 +}
   1.568 +
   1.569 +
   1.570 +// ------------------------------------------------------------------
   1.571 +// ciTypeFlow::StateVector::do_checkcast
   1.572 +void ciTypeFlow::StateVector::do_checkcast(ciBytecodeStream* str) {
   1.573 +  bool will_link;
   1.574 +  ciKlass* klass = str->get_klass(will_link);
   1.575 +  if (!will_link) {
   1.576 +    // VM's interpreter will not load 'klass' if object is NULL.
   1.577 +    // Type flow after this block may still be needed in two situations:
   1.578 +    // 1) C2 uses do_null_assert() and continues compilation for later blocks
   1.579 +    // 2) C2 does an OSR compile in a later block (see bug 4778368).
   1.580 +    pop_object();
   1.581 +    do_null_assert(klass);
   1.582 +  } else {
   1.583 +    pop_object();
   1.584 +    push_object(klass);
   1.585 +  }
   1.586 +}
   1.587 +
   1.588 +// ------------------------------------------------------------------
   1.589 +// ciTypeFlow::StateVector::do_getfield
   1.590 +void ciTypeFlow::StateVector::do_getfield(ciBytecodeStream* str) {
   1.591 +  // could add assert here for type of object.
   1.592 +  pop_object();
   1.593 +  do_getstatic(str);
   1.594 +}
   1.595 +
   1.596 +// ------------------------------------------------------------------
   1.597 +// ciTypeFlow::StateVector::do_getstatic
   1.598 +void ciTypeFlow::StateVector::do_getstatic(ciBytecodeStream* str) {
   1.599 +  bool will_link;
   1.600 +  ciField* field = str->get_field(will_link);
   1.601 +  if (!will_link) {
   1.602 +    trap(str, field->holder(), str->get_field_holder_index());
   1.603 +  } else {
   1.604 +    ciType* field_type = field->type();
   1.605 +    if (!field_type->is_loaded()) {
   1.606 +      // Normally, we need the field's type to be loaded if we are to
   1.607 +      // do anything interesting with its value.
   1.608 +      // We used to do this:  trap(str, str->get_field_signature_index());
   1.609 +      //
   1.610 +      // There is one good reason not to trap here.  Execution can
   1.611 +      // get past this "getfield" or "getstatic" if the value of
   1.612 +      // the field is null.  As long as the value is null, the class
   1.613 +      // does not need to be loaded!  The compiler must assume that
   1.614 +      // the value of the unloaded class reference is null; if the code
   1.615 +      // ever sees a non-null value, loading has occurred.
   1.616 +      //
   1.617 +      // This actually happens often enough to be annoying.  If the
   1.618 +      // compiler throws an uncommon trap at this bytecode, you can
   1.619 +      // get an endless loop of recompilations, when all the code
   1.620 +      // needs to do is load a series of null values.  Also, a trap
   1.621 +      // here can make an OSR entry point unreachable, triggering the
   1.622 +      // assert on non_osr_block in ciTypeFlow::get_start_state.
   1.623 +      // (See bug 4379915.)
   1.624 +      do_null_assert(field_type->as_klass());
   1.625 +    } else {
   1.626 +      push_translate(field_type);
   1.627 +    }
   1.628 +  }
   1.629 +}
   1.630 +
   1.631 +// ------------------------------------------------------------------
   1.632 +// ciTypeFlow::StateVector::do_invoke
   1.633 +void ciTypeFlow::StateVector::do_invoke(ciBytecodeStream* str,
   1.634 +                                        bool has_receiver) {
   1.635 +  bool will_link;
   1.636 +  ciMethod* method = str->get_method(will_link);
   1.637 +  if (!will_link) {
   1.638 +    // We weren't able to find the method.
   1.639 +    ciKlass* unloaded_holder = method->holder();
   1.640 +    trap(str, unloaded_holder, str->get_method_holder_index());
   1.641 +  } else {
   1.642 +    ciSignature* signature = method->signature();
   1.643 +    ciSignatureStream sigstr(signature);
   1.644 +    int arg_size = signature->size();
   1.645 +    int stack_base = stack_size() - arg_size;
   1.646 +    int i = 0;
   1.647 +    for( ; !sigstr.at_return_type(); sigstr.next()) {
   1.648 +      ciType* type = sigstr.type();
   1.649 +      ciType* stack_type = type_at(stack(stack_base + i++));
   1.650 +      // Do I want to check this type?
   1.651 +      // assert(stack_type->is_subtype_of(type), "bad type for field value");
   1.652 +      if (type->is_two_word()) {
   1.653 +        ciType* stack_type2 = type_at(stack(stack_base + i++));
   1.654 +        assert(stack_type2->equals(half_type(type)), "must be 2nd half");
   1.655 +      }
   1.656 +    }
   1.657 +    assert(arg_size == i, "must match");
   1.658 +    for (int j = 0; j < arg_size; j++) {
   1.659 +      pop();
   1.660 +    }
   1.661 +    if (has_receiver) {
   1.662 +      // Check this?
   1.663 +      pop_object();
   1.664 +    }
   1.665 +    assert(!sigstr.is_done(), "must have return type");
   1.666 +    ciType* return_type = sigstr.type();
   1.667 +    if (!return_type->is_void()) {
   1.668 +      if (!return_type->is_loaded()) {
   1.669 +        // As in do_getstatic(), generally speaking, we need the return type to
   1.670 +        // be loaded if we are to do anything interesting with its value.
   1.671 +        // We used to do this:  trap(str, str->get_method_signature_index());
   1.672 +        //
   1.673 +        // We do not trap here since execution can get past this invoke if
   1.674 +        // the return value is null.  As long as the value is null, the class
   1.675 +        // does not need to be loaded!  The compiler must assume that
   1.676 +        // the value of the unloaded class reference is null; if the code
   1.677 +        // ever sees a non-null value, loading has occurred.
   1.678 +        //
   1.679 +        // See do_getstatic() for similar explanation, as well as bug 4684993.
   1.680 +        do_null_assert(return_type->as_klass());
   1.681 +      } else {
   1.682 +        push_translate(return_type);
   1.683 +      }
   1.684 +    }
   1.685 +  }
   1.686 +}
   1.687 +
   1.688 +// ------------------------------------------------------------------
   1.689 +// ciTypeFlow::StateVector::do_jsr
   1.690 +void ciTypeFlow::StateVector::do_jsr(ciBytecodeStream* str) {
   1.691 +  push(ciReturnAddress::make(str->next_bci()));
   1.692 +}
   1.693 +
   1.694 +// ------------------------------------------------------------------
   1.695 +// ciTypeFlow::StateVector::do_ldc
   1.696 +void ciTypeFlow::StateVector::do_ldc(ciBytecodeStream* str) {
   1.697 +  ciConstant con = str->get_constant();
   1.698 +  BasicType basic_type = con.basic_type();
   1.699 +  if (basic_type == T_ILLEGAL) {
   1.700 +    // OutOfMemoryError in the CI while loading constant
   1.701 +    push_null();
   1.702 +    outer()->record_failure("ldc did not link");
   1.703 +    return;
   1.704 +  }
   1.705 +  if (basic_type == T_OBJECT || basic_type == T_ARRAY) {
   1.706 +    ciObject* obj = con.as_object();
   1.707 +    if (obj->is_null_object()) {
   1.708 +      push_null();
   1.709 +    } else if (obj->is_klass()) {
   1.710 +      // The type of ldc <class> is java.lang.Class
   1.711 +      push_object(outer()->env()->Class_klass());
   1.712 +    } else {
   1.713 +      push_object(obj->klass());
   1.714 +    }
   1.715 +  } else {
   1.716 +    push_translate(ciType::make(basic_type));
   1.717 +  }
   1.718 +}
   1.719 +
   1.720 +// ------------------------------------------------------------------
   1.721 +// ciTypeFlow::StateVector::do_multianewarray
   1.722 +void ciTypeFlow::StateVector::do_multianewarray(ciBytecodeStream* str) {
   1.723 +  int dimensions = str->get_dimensions();
   1.724 +  bool will_link;
   1.725 +  ciArrayKlass* array_klass = str->get_klass(will_link)->as_array_klass();
   1.726 +  if (!will_link) {
   1.727 +    trap(str, array_klass, str->get_klass_index());
   1.728 +  } else {
   1.729 +    for (int i = 0; i < dimensions; i++) {
   1.730 +      pop_int();
   1.731 +    }
   1.732 +    push_object(array_klass);
   1.733 +  }
   1.734 +}
   1.735 +
   1.736 +// ------------------------------------------------------------------
   1.737 +// ciTypeFlow::StateVector::do_new
   1.738 +void ciTypeFlow::StateVector::do_new(ciBytecodeStream* str) {
   1.739 +  bool will_link;
   1.740 +  ciKlass* klass = str->get_klass(will_link);
   1.741 +  if (!will_link) {
   1.742 +    trap(str, klass, str->get_klass_index());
   1.743 +  } else {
   1.744 +    push_object(klass);
   1.745 +  }
   1.746 +}
   1.747 +
   1.748 +// ------------------------------------------------------------------
   1.749 +// ciTypeFlow::StateVector::do_newarray
   1.750 +void ciTypeFlow::StateVector::do_newarray(ciBytecodeStream* str) {
   1.751 +  pop_int();
   1.752 +  ciKlass* klass = ciTypeArrayKlass::make((BasicType)str->get_index());
   1.753 +  push_object(klass);
   1.754 +}
   1.755 +
   1.756 +// ------------------------------------------------------------------
   1.757 +// ciTypeFlow::StateVector::do_putfield
   1.758 +void ciTypeFlow::StateVector::do_putfield(ciBytecodeStream* str) {
   1.759 +  do_putstatic(str);
   1.760 +  if (_trap_bci != -1)  return;  // unloaded field holder, etc.
   1.761 +  // could add assert here for type of object.
   1.762 +  pop_object();
   1.763 +}
   1.764 +
   1.765 +// ------------------------------------------------------------------
   1.766 +// ciTypeFlow::StateVector::do_putstatic
   1.767 +void ciTypeFlow::StateVector::do_putstatic(ciBytecodeStream* str) {
   1.768 +  bool will_link;
   1.769 +  ciField* field = str->get_field(will_link);
   1.770 +  if (!will_link) {
   1.771 +    trap(str, field->holder(), str->get_field_holder_index());
   1.772 +  } else {
   1.773 +    ciType* field_type = field->type();
   1.774 +    ciType* type = pop_value();
   1.775 +    // Do I want to check this type?
   1.776 +    //      assert(type->is_subtype_of(field_type), "bad type for field value");
   1.777 +    if (field_type->is_two_word()) {
   1.778 +      ciType* type2 = pop_value();
   1.779 +      assert(type2->is_two_word(), "must be 2nd half");
   1.780 +      assert(type == half_type(type2), "must be 2nd half");
   1.781 +    }
   1.782 +  }
   1.783 +}
   1.784 +
   1.785 +// ------------------------------------------------------------------
   1.786 +// ciTypeFlow::StateVector::do_ret
   1.787 +void ciTypeFlow::StateVector::do_ret(ciBytecodeStream* str) {
   1.788 +  Cell index = local(str->get_index());
   1.789 +
   1.790 +  ciType* address = type_at(index);
   1.791 +  assert(address->is_return_address(), "bad return address");
   1.792 +  set_type_at(index, bottom_type());
   1.793 +}
   1.794 +
   1.795 +// ------------------------------------------------------------------
   1.796 +// ciTypeFlow::StateVector::trap
   1.797 +//
   1.798 +// Stop interpretation of this path with a trap.
   1.799 +void ciTypeFlow::StateVector::trap(ciBytecodeStream* str, ciKlass* klass, int index) {
   1.800 +  _trap_bci = str->cur_bci();
   1.801 +  _trap_index = index;
   1.802 +
   1.803 +  // Log information about this trap:
   1.804 +  CompileLog* log = outer()->env()->log();
   1.805 +  if (log != NULL) {
   1.806 +    int mid = log->identify(outer()->method());
   1.807 +    int kid = (klass == NULL)? -1: log->identify(klass);
   1.808 +    log->begin_elem("uncommon_trap method='%d' bci='%d'", mid, str->cur_bci());
   1.809 +    char buf[100];
   1.810 +    log->print(" %s", Deoptimization::format_trap_request(buf, sizeof(buf),
   1.811 +                                                          index));
   1.812 +    if (kid >= 0)
   1.813 +      log->print(" klass='%d'", kid);
   1.814 +    log->end_elem();
   1.815 +  }
   1.816 +}
   1.817 +
   1.818 +// ------------------------------------------------------------------
   1.819 +// ciTypeFlow::StateVector::do_null_assert
   1.820 +// Corresponds to graphKit::do_null_assert.
   1.821 +void ciTypeFlow::StateVector::do_null_assert(ciKlass* unloaded_klass) {
   1.822 +  if (unloaded_klass->is_loaded()) {
   1.823 +    // We failed to link, but we can still compute with this class,
   1.824 +    // since it is loaded somewhere.  The compiler will uncommon_trap
   1.825 +    // if the object is not null, but the typeflow pass can not assume
   1.826 +    // that the object will be null, otherwise it may incorrectly tell
   1.827 +    // the parser that an object is known to be null. 4761344, 4807707
   1.828 +    push_object(unloaded_klass);
   1.829 +  } else {
   1.830 +    // The class is not loaded anywhere.  It is safe to model the
   1.831 +    // null in the typestates, because we can compile in a null check
   1.832 +    // which will deoptimize us if someone manages to load the
   1.833 +    // class later.
   1.834 +    push_null();
   1.835 +  }
   1.836 +}
   1.837 +
   1.838 +
   1.839 +// ------------------------------------------------------------------
   1.840 +// ciTypeFlow::StateVector::apply_one_bytecode
   1.841 +//
   1.842 +// Apply the effect of one bytecode to this StateVector
   1.843 +bool ciTypeFlow::StateVector::apply_one_bytecode(ciBytecodeStream* str) {
   1.844 +  _trap_bci = -1;
   1.845 +  _trap_index = 0;
   1.846 +
   1.847 +  if (CITraceTypeFlow) {
   1.848 +    tty->print_cr(">> Interpreting bytecode %d:%s", str->cur_bci(),
   1.849 +                  Bytecodes::name(str->cur_bc()));
   1.850 +  }
   1.851 +
   1.852 +  switch(str->cur_bc()) {
   1.853 +  case Bytecodes::_aaload: do_aaload(str);                       break;
   1.854 +
   1.855 +  case Bytecodes::_aastore:
   1.856 +    {
   1.857 +      pop_object();
   1.858 +      pop_int();
   1.859 +      pop_objArray();
   1.860 +      break;
   1.861 +    }
   1.862 +  case Bytecodes::_aconst_null:
   1.863 +    {
   1.864 +      push_null();
   1.865 +      break;
   1.866 +    }
   1.867 +  case Bytecodes::_aload:   load_local_object(str->get_index());    break;
   1.868 +  case Bytecodes::_aload_0: load_local_object(0);                   break;
   1.869 +  case Bytecodes::_aload_1: load_local_object(1);                   break;
   1.870 +  case Bytecodes::_aload_2: load_local_object(2);                   break;
   1.871 +  case Bytecodes::_aload_3: load_local_object(3);                   break;
   1.872 +
   1.873 +  case Bytecodes::_anewarray:
   1.874 +    {
   1.875 +      pop_int();
   1.876 +      bool will_link;
   1.877 +      ciKlass* element_klass = str->get_klass(will_link);
   1.878 +      if (!will_link) {
   1.879 +        trap(str, element_klass, str->get_klass_index());
   1.880 +      } else {
   1.881 +        push_object(ciObjArrayKlass::make(element_klass));
   1.882 +      }
   1.883 +      break;
   1.884 +    }
   1.885 +  case Bytecodes::_areturn:
   1.886 +  case Bytecodes::_ifnonnull:
   1.887 +  case Bytecodes::_ifnull:
   1.888 +    {
   1.889 +      pop_object();
   1.890 +      break;
   1.891 +    }
   1.892 +  case Bytecodes::_monitorenter:
   1.893 +    {
   1.894 +      pop_object();
   1.895 +      set_monitor_count(monitor_count() + 1);
   1.896 +      break;
   1.897 +    }
   1.898 +  case Bytecodes::_monitorexit:
   1.899 +    {
   1.900 +      pop_object();
   1.901 +      assert(monitor_count() > 0, "must be a monitor to exit from");
   1.902 +      set_monitor_count(monitor_count() - 1);
   1.903 +      break;
   1.904 +    }
   1.905 +  case Bytecodes::_arraylength:
   1.906 +    {
   1.907 +      pop_array();
   1.908 +      push_int();
   1.909 +      break;
   1.910 +    }
   1.911 +  case Bytecodes::_astore:   store_local_object(str->get_index());  break;
   1.912 +  case Bytecodes::_astore_0: store_local_object(0);                 break;
   1.913 +  case Bytecodes::_astore_1: store_local_object(1);                 break;
   1.914 +  case Bytecodes::_astore_2: store_local_object(2);                 break;
   1.915 +  case Bytecodes::_astore_3: store_local_object(3);                 break;
   1.916 +
   1.917 +  case Bytecodes::_athrow:
   1.918 +    {
   1.919 +      NEEDS_CLEANUP;
   1.920 +      pop_object();
   1.921 +      break;
   1.922 +    }
   1.923 +  case Bytecodes::_baload:
   1.924 +  case Bytecodes::_caload:
   1.925 +  case Bytecodes::_iaload:
   1.926 +  case Bytecodes::_saload:
   1.927 +    {
   1.928 +      pop_int();
   1.929 +      ciTypeArrayKlass* array_klass = pop_typeArray();
   1.930 +      // Put assert here for right type?
   1.931 +      push_int();
   1.932 +      break;
   1.933 +    }
   1.934 +  case Bytecodes::_bastore:
   1.935 +  case Bytecodes::_castore:
   1.936 +  case Bytecodes::_iastore:
   1.937 +  case Bytecodes::_sastore:
   1.938 +    {
   1.939 +      pop_int();
   1.940 +      pop_int();
   1.941 +      pop_typeArray();
   1.942 +      // assert here?
   1.943 +      break;
   1.944 +    }
   1.945 +  case Bytecodes::_bipush:
   1.946 +  case Bytecodes::_iconst_m1:
   1.947 +  case Bytecodes::_iconst_0:
   1.948 +  case Bytecodes::_iconst_1:
   1.949 +  case Bytecodes::_iconst_2:
   1.950 +  case Bytecodes::_iconst_3:
   1.951 +  case Bytecodes::_iconst_4:
   1.952 +  case Bytecodes::_iconst_5:
   1.953 +  case Bytecodes::_sipush:
   1.954 +    {
   1.955 +      push_int();
   1.956 +      break;
   1.957 +    }
   1.958 +  case Bytecodes::_checkcast: do_checkcast(str);                  break;
   1.959 +
   1.960 +  case Bytecodes::_d2f:
   1.961 +    {
   1.962 +      pop_double();
   1.963 +      push_float();
   1.964 +      break;
   1.965 +    }
   1.966 +  case Bytecodes::_d2i:
   1.967 +    {
   1.968 +      pop_double();
   1.969 +      push_int();
   1.970 +      break;
   1.971 +    }
   1.972 +  case Bytecodes::_d2l:
   1.973 +    {
   1.974 +      pop_double();
   1.975 +      push_long();
   1.976 +      break;
   1.977 +    }
   1.978 +  case Bytecodes::_dadd:
   1.979 +  case Bytecodes::_ddiv:
   1.980 +  case Bytecodes::_dmul:
   1.981 +  case Bytecodes::_drem:
   1.982 +  case Bytecodes::_dsub:
   1.983 +    {
   1.984 +      pop_double();
   1.985 +      pop_double();
   1.986 +      push_double();
   1.987 +      break;
   1.988 +    }
   1.989 +  case Bytecodes::_daload:
   1.990 +    {
   1.991 +      pop_int();
   1.992 +      ciTypeArrayKlass* array_klass = pop_typeArray();
   1.993 +      // Put assert here for right type?
   1.994 +      push_double();
   1.995 +      break;
   1.996 +    }
   1.997 +  case Bytecodes::_dastore:
   1.998 +    {
   1.999 +      pop_double();
  1.1000 +      pop_int();
  1.1001 +      pop_typeArray();
  1.1002 +      // assert here?
  1.1003 +      break;
  1.1004 +    }
  1.1005 +  case Bytecodes::_dcmpg:
  1.1006 +  case Bytecodes::_dcmpl:
  1.1007 +    {
  1.1008 +      pop_double();
  1.1009 +      pop_double();
  1.1010 +      push_int();
  1.1011 +      break;
  1.1012 +    }
  1.1013 +  case Bytecodes::_dconst_0:
  1.1014 +  case Bytecodes::_dconst_1:
  1.1015 +    {
  1.1016 +      push_double();
  1.1017 +      break;
  1.1018 +    }
  1.1019 +  case Bytecodes::_dload:   load_local_double(str->get_index());    break;
  1.1020 +  case Bytecodes::_dload_0: load_local_double(0);                   break;
  1.1021 +  case Bytecodes::_dload_1: load_local_double(1);                   break;
  1.1022 +  case Bytecodes::_dload_2: load_local_double(2);                   break;
  1.1023 +  case Bytecodes::_dload_3: load_local_double(3);                   break;
  1.1024 +
  1.1025 +  case Bytecodes::_dneg:
  1.1026 +    {
  1.1027 +      pop_double();
  1.1028 +      push_double();
  1.1029 +      break;
  1.1030 +    }
  1.1031 +  case Bytecodes::_dreturn:
  1.1032 +    {
  1.1033 +      pop_double();
  1.1034 +      break;
  1.1035 +    }
  1.1036 +  case Bytecodes::_dstore:   store_local_double(str->get_index());  break;
  1.1037 +  case Bytecodes::_dstore_0: store_local_double(0);                 break;
  1.1038 +  case Bytecodes::_dstore_1: store_local_double(1);                 break;
  1.1039 +  case Bytecodes::_dstore_2: store_local_double(2);                 break;
  1.1040 +  case Bytecodes::_dstore_3: store_local_double(3);                 break;
  1.1041 +
  1.1042 +  case Bytecodes::_dup:
  1.1043 +    {
  1.1044 +      push(type_at_tos());
  1.1045 +      break;
  1.1046 +    }
  1.1047 +  case Bytecodes::_dup_x1:
  1.1048 +    {
  1.1049 +      ciType* value1 = pop_value();
  1.1050 +      ciType* value2 = pop_value();
  1.1051 +      push(value1);
  1.1052 +      push(value2);
  1.1053 +      push(value1);
  1.1054 +      break;
  1.1055 +    }
  1.1056 +  case Bytecodes::_dup_x2:
  1.1057 +    {
  1.1058 +      ciType* value1 = pop_value();
  1.1059 +      ciType* value2 = pop_value();
  1.1060 +      ciType* value3 = pop_value();
  1.1061 +      push(value1);
  1.1062 +      push(value3);
  1.1063 +      push(value2);
  1.1064 +      push(value1);
  1.1065 +      break;
  1.1066 +    }
  1.1067 +  case Bytecodes::_dup2:
  1.1068 +    {
  1.1069 +      ciType* value1 = pop_value();
  1.1070 +      ciType* value2 = pop_value();
  1.1071 +      push(value2);
  1.1072 +      push(value1);
  1.1073 +      push(value2);
  1.1074 +      push(value1);
  1.1075 +      break;
  1.1076 +    }
  1.1077 +  case Bytecodes::_dup2_x1:
  1.1078 +    {
  1.1079 +      ciType* value1 = pop_value();
  1.1080 +      ciType* value2 = pop_value();
  1.1081 +      ciType* value3 = pop_value();
  1.1082 +      push(value2);
  1.1083 +      push(value1);
  1.1084 +      push(value3);
  1.1085 +      push(value2);
  1.1086 +      push(value1);
  1.1087 +      break;
  1.1088 +    }
  1.1089 +  case Bytecodes::_dup2_x2:
  1.1090 +    {
  1.1091 +      ciType* value1 = pop_value();
  1.1092 +      ciType* value2 = pop_value();
  1.1093 +      ciType* value3 = pop_value();
  1.1094 +      ciType* value4 = pop_value();
  1.1095 +      push(value2);
  1.1096 +      push(value1);
  1.1097 +      push(value4);
  1.1098 +      push(value3);
  1.1099 +      push(value2);
  1.1100 +      push(value1);
  1.1101 +      break;
  1.1102 +    }
  1.1103 +  case Bytecodes::_f2d:
  1.1104 +    {
  1.1105 +      pop_float();
  1.1106 +      push_double();
  1.1107 +      break;
  1.1108 +    }
  1.1109 +  case Bytecodes::_f2i:
  1.1110 +    {
  1.1111 +      pop_float();
  1.1112 +      push_int();
  1.1113 +      break;
  1.1114 +    }
  1.1115 +  case Bytecodes::_f2l:
  1.1116 +    {
  1.1117 +      pop_float();
  1.1118 +      push_long();
  1.1119 +      break;
  1.1120 +    }
  1.1121 +  case Bytecodes::_fadd:
  1.1122 +  case Bytecodes::_fdiv:
  1.1123 +  case Bytecodes::_fmul:
  1.1124 +  case Bytecodes::_frem:
  1.1125 +  case Bytecodes::_fsub:
  1.1126 +    {
  1.1127 +      pop_float();
  1.1128 +      pop_float();
  1.1129 +      push_float();
  1.1130 +      break;
  1.1131 +    }
  1.1132 +  case Bytecodes::_faload:
  1.1133 +    {
  1.1134 +      pop_int();
  1.1135 +      ciTypeArrayKlass* array_klass = pop_typeArray();
  1.1136 +      // Put assert here.
  1.1137 +      push_float();
  1.1138 +      break;
  1.1139 +    }
  1.1140 +  case Bytecodes::_fastore:
  1.1141 +    {
  1.1142 +      pop_float();
  1.1143 +      pop_int();
  1.1144 +      ciTypeArrayKlass* array_klass = pop_typeArray();
  1.1145 +      // Put assert here.
  1.1146 +      break;
  1.1147 +    }
  1.1148 +  case Bytecodes::_fcmpg:
  1.1149 +  case Bytecodes::_fcmpl:
  1.1150 +    {
  1.1151 +      pop_float();
  1.1152 +      pop_float();
  1.1153 +      push_int();
  1.1154 +      break;
  1.1155 +    }
  1.1156 +  case Bytecodes::_fconst_0:
  1.1157 +  case Bytecodes::_fconst_1:
  1.1158 +  case Bytecodes::_fconst_2:
  1.1159 +    {
  1.1160 +      push_float();
  1.1161 +      break;
  1.1162 +    }
  1.1163 +  case Bytecodes::_fload:   load_local_float(str->get_index());     break;
  1.1164 +  case Bytecodes::_fload_0: load_local_float(0);                    break;
  1.1165 +  case Bytecodes::_fload_1: load_local_float(1);                    break;
  1.1166 +  case Bytecodes::_fload_2: load_local_float(2);                    break;
  1.1167 +  case Bytecodes::_fload_3: load_local_float(3);                    break;
  1.1168 +
  1.1169 +  case Bytecodes::_fneg:
  1.1170 +    {
  1.1171 +      pop_float();
  1.1172 +      push_float();
  1.1173 +      break;
  1.1174 +    }
  1.1175 +  case Bytecodes::_freturn:
  1.1176 +    {
  1.1177 +      pop_float();
  1.1178 +      break;
  1.1179 +    }
  1.1180 +  case Bytecodes::_fstore:    store_local_float(str->get_index());   break;
  1.1181 +  case Bytecodes::_fstore_0:  store_local_float(0);                  break;
  1.1182 +  case Bytecodes::_fstore_1:  store_local_float(1);                  break;
  1.1183 +  case Bytecodes::_fstore_2:  store_local_float(2);                  break;
  1.1184 +  case Bytecodes::_fstore_3:  store_local_float(3);                  break;
  1.1185 +
  1.1186 +  case Bytecodes::_getfield:  do_getfield(str);                      break;
  1.1187 +  case Bytecodes::_getstatic: do_getstatic(str);                     break;
  1.1188 +
  1.1189 +  case Bytecodes::_goto:
  1.1190 +  case Bytecodes::_goto_w:
  1.1191 +  case Bytecodes::_nop:
  1.1192 +  case Bytecodes::_return:
  1.1193 +    {
  1.1194 +      // do nothing.
  1.1195 +      break;
  1.1196 +    }
  1.1197 +  case Bytecodes::_i2b:
  1.1198 +  case Bytecodes::_i2c:
  1.1199 +  case Bytecodes::_i2s:
  1.1200 +  case Bytecodes::_ineg:
  1.1201 +    {
  1.1202 +      pop_int();
  1.1203 +      push_int();
  1.1204 +      break;
  1.1205 +    }
  1.1206 +  case Bytecodes::_i2d:
  1.1207 +    {
  1.1208 +      pop_int();
  1.1209 +      push_double();
  1.1210 +      break;
  1.1211 +    }
  1.1212 +  case Bytecodes::_i2f:
  1.1213 +    {
  1.1214 +      pop_int();
  1.1215 +      push_float();
  1.1216 +      break;
  1.1217 +    }
  1.1218 +  case Bytecodes::_i2l:
  1.1219 +    {
  1.1220 +      pop_int();
  1.1221 +      push_long();
  1.1222 +      break;
  1.1223 +    }
  1.1224 +  case Bytecodes::_iadd:
  1.1225 +  case Bytecodes::_iand:
  1.1226 +  case Bytecodes::_idiv:
  1.1227 +  case Bytecodes::_imul:
  1.1228 +  case Bytecodes::_ior:
  1.1229 +  case Bytecodes::_irem:
  1.1230 +  case Bytecodes::_ishl:
  1.1231 +  case Bytecodes::_ishr:
  1.1232 +  case Bytecodes::_isub:
  1.1233 +  case Bytecodes::_iushr:
  1.1234 +  case Bytecodes::_ixor:
  1.1235 +    {
  1.1236 +      pop_int();
  1.1237 +      pop_int();
  1.1238 +      push_int();
  1.1239 +      break;
  1.1240 +    }
  1.1241 +  case Bytecodes::_if_acmpeq:
  1.1242 +  case Bytecodes::_if_acmpne:
  1.1243 +    {
  1.1244 +      pop_object();
  1.1245 +      pop_object();
  1.1246 +      break;
  1.1247 +    }
  1.1248 +  case Bytecodes::_if_icmpeq:
  1.1249 +  case Bytecodes::_if_icmpge:
  1.1250 +  case Bytecodes::_if_icmpgt:
  1.1251 +  case Bytecodes::_if_icmple:
  1.1252 +  case Bytecodes::_if_icmplt:
  1.1253 +  case Bytecodes::_if_icmpne:
  1.1254 +    {
  1.1255 +      pop_int();
  1.1256 +      pop_int();
  1.1257 +      break;
  1.1258 +    }
  1.1259 +  case Bytecodes::_ifeq:
  1.1260 +  case Bytecodes::_ifle:
  1.1261 +  case Bytecodes::_iflt:
  1.1262 +  case Bytecodes::_ifge:
  1.1263 +  case Bytecodes::_ifgt:
  1.1264 +  case Bytecodes::_ifne:
  1.1265 +  case Bytecodes::_ireturn:
  1.1266 +  case Bytecodes::_lookupswitch:
  1.1267 +  case Bytecodes::_tableswitch:
  1.1268 +    {
  1.1269 +      pop_int();
  1.1270 +      break;
  1.1271 +    }
  1.1272 +  case Bytecodes::_iinc:
  1.1273 +    {
  1.1274 +      check_int(local(str->get_index()));
  1.1275 +      break;
  1.1276 +    }
  1.1277 +  case Bytecodes::_iload:   load_local_int(str->get_index()); break;
  1.1278 +  case Bytecodes::_iload_0: load_local_int(0);                      break;
  1.1279 +  case Bytecodes::_iload_1: load_local_int(1);                      break;
  1.1280 +  case Bytecodes::_iload_2: load_local_int(2);                      break;
  1.1281 +  case Bytecodes::_iload_3: load_local_int(3);                      break;
  1.1282 +
  1.1283 +  case Bytecodes::_instanceof:
  1.1284 +    {
  1.1285 +      // Check for uncommon trap:
  1.1286 +      do_checkcast(str);
  1.1287 +      pop_object();
  1.1288 +      push_int();
  1.1289 +      break;
  1.1290 +    }
  1.1291 +  case Bytecodes::_invokeinterface: do_invoke(str, true);           break;
  1.1292 +  case Bytecodes::_invokespecial:   do_invoke(str, true);           break;
  1.1293 +  case Bytecodes::_invokestatic:    do_invoke(str, false);          break;
  1.1294 +
  1.1295 +  case Bytecodes::_invokevirtual:   do_invoke(str, true);           break;
  1.1296 +
  1.1297 +  case Bytecodes::_istore:   store_local_int(str->get_index());     break;
  1.1298 +  case Bytecodes::_istore_0: store_local_int(0);                    break;
  1.1299 +  case Bytecodes::_istore_1: store_local_int(1);                    break;
  1.1300 +  case Bytecodes::_istore_2: store_local_int(2);                    break;
  1.1301 +  case Bytecodes::_istore_3: store_local_int(3);                    break;
  1.1302 +
  1.1303 +  case Bytecodes::_jsr:
  1.1304 +  case Bytecodes::_jsr_w: do_jsr(str);                              break;
  1.1305 +
  1.1306 +  case Bytecodes::_l2d:
  1.1307 +    {
  1.1308 +      pop_long();
  1.1309 +      push_double();
  1.1310 +      break;
  1.1311 +    }
  1.1312 +  case Bytecodes::_l2f:
  1.1313 +    {
  1.1314 +      pop_long();
  1.1315 +      push_float();
  1.1316 +      break;
  1.1317 +    }
  1.1318 +  case Bytecodes::_l2i:
  1.1319 +    {
  1.1320 +      pop_long();
  1.1321 +      push_int();
  1.1322 +      break;
  1.1323 +    }
  1.1324 +  case Bytecodes::_ladd:
  1.1325 +  case Bytecodes::_land:
  1.1326 +  case Bytecodes::_ldiv:
  1.1327 +  case Bytecodes::_lmul:
  1.1328 +  case Bytecodes::_lor:
  1.1329 +  case Bytecodes::_lrem:
  1.1330 +  case Bytecodes::_lsub:
  1.1331 +  case Bytecodes::_lxor:
  1.1332 +    {
  1.1333 +      pop_long();
  1.1334 +      pop_long();
  1.1335 +      push_long();
  1.1336 +      break;
  1.1337 +    }
  1.1338 +  case Bytecodes::_laload:
  1.1339 +    {
  1.1340 +      pop_int();
  1.1341 +      ciTypeArrayKlass* array_klass = pop_typeArray();
  1.1342 +      // Put assert here for right type?
  1.1343 +      push_long();
  1.1344 +      break;
  1.1345 +    }
  1.1346 +  case Bytecodes::_lastore:
  1.1347 +    {
  1.1348 +      pop_long();
  1.1349 +      pop_int();
  1.1350 +      pop_typeArray();
  1.1351 +      // assert here?
  1.1352 +      break;
  1.1353 +    }
  1.1354 +  case Bytecodes::_lcmp:
  1.1355 +    {
  1.1356 +      pop_long();
  1.1357 +      pop_long();
  1.1358 +      push_int();
  1.1359 +      break;
  1.1360 +    }
  1.1361 +  case Bytecodes::_lconst_0:
  1.1362 +  case Bytecodes::_lconst_1:
  1.1363 +    {
  1.1364 +      push_long();
  1.1365 +      break;
  1.1366 +    }
  1.1367 +  case Bytecodes::_ldc:
  1.1368 +  case Bytecodes::_ldc_w:
  1.1369 +  case Bytecodes::_ldc2_w:
  1.1370 +    {
  1.1371 +      do_ldc(str);
  1.1372 +      break;
  1.1373 +    }
  1.1374 +
  1.1375 +  case Bytecodes::_lload:   load_local_long(str->get_index());      break;
  1.1376 +  case Bytecodes::_lload_0: load_local_long(0);                     break;
  1.1377 +  case Bytecodes::_lload_1: load_local_long(1);                     break;
  1.1378 +  case Bytecodes::_lload_2: load_local_long(2);                     break;
  1.1379 +  case Bytecodes::_lload_3: load_local_long(3);                     break;
  1.1380 +
  1.1381 +  case Bytecodes::_lneg:
  1.1382 +    {
  1.1383 +      pop_long();
  1.1384 +      push_long();
  1.1385 +      break;
  1.1386 +    }
  1.1387 +  case Bytecodes::_lreturn:
  1.1388 +    {
  1.1389 +      pop_long();
  1.1390 +      break;
  1.1391 +    }
  1.1392 +  case Bytecodes::_lshl:
  1.1393 +  case Bytecodes::_lshr:
  1.1394 +  case Bytecodes::_lushr:
  1.1395 +    {
  1.1396 +      pop_int();
  1.1397 +      pop_long();
  1.1398 +      push_long();
  1.1399 +      break;
  1.1400 +    }
  1.1401 +  case Bytecodes::_lstore:   store_local_long(str->get_index());    break;
  1.1402 +  case Bytecodes::_lstore_0: store_local_long(0);                   break;
  1.1403 +  case Bytecodes::_lstore_1: store_local_long(1);                   break;
  1.1404 +  case Bytecodes::_lstore_2: store_local_long(2);                   break;
  1.1405 +  case Bytecodes::_lstore_3: store_local_long(3);                   break;
  1.1406 +
  1.1407 +  case Bytecodes::_multianewarray: do_multianewarray(str);          break;
  1.1408 +
  1.1409 +  case Bytecodes::_new:      do_new(str);                           break;
  1.1410 +
  1.1411 +  case Bytecodes::_newarray: do_newarray(str);                      break;
  1.1412 +
  1.1413 +  case Bytecodes::_pop:
  1.1414 +    {
  1.1415 +      pop();
  1.1416 +      break;
  1.1417 +    }
  1.1418 +  case Bytecodes::_pop2:
  1.1419 +    {
  1.1420 +      pop();
  1.1421 +      pop();
  1.1422 +      break;
  1.1423 +    }
  1.1424 +
  1.1425 +  case Bytecodes::_putfield:       do_putfield(str);                 break;
  1.1426 +  case Bytecodes::_putstatic:      do_putstatic(str);                break;
  1.1427 +
  1.1428 +  case Bytecodes::_ret: do_ret(str);                                 break;
  1.1429 +
  1.1430 +  case Bytecodes::_swap:
  1.1431 +    {
  1.1432 +      ciType* value1 = pop_value();
  1.1433 +      ciType* value2 = pop_value();
  1.1434 +      push(value1);
  1.1435 +      push(value2);
  1.1436 +      break;
  1.1437 +    }
  1.1438 +  case Bytecodes::_wide:
  1.1439 +  default:
  1.1440 +    {
  1.1441 +      // The iterator should skip this.
  1.1442 +      ShouldNotReachHere();
  1.1443 +      break;
  1.1444 +    }
  1.1445 +  }
  1.1446 +
  1.1447 +  if (CITraceTypeFlow) {
  1.1448 +    print_on(tty);
  1.1449 +  }
  1.1450 +
  1.1451 +  return (_trap_bci != -1);
  1.1452 +}
  1.1453 +
  1.1454 +#ifndef PRODUCT
  1.1455 +// ------------------------------------------------------------------
  1.1456 +// ciTypeFlow::StateVector::print_cell_on
  1.1457 +void ciTypeFlow::StateVector::print_cell_on(outputStream* st, Cell c) const {
  1.1458 +  ciType* type = type_at(c);
  1.1459 +  if (type == top_type()) {
  1.1460 +    st->print("top");
  1.1461 +  } else if (type == bottom_type()) {
  1.1462 +    st->print("bottom");
  1.1463 +  } else if (type == null_type()) {
  1.1464 +    st->print("null");
  1.1465 +  } else if (type == long2_type()) {
  1.1466 +    st->print("long2");
  1.1467 +  } else if (type == double2_type()) {
  1.1468 +    st->print("double2");
  1.1469 +  } else if (is_int(type)) {
  1.1470 +    st->print("int");
  1.1471 +  } else if (is_long(type)) {
  1.1472 +    st->print("long");
  1.1473 +  } else if (is_float(type)) {
  1.1474 +    st->print("float");
  1.1475 +  } else if (is_double(type)) {
  1.1476 +    st->print("double");
  1.1477 +  } else if (type->is_return_address()) {
  1.1478 +    st->print("address(%d)", type->as_return_address()->bci());
  1.1479 +  } else {
  1.1480 +    if (type->is_klass()) {
  1.1481 +      type->as_klass()->name()->print_symbol_on(st);
  1.1482 +    } else {
  1.1483 +      st->print("UNEXPECTED TYPE");
  1.1484 +      type->print();
  1.1485 +    }
  1.1486 +  }
  1.1487 +}
  1.1488 +
  1.1489 +// ------------------------------------------------------------------
  1.1490 +// ciTypeFlow::StateVector::print_on
  1.1491 +void ciTypeFlow::StateVector::print_on(outputStream* st) const {
  1.1492 +  int num_locals   = _outer->max_locals();
  1.1493 +  int num_stack    = stack_size();
  1.1494 +  int num_monitors = monitor_count();
  1.1495 +  st->print_cr("  State : locals %d, stack %d, monitors %d", num_locals, num_stack, num_monitors);
  1.1496 +  if (num_stack >= 0) {
  1.1497 +    int i;
  1.1498 +    for (i = 0; i < num_locals; i++) {
  1.1499 +      st->print("    local %2d : ", i);
  1.1500 +      print_cell_on(st, local(i));
  1.1501 +      st->cr();
  1.1502 +    }
  1.1503 +    for (i = 0; i < num_stack; i++) {
  1.1504 +      st->print("    stack %2d : ", i);
  1.1505 +      print_cell_on(st, stack(i));
  1.1506 +      st->cr();
  1.1507 +    }
  1.1508 +  }
  1.1509 +}
  1.1510 +#endif
  1.1511 +
  1.1512 +// ciTypeFlow::Block
  1.1513 +//
  1.1514 +// A basic block.
  1.1515 +
  1.1516 +// ------------------------------------------------------------------
  1.1517 +// ciTypeFlow::Block::Block
  1.1518 +ciTypeFlow::Block::Block(ciTypeFlow* outer,
  1.1519 +                         ciBlock *ciblk,
  1.1520 +                         ciTypeFlow::JsrSet* jsrs) {
  1.1521 +  _ciblock = ciblk;
  1.1522 +  _exceptions = NULL;
  1.1523 +  _exc_klasses = NULL;
  1.1524 +  _successors = NULL;
  1.1525 +  _state = new (outer->arena()) StateVector(outer);
  1.1526 +  JsrSet* new_jsrs =
  1.1527 +    new (outer->arena()) JsrSet(outer->arena(), jsrs->size());
  1.1528 +  jsrs->copy_into(new_jsrs);
  1.1529 +  _jsrs = new_jsrs;
  1.1530 +  _next = NULL;
  1.1531 +  _on_work_list = false;
  1.1532 +  _pre_order = -1; assert(!has_pre_order(), "");
  1.1533 +  _private_copy = false;
  1.1534 +  _trap_bci = -1;
  1.1535 +  _trap_index = 0;
  1.1536 +
  1.1537 +  if (CITraceTypeFlow) {
  1.1538 +    tty->print_cr(">> Created new block");
  1.1539 +    print_on(tty);
  1.1540 +  }
  1.1541 +
  1.1542 +  assert(this->outer() == outer, "outer link set up");
  1.1543 +  assert(!outer->have_block_count(), "must not have mapped blocks yet");
  1.1544 +}
  1.1545 +
  1.1546 +// ------------------------------------------------------------------
  1.1547 +// ciTypeFlow::Block::clone_loop_head
  1.1548 +//
  1.1549 +ciTypeFlow::Block*
  1.1550 +ciTypeFlow::Block::clone_loop_head(ciTypeFlow* analyzer,
  1.1551 +                                   int branch_bci,
  1.1552 +                                   ciTypeFlow::Block* target,
  1.1553 +                                   ciTypeFlow::JsrSet* jsrs) {
  1.1554 +  // Loop optimizations are not performed on Tier1 compiles. Do nothing.
  1.1555 +  if (analyzer->env()->comp_level() < CompLevel_full_optimization) {
  1.1556 +    return target;
  1.1557 +  }
  1.1558 +
  1.1559 +  // The current block ends with a branch.
  1.1560 +  //
  1.1561 +  // If the target block appears to be the test-clause of a for loop, and
  1.1562 +  // it is not too large, and it has not yet been cloned, clone it.
  1.1563 +  // The pre-existing copy becomes the private clone used only by
  1.1564 +  // the initial iteration of the loop.  (We know we are simulating
  1.1565 +  // the initial iteration right now, since we have never calculated
  1.1566 +  // successors before for this block.)
  1.1567 +
  1.1568 +  if (branch_bci <= start()
  1.1569 +      && (target->limit() - target->start()) <= CICloneLoopTestLimit
  1.1570 +      && target->private_copy_count() == 0) {
  1.1571 +    // Setting the private_copy bit ensures that the target block cannot be
  1.1572 +    // reached by any other paths, such as fall-in from the loop body.
  1.1573 +    // The private copy will be accessible only on successor lists
  1.1574 +    // created up to this point.
  1.1575 +    target->set_private_copy(true);
  1.1576 +    if (CITraceTypeFlow) {
  1.1577 +      tty->print(">> Cloning a test-clause block ");
  1.1578 +      print_value_on(tty);
  1.1579 +      tty->cr();
  1.1580 +    }
  1.1581 +    // If the target is the current block, then later on a new copy of the
  1.1582 +    // target block will be created when its bytecodes are reached by
  1.1583 +    // an alternate path. (This is the case for loops with the loop
  1.1584 +    // head at the bci-wise bottom of the loop, as with pre-1.4.2 javac.)
  1.1585 +    //
  1.1586 +    // Otherwise, duplicate the target block now and use it immediately.
  1.1587 +    // (The case for loops with the loop head at the bci-wise top of the
  1.1588 +    // loop, as with 1.4.2 javac.)
  1.1589 +    //
  1.1590 +    // In either case, the new copy of the block will remain public.
  1.1591 +    if (target != this) {
  1.1592 +      target = analyzer->block_at(branch_bci, jsrs);
  1.1593 +    }
  1.1594 +  }
  1.1595 +  return target;
  1.1596 +}
  1.1597 +
  1.1598 +// ------------------------------------------------------------------
  1.1599 +// ciTypeFlow::Block::successors
  1.1600 +//
  1.1601 +// Get the successors for this Block.
  1.1602 +GrowableArray<ciTypeFlow::Block*>*
  1.1603 +ciTypeFlow::Block::successors(ciBytecodeStream* str,
  1.1604 +                              ciTypeFlow::StateVector* state,
  1.1605 +                              ciTypeFlow::JsrSet* jsrs) {
  1.1606 +  if (_successors == NULL) {
  1.1607 +    if (CITraceTypeFlow) {
  1.1608 +      tty->print(">> Computing successors for block ");
  1.1609 +      print_value_on(tty);
  1.1610 +      tty->cr();
  1.1611 +    }
  1.1612 +
  1.1613 +    ciTypeFlow* analyzer = outer();
  1.1614 +    Arena* arena = analyzer->arena();
  1.1615 +    Block* block = NULL;
  1.1616 +    bool has_successor = !has_trap() &&
  1.1617 +                         (control() != ciBlock::fall_through_bci || limit() < analyzer->code_size());
  1.1618 +    if (!has_successor) {
  1.1619 +      _successors =
  1.1620 +        new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1.1621 +      // No successors
  1.1622 +    } else if (control() == ciBlock::fall_through_bci) {
  1.1623 +      assert(str->cur_bci() == limit(), "bad block end");
  1.1624 +      // This block simply falls through to the next.
  1.1625 +      _successors =
  1.1626 +        new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1.1627 +
  1.1628 +      Block* block = analyzer->block_at(limit(), _jsrs);
  1.1629 +      assert(_successors->length() == FALL_THROUGH, "");
  1.1630 +      _successors->append(block);
  1.1631 +    } else {
  1.1632 +      int current_bci = str->cur_bci();
  1.1633 +      int next_bci = str->next_bci();
  1.1634 +      int branch_bci = -1;
  1.1635 +      Block* target = NULL;
  1.1636 +      assert(str->next_bci() == limit(), "bad block end");
  1.1637 +      // This block is not a simple fall-though.  Interpret
  1.1638 +      // the current bytecode to find our successors.
  1.1639 +      switch (str->cur_bc()) {
  1.1640 +      case Bytecodes::_ifeq:         case Bytecodes::_ifne:
  1.1641 +      case Bytecodes::_iflt:         case Bytecodes::_ifge:
  1.1642 +      case Bytecodes::_ifgt:         case Bytecodes::_ifle:
  1.1643 +      case Bytecodes::_if_icmpeq:    case Bytecodes::_if_icmpne:
  1.1644 +      case Bytecodes::_if_icmplt:    case Bytecodes::_if_icmpge:
  1.1645 +      case Bytecodes::_if_icmpgt:    case Bytecodes::_if_icmple:
  1.1646 +      case Bytecodes::_if_acmpeq:    case Bytecodes::_if_acmpne:
  1.1647 +      case Bytecodes::_ifnull:       case Bytecodes::_ifnonnull:
  1.1648 +        // Our successors are the branch target and the next bci.
  1.1649 +        branch_bci = str->get_dest();
  1.1650 +        clone_loop_head(analyzer, branch_bci, this, jsrs);
  1.1651 +        _successors =
  1.1652 +          new (arena) GrowableArray<Block*>(arena, 2, 0, NULL);
  1.1653 +        assert(_successors->length() == IF_NOT_TAKEN, "");
  1.1654 +        _successors->append(analyzer->block_at(next_bci, jsrs));
  1.1655 +        assert(_successors->length() == IF_TAKEN, "");
  1.1656 +        _successors->append(analyzer->block_at(branch_bci, jsrs));
  1.1657 +        break;
  1.1658 +
  1.1659 +      case Bytecodes::_goto:
  1.1660 +        branch_bci = str->get_dest();
  1.1661 +        _successors =
  1.1662 +          new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1.1663 +        assert(_successors->length() == GOTO_TARGET, "");
  1.1664 +        target = analyzer->block_at(branch_bci, jsrs);
  1.1665 +        // If the target block has not been visited yet, and looks like
  1.1666 +        // a two-way branch, attempt to clone it if it is a loop head.
  1.1667 +        if (target->_successors != NULL
  1.1668 +            && target->_successors->length() == (IF_TAKEN + 1)) {
  1.1669 +          target = clone_loop_head(analyzer, branch_bci, target, jsrs);
  1.1670 +        }
  1.1671 +        _successors->append(target);
  1.1672 +        break;
  1.1673 +
  1.1674 +      case Bytecodes::_jsr:
  1.1675 +        branch_bci = str->get_dest();
  1.1676 +        _successors =
  1.1677 +          new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1.1678 +        assert(_successors->length() == GOTO_TARGET, "");
  1.1679 +        _successors->append(analyzer->block_at(branch_bci, jsrs));
  1.1680 +        break;
  1.1681 +
  1.1682 +      case Bytecodes::_goto_w:
  1.1683 +      case Bytecodes::_jsr_w:
  1.1684 +        _successors =
  1.1685 +          new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1.1686 +        assert(_successors->length() == GOTO_TARGET, "");
  1.1687 +        _successors->append(analyzer->block_at(str->get_far_dest(), jsrs));
  1.1688 +        break;
  1.1689 +
  1.1690 +      case Bytecodes::_tableswitch:  {
  1.1691 +        Bytecode_tableswitch *tableswitch =
  1.1692 +          Bytecode_tableswitch_at(str->cur_bcp());
  1.1693 +
  1.1694 +        int len = tableswitch->length();
  1.1695 +        _successors =
  1.1696 +          new (arena) GrowableArray<Block*>(arena, len+1, 0, NULL);
  1.1697 +        int bci = current_bci + tableswitch->default_offset();
  1.1698 +        Block* block = analyzer->block_at(bci, jsrs);
  1.1699 +        assert(_successors->length() == SWITCH_DEFAULT, "");
  1.1700 +        _successors->append(block);
  1.1701 +        while (--len >= 0) {
  1.1702 +          int bci = current_bci + tableswitch->dest_offset_at(len);
  1.1703 +          block = analyzer->block_at(bci, jsrs);
  1.1704 +          assert(_successors->length() >= SWITCH_CASES, "");
  1.1705 +          _successors->append_if_missing(block);
  1.1706 +        }
  1.1707 +        break;
  1.1708 +      }
  1.1709 +
  1.1710 +      case Bytecodes::_lookupswitch: {
  1.1711 +        Bytecode_lookupswitch *lookupswitch =
  1.1712 +          Bytecode_lookupswitch_at(str->cur_bcp());
  1.1713 +
  1.1714 +        int npairs = lookupswitch->number_of_pairs();
  1.1715 +        _successors =
  1.1716 +          new (arena) GrowableArray<Block*>(arena, npairs+1, 0, NULL);
  1.1717 +        int bci = current_bci + lookupswitch->default_offset();
  1.1718 +        Block* block = analyzer->block_at(bci, jsrs);
  1.1719 +        assert(_successors->length() == SWITCH_DEFAULT, "");
  1.1720 +        _successors->append(block);
  1.1721 +        while(--npairs >= 0) {
  1.1722 +          LookupswitchPair *pair = lookupswitch->pair_at(npairs);
  1.1723 +          int bci = current_bci + pair->offset();
  1.1724 +          Block* block = analyzer->block_at(bci, jsrs);
  1.1725 +          assert(_successors->length() >= SWITCH_CASES, "");
  1.1726 +          _successors->append_if_missing(block);
  1.1727 +        }
  1.1728 +        break;
  1.1729 +      }
  1.1730 +
  1.1731 +      case Bytecodes::_athrow:     case Bytecodes::_ireturn:
  1.1732 +      case Bytecodes::_lreturn:    case Bytecodes::_freturn:
  1.1733 +      case Bytecodes::_dreturn:    case Bytecodes::_areturn:
  1.1734 +      case Bytecodes::_return:
  1.1735 +        _successors =
  1.1736 +          new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1.1737 +        // No successors
  1.1738 +        break;
  1.1739 +
  1.1740 +      case Bytecodes::_ret: {
  1.1741 +        _successors =
  1.1742 +          new (arena) GrowableArray<Block*>(arena, 1, 0, NULL);
  1.1743 +
  1.1744 +        Cell local = state->local(str->get_index());
  1.1745 +        ciType* return_address = state->type_at(local);
  1.1746 +        assert(return_address->is_return_address(), "verify: wrong type");
  1.1747 +        int bci = return_address->as_return_address()->bci();
  1.1748 +        assert(_successors->length() == GOTO_TARGET, "");
  1.1749 +        _successors->append(analyzer->block_at(bci, jsrs));
  1.1750 +        break;
  1.1751 +      }
  1.1752 +
  1.1753 +      case Bytecodes::_wide:
  1.1754 +      default:
  1.1755 +        ShouldNotReachHere();
  1.1756 +        break;
  1.1757 +      }
  1.1758 +    }
  1.1759 +  }
  1.1760 +  return _successors;
  1.1761 +}
  1.1762 +
  1.1763 +// ------------------------------------------------------------------
  1.1764 +// ciTypeFlow::Block:compute_exceptions
  1.1765 +//
  1.1766 +// Compute the exceptional successors and types for this Block.
  1.1767 +void ciTypeFlow::Block::compute_exceptions() {
  1.1768 +  assert(_exceptions == NULL && _exc_klasses == NULL, "repeat");
  1.1769 +
  1.1770 +  if (CITraceTypeFlow) {
  1.1771 +    tty->print(">> Computing exceptions for block ");
  1.1772 +    print_value_on(tty);
  1.1773 +    tty->cr();
  1.1774 +  }
  1.1775 +
  1.1776 +  ciTypeFlow* analyzer = outer();
  1.1777 +  Arena* arena = analyzer->arena();
  1.1778 +
  1.1779 +  // Any bci in the block will do.
  1.1780 +  ciExceptionHandlerStream str(analyzer->method(), start());
  1.1781 +
  1.1782 +  // Allocate our growable arrays.
  1.1783 +  int exc_count = str.count();
  1.1784 +  _exceptions = new (arena) GrowableArray<Block*>(arena, exc_count, 0, NULL);
  1.1785 +  _exc_klasses = new (arena) GrowableArray<ciInstanceKlass*>(arena, exc_count,
  1.1786 +                                                             0, NULL);
  1.1787 +
  1.1788 +  for ( ; !str.is_done(); str.next()) {
  1.1789 +    ciExceptionHandler* handler = str.handler();
  1.1790 +    int bci = handler->handler_bci();
  1.1791 +    ciInstanceKlass* klass = NULL;
  1.1792 +    if (bci == -1) {
  1.1793 +      // There is no catch all.  It is possible to exit the method.
  1.1794 +      break;
  1.1795 +    }
  1.1796 +    if (handler->is_catch_all()) {
  1.1797 +      klass = analyzer->env()->Throwable_klass();
  1.1798 +    } else {
  1.1799 +      klass = handler->catch_klass();
  1.1800 +    }
  1.1801 +    _exceptions->append(analyzer->block_at(bci, _jsrs));
  1.1802 +    _exc_klasses->append(klass);
  1.1803 +  }
  1.1804 +}
  1.1805 +
  1.1806 +// ------------------------------------------------------------------
  1.1807 +// ciTypeFlow::Block::is_simpler_than
  1.1808 +//
  1.1809 +// A relation used to order our work list.  We work on a block earlier
  1.1810 +// if it has a smaller jsr stack or it occurs earlier in the program
  1.1811 +// text.
  1.1812 +//
  1.1813 +// Note: maybe we should redo this functionality to make blocks
  1.1814 +// which correspond to exceptions lower priority.
  1.1815 +bool ciTypeFlow::Block::is_simpler_than(ciTypeFlow::Block* other) {
  1.1816 +  if (other == NULL) {
  1.1817 +    return true;
  1.1818 +  } else {
  1.1819 +    int size1 = _jsrs->size();
  1.1820 +    int size2 = other->_jsrs->size();
  1.1821 +    if (size1 < size2) {
  1.1822 +      return true;
  1.1823 +    } else if (size2 < size1) {
  1.1824 +      return false;
  1.1825 +    } else {
  1.1826 +#if 0
  1.1827 +      if (size1 > 0) {
  1.1828 +        int r1 = _jsrs->record_at(0)->return_address();
  1.1829 +        int r2 = _jsrs->record_at(0)->return_address();
  1.1830 +        if (r1 < r2) {
  1.1831 +          return true;
  1.1832 +        } else if (r2 < r1) {
  1.1833 +          return false;
  1.1834 +        } else {
  1.1835 +          int e1 = _jsrs->record_at(0)->return_address();
  1.1836 +          int e2 = _jsrs->record_at(0)->return_address();
  1.1837 +          if (e1 < e2) {
  1.1838 +            return true;
  1.1839 +          } else if (e2 < e1) {
  1.1840 +            return false;
  1.1841 +          }
  1.1842 +        }
  1.1843 +      }
  1.1844 +#endif
  1.1845 +      return (start() <= other->start());
  1.1846 +    }
  1.1847 +  }
  1.1848 +}
  1.1849 +
  1.1850 +// ------------------------------------------------------------------
  1.1851 +// ciTypeFlow::Block::set_private_copy
  1.1852 +// Use this only to make a pre-existing public block into a private copy.
  1.1853 +void ciTypeFlow::Block::set_private_copy(bool z) {
  1.1854 +  assert(z || (z == is_private_copy()), "cannot make a private copy public");
  1.1855 +  _private_copy = z;
  1.1856 +}
  1.1857 +
  1.1858 +#ifndef PRODUCT
  1.1859 +// ------------------------------------------------------------------
  1.1860 +// ciTypeFlow::Block::print_value_on
  1.1861 +void ciTypeFlow::Block::print_value_on(outputStream* st) const {
  1.1862 +  if (has_pre_order())  st->print("#%-2d ", pre_order());
  1.1863 +  st->print("[%d - %d)", start(), limit());
  1.1864 +  if (_jsrs->size() > 0) { st->print("/");  _jsrs->print_on(st); }
  1.1865 +  if (is_private_copy())  st->print("/private_copy");
  1.1866 +}
  1.1867 +
  1.1868 +// ------------------------------------------------------------------
  1.1869 +// ciTypeFlow::Block::print_on
  1.1870 +void ciTypeFlow::Block::print_on(outputStream* st) const {
  1.1871 +  if ((Verbose || WizardMode)) {
  1.1872 +    outer()->method()->print_codes_on(start(), limit(), st);
  1.1873 +  }
  1.1874 +  st->print_cr("  ====================================================  ");
  1.1875 +  st->print ("  ");
  1.1876 +  print_value_on(st);
  1.1877 +  st->cr();
  1.1878 +  _state->print_on(st);
  1.1879 +  if (_successors == NULL) {
  1.1880 +    st->print_cr("  No successor information");
  1.1881 +  } else {
  1.1882 +    int num_successors = _successors->length();
  1.1883 +    st->print_cr("  Successors : %d", num_successors);
  1.1884 +    for (int i = 0; i < num_successors; i++) {
  1.1885 +      Block* successor = _successors->at(i);
  1.1886 +      st->print("    ");
  1.1887 +      successor->print_value_on(st);
  1.1888 +      st->cr();
  1.1889 +    }
  1.1890 +  }
  1.1891 +  if (_exceptions == NULL) {
  1.1892 +    st->print_cr("  No exception information");
  1.1893 +  } else {
  1.1894 +    int num_exceptions = _exceptions->length();
  1.1895 +    st->print_cr("  Exceptions : %d", num_exceptions);
  1.1896 +    for (int i = 0; i < num_exceptions; i++) {
  1.1897 +      Block* exc_succ = _exceptions->at(i);
  1.1898 +      ciInstanceKlass* exc_klass = _exc_klasses->at(i);
  1.1899 +      st->print("    ");
  1.1900 +      exc_succ->print_value_on(st);
  1.1901 +      st->print(" -- ");
  1.1902 +      exc_klass->name()->print_symbol_on(st);
  1.1903 +      st->cr();
  1.1904 +    }
  1.1905 +  }
  1.1906 +  if (has_trap()) {
  1.1907 +    st->print_cr("  Traps on %d with trap index %d", trap_bci(), trap_index());
  1.1908 +  }
  1.1909 +  st->print_cr("  ====================================================  ");
  1.1910 +}
  1.1911 +#endif
  1.1912 +
  1.1913 +// ciTypeFlow
  1.1914 +//
  1.1915 +// This is a pass over the bytecodes which computes the following:
  1.1916 +//   basic block structure
  1.1917 +//   interpreter type-states (a la the verifier)
  1.1918 +
  1.1919 +// ------------------------------------------------------------------
  1.1920 +// ciTypeFlow::ciTypeFlow
  1.1921 +ciTypeFlow::ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci) {
  1.1922 +  _env = env;
  1.1923 +  _method = method;
  1.1924 +  _methodBlocks = method->get_method_blocks();
  1.1925 +  _max_locals = method->max_locals();
  1.1926 +  _max_stack = method->max_stack();
  1.1927 +  _code_size = method->code_size();
  1.1928 +  _osr_bci = osr_bci;
  1.1929 +  _failure_reason = NULL;
  1.1930 +  assert(start_bci() >= 0 && start_bci() < code_size() , "correct osr_bci argument");
  1.1931 +
  1.1932 +  _work_list = NULL;
  1.1933 +  _next_pre_order = 0;
  1.1934 +
  1.1935 +  _ciblock_count = _methodBlocks->num_blocks();
  1.1936 +  _idx_to_blocklist = NEW_ARENA_ARRAY(arena(), GrowableArray<Block*>*, _ciblock_count);
  1.1937 +  for (int i = 0; i < _ciblock_count; i++) {
  1.1938 +    _idx_to_blocklist[i] = NULL;
  1.1939 +  }
  1.1940 +  _block_map = NULL;  // until all blocks are seen
  1.1941 +  _jsr_count = 0;
  1.1942 +  _jsr_records = NULL;
  1.1943 +}
  1.1944 +
  1.1945 +// ------------------------------------------------------------------
  1.1946 +// ciTypeFlow::work_list_next
  1.1947 +//
  1.1948 +// Get the next basic block from our work list.
  1.1949 +ciTypeFlow::Block* ciTypeFlow::work_list_next() {
  1.1950 +  assert(!work_list_empty(), "work list must not be empty");
  1.1951 +  Block* next_block = _work_list;
  1.1952 +  _work_list = next_block->next();
  1.1953 +  next_block->set_next(NULL);
  1.1954 +  next_block->set_on_work_list(false);
  1.1955 +  if (!next_block->has_pre_order()) {
  1.1956 +    // Assign "pre_order" as each new block is taken from the work list.
  1.1957 +    // This number may be used by following phases to order block visits.
  1.1958 +    assert(!have_block_count(), "must not have mapped blocks yet")
  1.1959 +    next_block->set_pre_order(_next_pre_order++);
  1.1960 +  }
  1.1961 +  return next_block;
  1.1962 +}
  1.1963 +
  1.1964 +// ------------------------------------------------------------------
  1.1965 +// ciTypeFlow::add_to_work_list
  1.1966 +//
  1.1967 +// Add a basic block to our work list.
  1.1968 +void ciTypeFlow::add_to_work_list(ciTypeFlow::Block* block) {
  1.1969 +  assert(!block->is_on_work_list(), "must not already be on work list");
  1.1970 +
  1.1971 +  if (CITraceTypeFlow) {
  1.1972 +    tty->print(">> Adding block%s ", block->has_pre_order() ? " (again)" : "");
  1.1973 +    block->print_value_on(tty);
  1.1974 +    tty->print_cr(" to the work list : ");
  1.1975 +  }
  1.1976 +
  1.1977 +  block->set_on_work_list(true);
  1.1978 +  if (block->is_simpler_than(_work_list)) {
  1.1979 +    block->set_next(_work_list);
  1.1980 +    _work_list = block;
  1.1981 +  } else {
  1.1982 +    Block *temp = _work_list;
  1.1983 +    while (!block->is_simpler_than(temp->next())) {
  1.1984 +      if (CITraceTypeFlow) {
  1.1985 +        tty->print(".");
  1.1986 +      }
  1.1987 +      temp = temp->next();
  1.1988 +    }
  1.1989 +    block->set_next(temp->next());
  1.1990 +    temp->set_next(block);
  1.1991 +  }
  1.1992 +  if (CITraceTypeFlow) {
  1.1993 +    tty->cr();
  1.1994 +  }
  1.1995 +}
  1.1996 +
  1.1997 +// ------------------------------------------------------------------
  1.1998 +// ciTypeFlow::block_at
  1.1999 +//
  1.2000 +// Return the block beginning at bci which has a JsrSet compatible
  1.2001 +// with jsrs.
  1.2002 +ciTypeFlow::Block* ciTypeFlow::block_at(int bci, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
  1.2003 +  // First find the right ciBlock.
  1.2004 +  if (CITraceTypeFlow) {
  1.2005 +    tty->print(">> Requesting block for %d/", bci);
  1.2006 +    jsrs->print_on(tty);
  1.2007 +    tty->cr();
  1.2008 +  }
  1.2009 +
  1.2010 +  ciBlock* ciblk = _methodBlocks->block_containing(bci);
  1.2011 +  assert(ciblk->start_bci() == bci, "bad ciBlock boundaries");
  1.2012 +  Block* block = get_block_for(ciblk->index(), jsrs, option);
  1.2013 +
  1.2014 +  assert(block == NULL? (option == no_create): block->is_private_copy() == (option == create_private_copy), "create option consistent with result");
  1.2015 +
  1.2016 +  if (CITraceTypeFlow) {
  1.2017 +    if (block != NULL) {
  1.2018 +      tty->print(">> Found block ");
  1.2019 +      block->print_value_on(tty);
  1.2020 +      tty->cr();
  1.2021 +    } else {
  1.2022 +      tty->print_cr(">> No such block.");
  1.2023 +    }
  1.2024 +  }
  1.2025 +
  1.2026 +  return block;
  1.2027 +}
  1.2028 +
  1.2029 +// ------------------------------------------------------------------
  1.2030 +// ciTypeFlow::make_jsr_record
  1.2031 +//
  1.2032 +// Make a JsrRecord for a given (entry, return) pair, if such a record
  1.2033 +// does not already exist.
  1.2034 +ciTypeFlow::JsrRecord* ciTypeFlow::make_jsr_record(int entry_address,
  1.2035 +                                                   int return_address) {
  1.2036 +  if (_jsr_records == NULL) {
  1.2037 +    _jsr_records = new (arena()) GrowableArray<JsrRecord*>(arena(),
  1.2038 +                                                           _jsr_count,
  1.2039 +                                                           0,
  1.2040 +                                                           NULL);
  1.2041 +  }
  1.2042 +  JsrRecord* record = NULL;
  1.2043 +  int len = _jsr_records->length();
  1.2044 +  for (int i = 0; i < len; i++) {
  1.2045 +    JsrRecord* record = _jsr_records->at(i);
  1.2046 +    if (record->entry_address() == entry_address &&
  1.2047 +        record->return_address() == return_address) {
  1.2048 +      return record;
  1.2049 +    }
  1.2050 +  }
  1.2051 +
  1.2052 +  record = new (arena()) JsrRecord(entry_address, return_address);
  1.2053 +  _jsr_records->append(record);
  1.2054 +  return record;
  1.2055 +}
  1.2056 +
  1.2057 +// ------------------------------------------------------------------
  1.2058 +// ciTypeFlow::flow_exceptions
  1.2059 +//
  1.2060 +// Merge the current state into all exceptional successors at the
  1.2061 +// current point in the code.
  1.2062 +void ciTypeFlow::flow_exceptions(GrowableArray<ciTypeFlow::Block*>* exceptions,
  1.2063 +                                 GrowableArray<ciInstanceKlass*>* exc_klasses,
  1.2064 +                                 ciTypeFlow::StateVector* state) {
  1.2065 +  int len = exceptions->length();
  1.2066 +  assert(exc_klasses->length() == len, "must have same length");
  1.2067 +  for (int i = 0; i < len; i++) {
  1.2068 +    Block* block = exceptions->at(i);
  1.2069 +    ciInstanceKlass* exception_klass = exc_klasses->at(i);
  1.2070 +
  1.2071 +    if (!exception_klass->is_loaded()) {
  1.2072 +      // Do not compile any code for unloaded exception types.
  1.2073 +      // Following compiler passes are responsible for doing this also.
  1.2074 +      continue;
  1.2075 +    }
  1.2076 +
  1.2077 +    if (block->meet_exception(exception_klass, state)) {
  1.2078 +      // Block was modified.  Add it to the work list.
  1.2079 +      if (!block->is_on_work_list()) {
  1.2080 +        add_to_work_list(block);
  1.2081 +      }
  1.2082 +    }
  1.2083 +  }
  1.2084 +}
  1.2085 +
  1.2086 +// ------------------------------------------------------------------
  1.2087 +// ciTypeFlow::flow_successors
  1.2088 +//
  1.2089 +// Merge the current state into all successors at the current point
  1.2090 +// in the code.
  1.2091 +void ciTypeFlow::flow_successors(GrowableArray<ciTypeFlow::Block*>* successors,
  1.2092 +                                 ciTypeFlow::StateVector* state) {
  1.2093 +  int len = successors->length();
  1.2094 +  for (int i = 0; i < len; i++) {
  1.2095 +    Block* block = successors->at(i);
  1.2096 +    if (block->meet(state)) {
  1.2097 +      // Block was modified.  Add it to the work list.
  1.2098 +      if (!block->is_on_work_list()) {
  1.2099 +        add_to_work_list(block);
  1.2100 +      }
  1.2101 +    }
  1.2102 +  }
  1.2103 +}
  1.2104 +
  1.2105 +// ------------------------------------------------------------------
  1.2106 +// ciTypeFlow::can_trap
  1.2107 +//
  1.2108 +// Tells if a given instruction is able to generate an exception edge.
  1.2109 +bool ciTypeFlow::can_trap(ciBytecodeStream& str) {
  1.2110 +  // Cf. GenerateOopMap::do_exception_edge.
  1.2111 +  if (!Bytecodes::can_trap(str.cur_bc()))  return false;
  1.2112 +
  1.2113 +  switch (str.cur_bc()) {
  1.2114 +    case Bytecodes::_ldc:
  1.2115 +    case Bytecodes::_ldc_w:
  1.2116 +    case Bytecodes::_ldc2_w:
  1.2117 +    case Bytecodes::_aload_0:
  1.2118 +      // These bytecodes can trap for rewriting.  We need to assume that
  1.2119 +      // they do not throw exceptions to make the monitor analysis work.
  1.2120 +      return false;
  1.2121 +
  1.2122 +    case Bytecodes::_ireturn:
  1.2123 +    case Bytecodes::_lreturn:
  1.2124 +    case Bytecodes::_freturn:
  1.2125 +    case Bytecodes::_dreturn:
  1.2126 +    case Bytecodes::_areturn:
  1.2127 +    case Bytecodes::_return:
  1.2128 +      // We can assume the monitor stack is empty in this analysis.
  1.2129 +      return false;
  1.2130 +
  1.2131 +    case Bytecodes::_monitorexit:
  1.2132 +      // We can assume monitors are matched in this analysis.
  1.2133 +      return false;
  1.2134 +  }
  1.2135 +
  1.2136 +  return true;
  1.2137 +}
  1.2138 +
  1.2139 +
  1.2140 +// ------------------------------------------------------------------
  1.2141 +// ciTypeFlow::flow_block
  1.2142 +//
  1.2143 +// Interpret the effects of the bytecodes on the incoming state
  1.2144 +// vector of a basic block.  Push the changed state to succeeding
  1.2145 +// basic blocks.
  1.2146 +void ciTypeFlow::flow_block(ciTypeFlow::Block* block,
  1.2147 +                            ciTypeFlow::StateVector* state,
  1.2148 +                            ciTypeFlow::JsrSet* jsrs) {
  1.2149 +  if (CITraceTypeFlow) {
  1.2150 +    tty->print("\n>> ANALYZING BLOCK : ");
  1.2151 +    tty->cr();
  1.2152 +    block->print_on(tty);
  1.2153 +  }
  1.2154 +  assert(block->has_pre_order(), "pre-order is assigned before 1st flow");
  1.2155 +
  1.2156 +  int start = block->start();
  1.2157 +  int limit = block->limit();
  1.2158 +  int control = block->control();
  1.2159 +  if (control != ciBlock::fall_through_bci) {
  1.2160 +    limit = control;
  1.2161 +  }
  1.2162 +
  1.2163 +  // Grab the state from the current block.
  1.2164 +  block->copy_state_into(state);
  1.2165 +
  1.2166 +  GrowableArray<Block*>*           exceptions = block->exceptions();
  1.2167 +  GrowableArray<ciInstanceKlass*>* exc_klasses = block->exc_klasses();
  1.2168 +  bool has_exceptions = exceptions->length() > 0;
  1.2169 +
  1.2170 +  ciBytecodeStream str(method());
  1.2171 +  str.reset_to_bci(start);
  1.2172 +  Bytecodes::Code code;
  1.2173 +  while ((code = str.next()) != ciBytecodeStream::EOBC() &&
  1.2174 +         str.cur_bci() < limit) {
  1.2175 +    // Check for exceptional control flow from this point.
  1.2176 +    if (has_exceptions && can_trap(str)) {
  1.2177 +      flow_exceptions(exceptions, exc_klasses, state);
  1.2178 +    }
  1.2179 +    // Apply the effects of the current bytecode to our state.
  1.2180 +    bool res = state->apply_one_bytecode(&str);
  1.2181 +
  1.2182 +    // Watch for bailouts.
  1.2183 +    if (failing())  return;
  1.2184 +
  1.2185 +    if (res) {
  1.2186 +
  1.2187 +      // We have encountered a trap.  Record it in this block.
  1.2188 +      block->set_trap(state->trap_bci(), state->trap_index());
  1.2189 +
  1.2190 +      if (CITraceTypeFlow) {
  1.2191 +        tty->print_cr(">> Found trap");
  1.2192 +        block->print_on(tty);
  1.2193 +      }
  1.2194 +
  1.2195 +      // Record (no) successors.
  1.2196 +      block->successors(&str, state, jsrs);
  1.2197 +
  1.2198 +      // Discontinue interpretation of this Block.
  1.2199 +      return;
  1.2200 +    }
  1.2201 +  }
  1.2202 +
  1.2203 +  GrowableArray<Block*>* successors = NULL;
  1.2204 +  if (control != ciBlock::fall_through_bci) {
  1.2205 +    // Check for exceptional control flow from this point.
  1.2206 +    if (has_exceptions && can_trap(str)) {
  1.2207 +      flow_exceptions(exceptions, exc_klasses, state);
  1.2208 +    }
  1.2209 +
  1.2210 +    // Fix the JsrSet to reflect effect of the bytecode.
  1.2211 +    block->copy_jsrs_into(jsrs);
  1.2212 +    jsrs->apply_control(this, &str, state);
  1.2213 +
  1.2214 +    // Find successor edges based on old state and new JsrSet.
  1.2215 +    successors = block->successors(&str, state, jsrs);
  1.2216 +
  1.2217 +    // Apply the control changes to the state.
  1.2218 +    state->apply_one_bytecode(&str);
  1.2219 +  } else {
  1.2220 +    // Fall through control
  1.2221 +    successors = block->successors(&str, NULL, NULL);
  1.2222 +  }
  1.2223 +
  1.2224 +  // Pass our state to successors.
  1.2225 +  flow_successors(successors, state);
  1.2226 +}
  1.2227 +
  1.2228 +// ------------------------------------------------------------------
  1.2229 +// ciTypeFlow::flow_types
  1.2230 +//
  1.2231 +// Perform the type flow analysis, creating and cloning Blocks as
  1.2232 +// necessary.
  1.2233 +void ciTypeFlow::flow_types() {
  1.2234 +  ResourceMark rm;
  1.2235 +  StateVector* temp_vector = new StateVector(this);
  1.2236 +  JsrSet* temp_set = new JsrSet(NULL, 16);
  1.2237 +
  1.2238 +  // Create the method entry block.
  1.2239 +  Block* block = block_at(start_bci(), temp_set);
  1.2240 +  block->set_pre_order(_next_pre_order++);
  1.2241 +  assert(block->is_start(), "start block must have order #0");
  1.2242 +
  1.2243 +  // Load the initial state into it.
  1.2244 +  const StateVector* start_state = get_start_state();
  1.2245 +  if (failing())  return;
  1.2246 +  block->meet(start_state);
  1.2247 +  add_to_work_list(block);
  1.2248 +
  1.2249 +  // Trickle away.
  1.2250 +  while (!work_list_empty()) {
  1.2251 +    Block* block = work_list_next();
  1.2252 +    flow_block(block, temp_vector, temp_set);
  1.2253 +
  1.2254 +
  1.2255 +    // NodeCountCutoff is the number of nodes at which the parser
  1.2256 +    // will bail out.  Probably if we already have lots of BBs,
  1.2257 +    // the parser will generate at least twice that many nodes and bail out.
  1.2258 +    // Therefore, this is a conservatively large limit at which to
  1.2259 +    // bail out in the pre-parse typeflow pass.
  1.2260 +    int block_limit = MaxNodeLimit / 2;
  1.2261 +
  1.2262 +    if (_next_pre_order >= block_limit) {
  1.2263 +      // Too many basic blocks.  Bail out.
  1.2264 +      //
  1.2265 +      // This can happen when try/finally constructs are nested to depth N,
  1.2266 +      // and there is O(2**N) cloning of jsr bodies.  See bug 4697245!
  1.2267 +      record_failure("too many basic blocks");
  1.2268 +      return;
  1.2269 +    }
  1.2270 +
  1.2271 +    // Watch for bailouts.
  1.2272 +    if (failing())  return;
  1.2273 +  }
  1.2274 +}
  1.2275 +
  1.2276 +// ------------------------------------------------------------------
  1.2277 +// ciTypeFlow::map_blocks
  1.2278 +//
  1.2279 +// Create the block map, which indexes blocks in pre_order.
  1.2280 +void ciTypeFlow::map_blocks() {
  1.2281 +  assert(_block_map == NULL, "single initialization");
  1.2282 +  int pre_order_limit = _next_pre_order;
  1.2283 +  _block_map = NEW_ARENA_ARRAY(arena(), Block*, pre_order_limit);
  1.2284 +  assert(pre_order_limit == block_count(), "");
  1.2285 +  int po;
  1.2286 +  for (po = 0; po < pre_order_limit; po++) {
  1.2287 +    debug_only(_block_map[po] = NULL);
  1.2288 +  }
  1.2289 +  ciMethodBlocks *mblks = _methodBlocks;
  1.2290 +  ciBlock* current = NULL;
  1.2291 +  int limit_bci = code_size();
  1.2292 +  for (int bci = 0; bci < limit_bci; bci++) {
  1.2293 +    ciBlock* ciblk = mblks->block_containing(bci);
  1.2294 +    if (ciblk != NULL && ciblk != current) {
  1.2295 +      current = ciblk;
  1.2296 +      int curidx = ciblk->index();
  1.2297 +      int block_count = (_idx_to_blocklist[curidx] == NULL) ? 0 : _idx_to_blocklist[curidx]->length();
  1.2298 +      for (int i = 0; i < block_count; i++) {
  1.2299 +        Block* block = _idx_to_blocklist[curidx]->at(i);
  1.2300 +        if (!block->has_pre_order())  continue;
  1.2301 +        int po = block->pre_order();
  1.2302 +        assert(_block_map[po] == NULL, "unique ref to block");
  1.2303 +        assert(0 <= po && po < pre_order_limit, "");
  1.2304 +        _block_map[po] = block;
  1.2305 +      }
  1.2306 +    }
  1.2307 +  }
  1.2308 +  for (po = 0; po < pre_order_limit; po++) {
  1.2309 +    assert(_block_map[po] != NULL, "must not drop any blocks");
  1.2310 +    Block* block = _block_map[po];
  1.2311 +    // Remove dead blocks from successor lists:
  1.2312 +    for (int e = 0; e <= 1; e++) {
  1.2313 +      GrowableArray<Block*>* l = e? block->exceptions(): block->successors();
  1.2314 +      for (int i = 0; i < l->length(); i++) {
  1.2315 +        Block* s = l->at(i);
  1.2316 +        if (!s->has_pre_order()) {
  1.2317 +          if (CITraceTypeFlow) {
  1.2318 +            tty->print("Removing dead %s successor of #%d: ", (e? "exceptional":  "normal"), block->pre_order());
  1.2319 +            s->print_value_on(tty);
  1.2320 +            tty->cr();
  1.2321 +          }
  1.2322 +          l->remove(s);
  1.2323 +          --i;
  1.2324 +        }
  1.2325 +      }
  1.2326 +    }
  1.2327 +  }
  1.2328 +}
  1.2329 +
  1.2330 +// ------------------------------------------------------------------
  1.2331 +// ciTypeFlow::get_block_for
  1.2332 +//
  1.2333 +// Find a block with this ciBlock which has a compatible JsrSet.
  1.2334 +// If no such block exists, create it, unless the option is no_create.
  1.2335 +// If the option is create_private_copy, always create a fresh private copy.
  1.2336 +ciTypeFlow::Block* ciTypeFlow::get_block_for(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs, CreateOption option) {
  1.2337 +  Arena* a = arena();
  1.2338 +  GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
  1.2339 +  if (blocks == NULL) {
  1.2340 +    // Query only?
  1.2341 +    if (option == no_create)  return NULL;
  1.2342 +
  1.2343 +    // Allocate the growable array.
  1.2344 +    blocks = new (a) GrowableArray<Block*>(a, 4, 0, NULL);
  1.2345 +    _idx_to_blocklist[ciBlockIndex] = blocks;
  1.2346 +  }
  1.2347 +
  1.2348 +  if (option != create_private_copy) {
  1.2349 +    int len = blocks->length();
  1.2350 +    for (int i = 0; i < len; i++) {
  1.2351 +      Block* block = blocks->at(i);
  1.2352 +      if (!block->is_private_copy() && block->is_compatible_with(jsrs)) {
  1.2353 +        return block;
  1.2354 +      }
  1.2355 +    }
  1.2356 +  }
  1.2357 +
  1.2358 +  // Query only?
  1.2359 +  if (option == no_create)  return NULL;
  1.2360 +
  1.2361 +  // We did not find a compatible block.  Create one.
  1.2362 +  Block* new_block = new (a) Block(this, _methodBlocks->block(ciBlockIndex), jsrs);
  1.2363 +  if (option == create_private_copy)  new_block->set_private_copy(true);
  1.2364 +  blocks->append(new_block);
  1.2365 +  return new_block;
  1.2366 +}
  1.2367 +
  1.2368 +// ------------------------------------------------------------------
  1.2369 +// ciTypeFlow::private_copy_count
  1.2370 +//
  1.2371 +int ciTypeFlow::private_copy_count(int ciBlockIndex, ciTypeFlow::JsrSet* jsrs) const {
  1.2372 +  GrowableArray<Block*>* blocks = _idx_to_blocklist[ciBlockIndex];
  1.2373 +
  1.2374 +  if (blocks == NULL) {
  1.2375 +    return 0;
  1.2376 +  }
  1.2377 +
  1.2378 +  int count = 0;
  1.2379 +  int len = blocks->length();
  1.2380 +  for (int i = 0; i < len; i++) {
  1.2381 +    Block* block = blocks->at(i);
  1.2382 +    if (block->is_private_copy() && block->is_compatible_with(jsrs)) {
  1.2383 +      count++;
  1.2384 +    }
  1.2385 +  }
  1.2386 +
  1.2387 +  return count;
  1.2388 +}
  1.2389 +
  1.2390 +// ------------------------------------------------------------------
  1.2391 +// ciTypeFlow::do_flow
  1.2392 +//
  1.2393 +// Perform type inference flow analysis.
  1.2394 +void ciTypeFlow::do_flow() {
  1.2395 +  if (CITraceTypeFlow) {
  1.2396 +    tty->print_cr("\nPerforming flow analysis on method");
  1.2397 +    method()->print();
  1.2398 +    if (is_osr_flow())  tty->print(" at OSR bci %d", start_bci());
  1.2399 +    tty->cr();
  1.2400 +    method()->print_codes();
  1.2401 +  }
  1.2402 +  if (CITraceTypeFlow) {
  1.2403 +    tty->print_cr("Initial CI Blocks");
  1.2404 +    print_on(tty);
  1.2405 +  }
  1.2406 +  flow_types();
  1.2407 +  // Watch for bailouts.
  1.2408 +  if (failing()) {
  1.2409 +    return;
  1.2410 +  }
  1.2411 +  if (CIPrintTypeFlow || CITraceTypeFlow) {
  1.2412 +    print_on(tty);
  1.2413 +  }
  1.2414 +  map_blocks();
  1.2415 +}
  1.2416 +
  1.2417 +// ------------------------------------------------------------------
  1.2418 +// ciTypeFlow::record_failure()
  1.2419 +// The ciTypeFlow object keeps track of failure reasons separately from the ciEnv.
  1.2420 +// This is required because there is not a 1-1 relation between the ciEnv and
  1.2421 +// the TypeFlow passes within a compilation task.  For example, if the compiler
  1.2422 +// is considering inlining a method, it will request a TypeFlow.  If that fails,
  1.2423 +// the compilation as a whole may continue without the inlining.  Some TypeFlow
  1.2424 +// requests are not optional; if they fail the requestor is responsible for
  1.2425 +// copying the failure reason up to the ciEnv.  (See Parse::Parse.)
  1.2426 +void ciTypeFlow::record_failure(const char* reason) {
  1.2427 +  if (env()->log() != NULL) {
  1.2428 +    env()->log()->elem("failure reason='%s' phase='typeflow'", reason);
  1.2429 +  }
  1.2430 +  if (_failure_reason == NULL) {
  1.2431 +    // Record the first failure reason.
  1.2432 +    _failure_reason = reason;
  1.2433 +  }
  1.2434 +}
  1.2435 +
  1.2436 +#ifndef PRODUCT
  1.2437 +// ------------------------------------------------------------------
  1.2438 +// ciTypeFlow::print_on
  1.2439 +void ciTypeFlow::print_on(outputStream* st) const {
  1.2440 +  // Walk through CI blocks
  1.2441 +  st->print_cr("********************************************************");
  1.2442 +  st->print   ("TypeFlow for ");
  1.2443 +  method()->name()->print_symbol_on(st);
  1.2444 +  int limit_bci = code_size();
  1.2445 +  st->print_cr("  %d bytes", limit_bci);
  1.2446 +  ciMethodBlocks  *mblks = _methodBlocks;
  1.2447 +  ciBlock* current = NULL;
  1.2448 +  for (int bci = 0; bci < limit_bci; bci++) {
  1.2449 +    ciBlock* blk = mblks->block_containing(bci);
  1.2450 +    if (blk != NULL && blk != current) {
  1.2451 +      current = blk;
  1.2452 +      current->print_on(st);
  1.2453 +
  1.2454 +      GrowableArray<Block*>* blocks = _idx_to_blocklist[blk->index()];
  1.2455 +      int num_blocks = (blocks == NULL) ? 0 : blocks->length();
  1.2456 +
  1.2457 +      if (num_blocks == 0) {
  1.2458 +        st->print_cr("  No Blocks");
  1.2459 +      } else {
  1.2460 +        for (int i = 0; i < num_blocks; i++) {
  1.2461 +          Block* block = blocks->at(i);
  1.2462 +          block->print_on(st);
  1.2463 +        }
  1.2464 +      }
  1.2465 +      st->print_cr("--------------------------------------------------------");
  1.2466 +      st->cr();
  1.2467 +    }
  1.2468 +  }
  1.2469 +  st->print_cr("********************************************************");
  1.2470 +  st->cr();
  1.2471 +}
  1.2472 +#endif

mercurial