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 
 117 protected:
 118   uint hash() const;
 119   uint cmp(const Node& n) const;
 120   uint size_of() const;
 121 
 122 private:
 123   static bool needs_barrier_impl(PhaseTransform* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace, Unique_Node_List &visited);
 124 
 125 
 126   static bool dominates_memory(PhaseTransform* phase, Node* b1, Node* b2, bool linear);
 127   static bool dominates_memory_impl(PhaseTransform* phase, Node* b1, Node* b2, Node* current, bool linear);
 128 
 129 public:
 130   static bool is_dominator(Node *d_c, Node *n_c, Node* d, Node* n, PhaseIdealLoop* phase);
 131   static bool is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase);
 132 };
 133 
 134 class ShenandoahReadBarrierNode : public ShenandoahBarrierNode {
 135 public:
 136   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj)
 137     : ShenandoahBarrierNode(ctrl, mem, obj, true) {
 138     assert(UseShenandoahGC && (ShenandoahReadBarrier ||
 139                                ShenandoahWriteBarrier ||
 140                                ShenandoahAcmpBarrier),
 141            "should be enabled");
 142   }
 143   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj, bool allow_fromspace)
 144     : ShenandoahBarrierNode(ctrl, mem, obj, allow_fromspace) {
 145     assert(UseShenandoahGC && (ShenandoahReadBarrier ||
 146                                ShenandoahWriteBarrier ||
 147                                ShenandoahAcmpBarrier),
 148            "should be enabled");
 149   }
 150 
 151   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 152   virtual Node* Identity(PhaseTransform* phase);
 153   virtual int Opcode() const;
 154 
 155   bool is_independent(Node* mem);
 156 
 157 private:
 158   static bool is_independent(const Type* in_type, const Type* this_type);
 159   static bool dominates_memory_rb(PhaseTransform* phase, Node* b1, Node* b2, bool linear);
 160   static bool dominates_memory_rb_impl(PhaseTransform* phase, Node* b1, Node* b2, Node* current, bool linear);
 161 };
 162 
 163 class ShenandoahWriteBarrierNode : public ShenandoahBarrierNode {
 164 public:
 165   ShenandoahWriteBarrierNode(Compile* C, Node* ctrl, Node* mem, Node* obj)
 166     : ShenandoahBarrierNode(ctrl, mem, obj, false) {
 167     assert(UseShenandoahGC && ShenandoahWriteBarrier, "should be enabled");
 168     C->add_shenandoah_barrier(this);
 169   }
 170 
 171   virtual int Opcode() const;
 172   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 173   virtual Node* Identity(PhaseTransform* phase);
 174   virtual bool depends_only_on_test() const { return false; }
 175 
 176   static bool should_process_phi(Node* phi, int alias, Compile* C);
 177   static void fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 178   static MergeMemNode* allocate_merge_mem(Node* mem, int alias, Node* rep_proj, Node* rep_ctrl, PhaseIdealLoop* phase);
 179   static MergeMemNode* clone_merge_mem(Node* u, Node* mem, int alias, Node* rep_proj, Node* rep_ctrl, DUIterator& i, PhaseIdealLoop* phase);
 180   static Node* find_raw_mem(Node* ctrl, Node* wb, const Node_List& memory_nodes, PhaseIdealLoop* phase);
 181   static void collect_memory_nodes(int alias, Node_List& memory_nodes, PhaseIdealLoop* phase);
 182   static void fix_raw_mem(Node* ctrl, Node* region, Node* raw_mem, Node* raw_mem_for_ctrl,
 183                           Node* raw_mem_phi, Node_List& memory_nodes,
 184                           Unique_Node_List& uses,
 185                           PhaseIdealLoop* phase);
 186   static Node* get_ctrl(Node* n, PhaseIdealLoop* phase);
 187   static Node* ctrl_or_self(Node* n, PhaseIdealLoop* phase);
 188   static bool mem_is_valid(Node* m, Node* c, PhaseIdealLoop* phase);
 189 
 190   // virtual void set_req( uint i, Node *n ) {
 191   //   if (i == MemNode::Memory) { assert(n == Compiler::current()->immutable_memory(), "set only immutable mem on wb"); }
 192   //   Node::set_req(i, n);
 193   // }
 194 };
 195 
 196 class ShenandoahWBMemProjNode : public ProjNode {
 197 public:
 198   enum {SWBMEMPROJCON = (uint)-3};
 199   ShenandoahWBMemProjNode(Node *src) : ProjNode( src, SWBMEMPROJCON) {
 200     assert(UseShenandoahGC && ShenandoahWriteBarrier, "should be enabled");
 201     assert(src->Opcode() == Op_ShenandoahWriteBarrier || src->is_Mach(), "epxect wb");
 202   }
 203   virtual Node* Identity(PhaseTransform* phase);
 204 
 205   virtual int Opcode() const;
 206   virtual bool      is_CFG() const  { return false; }
 207   virtual const Type *bottom_type() const {return Type::MEMORY;}
 208   virtual const TypePtr *adr_type() const {
 209     Node* wb = in(0);
 210     if (wb == NULL || wb->is_top())  return NULL; // node is dead
 211     assert(wb->Opcode() == Op_ShenandoahWriteBarrier || (wb->is_Mach() && wb->as_Mach()->ideal_Opcode() == Op_ShenandoahWriteBarrier), "expect wb");
 212     return ShenandoahBarrierNode::brooks_pointer_type(wb->bottom_type());
 213   }
 214 
 215   virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
 216   virtual const Type *Value(PhaseTransform* phase ) const {
 217     return bottom_type();
 218   }
 219 #ifndef PRODUCT
 220   virtual void dump_spec(outputStream *st) const {};
 221 #endif
 222 };
 223 
 224 #endif // SHARE_VM_OPTO_SHENANDOAH_SUPPORT_HPP