1 /*
  2  * Copyright (c) 2016, 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 #include "memory/resourceArea.hpp"
 26 #include "opto/cfgnode.hpp"
 27 #include "opto/phaseX.hpp"
 28 #include "opto/replacednodes.hpp"
 29 
 30 void ReplacedNodes::allocate_if_necessary() {
 31   if (_replaced_nodes == nullptr) {
 32     _replaced_nodes = new GrowableArray<ReplacedNode>();
 33   }
 34 }
 35 
 36 bool ReplacedNodes::is_empty() const {
 37   return _replaced_nodes == nullptr || _replaced_nodes->length() == 0;
 38 }
 39 
 40 bool ReplacedNodes::has_node(const ReplacedNode& r) const {
 41   return _replaced_nodes->find(r) != -1;
 42 }
 43 
 44 bool ReplacedNodes::has_target_node(Node* n) const {
 45   for (int i = 0; i < _replaced_nodes->length(); i++) {
 46     if (_replaced_nodes->at(i).improved() == n) {
 47       return true;
 48     }
 49   }
 50   return false;
 51 }
 52 
 53 // Record replaced node if not seen before
 54 void ReplacedNodes::record(Node* initial, Node* improved) {
 55   allocate_if_necessary();
 56   ReplacedNode r(initial, improved);
 57   if (!has_node(r)) {
 58     _replaced_nodes->push(r);
 59   }
 60 }
 61 
 62 // Copy replaced nodes from one map to another. idx is used to
 63 // identify nodes that are too new to be of interest in the target
 64 // node list.
 65 void ReplacedNodes::transfer_from(const ReplacedNodes& other, uint idx) {
 66   if (other.is_empty()) {
 67     return;
 68   }
 69   allocate_if_necessary();
 70   for (int i = 0; i < other._replaced_nodes->length(); i++) {
 71     ReplacedNode replaced = other._replaced_nodes->at(i);
 72     // Only transfer the nodes that can actually be useful
 73     if (!has_node(replaced) && (replaced.initial()->_idx < idx || has_target_node(replaced.initial()))) {
 74       _replaced_nodes->push(replaced);
 75     }
 76   }
 77 }
 78 
 79 void ReplacedNodes::clone() {
 80   if (_replaced_nodes != nullptr) {
 81     GrowableArray<ReplacedNode>* replaced_nodes_clone = new GrowableArray<ReplacedNode>();
 82     replaced_nodes_clone->appendAll(_replaced_nodes);
 83     _replaced_nodes = replaced_nodes_clone;
 84   }
 85 }
 86 
 87 void ReplacedNodes::reset() {
 88   if (_replaced_nodes != nullptr) {
 89     _replaced_nodes->clear();
 90   }
 91 }
 92 
 93 // Perform node replacement (used when returning to caller)
 94 void ReplacedNodes::apply(Node* n, uint idx) {
 95   if (is_empty()) {
 96     return;
 97   }
 98   for (int i = 0; i < _replaced_nodes->length(); i++) {
 99     ReplacedNode replaced = _replaced_nodes->at(i);
100     // Only apply if improved node was created in a callee to avoid
101     // issues with irreducible loops in the caller
102     if (replaced.improved()->_idx >= idx) {
103       n->replace_edge(replaced.initial(), replaced.improved());
104     }
105   }
106 }
107 
108 // Perform node replacement following late inlining.
109 void ReplacedNodes::apply(Compile* C, Node* ctl) {
110   // ctl is the control on exit of the method that was late inlined
111   if (is_empty()) {
112     return;
113   }
114   ResourceMark rm;
115   Node_Stack stack(0);
116   Unique_Node_List to_fix; // nodes to clone + uses at the end of the chain that need to updated
117   VectorSet seen;
118   VectorSet valid_control;
119 
120   for (int i = 0; i < _replaced_nodes->length(); i++) {
121     ReplacedNode replaced = _replaced_nodes->at(i);
122     Node* initial = replaced.initial();
123     Node* improved = replaced.improved();
124     assert (ctl != nullptr && !ctl->is_top(), "replaced node should have actual control");
125 
126     if (initial->outcnt() == 0) {
127       continue;
128     }
129 
130     // Find uses of initial that are dominated by ctl so, initial can be replaced by improved.
131     // Proving domination here is not straightforward. To do so, we follow uses of initial, and uses of uses until we
132     // encounter a node which is a control node or is pinned at some control. Then, we try to prove this control is
133     // dominated by ctl. If that's the case, it's legal to replace initial by improved but for this chain of uses only.
134     // It may not be the case for some other chain of uses, so we clone that chain and perform the replacement only for
135     // these uses.
136     assert(stack.is_empty(), "");
137     stack.push(initial, 1);
138     Node* use = initial->raw_out(0);
139     stack.push(use, 0);
140     while (!stack.is_empty()) {
141       assert(stack.size() > 1, "at least initial + one use");
142       Node* n = stack.node();
143 
144       uint current_size = stack.size();
145 
146       if (seen.test_set(n->_idx)) {
147         if (to_fix.member(n)) {
148           collect_nodes_to_clone(stack, to_fix);
149         }
150       } else if (n->outcnt() != 0 && n != improved) {
151         if (n->is_Phi()) {
152           Node* region = n->in(0);
153           if (n->req() == region->req()) { // ignore dead phis
154             Node* prev = stack.node_at(stack.size() - 2);
155             for (uint j = 1; j < region->req(); ++j) {
156               if (n->in(j) == prev) {
157                 Node* in = region->in(j);
158                 if (in != nullptr && !in->is_top() && is_dominator(ctl, in)) {
159                   valid_control.set(in->_idx);
160                   collect_nodes_to_clone(stack, to_fix);
161                 }
162               }
163             }
164           }
165         } else if (n->is_CFG()) {
166           if (is_dominator(ctl, n)) {
167             collect_nodes_to_clone(stack, to_fix);
168           }
169         } else if (n->in(0) != nullptr && n->in(0)->is_CFG()) {
170           Node* c = n->in(0);
171           if (is_dominator(ctl, c)) {
172             collect_nodes_to_clone(stack, to_fix);
173           }
174         } else {
175           uint idx = stack.index();
176           if (idx < n->outcnt()) {
177             stack.set_index(idx + 1);
178             stack.push(n->raw_out(idx), 0);
179           }
180         }
181       }
182       if (stack.size() == current_size) {
183         for (;;) {
184           stack.pop();
185           if (stack.is_empty()) {
186             break;
187           }
188           n = stack.node();
189           uint idx = stack.index();
190           if (idx < n->outcnt()) {
191             stack.set_index(idx + 1);
192             stack.push(n->raw_out(idx), 0);
193             break;
194           }
195         }
196       }
197     }
198   }
199   if (to_fix.size() > 0) {
200     uint hash_table_size = _replaced_nodes->length();
201     for (uint i = 0; i < to_fix.size(); ++i) {
202       Node* n = to_fix.at(i);
203       if (n->is_CFG() || n->in(0) != nullptr) { // End of a chain is not cloned
204         continue;
205       }
206       hash_table_size++;
207     }
208     // Map from current node to cloned/replaced node
209     OrigToNewHashtable clones(hash_table_size, hash_table_size);
210     // Record mapping from initial to improved nodes
211     for (int i = 0; i < _replaced_nodes->length(); i++) {
212       ReplacedNode replaced = _replaced_nodes->at(i);
213       Node* initial = replaced.initial();
214       Node* improved = replaced.improved();
215       clones.put(initial, improved);
216       // If initial needs to be cloned but is also improved then there's no need to clone it.
217       if (to_fix.member(initial)) {
218         to_fix.remove(initial);
219       }
220     }
221 
222     // Clone nodes and record mapping from current to cloned nodes
223     uint index_before_clone = C->unique();
224     for (uint i = 0; i < to_fix.size(); ++i) {
225       Node* n = to_fix.at(i);
226       if (n->is_CFG() || n->in(0) != nullptr) { // End of a chain
227         continue;
228       }
229       Node* clone = n->clone();
230       bool added = clones.put(n, clone);
231       assert(added, "clone node must be added to mapping");
232       C->initial_gvn()->set_type_bottom(clone);
233       to_fix.map(i, clone); // Update list of nodes with cloned node
234     }
235 
236     // Fix edges in cloned nodes and use at the end of the chain
237     for (uint i = 0; i < to_fix.size(); ++i) {
238       Node* n = to_fix.at(i);
239       bool is_in_table = C->initial_gvn()->hash_delete(n);
240       uint updates = 0;
241       for (uint j = 0; j < n->req(); ++j) {
242         Node* in = n->in(j);
243         if (in == nullptr || (n->is_Phi() && n->in(0)->in(j) == nullptr)) {
244           continue;
245         }
246         if (n->is_Phi() && !valid_control.test(n->in(0)->in(j)->_idx)) {
247           continue;
248         }
249         Node** clone_ptr = clones.get(in);
250         if (clone_ptr != nullptr) {
251           Node* clone = *clone_ptr;
252           n->set_req(j, clone);
253           if (n->_idx < index_before_clone) {
254             PhaseIterGVN::add_users_of_use_to_worklist(clone, n, *C->igvn_worklist());
255           }
256           updates++;
257         }
258       }
259       assert(updates > 0, "");
260       C->record_for_igvn(n);
261       if (is_in_table) {
262         C->initial_gvn()->hash_find_insert(n);
263       }
264     }
265   }
266 }
267 
268 bool ReplacedNodes::is_dominator(const Node* ctl, Node* n) const {
269   assert(n->is_CFG(), "should be CFG now");
270   int depth = 0;
271   while (n != ctl) {
272     n = IfNode::up_one_dom(n);
273     depth++;
274     // limit search depth
275     if (depth >= 100 || n == nullptr) {
276       return false;
277     }
278   }
279   return true;
280 }
281 
282 void ReplacedNodes::dump(outputStream *st) const {
283   if (!is_empty()) {
284     st->print("replaced nodes: ");
285     for (int i = 0; i < _replaced_nodes->length(); i++) {
286       st->print("%d->%d", _replaced_nodes->at(i).initial()->_idx, _replaced_nodes->at(i).improved()->_idx);
287       if (i < _replaced_nodes->length()-1) {
288         st->print(",");
289       }
290     }
291   }
292 }
293 
294 // Merge 2 list of replaced node at a point where control flow paths merge
295 void ReplacedNodes::merge_with(const ReplacedNodes& other) {
296   if (is_empty()) {
297     return;
298   }
299   if (other.is_empty()) {
300     reset();
301     return;
302   }
303   int shift = 0;
304   int len = _replaced_nodes->length();
305   for (int i = 0; i < len; i++) {
306     if (!other.has_node(_replaced_nodes->at(i))) {
307       shift++;
308     } else if (shift > 0) {
309       _replaced_nodes->at_put(i-shift, _replaced_nodes->at(i));
310     }
311   }
312   if (shift > 0) {
313     _replaced_nodes->trunc_to(len - shift);
314   }
315 }
316 
317 void ReplacedNodes::collect_nodes_to_clone(const Node_Stack& stack, Unique_Node_List& to_fix) {
318   for (uint i = stack.size() - 1; i >= 1; i--) {
319     Node* n = stack.node_at(i);
320     to_fix.push(n);
321   }
322 }