1 /*
  2  * Copyright (c) 2018, 2021, Red Hat, Inc. 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 
 25 #ifndef SHARE_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP
 26 #define SHARE_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP
 27 
 28 #include "gc/shared/c2/barrierSetC2.hpp"
 29 #include "gc/shenandoah/c2/shenandoahSupport.hpp"
 30 #include "utilities/growableArray.hpp"
 31 
 32 class ShenandoahBarrierSetC2State : public ArenaObj {
 33 private:
 34   GrowableArray<ShenandoahIUBarrierNode*>* _iu_barriers;
 35   GrowableArray<ShenandoahLoadReferenceBarrierNode*>* _load_reference_barriers;
 36 
 37 public:
 38   ShenandoahBarrierSetC2State(Arena* comp_arena);
 39 
 40   int iu_barriers_count() const;
 41   ShenandoahIUBarrierNode* iu_barrier(int idx) const;
 42   void add_iu_barrier(ShenandoahIUBarrierNode* n);
 43   void remove_iu_barrier(ShenandoahIUBarrierNode * n);
 44 
 45   int load_reference_barriers_count() const;
 46   ShenandoahLoadReferenceBarrierNode* load_reference_barrier(int idx) const;
 47   void add_load_reference_barrier(ShenandoahLoadReferenceBarrierNode* n);
 48   void remove_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n);
 49 };
 50 
 51 class ShenandoahBarrierSetC2 : public BarrierSetC2 {
 52 private:
 53   void shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const;
 54 
 55   bool satb_can_remove_pre_barrier(GraphKit* kit, PhaseValues* phase, Node* adr,
 56                                    BasicType bt, uint adr_idx) const;
 57   void satb_write_barrier_pre(GraphKit* kit, bool do_load,
 58                               Node* obj,
 59                               Node* adr,
 60                               uint alias_idx,
 61                               Node* val,
 62                               const TypeOopPtr* val_type,
 63                               Node* pre_val,
 64                               BasicType bt) const;
 65 
 66   void shenandoah_write_barrier_pre(GraphKit* kit,
 67                                     bool do_load,
 68                                     Node* obj,
 69                                     Node* adr,
 70                                     uint alias_idx,
 71                                     Node* val,
 72                                     const TypeOopPtr* val_type,
 73                                     Node* pre_val,
 74                                     BasicType bt) const;
 75 
 76   Node* shenandoah_iu_barrier(GraphKit* kit, Node* obj) const;
 77 
 78   Node* byte_map_base_node(GraphKit* kit) const;
 79 
 80   void post_barrier(GraphKit* kit,
 81                     Node* ctl,
 82                     Node* store,
 83                     Node* obj,
 84                     Node* adr,
 85                     uint adr_idx,
 86                     Node* val,
 87                     BasicType bt,
 88                     bool use_precise) const;
 89 
 90   void insert_pre_barrier(GraphKit* kit, Node* base_oop, Node* offset,
 91                           Node* pre_val, bool need_mem_bar) const;
 92 
 93   static bool clone_needs_barrier(Node* src, PhaseGVN& gvn);
 94 
 95 protected:
 96   virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
 97   virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
 98   virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
 99                                                Node* new_val, const Type* val_type) const;
100   virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
101                                                 Node* new_val, const Type* value_type) const;
102   virtual Node* atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
103 
104 public:
105   static ShenandoahBarrierSetC2* bsc2();
106 
107   static bool is_shenandoah_wb_pre_call(Node* call);
108   static bool is_shenandoah_clone_call(Node* call);
109   static bool is_shenandoah_lrb_call(Node* call);
110   static bool is_shenandoah_marking_if(PhaseValues* phase, Node* n);
111   static bool is_shenandoah_state_load(Node* n);
112   static bool has_only_shenandoah_wb_pre_uses(Node* n);
113 
114   ShenandoahBarrierSetC2State* state() const;
115 
116   static const TypeFunc* write_ref_field_pre_entry_Type();
117   static const TypeFunc* shenandoah_clone_barrier_Type();
118   static const TypeFunc* shenandoah_load_reference_barrier_Type();
119   virtual bool has_load_barrier_nodes() const { return true; }
120 
121   // This is the entry-point for the backend to perform accesses through the Access API.
122   virtual void clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const;
123 
124   // These are general helper methods used by C2
125   virtual bool array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const;
126 
127   // Support for GC barriers emitted during parsing
128   virtual bool is_gc_pre_barrier_node(Node* node) const;
129   virtual bool is_gc_barrier_node(Node* node) const;
130   virtual Node* step_over_gc_barrier(Node* c) const;
131   virtual bool expand_barriers(Compile* C, PhaseIterGVN& igvn) const;
132   virtual bool optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const;
133   virtual bool strip_mined_loops_expanded(LoopOptsMode mode) const { return mode == LoopOptsShenandoahExpand || mode == LoopOptsShenandoahPostExpand; }
134   virtual bool is_gc_specific_loop_opts_pass(LoopOptsMode mode) const { return mode == LoopOptsShenandoahExpand || mode == LoopOptsShenandoahPostExpand; }
135 
136   // Support for macro expanded GC barriers
137   virtual void register_potential_barrier_node(Node* node) const;
138   virtual void unregister_potential_barrier_node(Node* node) const;
139   virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const;
140   virtual void enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const;
141   virtual void eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const;
142 
143   // Allow barrier sets to have shared state that is preserved across a compilation unit.
144   // This could for example comprise macro nodes to be expanded during macro expansion.
145   virtual void* create_barrier_state(Arena* comp_arena) const;
146   // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
147   // expanded later, then now is the time to do so.
148   virtual bool expand_macro_nodes(PhaseMacroExpand* macro) const;
149 
150 #ifdef ASSERT
151   virtual void verify_gc_barriers(Compile* compile, CompilePhase phase) const;
152 #endif
153 
154   virtual Node* ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const;
155   virtual bool final_graph_reshaping(Compile* compile, Node* n, uint opcode, Unique_Node_List& dead_nodes) const;
156 
157   virtual bool escape_add_to_con_graph(ConnectionGraph* conn_graph, PhaseGVN* gvn, Unique_Node_List* delayed_worklist, Node* n, uint opcode) const;
158   virtual bool escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const;
159   virtual bool escape_has_out_with_unsafe_object(Node* n) const;
160 
161   virtual bool matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const;
162   virtual bool matcher_is_store_load_barrier(Node* x, uint xop) const;
163 };
164 
165 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP