001/*
002 * Copyright (c) 2012, 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.replacements.nodes;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.graph.spi.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.calc.*;
034import com.oracle.graal.nodes.spi.*;
035
036/**
037 * Determines the index of the most significant "1" bit. Note that the result is undefined if the
038 * input is zero.
039 */
040@NodeInfo
041public final class BitScanReverseNode extends UnaryNode implements LIRLowerable {
042
043    public static final NodeClass<BitScanReverseNode> TYPE = NodeClass.create(BitScanReverseNode.class);
044
045    public BitScanReverseNode(ValueNode value) {
046        super(TYPE, StampFactory.forInteger(Kind.Int, 0, ((PrimitiveStamp) value.stamp()).getBits()), value);
047        assert value.getKind() == Kind.Int || value.getKind() == Kind.Long;
048    }
049
050    @Override
051    public boolean inferStamp() {
052        IntegerStamp valueStamp = (IntegerStamp) getValue().stamp();
053        int min;
054        int max;
055        long mask = CodeUtil.mask(valueStamp.getBits());
056        int lastAlwaysSetBit = scan(valueStamp.downMask() & mask);
057        if (lastAlwaysSetBit == -1) {
058            int firstMaybeSetBit = BitScanForwardNode.scan(valueStamp.upMask() & mask);
059            min = firstMaybeSetBit;
060        } else {
061            min = lastAlwaysSetBit;
062        }
063        int lastMaybeSetBit = scan(valueStamp.upMask() & mask);
064        max = lastMaybeSetBit;
065        return updateStamp(StampFactory.forInteger(Kind.Int, min, max));
066    }
067
068    @Override
069    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
070        if (forValue.isConstant()) {
071            JavaConstant c = forValue.asJavaConstant();
072            if (c.asLong() != 0) {
073                return ConstantNode.forInt(forValue.getKind() == Kind.Int ? scan(c.asInt()) : scan(c.asLong()));
074            }
075        }
076        return this;
077    }
078
079    /**
080     * Utility method with defined return value for 0.
081     *
082     * @param v
083     * @return index of first set bit or -1 if {@code v} == 0.
084     */
085    public static int scan(long v) {
086        return 63 - Long.numberOfLeadingZeros(v);
087    }
088
089    /**
090     * Utility method with defined return value for 0.
091     *
092     * @param v
093     * @return index of first set bit or -1 if {@code v} == 0.
094     */
095    public static int scan(int v) {
096        return 31 - Integer.numberOfLeadingZeros(v);
097    }
098
099    /**
100     * Raw intrinsic for bsr instruction.
101     *
102     * @param v
103     * @return index of first set bit or an undefined value if {@code v} == 0.
104     */
105    @NodeIntrinsic
106    public static native int unsafeScan(int v);
107
108    /**
109     * Raw intrinsic for bsr instruction.
110     *
111     * @param v
112     * @return index of first set bit or an undefined value if {@code v} == 0.
113     */
114    @NodeIntrinsic
115    public static native int unsafeScan(long v);
116
117    @Override
118    public void generate(NodeLIRBuilderTool gen) {
119        Value result = gen.getLIRGeneratorTool().emitBitScanReverse(gen.operand(getValue()));
120        gen.setResult(this, result);
121    }
122
123}