43972
|
1 |
/*
|
58533
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.nodes.calc;
|
|
26 |
|
48861
|
27 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
43972
|
28 |
import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
|
|
29 |
import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
|
|
30 |
import org.graalvm.compiler.core.common.type.ObjectStamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.TypeReference;
|
|
32 |
import org.graalvm.compiler.debug.GraalError;
|
|
33 |
import org.graalvm.compiler.graph.NodeClass;
|
|
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.LogicNode;
|
48190
|
39 |
import org.graalvm.compiler.nodes.NodeView;
|
52578
|
40 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
43972
|
41 |
import org.graalvm.compiler.nodes.ValueNode;
|
58533
|
42 |
import org.graalvm.compiler.nodes.extended.BoxNode;
|
43972
|
43 |
import org.graalvm.compiler.nodes.extended.GetClassNode;
|
58533
|
44 |
import org.graalvm.compiler.nodes.java.AbstractNewObjectNode;
|
43972
|
45 |
import org.graalvm.compiler.nodes.java.InstanceOfNode;
|
|
46 |
import org.graalvm.compiler.nodes.spi.Virtualizable;
|
|
47 |
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
|
58533
|
48 |
import org.graalvm.compiler.nodes.virtual.AllocatedObjectNode;
|
43972
|
49 |
import org.graalvm.compiler.nodes.virtual.VirtualBoxingNode;
|
|
50 |
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
|
48861
|
51 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
52 |
|
|
53 |
import jdk.vm.ci.meta.Constant;
|
|
54 |
import jdk.vm.ci.meta.ConstantReflectionProvider;
|
|
55 |
import jdk.vm.ci.meta.JavaConstant;
|
|
56 |
import jdk.vm.ci.meta.JavaKind;
|
|
57 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
58 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
59 |
|
|
60 |
@NodeInfo(shortName = "==")
|
|
61 |
public final class ObjectEqualsNode extends PointerEqualsNode implements Virtualizable {
|
|
62 |
|
|
63 |
public static final NodeClass<ObjectEqualsNode> TYPE = NodeClass.create(ObjectEqualsNode.class);
|
46393
|
64 |
private static final ObjectEqualsOp OP = new ObjectEqualsOp();
|
43972
|
65 |
|
|
66 |
public ObjectEqualsNode(ValueNode x, ValueNode y) {
|
|
67 |
super(TYPE, x, y);
|
48190
|
68 |
assert x.stamp(NodeView.DEFAULT) instanceof AbstractObjectStamp;
|
|
69 |
assert y.stamp(NodeView.DEFAULT) instanceof AbstractObjectStamp;
|
43972
|
70 |
}
|
|
71 |
|
48190
|
72 |
public static LogicNode create(ValueNode x, ValueNode y, ConstantReflectionProvider constantReflection, NodeView view) {
|
48861
|
73 |
LogicNode result = CompareNode.tryConstantFold(CanonicalCondition.EQ, x, y, constantReflection, false);
|
43972
|
74 |
if (result != null) {
|
|
75 |
return result;
|
|
76 |
} else {
|
48190
|
77 |
result = findSynonym(x, y, view);
|
43972
|
78 |
if (result != null) {
|
|
79 |
return result;
|
|
80 |
}
|
|
81 |
return new ObjectEqualsNode(x, y);
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
48190
|
85 |
public static LogicNode create(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, ValueNode x, ValueNode y, NodeView view) {
|
48861
|
86 |
LogicNode result = OP.canonical(constantReflection, metaAccess, options, null, CanonicalCondition.EQ, false, x, y, view);
|
46393
|
87 |
if (result != null) {
|
|
88 |
return result;
|
|
89 |
}
|
48190
|
90 |
return create(x, y, constantReflection, view);
|
46393
|
91 |
}
|
|
92 |
|
43972
|
93 |
@Override
|
46393
|
94 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
48190
|
95 |
NodeView view = NodeView.from(tool);
|
48861
|
96 |
ValueNode value = OP.canonical(tool.getConstantReflection(), tool.getMetaAccess(), tool.getOptions(), tool.smallestCompareWidth(), CanonicalCondition.EQ, false, forX, forY, view);
|
46393
|
97 |
if (value != null) {
|
|
98 |
return value;
|
|
99 |
}
|
|
100 |
return this;
|
|
101 |
}
|
|
102 |
|
|
103 |
public static class ObjectEqualsOp extends PointerEqualsOp {
|
|
104 |
|
|
105 |
@Override
|
|
106 |
protected LogicNode canonicalizeSymmetricConstant(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
|
48861
|
107 |
CanonicalCondition condition, Constant constant, ValueNode nonConstant, boolean mirrored, boolean unorderedIsTrue, NodeView view) {
|
46393
|
108 |
ResolvedJavaType type = constantReflection.asJavaType(constant);
|
|
109 |
if (type != null && nonConstant instanceof GetClassNode) {
|
|
110 |
GetClassNode getClassNode = (GetClassNode) nonConstant;
|
|
111 |
ValueNode object = getClassNode.getObject();
|
48190
|
112 |
assert ((ObjectStamp) object.stamp(view)).nonNull();
|
46393
|
113 |
if (!type.isPrimitive() && (type.isConcrete() || type.isArray())) {
|
|
114 |
return InstanceOfNode.create(TypeReference.createExactTrusted(type), object);
|
|
115 |
}
|
|
116 |
return LogicConstantNode.forBoolean(false);
|
43972
|
117 |
}
|
58533
|
118 |
if (nonConstant instanceof AbstractNewObjectNode || nonConstant instanceof AllocatedObjectNode) {
|
|
119 |
assert !(nonConstant instanceof BoxNode); // guard against class hierarchy changes
|
|
120 |
// a constant can never be equals to a new object
|
|
121 |
return LogicConstantNode.forBoolean(false);
|
|
122 |
}
|
48190
|
123 |
return super.canonicalizeSymmetricConstant(constantReflection, metaAccess, options, smallestCompareWidth, condition, constant, nonConstant, mirrored, unorderedIsTrue, view);
|
43972
|
124 |
}
|
46393
|
125 |
|
|
126 |
@Override
|
48190
|
127 |
protected CompareNode duplicateModified(ValueNode newX, ValueNode newY, boolean unorderedIsTrue, NodeView view) {
|
|
128 |
if (newX.stamp(view) instanceof ObjectStamp && newY.stamp(view) instanceof ObjectStamp) {
|
46393
|
129 |
return new ObjectEqualsNode(newX, newY);
|
48190
|
130 |
} else if (newX.stamp(view) instanceof AbstractPointerStamp && newY.stamp(view) instanceof AbstractPointerStamp) {
|
46393
|
131 |
return new PointerEqualsNode(newX, newY);
|
|
132 |
}
|
|
133 |
throw GraalError.shouldNotReachHere();
|
|
134 |
}
|
43972
|
135 |
}
|
|
136 |
|
52578
|
137 |
private static LogicNode virtualizeNonVirtualComparison(VirtualObjectNode virtual, ValueNode other, StructuredGraph graph, VirtualizerTool tool) {
|
43972
|
138 |
if (virtual instanceof VirtualBoxingNode && other.isConstant()) {
|
|
139 |
VirtualBoxingNode virtualBoxingNode = (VirtualBoxingNode) virtual;
|
|
140 |
if (virtualBoxingNode.getBoxingKind() == JavaKind.Boolean) {
|
52578
|
141 |
JavaConstant otherUnboxed = tool.getConstantReflection().unboxPrimitive(other.asJavaConstant());
|
43972
|
142 |
if (otherUnboxed != null && otherUnboxed.getJavaKind() == JavaKind.Boolean) {
|
|
143 |
int expectedValue = otherUnboxed.asBoolean() ? 1 : 0;
|
52578
|
144 |
return new IntegerEqualsNode(virtualBoxingNode.getBoxedValue(tool), ConstantNode.forInt(expectedValue, graph));
|
43972
|
145 |
} else {
|
52578
|
146 |
return LogicConstantNode.contradiction(graph);
|
43972
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
if (virtual.hasIdentity()) {
|
|
151 |
// one of them is virtual: they can never be the same objects
|
52578
|
152 |
return LogicConstantNode.contradiction(graph);
|
43972
|
153 |
}
|
52578
|
154 |
return null;
|
43972
|
155 |
}
|
|
156 |
|
52578
|
157 |
public static LogicNode virtualizeComparison(ValueNode x, ValueNode y, StructuredGraph graph, VirtualizerTool tool) {
|
|
158 |
ValueNode xAlias = tool.getAlias(x);
|
|
159 |
ValueNode yAlias = tool.getAlias(y);
|
43972
|
160 |
|
|
161 |
VirtualObjectNode xVirtual = xAlias instanceof VirtualObjectNode ? (VirtualObjectNode) xAlias : null;
|
|
162 |
VirtualObjectNode yVirtual = yAlias instanceof VirtualObjectNode ? (VirtualObjectNode) yAlias : null;
|
|
163 |
|
|
164 |
if (xVirtual != null && yVirtual == null) {
|
52578
|
165 |
return virtualizeNonVirtualComparison(xVirtual, yAlias, graph, tool);
|
43972
|
166 |
} else if (xVirtual == null && yVirtual != null) {
|
52578
|
167 |
return virtualizeNonVirtualComparison(yVirtual, xAlias, graph, tool);
|
43972
|
168 |
} else if (xVirtual != null && yVirtual != null) {
|
|
169 |
if (xVirtual.hasIdentity() ^ yVirtual.hasIdentity()) {
|
|
170 |
/*
|
|
171 |
* One of the two objects has identity, the other doesn't. In code, this looks like
|
|
172 |
* "Integer.valueOf(a) == new Integer(b)", which is always false.
|
|
173 |
*
|
|
174 |
* In other words: an object created via valueOf can never be equal to one created
|
|
175 |
* by new in the same compilation unit.
|
|
176 |
*/
|
52578
|
177 |
return LogicConstantNode.contradiction(graph);
|
43972
|
178 |
} else if (!xVirtual.hasIdentity() && !yVirtual.hasIdentity()) {
|
|
179 |
ResolvedJavaType type = xVirtual.type();
|
|
180 |
if (type.equals(yVirtual.type())) {
|
52578
|
181 |
MetaAccessProvider metaAccess = tool.getMetaAccess();
|
43972
|
182 |
if (type.equals(metaAccess.lookupJavaType(Integer.class)) || type.equals(metaAccess.lookupJavaType(Long.class))) {
|
|
183 |
// both are virtual without identity: check contents
|
|
184 |
assert xVirtual.entryCount() == 1 && yVirtual.entryCount() == 1;
|
|
185 |
assert xVirtual.entryKind(0).getStackKind() == JavaKind.Int || xVirtual.entryKind(0) == JavaKind.Long;
|
52578
|
186 |
return new IntegerEqualsNode(tool.getEntry(xVirtual, 0), tool.getEntry(yVirtual, 0));
|
43972
|
187 |
}
|
|
188 |
}
|
|
189 |
} else {
|
|
190 |
// both are virtual with identity: check if they refer to the same object
|
52578
|
191 |
return LogicConstantNode.forBoolean(xVirtual == yVirtual, graph);
|
43972
|
192 |
}
|
|
193 |
}
|
52578
|
194 |
return null;
|
|
195 |
}
|
|
196 |
|
|
197 |
@Override
|
|
198 |
public void virtualize(VirtualizerTool tool) {
|
|
199 |
ValueNode node = virtualizeComparison(getX(), getY(), graph(), tool);
|
|
200 |
if (node == null) {
|
|
201 |
return;
|
|
202 |
}
|
|
203 |
if (!node.isAlive()) {
|
|
204 |
tool.addNode(node);
|
|
205 |
}
|
|
206 |
tool.replaceWithValue(node);
|
43972
|
207 |
}
|
|
208 |
}
|