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