1 /*
   2  * Copyright (c) 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_VM_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP
  26 
  27 #include "gc/shared/c2/barrierSetC2.hpp"
  28 #include "gc/shenandoah/c2/shenandoahSupport.hpp"
  29 #include "utilities/growableArray.hpp"
  30 
  31 class ShenandoahBarrierSetC2State : public ResourceObj {
  32 private:
  33   GrowableArray<ShenandoahWriteBarrierNode*>* _shenandoah_barriers;
  34 
  35 public:
  36   ShenandoahBarrierSetC2State(Arena* comp_arena);
  37   int shenandoah_barriers_count() const;
  38   ShenandoahWriteBarrierNode* shenandoah_barrier(int idx) const;
  39   void add_shenandoah_barrier(ShenandoahWriteBarrierNode * n);
  40   void remove_shenandoah_barrier(ShenandoahWriteBarrierNode * n);
  41 };
  42 
  43 class ShenandoahBarrierSetC2 : public BarrierSetC2 {
  44 private:
  45   void shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const;
  46 
  47   bool satb_can_remove_pre_barrier(GraphKit* kit, PhaseTransform* phase, Node* adr,
  48                                    BasicType bt, uint adr_idx) const;
  49   void satb_write_barrier_pre(GraphKit* kit, bool do_load,
  50                               Node* obj,
  51                               Node* adr,
  52                               uint alias_idx,
  53                               Node* val,
  54                               const TypeOopPtr* val_type,
  55                               Node* pre_val,
  56                               BasicType bt) const;
  57 
  58   void shenandoah_write_barrier_pre(GraphKit* kit,
  59                                     bool do_load,
  60                                     Node* obj,
  61                                     Node* adr,
  62                                     uint alias_idx,
  63                                     Node* val,
  64                                     const TypeOopPtr* val_type,
  65                                     Node* pre_val,
  66                                     BasicType bt) const;
  67 
  68   Node* shenandoah_enqueue_barrier(GraphKit* kit, Node* val) const;
  69   Node* shenandoah_read_barrier(GraphKit* kit, Node* obj) const;
  70   Node* shenandoah_storeval_barrier(GraphKit* kit, Node* obj) const;
  71   Node* shenandoah_write_barrier(GraphKit* kit, Node* obj) const;
  72   Node* shenandoah_read_barrier_impl(GraphKit* kit, Node* obj, bool use_ctrl, bool use_mem, bool allow_fromspace) const;
  73   Node* shenandoah_write_barrier_impl(GraphKit* kit, Node* obj) const;
  74   Node* shenandoah_write_barrier_helper(GraphKit* kit, Node* obj, const TypePtr* adr_type) const;
  75 
  76   void insert_pre_barrier(GraphKit* kit, Node* base_oop, Node* offset,
  77                           Node* pre_val, bool need_mem_bar) const;
  78 
  79 protected:
  80   virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
  81   virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
  82   virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicAccess& access, Node* expected_val,
  83                                                Node* new_val, const Type* val_type) const;
  84   virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicAccess& access, Node* expected_val,
  85                                                 Node* new_val, const Type* value_type) const;
  86   virtual Node* atomic_xchg_at_resolved(C2AtomicAccess& access, Node* new_val, const Type* val_type) const;
  87 
  88 public:
  89   static ShenandoahBarrierSetC2* bsc2();
  90 
  91   static bool is_shenandoah_wb_pre_call(Node* call);
  92   static bool is_shenandoah_wb_call(Node* call);
  93   static bool is_shenandoah_marking_if(PhaseTransform *phase, Node* n);
  94   static bool is_shenandoah_state_load(Node* n);
  95   static bool has_only_shenandoah_wb_pre_uses(Node* n);
  96 
  97   ShenandoahBarrierSetC2State* state() const;
  98 
  99   static const TypeFunc* write_ref_field_pre_entry_Type();
 100   static const TypeFunc* shenandoah_clone_barrier_Type();
 101   static const TypeFunc* shenandoah_write_barrier_Type();
 102 
 103   // This is the entry-point for the backend to perform accesses through the Access API.
 104   virtual Node* store_at(C2Access& access, C2AccessValue& val) const;
 105   virtual Node* load_at(C2Access& access, const Type* val_type) const;
 106 
 107   virtual Node* atomic_cmpxchg_val_at(C2AtomicAccess& access, Node* expected_val,
 108                                       Node* new_val, const Type* val_type) const;
 109   virtual Node* atomic_cmpxchg_bool_at(C2AtomicAccess& access, Node* expected_val,
 110                                        Node* new_val, const Type* val_type) const;
 111   virtual Node* atomic_xchg_at(C2AtomicAccess& access, Node* new_val, const Type* value_type) const;
 112   virtual Node* atomic_add_at(C2AtomicAccess& access, Node* new_val, const Type* value_type) const;
 113 
 114   virtual void clone(GraphKit* kit, Node* src, Node* dst, Node* size, bool is_array) const;
 115 
 116   virtual Node* resolve_for_read(GraphKit* kit, Node* n) const;
 117   virtual Node* resolve_for_write(GraphKit* kit, Node* n) const;
 118 
 119   // These are general helper methods used by C2
 120   virtual bool array_copy_requires_gc_barriers(BasicType type) const;
 121 
 122   // Support for GC barriers emitted during parsing
 123   virtual bool is_gc_barrier_node(Node* node) const;
 124   virtual Node* step_over_gc_barrier(Node* c) const;
 125 
 126   // Support for macro expanded GC barriers
 127   virtual void register_potential_barrier_node(Node* node) const;
 128   virtual void unregister_potential_barrier_node(Node* node) const;
 129   virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const;
 130   virtual void enqueue_useful_gc_barrier(Unique_Node_List &worklist, Node* node) const;
 131   virtual void eliminate_useless_gc_barriers(Unique_Node_List &useful) const;
 132   virtual void add_users_to_worklist(Unique_Node_List* worklist) const;
 133 
 134   // Allow barrier sets to have shared state that is preserved across a compilation unit.
 135   // This could for example comprise macro nodes to be expanded during macro expansion.
 136   virtual void* create_barrier_state(Arena* comp_arena) const;
 137   // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
 138   // expanded later, then now is the time to do so.
 139   virtual bool expand_macro_nodes(PhaseMacroExpand* macro) const;
 140   virtual void verify_gc_barriers(bool post_parse) const;
 141 
 142   virtual Node* ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const;
 143 };
 144 
 145 #endif // SHARE_VM_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP