1 /*
   2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #ifndef SHARE_GC_Z_C2_ZBARRIERSETC2_HPP
  25 #define SHARE_GC_Z_C2_ZBARRIERSETC2_HPP
  26 
  27 #include "gc/shared/c2/barrierSetC2.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "opto/node.hpp"
  30 #include "utilities/growableArray.hpp"
  31 
  32 class LoadBarrierNode : public MultiNode {
  33 private:
  34   bool _weak;
  35   bool _writeback;          // Controls if the barrier writes the healed oop back to memory
  36                             // A swap on a memory location must never write back the healed oop
  37   bool _oop_reload_allowed; // Controls if the barrier are allowed to reload the oop from memory
  38                             // before healing, otherwise both the oop and the address must be
  39                             // passed to the barrier from the oop
  40 
  41   static bool is_dominator(PhaseIdealLoop* phase, bool linear_only, Node *d, Node *n);
  42   void push_dominated_barriers(PhaseIterGVN* igvn) const;
  43 
  44 public:
  45   enum {
  46     Control,
  47     Memory,
  48     Oop,
  49     Address,
  50     Number_of_Outputs = Address,
  51     Similar,
  52     Number_of_Inputs
  53   };
  54 
  55   LoadBarrierNode(Compile* C,
  56                   Node* c,
  57                   Node* mem,
  58                   Node* val,
  59                   Node* adr,
  60                   bool weak,
  61                   bool writeback,
  62                   bool oop_reload_allowed);
  63 
  64   virtual int Opcode() const;
  65   virtual const Type *bottom_type() const;
  66   virtual const Type *Value(PhaseGVN *phase) const;
  67   virtual Node *Identity(PhaseGVN *phase);
  68   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
  69 
  70   LoadBarrierNode* has_dominating_barrier(PhaseIdealLoop* phase,
  71                                           bool linear_only,
  72                                           bool look_for_similar);
  73 
  74   void fix_similar_in_uses(PhaseIterGVN* igvn);
  75 
  76   bool has_true_uses() const;
  77 
  78   bool can_be_eliminated() const {
  79     return !in(Similar)->is_top();
  80   }
  81 
  82   bool is_weak() const {
  83     return _weak;
  84   }
  85 
  86   bool is_writeback() const {
  87     return _writeback;
  88   }
  89 
  90   bool oop_reload_allowed() const {
  91     return _oop_reload_allowed;
  92   }
  93 };
  94 
  95 class LoadBarrierSlowRegNode : public LoadPNode {
  96 public:
  97   LoadBarrierSlowRegNode(Node *c,
  98                          Node *mem,
  99                          Node *adr,
 100                          const TypePtr *at,
 101                          const TypePtr* t,
 102                          MemOrd mo,
 103                          ControlDependency control_dependency = DependsOnlyOnTest)
 104     : LoadPNode(c, mem, adr, at, t, mo, control_dependency) {
 105     init_class_id(Class_LoadBarrierSlowReg);
 106   }
 107 
 108   virtual const char * name() {
 109     return "LoadBarrierSlowRegNode";
 110   }
 111 
 112   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 113     return NULL;
 114   }
 115 
 116   virtual int Opcode() const;
 117 };
 118 
 119 class LoadBarrierWeakSlowRegNode : public LoadPNode {
 120 public:
 121   LoadBarrierWeakSlowRegNode(Node *c,
 122                              Node *mem,
 123                              Node *adr,
 124                              const TypePtr *at,
 125                              const TypePtr* t,
 126                              MemOrd mo,
 127                              ControlDependency control_dependency = DependsOnlyOnTest)
 128     : LoadPNode(c, mem, adr, at, t, mo, control_dependency) {
 129     init_class_id(Class_LoadBarrierWeakSlowReg);
 130   }
 131 
 132   virtual const char * name() {
 133     return "LoadBarrierWeakSlowRegNode";
 134   }
 135 
 136   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 137     return NULL;
 138   }
 139 
 140   virtual int Opcode() const;
 141 };
 142 
 143 class ZBarrierSetC2State : public ResourceObj {
 144 private:
 145   // List of load barrier nodes which need to be expanded before matching
 146   GrowableArray<LoadBarrierNode*>* _load_barrier_nodes;
 147 
 148 public:
 149   ZBarrierSetC2State(Arena* comp_arena);
 150   int load_barrier_count() const;
 151   void add_load_barrier_node(LoadBarrierNode* n);
 152   void remove_load_barrier_node(LoadBarrierNode* n);
 153   LoadBarrierNode* load_barrier_node(int idx) const;
 154 };
 155 
 156 class ZBarrierSetC2 : public BarrierSetC2 {
 157 private:
 158   ZBarrierSetC2State* state() const;
 159   Node* make_cas_loadbarrier(C2AtomicParseAccess& access) const;
 160   Node* make_cmpx_loadbarrier(C2AtomicParseAccess& access) const;
 161   void expand_loadbarrier_basic(PhaseMacroExpand* phase, LoadBarrierNode *barrier) const;
 162   void expand_loadbarrier_node(PhaseMacroExpand* phase, LoadBarrierNode* barrier) const;
 163   void expand_loadbarrier_optimized(PhaseMacroExpand* phase, LoadBarrierNode *barrier) const;
 164   const TypeFunc* load_barrier_Type() const;
 165 
 166 protected:
 167   virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
 168   virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access,
 169                                                Node* expected_val,
 170                                                Node* new_val,
 171                                                const Type* val_type) const;
 172   virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access,
 173                                                 Node* expected_val,
 174                                                 Node* new_val,
 175                                                 const Type* value_type) const;
 176   virtual Node* atomic_xchg_at_resolved(C2AtomicParseAccess& access,
 177                                         Node* new_val,
 178                                         const Type* val_type) const;
 179 
 180 public:
 181   Node* load_barrier(GraphKit* kit,
 182                      Node* val,
 183                      Node* adr,
 184                      bool weak = false,
 185                      bool writeback = true,
 186                      bool oop_reload_allowed = true) const;
 187 
 188   virtual void* create_barrier_state(Arena* comp_arena) const;
 189   virtual bool has_load_barriers() const { return true; }
 190   virtual bool is_gc_barrier_node(Node* node) const;
 191   virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const { }
 192   virtual void eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const;
 193   virtual void add_users_to_worklist(Unique_Node_List* worklist) const;
 194   virtual void enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const;
 195   virtual void register_potential_barrier_node(Node* node) const;
 196   virtual void unregister_potential_barrier_node(Node* node) const;
 197   virtual bool array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, ArrayCopyPhase phase) const;
 198   virtual Node* step_over_gc_barrier(Node* c) const;
 199   // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
 200   // expanded later, then now is the time to do so.
 201   virtual bool expand_macro_nodes(PhaseMacroExpand* macro) const;
 202 
 203   static void find_dominating_barriers(PhaseIterGVN& igvn);
 204   static void loop_optimize_gc_barrier(PhaseIdealLoop* phase, Node* node, bool last_round);
 205 
 206 #ifdef ASSERT
 207   virtual void verify_gc_barriers(bool post_parse) const;
 208 #endif
 209 };
 210 
 211 #endif // SHARE_GC_Z_C2_ZBARRIERSETC2_HPP