1 /*
   2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "compiler/compileLog.hpp"
  27 #include "interpreter/linkResolver.hpp"
  28 #include "memory/universe.inline.hpp"
  29 #include "oops/objArrayKlass.hpp"
  30 #include "oops/valueArrayKlass.hpp"
  31 #include "opto/addnode.hpp"
  32 #include "opto/castnode.hpp"
  33 #include "opto/memnode.hpp"
  34 #include "opto/parse.hpp"
  35 #include "opto/rootnode.hpp"
  36 #include "opto/runtime.hpp"
  37 #include "opto/subnode.hpp"
  38 #include "opto/valuetypenode.hpp"
  39 #include "runtime/deoptimization.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 
  42 //=============================================================================
  43 // Helper methods for _get* and _put* bytecodes
  44 //=============================================================================
  45 bool Parse::static_field_ok_in_clinit(ciField *field, ciMethod *method) {
  46   // Could be the field_holder's <clinit> method, or <clinit> for a subklass.
  47   // Better to check now than to Deoptimize as soon as we execute
  48   assert( field->is_static(), "Only check if field is static");
  49   // is_being_initialized() is too generous.  It allows access to statics
  50   // by threads that are not running the <clinit> before the <clinit> finishes.
  51   // return field->holder()->is_being_initialized();
  52 
  53   // The following restriction is correct but conservative.
  54   // It is also desirable to allow compilation of methods called from <clinit>
  55   // but this generated code will need to be made safe for execution by
  56   // other threads, or the transition from interpreted to compiled code would
  57   // need to be guarded.
  58   ciInstanceKlass *field_holder = field->holder();
  59 
  60   bool access_OK = false;
  61   if (method->holder()->is_subclass_of(field_holder)) {
  62     if (method->is_static()) {
  63       if (method->name() == ciSymbol::class_initializer_name()) {
  64         // OK to access static fields inside initializer
  65         access_OK = true;
  66       }
  67     } else {
  68       if (method->name() == ciSymbol::object_initializer_name()) {
  69         // It's also OK to access static fields inside a constructor,
  70         // because any thread calling the constructor must first have
  71         // synchronized on the class by executing a '_new' bytecode.
  72         access_OK = true;
  73       }
  74     }
  75   }
  76 
  77   return access_OK;
  78 
  79 }
  80 
  81 
  82 void Parse::do_field_access(bool is_get, bool is_field) {
  83   bool will_link;
  84   ciField* field = iter().get_field(will_link);
  85   assert(will_link, "getfield: typeflow responsibility");
  86 
  87   ciInstanceKlass* field_holder = field->holder();
  88 
  89   if (is_field && field_holder->is_valuetype()) {
  90     assert(is_get, "value type field store not supported");
  91     BasicType bt = field->layout_type();
  92     ValueTypeNode* vt = pop()->as_ValueType();
  93     Node* value = vt->field_value_by_offset(field->offset());
  94     push_node(bt, value);
  95     return;
  96   }
  97 
  98   if (is_field == field->is_static()) {
  99     // Interpreter will throw java_lang_IncompatibleClassChangeError
 100     // Check this before allowing <clinit> methods to access static fields
 101     uncommon_trap(Deoptimization::Reason_unhandled,
 102                   Deoptimization::Action_none);
 103     return;
 104   }
 105 
 106   if (!is_field && !field_holder->is_initialized()) {
 107     if (!static_field_ok_in_clinit(field, method())) {
 108       uncommon_trap(Deoptimization::Reason_uninitialized,
 109                     Deoptimization::Action_reinterpret,
 110                     NULL, "!static_field_ok_in_clinit");
 111       return;
 112     }
 113   }
 114 
 115   // Deoptimize on putfield writes to call site target field.
 116   if (!is_get && field->is_call_site_target()) {
 117     uncommon_trap(Deoptimization::Reason_unhandled,
 118                   Deoptimization::Action_reinterpret,
 119                   NULL, "put to call site target field");
 120     return;
 121   }
 122 
 123   assert(field->will_link(method(), bc()), "getfield: typeflow responsibility");
 124 
 125   // Note:  We do not check for an unloaded field type here any more.
 126 
 127   // Generate code for the object pointer.
 128   Node* obj;
 129   if (is_field) {
 130     int obj_depth = is_get ? 0 : field->type()->size();
 131     obj = null_check(peek(obj_depth));
 132     // Compile-time detect of null-exception?
 133     if (stopped())  return;
 134 
 135 #ifdef ASSERT
 136     const TypeInstPtr *tjp = TypeInstPtr::make(TypePtr::NotNull, iter().get_declared_field_holder());
 137     assert(_gvn.type(obj)->higher_equal(tjp), "cast_up is no longer needed");
 138 #endif
 139 
 140     if (is_get) {
 141       (void) pop();  // pop receiver before getting
 142       do_get_xxx(obj, field, is_field);
 143     } else {
 144       do_put_xxx(obj, field, is_field);
 145       (void) pop();  // pop receiver after putting
 146     }
 147   } else {
 148     const TypeInstPtr* tip = TypeInstPtr::make(field_holder->java_mirror());
 149     obj = _gvn.makecon(tip);
 150     if (is_get) {
 151       do_get_xxx(obj, field, is_field);
 152     } else {
 153       do_put_xxx(obj, field, is_field);
 154     }
 155   }
 156 }
 157 
 158 void Parse::do_get_xxx(Node* obj, ciField* field, bool is_field) {
 159   BasicType bt = field->layout_type();
 160 
 161   // Does this field have a constant value?  If so, just push the value.
 162   if (field->is_constant() &&
 163       // Keep consistent with types found by ciTypeFlow: for an
 164       // unloaded field type, ciTypeFlow::StateVector::do_getstatic()
 165       // speculates the field is null. The code in the rest of this
 166       // method does the same. We must not bypass it and use a non
 167       // null constant here.
 168       (bt != T_OBJECT || field->type()->is_loaded())) {
 169     // final or stable field
 170     Node* con = make_constant_from_field(field, obj);
 171     if (con != NULL) {
 172       push_node(field->layout_type(), con);
 173       return;
 174     }
 175   }
 176 
 177   ciType* field_klass = field->type();
 178   bool is_vol = field->is_volatile();
 179   // TODO change this when we support non-flattened value type fields that are non-static
 180   bool flattened = (bt == T_VALUETYPE) && !field->is_static();
 181 
 182   // Compute address and memory type.
 183   int offset = field->offset_in_bytes();
 184   const TypePtr* adr_type = C->alias_type(field)->adr_type();
 185   Node *adr = basic_plus_adr(obj, obj, offset);
 186 
 187   // Build the resultant type of the load
 188   const Type *type;
 189 
 190   bool must_assert_null = false;
 191   if (bt == T_OBJECT || bt == T_VALUETYPE) {
 192     if (!field->type()->is_loaded()) {
 193       type = TypeInstPtr::BOTTOM;
 194       must_assert_null = true;
 195     } else if (field->is_static_constant()) {
 196       // This can happen if the constant oop is non-perm.
 197       ciObject* con = field->constant_value().as_object();
 198       // Do not "join" in the previous type; it doesn't add value,
 199       // and may yield a vacuous result if the field is of interface type.
 200       if (con->is_null_object()) {
 201         type = TypePtr::NULL_PTR;
 202       } else {
 203         type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
 204       }
 205       assert(type != NULL, "field singleton type must be consistent");
 206     } else {
 207       type = TypeOopPtr::make_from_klass(field_klass->as_klass());
 208       if (bt == T_VALUETYPE && !flattened) {
 209         // A non-flattened value type field may be NULL
 210         bool maybe_null = true;
 211         if (field->is_static()) {
 212           // Check if static field is already initialized
 213           ciInstance* mirror = field->holder()->java_mirror();
 214           ciObject* val = mirror->field_value(field).as_object();
 215           if (!val->is_null_object()) {
 216             maybe_null = false;
 217           }
 218         }
 219         if (maybe_null) {
 220           type = type->is_valuetypeptr()->cast_to_ptr_type(TypePtr::BotPTR);
 221         }
 222       }
 223     }
 224   } else {
 225     type = Type::get_const_basic_type(bt);
 226   }
 227   if (support_IRIW_for_not_multiple_copy_atomic_cpu && field->is_volatile()) {
 228     insert_mem_bar(Op_MemBarVolatile);   // StoreLoad barrier
 229   }
 230 
 231   // Build the load.
 232   //
 233   MemNode::MemOrd mo = is_vol ? MemNode::acquire : MemNode::unordered;
 234   bool needs_atomic_access = is_vol || AlwaysAtomicAccesses;
 235   Node* ld = NULL;
 236    if (flattened) {
 237     // Load flattened value type
 238     ld = ValueTypeNode::make(_gvn, field_klass->as_value_klass(), map()->memory(), obj, obj, field->holder(), offset);
 239   } else {
 240     ld = make_load(NULL, adr, type, bt, adr_type, mo, LoadNode::DependsOnlyOnTest, needs_atomic_access);
 241   }
 242 
 243   // Adjust Java stack
 244   if (type2size[bt] == 1)
 245     push(ld);
 246   else
 247     push_pair(ld);
 248 
 249   if (must_assert_null) {
 250     // Do not take a trap here.  It's possible that the program
 251     // will never load the field's class, and will happily see
 252     // null values in this field forever.  Don't stumble into a
 253     // trap for such a program, or we might get a long series
 254     // of useless recompilations.  (Or, we might load a class
 255     // which should not be loaded.)  If we ever see a non-null
 256     // value, we will then trap and recompile.  (The trap will
 257     // not need to mention the class index, since the class will
 258     // already have been loaded if we ever see a non-null value.)
 259     // uncommon_trap(iter().get_field_signature_index());
 260     if (PrintOpto && (Verbose || WizardMode)) {
 261       method()->print_name(); tty->print_cr(" asserting nullness of field at bci: %d", bci());
 262     }
 263     if (C->log() != NULL) {
 264       C->log()->elem("assert_null reason='field' klass='%d'",
 265                      C->log()->identify(field->type()));
 266     }
 267     // If there is going to be a trap, put it at the next bytecode:
 268     set_bci(iter().next_bci());
 269     null_assert(peek());
 270     set_bci(iter().cur_bci()); // put it back
 271   }
 272 
 273   // If reference is volatile, prevent following memory ops from
 274   // floating up past the volatile read.  Also prevents commoning
 275   // another volatile read.
 276   if (field->is_volatile()) {
 277     // Memory barrier includes bogus read of value to force load BEFORE membar
 278     insert_mem_bar(Op_MemBarAcquire, ld);
 279   }
 280 }
 281 
 282 void Parse::do_put_xxx(Node* obj, ciField* field, bool is_field) {
 283   bool is_vol = field->is_volatile();
 284   // If reference is volatile, prevent following memory ops from
 285   // floating down past the volatile write.  Also prevents commoning
 286   // another volatile read.
 287   if (is_vol)  insert_mem_bar(Op_MemBarRelease);
 288 
 289   // Compute address and memory type.
 290   int offset = field->offset_in_bytes();
 291   const TypePtr* adr_type = C->alias_type(field)->adr_type();
 292   Node* adr = basic_plus_adr(obj, obj, offset);
 293   BasicType bt = field->layout_type();
 294   // Value to be stored
 295   Node* val = type2size[bt] == 1 ? pop() : pop_pair();
 296   // Round doubles before storing
 297   if (bt == T_DOUBLE)  val = dstore_rounding(val);
 298 
 299   // Conservatively release stores of object references.
 300   const MemNode::MemOrd mo =
 301     is_vol ?
 302     // Volatile fields need releasing stores.
 303     MemNode::release :
 304     // Non-volatile fields also need releasing stores if they hold an
 305     // object reference, because the object reference might point to
 306     // a freshly created object.
 307     StoreNode::release_if_reference(bt);
 308 
 309   // Store the value.
 310   if (bt == T_OBJECT || bt == T_VALUETYPE) {
 311     const TypeOopPtr* field_type;
 312     if (!field->type()->is_loaded()) {
 313       field_type = TypeInstPtr::BOTTOM;
 314     } else {
 315       field_type = TypeOopPtr::make_from_klass(field->type()->as_klass());
 316     }
 317     if (bt == T_VALUETYPE && !field->is_static()) {
 318       // Store flattened value type to non-static field
 319       val->as_ValueType()->store_flattened(this, obj, obj, field->holder(), offset);
 320     } else {
 321       store_oop_to_object(control(), obj, adr, adr_type, val, field_type, bt, mo);
 322     }
 323   } else {
 324     bool needs_atomic_access = is_vol || AlwaysAtomicAccesses;
 325     store_to_memory(control(), adr, val, bt, adr_type, mo, needs_atomic_access);
 326   }
 327 
 328   // If reference is volatile, prevent following volatiles ops from
 329   // floating up before the volatile write.
 330   if (is_vol) {
 331     // If not multiple copy atomic, we do the MemBarVolatile before the load.
 332     if (!support_IRIW_for_not_multiple_copy_atomic_cpu) {
 333       insert_mem_bar(Op_MemBarVolatile); // Use fat membar
 334     }
 335     // Remember we wrote a volatile field.
 336     // For not multiple copy atomic cpu (ppc64) a barrier should be issued
 337     // in constructors which have such stores. See do_exits() in parse1.cpp.
 338     if (is_field) {
 339       set_wrote_volatile(true);
 340     }
 341   }
 342 
 343   if (is_field) {
 344     set_wrote_fields(true);
 345   }
 346 
 347   // If the field is final, the rules of Java say we are in <init> or <clinit>.
 348   // Note the presence of writes to final non-static fields, so that we
 349   // can insert a memory barrier later on to keep the writes from floating
 350   // out of the constructor.
 351   // Any method can write a @Stable field; insert memory barriers after those also.
 352   if (is_field && (field->is_final() || field->is_stable())) {
 353     if (field->is_final()) {
 354         set_wrote_final(true);
 355     }
 356     if (field->is_stable()) {
 357         set_wrote_stable(true);
 358     }
 359 
 360     // Preserve allocation ptr to create precedent edge to it in membar
 361     // generated on exit from constructor.
 362     // Can't bind stable with its allocation, only record allocation for final field.
 363     if (field->is_final() && AllocateNode::Ideal_allocation(obj, &_gvn) != NULL) {
 364       set_alloc_with_final(obj);
 365     }
 366   }
 367 }
 368 
 369 //=============================================================================
 370 
 371 void Parse::do_newarray() {
 372   bool will_link;
 373   ciKlass* klass = iter().get_klass(will_link);
 374 
 375   // Uncommon Trap when class that array contains is not loaded
 376   // we need the loaded class for the rest of graph; do not
 377   // initialize the container class (see Java spec)!!!
 378   assert(will_link, "newarray: typeflow responsibility");
 379 
 380   ciArrayKlass* array_klass = ciArrayKlass::make(klass);
 381   // Check that array_klass object is loaded
 382   if (!array_klass->is_loaded()) {
 383     // Generate uncommon_trap for unloaded array_class
 384     uncommon_trap(Deoptimization::Reason_unloaded,
 385                   Deoptimization::Action_reinterpret,
 386                   array_klass);
 387     return;
 388   } else if (array_klass->element_klass() != NULL &&
 389              array_klass->element_klass()->is_valuetype() &&
 390              !array_klass->element_klass()->as_value_klass()->is_initialized()) {
 391     uncommon_trap(Deoptimization::Reason_uninitialized,
 392                   Deoptimization::Action_reinterpret,
 393                   NULL);
 394     return;
 395   }
 396 
 397   kill_dead_locals();
 398 
 399   const TypeKlassPtr* array_klass_type = TypeKlassPtr::make(array_klass);
 400   Node* count_val = pop();
 401   Node* obj = new_array(makecon(array_klass_type), count_val, 1);
 402   push(obj);
 403 }
 404 
 405 
 406 void Parse::do_newarray(BasicType elem_type) {
 407   kill_dead_locals();
 408 
 409   Node*   count_val = pop();
 410   const TypeKlassPtr* array_klass = TypeKlassPtr::make(ciTypeArrayKlass::make(elem_type));
 411   Node*   obj = new_array(makecon(array_klass), count_val, 1);
 412   // Push resultant oop onto stack
 413   push(obj);
 414 }
 415 
 416 // Expand simple expressions like new int[3][5] and new Object[2][nonConLen].
 417 // Also handle the degenerate 1-dimensional case of anewarray.
 418 Node* Parse::expand_multianewarray(ciArrayKlass* array_klass, Node* *lengths, int ndimensions, int nargs) {
 419   Node* length = lengths[0];
 420   assert(length != NULL, "");
 421   Node* array = new_array(makecon(TypeKlassPtr::make(array_klass)), length, nargs);
 422   if (ndimensions > 1) {
 423     jint length_con = find_int_con(length, -1);
 424     guarantee(length_con >= 0, "non-constant multianewarray");
 425     ciArrayKlass* array_klass_1 = array_klass->as_obj_array_klass()->element_klass()->as_array_klass();
 426     const TypePtr* adr_type = TypeAryPtr::OOPS;
 427     const TypeOopPtr*    elemtype = _gvn.type(array)->is_aryptr()->elem()->make_oopptr();
 428     const intptr_t header   = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 429     for (jint i = 0; i < length_con; i++) {
 430       Node*    elem   = expand_multianewarray(array_klass_1, &lengths[1], ndimensions-1, nargs);
 431       intptr_t offset = header + ((intptr_t)i << LogBytesPerHeapOop);
 432       Node*    eaddr  = basic_plus_adr(array, offset);
 433       store_oop_to_array(control(), array, eaddr, adr_type, elem, elemtype, T_OBJECT, MemNode::unordered);
 434     }
 435   }
 436   return array;
 437 }
 438 
 439 void Parse::do_multianewarray() {
 440   int ndimensions = iter().get_dimensions();
 441 
 442   // the m-dimensional array
 443   bool will_link;
 444   ciArrayKlass* array_klass = iter().get_klass(will_link)->as_array_klass();
 445   assert(will_link, "multianewarray: typeflow responsibility");
 446 
 447   // Note:  Array classes are always initialized; no is_initialized check.
 448 
 449   kill_dead_locals();
 450 
 451   // get the lengths from the stack (first dimension is on top)
 452   Node** length = NEW_RESOURCE_ARRAY(Node*, ndimensions + 1);
 453   length[ndimensions] = NULL;  // terminating null for make_runtime_call
 454   int j;
 455   for (j = ndimensions-1; j >= 0 ; j--) length[j] = pop();
 456 
 457   // The original expression was of this form: new T[length0][length1]...
 458   // It is often the case that the lengths are small (except the last).
 459   // If that happens, use the fast 1-d creator a constant number of times.
 460   const jint expand_limit = MIN2((jint)MultiArrayExpandLimit, 100);
 461   jint expand_count = 1;        // count of allocations in the expansion
 462   jint expand_fanout = 1;       // running total fanout
 463   for (j = 0; j < ndimensions-1; j++) {
 464     jint dim_con = find_int_con(length[j], -1);
 465     expand_fanout *= dim_con;
 466     expand_count  += expand_fanout; // count the level-J sub-arrays
 467     if (dim_con <= 0
 468         || dim_con > expand_limit
 469         || expand_count > expand_limit) {
 470       expand_count = 0;
 471       break;
 472     }
 473   }
 474 
 475   // Can use multianewarray instead of [a]newarray if only one dimension,
 476   // or if all non-final dimensions are small constants.
 477   if (ndimensions == 1 || (1 <= expand_count && expand_count <= expand_limit)) {
 478     Node* obj = NULL;
 479     // Set the original stack and the reexecute bit for the interpreter
 480     // to reexecute the multianewarray bytecode if deoptimization happens.
 481     // Do it unconditionally even for one dimension multianewarray.
 482     // Note: the reexecute bit will be set in GraphKit::add_safepoint_edges()
 483     // when AllocateArray node for newarray is created.
 484     { PreserveReexecuteState preexecs(this);
 485       inc_sp(ndimensions);
 486       // Pass 0 as nargs since uncommon trap code does not need to restore stack.
 487       obj = expand_multianewarray(array_klass, &length[0], ndimensions, 0);
 488     } //original reexecute and sp are set back here
 489     push(obj);
 490     return;
 491   }
 492 
 493   address fun = NULL;
 494   switch (ndimensions) {
 495   case 1: ShouldNotReachHere(); break;
 496   case 2: fun = OptoRuntime::multianewarray2_Java(); break;
 497   case 3: fun = OptoRuntime::multianewarray3_Java(); break;
 498   case 4: fun = OptoRuntime::multianewarray4_Java(); break;
 499   case 5: fun = OptoRuntime::multianewarray5_Java(); break;
 500   };
 501   Node* c = NULL;
 502 
 503   if (fun != NULL) {
 504     c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 505                           OptoRuntime::multianewarray_Type(ndimensions),
 506                           fun, NULL, TypeRawPtr::BOTTOM,
 507                           makecon(TypeKlassPtr::make(array_klass)),
 508                           length[0], length[1], length[2],
 509                           (ndimensions > 2) ? length[3] : NULL,
 510                           (ndimensions > 3) ? length[4] : NULL);
 511   } else {
 512     // Create a java array for dimension sizes
 513     Node* dims = NULL;
 514     { PreserveReexecuteState preexecs(this);
 515       inc_sp(ndimensions);
 516       Node* dims_array_klass = makecon(TypeKlassPtr::make(ciArrayKlass::make(ciType::make(T_INT))));
 517       dims = new_array(dims_array_klass, intcon(ndimensions), 0);
 518 
 519       // Fill-in it with values
 520       for (j = 0; j < ndimensions; j++) {
 521         Node *dims_elem = array_element_address(dims, intcon(j), T_INT);
 522         store_to_memory(control(), dims_elem, length[j], T_INT, TypeAryPtr::INTS, MemNode::unordered);
 523       }
 524     }
 525 
 526     c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 527                           OptoRuntime::multianewarrayN_Type(),
 528                           OptoRuntime::multianewarrayN_Java(), NULL, TypeRawPtr::BOTTOM,
 529                           makecon(TypeKlassPtr::make(array_klass)),
 530                           dims);
 531   }
 532   make_slow_call_ex(c, env()->Throwable_klass(), false);
 533 
 534   Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms));
 535 
 536   const Type* type = TypeOopPtr::make_from_klass_raw(array_klass);
 537 
 538   // Improve the type:  We know it's not null, exact, and of a given length.
 539   type = type->is_ptr()->cast_to_ptr_type(TypePtr::NotNull);
 540   type = type->is_aryptr()->cast_to_exactness(true);
 541 
 542   const TypeInt* ltype = _gvn.find_int_type(length[0]);
 543   if (ltype != NULL)
 544     type = type->is_aryptr()->cast_to_size(ltype);
 545 
 546     // We cannot sharpen the nested sub-arrays, since the top level is mutable.
 547 
 548   Node* cast = _gvn.transform( new CheckCastPPNode(control(), res, type) );
 549   push(cast);
 550 
 551   // Possible improvements:
 552   // - Make a fast path for small multi-arrays.  (W/ implicit init. loops.)
 553   // - Issue CastII against length[*] values, to TypeInt::POS.
 554 }
 555 
 556 void Parse::do_vbox() {
 557   // Obtain target type (from bytecodes)
 558   bool will_link;
 559   ciKlass* target_klass = iter().get_klass(will_link);
 560   guarantee(will_link, "vbox: Value-capable class must be loaded");
 561   guarantee(target_klass->is_instance_klass(), "vbox: Target class must be an instance type");
 562 
 563   // Obtain source type
 564   ValueTypeNode* vt = peek()->as_ValueType();
 565   const TypeValueType* src_type = gvn().type(vt)->isa_valuetype();
 566   guarantee(src_type != NULL, "vbox: Source type must not be null");
 567   ciValueKlass* src_vk = src_type->value_klass();
 568   guarantee(src_vk != NULL && src_vk->is_loaded() && src_vk->exact_klass(),
 569             "vbox: Source class must be a value type and must be loaded and exact");
 570 
 571   kill_dead_locals();
 572 
 573   ciInstanceKlass* target_vcc_klass = target_klass->as_instance_klass();
 574   ciInstanceKlass* src_vcc_klass = src_vk->vcc_klass();
 575 
 576   // TODO: Extend type check below if (and once) value type class hierarchies become available.
 577   // (incl. extension to support dynamic type checks).
 578   if (!src_vcc_klass->equals(target_vcc_klass)) {
 579     builtin_throw(Deoptimization::Reason_class_check);
 580     guarantee(stopped(), "A ClassCastException must be always thrown on this path");
 581     return;
 582   }
 583   guarantee(src_vk->is_valuetype(), "vbox: Target DVT must be a value type");
 584   pop();
 585 
 586   // Create new object
 587   Node* kls = makecon(TypeKlassPtr::make(target_vcc_klass));
 588   Node* obj = new_instance(kls);
 589 
 590   // Store all field values to the newly created object.
 591   // The code below relies on the assumption that the VCC has the
 592   // same memory layout as the derived value type.
 593   // TODO: Once the layout of the two is not the same, update code below.
 594   vt->as_ValueType()->store(this, obj, obj, target_vcc_klass);
 595 
 596   // Push the new object onto the stack
 597   push(obj);
 598 }
 599 
 600 void Parse::do_vunbox() {
 601   kill_dead_locals();
 602 
 603   // Check if the VCC instance is null.
 604   Node* not_null_obj = null_check(peek());
 605 
 606   // Value determined to be null at compile time
 607   if (stopped()) {
 608     return;
 609   }
 610 
 611   // Obtain target type (from bytecodes)
 612   bool will_link;
 613   ciKlass* target_klass = iter().get_klass(will_link);
 614   guarantee(will_link, "vunbox: Derived value type must be loaded");
 615   guarantee(target_klass->is_instance_klass(), "vunbox: Target class must be an instance type");
 616 
 617   // Obtain source type
 618   const TypeOopPtr* source_type = gvn().type(not_null_obj)->isa_oopptr();
 619   guarantee(source_type != NULL && source_type->klass() != NULL &&
 620             source_type->klass()->is_instance_klass() && source_type->klass()->is_loaded(),
 621             "vunbox: Source class must be an instance type and must be loaded");
 622 
 623   ciInstanceKlass* target_dvt_klass = target_klass->as_instance_klass();
 624   ciInstanceKlass* target_vcc_klass = target_dvt_klass->vcc_klass();
 625 
 626   // Check if the class of the source is a subclass of the value-capable class
 627   // corresponding to the target.
 628   // TOOD: Implement profiling of vunbox bytecodes to enable type speculation.
 629   if (target_vcc_klass == NULL || !source_type->klass()->is_subclass_of(target_vcc_klass)) {
 630     // It is obvious at compile-time that source and target are unrelated.
 631     builtin_throw(Deoptimization::Reason_class_check);
 632     guarantee(stopped(), "A ClassCastException must be always thrown on this path");
 633     return;
 634   }
 635   guarantee(target_dvt_klass->is_valuetype(), "vunbox: Target DVT must be a value type");
 636 
 637   if (!target_vcc_klass->equals(source_type->klass()) || !source_type->klass_is_exact()) {
 638     Node* exact_obj = not_null_obj;
 639     Node* slow_ctl  = type_check_receiver(exact_obj, target_vcc_klass, 1.0, &exact_obj);
 640     {
 641       PreserveJVMState pjvms(this);
 642       set_control(slow_ctl);
 643       builtin_throw(Deoptimization::Reason_class_check);
 644     }
 645     replace_in_map(not_null_obj, exact_obj);
 646     not_null_obj = exact_obj;
 647   }
 648 
 649   // Remove object from the top of the stack
 650   pop();
 651 
 652   // Create a value type node with the corresponding type
 653   ciValueKlass* vk = target_dvt_klass->as_value_klass();
 654   Node* vt = ValueTypeNode::make(gvn(), vk, map()->memory(), not_null_obj, not_null_obj, target_vcc_klass, vk->first_field_offset());
 655 
 656   // Push the value type onto the stack
 657   push(vt);
 658 }