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