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     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 }