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 "memory/allocation.hpp"
  28 #include "opto/addnode.hpp"
  29 #include "opto/memnode.hpp"
  30 #include "opto/multnode.hpp"
  31 #include "opto/node.hpp"
  32 
  33 class PhaseTransform;
  34 
  35 
  36 class ShenandoahBarrierNode : public TypeNode {
  37 public:
  38 
  39 public:
  40   enum { Control,
  41          Memory,
  42          ValueIn
  43   };
  44 
  45   ShenandoahBarrierNode(Node* ctrl, Node* mem, Node* obj)
  46     : TypeNode(obj->bottom_type(), 3) {
  47 
  48     init_req(Control, ctrl);
  49     init_req(Memory, mem);
  50     init_req(ValueIn, obj);
  51 
  52     init_class_id(Class_ShenandoahBarrier);
  53   }
  54 
  55   static Node* skip_through_barrier(Node* n);
  56 
  57   virtual const class TypePtr* adr_type() const {
  58     //const TypePtr* adr_type = in(MemNode::Address)->bottom_type()->is_ptr();
  59     const TypePtr* adr_type = bottom_type()->is_ptr()->add_offset(-8);
  60     assert(adr_type->offset() == -8, "sane offset");
  61     assert(Compile::current()->alias_type(adr_type)->is_rewritable(), "brooks ptr must be rewritable");
  62     return adr_type;
  63   }
  64 
  65   virtual uint  ideal_reg() const { return Op_RegP; }
  66   virtual uint match_edge(uint idx) const {
  67     return idx >= ValueIn;
  68   }
  69   virtual uint size_of() const { return sizeof(*this); }
  70 
  71   virtual Node* Identity(PhaseTransform* phase);
  72   Node* Identity_impl(PhaseTransform* phase);
  73 
  74   virtual const Type* Value(PhaseTransform* phase) const;
  75   virtual bool depends_only_on_test() const {
  76     return true;
  77   };
  78 #ifdef ASSERT
  79   void check_invariants();
  80   uint num_mem_projs();
  81 #endif
  82 
  83   static bool needs_barrier(PhaseTransform* phase, Node* orig, Node* n, Node* rb_mem);
  84 
  85   static bool has_barrier_users(Node* n, Unique_Node_List &visited);
  86 
  87 private:
  88   static bool needs_barrier_impl(PhaseTransform* phase, Node* orig, Node* n, Node* rb_mem, Unique_Node_List &visited);
  89 
  90 
  91   bool dominates_control(PhaseTransform* phase, Node* c1, Node* c2);
  92   bool dominates_memory(PhaseTransform* phase, Node* b1, Node* b2);
  93   bool dominates_memory_impl(PhaseTransform* phase, Node* b1, Node* b2, Node* current, Unique_Node_List &visisted);
  94 };
  95 
  96 class ShenandoahReadBarrierNode : public ShenandoahBarrierNode {
  97 public:
  98   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj)
  99     : ShenandoahBarrierNode(ctrl, mem, obj) {
 100   }
 101 
 102   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 103   virtual Node* Identity(PhaseTransform* phase);
 104   virtual int Opcode() const;
 105 
 106 private:
 107   bool is_independent(const Type* in_type, const Type* this_type) const;
 108   bool dominates_memory_rb(PhaseTransform* phase, Node* b1, Node* b2);
 109   bool dominates_memory_rb_impl(PhaseTransform* phase, Node* b1, Node* b2, Node* current, Unique_Node_List &visited);
 110 };
 111 
 112 class ShenandoahWriteBarrierNode : public ShenandoahBarrierNode {
 113 public:
 114   ShenandoahWriteBarrierNode(Node* ctrl, Node* mem, Node* obj)
 115     : ShenandoahBarrierNode(ctrl, mem, obj) {
 116     //tty->print("new wb: "); dump();
 117   }
 118 
 119   virtual int Opcode() const;
 120   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 121 
 122   // virtual void set_req( uint i, Node *n ) {
 123   //   if (i == MemNode::Memory) { assert(n == Compiler::current()->immutable_memory(), "set only immutable mem on wb"); }
 124   //   Node::set_req(i, n);
 125   // }
 126 };
 127 
 128 class ShenandoahWBMemProjNode : public ProjNode {
 129 public:
 130   enum {SWBMEMPROJCON = (uint)-3};
 131   ShenandoahWBMemProjNode(Node *src) : ProjNode( src, SWBMEMPROJCON) {
 132     assert(src->Opcode() == Op_ShenandoahWriteBarrier || src->is_Mach(), "epxect wb");
 133 #ifdef ASSERT
 134     in(0)->as_ShenandoahBarrier()->check_invariants();
 135 #endif
 136   }
 137   virtual Node* Identity(PhaseTransform* phase);
 138 
 139   virtual int Opcode() const;
 140   virtual bool      is_CFG() const  { return false; }
 141   virtual const Type *bottom_type() const {return Type::MEMORY;}
 142   virtual const TypePtr *adr_type() const {
 143     Node* ctrl = in(0);
 144     if (ctrl == NULL)  return NULL; // node is dead
 145     assert(ctrl->Opcode() == Op_ShenandoahWriteBarrier || ctrl->is_Mach(), "expect wb");
 146     return ctrl->adr_type();
 147   }
 148 
 149   virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
 150   virtual const Type *Value( PhaseTransform *phase ) const {
 151     return bottom_type();
 152   }
 153 #ifndef PRODUCT
 154   virtual void dump_spec(outputStream *st) const {};
 155 #endif
 156 };
 157 
 158 #endif // SHARE_VM_OPTO_SHENANDOAH_SUPPORT_HPP