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_MULNODE_HPP
  26 #define SHARE_VM_OPTO_MULNODE_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 //------------------------------MulNode----------------------------------------
  37 // Classic MULTIPLY functionality.  This covers all the usual 'multiply'
  38 // behaviors for an algebraic ring.  Multiply-integer, multiply-float,
  39 // multiply-double, and binary-and are all inherited from this class.  The
  40 // various identity values are supplied by virtual functions.
  41 class MulNode : public Node {
  42   virtual uint hash() const;
  43 public:
  44   MulNode( Node *in1, Node *in2 ): Node(0,in1,in2) {
  45     init_class_id(Class_Mul);
  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   // Supplied function returns the product of the inputs.
  61   // This also type-checks the inputs for sanity.  Guaranteed never to
  62   // be passed a TOP or BOTTOM type, these are filtered out by a pre-check.
  63   // This call recognizes the multiplicative zero type.
  64   virtual const Type *mul_ring( const Type *, const Type * ) const = 0;
  65 
  66   // Supplied function to return the multiplicative identity type
  67   virtual const Type *mul_id() const = 0;
  68 
  69   // Supplied function to return the additive identity type
  70   virtual const Type *add_id() const = 0;
  71 
  72   // Supplied function to return the additive opcode
  73   virtual int add_opcode() const = 0;
  74 
  75   // Supplied function to return the multiplicative opcode
  76   virtual int mul_opcode() const = 0;
  77 
  78 #ifndef PRODUCT
  79   REL_IN_DATA_OUT_1;
  80 #endif
  81 };
  82 
  83 //------------------------------MulINode---------------------------------------
  84 // Multiply 2 integers
  85 class MulINode : public MulNode {
  86 public:
  87   MulINode( Node *in1, Node *in2 ) : MulNode(in1,in2) {}
  88   virtual int Opcode() const;
  89   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
  90   virtual const Type *mul_ring( const Type *, const Type * ) const;
  91   const Type *mul_id() const { return TypeInt::ONE; }
  92   const Type *add_id() const { return TypeInt::ZERO; }
  93   int add_opcode() const { return Op_AddI; }
  94   int mul_opcode() const { return Op_MulI; }
  95   const Type *bottom_type() const { return TypeInt::INT; }
  96   virtual uint ideal_reg() const { return Op_RegI; }
  97 };
  98 
  99 //------------------------------MulLNode---------------------------------------
 100 // Multiply 2 longs
 101 class MulLNode : public MulNode {
 102 public:
 103   MulLNode( Node *in1, Node *in2 ) : MulNode(in1,in2) {}
 104   virtual int Opcode() const;
 105   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 106   virtual const Type *mul_ring( const Type *, const Type * ) const;
 107   const Type *mul_id() const { return TypeLong::ONE; }
 108   const Type *add_id() const { return TypeLong::ZERO; }
 109   int add_opcode() const { return Op_AddL; }
 110   int mul_opcode() const { return Op_MulL; }
 111   const Type *bottom_type() const { return TypeLong::LONG; }
 112   virtual uint ideal_reg() const { return Op_RegL; }
 113 };
 114 
 115 
 116 //------------------------------MulFNode---------------------------------------
 117 // Multiply 2 floats
 118 class MulFNode : public MulNode {
 119 public:
 120   MulFNode( Node *in1, Node *in2 ) : MulNode(in1,in2) {}
 121   virtual int Opcode() const;
 122   virtual const Type *mul_ring( const Type *, const Type * ) const;
 123   const Type *mul_id() const { return TypeF::ONE; }
 124   const Type *add_id() const { return TypeF::ZERO; }
 125   int add_opcode() const { return Op_AddF; }
 126   int mul_opcode() const { return Op_MulF; }
 127   const Type *bottom_type() const { return Type::FLOAT; }
 128   virtual uint ideal_reg() const { return Op_RegF; }
 129 };
 130 
 131 //------------------------------MulDNode---------------------------------------
 132 // Multiply 2 doubles
 133 class MulDNode : public MulNode {
 134 public:
 135   MulDNode( Node *in1, Node *in2 ) : MulNode(in1,in2) {}
 136   virtual int Opcode() const;
 137   virtual const Type *mul_ring( const Type *, const Type * ) const;
 138   const Type *mul_id() const { return TypeD::ONE; }
 139   const Type *add_id() const { return TypeD::ZERO; }
 140   int add_opcode() const { return Op_AddD; }
 141   int mul_opcode() const { return Op_MulD; }
 142   const Type *bottom_type() const { return Type::DOUBLE; }
 143   virtual uint ideal_reg() const { return Op_RegD; }
 144 };
 145 
 146 //-------------------------------MulHiLNode------------------------------------
 147 // Upper 64 bits of a 64 bit by 64 bit multiply
 148 class MulHiLNode : public Node {
 149 public:
 150   MulHiLNode( Node *in1, Node *in2 ) : Node(0,in1,in2) {}
 151   virtual int Opcode() const;
 152   virtual const Type *Value( PhaseTransform *phase ) const;
 153   const Type *bottom_type() const { return TypeLong::LONG; }
 154   virtual uint ideal_reg() const { return Op_RegL; }
 155 
 156 #ifndef PRODUCT
 157   REL_IN_DATA_OUT_1;
 158 #endif
 159 };
 160 
 161 //------------------------------AndINode---------------------------------------
 162 // Logically AND 2 integers.  Included with the MUL nodes because it inherits
 163 // all the behavior of multiplication on a ring.
 164 class AndINode : public MulINode {
 165 public:
 166   AndINode( Node *in1, Node *in2 ) : MulINode(in1,in2) {}
 167   virtual int Opcode() const;
 168   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 169   virtual Node *Identity( PhaseTransform *phase );
 170   virtual const Type *mul_ring( const Type *, const Type * ) const;
 171   const Type *mul_id() const { return TypeInt::MINUS_1; }
 172   const Type *add_id() const { return TypeInt::ZERO; }
 173   int add_opcode() const { return Op_OrI; }
 174   int mul_opcode() const { return Op_AndI; }
 175   virtual uint ideal_reg() const { return Op_RegI; }
 176 };
 177 
 178 //------------------------------AndINode---------------------------------------
 179 // Logically AND 2 longs.  Included with the MUL nodes because it inherits
 180 // all the behavior of multiplication on a ring.
 181 class AndLNode : public MulLNode {
 182 public:
 183   AndLNode( Node *in1, Node *in2 ) : MulLNode(in1,in2) {}
 184   virtual int Opcode() const;
 185   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 186   virtual Node *Identity( PhaseTransform *phase );
 187   virtual const Type *mul_ring( const Type *, const Type * ) const;
 188   const Type *mul_id() const { return TypeLong::MINUS_1; }
 189   const Type *add_id() const { return TypeLong::ZERO; }
 190   int add_opcode() const { return Op_OrL; }
 191   int mul_opcode() const { return Op_AndL; }
 192   virtual uint ideal_reg() const { return Op_RegL; }
 193 };
 194 
 195 //------------------------------LShiftINode------------------------------------
 196 // Logical shift left
 197 class LShiftINode : public Node {
 198 public:
 199   LShiftINode( Node *in1, Node *in2 ) : Node(0,in1,in2) {}
 200   virtual int Opcode() const;
 201   virtual Node *Identity( PhaseTransform *phase );
 202   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 203   virtual const Type *Value( PhaseTransform *phase ) const;
 204   const Type *bottom_type() const { return TypeInt::INT; }
 205   virtual uint ideal_reg() const { return Op_RegI; }
 206 
 207 #ifndef PRODUCT
 208   REL_IN_DATA_OUT_1;
 209 #endif
 210 };
 211 
 212 //------------------------------LShiftLNode------------------------------------
 213 // Logical shift left
 214 class LShiftLNode : public Node {
 215 public:
 216   LShiftLNode( Node *in1, Node *in2 ) : Node(0,in1,in2) {}
 217   virtual int Opcode() const;
 218   virtual Node *Identity( PhaseTransform *phase );
 219   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 220   virtual const Type *Value( PhaseTransform *phase ) const;
 221   const Type *bottom_type() const { return TypeLong::LONG; }
 222   virtual uint ideal_reg() const { return Op_RegL; }
 223 
 224 #ifndef PRODUCT
 225   REL_IN_DATA_OUT_1;
 226 #endif
 227 };
 228 
 229 //------------------------------RShiftINode------------------------------------
 230 // Signed shift right
 231 class RShiftINode : public Node {
 232 public:
 233   RShiftINode( Node *in1, Node *in2 ) : Node(0,in1,in2) {}
 234   virtual int Opcode() const;
 235   virtual Node *Identity( PhaseTransform *phase );
 236   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 237   virtual const Type *Value( PhaseTransform *phase ) const;
 238   const Type *bottom_type() const { return TypeInt::INT; }
 239   virtual uint ideal_reg() const { return Op_RegI; }
 240 
 241 #ifndef PRODUCT
 242   REL_IN_DATA_OUT_1;
 243 #endif
 244 };
 245 
 246 //------------------------------RShiftLNode------------------------------------
 247 // Signed shift right
 248 class RShiftLNode : public Node {
 249 public:
 250   RShiftLNode( Node *in1, Node *in2 ) : Node(0,in1,in2) {}
 251   virtual int Opcode() const;
 252   virtual Node *Identity( PhaseTransform *phase );
 253   virtual const Type *Value( PhaseTransform *phase ) const;
 254   const Type *bottom_type() const { return TypeLong::LONG; }
 255   virtual uint ideal_reg() const { return Op_RegL; }
 256 
 257 #ifndef PRODUCT
 258   REL_IN_DATA_OUT_1;
 259 #endif
 260 };
 261 
 262 
 263 //------------------------------URShiftINode-----------------------------------
 264 // Logical shift right
 265 class URShiftINode : public Node {
 266 public:
 267   URShiftINode( Node *in1, Node *in2 ) : Node(0,in1,in2) {}
 268   virtual int Opcode() const;
 269   virtual Node *Identity( PhaseTransform *phase );
 270   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 271   virtual const Type *Value( PhaseTransform *phase ) const;
 272   const Type *bottom_type() const { return TypeInt::INT; }
 273   virtual uint ideal_reg() const { return Op_RegI; }
 274 
 275 #ifndef PRODUCT
 276   REL_IN_DATA_OUT_1;
 277 #endif
 278 };
 279 
 280 //------------------------------URShiftLNode-----------------------------------
 281 // Logical shift right
 282 class URShiftLNode : public Node {
 283 public:
 284   URShiftLNode( Node *in1, Node *in2 ) : Node(0,in1,in2) {}
 285   virtual int Opcode() const;
 286   virtual Node *Identity( PhaseTransform *phase );
 287   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 288   virtual const Type *Value( PhaseTransform *phase ) const;
 289   const Type *bottom_type() const { return TypeLong::LONG; }
 290   virtual uint ideal_reg() const { return Op_RegL; }
 291 
 292 #ifndef PRODUCT
 293   REL_IN_DATA_OUT_1;
 294 #endif
 295 };
 296 
 297 #endif // SHARE_VM_OPTO_MULNODE_HPP