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