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 enum DependencyType {
37 RegularDependency, // if cast doesn't improve input type, cast can be removed
38 StrongDependency, // leave cast in even if _type doesn't improve input type, can be replaced by stricter dominating cast if one exist
39 UnconditionalDependency // leave cast in unconditionally
40 };
41
42 protected:
43 const DependencyType _dependency;
44 virtual bool cmp( const Node &n ) const;
45 virtual uint size_of() const;
46 virtual uint hash() const; // Check the type
47 const Type* widen_type(const PhaseGVN* phase, const Type* res, BasicType bt) const;
48 Node* find_or_make_integer_cast(PhaseIterGVN* igvn, Node* parent, const TypeInteger* type) const;
49
50 private:
51 // PhiNode::Ideal() transforms a Phi that merges a single uncasted value into a single cast pinned at the region.
52 // The types of cast nodes eliminated as a consequence of this transformation are collected and stored here so the
53 // type dependencies carried by the cast are known. The cast can then be eliminated if the type of its input is
54 // narrower (or equal) than all the types it carries.
55 const TypeTuple* _extra_types;
56
57 public:
58 ConstraintCastNode(Node* ctrl, Node* n, const Type* t, ConstraintCastNode::DependencyType dependency,
59 const TypeTuple* extra_types)
60 : TypeNode(t,2), _dependency(dependency), _extra_types(extra_types) {
61 init_class_id(Class_ConstraintCast);
62 init_req(0, ctrl);
63 init_req(1, n);
64 }
65 virtual Node* Identity(PhaseGVN* phase);
66 virtual const Type* Value(PhaseGVN* phase) const;
67 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
68 virtual int Opcode() const;
69 virtual uint ideal_reg() const = 0;
70 virtual bool depends_only_on_test() const { return _dependency == RegularDependency; }
71 bool carry_dependency() const { return _dependency != RegularDependency; }
72 TypeNode* dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const;
73 static Node* make_cast_for_basic_type(Node* c, Node* n, const Type* t, DependencyType dependency, BasicType bt);
74
75 #ifndef PRODUCT
76 virtual void dump_spec(outputStream *st) const;
77 #endif
78
79 static Node* make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency,
80 const TypeTuple* types);
81
82 Node* optimize_integer_cast(PhaseGVN* phase, BasicType bt);
83
84 bool higher_equal_types(PhaseGVN* phase, const Node* other) const;
85
86 int extra_types_count() const {
87 return _extra_types == nullptr ? 0 : _extra_types->cnt();
88 }
89
90 const Type* extra_type_at(int i) const {
91 return _extra_types->field_at(i);
92 }
93 };
94
95 //------------------------------CastIINode-------------------------------------
96 // cast integer to integer (different range)
97 class CastIINode: public ConstraintCastNode {
98 protected:
99 // Is this node dependent on a range check?
100 const bool _range_check_dependency;
101 virtual bool cmp(const Node &n) const;
102 virtual uint size_of() const;
103
104 public:
105 CastIINode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, bool range_check_dependency = false, const TypeTuple* types = nullptr)
106 : ConstraintCastNode(ctrl, n, t, dependency, types), _range_check_dependency(range_check_dependency) {
107 assert(ctrl != nullptr, "control must be set");
108 init_class_id(Class_CastII);
109 }
110 virtual int Opcode() const;
111 virtual uint ideal_reg() const { return Op_RegI; }
112 virtual Node* Identity(PhaseGVN* phase);
113 virtual const Type* Value(PhaseGVN* phase) const;
114 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
115 bool has_range_check() const {
116 #ifdef _LP64
117 return _range_check_dependency;
118 #else
119 assert(!_range_check_dependency, "Should not have range check dependency");
120 return false;
121 #endif
122 }
123
124 CastIINode* pin_array_access_node() const;
125 void remove_range_check_cast(Compile* C);
126
127 #ifndef PRODUCT
128 virtual void dump_spec(outputStream* st) const;
129 #endif
130 };
131
132 class CastLLNode: public ConstraintCastNode {
133 public:
134 CastLLNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
135 : ConstraintCastNode(ctrl, n, t, dependency, types) {
136 assert(ctrl != nullptr, "control must be set");
137 init_class_id(Class_CastLL);
138 }
139
140 virtual const Type* Value(PhaseGVN* phase) const;
141
142 static bool is_inner_loop_backedge(ProjNode* proj);
143
144 static bool cmp_used_at_inner_loop_exit_test(CmpNode* cmp);
145 bool used_at_inner_loop_exit_test() const;
146
147 virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
148 virtual int Opcode() const;
149 virtual uint ideal_reg() const { return Op_RegL; }
150 };
151
152 class CastHHNode: public ConstraintCastNode {
153 public:
154 CastHHNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
155 : ConstraintCastNode(ctrl, n, t, dependency, types) {
156 assert(ctrl != nullptr, "control must be set");
157 init_class_id(Class_CastHH);
158 }
159 virtual int Opcode() const;
160 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
161 };
162
163 class CastFFNode: public ConstraintCastNode {
164 public:
165 CastFFNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
166 : ConstraintCastNode(ctrl, n, t, dependency, types) {
167 assert(ctrl != nullptr, "control must be set");
168 init_class_id(Class_CastFF);
169 }
170 virtual int Opcode() const;
171 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
172 };
173
174 class CastDDNode: public ConstraintCastNode {
175 public:
176 CastDDNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
177 : ConstraintCastNode(ctrl, n, t, dependency, types) {
178 assert(ctrl != nullptr, "control must be set");
179 init_class_id(Class_CastDD);
180 }
181 virtual int Opcode() const;
182 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
183 };
184
185 class CastVVNode: public ConstraintCastNode {
186 public:
187 CastVVNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
188 : ConstraintCastNode(ctrl, n, t, dependency, types) {
189 assert(ctrl != nullptr, "control must be set");
190 init_class_id(Class_CastVV);
191 }
192 virtual int Opcode() const;
193 virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
194 };
195
196
197 //------------------------------CastPPNode-------------------------------------
198 // cast pointer to pointer (different type)
199 class CastPPNode: public ConstraintCastNode {
200 public:
201 CastPPNode (Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
202 : ConstraintCastNode(ctrl, n, t, dependency, types) {
203 init_class_id(Class_CastPP);
204 }
205 virtual int Opcode() const;
206 virtual uint ideal_reg() const { return Op_RegP; }
207 };
208
209 //------------------------------CheckCastPPNode--------------------------------
210 // for _checkcast, cast pointer to pointer (different type), without JOIN,
211 class CheckCastPPNode: public ConstraintCastNode {
212 public:
213 CheckCastPPNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
214 : ConstraintCastNode(ctrl, n, t, dependency, types) {
215 assert(ctrl != nullptr, "control must be set");
216 init_class_id(Class_CheckCastPP);
217 }
218
219 virtual const Type* Value(PhaseGVN* phase) const;
220 virtual int Opcode() const;
221 virtual uint ideal_reg() const { return Op_RegP; }
222 bool depends_only_on_test() const { return !type()->isa_rawptr() && ConstraintCastNode::depends_only_on_test(); }
223 };
224
225
226 //------------------------------CastX2PNode-------------------------------------
227 // convert a machine-pointer-sized integer to a raw pointer
228 class CastX2PNode : public Node {
229 public:
230 CastX2PNode( Node *n ) : Node(nullptr, n) {}
231 virtual int Opcode() const;
232 virtual const Type* Value(PhaseGVN* phase) const;
233 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
234 virtual Node* Identity(PhaseGVN* phase);
235 virtual uint ideal_reg() const { return Op_RegP; }
236 virtual const Type *bottom_type() const { return TypeRawPtr::BOTTOM; }
237 };
238
239 //------------------------------CastP2XNode-------------------------------------
240 // Used in both 32-bit and 64-bit land.
241 // Used for card-marks and unsafe pointer math.
242 class CastP2XNode : public Node {
243 public:
244 CastP2XNode( Node *ctrl, Node *n ) : Node(ctrl, n) {}
245 virtual int Opcode() const;
246 virtual const Type* Value(PhaseGVN* phase) const;
247 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
248 virtual Node* Identity(PhaseGVN* phase);
249 virtual uint ideal_reg() const { return Op_RegX; }
250 virtual const Type *bottom_type() const { return TypeX_X; }
251 // Return false to keep node from moving away from an associated card mark.
252 virtual bool depends_only_on_test() const { return false; }
253 };
254
255
256
257 #endif // SHARE_OPTO_CASTNODE_HPP