1 /*
  2  * Copyright (c) 2015, 2021, Red Hat, Inc. 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_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP
 26 #define SHARE_GC_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP
 27 
 28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "opto/addnode.hpp"
 31 #include "opto/graphKit.hpp"
 32 #include "opto/machnode.hpp"
 33 #include "opto/memnode.hpp"
 34 #include "opto/multnode.hpp"
 35 #include "opto/node.hpp"
 36 
 37 class PhaseGVN;
 38 class MemoryGraphFixer;
 39 
 40 class ShenandoahBarrierC2Support : public AllStatic {
 41 private:
 42 #ifdef ASSERT
 43   enum verify_type {
 44     ShenandoahLoad,
 45     ShenandoahStore,
 46     ShenandoahValue,
 47     ShenandoahOopStore,
 48     ShenandoahNone
 49   };
 50 
 51   static bool verify_helper(Node* in, Node_Stack& phis, VectorSet& visited, verify_type t, bool trace, Unique_Node_List& barriers_used);
 52   static void report_verify_failure(const char* msg, Node* n1 = nullptr, Node* n2 = nullptr);
 53 #endif
 54   static Node* dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase);
 55   static Node* no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase);
 56   static bool is_gc_state_test(Node* iff, int mask);
 57   static bool has_safepoint_between(Node* start, Node* stop, PhaseIdealLoop *phase);
 58   static Node* find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase);
 59   static void follow_barrier_uses(Node* n, Node* ctrl, Unique_Node_List& uses, PhaseIdealLoop* phase);
 60   static void test_null(Node*& ctrl, Node* val, Node*& null_ctrl, PhaseIdealLoop* phase);
 61   static void test_gc_state(Node*& ctrl, Node* raw_mem, Node*& heap_stable_ctrl,
 62                             PhaseIdealLoop* phase, int flags);
 63   static void call_lrb_stub(Node*& ctrl, Node*& val, Node* load_addr,
 64                             DecoratorSet decorators, PhaseIdealLoop* phase);
 65   static void test_in_cset(Node*& ctrl, Node*& not_cset_ctrl, Node* val, Node* raw_mem, PhaseIdealLoop* phase);
 66   static void fix_ctrl(Node* barrier, Node* region, const MemoryGraphFixer& fixer, Unique_Node_List& uses, Unique_Node_List& uses_to_ignore, uint last, PhaseIdealLoop* phase);
 67 
 68   static Node* get_load_addr(PhaseIdealLoop* phase, VectorSet& visited, Node* lrb);
 69 public:
 70   static bool is_dominator(Node* d_c, Node* n_c, Node* d, Node* n, PhaseIdealLoop* phase);
 71   static bool is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase);
 72 
 73   static bool is_gc_state_load(Node* n);
 74   static bool is_heap_stable_test(Node* iff);
 75 
 76   static bool expand(Compile* C, PhaseIterGVN& igvn);
 77   static void pin_and_expand(PhaseIdealLoop* phase);
 78 
 79 #ifdef ASSERT
 80   static void verify(RootNode* root);
 81 #endif
 82 };
 83 
 84 class MemoryGraphFixer : public ResourceObj {
 85 private:
 86   Node_List _memory_nodes;
 87   int _alias;
 88   PhaseIdealLoop* _phase;
 89   bool _include_lsm;
 90 
 91   void collect_memory_nodes();
 92   Node* get_ctrl(Node* n) const;
 93   Node* ctrl_or_self(Node* n) const;
 94   bool mem_is_valid(Node* m, Node* c) const;
 95   MergeMemNode* allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const;
 96   MergeMemNode* clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const;
 97   void fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const;
 98   bool should_process_phi(Node* phi) const;
 99   bool has_mem_phi(Node* region) const;
100 
101 public:
102   MemoryGraphFixer(int alias, bool include_lsm, PhaseIdealLoop* phase) :
103     _alias(alias), _phase(phase), _include_lsm(include_lsm) {
104     assert(_alias != Compile::AliasIdxBot, "unsupported");
105     collect_memory_nodes();
106   }
107 
108   Node* find_mem(Node* ctrl, Node* n) const;
109   void fix_mem(Node* ctrl, Node* region, Node* mem, Node* mem_for_ctrl, Node* mem_phi, Unique_Node_List& uses);
110   int alias() const { return _alias; }
111 
112   Node* collect_memory_for_infinite_loop(const Node* in);
113 
114   void record_new_ctrl(Node* ctrl, Node* region, Node* mem, Node* mem_for_ctrl);
115 };
116 
117 class ShenandoahCompareAndSwapPNode : public CompareAndSwapPNode {
118 public:
119   ShenandoahCompareAndSwapPNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
120     : CompareAndSwapPNode(c, mem, adr, val, ex, mem_ord) { }
121 
122   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
123     if (in(ExpectedIn) != nullptr && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
124       return new CompareAndSwapPNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
125     }
126     return nullptr;
127   }
128 
129   virtual int Opcode() const;
130 };
131 
132 class ShenandoahCompareAndSwapNNode : public CompareAndSwapNNode {
133 public:
134   ShenandoahCompareAndSwapNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
135     : CompareAndSwapNNode(c, mem, adr, val, ex, mem_ord) { }
136 
137   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
138     if (in(ExpectedIn) != nullptr && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
139       return new CompareAndSwapNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
140     }
141     return nullptr;
142   }
143 
144   virtual int Opcode() const;
145 };
146 
147 class ShenandoahWeakCompareAndSwapPNode : public WeakCompareAndSwapPNode {
148 public:
149   ShenandoahWeakCompareAndSwapPNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
150     : WeakCompareAndSwapPNode(c, mem, adr, val, ex, mem_ord) { }
151 
152   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
153     if (in(ExpectedIn) != nullptr && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
154       return new WeakCompareAndSwapPNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
155     }
156     return nullptr;
157   }
158 
159   virtual int Opcode() const;
160 };
161 
162 class ShenandoahWeakCompareAndSwapNNode : public WeakCompareAndSwapNNode {
163 public:
164   ShenandoahWeakCompareAndSwapNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
165     : WeakCompareAndSwapNNode(c, mem, adr, val, ex, mem_ord) { }
166 
167   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
168     if (in(ExpectedIn) != nullptr && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
169       return new WeakCompareAndSwapNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
170     }
171     return nullptr;
172   }
173 
174   virtual int Opcode() const;
175 };
176 
177 class ShenandoahCompareAndExchangePNode : public CompareAndExchangePNode {
178 public:
179   ShenandoahCompareAndExchangePNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord)
180     : CompareAndExchangePNode(c, mem, adr, val, ex, at, t, mem_ord) { }
181 
182   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
183     if (in(ExpectedIn) != nullptr && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
184       return new CompareAndExchangePNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), adr_type(), bottom_type(), order());
185     }
186     return nullptr;
187   }
188 
189   virtual int Opcode() const;
190 };
191 
192 class ShenandoahCompareAndExchangeNNode : public CompareAndExchangeNNode {
193 public:
194   ShenandoahCompareAndExchangeNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord)
195     : CompareAndExchangeNNode(c, mem, adr, val, ex, at, t, mem_ord) { }
196 
197   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
198     if (in(ExpectedIn) != nullptr && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
199       return new CompareAndExchangeNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), adr_type(), bottom_type(), order());
200     }
201     return nullptr;
202   }
203 
204   virtual int Opcode() const;
205 };
206 
207 class ShenandoahLoadReferenceBarrierNode : public Node {
208 public:
209   enum {
210     Control,
211     ValueIn
212   };
213 
214 private:
215   DecoratorSet _decorators;
216 
217 public:
218   ShenandoahLoadReferenceBarrierNode(Node* ctrl, Node* val, DecoratorSet decorators);
219 
220   DecoratorSet decorators() const;
221   virtual int Opcode() const;
222   virtual const Type* bottom_type() const;
223   virtual const Type* Value(PhaseGVN* phase) const;
224   virtual const class TypePtr *adr_type() const { return TypeOopPtr::BOTTOM; }
225   virtual uint match_edge(uint idx) const {
226     return idx >= ValueIn;
227   }
228   virtual uint ideal_reg() const { return Op_RegP; }
229 
230   virtual Node* Identity(PhaseGVN* phase);
231 
232   virtual uint size_of() const;
233   virtual uint hash() const;
234   virtual bool cmp( const Node &n ) const;
235 
236 private:
237   bool needs_barrier(PhaseGVN* phase, Node* n);
238   bool needs_barrier_impl(PhaseGVN* phase, Node* n, Unique_Node_List &visited);
239 };
240 
241 
242 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP