1 /*
2  * Copyright (c) 2019, 2021, 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/allocation.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 
base()90   static address  base()                     { return _narrow_oop._base; }
begin()91   static address  begin()                    { return (address)_heap_address_range.start(); }
end()92   static address  end()                      { return (address)_heap_address_range.end(); }
is_base(void * addr)93   static bool     is_base(void* addr)        { return (base() == (address)addr); }
shift()94   static int      shift()                    { return _narrow_oop._shift; }
use_implicit_null_checks()95   static bool     use_implicit_null_checks() { return _narrow_oop._use_implicit_null_checks; }
96 
ptrs_base_addr()97   static address* ptrs_base_addr()           { return &_narrow_oop._base; }
ptrs_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 
is_null(oop v)119   static bool is_null(oop v)       { return v == NULL; }
is_null(narrowOop v)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_not_null(oop v);
131   static inline oop decode(oop v);
132   static inline narrowOop encode_not_null(narrowOop v);
133   static inline narrowOop encode(narrowOop v);
134 
135   static inline uint32_t narrow_oop_value(oop o);
136   static inline uint32_t narrow_oop_value(narrowOop o);
137 
138   template<typename T>
139   static inline narrowOop narrow_oop_cast(T i);
140 };
141 
142 // For UseCompressedClassPointers.
143 class CompressedKlassPointers : public AllStatic {
144   friend class VMStructs;
145 
146   static NarrowPtrStruct _narrow_klass;
147 
148   // Together with base, this defines the address range within which Klass
149   //  structures will be located: [base, base+range). While the maximal
150   //  possible encoding range is 4|32G for shift 0|3, if we know beforehand
151   //  the expected range of Klass* pointers will be smaller, a platform
152   //  could use this info to optimize encoding.
153   static size_t _range;
154 
155   static void set_base(address base);
156   static void set_range(size_t range);
157 
158 public:
159 
160   static void set_shift(int shift);
161 
162 
163   // Given an address p, return true if p can be used as an encoding base.
164   //  (Some platforms have restrictions of what constitutes a valid base
165   //   address).
166   static bool is_valid_base(address p);
167 
168   // Given an address range [addr, addr+len) which the encoding is supposed to
169   //  cover, choose base, shift and range.
170   //  The address range is the expected range of uncompressed Klass pointers we
171   //  will encounter (and the implicit promise that there will be no Klass
172   //  structures outside this range).
173   static void initialize(address addr, size_t len);
174 
175   static void     print_mode(outputStream* st);
176 
base()177   static address  base()               { return  _narrow_klass._base; }
range()178   static size_t   range()              { return  _range; }
shift()179   static int      shift()              { return  _narrow_klass._shift; }
180 
is_null(Klass * v)181   static bool is_null(Klass* v)      { return v == NULL; }
is_null(narrowKlass v)182   static bool is_null(narrowKlass v) { return v == 0; }
183 
184   static inline Klass* decode_raw(narrowKlass v, address base);
185   static inline Klass* decode_raw(narrowKlass v);
186   static inline Klass* decode_not_null(narrowKlass v);
187   static inline Klass* decode_not_null(narrowKlass v, address base);
188   static inline Klass* decode(narrowKlass v);
189   static inline narrowKlass encode_not_null(Klass* v);
190   static inline narrowKlass encode_not_null(Klass* v, address base);
191   static inline narrowKlass encode(Klass* v);
192 
193 };
194 
195 #endif // SHARE_OOPS_COMPRESSEDOOPS_HPP
196