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