1 /*
2 * Copyright (c) 2014, 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_CASTNODE_HPP
26 #define SHARE_OPTO_CASTNODE_HPP
27
28 #include "opto/node.hpp"
29 #include "opto/opcodes.hpp"
30
31
32 //------------------------------ConstraintCastNode-----------------------------
33 // cast to a different range
34 class ConstraintCastNode: public TypeNode {
35 public:
36 // Cast nodes are subject to a few optimizations:
37 //
38 // 1- if the type carried by the Cast doesn't narrow the type of its input, the cast can be replaced by its input.
39 // Similarly, if a dominating Cast with the same input and a narrower type constraint is found, it can replace the
40 // current cast.
41 //
42 // 2- if the condition that the Cast is control dependent is hoisted, the Cast is hoisted as well
43 //
44 // 1- and 2- are not always applied depending on what constraint are applied to the Cast: there are cases where 1-
45 // and 2- apply, where neither 1- nor 2- apply and where one or the other apply. This class abstract away these
46 // details.
47 //
48 // If _narrows_type is true, the cast carries a type dependency: "after" the control the cast is dependent on, its data
49 // input is known to have a narrower type (stored in the cast node itself). Optimizations 1- above only apply to cast
50 // nodes for which _narrows_type is true.
51 // if _floating is true, the cast only depends on a single control: its control input. Otherwise, it is pinned at its
52 // current location. Optimizations 2- only apply to cast nodes for which _floating is true.
53 // _floating here is similar to Node::depends_only_on_test().
54 // The 4 combinations of _narrows_types/_floating true/false have some use. See below, at the end of this class
55 // definition, for examples.
56 class DependencyType {
57 private:
58 const bool _floating; // Does this Cast depends on its control input or is it pinned?
59 const bool _narrows_type; // Does this Cast narrows the type i.e. if input type is narrower can it be removed?
60 const char* _desc;
61 DependencyType(bool depends_on_test, bool narrows_type, const char* desc)
62 : _floating(depends_on_test),
63 _narrows_type(narrows_type),
64 _desc(desc) {
65 }
66 NONCOPYABLE(DependencyType);
67
68 public:
69
70 bool is_floating() const {
71 return _floating;
72 }
73
74 bool narrows_type() const {
75 return _narrows_type;
76 }
77
78 void dump_on(outputStream *st) const {
79 st->print("%s", _desc);
80 }
81
82 uint hash() const {
83 return (_floating ? 1 : 0) + (_narrows_type ? 2 : 0);
84 }
85
86 bool cmp(const DependencyType& other) const {
87 return _floating == other._floating && _narrows_type == other._narrows_type;
88 }
89
90 const DependencyType& with_non_narrowing() const {
91 if (_floating) {
92 return FloatingNonNarrowing;
93 }
94 return NonFloatingNonNarrowing;
95 }
96
97 const DependencyType& with_pinned_dependency() const {
98 if (_narrows_type) {
99 return NonFloatingNarrowing;
100 }
101 return NonFloatingNonNarrowing;
102 }
103
104 // All the possible combinations of floating/narrowing with example use cases:
105
106 // Use case example: Range Check CastII
107 // Floating: The Cast is only dependent on the single range check. If the range check was ever to be hoisted it
108 // would be safe to let the Cast float to where the range check is hoisted up to.
109 // Narrowing: The Cast narrows the type to a positive index. If the input to the Cast is narrower, we can safely
110 // remove the cast because the array access will be safe.
111 static const DependencyType FloatingNarrowing;
112 // Use case example: Widening Cast nodes' types after loop opts: We want to common Casts with slightly different types.
113 // Floating: These Casts only depend on the single control.
114 // NonNarrowing: Even when the input type is narrower, we are not removing the Cast. Otherwise, the dependency
115 // to the single control is lost, and an array access could float above its range check because we
116 // just removed the dependency to the range check by removing the Cast. This could lead to an
117 // out-of-bounds access.
118 static const DependencyType FloatingNonNarrowing;
119 // Use case example: An array accesses that is no longer dependent on a single range check (e.g. range check smearing).
120 // NonFloating: The array access must be pinned below all the checks it depends on. If the check it directly depends
121 // on with a control input is hoisted, we do not hoist the Cast as well. If we allowed the Cast to float,
122 // we risk that the array access ends up above another check it depends on (we cannot model two control
123 // dependencies for a node in the IR). This could lead to an out-of-bounds access.
124 // Narrowing: If the Cast does not narrow the input type, then it's safe to remove the cast because the array access
125 // will be safe.
126 static const DependencyType NonFloatingNarrowing;
127 // Use case example: Sinking nodes out of a loop
128 // Non-Floating & Non-Narrowing: We don't want the Cast that forces the node to be out of loop to be removed in any
129 // case. Otherwise, the sunk node could float back into the loop, undoing the sinking.
130 // This Cast is only used for pinning without caring about narrowing types.
131 static const DependencyType NonFloatingNonNarrowing;
132
133 };
134
135 protected:
136 const DependencyType& _dependency;
137 virtual bool cmp( const Node &n ) const;
138 virtual uint size_of() const;
139 virtual uint hash() const; // Check the type
140 const TypeInteger* widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const;
141
142 virtual ConstraintCastNode* make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const {
143 ShouldNotReachHere(); // Only implemented for CastII and CastLL
144 return nullptr;
145 }
146
147 Node* find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, const TypeInteger* type, const DependencyType& dependency) const;
148
149 // PhiNode::Ideal() transforms a Phi that merges a single uncasted value into a single cast pinned at the region.
150 // The types of cast nodes eliminated as a consequence of this transformation are collected and stored here so the
151 // type dependencies carried by the cast are known. The cast can then be eliminated if the type of its input is
152 // narrower (or equal) than all the types it carries.
153 const TypeTuple* _extra_types;
154
155 public:
156 ConstraintCastNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency,
157 const TypeTuple* extra_types)
158 : TypeNode(t,2), _dependency(dependency), _extra_types(extra_types) {
159 init_class_id(Class_ConstraintCast);
160 init_req(0, ctrl);
161 init_req(1, n);
162 }
163 virtual Node* Identity(PhaseGVN* phase);
164 virtual const Type* Value(PhaseGVN* phase) const;
165 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
166 virtual int Opcode() const;
167 virtual uint ideal_reg() const = 0;
168 bool carry_dependency() const { return !_dependency.cmp(DependencyType::FloatingNarrowing); }
169 const DependencyType& dependency() const { return _dependency; }
170 TypeNode* dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const;
171 static Node* make_cast_for_basic_type(Node* c, Node* n, const Type* t, const DependencyType& dependency, BasicType bt);
172
173 #ifndef PRODUCT
174 virtual void dump_spec(outputStream *st) const;
175 #endif
176
177 static Node* make_cast_for_type(Node* c, Node* in, const Type* type, const DependencyType& dependency,
178 const TypeTuple* types);
179
180 Node* optimize_integer_cast_of_add(PhaseGVN* phase, BasicType bt);
181 Node* optimize_integer_cast(PhaseGVN* phase, BasicType bt);
182
183 bool higher_equal_types(PhaseGVN* phase, const Node* other) const;
184
185 int extra_types_count() const {
186 return _extra_types == nullptr ? 0 : _extra_types->cnt();
187 }
188
189 const Type* extra_type_at(int i) const {
190 return _extra_types->field_at(i);
191 }
192
193 protected:
194 virtual bool depends_only_on_test_impl() const { return _dependency.is_floating(); }
195
196 private:
197 virtual Node* pin_node_under_control_impl() const;
198 Node* ideal_cast_of_inline_type_node(PhaseGVN* phase);
199 };
200
201 //------------------------------CastIINode-------------------------------------
202 // cast integer to integer (different range)
203 class CastIINode: public ConstraintCastNode {
204 protected:
205 // Is this node dependent on a range check?
206 const bool _range_check_dependency;
207 virtual bool cmp(const Node &n) const;
208 virtual uint size_of() const;
209
210 public:
211 CastIINode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, bool range_check_dependency = false, const TypeTuple* types = nullptr)
212 : ConstraintCastNode(ctrl, n, t, dependency, types), _range_check_dependency(range_check_dependency) {
213 assert(ctrl != nullptr, "control must be set");
214 init_class_id(Class_CastII);
215 }
216 virtual int Opcode() const;
217 virtual uint ideal_reg() const { return Op_RegI; }
218 virtual Node* Identity(PhaseGVN* phase);
219
220 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
221 bool has_range_check() const {
222 #ifdef _LP64
223 return _range_check_dependency;
224 #else
225 assert(!_range_check_dependency, "Should not have range check dependency");
226 return false;
227 #endif
228 }
229
230 CastIINode* make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const;
231 void remove_range_check_cast(Compile* C);
232
233 #ifndef PRODUCT
234 virtual void dump_spec(outputStream* st) const;
235 #endif
236
237 private:
238 virtual CastIINode* pin_node_under_control_impl() const;
239 };
240
241 class CastLLNode: public ConstraintCastNode {
242 public:
243 CastLLNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
244 : ConstraintCastNode(ctrl, n, t, dependency, types) {
245 assert(ctrl != nullptr, "control must be set");
246 init_class_id(Class_CastLL);
247 }
248
249 static bool is_inner_loop_backedge(IfProjNode* proj);
250
251 static bool cmp_used_at_inner_loop_exit_test(CmpNode* cmp);
252 bool used_at_inner_loop_exit_test() const;
253
254 virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
255 virtual int Opcode() const;
256 virtual uint ideal_reg() const { return Op_RegL; }
257 CastLLNode* make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const;
258 };
259
260 class CastHHNode: public ConstraintCastNode {
261 public:
262 CastHHNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
263 : ConstraintCastNode(ctrl, n, t, dependency, types) {
264 assert(ctrl != nullptr, "control must be set");
265 init_class_id(Class_CastHH);
266 }
267 virtual int Opcode() const;
268 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
269 };
270
271 class CastFFNode: public ConstraintCastNode {
272 public:
273 CastFFNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
274 : ConstraintCastNode(ctrl, n, t, dependency, types) {
275 assert(ctrl != nullptr, "control must be set");
276 init_class_id(Class_CastFF);
277 }
278 virtual int Opcode() const;
279 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
280 };
281
282 class CastDDNode: public ConstraintCastNode {
283 public:
284 CastDDNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
285 : ConstraintCastNode(ctrl, n, t, dependency, types) {
286 assert(ctrl != nullptr, "control must be set");
287 init_class_id(Class_CastDD);
288 }
289 virtual int Opcode() const;
290 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
291 };
292
293 class CastVVNode: public ConstraintCastNode {
294 public:
295 CastVVNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
296 : ConstraintCastNode(ctrl, n, t, dependency, types) {
297 assert(ctrl != nullptr, "control must be set");
298 init_class_id(Class_CastVV);
299 }
300 virtual int Opcode() const;
301 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
302 };
303
304
305 //------------------------------CastPPNode-------------------------------------
306 // cast pointer to pointer (different type)
307 class CastPPNode : public ConstraintCastNode {
308 public:
309 CastPPNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
310 : ConstraintCastNode(ctrl, n, t, dependency, types) {
311 init_class_id(Class_CastPP);
312 verify_type(n->bottom_type(), t);
313 }
314 virtual int Opcode() const;
315 virtual uint ideal_reg() const { return Op_RegP; }
316
317 private:
318 static void verify_type(const Type* in_type, const Type* out_type);
319 };
320
321 //------------------------------CheckCastPPNode--------------------------------
322 // for _checkcast, cast pointer to pointer (different type), without JOIN,
323 class CheckCastPPNode: public ConstraintCastNode {
324 public:
325 CheckCastPPNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
326 : ConstraintCastNode(ctrl, n, t, dependency, types) {
327 assert(ctrl != nullptr, "control must be set");
328 init_class_id(Class_CheckCastPP);
329 }
330
331 virtual Node* Identity(PhaseGVN* phase);
332 virtual const Type* Value(PhaseGVN* phase) const;
333 virtual int Opcode() const;
334 virtual uint ideal_reg() const { return Op_RegP; }
335
336 private:
337 virtual bool depends_only_on_test_impl() const { return !type()->isa_rawptr() && ConstraintCastNode::depends_only_on_test_impl(); }
338 virtual Node* pin_node_under_control_impl() const;
339 };
340
341
342 //------------------------------CastX2PNode-------------------------------------
343 // convert a machine-pointer-sized integer to a raw pointer
344 class CastX2PNode : public Node {
345 public:
346 CastX2PNode( Node *n ) : Node(nullptr, n) {}
347 virtual int Opcode() const;
348 virtual const Type* Value(PhaseGVN* phase) const;
349 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
350 virtual Node* Identity(PhaseGVN* phase);
351 virtual uint ideal_reg() const { return Op_RegP; }
352 virtual const Type *bottom_type() const { return TypeRawPtr::BOTTOM; }
353 };
354
355 // Cast an integer to a narrow oop
356 class CastI2NNode : public TypeNode {
357 public:
358 CastI2NNode(Node* ctrl, Node* n, const Type* t) : TypeNode(t, 2) {
359 init_req(0, ctrl);
360 init_req(1, n);
361 }
362 virtual int Opcode() const;
363 virtual uint ideal_reg() const { return Op_RegN; }
364
365 private:
366 virtual bool depends_only_on_test_impl() const { return false; }
367 };
368
369 //------------------------------CastP2XNode-------------------------------------
370 // Used in both 32-bit and 64-bit land.
371 // Used for card-marks and unsafe pointer math.
372 class CastP2XNode : public Node {
373 public:
374 CastP2XNode( Node *ctrl, Node *n ) : Node(ctrl, n) {}
375 virtual int Opcode() const;
376 virtual const Type* Value(PhaseGVN* phase) const;
377 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
378 virtual Node* Identity(PhaseGVN* phase);
379 virtual uint ideal_reg() const { return Op_RegX; }
380 virtual const Type *bottom_type() const { return TypeX_X; }
381
382 private:
383 // Return false to keep node from moving away from an associated card mark.
384 virtual bool depends_only_on_test_impl() const { return false; }
385 };
386
387 #endif // SHARE_OPTO_CASTNODE_HPP