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