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 com.oracle.graal.compiler.common.type.*;
026import com.oracle.graal.compiler.common.type.ArithmeticOpTable.IntegerConvertOp;
027import com.oracle.graal.compiler.common.type.ArithmeticOpTable.IntegerConvertOp.Narrow;
028import com.oracle.graal.compiler.common.type.ArithmeticOpTable.IntegerConvertOp.SignExtend;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.lir.gen.*;
032import com.oracle.graal.nodeinfo.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.spi.*;
035
036/**
037 * The {@code SignExtendNode} converts an integer to a wider integer using sign extension.
038 */
039@NodeInfo
040public final class SignExtendNode extends IntegerConvertNode<SignExtend, Narrow> {
041
042    public static final NodeClass<SignExtendNode> TYPE = NodeClass.create(SignExtendNode.class);
043
044    public SignExtendNode(ValueNode input, int resultBits) {
045        this(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
046        assert 0 < PrimitiveStamp.getBits(input.stamp()) && PrimitiveStamp.getBits(input.stamp()) <= resultBits;
047    }
048
049    public SignExtendNode(ValueNode input, int inputBits, int resultBits) {
050        super(TYPE, ArithmeticOpTable::getSignExtend, ArithmeticOpTable::getNarrow, inputBits, resultBits, input);
051    }
052
053    public static ValueNode create(ValueNode input, int resultBits) {
054        return create(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
055    }
056
057    public static ValueNode create(ValueNode input, int inputBits, int resultBits) {
058        IntegerConvertOp<SignExtend> signExtend = ArithmeticOpTable.forStamp(input.stamp()).getSignExtend();
059        ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp()));
060        if (synonym != null) {
061            return synonym;
062        } else {
063            return new SignExtendNode(input, inputBits, resultBits);
064        }
065    }
066
067    @Override
068    public boolean isLossless() {
069        return true;
070    }
071
072    @Override
073    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
074        ValueNode ret = super.canonical(tool, forValue);
075        if (ret != this) {
076            return ret;
077        }
078
079        if (forValue instanceof SignExtendNode) {
080            // sxxx -(sign-extend)-> ssss sxxx -(sign-extend)-> ssssssss sssssxxx
081            // ==> sxxx -(sign-extend)-> ssssssss sssssxxx
082            SignExtendNode other = (SignExtendNode) forValue;
083            return new SignExtendNode(other.getValue(), other.getInputBits(), getResultBits());
084        } else if (forValue instanceof ZeroExtendNode) {
085            ZeroExtendNode other = (ZeroExtendNode) forValue;
086            if (other.getResultBits() > other.getInputBits()) {
087                // sxxx -(zero-extend)-> 0000 sxxx -(sign-extend)-> 00000000 0000sxxx
088                // ==> sxxx -(zero-extend)-> 00000000 0000sxxx
089                return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits());
090            }
091        }
092
093        if (forValue.stamp() instanceof IntegerStamp) {
094            IntegerStamp inputStamp = (IntegerStamp) forValue.stamp();
095            if ((inputStamp.upMask() & (1L << (getInputBits() - 1))) == 0L) {
096                // 0xxx -(sign-extend)-> 0000 0xxx
097                // ==> 0xxx -(zero-extend)-> 0000 0xxx
098                return new ZeroExtendNode(forValue, getInputBits(), getResultBits());
099            }
100        }
101
102        return this;
103    }
104
105    @Override
106    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
107        nodeValueMap.setResult(this, gen.emitSignExtend(nodeValueMap.operand(getValue()), getInputBits(), getResultBits()));
108    }
109}