1 /*
   2  * Copyright (c) 1997, 2015, 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_VM_OPTO_ADDNODE_HPP
  26 #define SHARE_VM_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( PhaseTransform *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( PhaseTransform *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 #ifndef PRODUCT
  72   REL_IN_DATA_OUT_1;
  73 #endif
  74 
  75 };
  76 
  77 //------------------------------AddINode---------------------------------------
  78 // Add 2 integers
  79 class AddINode : public AddNode {
  80 public:
  81   AddINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
  82   virtual int Opcode() const;
  83   virtual const Type *add_ring( const Type *, const Type * ) const;
  84   virtual const Type *add_id() const { return TypeInt::ZERO; }
  85   virtual const Type *bottom_type() const { return TypeInt::INT; }
  86   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
  87   virtual Node *Identity( PhaseTransform *phase );
  88   virtual uint ideal_reg() const { return Op_RegI; }
  89 };
  90 
  91 //------------------------------AddLNode---------------------------------------
  92 // Add 2 longs
  93 class AddLNode : public AddNode {
  94 public:
  95   AddLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
  96   virtual int Opcode() const;
  97   virtual const Type *add_ring( const Type *, const Type * ) const;
  98   virtual const Type *add_id() const { return TypeLong::ZERO; }
  99   virtual const Type *bottom_type() const { return TypeLong::LONG; }
 100   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 101   virtual Node *Identity( PhaseTransform *phase );
 102   virtual uint ideal_reg() const { return Op_RegL; }
 103 };
 104 
 105 //------------------------------AddFNode---------------------------------------
 106 // Add 2 floats
 107 class AddFNode : public AddNode {
 108 public:
 109   AddFNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 110   virtual int Opcode() const;
 111   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 112   virtual const Type *add_of_identity( const Type *t1, const Type *t2 ) const;
 113   virtual const Type *add_ring( const Type *, const Type * ) const;
 114   virtual const Type *add_id() const { return TypeF::ZERO; }
 115   virtual const Type *bottom_type() const { return Type::FLOAT; }
 116   virtual Node *Identity( PhaseTransform *phase ) { return this; }
 117   virtual uint ideal_reg() const { return Op_RegF; }
 118 };
 119 
 120 //------------------------------AddDNode---------------------------------------
 121 // Add 2 doubles
 122 class AddDNode : public AddNode {
 123 public:
 124   AddDNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 125   virtual int Opcode() const;
 126   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 127   virtual const Type *add_of_identity( const Type *t1, const Type *t2 ) const;
 128   virtual const Type *add_ring( const Type *, const Type * ) const;
 129   virtual const Type *add_id() const { return TypeD::ZERO; }
 130   virtual const Type *bottom_type() const { return Type::DOUBLE; }
 131   virtual Node *Identity( PhaseTransform *phase ) { return this; }
 132   virtual uint ideal_reg() const { return Op_RegD; }
 133 };
 134 
 135 //------------------------------AddPNode---------------------------------------
 136 // Add pointer plus integer to get pointer.  NOT commutative, really.
 137 // So not really an AddNode.  Lives here, because people associate it with
 138 // an add.
 139 class AddPNode : public Node {
 140 public:
 141   enum { Control,               // When is it safe to do this add?
 142          Base,                  // Base oop, for GC purposes
 143          Address,               // Actually address, derived from base
 144          Offset } ;             // Offset added to address
 145   AddPNode( Node *base, Node *ptr, Node *off ) : Node(0,base,ptr,off) {
 146     init_class_id(Class_AddP);
 147   }
 148   virtual int Opcode() const;
 149   virtual Node *Identity( PhaseTransform *phase );
 150   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 151   virtual const Type *Value( PhaseTransform *phase ) const;
 152   virtual const Type *bottom_type() const;
 153   virtual uint  ideal_reg() const { return Op_RegP; }
 154   Node         *base_node() { assert( req() > Base, "Missing base"); return in(Base); }
 155   static Node* Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
 156                                      // second return value:
 157                                      intptr_t& offset);
 158 
 159   // Collect the AddP offset values into the elements array, giving up
 160   // if there are more than length.
 161   int unpack_offsets(Node* elements[], int length);
 162 
 163   // Do not match base-ptr edge
 164   virtual uint match_edge(uint idx) const;
 165 
 166 #ifndef PRODUCT
 167   REL_IN_DATA_OUT_1;
 168 #endif
 169 };
 170 
 171 //------------------------------OrINode----------------------------------------
 172 // Logically OR 2 integers.  Included with the ADD nodes because it inherits
 173 // all the behavior of addition on a ring.
 174 class OrINode : public AddNode {
 175 public:
 176   OrINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 177   virtual int Opcode() const;
 178   virtual const Type *add_ring( const Type *, const Type * ) const;
 179   virtual const Type *add_id() const { return TypeInt::ZERO; }
 180   virtual const Type *bottom_type() const { return TypeInt::INT; }
 181   virtual Node *Identity( PhaseTransform *phase );
 182   virtual uint ideal_reg() const { return Op_RegI; }
 183 };
 184 
 185 //------------------------------OrLNode----------------------------------------
 186 // Logically OR 2 longs.  Included with the ADD nodes because it inherits
 187 // all the behavior of addition on a ring.
 188 class OrLNode : public AddNode {
 189 public:
 190   OrLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 191   virtual int Opcode() const;
 192   virtual const Type *add_ring( const Type *, const Type * ) const;
 193   virtual const Type *add_id() const { return TypeLong::ZERO; }
 194   virtual const Type *bottom_type() const { return TypeLong::LONG; }
 195   virtual Node *Identity( PhaseTransform *phase );
 196   virtual uint ideal_reg() const { return Op_RegL; }
 197 };
 198 
 199 //------------------------------XorINode---------------------------------------
 200 // XOR'ing 2 integers
 201 class XorINode : public AddNode {
 202 public:
 203   XorINode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 204   virtual int Opcode() const;
 205   virtual const Type *add_ring( const Type *, const Type * ) const;
 206   virtual const Type *add_id() const { return TypeInt::ZERO; }
 207   virtual const Type *bottom_type() const { return TypeInt::INT; }
 208   virtual uint ideal_reg() const { return Op_RegI; }
 209 };
 210 
 211 //------------------------------XorINode---------------------------------------
 212 // XOR'ing 2 longs
 213 class XorLNode : public AddNode {
 214 public:
 215   XorLNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 216   virtual int Opcode() const;
 217   virtual const Type *add_ring( const Type *, const Type * ) const;
 218   virtual const Type *add_id() const { return TypeLong::ZERO; }
 219   virtual const Type *bottom_type() const { return TypeLong::LONG; }
 220   virtual uint ideal_reg() const { return Op_RegL; }
 221 };
 222 
 223 //------------------------------MaxNode----------------------------------------
 224 // Max (or min) of 2 values.  Included with the ADD nodes because it inherits
 225 // all the behavior of addition on a ring.  Only new thing is that we allow
 226 // 2 equal inputs to be equal.
 227 class MaxNode : public AddNode {
 228 public:
 229   MaxNode( Node *in1, Node *in2 ) : AddNode(in1,in2) {}
 230   virtual int Opcode() const = 0;
 231 };
 232 
 233 //------------------------------MaxINode---------------------------------------
 234 // Maximum of 2 integers.  Included with the ADD nodes because it inherits
 235 // all the behavior of addition on a ring.
 236 class MaxINode : public MaxNode {
 237 public:
 238   MaxINode( Node *in1, Node *in2 ) : MaxNode(in1,in2) {}
 239   virtual int Opcode() const;
 240   virtual const Type *add_ring( const Type *, const Type * ) const;
 241   virtual const Type *add_id() const { return TypeInt::make(min_jint); }
 242   virtual const Type *bottom_type() const { return TypeInt::INT; }
 243   virtual uint ideal_reg() const { return Op_RegI; }
 244 };
 245 
 246 //------------------------------MinINode---------------------------------------
 247 // MINimum of 2 integers.  Included with the ADD nodes because it inherits
 248 // all the behavior of addition on a ring.
 249 class MinINode : public MaxNode {
 250 public:
 251   MinINode( Node *in1, Node *in2 ) : MaxNode(in1,in2) {}
 252   virtual int Opcode() const;
 253   virtual const Type *add_ring( const Type *, const Type * ) const;
 254   virtual const Type *add_id() const { return TypeInt::make(max_jint); }
 255   virtual const Type *bottom_type() const { return TypeInt::INT; }
 256   virtual uint ideal_reg() const { return Op_RegI; }
 257   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 258 };
 259 
 260 #endif // SHARE_VM_OPTO_ADDNODE_HPP