1/*
2 * Copyright (c) 2013, 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.aarch64;
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.Stamp;
30import org.graalvm.compiler.graph.NodeClass;
31import org.graalvm.compiler.graph.spi.CanonicalizerTool;
32import org.graalvm.compiler.lir.aarch64.AArch64ArithmeticLIRGeneratorTool;
33import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
34import org.graalvm.compiler.nodeinfo.NodeInfo;
35import org.graalvm.compiler.nodes.ConstantNode;
36import org.graalvm.compiler.nodes.ValueNode;
37import org.graalvm.compiler.nodes.calc.UnaryNode;
38import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
39import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
40import org.graalvm.compiler.nodes.type.StampTool;
41
42import jdk.vm.ci.meta.JavaConstant;
43import jdk.vm.ci.meta.JavaKind;
44
45@NodeInfo(cycles = CYCLES_2, size = SIZE_1)
46public final class AArch64CountLeadingZerosNode extends UnaryNode implements ArithmeticLIRLowerable {
47
48    public static final NodeClass<AArch64CountLeadingZerosNode> TYPE = NodeClass.create(AArch64CountLeadingZerosNode.class);
49
50    public AArch64CountLeadingZerosNode(ValueNode value) {
51        super(TYPE, computeStamp(value.stamp(), value), value);
52    }
53
54    @Override
55    public Stamp foldStamp(Stamp newStamp) {
56        return computeStamp(newStamp, getValue());
57    }
58
59    private static Stamp computeStamp(Stamp newStamp, ValueNode theValue) {
60        assert newStamp.isCompatible(theValue.stamp());
61        assert theValue.getStackKind() == JavaKind.Int || theValue.getStackKind() == JavaKind.Long;
62        return StampTool.stampForLeadingZeros((IntegerStamp) newStamp);
63    }
64
65    public static ValueNode tryFold(ValueNode value) {
66        if (value.isConstant()) {
67            JavaConstant c = value.asJavaConstant();
68            if (value.getStackKind() == JavaKind.Int) {
69                return ConstantNode.forInt(Integer.numberOfLeadingZeros(c.asInt()));
70            } else {
71                return ConstantNode.forInt(Long.numberOfLeadingZeros(c.asLong()));
72            }
73        }
74        return null;
75    }
76
77    @Override
78    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
79        ValueNode folded = tryFold(forValue);
80        return folded != null ? folded : this;
81    }
82
83    @Override
84    public void generate(NodeLIRBuilderTool builder, ArithmeticLIRGeneratorTool gen) {
85        builder.setResult(this, ((AArch64ArithmeticLIRGeneratorTool) gen).emitCountLeadingZeros(builder.operand(getValue())));
86    }
87}
88