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* 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(1, n);
 62   }
 63   virtual Node* Identity(PhaseGVN* phase);
 64   virtual const Type* Value(PhaseGVN* phase) const;
 65   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 66   virtual int Opcode() const;
 67   virtual uint ideal_reg() const = 0;
 68   virtual bool depends_only_on_test() const { return _dependency == RegularDependency; }
 69   bool carry_dependency() const { return _dependency != RegularDependency; }
 70   TypeNode* dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const;
 71   static Node* make_cast(int opcode, Node* c, Node* n, const Type* t, DependencyType dependency, const TypeTuple* extra_types);
 72   static Node* make(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(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)
109     : ConstraintCastNode(n, t, dependency, nullptr), _range_check_dependency(range_check_dependency) {
110     init_class_id(Class_CastII);
111     init_req(0, ctrl);
112   }
113   virtual int Opcode() const;
114   virtual uint ideal_reg() const { return Op_RegI; }
115   virtual Node* Identity(PhaseGVN* phase);
116   virtual const Type* Value(PhaseGVN* phase) const;
117   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
118   bool has_range_check() {
119 #ifdef _LP64
120     return _range_check_dependency;
121 #else
122     assert(!_range_check_dependency, "Should not have range check dependency");
123     return false;
124 #endif
125   }
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)
135     : ConstraintCastNode(n, t, dependency, nullptr) {
136     init_class_id(Class_CastLL);
137     init_req(0, ctrl);
138   }
139   CastLLNode(Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
140           : ConstraintCastNode(n, t, dependency, types) {
141     init_class_id(Class_CastLL);
142   }
143 
144   virtual const Type* Value(PhaseGVN* phase) const;
145   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
146   virtual int Opcode() const;
147   virtual uint ideal_reg() const { return Op_RegL; }
148 };
149 
150 class CastFFNode: public ConstraintCastNode {
151 public:
152   CastFFNode(Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
153           : ConstraintCastNode(n, t, dependency, types) {
154     init_class_id(Class_CastFF);
155   }
156   virtual int Opcode() const;
157   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
158 };
159 
160 class CastDDNode: public ConstraintCastNode {
161 public:
162   CastDDNode(Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
163           : ConstraintCastNode(n, t, dependency, types) {
164     init_class_id(Class_CastDD);
165   }
166   virtual int Opcode() const;
167   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
168 };
169 
170 class CastVVNode: public ConstraintCastNode {
171 public:
172   CastVVNode(Node* n, const Type* t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
173           : ConstraintCastNode(n, t, dependency, types) {
174     init_class_id(Class_CastVV);
175   }
176   virtual int Opcode() const;
177   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
178 };
179 
180 
181 //------------------------------CastPPNode-------------------------------------
182 // cast pointer to pointer (different type)
183 class CastPPNode: public ConstraintCastNode {
184   public:
185   CastPPNode (Node *n, const Type *t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
186     : ConstraintCastNode(n, t, dependency, types) {
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 *c, Node *n, const Type *t, DependencyType dependency = RegularDependency, const TypeTuple* types = nullptr)
197     : ConstraintCastNode(n, t, dependency, types) {
198     init_class_id(Class_CheckCastPP);
199     init_req(0, c);
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