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   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   // Visit all non-cast uses of the node, bypassing ConstraintCasts.
 74   // Pattern: this (-> ConstraintCast)* -> non_cast
 75   // In other words: find all non_cast nodes such that
 76   // non_cast->uncast() == this.
 77   template <typename Callback>
 78   static void visit_uncasted_uses(const Node* n, Callback callback) {
 79     ResourceMark rm;
 80     Unique_Node_List internals;
 81     internals.push((Node*)n); // start traversal
 82     for (uint j = 0; j < internals.size(); ++j) {
 83       Node* internal = internals.at(j); // for every internal
 84       for (DUIterator_Fast kmax, k = internal->fast_outs(kmax); k < kmax; k++) {
 85         Node* internal_use = internal->fast_out(k);
 86         if (internal_use->is_ConstraintCast()) {
 87           internals.push(internal_use); // traverse this cast also
 88         } else {
 89           callback(internal_use);
 90         }
 91       }
 92     }
 93   }
 94 };
 95 
 96 //------------------------------CastIINode-------------------------------------
 97 // cast integer to integer (different range)
 98 class CastIINode: public ConstraintCastNode {
 99   protected:
100   // Is this node dependent on a range check?
101   const bool _range_check_dependency;
102   virtual bool cmp(const Node &n) const;
103   virtual uint size_of() const;
104 
105   public:
106   CastIINode(Node* n, const Type* t, DependencyType dependency = RegularDependency, bool range_check_dependency = false)
107     : ConstraintCastNode(n, t, dependency), _range_check_dependency(range_check_dependency) {
108     init_class_id(Class_CastII);
109   }
110   CastIINode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency, bool range_check_dependency = false)
111     : ConstraintCastNode(n, t, dependency), _range_check_dependency(range_check_dependency) {
112     init_class_id(Class_CastII);
113     init_req(0, ctrl);
114   }
115   virtual int Opcode() const;
116   virtual uint ideal_reg() const { return Op_RegI; }
117   virtual Node* Identity(PhaseGVN* phase);
118   virtual const Type* Value(PhaseGVN* phase) const;
119   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
120   bool has_range_check() {
121 #ifdef _LP64
122     return _range_check_dependency;
123 #else
124     assert(!_range_check_dependency, "Should not have range check dependency");
125     return false;
126 #endif
127   }
128 
129 #ifndef PRODUCT
130   virtual void dump_spec(outputStream* st) const;
131 #endif
132 };
133 
134 class CastLLNode: public ConstraintCastNode {
135 public:
136   CastLLNode(Node* ctrl, Node* n, const Type* t, DependencyType dependency = RegularDependency)
137     : ConstraintCastNode(n, t, dependency) {
138     init_class_id(Class_CastLL);
139     init_req(0, ctrl);
140   }
141   CastLLNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
142           : ConstraintCastNode(n, t, dependency){
143     init_class_id(Class_CastLL);
144   }
145 
146   virtual const Type* Value(PhaseGVN* phase) const;
147   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
148   virtual int Opcode() const;
149   virtual uint ideal_reg() const { return Op_RegL; }
150 };
151 
152 class CastFFNode: public ConstraintCastNode {
153 public:
154   CastFFNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
155           : ConstraintCastNode(n, t, dependency){
156     init_class_id(Class_CastFF);
157   }
158   virtual int Opcode() const;
159   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
160 };
161 
162 class CastDDNode: public ConstraintCastNode {
163 public:
164   CastDDNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
165           : ConstraintCastNode(n, t, dependency){
166     init_class_id(Class_CastDD);
167   }
168   virtual int Opcode() const;
169   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
170 };
171 
172 class CastVVNode: public ConstraintCastNode {
173 public:
174   CastVVNode(Node* n, const Type* t, DependencyType dependency = RegularDependency)
175           : ConstraintCastNode(n, t, dependency){
176     init_class_id(Class_CastVV);
177   }
178   virtual int Opcode() const;
179   virtual uint ideal_reg() const { return in(1)->ideal_reg(); }
180 };
181 
182 
183 //------------------------------CastPPNode-------------------------------------
184 // cast pointer to pointer (different type)
185 class CastPPNode: public ConstraintCastNode {
186   public:
187   CastPPNode (Node *n, const Type *t, DependencyType dependency = RegularDependency)
188     : ConstraintCastNode(n, t, dependency) {
189   }
190   virtual int Opcode() const;
191   virtual uint ideal_reg() const { return Op_RegP; }
192 };
193 
194 //------------------------------CheckCastPPNode--------------------------------
195 // for _checkcast, cast pointer to pointer (different type), without JOIN,
196 class CheckCastPPNode: public ConstraintCastNode {
197   public:
198   CheckCastPPNode(Node *c, Node *n, const Type *t, DependencyType dependency = RegularDependency)
199     : ConstraintCastNode(n, t, dependency) {
200     init_class_id(Class_CheckCastPP);
201     init_req(0, c);
202   }
203 
204   virtual const Type* Value(PhaseGVN* phase) const;
205   virtual int   Opcode() const;
206   virtual uint  ideal_reg() const { return Op_RegP; }
207   bool depends_only_on_test() const { return !type()->isa_rawptr() && ConstraintCastNode::depends_only_on_test(); }
208  };
209 
210 
211 //------------------------------CastX2PNode-------------------------------------
212 // convert a machine-pointer-sized integer to a raw pointer
213 class CastX2PNode : public Node {
214   public:
215   CastX2PNode( Node *n ) : Node(nullptr, n) {}
216   virtual int Opcode() const;
217   virtual const Type* Value(PhaseGVN* phase) const;
218   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
219   virtual Node* Identity(PhaseGVN* phase);
220   virtual uint ideal_reg() const { return Op_RegP; }
221   virtual const Type *bottom_type() const { return TypeRawPtr::BOTTOM; }
222 };
223 
224 //------------------------------CastP2XNode-------------------------------------
225 // Used in both 32-bit and 64-bit land.
226 // Used for card-marks and unsafe pointer math.
227 class CastP2XNode : public Node {
228   public:
229   CastP2XNode( Node *ctrl, Node *n ) : Node(ctrl, n) {}
230   virtual int Opcode() const;
231   virtual const Type* Value(PhaseGVN* phase) const;
232   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
233   virtual Node* Identity(PhaseGVN* phase);
234   virtual uint ideal_reg() const { return Op_RegX; }
235   virtual const Type *bottom_type() const { return TypeX_X; }
236   // Return false to keep node from moving away from an associated card mark.
237   virtual bool depends_only_on_test() const { return false; }
238 };
239 
240 
241 
242 #endif // SHARE_OPTO_CASTNODE_HPP