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