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/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(PhaseGVN* phase);
  96 
  97   virtual const Type* Value(PhaseGVN* phase) const;
  98   virtual bool depends_only_on_test() const {
  99     return true;
 100   };
 101 
 102   static bool needs_barrier(PhaseGVN* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace);
 103 
 104 #ifdef ASSERT
 105   static void report_verify_failure(const char* msg, Node* n1 = NULL, Node* n2 = NULL);
 106   static void verify(RootNode* root);
 107   static void verify_raw_mem(RootNode* root);
 108 #endif
 109 #ifndef PRODUCT
 110   virtual void dump_spec(outputStream *st) const;
 111 #endif
 112 
 113   Node* try_common(Node *n_ctrl, PhaseIdealLoop* phase);
 114 
 115   // protected:
 116   static Node* dom_mem(Node* mem, Node*& mem_ctrl, Node* n, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 117   static Node* dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase);
 118   static bool is_dominator(Node *d_c, Node *n_c, Node* d, Node* n, PhaseIdealLoop* phase);
 119   static bool is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase);
 120   static Node* no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase);
 121   static void do_cmpp_if(GraphKit& kit, Node*& taken_branch, Node*& untaken_branch, Node*& taken_memory, Node*& untaken_memory);
 122   static const TypePtr* fix_addp_type(const TypePtr* res, Node* base);
 123 
 124 protected:
 125   uint hash() const;
 126   uint cmp(const Node& n) const;
 127   uint size_of() const;
 128 
 129 private:
 130   static bool needs_barrier_impl(PhaseGVN* phase, ShenandoahBarrierNode* orig, Node* n, Node* rb_mem, bool allow_fromspace, Unique_Node_List &visited);
 131 
 132 
 133   static bool dominates_memory(PhaseGVN* phase, Node* b1, Node* b2, bool linear);
 134   static bool dominates_memory_impl(PhaseGVN* phase, Node* b1, Node* b2, Node* current, bool linear);
 135 };
 136 
 137 class ShenandoahReadBarrierNode : public ShenandoahBarrierNode {
 138 public:
 139   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj)
 140     : ShenandoahBarrierNode(ctrl, mem, obj, true) {
 141     assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier ||
 142                                ShenandoahWriteBarrier || ShenandoahStoreValWriteBarrier ||
 143                                ShenandoahAcmpBarrier),
 144            "should be enabled");
 145   }
 146   ShenandoahReadBarrierNode(Node* ctrl, Node* mem, Node* obj, bool allow_fromspace)
 147     : ShenandoahBarrierNode(ctrl, mem, obj, allow_fromspace) {
 148     assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier ||
 149                                ShenandoahWriteBarrier || ShenandoahStoreValWriteBarrier ||
 150                                ShenandoahAcmpBarrier),
 151            "should be enabled");
 152   }
 153 
 154   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 155   virtual Node* Identity(PhaseGVN* phase);
 156   virtual int Opcode() const;
 157 
 158   bool is_independent(Node* mem);
 159 
 160   void try_move(Node *n_ctrl, PhaseIdealLoop* phase);
 161 
 162 private:
 163   static bool is_independent(const Type* in_type, const Type* this_type);
 164   static bool dominates_memory_rb(PhaseGVN* phase, Node* b1, Node* b2, bool linear);
 165   static bool dominates_memory_rb_impl(PhaseGVN* phase, Node* b1, Node* b2, Node* current, bool linear);
 166 };
 167 
 168 class ShenandoahWriteBarrierNode : public ShenandoahBarrierNode {
 169 private:
 170   static bool fix_mem_phis_helper(Node* c, Node* mem, Node* mem_ctrl, Node* rep_ctrl, int alias, VectorSet& controls, GrowableArray<Node*>& phis, PhaseIdealLoop* phase);
 171 
 172 public:
 173   ShenandoahWriteBarrierNode(Compile* C, Node* ctrl, Node* mem, Node* obj)
 174     : ShenandoahBarrierNode(ctrl, mem, obj, false) {
 175     assert(UseShenandoahGC && (ShenandoahWriteBarrier || ShenandoahStoreValWriteBarrier), "should be enabled");
 176     C->add_shenandoah_barrier(this);
 177   }
 178 
 179   virtual int Opcode() const;
 180   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 181   virtual Node* Identity(PhaseGVN* phase);
 182   virtual bool depends_only_on_test() const { return false; }
 183 
 184   static bool expand(Compile* C, PhaseIterGVN& igvn, int& loop_opts_cnt);
 185   static bool is_gc_state_load(Node *n);
 186   static bool is_evacuation_in_progress_test(Node* iff);
 187   static Node* evacuation_in_progress_test_ctrl(Node* iff);
 188   static bool try_common_gc_state_load(Node *n, PhaseIdealLoop *phase);
 189 
 190   static LoopNode* try_move_before_pre_loop(Node* c, Node* val_ctrl, PhaseIdealLoop* phase);
 191   static Node* move_above_predicates(LoopNode* cl, Node* val_ctrl, PhaseIdealLoop* phase);
 192   static bool fix_mem_phis(Node* mem, Node* mem_ctrl, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 193   static bool should_process_phi(Node* phi, int alias, Compile* C);
 194   static void fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 195   static MergeMemNode* allocate_merge_mem(Node* mem, int alias, Node* rep_proj, Node* rep_ctrl, PhaseIdealLoop* phase);
 196   static MergeMemNode* clone_merge_mem(Node* u, Node* mem, int alias, Node* rep_proj, Node* rep_ctrl, DUIterator& i, PhaseIdealLoop* phase);
 197 #ifdef ASSERT
 198   static bool memory_dominates_all_paths(Node* mem, Node* rep_ctrl, int alias, PhaseIdealLoop* phase);
 199   static void memory_dominates_all_paths_helper(Node* c, Node* rep_ctrl, Unique_Node_List& controls, PhaseIdealLoop* phase);
 200 #endif
 201   Node* try_move_before_loop(Node *n_ctrl, PhaseIdealLoop* phase);
 202   Node* try_move_before_loop_helper(LoopNode* cl,  Node* val_ctrl, Node* mem, PhaseIdealLoop* phase);
 203   static void pin_and_expand(PhaseIdealLoop* phase);
 204   CallStaticJavaNode* pin_and_expand_null_check(PhaseIterGVN& igvn);
 205   void pin_and_expand_move_barrier(PhaseIdealLoop* phase);
 206   void pin_and_expand_helper(PhaseIdealLoop* phase);
 207   static Node* find_raw_mem(Node* ctrl, Node* wb, const Node_List& memory_nodes, PhaseIdealLoop* phase);
 208   static Node* pick_phi(Node* phi1, Node* phi2, Node_Stack& phis, VectorSet& visited, PhaseIdealLoop* phase);
 209   static Node* find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase);
 210   static void follow_barrier_uses(Node* n, Node* ctrl, Unique_Node_List& uses, PhaseIdealLoop* phase);
 211   static void collect_memory_nodes(int alias, Node_List& memory_nodes, PhaseIdealLoop* phase);
 212   static void fix_raw_mem(Node* ctrl, Node* region, Node* raw_mem, Node* raw_mem_for_ctrl,
 213                           Node* raw_mem_phi, Node_List& memory_nodes,
 214                           Unique_Node_List& uses,
 215                           PhaseIdealLoop* phase);
 216   static void test_evacuation_in_progress(Node* ctrl, int alias, Node*& raw_mem, Node*& wb_mem,
 217                                           IfNode*& evacuation_iff, Node*& evac_in_progress,
 218                                           Node*& evac_not_in_progress, PhaseIdealLoop* phase);
 219   static void evacuation_not_in_progress(Node* c, Node* v, Node* unc_ctrl, Node* raw_mem, Node* wb_mem, Node* region,
 220                                          Node* val_phi, Node* mem_phi, Node* raw_mem_phi, Node*& unc_region,
 221                                          PhaseIdealLoop* phase);
 222   static void evacuation_in_progress(Node* c, Node* val, Node* evacuation_iff, Node* unc, Node* unc_ctrl,
 223                                      Node* raw_mem, Node* wb_mem, Node* region, Node* val_phi, Node* mem_phi,
 224                                      Node* raw_mem_phi, Node* unc_region, int alias, Unique_Node_List& uses,
 225                                      PhaseIdealLoop* phase);
 226   static void evacuation_not_in_progress_null_check(Node*& c, Node*& val, Node* unc_ctrl, Node*& unc_region,
 227                                                     PhaseIdealLoop* phase);
 228   static void evacuation_in_progress_null_check(Node*& c, Node*& val, Node* evacuation_iff, Node* unc, Node* unc_ctrl,
 229                                                 Node* unc_region, Unique_Node_List& uses, PhaseIdealLoop* phase);
 230   static void in_cset_fast_test(Node*& c, Node* rbtrue, Node* raw_mem, Node* wb_mem, Node* region, Node* val_phi,
 231                                 Node* mem_phi, Node* raw_mem_phi, PhaseIdealLoop* phase);
 232   static Node* get_ctrl(Node* n, PhaseIdealLoop* phase);
 233   static Node* ctrl_or_self(Node* n, PhaseIdealLoop* phase);
 234   static bool mem_is_valid(Node* m, Node* c, PhaseIdealLoop* phase);
 235   static void move_evacuation_test_out_of_loop(IfNode* iff, PhaseIdealLoop* phase);
 236   static void optimize_after_expansion(const Node_List& evacuation_tests, const Node_List& gc_state_loads, Node_List &old_new, PhaseIdealLoop* phase);
 237   static void merge_back_to_back_evacuation_tests(Node* n, PhaseIdealLoop* phase);
 238 };
 239 
 240 class ShenandoahWBMemProjNode : public ProjNode {
 241 public:
 242   enum {SWBMEMPROJCON = (uint)-3};
 243   ShenandoahWBMemProjNode(Node *src) : ProjNode( src, SWBMEMPROJCON) {
 244     assert(UseShenandoahGC && (ShenandoahWriteBarrier || ShenandoahStoreValWriteBarrier), "should be enabled");
 245     assert(src->Opcode() == Op_ShenandoahWriteBarrier || src->is_Mach(), "epxect wb");
 246   }
 247   virtual Node* Identity(PhaseGVN* phase);
 248 
 249   virtual int Opcode() const;
 250   virtual bool      is_CFG() const  { return false; }
 251   virtual const Type *bottom_type() const {return Type::MEMORY;}
 252   virtual const TypePtr *adr_type() const {
 253     Node* wb = in(0);
 254     if (wb == NULL || wb->is_top())  return NULL; // node is dead
 255     assert(wb->Opcode() == Op_ShenandoahWriteBarrier || (wb->is_Mach() && wb->as_Mach()->ideal_Opcode() == Op_ShenandoahWriteBarrier), "expect wb");
 256     return ShenandoahBarrierNode::brooks_pointer_type(wb->bottom_type());
 257   }
 258 
 259   virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
 260   virtual const Type *Value(PhaseGVN* phase ) const {
 261     return bottom_type();
 262   }
 263 #ifndef PRODUCT
 264   virtual void dump_spec(outputStream *st) const {};
 265 #endif
 266 };
 267 
 268 #endif // SHARE_VM_OPTO_SHENANDOAH_SUPPORT_HPP