1 /*
  2  * Copyright (c) 2014, 2024, 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   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
142   virtual int Opcode() const;
143   virtual uint ideal_reg() const { return Op_RegL; }
144 };
145 
146 class CastFFNode: public ConstraintCastNode {
147 public:
148   CastFFNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
149           : ConstraintCastNode(ctrl, n, t, dependency, types) {
150     assert(ctrl != nullptr, "control must be set");
151     init_class_id(Class_CastFF);
152   }
153   virtual int Opcode() const;
154   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
155 };
156 
157 class CastDDNode: public ConstraintCastNode {
158 public:
159   CastDDNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
160           : ConstraintCastNode(ctrl, n, t, dependency, types) {
161     assert(ctrl != nullptr, "control must be set");
162     init_class_id(Class_CastDD);
163   }
164   virtual int Opcode() const;
165   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
166 };
167 
168 class CastVVNode: public ConstraintCastNode {
169 public:
170   CastVVNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
171           : ConstraintCastNode(ctrl, n, t, dependency, types) {
172     assert(ctrl != nullptr, "control must be set");
173     init_class_id(Class_CastVV);
174   }
175   virtual int Opcode() const;
176   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
177 };
178 
179 
180 //------------------------------CastPPNode-------------------------------------
181 // cast pointer to pointer (different type)
182 class CastPPNode: public ConstraintCastNode {
183   public:
184   CastPPNode (Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
185     : ConstraintCastNode(ctrl, n, t, dependency, types) {
186     init_class_id(Class_CastPP);
187   }
188   virtual int Opcode() const;
189   virtual uint ideal_reg() const { return Op_RegP; }
190 };
191 
192 //------------------------------CheckCastPPNode--------------------------------
193 // for _checkcast, cast pointer to pointer (different type), without JOIN,
194 class CheckCastPPNode: public ConstraintCastNode {
195   public:
196   CheckCastPPNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
197     : ConstraintCastNode(ctrl, n, t, dependency, types) {
198     assert(ctrl != nullptr, "control must be set");
199     init_class_id(Class_CheckCastPP);
200   }
201 
202   virtual const Type* Value(PhaseGVN* phase) const;
203   virtual int   Opcode() const;
204   virtual uint  ideal_reg() const { return Op_RegP; }
205   bool depends_only_on_test() const { return !type()->isa_rawptr() && ConstraintCastNode::depends_only_on_test(); }
206  };
207 
208 
209 //------------------------------CastX2PNode-------------------------------------
210 // convert a machine-pointer-sized integer to a raw pointer
211 class CastX2PNode : public Node {
212   public:
213   CastX2PNode( Node *n ) : Node(nullptr, n) {}
214   virtual int Opcode() const;
215   virtual const Type* Value(PhaseGVN* phase) const;
216   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
217   virtual Node* Identity(PhaseGVN* phase);
218   virtual uint ideal_reg() const { return Op_RegP; }
219   virtual const Type *bottom_type() const { return TypeRawPtr::BOTTOM; }
220 };
221 
222 //------------------------------CastP2XNode-------------------------------------
223 // Used in both 32-bit and 64-bit land.
224 // Used for card-marks and unsafe pointer math.
225 class CastP2XNode : public Node {
226   public:
227   CastP2XNode( Node *ctrl, Node *n ) : Node(ctrl, n) {}
228   virtual int Opcode() const;
229   virtual const Type* Value(PhaseGVN* phase) const;
230   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
231   virtual Node* Identity(PhaseGVN* phase);
232   virtual uint ideal_reg() const { return Op_RegX; }
233   virtual const Type *bottom_type() const { return TypeX_X; }
234   // Return false to keep node from moving away from an associated card mark.
235   virtual bool depends_only_on_test() const { return false; }
236 };
237 
238 
239 
240 #endif // SHARE_OPTO_CASTNODE_HPP