LeftShiftNode.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2011, 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 org.graalvm.compiler.core.common.type.ArithmeticOpTable;
26import org.graalvm.compiler.core.common.type.ArithmeticOpTable.ShiftOp.Shl;
27import org.graalvm.compiler.graph.NodeClass;
28import org.graalvm.compiler.graph.spi.CanonicalizerTool;
29import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
30import org.graalvm.compiler.nodeinfo.NodeInfo;
31import org.graalvm.compiler.nodes.ConstantNode;
32import org.graalvm.compiler.nodes.ValueNode;
33import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
34
35import jdk.vm.ci.meta.JavaKind;
36
37@NodeInfo(shortName = "<<")
38public final class LeftShiftNode extends ShiftNode<Shl> {
39
40    public static final NodeClass<LeftShiftNode> TYPE = NodeClass.create(LeftShiftNode.class);
41
42    public LeftShiftNode(ValueNode x, ValueNode y) {
43        super(TYPE, ArithmeticOpTable::getShl, x, y);
44    }
45
46    @Override
47    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
48        ValueNode ret = super.canonical(tool, forX, forY);
49        if (ret != this) {
50            return ret;
51        }
52
53        if (forY.isConstant()) {
54            int amount = forY.asJavaConstant().asInt();
55            int originalAmout = amount;
56            int mask = getShiftAmountMask();
57            amount &= mask;
58            if (amount == 0) {
59                return forX;
60            }
61            if (forX instanceof ShiftNode) {
62                ShiftNode<?> other = (ShiftNode<?>) forX;
63                if (other.getY().isConstant()) {
64                    int otherAmount = other.getY().asJavaConstant().asInt() & mask;
65                    if (other instanceof LeftShiftNode) {
66                        int total = amount + otherAmount;
67                        if (total != (total & mask)) {
68                            return ConstantNode.forIntegerKind(getStackKind(), 0);
69                        }
70                        return new LeftShiftNode(other.getX(), ConstantNode.forInt(total));
71                    } else if ((other instanceof RightShiftNode || other instanceof UnsignedRightShiftNode) && otherAmount == amount) {
72                        if (getStackKind() == JavaKind.Long) {
73                            return new AndNode(other.getX(), ConstantNode.forLong(-1L << amount));
74                        } else {
75                            assert getStackKind() == JavaKind.Int;
76                            return new AndNode(other.getX(), ConstantNode.forInt(-1 << amount));
77                        }
78                    }
79                }
80            }
81            if (originalAmout != amount) {
82                return new LeftShiftNode(forX, ConstantNode.forInt(amount));
83            }
84        }
85        return this;
86    }
87
88    @Override
89    public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
90        nodeValueMap.setResult(this, gen.emitShl(nodeValueMap.operand(getX()), nodeValueMap.operand(getY())));
91    }
92}
93