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 NarrowNode} converts an integer to a narrower integer.
038 */
039@NodeInfo
040public final class NarrowNode extends IntegerConvertNode<Narrow, SignExtend> {
041
042    public static final NodeClass<NarrowNode> TYPE = NodeClass.create(NarrowNode.class);
043
044    public NarrowNode(ValueNode input, int resultBits) {
045        this(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
046        assert 0 < resultBits && resultBits <= PrimitiveStamp.getBits(input.stamp());
047    }
048
049    public NarrowNode(ValueNode input, int inputBits, int resultBits) {
050        super(TYPE, ArithmeticOpTable::getNarrow, ArithmeticOpTable::getSignExtend, 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<Narrow> signExtend = ArithmeticOpTable.forStamp(input.stamp()).getNarrow();
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 NarrowNode(input, inputBits, resultBits);
064        }
065    }
066
067    @Override
068    public boolean isLossless() {
069        return false;
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 NarrowNode) {
080            // zzzzzzzz yyyyxxxx -(narrow)-> yyyyxxxx -(narrow)-> xxxx
081            // ==> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
082            NarrowNode other = (NarrowNode) forValue;
083            return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
084        } else if (forValue instanceof IntegerConvertNode) {
085            // SignExtendNode or ZeroExtendNode
086            IntegerConvertNode<?, ?> other = (IntegerConvertNode<?, ?>) forValue;
087            if (getResultBits() == other.getInputBits()) {
088                // xxxx -(extend)-> yyyy xxxx -(narrow)-> xxxx
089                // ==> no-op
090                return other.getValue();
091            } else if (getResultBits() < other.getInputBits()) {
092                // yyyyxxxx -(extend)-> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
093                // ==> yyyyxxxx -(narrow)-> xxxx
094                return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
095            } else {
096                if (other instanceof SignExtendNode) {
097                    // sxxx -(sign-extend)-> ssssssss sssssxxx -(narrow)-> sssssxxx
098                    // ==> sxxx -(sign-extend)-> sssssxxx
099                    return new SignExtendNode(other.getValue(), other.getInputBits(), getResultBits());
100                } else if (other instanceof ZeroExtendNode) {
101                    // xxxx -(zero-extend)-> 00000000 00000xxx -(narrow)-> 0000xxxx
102                    // ==> xxxx -(zero-extend)-> 0000xxxx
103                    return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits());
104                }
105            }
106        }
107        return this;
108    }
109
110    @Override
111    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
112        nodeValueMap.setResult(this, gen.emitNarrow(nodeValueMap.operand(getValue()), getResultBits()));
113    }
114}