1 /*
   2  * Copyright (c) 2014, 2018, 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 package org.graalvm.compiler.replacements.nodes.arithmetic;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  29 
  30 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
  31 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.MulHigh;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.Canonicalizable;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.ConstantNode;
  38 import org.graalvm.compiler.nodes.NodeView;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode;
  41 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  42 
  43 import jdk.vm.ci.meta.Constant;
  44 import jdk.vm.ci.meta.PrimitiveConstant;
  45 import jdk.vm.ci.meta.Value;
  46 
  47 @NodeInfo(shortName = "*H", cycles = CYCLES_2, size = SIZE_2)
  48 public final class IntegerMulHighNode extends BinaryArithmeticNode<MulHigh> implements Canonicalizable.BinaryCommutative<ValueNode> {
  49     public static final NodeClass<IntegerMulHighNode> TYPE = NodeClass.create(IntegerMulHighNode.class);
  50 
  51     public IntegerMulHighNode(ValueNode x, ValueNode y) {
  52         super(TYPE, ArithmeticOpTable::getMulHigh, x, y);
  53     }
  54 
  55     @Override
  56     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
  57         Value a = nodeValueMap.operand(getX());
  58         Value b = nodeValueMap.operand(getY());
  59         nodeValueMap.setResult(this, gen.emitMulHigh(a, b));
  60     }
  61 
  62     @Override
  63     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  64         ValueNode ret = super.canonical(tool, forX, forY);
  65         if (ret != this) {
  66             return ret;
  67         }
  68 
  69         if (forX.isConstant() && !forY.isConstant()) {
  70             // we try to swap and canonicalize
  71             ValueNode improvement = canonical(tool, forY, forX);
  72             if (improvement != this) {
  73                 return improvement;
  74             }
  75             // if this fails we only swap
  76             return new IntegerMulHighNode(forY, forX);
  77         }
  78         return canonical(this, forY);
  79     }
  80 
  81     private static ValueNode canonical(IntegerMulHighNode self, ValueNode forY) {
  82         if (forY.isConstant()) {
  83             Constant c = forY.asConstant();
  84             if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) {
  85                 long i = ((PrimitiveConstant) c).asLong();
  86                 if (i == 0 || i == 1) {
  87                     return ConstantNode.forIntegerStamp(self.stamp(NodeView.DEFAULT), 0);
  88                 }
  89             }
  90         }
  91         return self;
  92     }
  93 }