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