1 /*
  2  * Copyright (c) 2014, 2019, 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 
 47   public:
 48   ConstraintCastNode(Node *n, const Type *t, DependencyType dependency)
 49     : TypeNode(t,2), _dependency(dependency) {
 50     init_class_id(Class_ConstraintCast);
 51     init_req(1, n);
 52   }
 53   virtual Node* Identity(PhaseGVN* phase);
 54   virtual const Type* Value(PhaseGVN* phase) const;
 55   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 56   virtual int Opcode() const;
 57   virtual uint ideal_reg() const = 0;
 58   virtual bool depends_only_on_test() const { return _dependency == RegularDependency; }
 59   bool carry_dependency() const { return _dependency != RegularDependency; }
 60   TypeNode* dominating_cast(PhaseGVN* gvn, PhaseTransform* pt) const;
 61   static Node* make_cast(int opcode, Node* c, Node *n, const Type *t, DependencyType dependency);
 62   static Node* make(Node* c, Node *n, const Type *t, BasicType bt);
 63   virtual bool operates_on(BasicType bt, bool signed_int) const {
 64     assert(bt == T_INT || bt == T_LONG, "unsupported");
 65     return false;
 66   }
 67 
 68 #ifndef PRODUCT
 69   virtual void dump_spec(outputStream *st) const;
 70 #endif
 71 
 72   static Node* make_cast_for_type(Node* c, Node* in, const Type* type, DependencyType dependency);
 73 };
 74 
 75 //------------------------------CastIINode-------------------------------------
 76 // cast integer to integer (different range)
 77 class CastIINode: public ConstraintCastNode {
 78   protected:
 79   // Is this node dependent on a range check?
 80   const bool _range_check_dependency;
 81   virtual bool cmp(const Node &n) const;
 82   virtual uint size_of() const;
 83 
 84   public:
 85   CastIINode(Node* n, const Type* t, DependencyType dependency = RegularDependency, bool range_check_dependency = false)
 86     : ConstraintCastNode(n, t, dependency), _range_check_dependency(range_check_dependency) {
 87     init_class_id(Class_CastII);
 88   }
 89   CastIINode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, bool range_check_dependency = false)
 90     : ConstraintCastNode(n, t, dependency), _range_check_dependency(range_check_dependency) {
 91     init_class_id(Class_CastII);
 92     init_req(0, ctrl);
 93   }
 94   virtual int Opcode() const;
 95   virtual uint ideal_reg() const { return Op_RegI; }
 96   virtual Node* Identity(PhaseGVN* phase);
 97   virtual const Type* Value(PhaseGVN* phase) const;
 98   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 99   const bool has_range_check() {
100 #ifdef _LP64
101     return _range_check_dependency;
102 #else
103     assert(!_range_check_dependency, "Should not have range check dependency");
104     return false;
105 #endif
106   }
107   virtual bool operates_on(BasicType bt, bool signed_int) const {
108     assert(bt == T_INT || bt == T_LONG, "unsupported");
109     return bt == T_INT;
110   }
111 
112 #ifndef PRODUCT
113   virtual void dump_spec(outputStream* st) const;
114 #endif
115 };
116 
117 class CastLLNode: public ConstraintCastNode {
118 public:
119   CastLLNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency)
120     : ConstraintCastNode(n, t, dependency) {
121     init_class_id(Class_CastLL);
122     init_req(0, ctrl);
123   }
124   CastLLNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
125           : ConstraintCastNode(n, t, dependency){
126     init_class_id(Class_CastLL);
127   }
128   virtual bool operates_on(BasicType bt, bool signed_int) const {
129     assert(bt == T_INT || bt == T_LONG, "unsupported");
130     return bt == T_LONG;
131   }
132   virtual int Opcode() const;
133   virtual uint ideal_reg() const { return Op_RegL; }
134 };
135 
136 class CastFFNode: public ConstraintCastNode {
137 public:
138   CastFFNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
139           : ConstraintCastNode(n, t, dependency){
140     init_class_id(Class_CastFF);
141   }
142   virtual int Opcode() const;
143   virtual uint ideal_reg() const { return Op_RegF; }
144 };
145 
146 class CastDDNode: public ConstraintCastNode {
147 public:
148   CastDDNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
149           : ConstraintCastNode(n, t, dependency){
150     init_class_id(Class_CastDD);
151   }
152   virtual int Opcode() const;
153   virtual uint ideal_reg() const { return Op_RegD; }
154 };
155 
156 class CastVVNode: public ConstraintCastNode {
157 public:
158   CastVVNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
159           : ConstraintCastNode(n, t, dependency){
160     init_class_id(Class_CastVV);
161   }
162   virtual int Opcode() const;
163   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
164 };
165 
166 
167 //------------------------------CastPPNode-------------------------------------
168 // cast pointer to pointer (different type)
169 class CastPPNode: public ConstraintCastNode {
170   public:
171   CastPPNode (Node *n, const Type *t, DependencyType dependency = RegularDependency)
172     : ConstraintCastNode(n, t, dependency) {
173   }
174   virtual int Opcode() const;
175   virtual uint ideal_reg() const { return Op_RegP; }
176 };
177 
178 //------------------------------CheckCastPPNode--------------------------------
179 // for _checkcast, cast pointer to pointer (different type), without JOIN,
180 class CheckCastPPNode: public ConstraintCastNode {
181   public:
182   CheckCastPPNode(Node *c, Node *n, const Type *t, DependencyType dependency = RegularDependency)
183     : ConstraintCastNode(n, t, dependency) {
184     init_class_id(Class_CheckCastPP);
185     init_req(0, c);
186   }
187 
188   virtual Node* Identity(PhaseGVN* phase);
189   virtual const Type* Value(PhaseGVN* phase) const;
190   virtual int   Opcode() const;
191   virtual uint  ideal_reg() const { return Op_RegP; }
192   bool depends_only_on_test() const { return !type()->isa_rawptr() && ConstraintCastNode::depends_only_on_test(); }
193  };
194 
195 
196 //------------------------------CastX2PNode-------------------------------------
197 // convert a machine-pointer-sized integer to a raw pointer
198 class CastX2PNode : public Node {
199   public:
200   CastX2PNode( Node *n ) : Node(NULL, n) {}
201   virtual int Opcode() const;
202   virtual const Type* Value(PhaseGVN* phase) const;
203   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
204   virtual Node* Identity(PhaseGVN* phase);
205   virtual uint ideal_reg() const { return Op_RegP; }
206   virtual const Type *bottom_type() const { return TypeRawPtr::BOTTOM; }
207 };
208 
209 //------------------------------CastP2XNode-------------------------------------
210 // Used in both 32-bit and 64-bit land.
211 // Used for card-marks and unsafe pointer math.
212 class CastP2XNode : public Node {
213   public:
214   CastP2XNode( Node *ctrl, Node *n ) : Node(ctrl, n) {}
215   virtual int Opcode() const;
216   virtual const Type* Value(PhaseGVN* phase) const;
217   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
218   virtual Node* Identity(PhaseGVN* phase);
219   virtual uint ideal_reg() const { return Op_RegX; }
220   virtual const Type *bottom_type() const { return TypeX_X; }
221   // Return false to keep node from moving away from an associated card mark.
222   virtual bool depends_only_on_test() const { return false; }
223 };
224 
225 
226 
227 #endif // SHARE_OPTO_CASTNODE_HPP