001/*
002 * Copyright (c) 2014, 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.nodes.calc;
024
025import java.io.*;
026import java.util.function.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.compiler.common.type.ArithmeticOpTable.UnaryOp;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.spi.*;
035
036@NodeInfo
037public abstract class UnaryArithmeticNode<OP> extends UnaryNode implements ArithmeticLIRLowerable {
038
039    @SuppressWarnings("rawtypes") public static final NodeClass<UnaryArithmeticNode> TYPE = NodeClass.create(UnaryArithmeticNode.class);
040
041    protected interface SerializableUnaryFunction<T> extends Function<ArithmeticOpTable, UnaryOp<T>>, Serializable {
042    }
043
044    protected final SerializableUnaryFunction<OP> getOp;
045
046    protected UnaryArithmeticNode(NodeClass<? extends UnaryArithmeticNode<OP>> c, SerializableUnaryFunction<OP> getOp, ValueNode value) {
047        super(c, getOp.apply(ArithmeticOpTable.forStamp(value.stamp())).foldStamp(value.stamp()), value);
048        this.getOp = getOp;
049    }
050
051    protected final UnaryOp<OP> getOp(ValueNode forValue) {
052        return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp()));
053    }
054
055    @Override
056    public boolean inferStamp() {
057        return updateStamp(getOp(getValue()).foldStamp(getValue().stamp()));
058    }
059
060    @Override
061    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
062        ValueNode synonym = findSynonym(forValue, getOp(forValue));
063        if (synonym != null) {
064            return synonym;
065        }
066        return this;
067    }
068
069    protected static <OP> ValueNode findSynonym(ValueNode forValue, UnaryOp<OP> op) {
070        if (forValue.isConstant()) {
071            return ConstantNode.forPrimitive(op.foldStamp(forValue.stamp()), op.foldConstant(forValue.asConstant()));
072        }
073        return null;
074    }
075}