1 /*
  2  * Copyright (c) 2019, 2023, 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_OOPS_COMPRESSEDOOPS_HPP
 26 #define SHARE_OOPS_COMPRESSEDOOPS_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "memory/memRegion.hpp"
 30 #include "oops/oopsHierarchy.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 #include <type_traits>
 33 
 34 class outputStream;
 35 class ReservedHeapSpace;
 36 
 37 struct NarrowPtrStruct {
 38   // Base address for oop-within-java-object materialization.
 39   // null if using wide oops or zero based narrow oops.
 40   address _base;
 41   // Number of shift bits for encoding/decoding narrow ptrs.
 42   // 0 if using wide ptrs or zero based unscaled narrow ptrs,
 43   // LogMinObjAlignmentInBytes/LogKlassAlignmentInBytes otherwise.
 44   int     _shift;
 45   // Generate code with implicit null checks for narrow ptrs.
 46   bool    _use_implicit_null_checks;
 47 };
 48 
 49 class CompressedOops : public AllStatic {
 50   friend class VMStructs;
 51 
 52   // For UseCompressedOops.
 53   static NarrowPtrStruct _narrow_oop;
 54 
 55   // The address range of the heap
 56   static MemRegion _heap_address_range;
 57 
 58 public:
 59   // For UseCompressedOops
 60   // Narrow Oop encoding mode:
 61   // 0 - Use 32-bits oops without encoding when
 62   //     NarrowOopHeapBaseMin + heap_size < 4Gb
 63   // 1 - Use zero based compressed oops with encoding when
 64   //     NarrowOopHeapBaseMin + heap_size < 32Gb
 65   // 2 - Use compressed oops with disjoint heap base if
 66   //     base is 32G-aligned and base > 0. This allows certain
 67   //     optimizations in encoding/decoding.
 68   //     Disjoint: Bits used in base are disjoint from bits used
 69   //     for oops ==> oop = (cOop << 3) | base.  One can disjoint
 70   //     the bits of an oop into base and compressed oop.
 71   // 3 - Use compressed oops with heap base + encoding.
 72   enum Mode {
 73     UnscaledNarrowOop  = 0,
 74     ZeroBasedNarrowOop = 1,
 75     DisjointBaseNarrowOop = 2,
 76     HeapBasedNarrowOop = 3,
 77     AnyNarrowOopMode = 4
 78   };
 79 
 80   // The representation type for narrowOop is assumed to be uint32_t.
 81   static_assert(std::is_same<uint32_t, std::underlying_type_t<narrowOop>>::value,
 82                 "narrowOop has unexpected representation type");
 83 
 84   static void initialize(const ReservedHeapSpace& heap_space);
 85 
 86   static void set_base(address base);
 87   static void set_shift(int shift);
 88   static void set_use_implicit_null_checks(bool use);
 89 
 90   static address  base()                     { return _narrow_oop._base; }
 91   static address  begin()                    { return (address)_heap_address_range.start(); }
 92   static address  end()                      { return (address)_heap_address_range.end(); }
 93   static bool     is_base(void* addr)        { return (base() == (address)addr); }
 94   static int      shift()                    { return _narrow_oop._shift; }
 95   static bool     use_implicit_null_checks() { return _narrow_oop._use_implicit_null_checks; }
 96 
 97   static address  ptrs_base_addr()           { return (address)&_narrow_oop._base; }
 98   static address  ptrs_base()                { return _narrow_oop._base; }
 99 
100   static bool is_in(void* addr);
101   static bool is_in(MemRegion mr);
102 
103   static Mode mode();
104   static const char* mode_to_string(Mode mode);
105 
106   // Test whether bits of addr and possible offsets into the heap overlap.
107   static bool     is_disjoint_heap_base_address(address addr);
108 
109   // Check for disjoint base compressed oops.
110   static bool     base_disjoint();
111 
112   // Check for real heapbased compressed oops.
113   // We must subtract the base as the bits overlap.
114   // If we negate above function, we also get unscaled and zerobased.
115   static bool     base_overlaps();
116 
117   static void     print_mode(outputStream* st);
118 
119   static bool is_null(oop v)       { return v == nullptr; }
120   static bool is_null(narrowOop v) { return v == narrowOop::null; }
121 
122   static inline oop decode_raw_not_null(narrowOop v);
123   static inline oop decode_raw(narrowOop v);
124   static inline oop decode_not_null(narrowOop v);
125   static inline oop decode(narrowOop v);
126   static inline narrowOop encode_not_null(oop v);
127   static inline narrowOop encode(oop v);
128 
129   // No conversions needed for these overloads
130   static inline oop decode_raw_not_null(oop v);
131   static inline oop decode_not_null(oop v);
132   static inline oop decode(oop v);
133   static inline narrowOop encode_not_null(narrowOop v);
134   static inline narrowOop encode(narrowOop v);
135 
136   static inline uint32_t narrow_oop_value(oop o);
137   static inline uint32_t narrow_oop_value(narrowOop o);
138 
139   template<typename T>
140   static inline narrowOop narrow_oop_cast(T i);
141 };
142 
143 // For UseCompressedClassPointers.
144 class CompressedKlassPointers : public AllStatic {
145   friend class VMStructs;
146 
147   static NarrowPtrStruct _narrow_klass;
148 
149   // Together with base, this defines the address range within which Klass
150   //  structures will be located: [base, base+range). While the maximal
151   //  possible encoding range is 4|32G for shift 0|3, if we know beforehand
152   //  the expected range of Klass* pointers will be smaller, a platform
153   //  could use this info to optimize encoding.
154   static size_t _range;
155 
156   static void set_base(address base);
157   static void set_range(size_t range);
158 
159 public:
160 
161   static void set_shift(int shift);
162 
163 
164   // Given an address p, return true if p can be used as an encoding base.
165   //  (Some platforms have restrictions of what constitutes a valid base
166   //   address).
167   static bool is_valid_base(address p);
168 
169   // Given an address range [addr, addr+len) which the encoding is supposed to
170   //  cover, choose base, shift and range.
171   //  The address range is the expected range of uncompressed Klass pointers we
172   //  will encounter (and the implicit promise that there will be no Klass
173   //  structures outside this range).
174   static void initialize(address addr, size_t len);
175 
176   static void     print_mode(outputStream* st);
177 
178   static address  base()               { return  _narrow_klass._base; }
179   static size_t   range()              { return  _range; }
180   static int      shift()              { return  _narrow_klass._shift; }
181 
182   static bool is_null(Klass* v)      { return v == nullptr; }
183   static bool is_null(narrowKlass v) { return v == 0; }
184 
185   static inline Klass* decode_raw(narrowKlass v, address base);
186   static inline Klass* decode_raw(narrowKlass v);
187   static inline Klass* decode_not_null(narrowKlass v);
188   static inline Klass* decode_not_null(narrowKlass v, address base);
189   static inline Klass* decode(narrowKlass v);
190   static inline narrowKlass encode_not_null(Klass* v);
191   static inline narrowKlass encode_not_null(Klass* v, address base);
192   static inline narrowKlass encode(Klass* v);
193 
194 };
195 
196 #endif // SHARE_OOPS_COMPRESSEDOOPS_HPP