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 LogicNode findSynonym(ValueNode forX, ValueNode forY, NodeView view) { 89 LogicNode result = super.findSynonym(forX, forY, view); 90 if (result != null) { 91 return result; 92 } 93 if (forX.stamp(view) instanceof IntegerStamp) { 94 assert forY.stamp(view) instanceof IntegerStamp; 95 int bits = ((IntegerStamp) forX.stamp(view)).getBits(); 96 assert ((IntegerStamp) forY.stamp(view)).getBits() == bits; 97 LogicNode logic = canonicalizeRangeFlip(forX, forY, bits, false, view); 98 if (logic != null) { 99 return logic; 100 } 101 } 102 return null; 103 } 104 105 @Override 106 protected long upperBound(IntegerStamp stamp) { 107 return stamp.unsignedUpperBound(); 108 } 109 110 @Override 111 protected long lowerBound(IntegerStamp stamp) { 112 return stamp.unsignedLowerBound(); 113 } 114 115 @Override 116 protected int compare(long a, long b) { 117 return Long.compareUnsigned(a, b); 118 } 119 120 @Override 121 protected long min(long a, long b) { 122 return NumUtil.minUnsigned(a, b); 123 } 124 125 @Override 126 protected long max(long a, long b) { 127 return NumUtil.maxUnsigned(a, b); 128 } 129 130 @Override 131 protected long cast(long a, int bits) { 132 return CodeUtil.zeroExtend(a, bits); 133 } 134 135 @Override 136 protected long minValue(int bits) { 137 return 0; 138 } 139 140 @Override 141 protected long maxValue(int bits) { 142 return NumUtil.maxValueUnsigned(bits); 143 } 144 145 @Override 146 protected IntegerStamp forInteger(int bits, long min, long max) { 147 return StampFactory.forUnsignedInteger(bits, min, max); 148 } 149 150 @Override 151 protected CanonicalCondition getCondition() { 152 return CanonicalCondition.BT; 153 } 154 155 @Override 156 protected IntegerLowerThanNode createNode(ValueNode x, ValueNode y) { 157 return new IntegerBelowNode(x, y); 158 } 159 } 160 161 @Override 162 public TriState implies(boolean thisNegated, LogicNode other) { 163 if (other instanceof LogicNegationNode) { 164 // Unwrap negations. 165 TriState result = implies(thisNegated, ((LogicNegationNode) other).getValue()); 166 if (result.isKnown()) { 167 return TriState.get(!result.toBoolean()); 168 } 169 } 170 if (!thisNegated) { 171 if (other instanceof IntegerLessThanNode) { 172 IntegerLessThanNode integerLessThanNode = (IntegerLessThanNode) other; 173 IntegerStamp stampL = (IntegerStamp) this.getY().stamp(NodeView.DEFAULT); 174 // if L >= 0: 175 if (stampL.isPositive()) { // L >= 0 176 if (this.getX() == integerLessThanNode.getX()) { 177 // x |<| L implies x < L 178 if (this.getY() == integerLessThanNode.getY()) { 179 return TriState.TRUE; 180 } 181 // x |<| L implies !(x < 0) 182 if (integerLessThanNode.getY().isConstant() && 183 IntegerStamp.OPS.getAdd().isNeutral(integerLessThanNode.getY().asConstant())) { 184 return TriState.FALSE; 185 } 186 } 187 } 188 } 189 } 190 return super.implies(thisNegated, other); 191 } 192 }