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
141 while (!stack.is_empty()) {
142 assert(stack.size() > 1, "at least initial + one use");
143 Node* n = stack.node();
144
145 uint current_size = stack.size();
146
147 if (seen.test_set(n->_idx)) {
148 if (to_fix.member(n)) {
149 collect_nodes_to_clone(stack, to_fix);
150 }
151 } else if (n->outcnt() != 0 && n != improved) {
152 if (n->is_Phi()) {
153 Node* region = n->in(0);
154 if (n->req() == region->req()) { // ignore dead phis
155 Node* prev = stack.node_at(stack.size() - 2);
156 for (uint j = 1; j < region->req(); ++j) {
157 if (n->in(j) == prev) {
158 Node* in = region->in(j);
159 if (in != nullptr && !in->is_top() && is_dominator(ctl, in)) {
160 valid_control.set(in->_idx);
161 collect_nodes_to_clone(stack, to_fix);
162 }
163 }
164 }
165 }
166 } else if (n->is_CFG()) {
167 if (is_dominator(ctl, n)) {
168 collect_nodes_to_clone(stack, to_fix);
169 }
170 } else if (n->in(0) != nullptr && n->in(0)->is_CFG()) {
171 Node* c = n->in(0);
172 if (is_dominator(ctl, c)) {
173 collect_nodes_to_clone(stack, to_fix);
174 }
175 } else {
176 uint idx = stack.index();
177 if (idx < n->outcnt()) {
178 stack.set_index(idx + 1);
179 stack.push(n->raw_out(idx), 0);
180 }
181 }
182 }
183 if (stack.size() == current_size) {
184 for (;;) {
185 stack.pop();
186 if (stack.is_empty()) {
187 break;
188 }
189 n = stack.node();
190 uint idx = stack.index();
191 if (idx < n->outcnt()) {
192 stack.set_index(idx + 1);
193 stack.push(n->raw_out(idx), 0);
194 break;
195 }
196 }
197 }
198 }
199 }
200 if (to_fix.size() > 0) {
201 uint hash_table_size = _replaced_nodes->length();
202 for (uint i = 0; i < to_fix.size(); ++i) {
203 Node* n = to_fix.at(i);
204 if (n->is_CFG() || n->in(0) != nullptr) { // End of a chain is not cloned
205 continue;
206 }
207 hash_table_size++;
208 }
209 // Map from current node to cloned/replaced node
210 OrigToNewHashtable clones(hash_table_size, hash_table_size);
211 // Record mapping from initial to improved nodes
212 for (int i = 0; i < _replaced_nodes->length(); i++) {
213 ReplacedNode replaced = _replaced_nodes->at(i);
214 Node* initial = replaced.initial();
215 Node* improved = replaced.improved();
216 clones.put(initial, improved);
217 // If initial needs to be cloned but is also improved then there's no need to clone it.
218 if (to_fix.member(initial)) {
219 to_fix.remove(initial);
220 }
221 }
222
223 // Clone nodes and record mapping from current to cloned nodes
224 uint index_before_clone = C->unique();
225 for (uint i = 0; i < to_fix.size(); ++i) {
226 Node* n = to_fix.at(i);
227 if (n->is_CFG() || n->in(0) != nullptr) { // End of a chain
228 continue;
229 }
230 Node* clone = n->clone();
231 bool added = clones.put(n, clone);
232 assert(added, "clone node must be added to mapping");
233 C->initial_gvn()->set_type_bottom(clone);
234 to_fix.map(i, clone); // Update list of nodes with cloned node
235 }
236
237 // Fix edges in cloned nodes and use at the end of the chain
238 for (uint i = 0; i < to_fix.size(); ++i) {
239 Node* n = to_fix.at(i);
240 bool is_in_table = C->initial_gvn()->hash_delete(n);
241 uint updates = 0;
242 for (uint j = 0; j < n->req(); ++j) {
243 Node* in = n->in(j);
244 if (in == nullptr || (n->is_Phi() && n->in(0)->in(j) == nullptr)) {
245 continue;
246 }
247 if (n->is_Phi() && !valid_control.test(n->in(0)->in(j)->_idx)) {
248 continue;
249 }
250 Node** clone_ptr = clones.get(in);
251 if (clone_ptr != nullptr) {
252 Node* clone = *clone_ptr;
253 n->set_req(j, clone);
254 if (n->_idx < index_before_clone) {
255 PhaseIterGVN::add_users_of_use_to_worklist(clone, n, *C->igvn_worklist());
256 }
257 updates++;
258 }
259 }
260 assert(updates > 0, "");
261 C->record_for_igvn(n);
262 if (is_in_table) {
263 C->initial_gvn()->hash_find_insert(n);
264 }
265 }
266 }
267 }
268
269 bool ReplacedNodes::is_dominator(const Node* ctl, Node* n) const {
270 assert(n->is_CFG(), "should be CFG now");
271 int depth = 0;
272 while (n != ctl) {
273 n = IfNode::up_one_dom(n);
274 depth++;
275 // limit search depth
276 if (depth >= 100 || n == nullptr) {
277 return false;
278 }
279 }
280 return true;
281 }
282
283 void ReplacedNodes::dump(outputStream *st) const {
284 if (!is_empty()) {
285 st->print("replaced nodes: ");
286 for (int i = 0; i < _replaced_nodes->length(); i++) {
287 st->print("%d->%d", _replaced_nodes->at(i).initial()->_idx, _replaced_nodes->at(i).improved()->_idx);
288 if (i < _replaced_nodes->length()-1) {
289 st->print(",");
290 }
291 }
292 }
293 }
294
295 // Merge 2 list of replaced node at a point where control flow paths merge
296 void ReplacedNodes::merge_with(const ReplacedNodes& other) {
297 if (is_empty()) {
298 return;
299 }
300 if (other.is_empty()) {
301 reset();
302 return;
303 }
304 int shift = 0;
305 int len = _replaced_nodes->length();
306 for (int i = 0; i < len; i++) {
307 if (!other.has_node(_replaced_nodes->at(i))) {
308 shift++;
309 } else if (shift > 0) {
310 _replaced_nodes->at_put(i-shift, _replaced_nodes->at(i));
311 }
312 }
313 if (shift > 0) {
314 _replaced_nodes->trunc_to(len - shift);
315 }
316 }
317
318 void ReplacedNodes::collect_nodes_to_clone(const Node_Stack& stack, Unique_Node_List& to_fix) {
319 for (uint i = stack.size() - 1; i >= 1; i--) {
320 Node* n = stack.node_at(i);
321 to_fix.push(n);
322 }
323 }