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* n, const Type* t, DependencyType dependency = RegularDependency, bool range_check_dependency = false, const TypeTuple* types = nullptr)
105     : ConstraintCastNode(nullptr, n, t, dependency, types), _range_check_dependency(range_check_dependency) {
106     init_class_id(Class_CastII);
107   }
108   CastIINode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, bool range_check_dependency = false, const TypeTuple* types = nullptr)
109     : ConstraintCastNode(ctrl, n, t, dependency, types), _range_check_dependency(range_check_dependency) {
110     init_class_id(Class_CastII);
111   }
112   virtual int Opcode() const;
113   virtual uint ideal_reg() const { return Op_RegI; }
114   virtual Node* Identity(PhaseGVN* phase);
115   virtual const Type* Value(PhaseGVN* phase) const;
116   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
117   bool has_range_check() const {
118 #ifdef _LP64
119     return _range_check_dependency;
120 #else
121     assert(!_range_check_dependency, "Should not have range check dependency");
122     return false;
123 #endif
124   }
125 
126   CastIINode* pin_array_access_node() const;
127 
128 #ifndef PRODUCT
129   virtual void dump_spec(outputStream* st) const;
130 #endif
131 };
132 
133 class CastLLNode: public ConstraintCastNode {
134 public:
135   CastLLNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
136           : ConstraintCastNode(ctrl, n, t, dependency, types) {
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     init_class_id(Class_CastFF);
151   }
152   virtual int Opcode() const;
153   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
154 };
155 
156 class CastDDNode: public ConstraintCastNode {
157 public:
158   CastDDNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
159           : ConstraintCastNode(ctrl, n, t, dependency, types) {
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     init_class_id(Class_CastVV);
171   }
172   virtual int Opcode() const;
173   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
174 };
175 
176 
177 //------------------------------CastPPNode-------------------------------------
178 // cast pointer to pointer (different type)
179 class CastPPNode: public ConstraintCastNode {
180   public:
181   CastPPNode (Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
182     : ConstraintCastNode(ctrl, n, t, dependency, types) {
183   }
184   virtual int Opcode() const;
185   virtual uint ideal_reg() const { return Op_RegP; }
186 };
187 
188 //------------------------------CheckCastPPNode--------------------------------
189 // for _checkcast, cast pointer to pointer (different type), without JOIN,
190 class CheckCastPPNode: public ConstraintCastNode {
191   public:
192   CheckCastPPNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
193     : ConstraintCastNode(ctrl, n, t, dependency, types) {
194     init_class_id(Class_CheckCastPP);
195   }
196 
197   virtual const Type* Value(PhaseGVN* phase) const;
198   virtual int   Opcode() const;
199   virtual uint  ideal_reg() const { return Op_RegP; }
200   bool depends_only_on_test() const { return !type()->isa_rawptr() && ConstraintCastNode::depends_only_on_test(); }
201  };
202 
203 
204 //------------------------------CastX2PNode-------------------------------------
205 // convert a machine-pointer-sized integer to a raw pointer
206 class CastX2PNode : public Node {
207   public:
208   CastX2PNode( Node *n ) : Node(nullptr, n) {}
209   virtual int Opcode() const;
210   virtual const Type* Value(PhaseGVN* phase) const;
211   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
212   virtual Node* Identity(PhaseGVN* phase);
213   virtual uint ideal_reg() const { return Op_RegP; }
214   virtual const Type *bottom_type() const { return TypeRawPtr::BOTTOM; }
215 };
216 
217 //------------------------------CastP2XNode-------------------------------------
218 // Used in both 32-bit and 64-bit land.
219 // Used for card-marks and unsafe pointer math.
220 class CastP2XNode : public Node {
221   public:
222   CastP2XNode( Node *ctrl, Node *n ) : Node(ctrl, n) {}
223   virtual int Opcode() const;
224   virtual const Type* Value(PhaseGVN* phase) const;
225   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
226   virtual Node* Identity(PhaseGVN* phase);
227   virtual uint ideal_reg() const { return Op_RegX; }
228   virtual const Type *bottom_type() const { return TypeX_X; }
229   // Return false to keep node from moving away from an associated card mark.
230   virtual bool depends_only_on_test() const { return false; }
231 };
232 
233 
234 
235 #endif // SHARE_OPTO_CASTNODE_HPP