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