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 #include "opto/callnode.hpp"
26 #include "opto/cfgnode.hpp"
27 #include "opto/matcher.hpp"
28 #include "opto/mathexactnode.hpp"
29 #include "opto/multnode.hpp"
30 #include "opto/opcodes.hpp"
31 #include "opto/phaseX.hpp"
32 #include "opto/regmask.hpp"
33 #include "opto/type.hpp"
34 #include "utilities/vmError.hpp"
35
36 //=============================================================================
37 //------------------------------MultiNode--------------------------------------
38 const RegMask &MultiNode::out_RegMask() const {
39 return RegMask::EMPTY;
40 }
41
42 Node *MultiNode::match(const ProjNode *proj, const Matcher *m, const RegMask* mask) { return proj->clone(); }
43
44 //------------------------------proj_out---------------------------------------
45 // Get a named projection or null if not found
46 ProjNode* MultiNode::proj_out_or_null(uint which_proj) const {
47 assert((Opcode() != Op_If && Opcode() != Op_RangeCheck) || which_proj == (uint)true || which_proj == (uint)false, "must be 1 or 0");
48 assert(number_of_projs(which_proj) <= 1, "only when there's a single projection");
49 ProjNode* proj = find_first(which_proj);
50 assert(proj == nullptr || (Opcode() != Op_If && Opcode() != Op_RangeCheck) || proj->Opcode() == (which_proj ? Op_IfTrue : Op_IfFalse),
51 "incorrect projection node at If/RangeCheck: IfTrue on false path or IfFalse on true path");
52 return proj;
53 }
54
55 ProjNode* MultiNode::proj_out_or_null(uint which_proj, bool is_io_use) const {
56 assert(number_of_projs(which_proj, is_io_use) <= 1, "only when there's a single projection");
57 return find_first(which_proj, is_io_use);
58 }
59
60 template<class Callback> ProjNode* MultiNode::apply_to_projs(Callback callback, uint which_proj, bool is_io_use) const {
61 auto filter = [&](ProjNode* proj) {
62 if (proj->_is_io_use == is_io_use && callback(proj) == BREAK_AND_RETURN_CURRENT_PROJ) {
63 return BREAK_AND_RETURN_CURRENT_PROJ;
64 }
65 return CONTINUE;
66 };
67 return apply_to_projs(filter, which_proj);
68 }
69
70 uint MultiNode::number_of_projs(uint which_proj) const {
71 uint cnt = 0;
72 auto count_projs = [&](ProjNode* proj) {
73 cnt++;
74 };
75 for_each_proj(count_projs, which_proj);
76 return cnt;
77 }
78
79 uint MultiNode::number_of_projs(uint which_proj, bool is_io_use) const {
80 uint cnt = 0;
81 auto count_projs = [&](ProjNode* proj) {
82 cnt++;
83 };
84 for_each_proj(count_projs, which_proj, is_io_use);
85 return cnt;
86 }
87
88 ProjNode* MultiNode::find_first(uint which_proj) const {
89 auto find_proj = [&](ProjNode* proj) {
90 return BREAK_AND_RETURN_CURRENT_PROJ;
91 };
92 return apply_to_projs(find_proj, which_proj);
93 }
94
95 ProjNode* MultiNode::find_first(uint which_proj, bool is_io_use) const {
96 auto find_proj = [](ProjNode* proj) {
97 return BREAK_AND_RETURN_CURRENT_PROJ;
98 };
99 return apply_to_projs(find_proj, which_proj, is_io_use);
100 }
101
102 // Get a named projection
103 ProjNode* MultiNode::proj_out(uint which_proj) const {
104 assert((Opcode() != Op_If && Opcode() != Op_RangeCheck) || outcnt() == 2, "bad if #1");
105 ProjNode* p = proj_out_or_null(which_proj);
106 assert(p != nullptr, "named projection %u not found", which_proj);
107 return p;
108 }
109
110 //=============================================================================
111 //------------------------------ProjNode---------------------------------------
112 uint ProjNode::hash() const {
113 // only one input
114 return (uintptr_t)in(TypeFunc::Control) + (_con << 1) + (_is_io_use ? 1 : 0);
115 }
116 bool ProjNode::cmp( const Node &n ) const { return _con == ((ProjNode&)n)._con && ((ProjNode&)n)._is_io_use == _is_io_use; }
117 uint ProjNode::size_of() const { return sizeof(ProjNode); }
118
119 // Test if we propagate interesting control along this projection
120 bool ProjNode::is_CFG() const {
121 Node *def = in(0);
122 return (_con == TypeFunc::Control && def->is_CFG());
123 }
124
125 const Type* ProjNode::proj_type(const Type* t) const {
126 if (t == Type::TOP) {
127 return Type::TOP;
128 }
129 if (t == Type::BOTTOM) {
130 return Type::BOTTOM;
131 }
132 t = t->is_tuple()->field_at(_con);
133 Node* n = in(0);
134 if ((_con == TypeFunc::Parms) &&
135 n->is_CallStaticJava() && n->as_CallStaticJava()->is_boxing_method()) {
136 // The result of autoboxing is always non-null on normal path.
137 t = t->join_speculative(TypePtr::NOTNULL);
138 }
139 return t;
140 }
141
142 const Type *ProjNode::bottom_type() const {
143 if (in(0) == nullptr) return Type::TOP;
144 return proj_type(in(0)->bottom_type());
145 }
146
147 const TypePtr *ProjNode::adr_type() const {
148 if (bottom_type() == Type::MEMORY) {
149 // in(0) might be a narrow MemBar; otherwise we will report TypePtr::BOTTOM
150 Node* ctrl = in(0);
151 if (ctrl->Opcode() == Op_Tuple) {
152 // Jumping over Tuples: the i-th projection of a Tuple is the i-th input of the Tuple.
153 ctrl = ctrl->in(_con);
154 }
155 // node is dead or we are in the process of removing a dead subgraph
156 if (ctrl == nullptr || ctrl->is_top()) {
157 return nullptr;
158 }
159 const TypePtr* adr_type = ctrl->adr_type();
160 #ifdef ASSERT
161 if (!VMError::is_error_reported() && !Node::in_dump())
162 assert(adr_type != nullptr, "source must have adr_type");
163 #endif
164 return adr_type;
165 }
166 assert(bottom_type()->base() != Type::Memory, "no other memories?");
167 return nullptr;
168 }
169
170 bool ProjNode::pinned() const { return in(0)->pinned(); }
171 #ifndef PRODUCT
172 void ProjNode::dump_spec(outputStream *st) const { st->print("#%d",_con); if(_is_io_use) st->print(" (i_o_use)");}
173
174 void ProjNode::dump_compact_spec(outputStream *st) const {
175 for (DUIterator i = this->outs(); this->has_out(i); i++) {
176 Node* o = this->out(i);
177 if (not_a_node(o)) {
178 st->print("[?]");
179 } else if (o == nullptr) {
180 st->print("[_]");
181 } else {
182 st->print("[%d]", o->_idx);
183 }
184 }
185 st->print("#%d", _con);
186 }
187 #endif
188
189 //----------------------------check_con----------------------------------------
190 void ProjNode::check_con() const {
191 Node* n = in(0);
192 if (n == nullptr) return; // should be assert, but NodeHash makes bogons
193 if (n->is_Mach()) return; // mach. projs. are not type-safe
194 if (n->is_Start()) return; // alas, starts can have mach. projs. also
195 if (_con == SCMemProjNode::SCMEMPROJCON ) return;
196 const Type* t = n->bottom_type();
197 if (t == Type::TOP) return; // multi is dead
198 assert(_con < t->is_tuple()->cnt(), "ProjNode::_con must be in range");
199 }
200
201 //------------------------------Identity---------------------------------------
202 Node* ProjNode::Identity(PhaseGVN* phase) {
203 if (in(0) != nullptr && in(0)->Opcode() == Op_Tuple) {
204 // Jumping over Tuples: the i-th projection of a Tuple is the i-th input of the Tuple.
205 return in(0)->in(_con);
206 }
207 return this;
208 }
209
210 //------------------------------Value------------------------------------------
211 const Type* ProjNode::Value(PhaseGVN* phase) const {
212 if (in(0) == nullptr) return Type::TOP;
213 return proj_type(phase->type(in(0)));
214 }
215
216 //------------------------------out_RegMask------------------------------------
217 // Pass the buck uphill
218 const RegMask &ProjNode::out_RegMask() const {
219 return RegMask::EMPTY;
220 }
221
222 //------------------------------ideal_reg--------------------------------------
223 uint ProjNode::ideal_reg() const {
224 return bottom_type()->ideal_reg();
225 }
226
227 //-------------------------------is_uncommon_trap_proj----------------------------
228 // Return uncommon trap call node if proj is for "proj->[region->..]call_uct"
229 // null otherwise
230 CallStaticJavaNode* ProjNode::is_uncommon_trap_proj(Deoptimization::DeoptReason reason) const {
231 const int path_limit = 10;
232 const Node* out = this;
233 for (int ct = 0; ct < path_limit; ct++) {
234 out = out->unique_ctrl_out_or_null();
235 if (out == nullptr)
236 return nullptr;
237 if (out->is_CallStaticJava()) {
238 CallStaticJavaNode* call = out->as_CallStaticJava();
239 int req = call->uncommon_trap_request();
240 if (req != 0) {
241 Deoptimization::DeoptReason trap_reason = Deoptimization::trap_request_reason(req);
242 if (trap_reason == reason || reason == Deoptimization::Reason_none) {
243 return call;
244 }
245 }
246 return nullptr; // don't do further after call
247 }
248 if (out->Opcode() != Op_Region)
249 return nullptr;
250 }
251 return nullptr;
252 }
253
254 //-------------------------------is_uncommon_trap_if_pattern-------------------------
255 // Return uncommon trap call node for "if(test)-> proj -> ...
256 // |
257 // V
258 // other_proj->[region->..]call_uct"
259 // or null otherwise.
260 CallStaticJavaNode* ProjNode::is_uncommon_trap_if_pattern(Deoptimization::DeoptReason reason) const {
261 Node* iff = in(0);
262 if (!iff->is_If() || iff->outcnt() < 2) {
263 // Not a projection of an If or variation of a dead If node.
264 return nullptr;
265 }
266 return as_IfProj()->other_if_proj()->is_uncommon_trap_proj(reason);
267 }
268
269 NarrowMemProjNode::NarrowMemProjNode(InitializeNode* src, const TypePtr* adr_type)
270 : ProjNode(src, TypeFunc::Memory), _adr_type(adr_type) {
271 init_class_id(Class_NarrowMemProj);
272 }