1 /*
   2  * Copyright (c) 2015, Red Hat, Inc. and/or its affiliates.
   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_OPTO_SHENANDOAH_SUPPORT_HPP
  25 #define SHARE_VM_OPTO_SHENANDOAH_SUPPORT_HPP
  26 
  27 #include "gc_implementation/shenandoah/brooksPointer.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 
  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     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 #endif
  53 
  54 public:
  55 
  56 public:
  57   enum { Control,
  58          Memory,
  59          ValueIn
  60   };
  61 
  62   ShenandoahBarrierNode(Node* ctrl, Node* mem, Node* obj, bool allow_fromspace)
  63     : TypeNode(obj->bottom_type()->isa_oopptr() ? obj->bottom_type()->is_oopptr()->cast_to_nonconst() : obj->bottom_type(), 3),
  64       _allow_fromspace(allow_fromspace) {
  65 
  66     init_req(Control, ctrl);
  67     init_req(Memory, mem);
  68     init_req(ValueIn, obj);
  69 
  70     init_class_id(Class_ShenandoahBarrier);
  71   }
  72 
  73   static Node* skip_through_barrier(Node* n);
  74 
  75   static const TypeOopPtr* brooks_pointer_type(const Type* t) {
  76     return t->is_oopptr()->cast_to_nonconst()->add_offset(BrooksPointer::byte_offset())->is_oopptr();
  77   }
  78 
  79   virtual const TypePtr* adr_type() const {
  80     if (bottom_type() == Type::TOP) {
  81       return NULL;
  82     }
  83     //const TypePtr* adr_type = in(MemNode::Address)->bottom_type()->is_ptr();
  84     const TypePtr* adr_type = brooks_pointer_type(bottom_type());
  85     assert(adr_type->offset() == BrooksPointer::byte_offset(), "sane offset");
  86     assert(Compile::current()->alias_type(adr_type)->is_rewritable(), "brooks ptr must be rewritable");
  87     return adr_type;
  88   }
  89 
  90   virtual uint  ideal_reg() const { return Op_RegP; }
  91   virtual uint match_edge(uint idx) const {
  92     return idx >= ValueIn;
  93   }
  94 
  95   Node* Identity_impl(PhaseTransform* phase);
  96 
  97   virtual const Type* Value(PhaseTransform* phase) const;
  98   virtual bool depends_only_on_test() const {
  99     return true;
 100   };
 101 
 102   static bool is_evacuation_in_progress_test(Node *n);
 103   static bool is_gc_state_load(Node *n);
 104 
 105   static bool needs_barrier(PhaseTransform* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace);
 106 
 107 #ifdef ASSERT
 108   static void report_verify_failure(const char* msg, Node* n1 = NULL, Node* n2 = NULL);
 109   static void verify(RootNode* root);
 110   static void verify_raw_mem(RootNode* root);
 111 #endif
 112 #ifndef PRODUCT
 113   virtual void dump_spec(outputStream *st) const;
 114 #endif
 115   static void do_cmpp_if(GraphKit& kit, Node*& taken_branch, Node*& untaken_branch, Node*& taken_memory, Node*& untaken_memory);
 116   static const TypePtr* fix_addp_type(const TypePtr* res, Node* base);
 117 
 118 protected:
 119   uint hash() const;
 120   uint cmp(const Node& n) const;
 121   uint size_of() const;
 122 
 123 private:
 124   static bool needs_barrier_impl(PhaseTransform* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace, Unique_Node_List &visited);
 125 
 126 
 127   static bool dominates_memory(PhaseTransform* phase, Node* b1, Node* b2, bool linear);
 128   static bool dominates_memory_impl(PhaseTransform* phase, Node* b1, Node* b2, Node* current, bool linear);
 129 
 130 public:
 131   static bool is_dominator(Node *d_c, Node *n_c, Node* d, Node* n, PhaseIdealLoop* phase);
 132   static bool is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase);
 133 };
 134 
 135 class ShenandoahReadBarrierNode : public ShenandoahBarrierNode {
 136 public:
 137   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj)
 138     : ShenandoahBarrierNode(ctrl, mem, obj, true) {
 139     assert(UseShenandoahGC && (ShenandoahReadBarrier ||
 140                                ShenandoahWriteBarrier ||
 141                                ShenandoahAcmpBarrier),
 142            "should be enabled");
 143   }
 144   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj, bool allow_fromspace)
 145     : ShenandoahBarrierNode(ctrl, mem, obj, allow_fromspace) {
 146     assert(UseShenandoahGC && (ShenandoahReadBarrier ||
 147                                ShenandoahWriteBarrier ||
 148                                ShenandoahAcmpBarrier),
 149            "should be enabled");
 150   }
 151 
 152   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 153   virtual Node* Identity(PhaseTransform* phase);
 154   virtual int Opcode() const;
 155 
 156   bool is_independent(Node* mem);
 157 
 158 private:
 159   static bool is_independent(const Type* in_type, const Type* this_type);
 160   static bool dominates_memory_rb(PhaseTransform* phase, Node* b1, Node* b2, bool linear);
 161   static bool dominates_memory_rb_impl(PhaseTransform* phase, Node* b1, Node* b2, Node* current, bool linear);
 162 };
 163 
 164 class ShenandoahWriteBarrierNode : public ShenandoahBarrierNode {
 165 public:
 166   ShenandoahWriteBarrierNode(Compile* C, Node* ctrl, Node* mem, Node* obj)
 167     : ShenandoahBarrierNode(ctrl, mem, obj, false) {
 168     assert(UseShenandoahGC && ShenandoahWriteBarrier, "should be enabled");
 169     C->add_shenandoah_barrier(this);
 170   }
 171 
 172   virtual int Opcode() const;
 173   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 174   virtual Node* Identity(PhaseTransform* phase);
 175   virtual bool depends_only_on_test() const { return false; }
 176 
 177   static bool should_process_phi(Node* phi, int alias, Compile* C);
 178   static void fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 179   static MergeMemNode* allocate_merge_mem(Node* mem, int alias, Node* rep_proj, Node* rep_ctrl, PhaseIdealLoop* phase);
 180   static MergeMemNode* clone_merge_mem(Node* u, Node* mem, int alias, Node* rep_proj, Node* rep_ctrl, DUIterator& i, PhaseIdealLoop* phase);
 181   static Node* find_raw_mem(Node* ctrl, Node* wb, const Node_List& memory_nodes, PhaseIdealLoop* phase);
 182   static void collect_memory_nodes(int alias, Node_List& memory_nodes, PhaseIdealLoop* phase);
 183   static void fix_raw_mem(Node* ctrl, Node* region, Node* raw_mem, Node* raw_mem_for_ctrl,
 184                           Node* raw_mem_phi, Node_List& memory_nodes,
 185                           Unique_Node_List& uses,
 186                           PhaseIdealLoop* phase);
 187   static Node* get_ctrl(Node* n, PhaseIdealLoop* phase);
 188   static Node* ctrl_or_self(Node* n, PhaseIdealLoop* phase);
 189   static bool mem_is_valid(Node* m, Node* c, PhaseIdealLoop* phase);
 190 
 191   // virtual void set_req( uint i, Node *n ) {
 192   //   if (i == MemNode::Memory) { assert(n == Compiler::current()->immutable_memory(), "set only immutable mem on wb"); }
 193   //   Node::set_req(i, n);
 194   // }
 195 };
 196 
 197 class ShenandoahWBMemProjNode : public ProjNode {
 198 public:
 199   enum {SWBMEMPROJCON = (uint)-3};
 200   ShenandoahWBMemProjNode(Node *src) : ProjNode( src, SWBMEMPROJCON) {
 201     assert(UseShenandoahGC && ShenandoahWriteBarrier, "should be enabled");
 202     assert(src->Opcode() == Op_ShenandoahWriteBarrier || src->is_Mach(), "epxect wb");
 203   }
 204   virtual Node* Identity(PhaseTransform* phase);
 205 
 206   virtual int Opcode() const;
 207   virtual bool      is_CFG() const  { return false; }
 208   virtual const Type *bottom_type() const {return Type::MEMORY;}
 209   virtual const TypePtr *adr_type() const {
 210     Node* wb = in(0);
 211     if (wb == NULL || wb->is_top())  return NULL; // node is dead
 212     assert(wb->Opcode() == Op_ShenandoahWriteBarrier || (wb->is_Mach() && wb->as_Mach()->ideal_Opcode() == Op_ShenandoahWriteBarrier), "expect wb");
 213     return ShenandoahBarrierNode::brooks_pointer_type(wb->bottom_type());
 214   }
 215 
 216   virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
 217   virtual const Type *Value(PhaseTransform* phase ) const {
 218     return bottom_type();
 219   }
 220 #ifndef PRODUCT
 221   virtual void dump_spec(outputStream *st) const {};
 222 #endif
 223 };
 224 
 225 #endif // SHARE_VM_OPTO_SHENANDOAH_SUPPORT_HPP