001/*
002 * Copyright (c) 2009, 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 java.io.*;
026import java.util.function.*;
027
028import jdk.internal.jvmci.code.*;
029import jdk.internal.jvmci.meta.*;
030
031import com.oracle.graal.compiler.common.type.*;
032import com.oracle.graal.compiler.common.type.ArithmeticOpTable.ShiftOp;
033import com.oracle.graal.graph.*;
034import com.oracle.graal.graph.spi.*;
035import com.oracle.graal.nodeinfo.*;
036import com.oracle.graal.nodes.*;
037import com.oracle.graal.nodes.spi.*;
038
039/**
040 * The {@code ShiftOp} class represents shift operations.
041 */
042@NodeInfo
043public abstract class ShiftNode<OP> extends BinaryNode implements ArithmeticLIRLowerable, NarrowableArithmeticNode {
044
045    @SuppressWarnings("rawtypes") public static final NodeClass<ShiftNode> TYPE = NodeClass.create(ShiftNode.class);
046
047    protected interface SerializableShiftFunction<T> extends Function<ArithmeticOpTable, ShiftOp<T>>, Serializable {
048    }
049
050    protected final SerializableShiftFunction<OP> getOp;
051
052    /**
053     * Creates a new shift operation.
054     *
055     * @param x the first input value
056     * @param s the second input value
057     */
058    protected ShiftNode(NodeClass<? extends ShiftNode<OP>> c, SerializableShiftFunction<OP> getOp, ValueNode x, ValueNode s) {
059        super(c, getOp.apply(ArithmeticOpTable.forStamp(x.stamp())).foldStamp(x.stamp(), (IntegerStamp) s.stamp()), x, s);
060        assert ((IntegerStamp) s.stamp()).getBits() == 32;
061        this.getOp = getOp;
062    }
063
064    protected final ShiftOp<OP> getOp(ValueNode forValue) {
065        return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp()));
066    }
067
068    @Override
069    public boolean inferStamp() {
070        return updateStamp(getOp(getX()).foldStamp(getX().stamp(), (IntegerStamp) getY().stamp()));
071    }
072
073    @Override
074    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
075        if (forX.isConstant() && forY.isConstant()) {
076            JavaConstant amount = forY.asJavaConstant();
077            assert amount.getKind() == Kind.Int;
078            return ConstantNode.forPrimitive(stamp(), getOp(forX).foldConstant(forX.asConstant(), amount.asInt()));
079        }
080        return this;
081    }
082
083    public int getShiftAmountMask() {
084        return getOp(getX()).getShiftAmountMask(stamp());
085    }
086
087    public boolean isNarrowable(int resultBits) {
088        assert CodeUtil.isPowerOf2(resultBits);
089        int narrowMask = resultBits - 1;
090        int wideMask = getShiftAmountMask();
091        assert (wideMask & narrowMask) == narrowMask : String.format("wideMask %x should be wider than narrowMask %x", wideMask, narrowMask);
092
093        /*
094         * Shifts are special because narrowing them also changes the implicit mask of the shift
095         * amount. We can narrow only if (y & wideMask) == (y & narrowMask) for all possible values
096         * of y.
097         */
098        IntegerStamp yStamp = (IntegerStamp) getY().stamp();
099        return (yStamp.upMask() & (wideMask & ~narrowMask)) == 0;
100    }
101}