001/*
002 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.amd64;
024
025import jdk.internal.jvmci.common.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.lir.amd64.*;
032import com.oracle.graal.lir.gen.*;
033import com.oracle.graal.nodeinfo.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.calc.*;
036import com.oracle.graal.nodes.spi.*;
037
038@NodeInfo
039public final class AMD64MathIntrinsicNode extends UnaryNode implements ArithmeticLIRLowerable {
040
041    public static final NodeClass<AMD64MathIntrinsicNode> TYPE = NodeClass.create(AMD64MathIntrinsicNode.class);
042    protected final Operation operation;
043
044    public enum Operation {
045        LOG,
046        LOG10,
047        SIN,
048        COS,
049        TAN
050    }
051
052    public Operation operation() {
053        return operation;
054    }
055
056    public static ValueNode create(ValueNode value, Operation op) {
057        ValueNode c = tryConstantFold(value, op);
058        if (c != null) {
059            return c;
060        }
061        return new AMD64MathIntrinsicNode(value, op);
062    }
063
064    protected static ValueNode tryConstantFold(ValueNode value, Operation op) {
065        if (value.isConstant()) {
066            double ret = doCompute(value.asJavaConstant().asDouble(), op);
067            return ConstantNode.forDouble(ret);
068        }
069        return null;
070    }
071
072    protected AMD64MathIntrinsicNode(ValueNode value, Operation op) {
073        super(TYPE, StampFactory.forKind(Kind.Double), value);
074        assert value.stamp() instanceof FloatStamp && PrimitiveStamp.getBits(value.stamp()) == 64;
075        this.operation = op;
076    }
077
078    @Override
079    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator lirGen) {
080        AMD64ArithmeticLIRGenerator gen = (AMD64ArithmeticLIRGenerator) lirGen;
081        Value input = nodeValueMap.operand(getValue());
082        Value result;
083        switch (operation()) {
084            case LOG:
085                result = gen.emitMathLog(input, false);
086                break;
087            case LOG10:
088                result = gen.emitMathLog(input, true);
089                break;
090            case SIN:
091                result = gen.emitMathSin(input);
092                break;
093            case COS:
094                result = gen.emitMathCos(input);
095                break;
096            case TAN:
097                result = gen.emitMathTan(input);
098                break;
099            default:
100                throw JVMCIError.shouldNotReachHere();
101        }
102        nodeValueMap.setResult(this, result);
103    }
104
105    @Override
106    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
107        ValueNode c = tryConstantFold(forValue, operation());
108        if (c != null) {
109            return c;
110        }
111        return this;
112    }
113
114    @NodeIntrinsic
115    public static native double compute(double value, @ConstantNodeParameter Operation op);
116
117    private static double doCompute(double value, Operation op) {
118        switch (op) {
119            case LOG:
120                return Math.log(value);
121            case LOG10:
122                return Math.log10(value);
123            case SIN:
124                return Math.sin(value);
125            case COS:
126                return Math.cos(value);
127            case TAN:
128                return Math.tan(value);
129            default:
130                throw new JVMCIError("unknown op %s", op);
131        }
132    }
133}