1 /*
2 * Copyright (c) 1997, 2026, 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_MULTNODE_HPP
26 #define SHARE_OPTO_MULTNODE_HPP
27
28 #include "opto/node.hpp"
29
30 class Matcher;
31 class ProjNode;
32
33 //------------------------------MultiNode--------------------------------------
34 // This class defines a MultiNode, a Node which produces many values. The
35 // values are wrapped up in a tuple Type, i.e. a TypeTuple.
36 class MultiNode : public Node {
37 public:
38 MultiNode( uint required ) : Node(required) {
39 init_class_id(Class_Multi);
40 }
41 virtual int Opcode() const;
42 virtual const Type *bottom_type() const = 0;
43 virtual bool is_CFG() const { return true; }
44 virtual uint hash() const { return NO_HASH; } // CFG nodes do not hash
45 virtual const RegMask &out_RegMask() const;
46 virtual Node *match(const ProjNode *proj, const Matcher *m, const RegMask* mask);
47 virtual uint ideal_reg() const { return NotAMachineReg; }
48 ProjNode* proj_out(uint which_proj) const; // Get a named projection
49 ProjNode* proj_out_or_null(uint which_proj) const;
50 ProjNode* proj_out_or_null(uint which_proj, bool is_io_use) const;
51 uint number_of_projs(uint which_proj) const;
52 uint number_of_projs(uint which_proj, bool is_io_use) const;
53
54 protected:
55
56 // Provide single interface for DUIterator_Fast/DUIterator for template method below
57 class UsesIteratorFast {
58 DUIterator_Fast& _imax;
59 DUIterator_Fast& _i;
60 const Node* _node;
61
62 public:
63 bool cont() {
64 return _i < _imax;
65 }
66 void next() {
67 _i++;
68 }
69 Node* current() {
70 return _node->fast_out(_i);
71 }
72 UsesIteratorFast(DUIterator_Fast& imax, DUIterator_Fast& i, const Node* node)
73 : _imax(imax), _i(i), _node(node) {
74 }
75 };
76
77 class UsesIterator {
78 DUIterator& _i;
79 const Node* _node;
80
81 public:
82 bool cont() {
83 return _node->has_out(_i);
84 }
85 void next() {
86 _i++;
87 }
88 Node* current() {
89 return _node->out(_i);
90 }
91 UsesIterator(DUIterator& i, const Node* node)
92 : _i(i), _node(node) {
93 }
94 };
95
96 // Iterate with i over all Proj uses calling callback
97 template<class Callback, class Iterator> ProjNode* apply_to_projs_any_iterator(Iterator i, Callback callback) const {
98 for (; i.cont(); i.next()) {
99 Node* p = i.current();
100 if (p->is_Proj()) {
101 ProjNode* proj = p->as_Proj();
102 ApplyToProjs result = callback(proj);
103 if (result == BREAK_AND_RETURN_CURRENT_PROJ) {
104 return proj;
105 }
106 assert(result == CONTINUE, "should be either break or continue");
107 } else {
108 assert(p == this && is_Start(), "else must be proj");
109 }
110 }
111 return nullptr;
112 }
113 enum ApplyToProjs {
114 CONTINUE,
115 BREAK_AND_RETURN_CURRENT_PROJ
116 };
117
118 // Run callback on projections with iterator passed as argument
119 template <class Callback> ProjNode* apply_to_projs(DUIterator_Fast& imax, DUIterator_Fast& i, Callback callback, uint which_proj) const;
120
121 // Same but with default iterator and for matching _con
122 template<class Callback> ProjNode* apply_to_projs(Callback callback, uint which_proj) const {
123 DUIterator_Fast imax, i = fast_outs(imax);
124 return apply_to_projs(imax, i, callback, which_proj);
125 }
126
127 // Same but for matching _con and _is_io_use
128 template <class Callback> ProjNode* apply_to_projs(Callback callback, uint which_proj, bool is_io_use) const;
129
130 public:
131 template<class Callback> void for_each_proj(Callback callback, uint which_proj) const {
132 auto callback_always_continue = [&](ProjNode* proj) {
133 callback(proj);
134 return MultiNode::CONTINUE;
135 };
136 apply_to_projs(callback_always_continue, which_proj);
137 }
138
139 template <class Callback> void for_each_proj(Callback callback, uint which_proj, bool is_io_use) const {
140 auto callback_always_continue = [&](ProjNode* proj) {
141 callback(proj);
142 return MultiNode::CONTINUE;
143 };
144 apply_to_projs(callback_always_continue, which_proj, is_io_use);
145 }
146
147
148 ProjNode* find_first(uint which_proj) const;
149 ProjNode* find_first(uint which_proj, bool is_io_use) const;
150 };
151
152 class BinaryMultiNode : public MultiNode {
153 protected:
154 BinaryMultiNode(Node* ctrl, Node* in1, Node* in2) : MultiNode(3) {
155 init_req(0, ctrl);
156 init_req(1, in1);
157 init_req(2, in2);
158 }
159
160 public:
161 enum {
162 first_proj_num = 0,
163 second_proj_num = 1
164 };
165
166 virtual Node* Identity(PhaseGVN* phase) { return this; }
167 virtual Node* Ideal(PhaseGVN* phase, bool can_reshape) { return nullptr; }
168 virtual const Type* Value(PhaseGVN* phase) const { return bottom_type(); }
169 virtual uint hash() const { return Node::hash(); }
170 virtual bool is_CFG() const { return false; }
171 virtual uint ideal_reg() const { return NotAMachineReg; }
172
173 ProjNode* first_proj() const { return proj_out_or_null(first_proj_num); }
174 ProjNode* second_proj() const { return proj_out_or_null(second_proj_num); }
175
176 private:
177 virtual bool depends_only_on_test() const { return false; }
178 };
179
180 //------------------------------ProjNode---------------------------------------
181 // This class defines a Projection node. Projections project a single element
182 // out of a tuple (or Signature) type. Only MultiNodes produce TypeTuple
183 // results.
184 class ProjNode : public Node {
185 protected:
186 virtual uint hash() const;
187 virtual bool cmp( const Node &n ) const;
188 virtual uint size_of() const;
189 void check_con() const; // Called from constructor.
190 const Type* proj_type(const Type* t) const;
191
192 public:
193 ProjNode( Node *src, uint con, bool io_use = false )
194 : Node( src ), _con(con), _is_io_use(io_use)
195 {
196 init_class_id(Class_Proj);
197 // Optimistic setting. Need additional checks in Node::is_dead_loop_safe().
198 if (con != TypeFunc::Memory || src->is_Start())
199 init_flags(Flag_is_dead_loop_safe);
200 DEBUG_ONLY(check_con());
201 }
202 const uint _con; // The field in the tuple we are projecting
203 const bool _is_io_use; // Used to distinguish between the projections
204 // used on the control and io paths from a macro node
205 virtual int Opcode() const;
206 virtual bool is_CFG() const;
207 virtual const Type *bottom_type() const;
208 virtual const TypePtr *adr_type() const;
209 virtual bool pinned() const;
210 virtual Node* Identity(PhaseGVN* phase);
211 virtual const Type* Value(PhaseGVN* phase) const;
212 virtual uint ideal_reg() const;
213 virtual const RegMask &out_RegMask() const;
214
215 #ifndef PRODUCT
216 virtual void dump_spec(outputStream *st) const;
217 virtual void dump_compact_spec(outputStream *st) const;
218 #endif
219
220 // Return uncommon trap call node if proj is for "proj->[region->..]call_uct"
221 // null otherwise
222 CallStaticJavaNode* is_uncommon_trap_proj(Deoptimization::DeoptReason reason = Deoptimization::Reason_none) const;
223 // Return uncommon trap call node for "if(test)-> proj -> ...
224 // |
225 // V
226 // other_proj->[region->..]call_uct"
227 // null otherwise
228 CallStaticJavaNode* is_uncommon_trap_if_pattern(Deoptimization::DeoptReason reason = Deoptimization::Reason_none) const;
229 };
230
231 // A ProjNode variant that captures an adr_type(). Used as a projection of InitializeNode to have the right adr_type()
232 // for array elements/fields.
233 class NarrowMemProjNode : public ProjNode {
234 private:
235 const TypePtr* const _adr_type;
236 protected:
237 virtual uint hash() const {
238 return ProjNode::hash() + _adr_type->hash();
239 }
240 virtual bool cmp(const Node& n) const {
241 return ProjNode::cmp(n) && ((NarrowMemProjNode&)n)._adr_type == _adr_type;
242 }
243 virtual uint size_of() const {
244 return sizeof(*this);
245 }
246 public:
247 NarrowMemProjNode(InitializeNode* src, const TypePtr* adr_type);
248
249 virtual const TypePtr* adr_type() const {
250 return _adr_type;
251 }
252
253 virtual int Opcode() const;
254 };
255
256 template <class Callback> ProjNode* MultiNode::apply_to_projs(DUIterator_Fast& imax, DUIterator_Fast& i, Callback callback, uint which_proj) const {
257 auto filter = [&](ProjNode* proj) {
258 if (proj->_con == which_proj && callback(proj) == BREAK_AND_RETURN_CURRENT_PROJ) {
259 return BREAK_AND_RETURN_CURRENT_PROJ;
260 }
261 return CONTINUE;
262 };
263 return apply_to_projs_any_iterator(UsesIteratorFast(imax, i, this), filter);
264 }
265
266 /* Tuples are used to avoid manual graph surgery. When a node with Proj outputs (such as a call)
267 * must be removed and its ouputs replaced by its input, or some other value, we can make its
268 * ::Ideal return a tuple of what we want for each output: the ::Identity of output Proj will
269 * take care to jump over the Tuple and directly pick up the right input of the Tuple.
270 *
271 * For instance, if a function call is proven to have no side effect and return the constant 0,
272 * we can replace it with the 6-tuple:
273 * (control input, IO input, memory input, frame ptr input, return addr input, Con:0)
274 * all the output projections will pick up the input of the now gone call, except for the result
275 * projection that is replaced by 0.
276 *
277 * Using TupleNode avoid manual graph surgery and leave that to our expert surgeon: IGVN.
278 * Since the user of a Tuple are expected to be Proj, when creating a tuple during idealization,
279 * the output Proj should be enqueued for IGVN immediately after, and the tuple should not survive
280 * after the current IGVN.
281 */
282 class TupleNode : public MultiNode {
283 const TypeTuple* _tf;
284
285 template <typename... NN>
286 static void make_helper(TupleNode* tn, uint i, Node* node, NN... nn) {
287 tn->set_req(i, node);
288 make_helper(tn, i + 1, nn...);
289 }
290
291 static void make_helper(TupleNode*, uint) {}
292
293 public:
294 TupleNode(const TypeTuple* tf) : MultiNode(tf->cnt()), _tf(tf) {}
295
296 int Opcode() const override;
297 const Type* bottom_type() const override { return _tf; }
298
299 /* Give as many `Node*` as you want in the `nn` pack:
300 * TupleNode::make(tf, input1)
301 * TupleNode::make(tf, input1, input2, input3, input4)
302 */
303 template <typename... NN>
304 static TupleNode* make(const TypeTuple* tf, NN... nn) {
305 TupleNode* tn = new TupleNode(tf);
306 make_helper(tn, 0, nn...);
307 return tn;
308 }
309 };
310
311 #endif // SHARE_OPTO_MULTNODE_HPP