1 /*
2  * Copyright (c) 1997, 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_ADDNODE_HPP
26 #define SHARE_OPTO_ADDNODE_HPP
27 
28 #include "opto/node.hpp"
29 #include "opto/opcodes.hpp"
30 #include "opto/type.hpp"
31 
32 // Portions of code courtesy of Clifford Click
33 
34 class PhaseTransform;
35 
36 //------------------------------AddNode----------------------------------------
37 // Classic Add functionality.  This covers all the usual 'add' behaviors for
38 // an algebraic ring.  Add-integer, add-float, add-double, and binary-or are
39 // all inherited from this class.  The various identity values are supplied
40 // by virtual functions.
41 class AddNode : public Node {
42   virtual uint hash() const;
43 public:
AddNode(Node * in1,Node * in2)44   AddNode( Node *in1, Node *in2 ) : Node(0,in1,in2) {
45     init_class_id(Class_Add);
46   }
47 
48   // Handle algebraic identities here.  If we have an identity, return the Node
49   // we are equivalent to.  We look for "add of zero" as an identity.
50   virtual Node* Identity(PhaseGVN* phase);
51 
52   // We also canonicalize the Node, moving constants to the right input,
53   // and flatten expressions (so that 1+x+2 becomes x+3).
54   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
55 
56   // Compute a new Type for this node.  Basically we just do the pre-check,
57   // then call the virtual add() to set the type.
58   virtual const Type* Value(PhaseGVN* phase) const;
59 
60   // Check if this addition involves the additive identity
61   virtual const Type *add_of_identity( const Type *t1, const Type *t2 ) const;
62 
63   // Supplied function returns the sum of the inputs.
64   // This also type-checks the inputs for sanity.  Guaranteed never to
65   // be passed a TOP or BOTTOM type, these are filtered out by a pre-check.
66   virtual const Type *add_ring( const Type *, const Type * ) const = 0;
67 
68   // Supplied function to return the additive identity type
69   virtual const Type *add_id() const = 0;
70 
operates_on(BasicType bt,bool signed_int) const71   virtual bool operates_on(BasicType bt, bool signed_int) const {
72     assert(bt == T_INT || bt == T_LONG, "unsupported");
73     return false;
74   }
75   static AddNode* make(Node* in1, Node* in2, BasicType bt);
76 };
77 
78 //------------------------------AddINode---------------------------------------
79 // Add 2 integers
80 class AddINode : public AddNode {
81 public:
AddINode(Node * in1,Node * in2)82   AddINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
83   virtual int Opcode() const;
84   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const85   virtual const Type *add_id() const { return TypeInt::ZERO; }
bottom_type() const86   virtual const Type *bottom_type() const { return TypeInt::INT; }
87   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
88   virtual Node* Identity(PhaseGVN* phase);
operates_on(BasicType bt,bool signed_int) const89   virtual bool operates_on(BasicType bt, bool signed_int) const {
90     assert(bt == T_INT || bt == T_LONG, "unsupported");
91     return bt == T_INT;
92   }
ideal_reg() const93   virtual uint ideal_reg() const { return Op_RegI; }
94 };
95 
96 //------------------------------AddLNode---------------------------------------
97 // Add 2 longs
98 class AddLNode : public AddNode {
99 public:
AddLNode(Node * in1,Node * in2)100   AddLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
101   virtual int Opcode() const;
102   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const103   virtual const Type *add_id() const { return TypeLong::ZERO; }
bottom_type() const104   virtual const Type *bottom_type() const { return TypeLong::LONG; }
105   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
106   virtual Node* Identity(PhaseGVN* phase);
operates_on(BasicType bt,bool signed_int) const107   virtual bool operates_on(BasicType bt, bool signed_int) const {
108     assert(bt == T_INT || bt == T_LONG, "unsupported");
109     return bt == T_LONG;
110   }
ideal_reg() const111   virtual uint ideal_reg() const { return Op_RegL; }
112 };
113 
114 //------------------------------AddFNode---------------------------------------
115 // Add 2 floats
116 class AddFNode : public AddNode {
117 public:
AddFNode(Node * in1,Node * in2)118   AddFNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
119   virtual int Opcode() const;
120   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
121   virtual const Type *add_of_identity( const Type *t1, const Type *t2 ) const;
122   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const123   virtual const Type *add_id() const { return TypeF::ZERO; }
bottom_type() const124   virtual const Type *bottom_type() const { return Type::FLOAT; }
Identity(PhaseGVN * phase)125   virtual Node* Identity(PhaseGVN* phase) { return this; }
ideal_reg() const126   virtual uint ideal_reg() const { return Op_RegF; }
127 };
128 
129 //------------------------------AddDNode---------------------------------------
130 // Add 2 doubles
131 class AddDNode : public AddNode {
132 public:
AddDNode(Node * in1,Node * in2)133   AddDNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
134   virtual int Opcode() const;
135   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
136   virtual const Type *add_of_identity( const Type *t1, const Type *t2 ) const;
137   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const138   virtual const Type *add_id() const { return TypeD::ZERO; }
bottom_type() const139   virtual const Type *bottom_type() const { return Type::DOUBLE; }
Identity(PhaseGVN * phase)140   virtual Node* Identity(PhaseGVN* phase) { return this; }
ideal_reg() const141   virtual uint ideal_reg() const { return Op_RegD; }
142 };
143 
144 //------------------------------AddPNode---------------------------------------
145 // Add pointer plus integer to get pointer.  NOT commutative, really.
146 // So not really an AddNode.  Lives here, because people associate it with
147 // an add.
148 class AddPNode : public Node {
149 public:
150   enum { Control,               // When is it safe to do this add?
151          Base,                  // Base oop, for GC purposes
152          Address,               // Actually address, derived from base
153          Offset } ;             // Offset added to address
AddPNode(Node * base,Node * ptr,Node * off)154   AddPNode( Node *base, Node *ptr, Node *off ) : Node(0,base,ptr,off) {
155     init_class_id(Class_AddP);
156   }
157   virtual int Opcode() const;
158   virtual Node* Identity(PhaseGVN* phase);
159   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
160   virtual const Type* Value(PhaseGVN* phase) const;
161   virtual const Type *bottom_type() const;
ideal_reg() const162   virtual uint  ideal_reg() const { return Op_RegP; }
base_node()163   Node         *base_node() { assert( req() > Base, "Missing base"); return in(Base); }
164   static Node* Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
165                                      // second return value:
166                                      intptr_t& offset);
167 
168   // Collect the AddP offset values into the elements array, giving up
169   // if there are more than length.
170   int unpack_offsets(Node* elements[], int length);
171 
172   // Do not match base-ptr edge
173   virtual uint match_edge(uint idx) const;
174 };
175 
176 //------------------------------OrINode----------------------------------------
177 // Logically OR 2 integers.  Included with the ADD nodes because it inherits
178 // all the behavior of addition on a ring.
179 class OrINode : public AddNode {
180 public:
OrINode(Node * in1,Node * in2)181   OrINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
182   virtual int Opcode() const;
183   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const184   virtual const Type *add_id() const { return TypeInt::ZERO; }
bottom_type() const185   virtual const Type *bottom_type() const { return TypeInt::INT; }
186   virtual Node* Identity(PhaseGVN* phase);
ideal_reg() const187   virtual uint ideal_reg() const { return Op_RegI; }
188   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
189 };
190 
191 //------------------------------OrLNode----------------------------------------
192 // Logically OR 2 longs.  Included with the ADD nodes because it inherits
193 // all the behavior of addition on a ring.
194 class OrLNode : public AddNode {
195 public:
OrLNode(Node * in1,Node * in2)196   OrLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
197   virtual int Opcode() const;
198   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const199   virtual const Type *add_id() const { return TypeLong::ZERO; }
bottom_type() const200   virtual const Type *bottom_type() const { return TypeLong::LONG; }
201   virtual Node* Identity(PhaseGVN* phase);
ideal_reg() const202   virtual uint ideal_reg() const { return Op_RegL; }
203   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
204 };
205 
206 //------------------------------XorINode---------------------------------------
207 // XOR'ing 2 integers
208 class XorINode : public AddNode {
209 public:
XorINode(Node * in1,Node * in2)210   XorINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
211   virtual int Opcode() const;
212   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const213   virtual const Type *add_id() const { return TypeInt::ZERO; }
bottom_type() const214   virtual const Type *bottom_type() const { return TypeInt::INT; }
ideal_reg() const215   virtual uint ideal_reg() const { return Op_RegI; }
216 };
217 
218 //------------------------------XorINode---------------------------------------
219 // XOR'ing 2 longs
220 class XorLNode : public AddNode {
221 public:
XorLNode(Node * in1,Node * in2)222   XorLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
223   virtual int Opcode() const;
224   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const225   virtual const Type *add_id() const { return TypeLong::ZERO; }
bottom_type() const226   virtual const Type *bottom_type() const { return TypeLong::LONG; }
ideal_reg() const227   virtual uint ideal_reg() const { return Op_RegL; }
228 };
229 
230 //------------------------------MaxNode----------------------------------------
231 // Max (or min) of 2 values.  Included with the ADD nodes because it inherits
232 // all the behavior of addition on a ring.  Only new thing is that we allow
233 // 2 equal inputs to be equal.
234 class MaxNode : public AddNode {
235 private:
236   static Node* build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn);
237   static Node* build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn);
238 
239 public:
MaxNode(Node * in1,Node * in2)240   MaxNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
241   virtual int Opcode() const = 0;
242 
unsigned_max(Node * a,Node * b,const Type * t,PhaseGVN & gvn)243   static Node* unsigned_max(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
244     return build_min_max(a, b, true, true, t, gvn);
245   }
246 
unsigned_min(Node * a,Node * b,const Type * t,PhaseGVN & gvn)247   static Node* unsigned_min(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
248     return build_min_max(a, b, false, true, t, gvn);
249   }
250 
signed_max(Node * a,Node * b,const Type * t,PhaseGVN & gvn)251   static Node* signed_max(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
252     return build_min_max(a, b, true, false, t, gvn);
253   }
254 
signed_min(Node * a,Node * b,const Type * t,PhaseGVN & gvn)255   static Node* signed_min(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
256     return build_min_max(a, b, false, false, t, gvn);
257   }
258 
259   // max(a-b, 0)
max_diff_with_zero(Node * a,Node * b,const Type * t,PhaseGVN & gvn)260   static Node* max_diff_with_zero(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
261     return build_min_max_diff_with_zero(a, b, true, t, gvn);
262   }
263 
264   // min(a-b, 0)
min_diff_with_zero(Node * a,Node * b,const Type * t,PhaseGVN & gvn)265   static Node* min_diff_with_zero(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
266     return build_min_max_diff_with_zero(a, b, false, t, gvn);
267   }
268 };
269 
270 //------------------------------MaxINode---------------------------------------
271 // Maximum of 2 integers.  Included with the ADD nodes because it inherits
272 // all the behavior of addition on a ring.
273 class MaxINode : public MaxNode {
274 public:
MaxINode(Node * in1,Node * in2)275   MaxINode( Node *in1, Node *in2 ) : MaxNode(in1,in2) {}
276   virtual int Opcode() const;
277   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const278   virtual const Type *add_id() const { return TypeInt::make(min_jint); }
bottom_type() const279   virtual const Type *bottom_type() const { return TypeInt::INT; }
ideal_reg() const280   virtual uint ideal_reg() const { return Op_RegI; }
281 };
282 
283 //------------------------------MinINode---------------------------------------
284 // MINimum of 2 integers.  Included with the ADD nodes because it inherits
285 // all the behavior of addition on a ring.
286 class MinINode : public MaxNode {
287 public:
MinINode(Node * in1,Node * in2)288   MinINode( Node *in1, Node *in2 ) : MaxNode(in1,in2) {}
289   virtual int Opcode() const;
290   virtual const Type *add_ring( const Type *, const Type * ) const;
add_id() const291   virtual const Type *add_id() const { return TypeInt::make(max_jint); }
bottom_type() const292   virtual const Type *bottom_type() const { return TypeInt::INT; }
ideal_reg() const293   virtual uint ideal_reg() const { return Op_RegI; }
294   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
295 };
296 
297 //------------------------------MaxLNode---------------------------------------
298 // MAXimum of 2 longs.
299 class MaxLNode : public MaxNode {
300 public:
MaxLNode(Node * in1,Node * in2)301   MaxLNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
302   virtual int Opcode() const;
add_ring(const Type *,const Type *) const303   virtual const Type *add_ring(const Type*, const Type*) const { return TypeLong::LONG; }
add_id() const304   virtual const Type *add_id() const { return TypeLong::make(min_jlong); }
bottom_type() const305   virtual const Type *bottom_type() const { return TypeLong::LONG; }
ideal_reg() const306   virtual uint ideal_reg() const { return Op_RegL; }
307 };
308 
309 //------------------------------MinLNode---------------------------------------
310 // MINimum of 2 longs.
311 class MinLNode : public MaxNode {
312 public:
MinLNode(Node * in1,Node * in2)313   MinLNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
314   virtual int Opcode() const;
add_ring(const Type *,const Type *) const315   virtual const Type *add_ring(const Type*, const Type*) const { return TypeLong::LONG; }
add_id() const316   virtual const Type *add_id() const { return TypeLong::make(max_jlong); }
bottom_type() const317   virtual const Type *bottom_type() const { return TypeLong::LONG; }
ideal_reg() const318   virtual uint ideal_reg() const { return Op_RegL; }
319 };
320 
321 //------------------------------MaxFNode---------------------------------------
322 // Maximum of 2 floats.
323 class MaxFNode : public MaxNode {
324 public:
MaxFNode(Node * in1,Node * in2)325   MaxFNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
326   virtual int Opcode() const;
327   virtual const Type *add_ring(const Type*, const Type*) const;
add_id() const328   virtual const Type *add_id() const { return TypeF::NEG_INF; }
bottom_type() const329   virtual const Type *bottom_type() const { return Type::FLOAT; }
ideal_reg() const330   virtual uint ideal_reg() const { return Op_RegF; }
331 };
332 
333 //------------------------------MinFNode---------------------------------------
334 // Minimum of 2 floats.
335 class MinFNode : public MaxNode {
336 public:
MinFNode(Node * in1,Node * in2)337   MinFNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
338   virtual int Opcode() const;
339   virtual const Type *add_ring(const Type*, const Type*) const;
add_id() const340   virtual const Type *add_id() const { return TypeF::POS_INF; }
bottom_type() const341   virtual const Type *bottom_type() const { return Type::FLOAT; }
ideal_reg() const342   virtual uint ideal_reg() const { return Op_RegF; }
343 };
344 
345 //------------------------------MaxDNode---------------------------------------
346 // Maximum of 2 doubles.
347 class MaxDNode : public MaxNode {
348 public:
MaxDNode(Node * in1,Node * in2)349   MaxDNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
350   virtual int Opcode() const;
351   virtual const Type *add_ring(const Type*, const Type*) const;
add_id() const352   virtual const Type *add_id() const { return TypeD::NEG_INF; }
bottom_type() const353   virtual const Type *bottom_type() const { return Type::DOUBLE; }
ideal_reg() const354   virtual uint ideal_reg() const { return Op_RegD; }
355 };
356 
357 //------------------------------MinDNode---------------------------------------
358 // Minimum of 2 doubles.
359 class MinDNode : public MaxNode {
360 public:
MinDNode(Node * in1,Node * in2)361   MinDNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
362   virtual int Opcode() const;
363   virtual const Type *add_ring(const Type*, const Type*) const;
add_id() const364   virtual const Type *add_id() const { return TypeD::POS_INF; }
bottom_type() const365   virtual const Type *bottom_type() const { return Type::DOUBLE; }
ideal_reg() const366   virtual uint ideal_reg() const { return Op_RegD; }
367 };
368 
369 #endif // SHARE_OPTO_ADDNODE_HPP
370