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 jdk.internal.jvmci.meta.*;
029
030import com.oracle.graal.compiler.common.type.*;
031import com.oracle.graal.compiler.common.type.ArithmeticOpTable.IntegerConvertOp;
032import com.oracle.graal.graph.*;
033import com.oracle.graal.graph.spi.*;
034import com.oracle.graal.nodeinfo.*;
035import com.oracle.graal.nodes.*;
036import com.oracle.graal.nodes.spi.*;
037
038/**
039 * An {@code IntegerConvert} converts an integer to an integer of different width.
040 */
041@NodeInfo
042public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ConvertNode, ArithmeticLIRLowerable {
043    @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class);
044
045    protected final SerializableIntegerConvertFunction<OP> getOp;
046    protected final SerializableIntegerConvertFunction<REV> getReverseOp;
047
048    protected final int inputBits;
049    protected final int resultBits;
050
051    protected interface SerializableIntegerConvertFunction<T> extends Function<ArithmeticOpTable, IntegerConvertOp<T>>, Serializable {
052    }
053
054    protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, SerializableIntegerConvertFunction<OP> getOp, SerializableIntegerConvertFunction<REV> getReverseOp, int inputBits,
055                    int resultBits, ValueNode input) {
056        super(c, getOp.apply(ArithmeticOpTable.forStamp(input.stamp())).foldStamp(inputBits, resultBits, input.stamp()), input);
057        this.getOp = getOp;
058        this.getReverseOp = getReverseOp;
059        this.inputBits = inputBits;
060        this.resultBits = resultBits;
061    }
062
063    public int getInputBits() {
064        return inputBits;
065    }
066
067    public int getResultBits() {
068        return resultBits;
069    }
070
071    protected final IntegerConvertOp<OP> getOp(ValueNode forValue) {
072        return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp()));
073    }
074
075    @Override
076    public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
077        return getOp(getValue()).foldConstant(getInputBits(), getResultBits(), c);
078    }
079
080    @Override
081    public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
082        IntegerConvertOp<REV> reverse = getReverseOp.apply(ArithmeticOpTable.forStamp(stamp()));
083        return reverse.foldConstant(getResultBits(), getInputBits(), c);
084    }
085
086    @Override
087    public boolean inferStamp() {
088        return updateStamp(getOp(getValue()).foldStamp(inputBits, resultBits, getValue().stamp()));
089    }
090
091    @Override
092    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
093        ValueNode synonym = findSynonym(getOp(forValue), forValue, inputBits, resultBits, stamp());
094        if (synonym != null) {
095            return synonym;
096        }
097        return this;
098    }
099
100    protected static <T> ValueNode findSynonym(IntegerConvertOp<T> operation, ValueNode value, int inputBits, int resultBits, Stamp stamp) {
101        if (inputBits == resultBits) {
102            return value;
103        } else if (value.isConstant()) {
104            return ConstantNode.forPrimitive(stamp, operation.foldConstant(inputBits, resultBits, value.asConstant()));
105        }
106        return null;
107    }
108
109    public static ValueNode convert(ValueNode input, Stamp stamp) {
110        return convert(input, stamp, false);
111    }
112
113    public static ValueNode convert(ValueNode input, Stamp stamp, StructuredGraph graph) {
114        ValueNode convert = convert(input, stamp, false);
115        if (!convert.isAlive()) {
116            assert !convert.isDeleted();
117            convert = graph.addOrUnique(convert);
118        }
119        return convert;
120    }
121
122    public static ValueNode convertUnsigned(ValueNode input, Stamp stamp) {
123        return convert(input, stamp, true);
124    }
125
126    public static ValueNode convert(ValueNode input, Stamp stamp, boolean zeroExtend) {
127        IntegerStamp fromStamp = (IntegerStamp) input.stamp();
128        IntegerStamp toStamp = (IntegerStamp) stamp;
129
130        ValueNode result;
131        if (toStamp.getBits() == fromStamp.getBits()) {
132            result = input;
133        } else if (toStamp.getBits() < fromStamp.getBits()) {
134            result = new NarrowNode(input, fromStamp.getBits(), toStamp.getBits());
135        } else if (zeroExtend) {
136            // toStamp.getBits() > fromStamp.getBits()
137            result = new ZeroExtendNode(input, toStamp.getBits());
138        } else {
139            // toStamp.getBits() > fromStamp.getBits()
140            result = new SignExtendNode(input, toStamp.getBits());
141        }
142
143        IntegerStamp resultStamp = (IntegerStamp) result.stamp();
144        assert toStamp.getBits() == resultStamp.getBits();
145        return result;
146    }
147}