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:
  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 
  71 };
  72 
  73 //------------------------------AddINode---------------------------------------
  74 // Add 2 integers
  75 class AddINode : public AddNode {
  76 public:
  77   AddINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
  78   virtual int Opcode() const;
  79   virtual const Type *add_ring( const Type *, const Type * ) const;
  80   virtual const Type *add_id() const { return TypeInt::ZERO; }
  81   virtual const Type *bottom_type() const { return TypeInt::INT; }
  82   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
  83   virtual Node* Identity(PhaseGVN* phase);
  84   virtual uint ideal_reg() const { return Op_RegI; }
  85 };
  86 
  87 //------------------------------AddLNode---------------------------------------
  88 // Add 2 longs
  89 class AddLNode : public AddNode {
  90 public:
  91   AddLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
  92   virtual int Opcode() const;
  93   virtual const Type *add_ring( const Type *, const Type * ) const;
  94   virtual const Type *add_id() const { return TypeLong::ZERO; }
  95   virtual const Type *bottom_type() const { return TypeLong::LONG; }
  96   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
  97   virtual Node* Identity(PhaseGVN* phase);
  98   virtual uint ideal_reg() const { return Op_RegL; }
  99 };
 100 
 101 //------------------------------AddFNode---------------------------------------
 102 // Add 2 floats
 103 class AddFNode : public AddNode {
 104 public:
 105   AddFNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 106   virtual int Opcode() const;
 107   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 108   virtual const Type *add_of_identity( const Type *t1, const Type *t2 ) const;
 109   virtual const Type *add_ring( const Type *, const Type * ) const;
 110   virtual const Type *add_id() const { return TypeF::ZERO; }
 111   virtual const Type *bottom_type() const { return Type::FLOAT; }
 112   virtual Node* Identity(PhaseGVN* phase) { return this; }
 113   virtual uint ideal_reg() const { return Op_RegF; }
 114 };
 115 
 116 //------------------------------AddDNode---------------------------------------
 117 // Add 2 doubles
 118 class AddDNode : public AddNode {
 119 public:
 120   AddDNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 121   virtual int Opcode() const;
 122   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 123   virtual const Type *add_of_identity( const Type *t1, const Type *t2 ) const;
 124   virtual const Type *add_ring( const Type *, const Type * ) const;
 125   virtual const Type *add_id() const { return TypeD::ZERO; }
 126   virtual const Type *bottom_type() const { return Type::DOUBLE; }
 127   virtual Node* Identity(PhaseGVN* phase) { return this; }
 128   virtual uint ideal_reg() const { return Op_RegD; }
 129 };
 130 
 131 //------------------------------AddPNode---------------------------------------
 132 // Add pointer plus integer to get pointer.  NOT commutative, really.
 133 // So not really an AddNode.  Lives here, because people associate it with
 134 // an add.
 135 class AddPNode : public Node {
 136 public:
 137   enum { Control,               // When is it safe to do this add?
 138          Base,                  // Base oop, for GC purposes
 139          Address,               // Actually address, derived from base
 140          Offset } ;             // Offset added to address
 141   AddPNode( Node *base, Node *ptr, Node *off ) : Node(0,base,ptr,off) {
 142     init_class_id(Class_AddP);
 143   }
 144   virtual int Opcode() const;
 145   virtual Node* Identity(PhaseGVN* phase);
 146   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 147   virtual const Type* Value(PhaseGVN* phase) const;
 148   virtual const Type *bottom_type() const;
 149   virtual uint  ideal_reg() const { return Op_RegP; }
 150   Node         *base_node() { assert( req() > Base, "Missing base"); return in(Base); }
 151   static Node* Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
 152                                      // second return value:
 153                                      intptr_t& offset);
 154 
 155   // Collect the AddP offset values into the elements array, giving up
 156   // if there are more than length.
 157   int unpack_offsets(Node* elements[], int length);
 158 
 159   // Do not match base-ptr edge
 160   virtual uint match_edge(uint idx) const;
 161 };
 162 
 163 //------------------------------OrINode----------------------------------------
 164 // Logically OR 2 integers.  Included with the ADD nodes because it inherits
 165 // all the behavior of addition on a ring.
 166 class OrINode : public AddNode {
 167 public:
 168   OrINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 169   virtual int Opcode() const;
 170   virtual const Type *add_ring( const Type *, const Type * ) const;
 171   virtual const Type *add_id() const { return TypeInt::ZERO; }
 172   virtual const Type *bottom_type() const { return TypeInt::INT; }
 173   virtual Node* Identity(PhaseGVN* phase);
 174   virtual uint ideal_reg() const { return Op_RegI; }
 175 };
 176 
 177 //------------------------------OrLNode----------------------------------------
 178 // Logically OR 2 longs.  Included with the ADD nodes because it inherits
 179 // all the behavior of addition on a ring.
 180 class OrLNode : public AddNode {
 181 public:
 182   OrLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 183   virtual int Opcode() const;
 184   virtual const Type *add_ring( const Type *, const Type * ) const;
 185   virtual const Type *add_id() const { return TypeLong::ZERO; }
 186   virtual const Type *bottom_type() const { return TypeLong::LONG; }
 187   virtual Node* Identity(PhaseGVN* phase);
 188   virtual uint ideal_reg() const { return Op_RegL; }
 189 };
 190 
 191 //------------------------------XorINode---------------------------------------
 192 // XOR'ing 2 integers
 193 class XorINode : public AddNode {
 194 public:
 195   XorINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 196   virtual int Opcode() const;
 197   virtual const Type *add_ring( const Type *, const Type * ) const;
 198   virtual const Type *add_id() const { return TypeInt::ZERO; }
 199   virtual const Type *bottom_type() const { return TypeInt::INT; }
 200   virtual uint ideal_reg() const { return Op_RegI; }
 201 };
 202 
 203 //------------------------------XorINode---------------------------------------
 204 // XOR'ing 2 longs
 205 class XorLNode : public AddNode {
 206 public:
 207   XorLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 208   virtual int Opcode() const;
 209   virtual const Type *add_ring( const Type *, const Type * ) const;
 210   virtual const Type *add_id() const { return TypeLong::ZERO; }
 211   virtual const Type *bottom_type() const { return TypeLong::LONG; }
 212   virtual uint ideal_reg() const { return Op_RegL; }
 213 };
 214 
 215 //------------------------------MaxNode----------------------------------------
 216 // Max (or min) of 2 values.  Included with the ADD nodes because it inherits
 217 // all the behavior of addition on a ring.  Only new thing is that we allow
 218 // 2 equal inputs to be equal.
 219 class MaxNode : public AddNode {
 220 private:
 221   static Node* build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn);
 222   static Node* build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn);
 223 
 224 public:
 225   MaxNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 226   virtual int Opcode() const = 0;
 227 
 228   static Node* unsigned_max(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
 229     return build_min_max(a, b, true, true, t, gvn);
 230   }
 231 
 232   static Node* unsigned_min(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
 233     return build_min_max(a, b, false, true, t, gvn);
 234   }
 235 
 236   static Node* signed_max(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
 237     return build_min_max(a, b, true, false, t, gvn);
 238   }
 239 
 240   static Node* signed_min(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
 241     return build_min_max(a, b, false, false, t, gvn);
 242   }
 243 
 244   // max(a-b, 0)
 245   static Node* max_diff_with_zero(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
 246     return build_min_max_diff_with_zero(a, b, true, t, gvn);
 247   }
 248 
 249   // min(a-b, 0)
 250   static Node* min_diff_with_zero(Node* a, Node* b, const Type* t, PhaseGVN& gvn) {
 251     return build_min_max_diff_with_zero(a, b, false, t, gvn);
 252   }
 253 };
 254 
 255 //------------------------------MaxINode---------------------------------------
 256 // Maximum of 2 integers.  Included with the ADD nodes because it inherits
 257 // all the behavior of addition on a ring.
 258 class MaxINode : public MaxNode {
 259 public:
 260   MaxINode( Node *in1, Node *in2 ) : MaxNode(in1,in2) {}
 261   virtual int Opcode() const;
 262   virtual const Type *add_ring( const Type *, const Type * ) const;
 263   virtual const Type *add_id() const { return TypeInt::make(min_jint); }
 264   virtual const Type *bottom_type() const { return TypeInt::INT; }
 265   virtual uint ideal_reg() const { return Op_RegI; }
 266 };
 267 
 268 //------------------------------MinINode---------------------------------------
 269 // MINimum of 2 integers.  Included with the ADD nodes because it inherits
 270 // all the behavior of addition on a ring.
 271 class MinINode : public MaxNode {
 272 public:
 273   MinINode( Node *in1, Node *in2 ) : MaxNode(in1,in2) {}
 274   virtual int Opcode() const;
 275   virtual const Type *add_ring( const Type *, const Type * ) const;
 276   virtual const Type *add_id() const { return TypeInt::make(max_jint); }
 277   virtual const Type *bottom_type() const { return TypeInt::INT; }
 278   virtual uint ideal_reg() const { return Op_RegI; }
 279   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 280 };
 281 
 282 //------------------------------MaxLNode---------------------------------------
 283 // MAXimum of 2 longs.
 284 class MaxLNode : public MaxNode {
 285 public:
 286   MaxLNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
 287   virtual int Opcode() const;
 288   virtual const Type *add_ring(const Type*, const Type*) const { return TypeLong::LONG; }
 289   virtual const Type *add_id() const { return TypeLong::make(min_jlong); }
 290   virtual const Type *bottom_type() const { return TypeLong::LONG; }
 291   virtual uint ideal_reg() const { return Op_RegL; }
 292 };
 293 
 294 //------------------------------MinLNode---------------------------------------
 295 // MINimum of 2 longs.
 296 class MinLNode : public MaxNode {
 297 public:
 298   MinLNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
 299   virtual int Opcode() const;
 300   virtual const Type *add_ring(const Type*, const Type*) const { return TypeLong::LONG; }
 301   virtual const Type *add_id() const { return TypeLong::make(max_jlong); }
 302   virtual const Type *bottom_type() const { return TypeLong::LONG; }
 303   virtual uint ideal_reg() const { return Op_RegL; }
 304 };
 305 
 306 //------------------------------MaxFNode---------------------------------------
 307 // Maximum of 2 floats.
 308 class MaxFNode : public MaxNode {
 309 public:
 310   MaxFNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
 311   virtual int Opcode() const;
 312   virtual const Type *add_ring(const Type*, const Type*) const;
 313   virtual const Type *add_id() const { return TypeF::NEG_INF; }
 314   virtual const Type *bottom_type() const { return Type::FLOAT; }
 315   virtual uint ideal_reg() const { return Op_RegF; }
 316 };
 317 
 318 //------------------------------MinFNode---------------------------------------
 319 // Minimum of 2 floats.
 320 class MinFNode : public MaxNode {
 321 public:
 322   MinFNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
 323   virtual int Opcode() const;
 324   virtual const Type *add_ring(const Type*, const Type*) const;
 325   virtual const Type *add_id() const { return TypeF::POS_INF; }
 326   virtual const Type *bottom_type() const { return Type::FLOAT; }
 327   virtual uint ideal_reg() const { return Op_RegF; }
 328 };
 329 
 330 //------------------------------MaxDNode---------------------------------------
 331 // Maximum of 2 doubles.
 332 class MaxDNode : public MaxNode {
 333 public:
 334   MaxDNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
 335   virtual int Opcode() const;
 336   virtual const Type *add_ring(const Type*, const Type*) const;
 337   virtual const Type *add_id() const { return TypeD::NEG_INF; }
 338   virtual const Type *bottom_type() const { return Type::DOUBLE; }
 339   virtual uint ideal_reg() const { return Op_RegD; }
 340 };
 341 
 342 //------------------------------MinDNode---------------------------------------
 343 // Minimum of 2 doubles.
 344 class MinDNode : public MaxNode {
 345 public:
 346   MinDNode(Node *in1, Node *in2) : MaxNode(in1, in2) {}
 347   virtual int Opcode() const;
 348   virtual const Type *add_ring(const Type*, const Type*) const;
 349   virtual const Type *add_id() const { return TypeD::POS_INF; }
 350   virtual const Type *bottom_type() const { return Type::DOUBLE; }
 351   virtual uint ideal_reg() const { return Op_RegD; }
 352 };
 353 
 354 #endif // SHARE_OPTO_ADDNODE_HPP