1 /*
   2  * Copyright (c) 2015, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_C2_SHENANDOAH_SUPPORT_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_C2_SHENANDOAH_SUPPORT_HPP
  26 
  27 #include "gc/shenandoah/shenandoahBrooksPointer.hpp"
  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 ShenandoahBarrierNode : public TypeNode {
  40 private:
  41   bool _allow_fromspace;
  42 
  43 #ifdef ASSERT
  44   enum verify_type {
  45     ShenandoahLoad,
  46     ShenandoahStore,
  47     ShenandoahValue,
  48     ShenandoahOopStore,
  49     ShenandoahNone
  50   };
  51 
  52   static bool verify_helper(Node* in, Node_Stack& phis, VectorSet& visited, verify_type t, bool trace, Unique_Node_List& barriers_used);
  53 #endif
  54 
  55 public:
  56   enum { Control,
  57          Memory,
  58          ValueIn
  59   };
  60 
  61   ShenandoahBarrierNode(Node* ctrl, Node* mem, Node* obj, bool allow_fromspace)
  62     : TypeNode(obj->bottom_type()->isa_oopptr() ? obj->bottom_type()->is_oopptr()->cast_to_nonconst() : obj->bottom_type(), 3),
  63       _allow_fromspace(allow_fromspace) {
  64 
  65     init_req(Control, ctrl);
  66     init_req(Memory, mem);
  67     init_req(ValueIn, obj);
  68 
  69     init_class_id(Class_ShenandoahBarrier);
  70   }
  71 
  72   static Node* skip_through_barrier(Node* n);
  73 
  74   static const TypeOopPtr* brooks_pointer_type(const Type* t) {
  75     return t->is_oopptr()->cast_to_nonconst()->add_offset(ShenandoahBrooksPointer::byte_offset())->is_oopptr();
  76   }
  77 
  78   virtual const TypePtr* adr_type() const {
  79     if (bottom_type() == Type::TOP) {
  80       return NULL;
  81     }
  82     //const TypePtr* adr_type = in(MemNode::Address)->bottom_type()->is_ptr();
  83     const TypePtr* adr_type = brooks_pointer_type(bottom_type());
  84     assert(adr_type->offset() == ShenandoahBrooksPointer::byte_offset(), "sane offset");
  85     assert(Compile::current()->alias_type(adr_type)->is_rewritable(), "brooks ptr must be rewritable");
  86     return adr_type;
  87   }
  88 
  89   virtual uint  ideal_reg() const { return Op_RegP; }
  90   virtual uint match_edge(uint idx) const {
  91     return idx >= ValueIn;
  92   }
  93 
  94   Node* Identity_impl(PhaseGVN* phase);
  95 
  96   virtual const Type* Value(PhaseGVN* phase) const;
  97   virtual bool depends_only_on_test() const {
  98     return true;
  99   };
 100 
 101   static bool needs_barrier(PhaseGVN* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace);
 102 
 103 #ifdef ASSERT
 104   static void report_verify_failure(const char* msg, Node* n1 = NULL, Node* n2 = NULL);
 105   static void verify(RootNode* root);
 106   static void verify_raw_mem(RootNode* root);
 107 #endif
 108 #ifndef PRODUCT
 109   virtual void dump_spec(outputStream *st) const;
 110 #endif
 111 
 112   // protected:
 113   static Node* dom_mem(Node* mem, Node*& mem_ctrl, Node* n, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 114   static Node* dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase);
 115   static bool is_dominator(Node *d_c, Node *n_c, Node* d, Node* n, PhaseIdealLoop* phase);
 116   static bool is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase);
 117   static Node* no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase);
 118 
 119 protected:
 120   uint hash() const;
 121   uint cmp(const Node& n) const;
 122   uint size_of() const;
 123 
 124 private:
 125   static bool needs_barrier_impl(PhaseGVN* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace, Unique_Node_List &visited);
 126 
 127   static bool dominates_memory(PhaseGVN* phase, Node* b1, Node* b2, bool linear);
 128   static bool dominates_memory_impl(PhaseGVN* phase, Node* b1, Node* b2, Node* current, bool linear);
 129 };
 130 
 131 class ShenandoahReadBarrierNode : public ShenandoahBarrierNode {
 132 public:
 133   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj)
 134     : ShenandoahBarrierNode(ctrl, mem, obj, true) {
 135     assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier ||
 136                                ShenandoahWriteBarrier || ShenandoahAcmpBarrier),
 137            "should be enabled");
 138   }
 139   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj, bool allow_fromspace)
 140     : ShenandoahBarrierNode(ctrl, mem, obj, allow_fromspace) {
 141     assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier ||
 142                                ShenandoahWriteBarrier || ShenandoahAcmpBarrier),
 143            "should be enabled");
 144   }
 145 
 146   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 147   virtual Node* Identity(PhaseGVN* phase);
 148   virtual int Opcode() const;
 149 
 150   bool is_independent(Node* mem);
 151 
 152   void try_move(Node *n_ctrl, PhaseIdealLoop* phase);
 153 
 154 private:
 155   static bool is_independent(const Type* in_type, const Type* this_type);
 156   static bool dominates_memory_rb(PhaseGVN* phase, Node* b1, Node* b2, bool linear);
 157   static bool dominates_memory_rb_impl(PhaseGVN* phase, Node* b1, Node* b2, Node* current, bool linear);
 158 };
 159 
 160 class ShenandoahWriteBarrierNode : public ShenandoahBarrierNode {
 161 public:
 162   ShenandoahWriteBarrierNode(Compile* C, Node* ctrl, Node* mem, Node* obj);
 163 
 164   virtual int Opcode() const;
 165   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 166   virtual Node* Identity(PhaseGVN* phase);
 167   virtual bool depends_only_on_test() const { return false; }
 168 
 169   static bool expand(Compile* C, PhaseIterGVN& igvn, int& loop_opts_cnt);
 170   static bool is_gc_state_load(Node *n);
 171   static bool is_heap_state_test(Node* iff, int mask);
 172   static bool is_heap_stable_test(Node* iff);
 173   static bool try_common_gc_state_load(Node *n, PhaseIdealLoop *phase);
 174   static bool has_safepoint_between(Node* start, Node* stop, PhaseIdealLoop *phase);
 175 
 176   static LoopNode* try_move_before_pre_loop(Node* c, Node* val_ctrl, PhaseIdealLoop* phase);
 177   static Node* move_above_predicates(LoopNode* cl, Node* val_ctrl, PhaseIdealLoop* phase);
 178 #ifdef ASSERT
 179   static bool memory_dominates_all_paths(Node* mem, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 180   static void memory_dominates_all_paths_helper(Node* c, Node* rep_ctrl, Unique_Node_List& controls, PhaseIdealLoop* phase);
 181 #endif
 182   void try_move_before_loop(GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, PhaseIdealLoop* phase, bool include_lsm, Unique_Node_List& uses);
 183   void try_move_before_loop_helper(LoopNode* cl, Node* val_ctrl, GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, PhaseIdealLoop* phase, bool include_lsm, Unique_Node_List& uses);
 184   static void pin_and_expand(PhaseIdealLoop* phase);
 185   CallStaticJavaNode* pin_and_expand_null_check(PhaseIterGVN& igvn);
 186   void pin_and_expand_move_barrier(PhaseIdealLoop* phase, GrowableArray<MemoryGraphFixer*>& memory_graph_fixers, Unique_Node_List& uses);
 187   void pin_and_expand_helper(PhaseIdealLoop* phase);
 188   static Node* find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase);
 189   static void follow_barrier_uses(Node* n, Node* ctrl, Unique_Node_List& uses, PhaseIdealLoop* phase);
 190   static void test_null(Node*& ctrl, Node* val, Node*& null_ctrl, PhaseIdealLoop* phase);
 191 
 192   static void test_heap_stable(Node*& ctrl, Node* raw_mem, Node*& heap_stable_ctrl,
 193                                PhaseIdealLoop* phase);
 194   static void call_wb_stub(Node*& ctrl, Node*& val, Node*& result_mem,
 195                            Node* raw_mem, Node* wb_mem, int alias,
 196                            PhaseIdealLoop* phase);
 197   static void heap_stable(Node* c, Node* val, Node* unc_ctrl, Node* raw_mem, Node* wb_mem, Node* region,
 198                           Node* val_phi, Node* mem_phi, Node* raw_mem_phi, Node* unc_region, PhaseIdealLoop* phase);
 199   static Node* clone_null_check(Node*& c, Node* val, Node* unc_ctrl, PhaseIdealLoop* phase);
 200   static void fix_null_check(Node* unc, Node* unc_ctrl, Node* new_unc_ctrl, Unique_Node_List& uses,
 201                              PhaseIdealLoop* phase);
 202   static void in_cset_fast_test(Node*& ctrl, Node*& not_cset_ctrl, Node* val, Node* raw_mem, PhaseIdealLoop* phase);
 203   static void move_heap_stable_test_out_of_loop(IfNode* iff, PhaseIdealLoop* phase);
 204 
 205   static void optimize_after_expansion(VectorSet &visited, Node_Stack &nstack, Node_List &old_new, PhaseIdealLoop* phase);
 206   static void merge_back_to_back_tests(Node* n, PhaseIdealLoop* phase);
 207   static bool identical_backtoback_ifs(Node *n, PhaseIdealLoop* phase);
 208   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);
 209 
 210   static void optimize_before_expansion(PhaseIdealLoop* phase, GrowableArray<MemoryGraphFixer*> memory_graph_fixers, bool include_lsm);
 211   Node* would_subsume(ShenandoahBarrierNode* other, PhaseIdealLoop* phase);
 212 };
 213 
 214 class ShenandoahWBMemProjNode : public Node {
 215 public:
 216   enum { Control,
 217          WriteBarrier };
 218 
 219   ShenandoahWBMemProjNode(Node *src) : Node(NULL, src) {
 220     assert(UseShenandoahGC && ShenandoahWriteBarrier, "should be enabled");
 221     assert(src->Opcode() == Op_ShenandoahWriteBarrier || src->is_Mach(), "epxect wb");
 222   }
 223   virtual Node* Identity(PhaseGVN* phase);
 224 
 225   virtual int Opcode() const;
 226   virtual bool      is_CFG() const  { return false; }
 227   virtual const Type *bottom_type() const {return Type::MEMORY;}
 228   virtual const TypePtr *adr_type() const {
 229     Node* wb = in(WriteBarrier);
 230     if (wb == NULL || wb->is_top())  return NULL; // node is dead
 231     assert(wb->Opcode() == Op_ShenandoahWriteBarrier || (wb->is_Mach() && wb->as_Mach()->ideal_Opcode() == Op_ShenandoahWriteBarrier) || wb->is_Phi(), "expect wb");
 232     return ShenandoahBarrierNode::brooks_pointer_type(wb->bottom_type());
 233   }
 234 
 235   virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
 236   virtual const Type *Value(PhaseGVN* phase ) const {
 237     return bottom_type();
 238   }
 239 #ifndef PRODUCT
 240   virtual void dump_spec(outputStream *st) const {};
 241 #endif
 242 };
 243 
 244 class ShenandoahEnqueueBarrierNode : public Node {
 245 public:
 246   ShenandoahEnqueueBarrierNode(Node* val) : Node(NULL, val) {
 247   }
 248 
 249   const Type *bottom_type() const;
 250   const Type* Value(PhaseGVN* phase) const;
 251   Node* Identity(PhaseGVN* phase);
 252 
 253   int Opcode() const;
 254 
 255 private:
 256   enum { Needed, NotNeeded, MaybeNeeded };
 257 
 258   static int needed(Node* n);
 259   static Node* next(Node* n);
 260 };
 261 
 262 class MemoryGraphFixer : public ResourceObj {
 263 private:
 264   Node_List _memory_nodes;
 265   int _alias;
 266   PhaseIdealLoop* _phase;
 267   bool _include_lsm;
 268 
 269   void collect_memory_nodes();
 270   Node* get_ctrl(Node* n) const;
 271   Node* ctrl_or_self(Node* n) const;
 272   bool mem_is_valid(Node* m, Node* c) const;
 273   MergeMemNode* allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const;
 274   MergeMemNode* clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const;
 275   void fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const;
 276   bool should_process_phi(Node* phi) const;
 277   bool has_mem_phi(Node* region) const;
 278 
 279 public:
 280   MemoryGraphFixer(int alias, bool include_lsm, PhaseIdealLoop* phase) :
 281     _alias(alias), _phase(phase), _include_lsm(include_lsm) {
 282     assert(_alias != Compile::AliasIdxBot, "unsupported");
 283     collect_memory_nodes();
 284   }
 285 
 286   Node* find_mem(Node* ctrl, Node* n) const;
 287   void fix_mem(Node* ctrl, Node* region, Node* mem, Node* mem_for_ctrl, Node* mem_phi, Unique_Node_List& uses);
 288   int alias() const { return _alias; }
 289   void remove(Node* n);
 290 };
 291 
 292 class ShenandoahCompareAndSwapPNode : public CompareAndSwapPNode {
 293 public:
 294   ShenandoahCompareAndSwapPNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 295     : CompareAndSwapPNode(c, mem, adr, val, ex, mem_ord) { }
 296 
 297   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 298     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
 299       return new CompareAndSwapPNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 300     }
 301     return NULL;
 302   }
 303 
 304   virtual int Opcode() const;
 305 };
 306 
 307 class ShenandoahCompareAndSwapNNode : public CompareAndSwapNNode {
 308 public:
 309   ShenandoahCompareAndSwapNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 310     : CompareAndSwapNNode(c, mem, adr, val, ex, mem_ord) { }
 311 
 312   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 313     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
 314       return new CompareAndSwapNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 315     }
 316     return NULL;
 317   }
 318 
 319   virtual int Opcode() const;
 320 };
 321 
 322 class ShenandoahWeakCompareAndSwapPNode : public WeakCompareAndSwapPNode {
 323 public:
 324   ShenandoahWeakCompareAndSwapPNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 325     : WeakCompareAndSwapPNode(c, mem, adr, val, ex, mem_ord) { }
 326 
 327   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 328     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
 329       return new WeakCompareAndSwapPNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 330     }
 331     return NULL;
 332   }
 333 
 334   virtual int Opcode() const;
 335 };
 336 
 337 class ShenandoahWeakCompareAndSwapNNode : public WeakCompareAndSwapNNode {
 338 public:
 339   ShenandoahWeakCompareAndSwapNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord)
 340     : WeakCompareAndSwapNNode(c, mem, adr, val, ex, mem_ord) { }
 341 
 342   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 343     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
 344       return new WeakCompareAndSwapNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), order());
 345     }
 346     return NULL;
 347   }
 348 
 349   virtual int Opcode() const;
 350 };
 351 
 352 class ShenandoahCompareAndExchangePNode : public CompareAndExchangePNode {
 353 public:
 354   ShenandoahCompareAndExchangePNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord)
 355     : CompareAndExchangePNode(c, mem, adr, val, ex, at, t, mem_ord) { }
 356 
 357   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 358     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
 359       return new CompareAndExchangePNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), adr_type(), bottom_type(), order());
 360     }
 361     return NULL;
 362   }
 363 
 364   virtual int Opcode() const;
 365 };
 366 
 367 class ShenandoahCompareAndExchangeNNode : public CompareAndExchangeNNode {
 368 public:
 369   ShenandoahCompareAndExchangeNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord)
 370     : CompareAndExchangeNNode(c, mem, adr, val, ex, at, t, mem_ord) { }
 371 
 372   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 373     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
 374       return new CompareAndExchangeNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn), adr_type(), bottom_type(), order());
 375     }
 376     return NULL;
 377   }
 378 
 379   virtual int Opcode() const;
 380 };
 381 
 382 
 383 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP