43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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 |
*/
|
|
23 |
package org.graalvm.compiler.nodes.calc;
|
|
24 |
|
48861
|
25 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
43972
|
26 |
import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
|
|
27 |
import org.graalvm.compiler.core.common.type.FloatStamp;
|
|
28 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
29 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
30 |
import org.graalvm.compiler.debug.GraalError;
|
46393
|
31 |
import org.graalvm.compiler.graph.Node;
|
43972
|
32 |
import org.graalvm.compiler.graph.NodeClass;
|
|
33 |
import org.graalvm.compiler.graph.spi.Canonicalizable.BinaryCommutative;
|
|
34 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
35 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
36 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
37 |
import org.graalvm.compiler.nodes.LogicConstantNode;
|
|
38 |
import org.graalvm.compiler.nodes.LogicNegationNode;
|
|
39 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
40 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
41 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
42 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
48861
|
43 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
44 |
|
|
45 |
import jdk.vm.ci.meta.Constant;
|
48861
|
46 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
43972
|
47 |
import jdk.vm.ci.meta.JavaKind;
|
48861
|
48 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
43972
|
49 |
import jdk.vm.ci.meta.PrimitiveConstant;
|
|
50 |
import jdk.vm.ci.meta.TriState;
|
|
51 |
|
|
52 |
@NodeInfo(shortName = "==")
|
46344
|
53 |
public final class IntegerEqualsNode extends CompareNode implements BinaryCommutative<ValueNode> {
|
43972
|
54 |
public static final NodeClass<IntegerEqualsNode> TYPE = NodeClass.create(IntegerEqualsNode.class);
|
46393
|
55 |
private static final IntegerEqualsOp OP = new IntegerEqualsOp();
|
43972
|
56 |
|
|
57 |
public IntegerEqualsNode(ValueNode x, ValueNode y) {
|
48861
|
58 |
super(TYPE, CanonicalCondition.EQ, false, x, y);
|
43972
|
59 |
assert !x.getStackKind().isNumericFloat() && x.getStackKind() != JavaKind.Object;
|
|
60 |
assert !y.getStackKind().isNumericFloat() && y.getStackKind() != JavaKind.Object;
|
|
61 |
}
|
|
62 |
|
48190
|
63 |
public static LogicNode create(ValueNode x, ValueNode y, NodeView view) {
|
48861
|
64 |
LogicNode result = CompareNode.tryConstantFoldPrimitive(CanonicalCondition.EQ, x, y, false, view);
|
43972
|
65 |
if (result != null) {
|
|
66 |
return result;
|
46393
|
67 |
}
|
|
68 |
if (x instanceof ConditionalNode) {
|
|
69 |
ConditionalNode conditionalNode = (ConditionalNode) x;
|
|
70 |
if (conditionalNode.trueValue() == y) {
|
|
71 |
return conditionalNode.condition();
|
|
72 |
}
|
|
73 |
if (conditionalNode.falseValue() == y) {
|
|
74 |
return LogicNegationNode.create(conditionalNode.condition());
|
|
75 |
}
|
|
76 |
} else if (y instanceof ConditionalNode) {
|
|
77 |
ConditionalNode conditionalNode = (ConditionalNode) y;
|
|
78 |
if (conditionalNode.trueValue() == x) {
|
|
79 |
return conditionalNode.condition();
|
43972
|
80 |
}
|
46393
|
81 |
if (conditionalNode.falseValue() == x) {
|
|
82 |
return LogicNegationNode.create(conditionalNode.condition());
|
|
83 |
}
|
|
84 |
}
|
|
85 |
return new IntegerEqualsNode(x, y).maybeCommuteInputs();
|
|
86 |
}
|
43972
|
87 |
|
48190
|
88 |
public static LogicNode create(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth, ValueNode x, ValueNode y,
|
|
89 |
NodeView view) {
|
48861
|
90 |
LogicNode value = OP.canonical(constantReflection, metaAccess, options, smallestCompareWidth, CanonicalCondition.EQ, false, x, y, view);
|
46393
|
91 |
if (value != null) {
|
|
92 |
return value;
|
43972
|
93 |
}
|
48190
|
94 |
return create(x, y, view);
|
43972
|
95 |
}
|
|
96 |
|
|
97 |
@Override
|
46393
|
98 |
public Node canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
48190
|
99 |
NodeView view = NodeView.from(tool);
|
48861
|
100 |
ValueNode value = OP.canonical(tool.getConstantReflection(), tool.getMetaAccess(), tool.getOptions(), tool.smallestCompareWidth(), CanonicalCondition.EQ, false, forX, forY, view);
|
46393
|
101 |
if (value != null) {
|
|
102 |
return value;
|
43972
|
103 |
}
|
|
104 |
return this;
|
|
105 |
}
|
|
106 |
|
46393
|
107 |
public static class IntegerEqualsOp extends CompareOp {
|
|
108 |
@Override
|
|
109 |
protected LogicNode optimizeNormalizeCompare(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
|
48190
|
110 |
Constant constant, NormalizeCompareNode normalizeNode, boolean mirrored, NodeView view) {
|
46393
|
111 |
PrimitiveConstant primitive = (PrimitiveConstant) constant;
|
|
112 |
ValueNode a = normalizeNode.getX();
|
|
113 |
ValueNode b = normalizeNode.getY();
|
|
114 |
long cst = primitive.asLong();
|
43972
|
115 |
|
46393
|
116 |
if (cst == 0) {
|
|
117 |
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
|
48190
|
118 |
return FloatEqualsNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, view);
|
46393
|
119 |
} else {
|
48190
|
120 |
return IntegerEqualsNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, view);
|
46393
|
121 |
}
|
|
122 |
} else if (cst == 1) {
|
|
123 |
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
|
48190
|
124 |
return FloatLessThanNode.create(b, a, !normalizeNode.isUnorderedLess, view);
|
46393
|
125 |
} else {
|
48190
|
126 |
return IntegerLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, b, a, view);
|
46393
|
127 |
}
|
|
128 |
} else if (cst == -1) {
|
|
129 |
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
|
48190
|
130 |
return FloatLessThanNode.create(a, b, normalizeNode.isUnorderedLess, view);
|
46393
|
131 |
} else {
|
48190
|
132 |
return IntegerLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, view);
|
46393
|
133 |
}
|
|
134 |
} else {
|
|
135 |
return LogicConstantNode.contradiction();
|
46371
|
136 |
}
|
|
137 |
}
|
46393
|
138 |
|
|
139 |
@Override
|
48190
|
140 |
protected CompareNode duplicateModified(ValueNode newX, ValueNode newY, boolean unorderedIsTrue, NodeView view) {
|
|
141 |
if (newX.stamp(view) instanceof FloatStamp && newY.stamp(view) instanceof FloatStamp) {
|
46393
|
142 |
return new FloatEqualsNode(newX, newY);
|
48190
|
143 |
} else if (newX.stamp(view) instanceof IntegerStamp && newY.stamp(view) instanceof IntegerStamp) {
|
46393
|
144 |
return new IntegerEqualsNode(newX, newY);
|
48190
|
145 |
} else if (newX.stamp(view) instanceof AbstractPointerStamp && newY.stamp(view) instanceof AbstractPointerStamp) {
|
46393
|
146 |
return new IntegerEqualsNode(newX, newY);
|
|
147 |
}
|
|
148 |
throw GraalError.shouldNotReachHere();
|
|
149 |
}
|
|
150 |
|
|
151 |
@Override
|
48861
|
152 |
public LogicNode canonical(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth, CanonicalCondition condition,
|
48190
|
153 |
boolean unorderedIsTrue, ValueNode forX, ValueNode forY, NodeView view) {
|
46393
|
154 |
if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
|
|
155 |
return LogicConstantNode.tautology();
|
48190
|
156 |
} else if (forX.stamp(view).alwaysDistinct(forY.stamp(view))) {
|
46393
|
157 |
return LogicConstantNode.contradiction();
|
|
158 |
}
|
46807
|
159 |
|
46393
|
160 |
if (forX instanceof AddNode && forY instanceof AddNode) {
|
|
161 |
AddNode addX = (AddNode) forX;
|
|
162 |
AddNode addY = (AddNode) forY;
|
|
163 |
ValueNode v1 = null;
|
|
164 |
ValueNode v2 = null;
|
|
165 |
if (addX.getX() == addY.getX()) {
|
|
166 |
v1 = addX.getY();
|
|
167 |
v2 = addY.getY();
|
|
168 |
} else if (addX.getX() == addY.getY()) {
|
|
169 |
v1 = addX.getY();
|
|
170 |
v2 = addY.getX();
|
|
171 |
} else if (addX.getY() == addY.getX()) {
|
|
172 |
v1 = addX.getX();
|
|
173 |
v2 = addY.getY();
|
|
174 |
} else if (addX.getY() == addY.getY()) {
|
|
175 |
v1 = addX.getX();
|
|
176 |
v2 = addY.getX();
|
|
177 |
}
|
|
178 |
if (v1 != null) {
|
|
179 |
assert v2 != null;
|
48190
|
180 |
return create(v1, v2, view);
|
46393
|
181 |
}
|
|
182 |
}
|
46807
|
183 |
|
48190
|
184 |
return super.canonical(constantReflection, metaAccess, options, smallestCompareWidth, condition, unorderedIsTrue, forX, forY, view);
|
46393
|
185 |
}
|
43972
|
186 |
|
46393
|
187 |
@Override
|
|
188 |
protected LogicNode canonicalizeSymmetricConstant(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
|
48861
|
189 |
CanonicalCondition condition, Constant constant, ValueNode nonConstant, boolean mirrored, boolean unorderedIsTrue, NodeView view) {
|
46393
|
190 |
if (constant instanceof PrimitiveConstant) {
|
|
191 |
PrimitiveConstant primitiveConstant = (PrimitiveConstant) constant;
|
48190
|
192 |
IntegerStamp nonConstantStamp = ((IntegerStamp) nonConstant.stamp(view));
|
46393
|
193 |
if ((primitiveConstant.asLong() == 1 && nonConstantStamp.upperBound() == 1 && nonConstantStamp.lowerBound() == 0) ||
|
|
194 |
(primitiveConstant.asLong() == -1 && nonConstantStamp.upperBound() == 0 && nonConstantStamp.lowerBound() == -1)) {
|
|
195 |
// nonConstant can only be 0 or 1 (respective -1), test against 0 instead of 1
|
|
196 |
// (respective -1) for a more canonical graph and also to allow for faster
|
|
197 |
// execution
|
|
198 |
// on specific platforms.
|
|
199 |
return LogicNegationNode.create(
|
48190
|
200 |
IntegerEqualsNode.create(constantReflection, metaAccess, options, smallestCompareWidth, nonConstant, ConstantNode.forIntegerKind(nonConstant.getStackKind(), 0),
|
|
201 |
view));
|
46393
|
202 |
} else if (primitiveConstant.asLong() == 0) {
|
|
203 |
if (nonConstant instanceof AndNode) {
|
|
204 |
AndNode andNode = (AndNode) nonConstant;
|
|
205 |
return new IntegerTestNode(andNode.getX(), andNode.getY());
|
|
206 |
} else if (nonConstant instanceof SubNode) {
|
|
207 |
SubNode subNode = (SubNode) nonConstant;
|
48190
|
208 |
return IntegerEqualsNode.create(constantReflection, metaAccess, options, smallestCompareWidth, subNode.getX(), subNode.getY(), view);
|
|
209 |
} else if (nonConstant instanceof ShiftNode && nonConstant.stamp(view) instanceof IntegerStamp) {
|
46393
|
210 |
if (nonConstant instanceof LeftShiftNode) {
|
|
211 |
LeftShiftNode shift = (LeftShiftNode) nonConstant;
|
|
212 |
if (shift.getY().isConstant()) {
|
|
213 |
int mask = shift.getShiftAmountMask();
|
|
214 |
int amount = shift.getY().asJavaConstant().asInt() & mask;
|
|
215 |
if (shift.getX().getStackKind() == JavaKind.Int) {
|
|
216 |
return new IntegerTestNode(shift.getX(), ConstantNode.forInt(-1 >>> amount));
|
|
217 |
} else {
|
|
218 |
assert shift.getX().getStackKind() == JavaKind.Long;
|
|
219 |
return new IntegerTestNode(shift.getX(), ConstantNode.forLong(-1L >>> amount));
|
|
220 |
}
|
46344
|
221 |
}
|
46393
|
222 |
} else if (nonConstant instanceof RightShiftNode) {
|
|
223 |
RightShiftNode shift = (RightShiftNode) nonConstant;
|
48190
|
224 |
if (shift.getY().isConstant() && ((IntegerStamp) shift.getX().stamp(view)).isPositive()) {
|
46393
|
225 |
int mask = shift.getShiftAmountMask();
|
|
226 |
int amount = shift.getY().asJavaConstant().asInt() & mask;
|
|
227 |
if (shift.getX().getStackKind() == JavaKind.Int) {
|
|
228 |
return new IntegerTestNode(shift.getX(), ConstantNode.forInt(-1 << amount));
|
|
229 |
} else {
|
|
230 |
assert shift.getX().getStackKind() == JavaKind.Long;
|
|
231 |
return new IntegerTestNode(shift.getX(), ConstantNode.forLong(-1L << amount));
|
|
232 |
}
|
46344
|
233 |
}
|
46393
|
234 |
} else if (nonConstant instanceof UnsignedRightShiftNode) {
|
|
235 |
UnsignedRightShiftNode shift = (UnsignedRightShiftNode) nonConstant;
|
|
236 |
if (shift.getY().isConstant()) {
|
|
237 |
int mask = shift.getShiftAmountMask();
|
|
238 |
int amount = shift.getY().asJavaConstant().asInt() & mask;
|
|
239 |
if (shift.getX().getStackKind() == JavaKind.Int) {
|
|
240 |
return new IntegerTestNode(shift.getX(), ConstantNode.forInt(-1 << amount));
|
|
241 |
} else {
|
|
242 |
assert shift.getX().getStackKind() == JavaKind.Long;
|
|
243 |
return new IntegerTestNode(shift.getX(), ConstantNode.forLong(-1L << amount));
|
|
244 |
}
|
46344
|
245 |
}
|
43972
|
246 |
}
|
|
247 |
}
|
|
248 |
}
|
46393
|
249 |
if (nonConstant instanceof AddNode) {
|
|
250 |
AddNode addNode = (AddNode) nonConstant;
|
|
251 |
if (addNode.getY().isJavaConstant()) {
|
|
252 |
return new IntegerEqualsNode(addNode.getX(), ConstantNode.forIntegerStamp(nonConstantStamp, primitiveConstant.asLong() - addNode.getY().asJavaConstant().asLong()));
|
|
253 |
}
|
|
254 |
}
|
|
255 |
if (nonConstant instanceof AndNode) {
|
|
256 |
/*
|
|
257 |
* a & c == c is the same as a & c != 0, if c is a single bit.
|
|
258 |
*/
|
|
259 |
AndNode andNode = (AndNode) nonConstant;
|
|
260 |
if (Long.bitCount(((PrimitiveConstant) constant).asLong()) == 1 && andNode.getY().isConstant() && andNode.getY().asJavaConstant().equals(constant)) {
|
|
261 |
return new LogicNegationNode(new IntegerTestNode(andNode.getX(), andNode.getY()));
|
|
262 |
}
|
46344
|
263 |
}
|
46807
|
264 |
|
48190
|
265 |
if (nonConstant instanceof XorNode && nonConstant.stamp(view) instanceof IntegerStamp) {
|
46807
|
266 |
XorNode xorNode = (XorNode) nonConstant;
|
48190
|
267 |
if (xorNode.getY().isJavaConstant() && xorNode.getY().asJavaConstant().asLong() == 1 && ((IntegerStamp) xorNode.getX().stamp(view)).upMask() == 1) {
|
46807
|
268 |
// x ^ 1 == 0 is the same as x == 1 if x in [0, 1]
|
|
269 |
// x ^ 1 == 1 is the same as x == 0 if x in [0, 1]
|
48190
|
270 |
return new IntegerEqualsNode(xorNode.getX(), ConstantNode.forIntegerStamp(xorNode.getX().stamp(view), primitiveConstant.asLong() ^ 1));
|
46807
|
271 |
}
|
|
272 |
}
|
46344
|
273 |
}
|
48190
|
274 |
return super.canonicalizeSymmetricConstant(constantReflection, metaAccess, options, smallestCompareWidth, condition, constant, nonConstant, mirrored, unorderedIsTrue, view);
|
43972
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
@Override
|
46344
|
279 |
public Stamp getSucceedingStampForX(boolean negated, Stamp xStamp, Stamp yStamp) {
|
43972
|
280 |
if (!negated) {
|
46344
|
281 |
return xStamp.join(yStamp);
|
43972
|
282 |
}
|
|
283 |
return null;
|
|
284 |
}
|
|
285 |
|
|
286 |
@Override
|
46344
|
287 |
public Stamp getSucceedingStampForY(boolean negated, Stamp xStamp, Stamp yStamp) {
|
43972
|
288 |
if (!negated) {
|
46344
|
289 |
return xStamp.join(yStamp);
|
43972
|
290 |
}
|
|
291 |
return null;
|
|
292 |
}
|
|
293 |
|
|
294 |
@Override
|
|
295 |
public TriState tryFold(Stamp xStampGeneric, Stamp yStampGeneric) {
|
|
296 |
if (xStampGeneric instanceof IntegerStamp && yStampGeneric instanceof IntegerStamp) {
|
|
297 |
IntegerStamp xStamp = (IntegerStamp) xStampGeneric;
|
|
298 |
IntegerStamp yStamp = (IntegerStamp) yStampGeneric;
|
|
299 |
if (xStamp.alwaysDistinct(yStamp)) {
|
|
300 |
return TriState.FALSE;
|
|
301 |
} else if (xStamp.neverDistinct(yStamp)) {
|
|
302 |
return TriState.TRUE;
|
|
303 |
}
|
|
304 |
}
|
|
305 |
return TriState.UNKNOWN;
|
|
306 |
}
|
|
307 |
}
|