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