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 least significant "1" bit. Note that the result is undefined if the
038 * input is zero.
039 */
040@NodeInfo
041public final class BitScanForwardNode extends UnaryNode implements LIRLowerable {
042
043    public static final NodeClass<BitScanForwardNode> TYPE = NodeClass.create(BitScanForwardNode.class);
044
045    public BitScanForwardNode(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 firstAlwaysSetBit = scan(valueStamp.downMask() & mask);
057        int firstMaybeSetBit = scan(valueStamp.upMask() & mask);
058        if (firstAlwaysSetBit == -1) {
059            int lastMaybeSetBit = BitScanReverseNode.scan(valueStamp.upMask() & mask);
060            min = firstMaybeSetBit;
061            max = lastMaybeSetBit;
062        } else {
063            min = firstMaybeSetBit;
064            max = firstAlwaysSetBit;
065        }
066        return updateStamp(StampFactory.forInteger(Kind.Int, min, max));
067    }
068
069    @Override
070    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
071        if (forValue.isConstant()) {
072            JavaConstant c = forValue.asJavaConstant();
073            if (c.asLong() != 0) {
074                return ConstantNode.forInt(forValue.getKind() == Kind.Int ? scan(c.asInt()) : scan(c.asLong()));
075            }
076        }
077        return this;
078    }
079
080    /**
081     * Utility method with defined return value for 0.
082     *
083     * @param v
084     * @return number of trailing zeros or -1 if {@code v} == 0.
085     */
086    public static int scan(long v) {
087        if (v == 0) {
088            return -1;
089        }
090        return Long.numberOfTrailingZeros(v);
091    }
092
093    /**
094     * Utility method with defined return value for 0.
095     *
096     * @param v
097     * @return number of trailing zeros or -1 if {@code v} == 0.
098     */
099    public static int scan(int v) {
100        return scan(0xffffffffL & v);
101    }
102
103    /**
104     * Raw intrinsic for bsf instruction.
105     *
106     * @param v
107     * @return number of trailing zeros or an undefined value if {@code v} == 0.
108     */
109    @NodeIntrinsic
110    public static native int unsafeScan(long v);
111
112    /**
113     * Raw intrinsic for bsf instruction.
114     *
115     * @param v
116     * @return number of trailing zeros or an undefined value if {@code v} == 0.
117     */
118    @NodeIntrinsic
119    public static native int unsafeScan(int v);
120
121    @Override
122    public void generate(NodeLIRBuilderTool gen) {
123        Value result = gen.getLIRGeneratorTool().emitBitScanForward(gen.operand(getValue()));
124        gen.setResult(this, result);
125    }
126}