1 /*
   2  * Copyright (c) 2015, 2019, 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 "memory/allocation.hpp"
  29 #include "opto/addnode.hpp"
  30 #include "opto/graphKit.hpp"
  31 #include "opto/machnode.hpp"
  32 #include "opto/memnode.hpp"
  33 #include "opto/multnode.hpp"
  34 #include "opto/node.hpp"
  35 
  36 class PhaseGVN;
  37 class MemoryGraphFixer;
  38 
  39 class ShenandoahBarrierC2Support : public AllStatic {
  40 private:
  41 #ifdef ASSERT
  42   enum verify_type {
  43     ShenandoahLoad,
  44     ShenandoahStore,
  45     ShenandoahValue,
  46     ShenandoahOopStore,
  47     ShenandoahNone
  48   };
  49 
  50   static bool verify_helper(Node* in, Node_Stack& phis, VectorSet& visited, verify_type t, bool trace, Unique_Node_List& barriers_used);
  51   static void report_verify_failure(const char* msg, Node* n1 = NULL, Node* n2 = NULL);
  52   static void verify_raw_mem(RootNode* root);
  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_heap_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_heap_stable(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, Node*& result_mem, Node* raw_mem, bool is_native, PhaseIdealLoop* phase);
  64   static Node* clone_null_check(Node*& c, Node* val, Node* unc_ctrl, PhaseIdealLoop* phase);
  65   static void fix_null_check(Node* unc, Node* unc_ctrl, Node* new_unc_ctrl, Unique_Node_List& uses,
  66                              PhaseIdealLoop* phase);
  67   static void in_cset_fast_test(Node*& ctrl, Node*& not_cset_ctrl, Node* val, Node* raw_mem, PhaseIdealLoop* phase);
  68   static void move_heap_stable_test_out_of_loop(IfNode* iff, PhaseIdealLoop* phase);
  69   static void merge_back_to_back_tests(Node* n, PhaseIdealLoop* phase);
  70   static bool identical_backtoback_ifs(Node *n, PhaseIdealLoop* phase);
  71   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);
  72   static IfNode* find_unswitching_candidate(const IdealLoopTree *loop, PhaseIdealLoop* phase);
  73 
  74   static Node* get_load_addr(PhaseIdealLoop* phase, VectorSet& visited, Node* lrb);
  75 public:
  76   static bool is_dominator(Node* d_c, Node* n_c, Node* d, Node* n, PhaseIdealLoop* phase);
  77   static bool is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase);
  78 
  79   static bool is_gc_state_load(Node* n);
  80   static bool is_heap_stable_test(Node* iff);
  81 
  82   static bool expand(Compile* C, PhaseIterGVN& igvn);
  83   static void pin_and_expand(PhaseIdealLoop* phase);
  84   static void optimize_after_expansion(VectorSet& visited, Node_Stack& nstack, Node_List& old_new, PhaseIdealLoop* phase);
  85 
  86 #ifdef ASSERT
  87   static void verify(RootNode* root);
  88 #endif
  89 };
  90 
  91 class ShenandoahEnqueueBarrierNode : public Node {
  92 public:
  93   ShenandoahEnqueueBarrierNode(Node* val);
  94 
  95   const Type *bottom_type() const;
  96   const Type* Value(PhaseGVN* phase) const;
  97   Node* Identity(PhaseGVN* phase);
  98 
  99   int Opcode() const;
 100   bool can_eliminate(PhaseIdealLoop* phase);
 101   bool is_redundant();
 102 
 103 private:
 104   enum { Needed, NotNeeded, MaybeNeeded };
 105 
 106   static int needed(Node* n);
 107   static Node* next(Node* n);
 108 };
 109 
 110 class MemoryGraphFixer : public ResourceObj {
 111 private:
 112   Node_List _memory_nodes;
 113   int _alias;
 114   PhaseIdealLoop* _phase;
 115   bool _include_lsm;
 116 
 117   void collect_memory_nodes();
 118   Node* get_ctrl(Node* n) const;
 119   Node* ctrl_or_self(Node* n) const;
 120   bool mem_is_valid(Node* m, Node* c) const;
 121   MergeMemNode* allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const;
 122   MergeMemNode* clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const;
 123   void fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const;
 124   bool should_process_phi(Node* phi) const;
 125   bool has_mem_phi(Node* region) const;
 126 
 127 public:
 128   MemoryGraphFixer(int alias, bool include_lsm, PhaseIdealLoop* phase) :
 129     _alias(alias), _phase(phase), _include_lsm(include_lsm) {
 130     assert(_alias != Compile::AliasIdxBot, "unsupported");
 131     collect_memory_nodes();
 132   }
 133 
 134   Node* find_mem(Node* ctrl, Node* n) const;
 135   void fix_mem(Node* ctrl, Node* region, Node* mem, Node* mem_for_ctrl, Node* mem_phi, Unique_Node_List& uses);
 136   int alias() const { return _alias; }
 137 };
 138 
 139 class ShenandoahCompareAndSwapPNode : public CompareAndSwapPNode {
 140 public:
 141   ShenandoahCompareAndSwapPNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 142     : CompareAndSwapPNode(c, mem, adr, val, ex, mem_ord) { }
 143 
 144   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 145     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
 146       return new CompareAndSwapPNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 147     }
 148     return NULL;
 149   }
 150 
 151   virtual int Opcode() const;
 152 };
 153 
 154 class ShenandoahCompareAndSwapNNode : public CompareAndSwapNNode {
 155 public:
 156   ShenandoahCompareAndSwapNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 157     : CompareAndSwapNNode(c, mem, adr, val, ex, mem_ord) { }
 158 
 159   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 160     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
 161       return new CompareAndSwapNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 162     }
 163     return NULL;
 164   }
 165 
 166   virtual int Opcode() const;
 167 };
 168 
 169 class ShenandoahWeakCompareAndSwapPNode : public WeakCompareAndSwapPNode {
 170 public:
 171   ShenandoahWeakCompareAndSwapPNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 172     : WeakCompareAndSwapPNode(c, mem, adr, val, ex, mem_ord) { }
 173 
 174   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 175     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
 176       return new WeakCompareAndSwapPNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 177     }
 178     return NULL;
 179   }
 180 
 181   virtual int Opcode() const;
 182 };
 183 
 184 class ShenandoahWeakCompareAndSwapNNode : public WeakCompareAndSwapNNode {
 185 public:
 186   ShenandoahWeakCompareAndSwapNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 187     : WeakCompareAndSwapNNode(c, mem, adr, val, ex, mem_ord) { }
 188 
 189   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 190     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
 191       return new WeakCompareAndSwapNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 192     }
 193     return NULL;
 194   }
 195 
 196   virtual int Opcode() const;
 197 };
 198 
 199 class ShenandoahCompareAndExchangePNode : public CompareAndExchangePNode {
 200 public:
 201   ShenandoahCompareAndExchangePNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord)
 202     : CompareAndExchangePNode(c, mem, adr, val, ex, at, t, mem_ord) { }
 203 
 204   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 205     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
 206       return new CompareAndExchangePNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), adr_type(), bottom_type(), order());
 207     }
 208     return NULL;
 209   }
 210 
 211   virtual int Opcode() const;
 212 };
 213 
 214 class ShenandoahCompareAndExchangeNNode : public CompareAndExchangeNNode {
 215 public:
 216   ShenandoahCompareAndExchangeNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord)
 217     : CompareAndExchangeNNode(c, mem, adr, val, ex, at, t, mem_ord) { }
 218 
 219   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 220     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
 221       return new CompareAndExchangeNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), adr_type(), bottom_type(), order());
 222     }
 223     return NULL;
 224   }
 225 
 226   virtual int Opcode() const;
 227 };
 228 
 229 class ShenandoahLoadReferenceBarrierNode : public Node {
 230 public:
 231   enum {
 232     Control,
 233     ValueIn
 234   };
 235 
 236 private:
 237   bool _native;
 238 
 239 public:
 240   ShenandoahLoadReferenceBarrierNode(Node* ctrl, Node* val, bool native);
 241 
 242   bool is_native() const;
 243   virtual int Opcode() const;
 244   virtual const Type* bottom_type() const;
 245   virtual const Type* Value(PhaseGVN* phase) const;
 246   virtual const class TypePtr *adr_type() const { return TypeOopPtr::BOTTOM; }
 247   virtual uint match_edge(uint idx) const {
 248     return idx >= ValueIn;
 249   }
 250   virtual uint ideal_reg() const { return Op_RegP; }
 251 
 252   virtual Node* Identity(PhaseGVN* phase);
 253 
 254   virtual uint size_of() const;
 255   virtual uint hash() const;
 256   virtual bool cmp( const Node &n ) const;
 257 
 258   bool is_redundant();
 259   CallStaticJavaNode* pin_and_expand_null_check(PhaseIterGVN& igvn);
 260 
 261 private:
 262   bool needs_barrier(PhaseGVN* phase, Node* n);
 263   bool needs_barrier_impl(PhaseGVN* phase, Node* n, Unique_Node_List &visited);
 264 };
 265 
 266 
 267 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP