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_GC_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP
  25 #define SHARE_GC_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP
  26 
  27 #include "gc_implementation/shenandoah/shenandoahForwarding.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 IdealLoopTree;
  37 class PhaseGVN;
  38 class MemoryGraphFixer;
  39 
  40 class ShenandoahBarrierC2Support : public AllStatic {
  41 private:
  42 #ifdef ASSERT
  43   enum verify_type {
  44     ShenandoahLoad,
  45     ShenandoahStore,
  46     ShenandoahValue,
  47     ShenandoahOopStore,
  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   static void report_verify_failure(const char* msg, Node* n1 = NULL, Node* n2 = NULL);
  53 public:
  54   static void verify_raw_mem(RootNode* root);
  55 private:
  56 #endif
  57   static Node* dom_mem(Node* mem, Node* ctrl, int alias, Node*& mem_ctrl, PhaseIdealLoop* phase);
  58   static Node* no_branches(Node* c, Node* dom, bool allow_one_proj, PhaseIdealLoop* phase);
  59   static bool is_heap_state_test(Node* iff, int mask);
  60   static bool try_common_gc_state_load(Node *n, PhaseIdealLoop *phase);
  61   static bool has_safepoint_between(Node* start, Node* stop, PhaseIdealLoop *phase);
  62   static Node* find_bottom_mem(Node* ctrl, PhaseIdealLoop* phase);
  63   static void follow_barrier_uses(Node* n, Node* ctrl, Unique_Node_List& uses, PhaseIdealLoop* phase);
  64   static void test_null(Node*& ctrl, Node* val, Node*& null_ctrl, PhaseIdealLoop* phase);
  65   static void test_heap_stable(Node*& ctrl, Node* raw_mem, Node*& heap_stable_ctrl,
  66                                PhaseIdealLoop* phase);
  67   static void call_lrb_stub(Node*& ctrl, Node*& val, Node*& result_mem, Node* raw_mem, PhaseIdealLoop* phase);
  68   static Node* clone_null_check(Node*& c, Node* val, Node* unc_ctrl, PhaseIdealLoop* phase);
  69   static void fix_null_check(Node* unc, Node* unc_ctrl, Node* new_unc_ctrl, Unique_Node_List& uses,
  70                              PhaseIdealLoop* phase);
  71   static void in_cset_fast_test(Node*& ctrl, Node*& not_cset_ctrl, Node* val, Node* raw_mem, PhaseIdealLoop* phase);
  72   static void move_heap_stable_test_out_of_loop(IfNode* iff, PhaseIdealLoop* phase);
  73   static void merge_back_to_back_tests(Node* n, PhaseIdealLoop* phase);
  74   static bool identical_backtoback_ifs(Node *n, PhaseIdealLoop* phase);
  75   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);
  76   static IfNode* find_unswitching_candidate(const IdealLoopTree *loop, PhaseIdealLoop* phase);
  77 
  78 public:
  79   static bool is_dominator(Node* d_c, Node* n_c, Node* d, Node* n, PhaseIdealLoop* phase);
  80   static bool is_dominator_same_ctrl(Node* c, Node* d, Node* n, PhaseIdealLoop* phase);
  81 
  82   static bool is_gc_state_load(Node* n);
  83   static bool is_heap_stable_test(Node* iff);
  84 
  85   static bool expand(Compile* C, PhaseIterGVN& igvn);
  86   static void pin_and_expand(PhaseIdealLoop* phase);
  87   static void optimize_after_expansion(VectorSet& visited, Node_Stack& nstack, Node_List& old_new, PhaseIdealLoop* phase);
  88 
  89 #ifdef ASSERT
  90   static void verify(RootNode* root);
  91 #endif
  92 };
  93 
  94 class MemoryGraphFixer : public ResourceObj {
  95 private:
  96   Node_List _memory_nodes;
  97   int _alias;
  98   PhaseIdealLoop* _phase;
  99   bool _include_lsm;
 100 
 101   void collect_memory_nodes();
 102   Node* get_ctrl(Node* n) const;
 103   Node* ctrl_or_self(Node* n) const;
 104   bool mem_is_valid(Node* m, Node* c) const;
 105   MergeMemNode* allocate_merge_mem(Node* mem, Node* rep_proj, Node* rep_ctrl) const;
 106   MergeMemNode* clone_merge_mem(Node* u, Node* mem, Node* rep_proj, Node* rep_ctrl, DUIterator& i) const;
 107   void fix_memory_uses(Node* mem, Node* replacement, Node* rep_proj, Node* rep_ctrl) const;
 108   bool should_process_phi(Node* phi) const;
 109   bool has_mem_phi(Node* region) const;
 110 
 111 public:
 112   MemoryGraphFixer(int alias, bool include_lsm, PhaseIdealLoop* phase) :
 113     _alias(alias), _phase(phase), _include_lsm(include_lsm) {
 114     assert(_alias != Compile::AliasIdxBot, "unsupported");
 115     collect_memory_nodes();
 116   }
 117 
 118   Node* find_mem(Node* ctrl, Node* n) const;
 119   void fix_mem(Node* ctrl, Node* region, Node* mem, Node* mem_for_ctrl, Node* mem_phi, Unique_Node_List& uses);
 120   int alias() const { return _alias; }
 121 };
 122 
 123 class ShenandoahCompareAndSwapPNode : public CompareAndSwapPNode {
 124 public:
 125   ShenandoahCompareAndSwapPNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex)
 126     : CompareAndSwapPNode(c, mem, adr, val, ex) { }
 127 
 128   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 129     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypePtr::NULL_PTR) {
 130       return new (phase->C) CompareAndSwapPNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn));
 131     }
 132     return NULL;
 133   }
 134 
 135   virtual int Opcode() const;
 136 };
 137 
 138 class ShenandoahCompareAndSwapNNode : public CompareAndSwapNNode {
 139 public:
 140   ShenandoahCompareAndSwapNNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex)
 141     : CompareAndSwapNNode(c, mem, adr, val, ex) { }
 142 
 143   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 144     if (in(ExpectedIn) != NULL && phase->type(in(ExpectedIn)) == TypeNarrowOop::NULL_PTR) {
 145       return new (phase->C) CompareAndSwapNNode(in(MemNode::Control), in(MemNode::Memory), in(MemNode::Address), in(MemNode::ValueIn), in(ExpectedIn));
 146     }
 147     return NULL;
 148   }
 149 
 150   virtual int Opcode() const;
 151 };
 152 
 153 class ShenandoahLoadReferenceBarrierNode : public Node {
 154 public:
 155   enum {
 156     Control,
 157     ValueIn
 158   };
 159 
 160   enum Strength {
 161     NONE, STRONG
 162   };
 163 
 164   ShenandoahLoadReferenceBarrierNode(Node* ctrl, Node* val);
 165 
 166   virtual int Opcode() const;
 167   virtual const Type* bottom_type() const;
 168   virtual const Type* Value(PhaseTransform *phase) const;
 169   virtual const class TypePtr *adr_type() const { return TypeOopPtr::BOTTOM; }
 170   virtual uint match_edge(uint idx) const {
 171     return idx >= ValueIn;
 172   }
 173   virtual uint ideal_reg() const { return Op_RegP; }
 174 
 175   virtual Node* Identity(PhaseTransform *phase);
 176 
 177   uint size_of() const {
 178     return sizeof(*this);
 179   }
 180 
 181   Strength get_barrier_strength();
 182   CallStaticJavaNode* pin_and_expand_null_check(PhaseIterGVN& igvn);
 183 
 184 private:
 185   bool needs_barrier(PhaseTransform* phase, Node* n);
 186   bool needs_barrier_impl(PhaseTransform* phase, Node* n, Unique_Node_List &visited);
 187 };
 188 
 189 
 190 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHSUPPORT_HPP