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/memnode.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 
 33 // This means the access is mismatched. This means the value of an access
 34 // is not equivalent to the value pointed to by the address.
 35 const DecoratorSet C2_MISMATCHED             = DECORATOR_LAST << 1;
 36 // The access may not be aligned to its natural size.
 37 const DecoratorSet C2_UNALIGNED              = DECORATOR_LAST << 2;
 38 // The atomic cmpxchg is weak, meaning that spurious false negatives are allowed,
 39 // but never false positives.
 40 const DecoratorSet C2_WEAK_CMPXCHG           = DECORATOR_LAST << 3;
 41 // This denotes that a load has control dependency.
 42 const DecoratorSet C2_CONTROL_DEPENDENT_LOAD = DECORATOR_LAST << 4;
 43 // This denotes that a load that must be pinned.
 44 const DecoratorSet C2_PINNED_LOAD            = DECORATOR_LAST << 5;
 45 // This denotes that the access is produced from the sun.misc.Unsafe intrinsics.
 46 const DecoratorSet C2_UNSAFE_ACCESS          = DECORATOR_LAST << 6;
 47 // This denotes that the access mutates state.
 48 const DecoratorSet C2_WRITE_ACCESS           = DECORATOR_LAST << 7;
 49 // This denotes that the access reads state.
 50 const DecoratorSet C2_READ_ACCESS            = DECORATOR_LAST << 8;
 51 
 52 class GraphKit;
 53 class IdealKit;
 54 class Node;
 55 class Type;
 56 class TypePtr;
 57 class PhaseMacroExpand;
 58 
 59 // This class wraps a node and a type.
 60 class C2AccessValue: public StackObj {
 61 protected:
 62   Node* _node;
 63   const Type* _type;
 64 
 65 public:
 66   C2AccessValue(Node* node, const Type* type) :
 67     _node(node),
 68     _type(type) {}
 69 
 70   Node* node() const        { return _node; }
 71   const Type* type() const  { return _type; }
 72 
 73   void set_node(Node* node) { _node = node; }
 74 };
 75 
 76 // This class wraps a node and a pointer type.
 77 class C2AccessValuePtr: public C2AccessValue {
 78   int _alias_idx;
 79 
 80 public:
 81   C2AccessValuePtr(Node* node, const TypePtr* type) :
 82     C2AccessValue(node, reinterpret_cast<const Type*>(type)) {}
 83 
 84   const TypePtr* type() const { return reinterpret_cast<const TypePtr*>(_type); }
 85   int alias_idx() const       { return _alias_idx; }
 86 };
 87 
 88 // This class wraps a bunch of context parameters thare are passed around in the
 89 // BarrierSetC2 backend hierarchy, for loads and stores, to reduce boiler plate.
 90 class C2Access: public StackObj {
 91 protected:
 92   GraphKit*         _kit;
 93   DecoratorSet      _decorators;
 94   BasicType         _type;
 95   Node*             _base;
 96   C2AccessValuePtr& _addr;
 97   Node*             _raw_access;
 98 
 99   void fixup_decorators();
100   void* barrier_set_state() const;
101 
102 public:
103   C2Access(GraphKit* kit, DecoratorSet decorators,
104            BasicType type, Node* base, C2AccessValuePtr& addr) :
105     _kit(kit),
106     _decorators(decorators),
107     _type(type),
108     _base(base),
109     _addr(addr),
110     _raw_access(NULL)
111   {
112     fixup_decorators();
113   }
114 
115   GraphKit* kit() const           { return _kit; }
116   DecoratorSet decorators() const { return _decorators; }
117   Node* base() const              { return _base; }
118   C2AccessValuePtr& addr() const  { return _addr; }
119   BasicType type() const          { return _type; }
120   bool is_oop() const             { return _type == T_OBJECT || _type == T_ARRAY; }
121   bool is_raw() const             { return (_decorators & AS_RAW) != 0; }
122   Node* raw_access() const        { return _raw_access; }
123 
124   void set_raw_access(Node* raw_access) { _raw_access = raw_access; }
125   virtual void set_memory() {} // no-op for normal accesses, but not for atomic accesses.
126 
127   MemNode::MemOrd mem_node_mo() const;
128   bool needs_cpu_membar() const;
129 
130   template <typename T>
131   T barrier_set_state_as() const {
132     return reinterpret_cast<T>(barrier_set_state());
133   }
134 };
135 
136 // This class wraps a bunch of context parameters thare are passed around in the
137 // BarrierSetC2 backend hierarchy, for atomic accesses, to reduce boiler plate.
138 class C2AtomicAccess: public C2Access {
139   Node* _memory;
140   uint  _alias_idx;
141   bool  _needs_pinning;
142 
143 public:
144   C2AtomicAccess(GraphKit* kit, DecoratorSet decorators, BasicType type,
145                  Node* base, C2AccessValuePtr& addr, uint alias_idx) :
146     C2Access(kit, decorators, type, base, addr),
147     _memory(NULL),
148     _alias_idx(alias_idx),
149     _needs_pinning(true) {}
150 
151   // Set the memory node based on the current memory slice.
152   virtual void set_memory();
153 
154   Node* memory() const       { return _memory; }
155   uint alias_idx() const     { return _alias_idx; }
156   bool needs_pinning() const { return _needs_pinning; }
157 
158   void set_needs_pinning(bool value)    { _needs_pinning = value; }
159 };
160 
161 // This is the top-level class for the backend of the Access API in C2.
162 // The top-level class is responsible for performing raw accesses. The
163 // various GC barrier sets inherit from the BarrierSetC2 class to sprinkle
164 // barriers into the accesses.
165 class BarrierSetC2: public CHeapObj<mtGC> {
166 protected:
167   virtual void resolve_address(C2Access& access) const;
168   virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
169   virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
170 
171   virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicAccess& access, Node* expected_val,
172                                                Node* new_val, const Type* val_type) const;
173   virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicAccess& access, Node* expected_val,
174                                                 Node* new_val, const Type* value_type) const;
175   virtual Node* atomic_xchg_at_resolved(C2AtomicAccess& access, Node* new_val, const Type* val_type) const;
176   virtual Node* atomic_add_at_resolved(C2AtomicAccess& access, Node* new_val, const Type* val_type) const;
177 
178 public:
179   // This is the entry-point for the backend to perform accesses through the Access API.
180   virtual Node* store_at(C2Access& access, C2AccessValue& val) const;
181   virtual Node* load_at(C2Access& access, const Type* val_type) const;
182 
183   virtual Node* atomic_cmpxchg_val_at(C2AtomicAccess& access, Node* expected_val,
184                                       Node* new_val, const Type* val_type) const;
185   virtual Node* atomic_cmpxchg_bool_at(C2AtomicAccess& access, Node* expected_val,
186                                        Node* new_val, const Type* val_type) const;
187   virtual Node* atomic_xchg_at(C2AtomicAccess& access, Node* new_val, const Type* value_type) const;
188   virtual Node* atomic_add_at(C2AtomicAccess& access, Node* new_val, const Type* value_type) const;
189 
190   virtual void clone(GraphKit* kit, Node* src, Node* dst, Node* size, bool is_array) const;
191 
192   // These are general helper methods used by C2
193   virtual bool is_gc_barrier_node(Node* node) const { return false; }
194   virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const { }
195   virtual bool array_copy_requires_gc_barriers(BasicType type) const { return false; }
196   virtual Node* step_over_gc_barrier(Node* c) const { return c; }
197 
198   virtual void loop_optimize_gc_barrier(Node* node) const { }
199 
200   // Allow barrier sets to have shared state that is preserved across a compilation unit.
201   // This could for example comprise macro nodes to be expanded during macro expansion.
202   virtual void* create_barrier_state() const { return NULL; }
203   // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
204   // expanded later, then now is the time to do so.
205   virtual bool expand_macro_nodes(PhaseMacroExpand* macro) const { return false; }
206 };
207 
208 #endif // SHARE_GC_SHARED_C2_BARRIERSETC2_HPP