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 jdk.internal.jvmci.code.*;
026
027import com.oracle.graal.compiler.common.calc.*;
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.compiler.common.type.ArithmeticOpTable.IntegerConvertOp;
030import com.oracle.graal.compiler.common.type.ArithmeticOpTable.IntegerConvertOp.Narrow;
031import com.oracle.graal.compiler.common.type.ArithmeticOpTable.IntegerConvertOp.ZeroExtend;
032import com.oracle.graal.graph.*;
033import com.oracle.graal.graph.spi.*;
034import com.oracle.graal.lir.gen.*;
035import com.oracle.graal.nodeinfo.*;
036import com.oracle.graal.nodes.*;
037import com.oracle.graal.nodes.spi.*;
038
039/**
040 * The {@code ZeroExtendNode} converts an integer to a wider integer using zero extension.
041 */
042@NodeInfo
043public final class ZeroExtendNode extends IntegerConvertNode<ZeroExtend, Narrow> {
044
045    public static final NodeClass<ZeroExtendNode> TYPE = NodeClass.create(ZeroExtendNode.class);
046
047    public ZeroExtendNode(ValueNode input, int resultBits) {
048        this(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
049        assert 0 < PrimitiveStamp.getBits(input.stamp()) && PrimitiveStamp.getBits(input.stamp()) <= resultBits;
050    }
051
052    public ZeroExtendNode(ValueNode input, int inputBits, int resultBits) {
053        super(TYPE, ArithmeticOpTable::getZeroExtend, ArithmeticOpTable::getNarrow, inputBits, resultBits, input);
054    }
055
056    public static ValueNode create(ValueNode input, int resultBits) {
057        return create(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
058    }
059
060    public static ValueNode create(ValueNode input, int inputBits, int resultBits) {
061        IntegerConvertOp<ZeroExtend> signExtend = ArithmeticOpTable.forStamp(input.stamp()).getZeroExtend();
062        ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp()));
063        if (synonym != null) {
064            return synonym;
065        } else {
066            return new ZeroExtendNode(input, inputBits, resultBits);
067        }
068    }
069
070    @Override
071    public boolean isLossless() {
072        return true;
073    }
074
075    @Override
076    public boolean preservesOrder(Condition cond) {
077        switch (cond) {
078            case GE:
079            case GT:
080            case LE:
081            case LT:
082                return false;
083            default:
084                return true;
085        }
086    }
087
088    @Override
089    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
090        ValueNode ret = super.canonical(tool, forValue);
091        if (ret != this) {
092            return ret;
093        }
094
095        if (forValue instanceof ZeroExtendNode) {
096            // xxxx -(zero-extend)-> 0000 xxxx -(zero-extend)-> 00000000 0000xxxx
097            // ==> xxxx -(zero-extend)-> 00000000 0000xxxx
098            ZeroExtendNode other = (ZeroExtendNode) forValue;
099            return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits());
100        }
101        if (forValue instanceof NarrowNode) {
102            NarrowNode narrow = (NarrowNode) forValue;
103            Stamp inputStamp = narrow.getValue().stamp();
104            if (inputStamp instanceof IntegerStamp && inputStamp.isCompatible(stamp())) {
105                IntegerStamp istamp = (IntegerStamp) inputStamp;
106                long mask = CodeUtil.mask(PrimitiveStamp.getBits(narrow.stamp()));
107                if (((istamp.upMask() | istamp.downMask()) & ~mask) == 0) {
108                    // The original value is in the range of the masked zero extended result so
109                    // simply return the original input.
110                    return narrow.getValue();
111                }
112            }
113        }
114
115        return this;
116    }
117
118    @Override
119    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
120        nodeValueMap.setResult(this, gen.emitZeroExtend(nodeValueMap.operand(getValue()), getInputBits(), getResultBits()));
121    }
122}