1 /*
   2  * Copyright (c) 2018, 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_ACCESSDECORATORS_HPP
  26 #define SHARE_OOPS_ACCESSDECORATORS_HPP
  27 
  28 #include "gc/shared/barrierSetConfig.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "metaprogramming/integralConstant.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 // A decorator is an attribute or property that affects the way a memory access is performed in some way.
  34 // There are different groups of decorators. Some have to do with memory ordering, others to do with,
  35 // e.g. strength of references, strength of GC barriers, or whether compression should be applied or not.
  36 // Some decorators are set at buildtime, such as whether primitives require GC barriers or not, others
  37 // at callsites such as whether an access is in the heap or not, and others are resolved at runtime
  38 // such as GC-specific barriers and encoding/decoding compressed oops.
  39 typedef uint64_t DecoratorSet;
  40 
  41 // The HasDecorator trait can help at compile-time determining whether a decorator set
  42 // has an intersection with a certain other decorator set
  43 template <DecoratorSet decorators, DecoratorSet decorator>
  44 struct HasDecorator: public IntegralConstant<bool, (decorators & decorator) != 0> {};
  45 
  46 // == Internal Decorators - do not use ==
  47 // * INTERNAL_EMPTY: This is the name for the empty decorator set (in absence of other decorators).
  48 // * INTERNAL_CONVERT_COMPRESSED_OOPS: This is an oop access that will require converting an oop
  49 //   to a narrowOop or vice versa, if UseCompressedOops is known to be set.
  50 // * INTERNAL_VALUE_IS_OOP: Remember that the involved access is on oop rather than primitive.
  51 const DecoratorSet INTERNAL_EMPTY                    = UCONST64(0);
  52 const DecoratorSet INTERNAL_CONVERT_COMPRESSED_OOP   = UCONST64(1) << 1;
  53 const DecoratorSet INTERNAL_VALUE_IS_OOP             = UCONST64(1) << 2;
  54 
  55 // == Internal build-time Decorators ==
  56 // * INTERNAL_BT_BARRIER_ON_PRIMITIVES: This is set in the barrierSetConfig.hpp file.
  57 // * INTERNAL_BT_TO_SPACE_INVARIANT: This is set in the barrierSetConfig.hpp file iff
  58 //   no GC is bundled in the build that is to-space invariant.
  59 const DecoratorSet INTERNAL_BT_BARRIER_ON_PRIMITIVES = UCONST64(1) << 3;
  60 const DecoratorSet INTERNAL_BT_TO_SPACE_INVARIANT    = UCONST64(1) << 4;
  61 
  62 // == Internal run-time Decorators ==
  63 // * INTERNAL_RT_USE_COMPRESSED_OOPS: This decorator will be set in runtime resolved
  64 //   access backends iff UseCompressedOops is true.
  65 const DecoratorSet INTERNAL_RT_USE_COMPRESSED_OOPS   = UCONST64(1) << 5;
  66 
  67 const DecoratorSet INTERNAL_DECORATOR_MASK           = INTERNAL_CONVERT_COMPRESSED_OOP | INTERNAL_VALUE_IS_OOP |
  68                                                        INTERNAL_BT_BARRIER_ON_PRIMITIVES | INTERNAL_RT_USE_COMPRESSED_OOPS;
  69 
  70 // == Memory Ordering Decorators ==
  71 // The memory ordering decorators can be described in the following way:
  72 // === Decorator Rules ===
  73 // The different types of memory ordering guarantees have a strict order of strength.
  74 // Explicitly specifying the stronger ordering implies that the guarantees of the weaker
  75 // property holds too. The names come from the C++11 atomic operations, and typically
  76 // have a JMM equivalent property.
  77 // The equivalence may be viewed like this:
  78 // MO_UNORDERED is equivalent to JMM plain.
  79 // MO_VOLATILE has no equivalence in JMM, because it's a C++ thing.
  80 // MO_RELAXED is equivalent to JMM opaque.
  81 // MO_ACQUIRE is equivalent to JMM acquire.
  82 // MO_RELEASE is equivalent to JMM release.
  83 // MO_SEQ_CST is equivalent to JMM volatile.
  84 //
  85 // === Stores ===
  86 //  * MO_UNORDERED (Default): No guarantees.
  87 //    - The compiler and hardware are free to reorder aggressively. And they will.
  88 //  * MO_VOLATILE: Volatile stores (in the C++ sense).
  89 //    - The stores are not reordered by the compiler (but possibly the HW) w.r.t. other
  90 //      volatile accesses in program order (but possibly non-volatile accesses).
  91 //  * MO_RELAXED: Relaxed atomic stores.
  92 //    - The stores are atomic.
  93 //    - Guarantees from volatile stores hold.
  94 //  * MO_RELEASE: Releasing stores.
  95 //    - The releasing store will make its preceding memory accesses observable to memory accesses
  96 //      subsequent to an acquiring load observing this releasing store.
  97 //    - Guarantees from relaxed stores hold.
  98 //  * MO_SEQ_CST: Sequentially consistent stores.
  99 //    - The stores are observed in the same order by MO_SEQ_CST loads on other processors
 100 //    - Preceding loads and stores in program order are not reordered with subsequent loads and stores in program order.
 101 //    - Guarantees from releasing stores hold.
 102 // === Loads ===
 103 //  * MO_UNORDERED (Default): No guarantees
 104 //    - The compiler and hardware are free to reorder aggressively. And they will.
 105 //  * MO_VOLATILE: Volatile loads (in the C++ sense).
 106 //    - The loads are not reordered by the compiler (but possibly the HW) w.r.t. other
 107 //      volatile accesses in program order (but possibly non-volatile accesses).
 108 //  * MO_RELAXED: Relaxed atomic loads.
 109 //    - The loads are atomic.
 110 //    - Guarantees from volatile loads hold.
 111 //  * MO_ACQUIRE: Acquiring loads.
 112 //    - An acquiring load will make subsequent memory accesses observe the memory accesses
 113 //      preceding the releasing store that the acquiring load observed.
 114 //    - Guarantees from relaxed loads hold.
 115 //  * MO_SEQ_CST: Sequentially consistent loads.
 116 //    - These loads observe MO_SEQ_CST stores in the same order on other processors
 117 //    - Preceding loads and stores in program order are not reordered with subsequent loads and stores in program order.
 118 //    - Guarantees from acquiring loads hold.
 119 // === Atomic Cmpxchg ===
 120 //  * MO_RELAXED: Atomic but relaxed cmpxchg.
 121 //    - Guarantees from MO_RELAXED loads and MO_RELAXED stores hold unconditionally.
 122 //  * MO_SEQ_CST: Sequentially consistent cmpxchg.
 123 //    - Guarantees from MO_SEQ_CST loads and MO_SEQ_CST stores hold unconditionally.
 124 // === Atomic Xchg ===
 125 //  * MO_RELAXED: Atomic but relaxed atomic xchg.
 126 //    - Guarantees from MO_RELAXED loads and MO_RELAXED stores hold.
 127 //  * MO_SEQ_CST: Sequentially consistent xchg.
 128 //    - Guarantees from MO_SEQ_CST loads and MO_SEQ_CST stores hold.
 129 const DecoratorSet MO_UNORDERED      = UCONST64(1) << 6;
 130 const DecoratorSet MO_VOLATILE       = UCONST64(1) << 7;
 131 const DecoratorSet MO_RELAXED        = UCONST64(1) << 8;
 132 const DecoratorSet MO_ACQUIRE        = UCONST64(1) << 9;
 133 const DecoratorSet MO_RELEASE        = UCONST64(1) << 10;
 134 const DecoratorSet MO_SEQ_CST        = UCONST64(1) << 11;
 135 const DecoratorSet MO_DECORATOR_MASK = MO_UNORDERED | MO_VOLATILE | MO_RELAXED |
 136                                        MO_ACQUIRE | MO_RELEASE | MO_SEQ_CST;
 137 
 138 // === Barrier Strength Decorators ===
 139 // * AS_RAW: The access will translate into a raw memory access, hence ignoring all semantic concerns
 140 //   except memory ordering and compressed oops. This will bypass runtime function pointer dispatching
 141 //   in the pipeline and hardwire to raw accesses without going trough the GC access barriers.
 142 //  - Accesses on oop* translate to raw memory accesses without runtime checks
 143 //  - Accesses on narrowOop* translate to encoded/decoded memory accesses without runtime checks
 144 //  - Accesses on HeapWord* translate to a runtime check choosing one of the above
 145 //  - Accesses on other types translate to raw memory accesses without runtime checks
 146 // * AS_NO_KEEPALIVE: The barrier is used only on oop references and will not keep any involved objects
 147 //   alive, regardless of the type of reference being accessed. It will however perform the memory access
 148 //   in a consistent way w.r.t. e.g. concurrent compaction, so that the right field is being accessed,
 149 //   or maintain, e.g. intergenerational or interregional pointers if applicable. This should be used with
 150 //   extreme caution in isolated scopes.
 151 // * AS_NORMAL: The accesses will be resolved to an accessor on the BarrierSet class, giving the
 152 //   responsibility of performing the access and what barriers to be performed to the GC. This is the default.
 153 //   Note that primitive accesses will only be resolved on the barrier set if the appropriate build-time
 154 //   decorator for enabling primitive barriers is enabled for the build.
 155 const DecoratorSet AS_RAW                  = UCONST64(1) << 12;
 156 const DecoratorSet AS_NO_KEEPALIVE         = UCONST64(1) << 13;
 157 const DecoratorSet AS_NORMAL               = UCONST64(1) << 14;
 158 const DecoratorSet AS_DECORATOR_MASK       = AS_RAW | AS_NO_KEEPALIVE | AS_NORMAL;
 159 
 160 // === Reference Strength Decorators ===
 161 // These decorators only apply to accesses on oop-like types (oop/narrowOop).
 162 // * ON_STRONG_OOP_REF: Memory access is performed on a strongly reachable reference.
 163 // * ON_WEAK_OOP_REF: The memory access is performed on a weakly reachable reference.
 164 // * ON_PHANTOM_OOP_REF: The memory access is performed on a phantomly reachable reference.
 165 //   This is the same ring of strength as jweak and weak oops in the VM.
 166 // * ON_UNKNOWN_OOP_REF: The memory access is performed on a reference of unknown strength.
 167 //   This could for example come from the unsafe API.
 168 // * Default (no explicit reference strength specified): ON_STRONG_OOP_REF
 169 const DecoratorSet ON_STRONG_OOP_REF  = UCONST64(1) << 15;
 170 const DecoratorSet ON_WEAK_OOP_REF    = UCONST64(1) << 16;
 171 const DecoratorSet ON_PHANTOM_OOP_REF = UCONST64(1) << 17;
 172 const DecoratorSet ON_UNKNOWN_OOP_REF = UCONST64(1) << 18;
 173 const DecoratorSet ON_DECORATOR_MASK  = ON_STRONG_OOP_REF | ON_WEAK_OOP_REF |
 174                                         ON_PHANTOM_OOP_REF | ON_UNKNOWN_OOP_REF;
 175 
 176 // === Access Location ===
 177 // Accesses can take place in, e.g. the heap, old or young generation and different native roots.
 178 // The location is important to the GC as it may imply different actions. The following decorators are used:
 179 // * IN_HEAP: The access is performed in the heap. Many barriers such as card marking will
 180 //   be omitted if this decorator is not set.
 181 // * IN_NATIVE: The access is performed in an off-heap data structure pointing into the Java heap.
 182 const DecoratorSet IN_HEAP            = UCONST64(1) << 19;
 183 const DecoratorSet IN_NATIVE          = UCONST64(1) << 20;
 184 const DecoratorSet IN_DECORATOR_MASK  = IN_HEAP | IN_NATIVE;
 185 
 186 // == Boolean Flag Decorators ==
 187 // * IS_ARRAY: The access is performed on a heap allocated array. This is sometimes a special case
 188 //   for some GCs.
 189 // * IS_DEST_UNINITIALIZED: This property can be important to e.g. SATB barriers by
 190 //   marking that the previous value is uninitialized nonsense rather than a real value.
 191 // * IS_NOT_NULL: This property can make certain barriers faster such as compressing oops.
 192 const DecoratorSet IS_ARRAY              = UCONST64(1) << 21;
 193 const DecoratorSet IS_DEST_UNINITIALIZED = UCONST64(1) << 22;
 194 const DecoratorSet IS_NOT_NULL           = UCONST64(1) << 23;
 195 
 196 // == Arraycopy Decorators ==
 197 // * ARRAYCOPY_CHECKCAST: This property means that the class of the objects in source
 198 //   are not guaranteed to be subclasses of the class of the destination array. This requires
 199 //   a check-cast barrier during the copying operation. If this is not set, it is assumed
 200 //   that the array is covariant: (the source array type is-a destination array type)
 201 // * ARRAYCOPY_DISJOINT: This property means that it is known that the two array ranges
 202 //   are disjoint.
 203 // * ARRAYCOPY_ARRAYOF: The copy is in the arrayof form.
 204 // * ARRAYCOPY_ATOMIC: The accesses have to be atomic over the size of its elements.
 205 // * ARRAYCOPY_ALIGNED: The accesses have to be aligned on a HeapWord.
 206 const DecoratorSet ARRAYCOPY_CHECKCAST            = UCONST64(1) << 24;
 207 const DecoratorSet ARRAYCOPY_DISJOINT             = UCONST64(1) << 25;
 208 const DecoratorSet ARRAYCOPY_ARRAYOF              = UCONST64(1) << 26;
 209 const DecoratorSet ARRAYCOPY_ATOMIC               = UCONST64(1) << 27;
 210 const DecoratorSet ARRAYCOPY_ALIGNED              = UCONST64(1) << 28;
 211 const DecoratorSet ARRAYCOPY_DECORATOR_MASK       = ARRAYCOPY_CHECKCAST | ARRAYCOPY_DISJOINT |
 212                                                     ARRAYCOPY_DISJOINT | ARRAYCOPY_ARRAYOF |
 213                                                     ARRAYCOPY_ATOMIC | ARRAYCOPY_ALIGNED;
 214 
 215 // Keep track of the last decorator.
 216 const DecoratorSet DECORATOR_LAST = UCONST64(1) << 28;
 217 
 218 namespace AccessInternal {
 219   // This class adds implied decorators that follow according to decorator rules.
 220   // For example adding default reference strength and default memory ordering
 221   // semantics.
 222   template <DecoratorSet input_decorators>
 223   struct DecoratorFixup: AllStatic {
 224     // If no reference strength has been picked, then strong will be picked
 225     static const DecoratorSet ref_strength_default = input_decorators |
 226       (((ON_DECORATOR_MASK & input_decorators) == 0 && (INTERNAL_VALUE_IS_OOP & input_decorators) != 0) ?
 227        ON_STRONG_OOP_REF : INTERNAL_EMPTY);
 228     // If no memory ordering has been picked, unordered will be picked
 229     static const DecoratorSet memory_ordering_default = ref_strength_default |
 230       ((MO_DECORATOR_MASK & ref_strength_default) == 0 ? MO_UNORDERED : INTERNAL_EMPTY);
 231     // If no barrier strength has been picked, normal will be used
 232     static const DecoratorSet barrier_strength_default = memory_ordering_default |
 233       ((AS_DECORATOR_MASK & memory_ordering_default) == 0 ? AS_NORMAL : INTERNAL_EMPTY);
 234     static const DecoratorSet value = barrier_strength_default | BT_BUILDTIME_DECORATORS;
 235   };
 236 
 237   // This function implements the above DecoratorFixup rules, but without meta
 238   // programming for code generation that does not use templates.
 239   inline DecoratorSet decorator_fixup(DecoratorSet input_decorators) {
 240     // If no reference strength has been picked, then strong will be picked
 241     DecoratorSet ref_strength_default = input_decorators |
 242       (((ON_DECORATOR_MASK & input_decorators) == 0 && (INTERNAL_VALUE_IS_OOP & input_decorators) != 0) ?
 243        ON_STRONG_OOP_REF : INTERNAL_EMPTY);
 244     // If no memory ordering has been picked, unordered will be picked
 245     DecoratorSet memory_ordering_default = ref_strength_default |
 246       ((MO_DECORATOR_MASK & ref_strength_default) == 0 ? MO_UNORDERED : INTERNAL_EMPTY);
 247     // If no barrier strength has been picked, normal will be used
 248     DecoratorSet barrier_strength_default = memory_ordering_default |
 249       ((AS_DECORATOR_MASK & memory_ordering_default) == 0 ? AS_NORMAL : INTERNAL_EMPTY);
 250     DecoratorSet value = barrier_strength_default | BT_BUILDTIME_DECORATORS;
 251     return value;
 252   }
 253 }
 254 
 255 #endif // SHARE_OOPS_ACCESSDECORATORS_HPP