1 /* 2 * Copyright (c) 1998, 2015, 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 #ifndef SHARE_VM_COMPILER_OOPMAP_HPP 26 #define SHARE_VM_COMPILER_OOPMAP_HPP 27 28 #include "code/compressedStream.hpp" 29 #include "code/vmreg.hpp" 30 #include "memory/allocation.hpp" 31 #include "utilities/growableArray.hpp" 32 33 // Interface for generating the frame map for compiled code. A frame map 34 // describes for a specific pc whether each register and frame stack slot is: 35 // Oop - A GC root for current frame 36 // Dead - Dead; can be Zapped for debugging 37 // CalleeXX - Callee saved; also describes which caller register is saved 38 // DerivedXX - A derived oop; original oop is described. 39 // 40 // OopMapValue describes a single OopMap entry 41 42 class frame; 43 class RegisterMap; 44 class DerivedPointerEntry; 45 46 class OopMapValue: public StackObj { 47 friend class VMStructs; 48 private: 49 short _value; 50 int value() const { return _value; } 51 void set_value(int value) { _value = value; } 52 short _content_reg; 53 54 public: 55 // Constants 56 enum { type_bits = 4, 57 register_bits = BitsPerShort - type_bits }; 58 59 enum { type_shift = 0, 60 register_shift = type_bits }; 61 62 enum { type_mask = right_n_bits(type_bits), 63 type_mask_in_place = type_mask << type_shift, 64 register_mask = right_n_bits(register_bits), 65 register_mask_in_place = register_mask << register_shift }; 66 67 enum oop_types { // must fit in type_bits 68 unused_value =0, // powers of 2, for masking OopMapStream 69 oop_value = 1, 70 narrowoop_value = 2, 71 callee_saved_value = 4, 72 derived_oop_value= 8 }; 73 74 // Constructors 75 OopMapValue () { set_value(0); set_content_reg(VMRegImpl::Bad()); } 76 OopMapValue (VMReg reg, oop_types t) { set_reg_type(reg, t); set_content_reg(VMRegImpl::Bad()); } 77 OopMapValue (VMReg reg, oop_types t, VMReg reg2) { set_reg_type(reg, t); set_content_reg(reg2); } 78 OopMapValue (CompressedReadStream* stream) { read_from(stream); } 79 80 // Archiving 81 void write_on(CompressedWriteStream* stream) { 82 stream->write_int(value()); 83 if(is_callee_saved() || is_derived_oop()) { 84 stream->write_int(content_reg()->value()); 85 } 86 } 87 88 void read_from(CompressedReadStream* stream) { 89 set_value(stream->read_int()); 90 if (is_callee_saved() || is_derived_oop()) { 91 set_content_reg(VMRegImpl::as_VMReg(stream->read_int(), true)); 92 } 93 } 94 95 // Querying 96 bool is_oop() { return mask_bits(value(), type_mask_in_place) == oop_value; } 97 bool is_narrowoop() { return mask_bits(value(), type_mask_in_place) == narrowoop_value; } 98 bool is_callee_saved() { return mask_bits(value(), type_mask_in_place) == callee_saved_value; } 99 bool is_derived_oop() { return mask_bits(value(), type_mask_in_place) == derived_oop_value; } 100 101 void set_oop() { set_value((value() & register_mask_in_place) | oop_value); } 102 void set_narrowoop() { set_value((value() & register_mask_in_place) | narrowoop_value); } 103 void set_callee_saved() { set_value((value() & register_mask_in_place) | callee_saved_value); } 104 void set_derived_oop() { set_value((value() & register_mask_in_place) | derived_oop_value); } 105 106 VMReg reg() const { return VMRegImpl::as_VMReg(mask_bits(value(), register_mask_in_place) >> register_shift); } 107 oop_types type() const { return (oop_types)mask_bits(value(), type_mask_in_place); } 108 109 static bool legal_vm_reg_name(VMReg p) { 110 return (p->value() == (p->value() & register_mask)); 111 } 112 113 void set_reg_type(VMReg p, oop_types t) { 114 set_value((p->value() << register_shift) | t); 115 assert(reg() == p, "sanity check" ); 116 assert(type() == t, "sanity check" ); 117 } 118 119 120 VMReg content_reg() const { return VMRegImpl::as_VMReg(_content_reg, true); } 121 void set_content_reg(VMReg r) { _content_reg = r->value(); } 122 123 // Physical location queries 124 bool is_register_loc() { return reg()->is_reg(); } 125 bool is_stack_loc() { return reg()->is_stack(); } 126 127 // Returns offset from sp. 128 int stack_offset() { 129 assert(is_stack_loc(), "must be stack location"); 130 return reg()->reg2stack(); 131 } 132 133 void print_on(outputStream* st) const; 134 void print() const { print_on(tty); } 135 }; 136 137 138 class OopMap: public ResourceObj { 139 friend class OopMapStream; 140 friend class VMStructs; 141 private: 142 int _pc_offset; // offset in the code that this OopMap corresponds to 143 int _omv_count; // number of OopMapValues in the stream 144 CompressedWriteStream* _write_stream; 145 146 debug_only( OopMapValue::oop_types* _locs_used; int _locs_length;) 147 148 // Accessors 149 int omv_count() const { return _omv_count; } 150 void set_omv_count(int value) { _omv_count = value; } 151 void increment_count() { _omv_count++; } 152 CompressedWriteStream* write_stream() const { return _write_stream; } 153 void set_write_stream(CompressedWriteStream* value) { _write_stream = value; } 154 155 private: 156 enum DeepCopyToken { _deep_copy_token }; 157 OopMap(DeepCopyToken, OopMap* source); // used only by deep_copy 158 159 public: 160 OopMap(int frame_size, int arg_count); 161 162 // pc-offset handling 163 int offset() const { return _pc_offset; } 164 void set_offset(int o) { _pc_offset = o; } 165 int count() const { return _omv_count; } 166 int data_size() const { return write_stream()->position(); } 167 address data() const { return write_stream()->buffer(); } 168 169 // Check to avoid double insertion 170 debug_only(OopMapValue::oop_types locs_used( int indx ) { return _locs_used[indx]; }) 171 172 // Construction 173 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd 174 // slots to hold 4-byte values like ints and floats in the LP64 build. 175 void set_oop ( VMReg local); 176 void set_value( VMReg local); 177 void set_narrowoop(VMReg local); 178 void set_dead ( VMReg local); 179 void set_callee_saved( VMReg local, VMReg caller_machine_register ); 180 void set_derived_oop ( VMReg local, VMReg derived_from_local_register ); 181 void set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional); 182 183 int heap_size() const; 184 void copy_data_to(address addr) const; 185 OopMap* deep_copy(); 186 187 bool has_derived_pointer() const PRODUCT_RETURN0; 188 189 bool legal_vm_reg_name(VMReg local) { 190 return OopMapValue::legal_vm_reg_name(local); 191 } 192 193 // Printing 194 void print_on(outputStream* st) const; 195 void print() const { print_on(tty); } 196 bool equals(const OopMap* other) const; 197 }; 198 199 200 class OopMapSet : public ResourceObj { 201 friend class VMStructs; 202 private: 203 int _om_count; 204 int _om_size; 205 OopMap** _om_data; 206 207 int om_count() const { return _om_count; } 208 void set_om_count(int value) { _om_count = value; } 209 void increment_count() { _om_count++; } 210 int om_size() const { return _om_size; } 211 void set_om_size(int value) { _om_size = value; } 212 OopMap** om_data() const { return _om_data; } 213 void set_om_data(OopMap** value) { _om_data = value; } 214 void grow_om_data(); 215 void set(int index,OopMap* value) { assert((index == 0) || ((index > 0) && (index < om_size())),"bad index"); _om_data[index] = value; } 216 217 public: 218 OopMapSet(); 219 220 // returns the number of OopMaps in this OopMapSet 221 int size() const { return _om_count; } 222 // returns the OopMap at a given index 223 OopMap* at(int index) const { assert((index >= 0) && (index <= om_count()),"bad index"); return _om_data[index]; } 224 225 // Collect OopMaps. 226 void add_gc_map(int pc, OopMap* map); 227 228 // Returns the only oop map. Used for reconstructing 229 // Adapter frames during deoptimization 230 OopMap* singular_oop_map(); 231 232 // returns OopMap in that is anchored to the pc 233 OopMap* find_map_at_offset(int pc_offset) const; 234 235 int heap_size() const; 236 237 // Methods oops_do() and all_do() filter out NULL oops and 238 // oop == Universe::narrow_oop_base() before passing oops 239 // to closures. 240 241 // Iterates through frame for a compiled method 242 static void oops_do (const frame* fr, 243 const RegisterMap* reg_map, OopClosure* f); 244 static void update_register_map(const frame* fr, RegisterMap *reg_map); 245 246 // Iterates through frame for a compiled method for dead ones and values, too 247 static void all_do(const frame* fr, const RegisterMap* reg_map, 248 OopClosure* oop_fn, 249 void derived_oop_fn(oop* base, oop* derived), 250 OopClosure* value_fn); 251 252 // Printing 253 void print_on(outputStream* st) const; 254 void print() const { print_on(tty); } 255 }; 256 257 class ImmutableOopMapBuilder; 258 259 class ImmutableOopMap { 260 friend class OopMapStream; 261 friend class VMStructs; 262 #ifdef ASSERT 263 friend class ImmutableOopMapBuilder; 264 #endif 265 private: 266 int _count; // contains the number of entries in this OopMap 267 268 address data_addr() const { return (address) this + sizeof(ImmutableOopMap); } 269 public: 270 ImmutableOopMap(const OopMap* oopmap); 271 272 bool has_derived_pointer() const PRODUCT_RETURN0; 273 int count() const { return _count; } 274 #ifdef ASSERT 275 int nr_of_bytes() const; // this is an expensive operation, only used in debug builds 276 #endif 277 278 // Printing 279 void print_on(outputStream* st) const; 280 void print() const { print_on(tty); } 281 }; 282 283 class ImmutableOopMapSet; 284 class ImmutableOopMap; 285 class OopMapSet; 286 287 class ImmutableOopMapPair { 288 friend class VMStructs; 289 private: 290 int _pc_offset; // program counter offset from the beginning of the method 291 int _oopmap_offset; // offset in the data in the ImmutableOopMapSet where the ImmutableOopMap is located 292 public: 293 ImmutableOopMapPair(int pc_offset, int oopmap_offset) : _pc_offset(pc_offset), _oopmap_offset(oopmap_offset) { 294 assert(pc_offset >= 0 && oopmap_offset >= 0, "check"); 295 } 296 const ImmutableOopMap* get_from(const ImmutableOopMapSet* set) const; 297 298 int pc_offset() const { return _pc_offset; } 299 int oopmap_offset() const { return _oopmap_offset; } 300 }; 301 302 class ImmutableOopMapSet { 303 friend class VMStructs; 304 private: 305 int _count; // nr of ImmutableOopMapPairs in the Set 306 int _size; // nr of bytes including ImmutableOopMapSet itself 307 308 address data() const { return (address) this + sizeof(*this) + sizeof(ImmutableOopMapPair) * _count; } 309 310 public: 311 ImmutableOopMapSet(const OopMapSet* oopmap_set, int size) : _count(oopmap_set->size()), _size(size) {} 312 313 ImmutableOopMap* oopmap_at_offset(int offset) const { 314 assert(offset >= 0 && offset < _size, "must be within boundaries"); 315 address addr = data() + offset; 316 return (ImmutableOopMap*) addr; 317 } 318 319 ImmutableOopMapPair* get_pairs() const { return (ImmutableOopMapPair*) ((address) this + sizeof(*this)); } 320 321 static ImmutableOopMapSet* build_from(const OopMapSet* oopmap_set); 322 323 const ImmutableOopMap* find_map_at_offset(int pc_offset) const; 324 325 const ImmutableOopMapPair* pair_at(int index) const { assert(index >= 0 && index < _count, "check"); return &get_pairs()[index]; } 326 327 int count() const { return _count; } 328 int nr_of_bytes() const { return _size; } 329 330 void print_on(outputStream* st) const; 331 void print() const { print_on(tty); } 332 }; 333 334 class OopMapStream : public StackObj { 335 private: 336 CompressedReadStream* _stream; 337 int _mask; 338 int _size; 339 int _position; 340 bool _valid_omv; 341 OopMapValue _omv; 342 void find_next(); 343 344 public: 345 OopMapStream(OopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place); 346 OopMapStream(const ImmutableOopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place); 347 bool is_done() { if(!_valid_omv) { find_next(); } return !_valid_omv; } 348 void next() { find_next(); } 349 OopMapValue current() { return _omv; } 350 #ifdef ASSERT 351 int stream_position() const { return _stream->position(); } 352 #endif 353 }; 354 355 class ImmutableOopMapBuilder { 356 private: 357 class Mapping; 358 359 private: 360 const OopMapSet* _set; 361 const OopMap* _empty; 362 const OopMap* _last; 363 int _empty_offset; 364 int _last_offset; 365 int _offset; 366 int _required; 367 Mapping* _mapping; 368 ImmutableOopMapSet* _new_set; 369 370 /* Used for bookkeeping when building ImmutableOopMaps */ 371 class Mapping : public ResourceObj { 372 public: 373 enum kind_t { OOPMAP_UNKNOWN = 0, OOPMAP_NEW = 1, OOPMAP_EMPTY = 2, OOPMAP_DUPLICATE = 3 }; 374 375 kind_t _kind; 376 int _offset; 377 int _size; 378 const OopMap* _map; 379 const OopMap* _other; 380 381 Mapping() : _kind(OOPMAP_UNKNOWN), _offset(-1), _size(-1), _map(NULL) {} 382 383 void set(kind_t kind, int offset, int size, const OopMap* map = 0, const OopMap* other = 0) { 384 _kind = kind; 385 _offset = offset; 386 _size = size; 387 _map = map; 388 _other = other; 389 } 390 }; 391 392 public: 393 ImmutableOopMapBuilder(const OopMapSet* set); 394 395 int heap_size(); 396 ImmutableOopMapSet* build(); 397 ImmutableOopMapSet* generate_into(address buffer); 398 private: 399 bool is_empty(const OopMap* map) const { 400 return map->count() == 0; 401 } 402 403 bool is_last_duplicate(const OopMap* map) { 404 if (_last != NULL && _last->count() > 0 && _last->equals(map)) { 405 return true; 406 } 407 return false; 408 } 409 410 #ifdef ASSERT 411 void verify(address buffer, int size, const ImmutableOopMapSet* set); 412 #endif 413 414 bool has_empty() const { 415 return _empty_offset != -1; 416 } 417 418 int size_for(const OopMap* map) const; 419 void fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set); 420 int fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set); 421 void fill(ImmutableOopMapSet* set, int size); 422 }; 423 424 425 // Derived pointer support. This table keeps track of all derived points on a 426 // stack. It is cleared before each scavenge/GC. During the traversal of all 427 // oops, it is filled in with references to all locations that contains a 428 // derived oop (assumed to be very few). When the GC is complete, the derived 429 // pointers are updated based on their base pointers new value and an offset. 430 #if defined(COMPILER2) || INCLUDE_JVMCI 431 class DerivedPointerTable : public AllStatic { 432 friend class VMStructs; 433 private: 434 static GrowableArray<DerivedPointerEntry*>* _list; 435 static bool _active; // do not record pointers for verify pass etc. 436 public: 437 static void clear(); // Called before scavenge/GC 438 static void add(oop *derived, oop *base); // Called during scavenge/GC 439 static void update_pointers(); // Called after scavenge/GC 440 static bool is_empty() { return _list == NULL || _list->is_empty(); } 441 static bool is_active() { return _active; } 442 static void set_active(bool value) { _active = value; } 443 }; 444 445 // A utility class to temporarily "deactivate" the DerivedPointerTable. 446 // (Note: clients are responsible for any MT-safety issues) 447 class DerivedPointerTableDeactivate: public StackObj { 448 private: 449 bool _active; 450 public: 451 DerivedPointerTableDeactivate() { 452 _active = DerivedPointerTable::is_active(); 453 if (_active) { 454 DerivedPointerTable::set_active(false); 455 } 456 } 457 458 ~DerivedPointerTableDeactivate() { 459 assert(!DerivedPointerTable::is_active(), 460 "Inconsistency: not MT-safe"); 461 if (_active) { 462 DerivedPointerTable::set_active(true); 463 } 464 } 465 }; 466 #endif // COMPILER2 || INCLUDE_JVMCI 467 468 #endif // SHARE_VM_COMPILER_OOPMAP_HPP