BitScanReverseNode.java revision 13017:134219a5b0ec
1/*
2 * Copyright (c) 2012, 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.replacements.nodes;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
27
28import org.graalvm.compiler.core.common.type.IntegerStamp;
29import org.graalvm.compiler.core.common.type.PrimitiveStamp;
30import org.graalvm.compiler.core.common.type.Stamp;
31import org.graalvm.compiler.core.common.type.StampFactory;
32import org.graalvm.compiler.graph.NodeClass;
33import org.graalvm.compiler.graph.spi.CanonicalizerTool;
34import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
35import org.graalvm.compiler.nodeinfo.NodeInfo;
36import org.graalvm.compiler.nodes.ConstantNode;
37import org.graalvm.compiler.nodes.ValueNode;
38import org.graalvm.compiler.nodes.calc.UnaryNode;
39import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
40import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
41
42import jdk.vm.ci.code.CodeUtil;
43import jdk.vm.ci.meta.JavaConstant;
44import jdk.vm.ci.meta.JavaKind;
45
46/**
47 * Determines the index of the most significant "1" bit. Note that the result is undefined if the
48 * input is zero.
49 */
50@NodeInfo(cycles = CYCLES_2, size = SIZE_1)
51public final class BitScanReverseNode extends UnaryNode implements ArithmeticLIRLowerable {
52
53    public static final NodeClass<BitScanReverseNode> TYPE = NodeClass.create(BitScanReverseNode.class);
54
55    public BitScanReverseNode(ValueNode value) {
56        super(TYPE, StampFactory.forInteger(JavaKind.Int, 0, ((PrimitiveStamp) value.stamp()).getBits()), value);
57        assert value.getStackKind() == JavaKind.Int || value.getStackKind() == JavaKind.Long;
58    }
59
60    @Override
61    public Stamp foldStamp(Stamp newStamp) {
62        assert newStamp.isCompatible(getValue().stamp());
63        IntegerStamp valueStamp = (IntegerStamp) newStamp;
64        int min;
65        int max;
66        long mask = CodeUtil.mask(valueStamp.getBits());
67        int lastAlwaysSetBit = scan(valueStamp.downMask() & mask);
68        if (lastAlwaysSetBit == -1) {
69            int firstMaybeSetBit = BitScanForwardNode.scan(valueStamp.upMask() & mask);
70            min = firstMaybeSetBit;
71        } else {
72            min = lastAlwaysSetBit;
73        }
74        int lastMaybeSetBit = scan(valueStamp.upMask() & mask);
75        max = lastMaybeSetBit;
76        return StampFactory.forInteger(JavaKind.Int, min, max);
77    }
78
79    @Override
80    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
81        if (forValue.isConstant()) {
82            JavaConstant c = forValue.asJavaConstant();
83            if (c.asLong() != 0) {
84                return ConstantNode.forInt(forValue.getStackKind() == JavaKind.Int ? scan(c.asInt()) : scan(c.asLong()));
85            }
86        }
87        return this;
88    }
89
90    /**
91     * Utility method with defined return value for 0.
92     *
93     * @param v
94     * @return index of first set bit or -1 if {@code v} == 0.
95     */
96    public static int scan(long v) {
97        return 63 - Long.numberOfLeadingZeros(v);
98    }
99
100    /**
101     * Utility method with defined return value for 0.
102     *
103     * @param v
104     * @return index of first set bit or -1 if {@code v} == 0.
105     */
106    public static int scan(int v) {
107        return 31 - Integer.numberOfLeadingZeros(v);
108    }
109
110    /**
111     * Raw intrinsic for bsr instruction.
112     *
113     * @param v
114     * @return index of first set bit or an undefined value if {@code v} == 0.
115     */
116    @NodeIntrinsic
117    public static native int unsafeScan(int v);
118
119    /**
120     * Raw intrinsic for bsr instruction.
121     *
122     * @param v
123     * @return index of first set bit or an undefined value if {@code v} == 0.
124     */
125    @NodeIntrinsic
126    public static native int unsafeScan(long v);
127
128    @Override
129    public void generate(NodeLIRBuilderTool builder, ArithmeticLIRGeneratorTool gen) {
130        builder.setResult(this, gen.emitBitScanReverse(builder.operand(getValue())));
131    }
132
133}
134