1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.nodes.calc;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
26
27import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
28import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
29import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.Narrow;
30import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.SignExtend;
31import org.graalvm.compiler.core.common.type.IntegerStamp;
32import org.graalvm.compiler.core.common.type.PrimitiveStamp;
33import org.graalvm.compiler.graph.NodeClass;
34import org.graalvm.compiler.graph.spi.CanonicalizerTool;
35import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
36import org.graalvm.compiler.nodeinfo.NodeInfo;
37import org.graalvm.compiler.nodes.ValueNode;
38import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
39
40/**
41 * The {@code SignExtendNode} converts an integer to a wider integer using sign extension.
42 */
43@NodeInfo(cycles = CYCLES_1)
44public final class SignExtendNode extends IntegerConvertNode<SignExtend, Narrow> {
45
46    public static final NodeClass<SignExtendNode> TYPE = NodeClass.create(SignExtendNode.class);
47
48    public SignExtendNode(ValueNode input, int resultBits) {
49        this(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
50        assert 0 < PrimitiveStamp.getBits(input.stamp()) && PrimitiveStamp.getBits(input.stamp()) <= resultBits;
51    }
52
53    public SignExtendNode(ValueNode input, int inputBits, int resultBits) {
54        super(TYPE, ArithmeticOpTable::getSignExtend, ArithmeticOpTable::getNarrow, inputBits, resultBits, input);
55    }
56
57    public static ValueNode create(ValueNode input, int resultBits) {
58        return create(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
59    }
60
61    public static ValueNode create(ValueNode input, int inputBits, int resultBits) {
62        IntegerConvertOp<SignExtend> signExtend = ArithmeticOpTable.forStamp(input.stamp()).getSignExtend();
63        ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp()));
64        if (synonym != null) {
65            return synonym;
66        }
67        return canonical(null, input, inputBits, resultBits);
68    }
69
70    @Override
71    public boolean isLossless() {
72        return true;
73    }
74
75    @Override
76    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
77        ValueNode ret = super.canonical(tool, forValue);
78        if (ret != this) {
79            return ret;
80        }
81
82        return canonical(this, forValue, getInputBits(), getResultBits());
83    }
84
85    private static ValueNode canonical(SignExtendNode self, ValueNode forValue, int inputBits, int resultBits) {
86        if (forValue instanceof SignExtendNode) {
87            // sxxx -(sign-extend)-> ssss sxxx -(sign-extend)-> ssssssss sssssxxx
88            // ==> sxxx -(sign-extend)-> ssssssss sssssxxx
89            SignExtendNode other = (SignExtendNode) forValue;
90            return SignExtendNode.create(other.getValue(), other.getInputBits(), resultBits);
91        } else if (forValue instanceof ZeroExtendNode) {
92            ZeroExtendNode other = (ZeroExtendNode) forValue;
93            if (other.getResultBits() > other.getInputBits()) {
94                // sxxx -(zero-extend)-> 0000 sxxx -(sign-extend)-> 00000000 0000sxxx
95                // ==> sxxx -(zero-extend)-> 00000000 0000sxxx
96                return ZeroExtendNode.create(other.getValue(), other.getInputBits(), resultBits);
97            }
98        }
99
100        if (forValue.stamp() instanceof IntegerStamp) {
101            IntegerStamp inputStamp = (IntegerStamp) forValue.stamp();
102            if ((inputStamp.upMask() & (1L << (inputBits - 1))) == 0L) {
103                // 0xxx -(sign-extend)-> 0000 0xxx
104                // ==> 0xxx -(zero-extend)-> 0000 0xxx
105                return ZeroExtendNode.create(forValue, inputBits, resultBits);
106            }
107        }
108
109        return self != null ? self : new SignExtendNode(forValue, inputBits, resultBits);
110    }
111
112    @Override
113    public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
114        nodeValueMap.setResult(this, gen.emitSignExtend(nodeValueMap.operand(getValue()), getInputBits(), getResultBits()));
115    }
116
117    @Override
118    public boolean mayNullCheckSkipConversion() {
119        return true;
120    }
121}
122