1 /*
   2  * Copyright (c) 2011, 2017, 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  */
  23 
  24 
  25 package org.graalvm.compiler.nodes.calc;
  26 
  27 import org.graalvm.compiler.core.common.NumUtil;
  28 import org.graalvm.compiler.core.common.calc.CanonicalCondition;
  29 import org.graalvm.compiler.core.common.type.IntegerStamp;
  30 import org.graalvm.compiler.core.common.type.StampFactory;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodes.LogicNegationNode;
  36 import org.graalvm.compiler.nodes.LogicNode;
  37 import org.graalvm.compiler.nodes.NodeView;
  38 import org.graalvm.compiler.nodes.ValueNode;
  39 import org.graalvm.compiler.options.OptionValues;
  40 
  41 import jdk.vm.ci.code.CodeUtil;
  42 import jdk.vm.ci.meta.ConstantReflectionProvider;
  43 import jdk.vm.ci.meta.MetaAccessProvider;
  44 import jdk.vm.ci.meta.TriState;
  45 
  46 @NodeInfo(shortName = "|<|")
  47 public final class IntegerBelowNode extends IntegerLowerThanNode {
  48     public static final NodeClass<IntegerBelowNode> TYPE = NodeClass.create(IntegerBelowNode.class);
  49     private static final BelowOp OP = new BelowOp();
  50 
  51     public IntegerBelowNode(ValueNode x, ValueNode y) {
  52         super(TYPE, x, y, OP);
  53         assert x.stamp(NodeView.DEFAULT) instanceof IntegerStamp;
  54         assert y.stamp(NodeView.DEFAULT) instanceof IntegerStamp;
  55     }
  56 
  57     public static LogicNode create(ValueNode x, ValueNode y, NodeView view) {
  58         return OP.create(x, y, view);
  59     }
  60 
  61     public static LogicNode create(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth, ValueNode x, ValueNode y,
  62                     NodeView view) {
  63         LogicNode value = OP.canonical(constantReflection, metaAccess, options, smallestCompareWidth, OP.getCondition(), false, x, y, view);
  64         if (value != null) {
  65             return value;
  66         }
  67         return create(x, y, view);
  68     }
  69 
  70     @Override
  71     public Node canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  72         NodeView view = NodeView.from(tool);
  73         ValueNode value = OP.canonical(tool.getConstantReflection(), tool.getMetaAccess(), tool.getOptions(), tool.smallestCompareWidth(), OP.getCondition(), false, forX, forY, view);
  74         if (value != null) {
  75             return value;
  76         }
  77         return this;
  78     }
  79 
  80     public static class BelowOp extends LowerOp {
  81         @Override
  82         protected CompareNode duplicateModified(ValueNode newX, ValueNode newY, boolean unorderedIsTrue, NodeView view) {
  83             assert newX.stamp(NodeView.DEFAULT) instanceof IntegerStamp && newY.stamp(NodeView.DEFAULT) instanceof IntegerStamp;
  84             return new IntegerBelowNode(newX, newY);
  85         }
  86 
  87         @Override
  88         protected long upperBound(IntegerStamp stamp) {
  89             return stamp.unsignedUpperBound();
  90         }
  91 
  92         @Override
  93         protected long lowerBound(IntegerStamp stamp) {
  94             return stamp.unsignedLowerBound();
  95         }
  96 
  97         @Override
  98         protected int compare(long a, long b) {
  99             return Long.compareUnsigned(a, b);
 100         }
 101 
 102         @Override
 103         protected long min(long a, long b) {
 104             return NumUtil.minUnsigned(a, b);
 105         }
 106 
 107         @Override
 108         protected long max(long a, long b) {
 109             return NumUtil.maxUnsigned(a, b);
 110         }
 111 
 112         @Override
 113         protected long cast(long a, int bits) {
 114             return CodeUtil.zeroExtend(a, bits);
 115         }
 116 
 117         @Override
 118         protected long minValue(int bits) {
 119             return 0;
 120         }
 121 
 122         @Override
 123         protected long maxValue(int bits) {
 124             return NumUtil.maxValueUnsigned(bits);
 125         }
 126 
 127         @Override
 128         protected IntegerStamp forInteger(int bits, long min, long max) {
 129             return StampFactory.forUnsignedInteger(bits, min, max);
 130         }
 131 
 132         @Override
 133         protected CanonicalCondition getCondition() {
 134             return CanonicalCondition.BT;
 135         }
 136 
 137         @Override
 138         protected IntegerLowerThanNode createNode(ValueNode x, ValueNode y) {
 139             return new IntegerBelowNode(x, y);
 140         }
 141     }
 142 
 143     @Override
 144     public TriState implies(boolean thisNegated, LogicNode other) {
 145         if (other instanceof LogicNegationNode) {
 146             // Unwrap negations.
 147             TriState result = implies(thisNegated, ((LogicNegationNode) other).getValue());
 148             if (result.isKnown()) {
 149                 return TriState.get(!result.toBoolean());
 150             }
 151         }
 152         if (!thisNegated) {
 153             if (other instanceof IntegerLessThanNode) {
 154                 IntegerLessThanNode integerLessThanNode = (IntegerLessThanNode) other;
 155                 IntegerStamp stampL = (IntegerStamp) this.getY().stamp(NodeView.DEFAULT);
 156                 // if L >= 0:
 157                 if (stampL.isPositive()) { // L >= 0
 158                     if (this.getX() == integerLessThanNode.getX()) {
 159                         // x |<| L implies x < L
 160                         if (this.getY() == integerLessThanNode.getY()) {
 161                             return TriState.TRUE;
 162                         }
 163                         // x |<| L implies !(x < 0)
 164                         if (integerLessThanNode.getY().isConstant() &&
 165                                         IntegerStamp.OPS.getAdd().isNeutral(integerLessThanNode.getY().asConstant())) {
 166                             return TriState.FALSE;
 167                         }
 168                     }
 169                 }
 170             }
 171         }
 172         return super.implies(thisNegated, other);
 173     }
 174 }