43972
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.replacements.amd64;
|
|
26 |
|
46393
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
|
43972
|
28 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
|
|
29 |
|
|
30 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
32 |
import org.graalvm.compiler.graph.NodeClass;
|
|
33 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
34 |
import org.graalvm.compiler.lir.amd64.AMD64ArithmeticLIRGeneratorTool;
|
|
35 |
import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
|
|
36 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
37 |
import org.graalvm.compiler.nodes.ConstantNode;
|
48190
|
38 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
39 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
40 |
import org.graalvm.compiler.nodes.calc.UnaryNode;
|
|
41 |
import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
|
|
42 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
43 |
import org.graalvm.compiler.nodes.type.StampTool;
|
|
44 |
|
|
45 |
import jdk.vm.ci.meta.JavaConstant;
|
|
46 |
import jdk.vm.ci.meta.JavaKind;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Count the number of trailing zeros using the {@code tzcntq} or {@code tzcntl} instructions.
|
|
50 |
*/
|
46393
|
51 |
@NodeInfo(cycles = CYCLES_2, size = SIZE_1)
|
43972
|
52 |
public final class AMD64CountTrailingZerosNode extends UnaryNode implements ArithmeticLIRLowerable {
|
|
53 |
public static final NodeClass<AMD64CountTrailingZerosNode> TYPE = NodeClass.create(AMD64CountTrailingZerosNode.class);
|
|
54 |
|
|
55 |
public AMD64CountTrailingZerosNode(ValueNode value) {
|
48190
|
56 |
super(TYPE, computeStamp(value.stamp(NodeView.DEFAULT), value), value);
|
43972
|
57 |
assert value.getStackKind() == JavaKind.Int || value.getStackKind() == JavaKind.Long;
|
|
58 |
}
|
|
59 |
|
|
60 |
@Override
|
|
61 |
public Stamp foldStamp(Stamp newStamp) {
|
|
62 |
return computeStamp(newStamp, getValue());
|
|
63 |
}
|
|
64 |
|
|
65 |
static Stamp computeStamp(Stamp newStamp, ValueNode value) {
|
48190
|
66 |
assert newStamp.isCompatible(value.stamp(NodeView.DEFAULT));
|
43972
|
67 |
IntegerStamp valueStamp = (IntegerStamp) newStamp;
|
|
68 |
return StampTool.stampForTrailingZeros(valueStamp);
|
|
69 |
}
|
|
70 |
|
|
71 |
public static ValueNode tryFold(ValueNode value) {
|
|
72 |
if (value.isConstant()) {
|
|
73 |
JavaConstant c = value.asJavaConstant();
|
|
74 |
if (value.getStackKind() == JavaKind.Int) {
|
|
75 |
return ConstantNode.forInt(Integer.numberOfTrailingZeros(c.asInt()));
|
|
76 |
} else {
|
|
77 |
return ConstantNode.forInt(Long.numberOfTrailingZeros(c.asLong()));
|
|
78 |
}
|
|
79 |
}
|
|
80 |
return null;
|
|
81 |
}
|
|
82 |
|
|
83 |
@Override
|
|
84 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
|
|
85 |
ValueNode folded = tryFold(forValue);
|
|
86 |
return folded != null ? folded : this;
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
public void generate(NodeLIRBuilderTool builder, ArithmeticLIRGeneratorTool gen) {
|
|
91 |
builder.setResult(this, ((AMD64ArithmeticLIRGeneratorTool) gen).emitCountTrailingZeros(builder.operand(getValue())));
|
|
92 |
}
|
58877
|
93 |
|
|
94 |
@NodeIntrinsic
|
|
95 |
public static native int countTrailingZeros(int i);
|
|
96 |
|
|
97 |
@NodeIntrinsic
|
|
98 |
public static native int countTrailingZeros(long i);
|
43972
|
99 |
}
|