1 /*
   2  * Copyright (c) 2016, 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;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  29 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  30 import org.graalvm.compiler.core.common.type.FloatStamp;
  31 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
  32 import org.graalvm.compiler.core.common.type.Stamp;
  33 import org.graalvm.compiler.core.common.type.StampFactory;
  34 import org.graalvm.compiler.debug.GraalError;
  35 import org.graalvm.compiler.graph.NodeClass;
  36 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  37 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  38 import org.graalvm.compiler.nodeinfo.NodeInfo;
  39 import org.graalvm.compiler.nodes.ConstantNode;
  40 import org.graalvm.compiler.nodes.NodeView;
  41 import org.graalvm.compiler.nodes.ValueNode;
  42 import org.graalvm.compiler.nodes.calc.BinaryNode;
  43 import org.graalvm.compiler.nodes.calc.FloatDivNode;
  44 import org.graalvm.compiler.nodes.calc.MulNode;
  45 import org.graalvm.compiler.nodes.calc.SqrtNode;
  46 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
  47 import org.graalvm.compiler.nodes.spi.Lowerable;
  48 import org.graalvm.compiler.nodes.spi.LoweringTool;
  49 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  50 
  51 import jdk.vm.ci.meta.JavaKind;
  52 import jdk.vm.ci.meta.Value;
  53 
  54 @NodeInfo(nameTemplate = "MathIntrinsic#{p#operation/s}", cycles = CYCLES_UNKNOWN, size = SIZE_1)
  55 public final class BinaryMathIntrinsicNode extends BinaryNode implements ArithmeticLIRLowerable, Lowerable {
  56 
  57     public static final NodeClass<BinaryMathIntrinsicNode> TYPE = NodeClass.create(BinaryMathIntrinsicNode.class);
  58     protected final BinaryOperation operation;
  59 
  60     public enum BinaryOperation {
  61         POW(new ForeignCallDescriptor("arithmeticPow", double.class, double.class, double.class));
  62 
  63         public final ForeignCallDescriptor foreignCallDescriptor;
  64 
  65         BinaryOperation(ForeignCallDescriptor foreignCallDescriptor) {
  66             this.foreignCallDescriptor = foreignCallDescriptor;
  67         }
  68     }
  69 
  70     public BinaryOperation getOperation() {
  71         return operation;
  72     }
  73 
  74     public static ValueNode create(ValueNode forX, ValueNode forY, BinaryOperation op) {
  75         ValueNode c = tryConstantFold(forX, forY, op);
  76         if (c != null) {
  77             return c;
  78         }
  79         return new BinaryMathIntrinsicNode(forX, forY, op);
  80     }
  81 
  82     protected static ValueNode tryConstantFold(ValueNode forX, ValueNode forY, BinaryOperation op) {
  83         if (forX.isConstant() && forY.isConstant()) {
  84             double ret = doCompute(forX.asJavaConstant().asDouble(), forY.asJavaConstant().asDouble(), op);
  85             return ConstantNode.forDouble(ret);
  86         }
  87         return null;
  88     }
  89 
  90     @Override
  91     public Stamp foldStamp(Stamp stampX, Stamp stampY) {
  92         return stamp(NodeView.DEFAULT);
  93     }
  94 
  95     protected BinaryMathIntrinsicNode(ValueNode forX, ValueNode forY, BinaryOperation op) {
  96         super(TYPE, StampFactory.forKind(JavaKind.Double), forX, forY);
  97         assert forX.stamp(NodeView.DEFAULT) instanceof FloatStamp && PrimitiveStamp.getBits(forX.stamp(NodeView.DEFAULT)) == 64;
  98         assert forY.stamp(NodeView.DEFAULT) instanceof FloatStamp && PrimitiveStamp.getBits(forY.stamp(NodeView.DEFAULT)) == 64;
  99         this.operation = op;
 100     }
 101 
 102     @Override
 103     public void lower(LoweringTool tool) {
 104         tool.getLowerer().lower(this, tool);
 105     }
 106 
 107     @Override
 108     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
 109         Value xValue = nodeValueMap.operand(getX());
 110         Value yValue = nodeValueMap.operand(getY());
 111         Value result;
 112         switch (getOperation()) {
 113             case POW:
 114                 result = gen.emitMathPow(xValue, yValue);
 115                 break;
 116             default:
 117                 throw GraalError.shouldNotReachHere();
 118         }
 119         nodeValueMap.setResult(this, result);
 120     }
 121 
 122     @Override
 123     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
 124         NodeView view = NodeView.from(tool);
 125         ValueNode c = tryConstantFold(forX, forY, getOperation());
 126         if (c != null) {
 127             return c;
 128         }
 129         if (forY.isConstant()) {
 130             double yValue = forY.asJavaConstant().asDouble();
 131             // If the second argument is positive or negative zero, then the result is 1.0.
 132             if (yValue == 0.0D) {
 133                 return ConstantNode.forDouble(1);
 134             }
 135 
 136             // If the second argument is 1.0, then the result is the same as the first argument.
 137             if (yValue == 1.0D) {
 138                 return x;
 139             }
 140 
 141             // If the second argument is NaN, then the result is NaN.
 142             if (Double.isNaN(yValue)) {
 143                 return ConstantNode.forDouble(Double.NaN);
 144             }
 145 
 146             // x**-1 = 1/x
 147             if (yValue == -1.0D) {
 148                 return new FloatDivNode(ConstantNode.forDouble(1), x);
 149             }
 150 
 151             // x**2 = x*x
 152             if (yValue == 2.0D) {
 153                 return new MulNode(x, x);
 154             }
 155 
 156             // x**0.5 = sqrt(x)
 157             if (yValue == 0.5D && x.stamp(view) instanceof FloatStamp && ((FloatStamp) x.stamp(view)).lowerBound() >= 0.0D) {
 158                 return SqrtNode.create(x, view);
 159             }
 160         }
 161         return this;
 162     }
 163 
 164     @NodeIntrinsic
 165     public static native double compute(double x, double y, @ConstantNodeParameter BinaryOperation op);
 166 
 167     private static double doCompute(double x, double y, BinaryOperation op) {
 168         switch (op) {
 169             case POW:
 170                 return Math.pow(x, y);
 171             default:
 172                 throw new GraalError("unknown op %s", op);
 173         }
 174     }
 175 
 176 }