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 
 106   virtual const char * name() {
 107     return "LoadBarrierSlowRegNode";
 108   }
 109 
 110   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 111     return NULL;
 112   }
 113 
 114   virtual int Opcode() const;
 115 };
 116 
 117 class LoadBarrierWeakSlowRegNode : public LoadPNode {
 118 public:
 119   LoadBarrierWeakSlowRegNode(Node *c,
 120                              Node *mem,
 121                              Node *adr,
 122                              const TypePtr *at,
 123                              const TypePtr* t,
 124                              MemOrd mo,
 125                              ControlDependency control_dependency = DependsOnlyOnTest)
 126     : LoadPNode(c, mem, adr, at, t, mo, control_dependency) {}
 127 
 128   virtual const char * name() {
 129     return "LoadBarrierWeakSlowRegNode";
 130   }
 131 
 132   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape) {
 133     return NULL;
 134   }
 135 
 136   virtual int Opcode() const;
 137 };
 138 
 139 class ZBarrierSetC2State : public ResourceObj {
 140 private:
 141   // List of load barrier nodes which need to be expanded before matching
 142   GrowableArray<LoadBarrierNode*>* _load_barrier_nodes;
 143 
 144 public:
 145   ZBarrierSetC2State(Arena* comp_arena);
 146   int load_barrier_count() const;
 147   void add_load_barrier_node(LoadBarrierNode* n);
 148   void remove_load_barrier_node(LoadBarrierNode* n);
 149   LoadBarrierNode* load_barrier_node(int idx) const;
 150 };
 151 
 152 class ZBarrierSetC2 : public BarrierSetC2 {
 153 private:
 154   ZBarrierSetC2State* state() const;
 155   Node* make_cas_loadbarrier(C2AtomicAccess& access) const;
 156   Node* make_cmpx_loadbarrier(C2AtomicAccess& access) const;
 157   void expand_loadbarrier_basic(PhaseMacroExpand* phase, LoadBarrierNode *barrier) const;
 158   void expand_loadbarrier_node(PhaseMacroExpand* phase, LoadBarrierNode* barrier) const;
 159   void expand_loadbarrier_optimized(PhaseMacroExpand* phase, LoadBarrierNode *barrier) const;
 160   const TypeFunc* load_barrier_Type() const;
 161 
 162 protected:
 163   virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
 164   virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicAccess& access,
 165                                                Node* expected_val,
 166                                                Node* new_val,
 167                                                const Type* val_type) const;
 168   virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicAccess& access,
 169                                                 Node* expected_val,
 170                                                 Node* new_val,
 171                                                 const Type* value_type) const;
 172   virtual Node* atomic_xchg_at_resolved(C2AtomicAccess& access,
 173                                         Node* new_val,
 174                                         const Type* val_type) const;
 175 
 176 public:
 177   Node* load_barrier(GraphKit* kit,
 178                      Node* val,
 179                      Node* adr,
 180                      bool weak = false,
 181                      bool writeback = true,
 182                      bool oop_reload_allowed = true) const;
 183 
 184   virtual void* create_barrier_state(Arena* comp_arena) const;
 185   virtual bool is_gc_barrier_node(Node* node) const;
 186   virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const { }
 187   virtual void eliminate_useless_gc_barriers(Unique_Node_List &useful) const;
 188   virtual void add_users_to_worklist(Unique_Node_List* worklist) const;
 189   virtual void enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const;
 190   virtual void register_potential_barrier_node(Node* node) const;
 191   virtual void unregister_potential_barrier_node(Node* node) const;
 192   virtual bool array_copy_requires_gc_barriers(BasicType type) const { return true; }
 193   virtual Node* step_over_gc_barrier(Node* c) const { return c; }
 194   // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
 195   // expanded later, then now is the time to do so.
 196   virtual bool expand_macro_nodes(PhaseMacroExpand* macro) const;
 197 
 198   static void find_dominating_barriers(PhaseIterGVN& igvn);
 199   static void loop_optimize_gc_barrier(PhaseIdealLoop* phase, Node* node, bool last_round);
 200 
 201 #ifdef ASSERT
 202   virtual void verify_gc_barriers(bool post_parse) const;
 203 #endif
 204 };
 205 
 206 #endif // SHARE_GC_Z_C2_ZBARRIERSETC2_HPP