NormalizeCompareNode.java revision 13017:134219a5b0ec
1/*
2 * Copyright (c) 2009, 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.nodes.calc;
24
25import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
26
27import org.graalvm.compiler.core.common.calc.Condition;
28import org.graalvm.compiler.core.common.type.FloatStamp;
29import org.graalvm.compiler.core.common.type.Stamp;
30import org.graalvm.compiler.core.common.type.StampFactory;
31import org.graalvm.compiler.graph.NodeClass;
32import org.graalvm.compiler.graph.spi.CanonicalizerTool;
33import org.graalvm.compiler.nodeinfo.NodeCycles;
34import org.graalvm.compiler.nodeinfo.NodeInfo;
35import org.graalvm.compiler.nodes.ConstantNode;
36import org.graalvm.compiler.nodes.LogicConstantNode;
37import org.graalvm.compiler.nodes.LogicNode;
38import org.graalvm.compiler.nodes.ValueNode;
39import org.graalvm.compiler.nodes.spi.Lowerable;
40import org.graalvm.compiler.nodes.spi.LoweringTool;
41
42import jdk.vm.ci.meta.ConstantReflectionProvider;
43import jdk.vm.ci.meta.JavaKind;
44
45/**
46 * Returns -1, 0, or 1 if either x < y, x == y, or x > y. If the comparison is undecided (one
47 * of the inputs is NaN), the result is 1 if isUnorderedLess is false and -1 if isUnorderedLess is
48 * true.
49 */
50@NodeInfo(cycles = NodeCycles.CYCLES_2, size = SIZE_2)
51public final class NormalizeCompareNode extends BinaryNode implements Lowerable {
52
53    public static final NodeClass<NormalizeCompareNode> TYPE = NodeClass.create(NormalizeCompareNode.class);
54    protected final boolean isUnorderedLess;
55
56    public NormalizeCompareNode(ValueNode x, ValueNode y, boolean isUnorderedLess) {
57        super(TYPE, StampFactory.forKind(JavaKind.Int), x, y);
58        this.isUnorderedLess = isUnorderedLess;
59    }
60
61    public static ValueNode create(ValueNode x, ValueNode y, boolean isUnorderedLess, ConstantReflectionProvider constantReflection) {
62        LogicNode result = CompareNode.tryConstantFold(Condition.EQ, x, y, constantReflection, false);
63        if (result instanceof LogicConstantNode) {
64            LogicConstantNode logicConstantNode = (LogicConstantNode) result;
65            LogicNode resultLT = CompareNode.tryConstantFold(Condition.LT, x, y, constantReflection, isUnorderedLess);
66            if (resultLT instanceof LogicConstantNode) {
67                LogicConstantNode logicConstantNodeLT = (LogicConstantNode) resultLT;
68                if (logicConstantNodeLT.getValue()) {
69                    return ConstantNode.forInt(-1);
70                } else if (logicConstantNode.getValue()) {
71                    return ConstantNode.forInt(0);
72                } else {
73                    return ConstantNode.forInt(1);
74                }
75            }
76        }
77
78        return new NormalizeCompareNode(x, y, isUnorderedLess);
79    }
80
81    @Override
82    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
83        // nothing to do
84        return this;
85    }
86
87    @Override
88    public boolean inferStamp() {
89        return false;
90    }
91
92    @Override
93    public void lower(LoweringTool tool) {
94        LogicNode equalComp;
95        LogicNode lessComp;
96        if (getX().stamp() instanceof FloatStamp) {
97            equalComp = graph().addOrUniqueWithInputs(FloatEqualsNode.create(getX(), getY()));
98            lessComp = graph().addOrUniqueWithInputs(FloatLessThanNode.create(getX(), getY(), isUnorderedLess));
99        } else {
100            equalComp = graph().addOrUniqueWithInputs(IntegerEqualsNode.create(getX(), getY()));
101            lessComp = graph().addOrUniqueWithInputs(IntegerLessThanNode.create(getX(), getY()));
102        }
103
104        ConditionalNode equalValue = graph().unique(new ConditionalNode(equalComp, ConstantNode.forInt(0, graph()), ConstantNode.forInt(1, graph())));
105        ConditionalNode value = graph().unique(new ConditionalNode(lessComp, ConstantNode.forInt(-1, graph()), equalValue));
106        replaceAtUsagesAndDelete(value);
107    }
108
109    @Override
110    public Stamp foldStamp(Stamp stampX, Stamp stampY) {
111        return stamp();
112    }
113}
114