1 /*
   2  * Copyright (c) 2017, 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 "gc/shared/barrierSet.hpp"
  27 #include "gc/shared/gcLocker.inline.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/metadataFactory.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "oops/fieldStreams.hpp"
  34 #include "oops/method.hpp"
  35 #include "oops/objArrayKlass.hpp"
  36 #include "oops/valueKlass.hpp"
  37 #include "oops/valueArrayKlass.hpp"
  38 #include "runtime/signature.hpp"
  39 #include "utilities/copy.hpp"
  40 
  41 int ValueKlass::first_field_offset() const {
  42 #ifdef ASSERT
  43   int first_offset = INT_MAX;
  44   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
  45     if (fs.offset() < first_offset) first_offset= fs.offset();
  46   }
  47 #endif
  48   int base_offset = instanceOopDesc::base_offset_in_bytes();
  49   // The first field of value types is aligned on a long boundary
  50   base_offset = align_up(base_offset, BytesPerLong);
  51   assert(base_offset == first_offset, "inconsistent offsets");
  52   return base_offset;
  53 }
  54 
  55 int ValueKlass::raw_value_byte_size() const {
  56   int heapOopAlignedSize = nonstatic_field_size() << LogBytesPerHeapOop;
  57   // If bigger than 64 bits or needs oop alignment, then use jlong aligned
  58   // which for values should be jlong aligned, asserts in raw_field_copy otherwise
  59   if (heapOopAlignedSize >= longSize || contains_oops()) {
  60     return heapOopAlignedSize;
  61   }
  62   // Small primitives...
  63   // If a few small basic type fields, return the actual size, i.e.
  64   // 1 byte = 1
  65   // 2 byte = 2
  66   // 3 byte = 4, because pow2 needed for element stores
  67   int first_offset = first_field_offset();
  68   int last_offset  = 0; // find the last offset, add basic type size
  69   int last_tsz     = 0;
  70   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
  71     if (fs.offset() > last_offset) {
  72       BasicType type = fs.field_descriptor().field_type();
  73       if (is_java_primitive(type)) {
  74         last_tsz = type2aelembytes(type);
  75       } else if (type == T_VALUETYPE) {
  76         // Not just primitives. Layout aligns embedded value, so use jlong aligned it is
  77         return heapOopAlignedSize;
  78       } else {
  79         guarantee(0, "Unknown type %d", type);
  80       }
  81       assert(last_tsz != 0, "Invariant");
  82       last_offset = fs.offset();
  83     }
  84   }
  85   // Assumes VT with no fields are meaningless and illegal
  86   last_offset += last_tsz;
  87   assert(last_offset > first_offset && last_tsz, "Invariant");
  88   return 1 << upper_log2(last_offset - first_offset);
  89 }
  90 
  91 instanceOop ValueKlass::allocate_instance(TRAPS) {
  92   int size = size_helper();  // Query before forming handle.
  93 
  94   return (instanceOop)CollectedHeap::obj_allocate(this, size, CHECK_NULL);
  95 }
  96 
  97 instanceOop ValueKlass::allocate_buffered_or_heap_instance(bool* in_heap, TRAPS) {
  98   assert(THREAD->is_Java_thread(), "Only Java threads can call this method");
  99 
 100   instanceOop value = NULL;
 101   if (is_bufferable()) {
 102     value = (instanceOop)VTBuffer::allocate_value(this, CHECK_NULL);
 103     *in_heap = false;
 104   }
 105   if (value == NULL) {
 106     log_info(valuetypes)("Value buffering failed, allocating in the Java heap");
 107     value = allocate_instance(CHECK_NULL);
 108     *in_heap = true;
 109   }
 110   return value;
 111 }
 112 
 113 bool ValueKlass::is_atomic() {
 114   return (nonstatic_field_size() * heapOopSize) <= longSize;
 115 }
 116 
 117 int ValueKlass::nonstatic_oop_count() {
 118   int oops = 0;
 119   int map_count = nonstatic_oop_map_count();
 120   OopMapBlock* block = start_of_nonstatic_oop_maps();
 121   OopMapBlock* end = block + map_count;
 122   while (block != end) {
 123     oops += block->count();
 124     block++;
 125   }
 126   return oops;
 127 }
 128 
 129 // Arrays of...
 130 
 131 bool ValueKlass::flatten_array() {
 132   if (!ValueArrayFlatten) {
 133     return false;
 134   }
 135 
 136   int elem_bytes = raw_value_byte_size();
 137   // Too big
 138   if ((ValueArrayElemMaxFlatSize >= 0) && (elem_bytes > ValueArrayElemMaxFlatSize)) {
 139     return false;
 140   }
 141   // Too many embedded oops
 142   if ((ValueArrayElemMaxFlatOops >= 0) && (nonstatic_oop_count() > ValueArrayElemMaxFlatOops)) {
 143     return false;
 144   }
 145 
 146   return true;
 147 }
 148 
 149 
 150 Klass* ValueKlass::array_klass_impl(bool or_null, int n, TRAPS) {
 151   if (!flatten_array()) {
 152     return InstanceKlass::array_klass_impl(or_null, n, THREAD);
 153   }
 154 
 155   // Basically the same as instanceKlass, but using "ValueArrayKlass::allocate_klass"
 156   if (array_klasses() == NULL) {
 157     if (or_null) return NULL;
 158 
 159     ResourceMark rm;
 160     JavaThread *jt = (JavaThread *)THREAD;
 161     {
 162       // Atomic creation of array_klasses
 163       MutexLocker mc(Compile_lock, THREAD);   // for vtables
 164       MutexLocker ma(MultiArray_lock, THREAD);
 165 
 166       // Check if update has already taken place
 167       if (array_klasses() == NULL) {
 168         Klass* ak;
 169         if (is_atomic() || (!ValueArrayAtomicAccess)) {
 170           ak = ValueArrayKlass::allocate_klass(this, CHECK_NULL);
 171         } else {
 172           ak = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
 173         }
 174         set_array_klasses(ak);
 175       }
 176     }
 177   }
 178   // _this will always be set at this point
 179   ArrayKlass* ak = ArrayKlass::cast(array_klasses());
 180   if (or_null) {
 181     return ak->array_klass_or_null(n);
 182   }
 183   return ak->array_klass(n, THREAD);
 184 }
 185 
 186 Klass* ValueKlass::array_klass_impl(bool or_null, TRAPS) {
 187   return array_klass_impl(or_null, 1, THREAD);
 188 }
 189 
 190 void ValueKlass::raw_field_copy(void* src, void* dst, size_t raw_byte_size) {
 191   /*
 192    * Try not to shear fields even if not an atomic store...
 193    *
 194    * First 3 cases handle value array store, otherwise works on the same basis
 195    * as JVM_Clone, at this size data is aligned. The order of primitive types
 196    * is largest to smallest, and it not possible for fields to stradle long
 197    * copy boundaries.
 198    *
 199    * If MT without exclusive access, possible to observe partial value store,
 200    * but not partial primitive and reference field values
 201    */
 202   switch (raw_byte_size) {
 203     case 1:
 204       *((jbyte*) dst) = *(jbyte*)src;
 205       break;
 206     case 2:
 207       *((jshort*) dst) = *(jshort*)src;
 208       break;
 209     case 4:
 210       *((jint*) dst) = *(jint*) src;
 211       break;
 212     default:
 213       assert(raw_byte_size % sizeof(jlong) == 0, "Unaligned raw_byte_size");
 214       Copy::conjoint_jlongs_atomic((jlong*)src, (jlong*)dst, raw_byte_size >> LogBytesPerLong);
 215   }
 216 }
 217 
 218 /*
 219  * Store the value of this klass contained with src into dst.
 220  *
 221  * This operation is appropriate for use from vastore, vaload and putfield (for values)
 222  *
 223  * GC barriers currently can lock with no safepoint check and allocate c-heap,
 224  * so raw point is "safe" for now.
 225  *
 226  * Going forward, look to use machine generated (stub gen or bc) version for most used klass layouts
 227  *
 228  */
 229 void ValueKlass::value_store(void* src, void* dst, size_t raw_byte_size, bool dst_heap, bool dst_uninitialized) {
 230   if (contains_oops()) {
 231     if (dst_heap) {
 232       // src/dst aren't oops, need offset to adjust oop map offset
 233       const address dst_oop_addr = ((address) dst) - first_field_offset();
 234 
 235       // Pre-barriers...
 236       OopMapBlock* map = start_of_nonstatic_oop_maps();
 237       OopMapBlock* const end = map + nonstatic_oop_map_count();
 238       while (map != end) {
 239         // Shame we can't just use the existing oop iterator...src/dst aren't oop
 240         address doop_address = dst_oop_addr + map->offset();
 241         // TEMP HACK: barrier code need to migrate to => access API (need own versions of value type ops)
 242         if (UseCompressedOops) {
 243           BarrierSet::barrier_set()->write_ref_array_pre((narrowOop*) doop_address, map->count(), dst_uninitialized);
 244         } else {
 245           BarrierSet::barrier_set()->write_ref_array_pre((oop*) doop_address, map->count(), dst_uninitialized);
 246         }
 247         map++;
 248       }
 249 
 250       raw_field_copy(src, dst, raw_byte_size);
 251 
 252       // Post-barriers...
 253       map = start_of_nonstatic_oop_maps();
 254       while (map != end) {
 255         address doop_address = dst_oop_addr + map->offset();
 256         BarrierSet::barrier_set()->write_ref_array((HeapWord*) doop_address, map->count());
 257         map++;
 258       }
 259     } else { // Buffered value case
 260       raw_field_copy(src, dst, raw_byte_size);
 261     }
 262   } else {   // Primitive-only case...
 263     raw_field_copy(src, dst, raw_byte_size);
 264   }
 265 }
 266 
 267 // Value type arguments are not passed by reference, instead each
 268 // field of the value type is passed as an argument. This helper
 269 // function collects the fields of the value types (including embedded
 270 // value type's fields) in a list. Included with the field's type is
 271 // the offset of each field in the value type: i2c and c2i adapters
 272 // need that to load or store fields. Finally, the list of fields is
 273 // sorted in order of increasing offsets: the adapters and the
 274 // compiled code need and agreed upon order of fields.
 275 //
 276 // The list of basic types that is returned starts with a T_VALUETYPE
 277 // and ends with an extra T_VOID. T_VALUETYPE/T_VOID are used as
 278 // delimiters. Every entry between the two is a field of the value
 279 // type. If there's an embedded value type in the list, it also starts
 280 // with a T_VALUETYPE and ends with a T_VOID. This is so we can
 281 // generate a unique fingerprint for the method's adapters and we can
 282 // generate the list of basic types from the interpreter point of view
 283 // (value types passed as reference: iterate on the list until a
 284 // T_VALUETYPE, drop everything until and including the closing
 285 // T_VOID) or the compiler point of view (each field of the value
 286 // types is an argument: drop all T_VALUETYPE/T_VOID from the list).
 287 GrowableArray<SigEntry> ValueKlass::collect_fields(int base_off) const {
 288   GrowableArray<SigEntry> sig_extended;
 289   sig_extended.push(SigEntry(T_VALUETYPE, base_off));
 290   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
 291     if (fs.access_flags().is_static()) continue;
 292     fieldDescriptor& fd = fs.field_descriptor();
 293     BasicType bt = fd.field_type();
 294     int offset = base_off + fd.offset() - (base_off > 0 ? first_field_offset() : 0);
 295     if (bt == T_VALUETYPE) {
 296       if (fd.is_flatten()) {
 297         Symbol* signature = fd.signature();
 298         JavaThread* THREAD = JavaThread::current();
 299         oop loader = class_loader();
 300         oop domain = protection_domain();
 301         ResetNoHandleMark rnhm;
 302         HandleMark hm;
 303         NoSafepointVerifier nsv;
 304         Klass* klass = SystemDictionary::resolve_or_null(signature,
 305                                                          Handle(THREAD, loader), Handle(THREAD, domain),
 306                                                          THREAD);
 307         assert(klass != NULL && !HAS_PENDING_EXCEPTION, "lookup shouldn't fail");
 308         const GrowableArray<SigEntry>& embedded = ValueKlass::cast(klass)->collect_fields(offset);
 309         sig_extended.appendAll(&embedded);
 310       } else {
 311         sig_extended.push(SigEntry(T_VALUETYPEPTR, offset));
 312       }
 313     } else {
 314       sig_extended.push(SigEntry(bt, offset));
 315       if (bt == T_LONG || bt == T_DOUBLE) {
 316         sig_extended.push(SigEntry(T_VOID, offset));
 317       }
 318     }
 319   }
 320   int offset = base_off + size_helper()*HeapWordSize - (base_off > 0 ? first_field_offset() : 0);
 321   sig_extended.push(SigEntry(T_VOID, offset)); // hack: use T_VOID to mark end of value type fields
 322   if (base_off == 0) {
 323     sig_extended.sort(SigEntry::compare);
 324   }
 325   assert(sig_extended.at(0)._bt == T_VALUETYPE && sig_extended.at(sig_extended.length()-1)._bt == T_VOID, "broken structure");
 326   return sig_extended;
 327 }
 328 
 329 void ValueKlass::initialize_calling_convention() {
 330   // Because the pack and unpack handler addresses need to be loadable from generated code,
 331   // they are stored at a fixed offset in the klass metadata. Since value type klasses do
 332   // not have a vtable, the vtable offset is used to store these addresses.
 333   //guarantee(vtable_length() == 0, "vtables are not supported in value klasses");
 334   if (ValueTypeReturnedAsFields || ValueTypePassFieldsAsArgs) {
 335     Thread* THREAD = Thread::current();
 336     assert(!HAS_PENDING_EXCEPTION, "should have no exception");
 337     ResourceMark rm;
 338     const GrowableArray<SigEntry>& sig_vk = collect_fields();
 339     int nb_fields = SigEntry::count_fields(sig_vk)+1;
 340     Array<SigEntry>* extended_sig = MetadataFactory::new_array<SigEntry>(class_loader_data(), sig_vk.length(), CHECK_AND_CLEAR);
 341     *((Array<SigEntry>**)adr_extended_sig()) = extended_sig;
 342     for (int i = 0; i < sig_vk.length(); i++) {
 343       extended_sig->at_put(i, sig_vk.at(i));
 344     }
 345 
 346     if (ValueTypeReturnedAsFields) {
 347       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, nb_fields);
 348       sig_bt[0] = T_METADATA;
 349       SigEntry::fill_sig_bt(sig_vk, sig_bt+1, nb_fields-1, true);
 350       VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, nb_fields);
 351       int total = SharedRuntime::java_return_convention(sig_bt, regs, nb_fields);
 352 
 353       if (total > 0) {
 354         Array<VMRegPair>* return_regs = MetadataFactory::new_array<VMRegPair>(class_loader_data(), nb_fields, CHECK_AND_CLEAR);
 355         *((Array<VMRegPair>**)adr_return_regs()) = return_regs;
 356         for (int i = 0; i < nb_fields; i++) {
 357           return_regs->at_put(i, regs[i]);
 358         }
 359 
 360         BufferedValueTypeBlob* buffered_blob = SharedRuntime::generate_buffered_value_type_adapter(this);
 361         *((address*)adr_pack_handler()) = buffered_blob->pack_fields();
 362         *((address*)adr_unpack_handler()) = buffered_blob->unpack_fields();
 363         assert(CodeCache::find_blob(pack_handler()) == buffered_blob, "lost track of blob");
 364       }
 365     }
 366   }
 367 }
 368 
 369 void ValueKlass::deallocate_contents(ClassLoaderData* loader_data) {
 370   if (extended_sig() != NULL) {
 371     MetadataFactory::free_array<SigEntry>(loader_data, extended_sig());
 372   }
 373   if (return_regs() != NULL) {
 374     MetadataFactory::free_array<VMRegPair>(loader_data, return_regs());
 375   }
 376   cleanup_blobs();
 377   InstanceKlass::deallocate_contents(loader_data);
 378 }
 379 
 380 void ValueKlass::cleanup(ValueKlass* ik) {
 381   ik->cleanup_blobs();
 382 }
 383 
 384 void ValueKlass::cleanup_blobs() {
 385   if (pack_handler() != NULL) {
 386     CodeBlob* buffered_blob = CodeCache::find_blob(pack_handler());
 387     assert(buffered_blob->is_buffered_value_type_blob(), "bad blob type");
 388     BufferBlob::free((BufferBlob*)buffered_blob);
 389     *((address*)adr_pack_handler()) = NULL;
 390     *((address*)adr_unpack_handler()) = NULL;
 391   }
 392 }
 393 
 394 // Can this value type be returned as multiple values?
 395 bool ValueKlass::can_be_returned_as_fields() const {
 396   return return_regs() != NULL;
 397 }
 398 
 399 // Create handles for all oop fields returned in registers that are going to be live across a safepoint
 400 void ValueKlass::save_oop_fields(const RegisterMap& reg_map, GrowableArray<Handle>& handles) const {
 401   Thread* thread = Thread::current();
 402   const Array<SigEntry>* sig_vk = extended_sig();
 403   const Array<VMRegPair>* regs = return_regs();
 404   int j = 1;
 405 
 406   for (int i = 0; i < sig_vk->length(); i++) {
 407     BasicType bt = sig_vk->at(i)._bt;
 408     if (bt == T_OBJECT || bt == T_VALUETYPEPTR || bt == T_ARRAY) {
 409       int off = sig_vk->at(i)._offset;
 410       VMRegPair pair = regs->at(j);
 411       address loc = reg_map.location(pair.first());
 412       oop v = *(oop*)loc;
 413       assert(v == NULL || oopDesc::is_oop(v), "not an oop?");
 414       assert(Universe::heap()->is_in_or_null(v), "must be heap pointer");
 415       handles.push(Handle(thread, v));
 416     }
 417     if (bt == T_VALUETYPE) {
 418       continue;
 419     }
 420     if (bt == T_VOID &&
 421         sig_vk->at(i-1)._bt != T_LONG &&
 422         sig_vk->at(i-1)._bt != T_DOUBLE) {
 423       continue;
 424     }
 425     j++;
 426   }
 427   assert(j == regs->length(), "missed a field?");
 428 }
 429 
 430 // Update oop fields in registers from handles after a safepoint
 431 void ValueKlass::restore_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const {
 432   assert(ValueTypeReturnedAsFields, "inconsistent");
 433   const Array<SigEntry>* sig_vk = extended_sig();
 434   const Array<VMRegPair>* regs = return_regs();
 435   assert(regs != NULL, "inconsistent");
 436 
 437   int j = 1;
 438   for (int i = 0, k = 0; i < sig_vk->length(); i++) {
 439     BasicType bt = sig_vk->at(i)._bt;
 440     if (bt == T_OBJECT || bt == T_ARRAY) {
 441       int off = sig_vk->at(i)._offset;
 442       VMRegPair pair = regs->at(j);
 443       address loc = reg_map.location(pair.first());
 444       *(oop*)loc = handles.at(k++)();
 445     }
 446     if (bt == T_VALUETYPE) {
 447       continue;
 448     }
 449     if (bt == T_VOID &&
 450         sig_vk->at(i-1)._bt != T_LONG &&
 451         sig_vk->at(i-1)._bt != T_DOUBLE) {
 452       continue;
 453     }
 454     j++;
 455   }
 456   assert(j == regs->length(), "missed a field?");
 457 }
 458 
 459 // Fields are in registers. Create an instance of the value type and
 460 // initialize it with the values of the fields.
 461 oop ValueKlass::realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, bool buffered, TRAPS) {
 462   bool ignored = false;
 463   oop new_vt = NULL;
 464   if (buffered) {
 465     new_vt = allocate_buffered_or_heap_instance(&ignored, CHECK_NULL);
 466   } else {
 467     new_vt = allocate_instance(CHECK_NULL);
 468   }
 469 
 470   const Array<SigEntry>* sig_vk = extended_sig();
 471   const Array<VMRegPair>* regs = return_regs();
 472 
 473   int j = 1;
 474   int k = 0;
 475   for (int i = 0; i < sig_vk->length(); i++) {
 476     BasicType bt = sig_vk->at(i)._bt;
 477     if (bt == T_VALUETYPE) {
 478       continue;
 479     }
 480     if (bt == T_VOID) {
 481       if (sig_vk->at(i-1)._bt == T_LONG ||
 482           sig_vk->at(i-1)._bt == T_DOUBLE) {
 483         j++;
 484       }
 485       continue;
 486     }
 487     int off = sig_vk->at(i)._offset;
 488     VMRegPair pair = regs->at(j);
 489     address loc = reg_map.location(pair.first());
 490     switch(bt) {
 491     case T_BOOLEAN: {
 492       jboolean v = *(intptr_t*)loc;
 493       *(jboolean*)((address)new_vt + off) = v;
 494       break;
 495     }
 496     case T_CHAR: {
 497       jchar v = *(intptr_t*)loc;
 498       *(jchar*)((address)new_vt + off) = v;
 499       break;
 500     }
 501     case T_BYTE: {
 502       jbyte v = *(intptr_t*)loc;
 503       *(jbyte*)((address)new_vt + off) = v;
 504       break;
 505     }
 506     case T_SHORT: {
 507       jshort v = *(intptr_t*)loc;
 508       *(jshort*)((address)new_vt + off) = v;
 509       break;
 510     }
 511     case T_INT: {
 512       jint v = *(intptr_t*)loc;
 513       *(jint*)((address)new_vt + off) = v;
 514       break;
 515     }
 516     case T_LONG: {
 517 #ifdef _LP64
 518       jlong v = *(intptr_t*)loc;
 519       *(jlong*)((address)new_vt + off) = v;
 520 #else
 521       Unimplemented();
 522 #endif
 523       break;
 524     }
 525     case T_OBJECT:
 526     case T_VALUETYPEPTR:
 527     case T_ARRAY: {
 528       Handle handle = handles.at(k++);
 529       oop v = handle();
 530       if (!UseCompressedOops) {
 531         oop* p = (oop*)((address)new_vt + off);
 532         oopDesc::store_heap_oop(p, v);
 533       } else {
 534         narrowOop* p = (narrowOop*)((address)new_vt + off);
 535         oopDesc::encode_store_heap_oop(p, v);
 536       }
 537       break;
 538     }
 539     case T_FLOAT: {
 540       jfloat v = *(jfloat*)loc;
 541       *(jfloat*)((address)new_vt + off) = v;
 542       break;
 543     }
 544     case T_DOUBLE: {
 545       jdouble v = *(jdouble*)loc;
 546       *(jdouble*)((address)new_vt + off) = v;
 547       break;
 548     }
 549     default:
 550       ShouldNotReachHere();
 551     }
 552     *(intptr_t*)loc = 0xDEAD;
 553     j++;
 554   }
 555   assert(j == regs->length(), "missed a field?");
 556   assert(k == handles.length(), "missed an oop?");
 557   return new_vt;
 558 }
 559 
 560 // Check the return register for a ValueKlass oop
 561 ValueKlass* ValueKlass::returned_value_klass(const RegisterMap& map) {
 562   BasicType bt = T_METADATA;
 563   VMRegPair pair;
 564   int nb = SharedRuntime::java_return_convention(&bt, &pair, 1);
 565   assert(nb == 1, "broken");
 566 
 567   address loc = map.location(pair.first());
 568   intptr_t ptr = *(intptr_t*)loc;
 569   if (is_set_nth_bit(ptr, 0)) {
 570     // Oop is tagged, must be a ValueKlass oop
 571     clear_nth_bit(ptr, 0);
 572     assert(Metaspace::contains((void*)ptr), "should be klass");
 573     ValueKlass* vk = (ValueKlass*)ptr;
 574     assert(vk->can_be_returned_as_fields(), "must be able to return as fields");
 575     return vk;
 576   }
 577 #ifdef ASSERT
 578   // Oop is not tagged, must be a valid oop
 579   if (VerifyOops) {
 580     oop((HeapWord*)ptr)->verify();
 581   }
 582 #endif
 583   return NULL;
 584 }
 585 
 586 void ValueKlass::iterate_over_inside_oops(OopClosure* f, oop value) {
 587   assert(!Universe::heap()->is_in_reserved(value), "This method is used on buffered values");
 588 
 589   oop* addr_mirror = (oop*)(value)->mark_addr();
 590   f->do_oop_no_buffering(addr_mirror);
 591 
 592   if (!contains_oops()) return;
 593 
 594   OopMapBlock* map = start_of_nonstatic_oop_maps();
 595   OopMapBlock* const end_map = map + nonstatic_oop_map_count();
 596 
 597   if (!UseCompressedOops) {
 598     for (; map < end_map; map++) {
 599       oop* p = (oop*) (((char*)(oopDesc*)value) + map->offset());
 600       oop* const end = p + map->count();
 601       for (; p < end; ++p) {
 602         assert(oopDesc::is_oop_or_null(*p), "Sanity check");
 603         f->do_oop(p);
 604       }
 605     }
 606   } else {
 607     for (; map < end_map; map++) {
 608       narrowOop* p = (narrowOop*) (((char*)(oopDesc*)value) + map->offset());
 609       narrowOop* const end = p + map->count();
 610       for (; p < end; ++p) {
 611         oop o = oopDesc::decode_heap_oop(*p);
 612         assert(Universe::heap()->is_in_reserved_or_null(o), "Sanity check");
 613         assert(oopDesc::is_oop_or_null(o), "Sanity check");
 614         f->do_oop(p);
 615       }
 616     }
 617   }
 618 }