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 };
199
200 //------------------------------CastIINode-------------------------------------
201 // cast integer to integer (different range)
202 class CastIINode: public ConstraintCastNode {
203 protected:
204 // Is this node dependent on a range check?
205 const bool _range_check_dependency;
206 virtual bool cmp(const Node &n) const;
207 virtual uint size_of() const;
208
209 public:
210 CastIINode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, bool range_check_dependency = false, const TypeTuple* types = nullptr)
211 : ConstraintCastNode(ctrl, n, t, dependency, types), _range_check_dependency(range_check_dependency) {
212 assert(ctrl != nullptr, "control must be set");
213 init_class_id(Class_CastII);
214 }
215 virtual int Opcode() const;
216 virtual uint ideal_reg() const { return Op_RegI; }
217 virtual Node* Identity(PhaseGVN* phase);
218
219 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
220 bool has_range_check() const {
221 #ifdef _LP64
222 return _range_check_dependency;
223 #else
224 assert(!_range_check_dependency, "Should not have range check dependency");
225 return false;
226 #endif
227 }
228
229 CastIINode* make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const;
230 void remove_range_check_cast(Compile* C);
231
232 #ifndef PRODUCT
233 virtual void dump_spec(outputStream* st) const;
234 #endif
235
236 private:
237 virtual CastIINode* pin_node_under_control_impl() const;
238 };
239
240 class CastLLNode: public ConstraintCastNode {
241 public:
242 CastLLNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
243 : ConstraintCastNode(ctrl, n, t, dependency, types) {
244 assert(ctrl != nullptr, "control must be set");
245 init_class_id(Class_CastLL);
246 }
247
248 static bool is_inner_loop_backedge(IfProjNode* proj);
249
250 static bool cmp_used_at_inner_loop_exit_test(CmpNode* cmp);
251 bool used_at_inner_loop_exit_test() const;
252
253 virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
254 virtual int Opcode() const;
255 virtual uint ideal_reg() const { return Op_RegL; }
256 CastLLNode* make_with(Node* parent, const TypeInteger* type, const DependencyType& dependency) const;
257 };
258
259 class CastHHNode: public ConstraintCastNode {
260 public:
261 CastHHNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
262 : ConstraintCastNode(ctrl, n, t, dependency, types) {
263 assert(ctrl != nullptr, "control must be set");
264 init_class_id(Class_CastHH);
265 }
266 virtual int Opcode() const;
267 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
268 };
269
270 class CastFFNode: public ConstraintCastNode {
271 public:
272 CastFFNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
273 : ConstraintCastNode(ctrl, n, t, dependency, types) {
274 assert(ctrl != nullptr, "control must be set");
275 init_class_id(Class_CastFF);
276 }
277 virtual int Opcode() const;
278 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
279 };
280
281 class CastDDNode: public ConstraintCastNode {
282 public:
283 CastDDNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
284 : ConstraintCastNode(ctrl, n, t, dependency, types) {
285 assert(ctrl != nullptr, "control must be set");
286 init_class_id(Class_CastDD);
287 }
288 virtual int Opcode() const;
289 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
290 };
291
292 class CastVVNode: public ConstraintCastNode {
293 public:
294 CastVVNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
295 : ConstraintCastNode(ctrl, n, t, dependency, types) {
296 assert(ctrl != nullptr, "control must be set");
297 init_class_id(Class_CastVV);
298 }
299 virtual int Opcode() const;
300 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
301 };
302
303
304 //------------------------------CastPPNode-------------------------------------
305 // cast pointer to pointer (different type)
306 class CastPPNode: public ConstraintCastNode {
307 public:
308 CastPPNode (Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
309 : ConstraintCastNode(ctrl, n, t, dependency, types) {
310 init_class_id(Class_CastPP);
311 }
312 virtual int Opcode() const;
313 virtual uint ideal_reg() const { return Op_RegP; }
314 };
315
316 //------------------------------CheckCastPPNode--------------------------------
317 // for _checkcast, cast pointer to pointer (different type), without JOIN,
318 class CheckCastPPNode: public ConstraintCastNode {
319 public:
320 CheckCastPPNode(Node* ctrl, Node* n, const Type* t, const DependencyType& dependency = DependencyType::FloatingNarrowing, const TypeTuple* types = nullptr)
321 : ConstraintCastNode(ctrl, n, t, dependency, types) {
322 assert(ctrl != nullptr, "control must be set");
323 init_class_id(Class_CheckCastPP);
324 }
325
326 virtual const Type* Value(PhaseGVN* phase) const;
327 virtual int Opcode() const;
328 virtual uint ideal_reg() const { return Op_RegP; }
329
330 private:
331 virtual bool depends_only_on_test_impl() const { return !type()->isa_rawptr() && ConstraintCastNode::depends_only_on_test_impl(); }
332 };
333
334
335 //------------------------------CastX2PNode-------------------------------------
336 // convert a machine-pointer-sized integer to a raw pointer
337 class CastX2PNode : public Node {
338 public:
339 CastX2PNode( Node *n ) : Node(nullptr, n) {}
340 virtual int Opcode() const;
341 virtual const Type* Value(PhaseGVN* phase) const;
342 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
343 virtual Node* Identity(PhaseGVN* phase);
344 virtual uint ideal_reg() const { return Op_RegP; }
345 virtual const Type *bottom_type() const { return TypeRawPtr::BOTTOM; }
346 };
347
348 //------------------------------CastP2XNode-------------------------------------
349 // Used in both 32-bit and 64-bit land.
350 // Used for card-marks and unsafe pointer math.
351 class CastP2XNode : public Node {
352 public:
353 CastP2XNode( Node *ctrl, Node *n ) : Node(ctrl, n) {}
354 virtual int Opcode() const;
355 virtual const Type* Value(PhaseGVN* phase) const;
356 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
357 virtual Node* Identity(PhaseGVN* phase);
358 virtual uint ideal_reg() const { return Op_RegX; }
359 virtual const Type *bottom_type() const { return TypeX_X; }
360
361 private:
362 // Return false to keep node from moving away from an associated card mark.
363 virtual bool depends_only_on_test_impl() const { return false; }
364 };
365
366
367
368 #endif // SHARE_OPTO_CASTNODE_HPP