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_GC_SHARED_C2_BARRIERSETC2_HPP
  26 #define SHARE_GC_SHARED_C2_BARRIERSETC2_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "oops/accessDecorators.hpp"
  30 #include "opto/loopnode.hpp"
  31 #include "opto/memnode.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 // This means the access is mismatched. This means the value of an access
  35 // is not equivalent to the value pointed to by the address.
  36 const DecoratorSet C2_MISMATCHED             = DECORATOR_LAST << 1;
  37 // The access may not be aligned to its natural size.
  38 const DecoratorSet C2_UNALIGNED              = DECORATOR_LAST << 2;
  39 // The atomic cmpxchg is weak, meaning that spurious false negatives are allowed,
  40 // but never false positives.
  41 const DecoratorSet C2_WEAK_CMPXCHG           = DECORATOR_LAST << 3;
  42 // This denotes that a load has control dependency.
  43 const DecoratorSet C2_CONTROL_DEPENDENT_LOAD = DECORATOR_LAST << 4;
  44 // This denotes that a load that must be pinned.
  45 const DecoratorSet C2_PINNED_LOAD            = DECORATOR_LAST << 5;
  46 // This denotes that the access is produced from the sun.misc.Unsafe intrinsics.
  47 const DecoratorSet C2_UNSAFE_ACCESS          = DECORATOR_LAST << 6;
  48 // This denotes that the access mutates state.
  49 const DecoratorSet C2_WRITE_ACCESS           = DECORATOR_LAST << 7;
  50 // This denotes that the access reads state.
  51 const DecoratorSet C2_READ_ACCESS            = DECORATOR_LAST << 8;
  52 // A nearby allocation?
  53 const DecoratorSet C2_TIGHLY_COUPLED_ALLOC   = DECORATOR_LAST << 9;
  54 // Loads and stores from an arraycopy being optimized
  55 const DecoratorSet C2_ARRAY_COPY             = DECORATOR_LAST << 10;
  56 
  57 class Compile;
  58 class GraphKit;
  59 class IdealKit;
  60 class Node;
  61 class Type;
  62 class TypePtr;
  63 class PhaseMacroExpand;
  64 
  65 // This class wraps a node and a type.
  66 class C2AccessValue: public StackObj {
  67 protected:
  68   Node* _node;
  69   const Type* _type;
  70 
  71 public:
  72   C2AccessValue(Node* node, const Type* type) :
  73     _node(node),
  74     _type(type) {}
  75 
  76   Node* node() const        { return _node; }
  77   const Type* type() const  { return _type; }
  78 
  79   void set_node(Node* node) { _node = node; }
  80 };
  81 
  82 // This class wraps a node and a pointer type.
  83 class C2AccessValuePtr: public C2AccessValue {
  84 
  85 public:
  86   C2AccessValuePtr(Node* node, const TypePtr* type) :
  87     C2AccessValue(node, reinterpret_cast<const Type*>(type)) {}
  88 
  89   const TypePtr* type() const { return reinterpret_cast<const TypePtr*>(_type); }
  90 };
  91 
  92 // This class wraps a bunch of context parameters thare are passed around in the
  93 // BarrierSetC2 backend hierarchy, for loads and stores, to reduce boiler plate.
  94 class C2Access: public StackObj {
  95 protected:
  96   DecoratorSet      _decorators;
  97   BasicType         _type;
  98   Node*             _base;
  99   C2AccessValuePtr& _addr;
 100   Node*             _raw_access;
 101 
 102   void fixup_decorators();
 103 
 104 public:
 105   C2Access(DecoratorSet decorators,
 106            BasicType type, Node* base, C2AccessValuePtr& addr) :
 107     _decorators(decorators),
 108     _type(type),
 109     _base(base),
 110     _addr(addr),
 111     _raw_access(NULL)
 112   {}
 113 
 114   DecoratorSet decorators() const { return _decorators; }
 115   Node* base() const              { return _base; }
 116   C2AccessValuePtr& addr() const  { return _addr; }
 117   BasicType type() const          { return _type; }
 118   bool is_oop() const             { return _type == T_OBJECT || _type == T_ARRAY; }
 119   bool is_raw() const             { return (_decorators & AS_RAW) != 0; }
 120   Node* raw_access() const        { return _raw_access; }
 121 
 122   void set_raw_access(Node* raw_access) { _raw_access = raw_access; }
 123   virtual void set_memory() {} // no-op for normal accesses, but not for atomic accesses.
 124 
 125   MemNode::MemOrd mem_node_mo() const;
 126   bool needs_cpu_membar() const;
 127 
 128   virtual PhaseGVN& gvn() const = 0;
 129   virtual bool is_parse_access() const { return false; }
 130   virtual bool is_opt_access() const { return false; }
 131 };
 132 
 133 // C2Access for parse time calls to the BarrierSetC2 backend.
 134 class C2ParseAccess: public C2Access {
 135 protected:
 136   GraphKit*         _kit;
 137 
 138   void* barrier_set_state() const;
 139 
 140 public:
 141   C2ParseAccess(GraphKit* kit, DecoratorSet decorators,
 142                 BasicType type, Node* base, C2AccessValuePtr& addr) :
 143     C2Access(decorators, type, base, addr),
 144     _kit(kit) {
 145     fixup_decorators();
 146   }
 147 
 148   GraphKit* kit() const           { return _kit; }
 149 
 150   template <typename T>
 151   T barrier_set_state_as() const {
 152     return reinterpret_cast<T>(barrier_set_state());
 153   }
 154 
 155   virtual PhaseGVN& gvn() const;
 156   virtual bool is_parse_access() const { return true; }
 157 };
 158 
 159 // This class wraps a bunch of context parameters thare are passed around in the
 160 // BarrierSetC2 backend hierarchy, for atomic accesses, to reduce boiler plate.
 161 class C2AtomicParseAccess: public C2ParseAccess {
 162   Node* _memory;
 163   uint  _alias_idx;
 164   bool  _needs_pinning;
 165 
 166 public:
 167   C2AtomicParseAccess(GraphKit* kit, DecoratorSet decorators, BasicType type,
 168                  Node* base, C2AccessValuePtr& addr, uint alias_idx) :
 169     C2ParseAccess(kit, decorators, type, base, addr),
 170     _memory(NULL),
 171     _alias_idx(alias_idx),
 172     _needs_pinning(true) {}
 173 
 174   // Set the memory node based on the current memory slice.
 175   virtual void set_memory();
 176 
 177   Node* memory() const       { return _memory; }
 178   uint alias_idx() const     { return _alias_idx; }
 179   bool needs_pinning() const { return _needs_pinning; }
 180 
 181   void set_needs_pinning(bool value)    { _needs_pinning = value; }
 182 };
 183 
 184 // C2Access for optimization time calls to the BarrierSetC2 backend.
 185 class C2OptAccess: public C2Access {
 186   PhaseGVN& _gvn;
 187   MergeMemNode* _mem;
 188   Node* _ctl;
 189 
 190 public:
 191   C2OptAccess(PhaseGVN& gvn, Node* ctl, MergeMemNode* mem, DecoratorSet decorators,
 192               BasicType type, Node* base, C2AccessValuePtr& addr) :
 193     C2Access(decorators, type, base, addr),
 194     _gvn(gvn), _mem(mem), _ctl(ctl) {
 195     fixup_decorators();
 196   }
 197 
 198 
 199   MergeMemNode* mem() const { return _mem; }
 200   Node* ctl() const { return _ctl; }
 201   // void set_mem(Node* mem) { _mem = mem; }
 202   void set_ctl(Node* ctl) { _ctl = ctl; }
 203 
 204   virtual PhaseGVN& gvn() const { return _gvn; }
 205   virtual bool is_opt_access() const { return true; }
 206 };
 207 
 208 
 209 // This is the top-level class for the backend of the Access API in C2.
 210 // The top-level class is responsible for performing raw accesses. The
 211 // various GC barrier sets inherit from the BarrierSetC2 class to sprinkle
 212 // barriers into the accesses.
 213 class BarrierSetC2: public CHeapObj<mtGC> {
 214 protected:
 215   virtual void resolve_address(C2Access& access) const;
 216   virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
 217   virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
 218 
 219   virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
 220                                                Node* new_val, const Type* val_type) const;
 221   virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
 222                                                 Node* new_val, const Type* value_type) const;
 223   virtual Node* atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
 224   virtual Node* atomic_add_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
 225   void pin_atomic_op(C2AtomicParseAccess& access) const;
 226 
 227 public:
 228   // This is the entry-point for the backend to perform accesses through the Access API.
 229   virtual Node* store_at(C2Access& access, C2AccessValue& val) const;
 230   virtual Node* load_at(C2Access& access, const Type* val_type) const;
 231 
 232   virtual Node* atomic_cmpxchg_val_at(C2AtomicParseAccess& access, Node* expected_val,
 233                                       Node* new_val, const Type* val_type) const;
 234   virtual Node* atomic_cmpxchg_bool_at(C2AtomicParseAccess& access, Node* expected_val,
 235                                        Node* new_val, const Type* val_type) const;
 236   virtual Node* atomic_xchg_at(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const;
 237   virtual Node* atomic_add_at(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const;
 238 
 239   virtual void clone(GraphKit* kit, Node* src, Node* dst, Node* size, bool is_array) const;
 240 
 241   virtual Node* resolve(GraphKit* kit, Node* n, DecoratorSet decorators) const { return n; }
 242 
 243   virtual Node* obj_allocate(PhaseMacroExpand* macro, Node* ctrl, Node* mem, Node* toobig_false, Node* size_in_bytes,
 244                              Node*& i_o, Node*& needgc_ctrl,
 245                              Node*& fast_oop_ctrl, Node*& fast_oop_rawmem,
 246                              intx prefetch_lines) const;
 247 
 248   virtual Node* ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const { return NULL; }
 249   virtual Node* identity_node(PhaseGVN* phase, Node* n) const { return n; }
 250 
 251   // These are general helper methods used by C2
 252   enum ArrayCopyPhase {
 253     Parsing,
 254     Optimization,
 255     Expansion
 256   };
 257   virtual bool array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, ArrayCopyPhase phase) const { return false; }
 258   virtual void clone_barrier_at_expansion(ArrayCopyNode* ac, Node* call, PhaseIterGVN& igvn) const;
 259 
 260   // Support for GC barriers emitted during parsing
 261   virtual bool has_load_barriers() const { return false; }
 262   virtual bool is_gc_barrier_node(Node* node) const { return false; }
 263   virtual Node* step_over_gc_barrier(Node* c) const { return c; }
 264 
 265   // Support for macro expanded GC barriers
 266   virtual void register_potential_barrier_node(Node* node) const { }
 267   virtual void unregister_potential_barrier_node(Node* node) const { }
 268   virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const { }
 269   virtual void enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const {}
 270   virtual void eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const {}
 271   virtual void add_users_to_worklist(Unique_Node_List* worklist) const {}
 272 
 273   // Allow barrier sets to have shared state that is preserved across a compilation unit.
 274   // This could for example comprise macro nodes to be expanded during macro expansion.
 275   virtual void* create_barrier_state(Arena* comp_arena) const { return NULL; }
 276   // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
 277   // expanded later, then now is the time to do so.
 278   virtual bool expand_macro_nodes(PhaseMacroExpand* macro) const { return false; }
 279 
 280   enum CompilePhase {
 281     BeforeOptimize, /* post_parse = true */
 282     BeforeExpand, /* post_parse = false */
 283     BeforeCodeGen
 284   };
 285   virtual void verify_gc_barriers(Compile* compile, CompilePhase phase) const {}
 286 
 287   virtual bool flatten_gc_alias_type(const TypePtr*& adr_type) const { return false; }
 288 #ifdef ASSERT
 289   virtual bool verify_gc_alias_type(const TypePtr* adr_type, int offset) const { return false; }
 290 #endif
 291 };
 292 
 293 #endif // SHARE_GC_SHARED_C2_BARRIERSETC2_HPP