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