1 /*
   2  * Copyright (c) 2012, 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_64;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  29 
  30 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  31 import org.graalvm.compiler.core.common.type.FloatStamp;
  32 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
  33 import org.graalvm.compiler.core.common.type.Stamp;
  34 import org.graalvm.compiler.core.common.type.StampFactory;
  35 import org.graalvm.compiler.debug.GraalError;
  36 import org.graalvm.compiler.graph.NodeClass;
  37 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  38 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  39 import org.graalvm.compiler.nodeinfo.NodeInfo;
  40 import org.graalvm.compiler.nodes.ConstantNode;
  41 import org.graalvm.compiler.nodes.NodeView;
  42 import org.graalvm.compiler.nodes.ValueNode;
  43 import org.graalvm.compiler.nodes.calc.UnaryNode;
  44 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
  45 import org.graalvm.compiler.nodes.spi.Lowerable;
  46 import org.graalvm.compiler.nodes.spi.LoweringTool;
  47 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  48 
  49 import jdk.vm.ci.meta.JavaKind;
  50 import jdk.vm.ci.meta.Value;
  51 
  52 @NodeInfo(nameTemplate = "MathIntrinsic#{p#operation/s}", cycles = CYCLES_64, size = SIZE_1)
  53 public final class UnaryMathIntrinsicNode extends UnaryNode implements ArithmeticLIRLowerable, Lowerable {
  54 
  55     public static final NodeClass<UnaryMathIntrinsicNode> TYPE = NodeClass.create(UnaryMathIntrinsicNode.class);
  56     protected final UnaryOperation operation;
  57 
  58     public enum UnaryOperation {
  59         LOG(new ForeignCallDescriptor("arithmeticLog", double.class, double.class)),
  60         LOG10(new ForeignCallDescriptor("arithmeticLog10", double.class, double.class)),
  61         SIN(new ForeignCallDescriptor("arithmeticSin", double.class, double.class)),
  62         COS(new ForeignCallDescriptor("arithmeticCos", double.class, double.class)),
  63         TAN(new ForeignCallDescriptor("arithmeticTan", double.class, double.class)),
  64         EXP(new ForeignCallDescriptor("arithmeticExp", double.class, double.class));
  65 
  66         public final ForeignCallDescriptor foreignCallDescriptor;
  67 
  68         UnaryOperation(ForeignCallDescriptor foreignCallDescriptor) {
  69             this.foreignCallDescriptor = foreignCallDescriptor;
  70         }
  71 
  72         public double compute(double value) {
  73             switch (this) {
  74                 case LOG:
  75                     return Math.log(value);
  76                 case LOG10:
  77                     return Math.log10(value);
  78                 case EXP:
  79                     return Math.exp(value);
  80                 case SIN:
  81                     return Math.sin(value);
  82                 case COS:
  83                     return Math.cos(value);
  84                 case TAN:
  85                     return Math.tan(value);
  86                 default:
  87                     throw new GraalError("unknown op %s", this);
  88             }
  89         }
  90     }
  91 
  92     public UnaryOperation getOperation() {
  93         return operation;
  94     }
  95 
  96     public static ValueNode create(ValueNode value, UnaryOperation op) {
  97         ValueNode c = tryConstantFold(value, op);
  98         if (c != null) {
  99             return c;
 100         }
 101         return new UnaryMathIntrinsicNode(value, op);
 102     }
 103 
 104     protected static ValueNode tryConstantFold(ValueNode value, UnaryOperation op) {
 105         if (value.isConstant()) {
 106             return ConstantNode.forDouble(op.compute(value.asJavaConstant().asDouble()));
 107         }
 108         return null;
 109     }
 110 
 111     protected UnaryMathIntrinsicNode(ValueNode value, UnaryOperation op) {
 112         super(TYPE, computeStamp(value.stamp(NodeView.DEFAULT), op), value);
 113         assert value.stamp(NodeView.DEFAULT) instanceof FloatStamp && PrimitiveStamp.getBits(value.stamp(NodeView.DEFAULT)) == 64;
 114         this.operation = op;
 115     }
 116 
 117     @Override
 118     public Stamp foldStamp(Stamp valueStamp) {
 119         return computeStamp(valueStamp, getOperation());
 120     }
 121 
 122     static Stamp computeStamp(Stamp valueStamp, UnaryOperation op) {
 123         if (valueStamp instanceof FloatStamp) {
 124             FloatStamp floatStamp = (FloatStamp) valueStamp;
 125             switch (op) {
 126                 case COS:
 127                 case SIN: {
 128                     boolean nonNaN = floatStamp.lowerBound() != Double.NEGATIVE_INFINITY && floatStamp.upperBound() != Double.POSITIVE_INFINITY && floatStamp.isNonNaN();
 129                     return StampFactory.forFloat(JavaKind.Double, -1.0, 1.0, nonNaN);
 130                 }
 131                 case TAN: {
 132                     boolean nonNaN = floatStamp.lowerBound() != Double.NEGATIVE_INFINITY && floatStamp.upperBound() != Double.POSITIVE_INFINITY && floatStamp.isNonNaN();
 133                     return StampFactory.forFloat(JavaKind.Double, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, nonNaN);
 134                 }
 135                 case LOG:
 136                 case LOG10: {
 137                     double lowerBound = op.compute(floatStamp.lowerBound());
 138                     double upperBound = op.compute(floatStamp.upperBound());
 139                     if (floatStamp.contains(0.0)) {
 140                         // 0.0 and -0.0 infinity produces -Inf
 141                         lowerBound = Double.NEGATIVE_INFINITY;
 142                     }
 143                     boolean nonNaN = floatStamp.lowerBound() >= 0.0 && floatStamp.isNonNaN();
 144                     return StampFactory.forFloat(JavaKind.Double, lowerBound, upperBound, nonNaN);
 145                 }
 146                 case EXP: {
 147                     double lowerBound = Math.exp(floatStamp.lowerBound());
 148                     double upperBound = Math.exp(floatStamp.upperBound());
 149                     boolean nonNaN = floatStamp.isNonNaN();
 150                     return StampFactory.forFloat(JavaKind.Double, lowerBound, upperBound, nonNaN);
 151                 }
 152 
 153             }
 154         }
 155         return StampFactory.forKind(JavaKind.Double);
 156     }
 157 
 158     @Override
 159     public void lower(LoweringTool tool) {
 160         tool.getLowerer().lower(this, tool);
 161     }
 162 
 163     @Override
 164     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
 165         Value input = nodeValueMap.operand(getValue());
 166         Value result;
 167         switch (getOperation()) {
 168             case LOG:
 169                 result = gen.emitMathLog(input, false);
 170                 break;
 171             case LOG10:
 172                 result = gen.emitMathLog(input, true);
 173                 break;
 174             case EXP:
 175                 result = gen.emitMathExp(input);
 176                 break;
 177             case SIN:
 178                 result = gen.emitMathSin(input);
 179                 break;
 180             case COS:
 181                 result = gen.emitMathCos(input);
 182                 break;
 183             case TAN:
 184                 result = gen.emitMathTan(input);
 185                 break;
 186             default:
 187                 throw GraalError.shouldNotReachHere();
 188         }
 189         nodeValueMap.setResult(this, result);
 190     }
 191 
 192     @Override
 193     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
 194         ValueNode c = tryConstantFold(forValue, getOperation());
 195         if (c != null) {
 196             return c;
 197         }
 198         return this;
 199     }
 200 
 201     @NodeIntrinsic
 202     public static native double compute(double value, @ConstantNodeParameter UnaryOperation op);
 203 
 204 }