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