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