001/*
002 * Copyright (c) 2011, 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.nodes.calc;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.common.*;
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.calc.*;
030import com.oracle.graal.compiler.common.type.*;
031import com.oracle.graal.graph.*;
032import com.oracle.graal.graph.spi.*;
033import com.oracle.graal.nodeinfo.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.util.*;
036
037@NodeInfo(shortName = "<")
038public final class IntegerLessThanNode extends CompareNode {
039    public static final NodeClass<IntegerLessThanNode> TYPE = NodeClass.create(IntegerLessThanNode.class);
040
041    public IntegerLessThanNode(ValueNode x, ValueNode y) {
042        super(TYPE, Condition.LT, false, x, y);
043        assert !x.getKind().isNumericFloat() && x.getKind() != Kind.Object;
044        assert !y.getKind().isNumericFloat() && y.getKind() != Kind.Object;
045    }
046
047    public static LogicNode create(ValueNode x, ValueNode y, ConstantReflectionProvider constantReflection) {
048        LogicNode result = CompareNode.tryConstantFold(Condition.LT, x, y, constantReflection, false);
049        if (result != null) {
050            return result;
051        } else {
052            result = findSynonym(x, y);
053            if (result != null) {
054                return result;
055            }
056            return new IntegerLessThanNode(x, y);
057        }
058    }
059
060    @Override
061    protected ValueNode optimizeNormalizeCmp(Constant constant, NormalizeCompareNode normalizeNode, boolean mirrored) {
062        PrimitiveConstant primitive = (PrimitiveConstant) constant;
063        assert condition() == Condition.LT;
064        if (primitive.getKind() == Kind.Int && primitive.asInt() == 0) {
065            ValueNode a = mirrored ? normalizeNode.getY() : normalizeNode.getX();
066            ValueNode b = mirrored ? normalizeNode.getX() : normalizeNode.getY();
067
068            if (normalizeNode.getX().getKind() == Kind.Double || normalizeNode.getX().getKind() == Kind.Float) {
069                return new FloatLessThanNode(a, b, mirrored ^ normalizeNode.isUnorderedLess);
070            } else {
071                return new IntegerLessThanNode(a, b);
072            }
073        }
074        return this;
075    }
076
077    @Override
078    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
079        ValueNode result = super.canonical(tool, forX, forY);
080        if (result != this) {
081            return result;
082        }
083        ValueNode synonym = findSynonym(forX, forY);
084        if (synonym != null) {
085            return synonym;
086        }
087        if (forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) {
088            if (IntegerStamp.sameSign((IntegerStamp) forX.stamp(), (IntegerStamp) forY.stamp())) {
089                return new IntegerBelowNode(forX, forY);
090            }
091        }
092        return this;
093    }
094
095    private static LogicNode findSynonym(ValueNode forX, ValueNode forY) {
096        if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
097            return LogicConstantNode.contradiction();
098        } else if (forX.stamp() instanceof IntegerStamp && forY.stamp() instanceof IntegerStamp) {
099            IntegerStamp xStamp = (IntegerStamp) forX.stamp();
100            IntegerStamp yStamp = (IntegerStamp) forY.stamp();
101            if (xStamp.upperBound() < yStamp.lowerBound()) {
102                return LogicConstantNode.tautology();
103            } else if (xStamp.lowerBound() >= yStamp.upperBound()) {
104                return LogicConstantNode.contradiction();
105            }
106        }
107        return null;
108    }
109
110    @Override
111    protected CompareNode duplicateModified(ValueNode newX, ValueNode newY) {
112        if (newX.stamp() instanceof FloatStamp && newY.stamp() instanceof FloatStamp) {
113            return new FloatLessThanNode(newX, newY, true);
114        } else if (newX.stamp() instanceof IntegerStamp && newY.stamp() instanceof IntegerStamp) {
115            return new IntegerLessThanNode(newX, newY);
116        }
117        throw JVMCIError.shouldNotReachHere();
118    }
119
120    @Override
121    public Stamp getSucceedingStampForX(boolean negated) {
122        Stamp xStampGeneric = getX().stamp();
123        Stamp yStampGeneric = getY().stamp();
124        if (xStampGeneric instanceof IntegerStamp) {
125            IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
126            int bits = xStamp.getBits();
127            if (yStampGeneric instanceof IntegerStamp) {
128                IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
129                assert yStamp.getBits() == bits;
130                if (negated) {
131                    // x >= y
132                    long xLowerBound = xStamp.lowerBound();
133                    long yLowerBound = yStamp.lowerBound();
134                    if (yLowerBound > xLowerBound) {
135                        return new IntegerStamp(bits, yLowerBound, xStamp.upperBound(), xStamp.downMask(), xStamp.upMask());
136                    }
137                } else {
138                    // x < y
139                    long xUpperBound = xStamp.upperBound();
140                    long yUpperBound = yStamp.upperBound();
141                    if (yUpperBound == CodeUtil.minValue(bits)) {
142                        return null;
143                    } else if (yUpperBound <= xUpperBound) {
144                        assert yUpperBound != CodeUtil.minValue(bits);
145                        return new IntegerStamp(bits, xStamp.lowerBound(), yUpperBound - 1, xStamp.downMask(), xStamp.upMask());
146                    }
147                }
148            }
149        }
150        return null;
151    }
152
153    @Override
154    public Stamp getSucceedingStampForY(boolean negated) {
155        Stamp xStampGeneric = getX().stamp();
156        Stamp yStampGeneric = getY().stamp();
157        if (xStampGeneric instanceof IntegerStamp) {
158            IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
159            int bits = xStamp.getBits();
160            if (yStampGeneric instanceof IntegerStamp) {
161                IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
162                assert yStamp.getBits() == bits;
163                if (negated) {
164                    // y <= x
165                    long xUpperBound = xStamp.upperBound();
166                    long yUpperBound = yStamp.upperBound();
167                    if (xUpperBound < yUpperBound) {
168                        return new IntegerStamp(bits, yStamp.lowerBound(), xUpperBound, yStamp.downMask(), yStamp.upMask());
169                    }
170                } else {
171                    // y > x
172                    long xLowerBound = xStamp.lowerBound();
173                    long yLowerBound = yStamp.lowerBound();
174                    if (xLowerBound == CodeUtil.maxValue(bits)) {
175                        return null;
176                    } else if (xLowerBound >= yLowerBound) {
177                        assert xLowerBound != CodeUtil.maxValue(bits);
178                        return new IntegerStamp(bits, xLowerBound + 1, yStamp.upperBound(), yStamp.downMask(), yStamp.upMask());
179                    }
180                }
181            }
182        }
183        return null;
184    }
185
186    @Override
187    public TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
188        if (xStampGeneric instanceof IntegerStamp && yStampGeneric instanceof IntegerStamp) {
189            IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
190            IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
191            if (xStamp.upperBound() < yStamp.lowerBound()) {
192                return TriState.TRUE;
193            }
194            if (xStamp.lowerBound() >= yStamp.upperBound()) {
195                return TriState.FALSE;
196            }
197        }
198        return TriState.UNKNOWN;
199    }
200}