1 /*
  2  * Copyright (c) 2005, 2021, 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 
 25 #ifndef SHARE_OPTO_MACRO_HPP
 26 #define SHARE_OPTO_MACRO_HPP
 27 
 28 #include "opto/phase.hpp"
 29 
 30 class  AllocateNode;
 31 class  AllocateArrayNode;
 32 class  CallNode;
 33 class  SubTypeCheckNode;
 34 class  Node;
 35 class  PhaseIterGVN;
 36 
 37 class PhaseMacroExpand : public Phase {
 38 private:
 39   PhaseIterGVN &_igvn;
 40 
 41 public:
 42   // Helper methods roughly modeled after GraphKit:
 43   Node* basic_plus_adr(Node* base, int offset) {
 44     return (offset == 0)? base: basic_plus_adr(base, MakeConX(offset));
 45   }
 46   Node* basic_plus_adr(Node* base, Node* ptr, int offset) {
 47     return (offset == 0)? ptr: basic_plus_adr(base, ptr, MakeConX(offset));
 48   }
 49   Node* basic_plus_adr(Node* base, Node* offset) {
 50     return basic_plus_adr(base, base, offset);
 51   }
 52   Node* basic_plus_adr(Node* base, Node* ptr, Node* offset) {
 53     Node* adr = new AddPNode(base, ptr, offset);
 54     return transform_later(adr);
 55   }
 56   Node* transform_later(Node* n) {
 57     // equivalent to _gvn.transform in GraphKit, Ideal, etc.
 58     _igvn.register_new_node_with_optimizer(n);
 59     return n;
 60   }
 61   Node* make_load( Node* ctl, Node* mem, Node* base, int offset,
 62                    const Type* value_type, BasicType bt);
 63   Node* make_store(Node* ctl, Node* mem, Node* base, int offset,
 64                    Node* value, BasicType bt);
 65 
 66   Node* make_leaf_call(Node* ctrl, Node* mem,
 67                        const TypeFunc* call_type, address call_addr,
 68                        const char* call_name,
 69                        const TypePtr* adr_type,
 70                        Node* parm0 = NULL, Node* parm1 = NULL,
 71                        Node* parm2 = NULL, Node* parm3 = NULL,
 72                        Node* parm4 = NULL, Node* parm5 = NULL,
 73                        Node* parm6 = NULL, Node* parm7 = NULL);
 74 
 75   address basictype2arraycopy(BasicType t,
 76                               Node* src_offset,
 77                               Node* dest_offset,
 78                               bool disjoint_bases,
 79                               const char* &name,
 80                               bool dest_uninitialized);
 81 
 82 private:
 83   // projections extracted from a call node
 84   CallProjections* _callprojs;
 85 
 86   // Additional data collected during macro expansion
 87   bool _has_locks;
 88 
 89   void expand_allocate(AllocateNode *alloc);
 90   void expand_allocate_array(AllocateArrayNode *alloc);
 91   void expand_allocate_common(AllocateNode* alloc,
 92                               Node* length,
 93                               const TypeFunc* slow_call_type,
 94                               address slow_call_address);
 95   void yank_initalize_node(InitializeNode* node);
 96   void yank_alloc_node(AllocateNode* alloc);
 97   Node *value_from_mem(Node *mem, Node *ctl, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc);
 98   Node *value_from_mem_phi(Node *mem, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc, Node_Stack *value_phis, int level);
 99   Node* inline_type_from_mem(Node* mem, Node* ctl, ciInlineKlass* vk, const TypeAryPtr* adr_type, int offset, AllocateNode* alloc);
100 
101   bool eliminate_boxing_node(CallStaticJavaNode *boxing);
102   bool eliminate_allocate_node(AllocateNode *alloc);
103   bool can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints);
104   bool scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints_done);
105   void process_users_of_allocation(CallNode *alloc, bool inline_alloc = false);
106 
107   void eliminate_gc_barrier(Node *p2x);
108   void mark_eliminated_box(Node* box, Node* obj);
109   void mark_eliminated_locking_nodes(AbstractLockNode *alock);
110   bool eliminate_locking_node(AbstractLockNode *alock);
111   void expand_lock_node(LockNode *lock);
112   void inline_type_guard(Node** ctrl, LockNode* lock);
113   void expand_unlock_node(UnlockNode *unlock);
114   void expand_mh_intrinsic_return(CallStaticJavaNode* call);
115 
116   // More helper methods modeled after GraphKit for array copy
117   void insert_mem_bar(Node** ctrl, Node** mem, int opcode, Node* precedent = NULL);
118   Node* array_element_address(Node* ary, Node* idx, BasicType elembt);
119   Node* ConvI2L(Node* offset);
120 
121   // helper methods modeled after LibraryCallKit for array copy
122   Node* generate_guard(Node** ctrl, Node* test, RegionNode* region, float true_prob);
123   Node* generate_slow_guard(Node** ctrl, Node* test, RegionNode* region);
124   Node* generate_fair_guard(Node** ctrl, Node* test, RegionNode* region);
125 
126   void generate_partial_inlining_block(Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type,
127                                        RegionNode** exit_block, Node** result_memory, Node* length,
128                                        Node* src_start, Node* dst_start, BasicType type);
129 
130   void generate_negative_guard(Node** ctrl, Node* index, RegionNode* region);
131   void generate_limit_guard(Node** ctrl, Node* offset, Node* subseq_length, Node* array_length, RegionNode* region);
132 
133   // More helper methods for array copy
134   Node* generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative);
135   Node* array_lh_test(Node* array, jint mask);
136   Node* generate_flat_array_guard(Node** ctrl, Node* array, RegionNode* region);
137   Node* generate_null_free_array_guard(Node** ctrl, Node* array, RegionNode* region);
138 
139   void finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type);
140   Node* generate_arraycopy(ArrayCopyNode *ac,
141                            AllocateArrayNode* alloc,
142                            Node** ctrl, MergeMemNode* mem, Node** io,
143                            const TypePtr* adr_type,
144                            BasicType basic_elem_type,
145                            Node* src,  Node* src_offset,
146                            Node* dest, Node* dest_offset,
147                            Node* copy_length,
148                            Node* dest_length,
149                            bool disjoint_bases = false,
150                            bool length_never_negative = false,
151                            RegionNode* slow_region = NULL);
152   void generate_clear_array(Node* ctrl, MergeMemNode* merge_mem,
153                             const TypePtr* adr_type,
154                             Node* dest,
155                             Node* val,
156                             Node* raw_val,
157                             BasicType basic_elem_type,
158                             Node* slice_idx,
159                             Node* slice_len,
160                             Node* dest_size);
161   bool generate_block_arraycopy(Node** ctrl, MergeMemNode** mem, Node* io,
162                                 const TypePtr* adr_type,
163                                 BasicType basic_elem_type,
164                                 AllocateNode* alloc,
165                                 Node* src,  Node* src_offset,
166                                 Node* dest, Node* dest_offset,
167                                 Node* dest_size, bool dest_uninitialized);
168   MergeMemNode* generate_slow_arraycopy(ArrayCopyNode *ac,
169                                         Node** ctrl, Node* mem, Node** io,
170                                         const TypePtr* adr_type,
171                                         Node* src,  Node* src_offset,
172                                         Node* dest, Node* dest_offset,
173                                         Node* copy_length, bool dest_uninitialized);
174   Node* generate_checkcast_arraycopy(Node** ctrl, MergeMemNode** mem,
175                                      const TypePtr* adr_type,
176                                      Node* dest_elem_klass,
177                                      Node* src,  Node* src_offset,
178                                      Node* dest, Node* dest_offset,
179                                      Node* copy_length, bool dest_uninitialized);
180   Node* generate_generic_arraycopy(Node** ctrl, MergeMemNode** mem,
181                                    const TypePtr* adr_type,
182                                    Node* src,  Node* src_offset,
183                                    Node* dest, Node* dest_offset,
184                                    Node* copy_length, bool dest_uninitialized);
185   bool generate_unchecked_arraycopy(Node** ctrl, MergeMemNode** mem,
186                                     const TypePtr* adr_type,
187                                     BasicType basic_elem_type,
188                                     bool disjoint_bases,
189                                     Node* src,  Node* src_offset,
190                                     Node* dest, Node* dest_offset,
191                                     Node* copy_length, bool dest_uninitialized);
192   const TypePtr* adjust_for_flat_array(const TypeAryPtr* top_dest, Node*& src_offset,
193                                        Node*& dest_offset, Node*& length, BasicType& dest_elem,
194                                        Node*& dest_length);
195   void expand_arraycopy_node(ArrayCopyNode *ac);
196 
197   void expand_subtypecheck_node(SubTypeCheckNode *check);
198 
199   void expand_flatarraycheck_node(FlatArrayCheckNode* check);
200 
201   int replace_input(Node *use, Node *oldref, Node *newref);
202   void migrate_outs(Node *old, Node *target);
203   Node* opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path = false);
204   void copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call);
205   CallNode* make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type, address slow_call,
206                            const char* leaf_name, Node* slow_path, Node* parm0, Node* parm1,
207                            Node* parm2);
208 
209   Node* initialize_object(AllocateNode* alloc,
210                           Node* control, Node* rawmem, Node* object,
211                           Node* klass_node, Node* length,
212                           Node* size_in_bytes);
213 
214   Node* make_arraycopy_load(ArrayCopyNode* ac, intptr_t offset, Node* ctl, Node* mem, BasicType ft, const Type *ftype, AllocateNode *alloc);
215 
216 public:
217   PhaseMacroExpand(PhaseIterGVN &igvn) : Phase(Macro_Expand), _igvn(igvn), _has_locks(false) {
218     _igvn.set_delay_transform(true);
219   }
220   void eliminate_macro_nodes();
221   bool expand_macro_nodes();
222 
223   PhaseIterGVN &igvn() const { return _igvn; }
224 
225 #ifndef PRODUCT
226     static int _objs_scalar_replaced_counter;
227     static int _monitor_objects_removed_counter;
228     static int _GC_barriers_removed_counter;
229     static int _memory_barriers_removed_counter;
230     static void print_statistics();
231     static int count_MemBar(Compile *C);
232 #endif
233 
234   // Members accessed from BarrierSetC2
235   void replace_node(Node* source, Node* target) { _igvn.replace_node(source, target); }
236   Node* intcon(jint con)        const { return _igvn.intcon(con); }
237   Node* longcon(jlong con)      const { return _igvn.longcon(con); }
238   Node* makecon(const Type *t)  const { return _igvn.makecon(t); }
239   Node* top()                   const { return C->top(); }
240 
241   Node* prefetch_allocation(Node* i_o,
242                             Node*& needgc_false, Node*& contended_phi_rawmem,
243                             Node* old_eden_top, Node* new_eden_top,
244                             intx lines);
245   void expand_dtrace_alloc_probe(AllocateNode* alloc, Node* fast_oop, Node*&fast_oop_ctrl, Node*&fast_oop_rawmem);
246   void expand_initialize_membar(AllocateNode* alloc, InitializeNode* init, Node*&fast_oop_ctrl, Node*&fast_oop_rawmem);
247 };
248 
249 #endif // SHARE_OPTO_MACRO_HPP