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