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