43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2009, 2016, 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.java;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.nodeinfo.InputType.Anchor;
|
46393
|
26 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8;
|
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
|
43972
|
28 |
|
|
29 |
import org.graalvm.compiler.core.common.type.ObjectStamp;
|
|
30 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
32 |
import org.graalvm.compiler.core.common.type.TypeReference;
|
|
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.LogicConstantNode;
|
|
37 |
import org.graalvm.compiler.nodes.LogicNegationNode;
|
|
38 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
39 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
40 |
import org.graalvm.compiler.nodes.UnaryOpLogicNode;
|
|
41 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
42 |
import org.graalvm.compiler.nodes.calc.IsNullNode;
|
|
43 |
import org.graalvm.compiler.nodes.extended.AnchoringNode;
|
|
44 |
import org.graalvm.compiler.nodes.spi.Lowerable;
|
|
45 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
46 |
import org.graalvm.compiler.nodes.spi.Virtualizable;
|
|
47 |
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
|
|
48 |
import org.graalvm.compiler.nodes.type.StampTool;
|
|
49 |
|
|
50 |
import jdk.vm.ci.meta.JavaTypeProfile;
|
|
51 |
import jdk.vm.ci.meta.TriState;
|
|
52 |
|
47798
|
53 |
import java.util.Objects;
|
|
54 |
|
43972
|
55 |
/**
|
|
56 |
* The {@code InstanceOfNode} represents an instanceof test.
|
|
57 |
*/
|
46393
|
58 |
@NodeInfo(cycles = CYCLES_8, size = SIZE_8)
|
43972
|
59 |
public class InstanceOfNode extends UnaryOpLogicNode implements Lowerable, Virtualizable {
|
|
60 |
public static final NodeClass<InstanceOfNode> TYPE = NodeClass.create(InstanceOfNode.class);
|
|
61 |
|
47798
|
62 |
private ObjectStamp checkedStamp;
|
43972
|
63 |
|
|
64 |
private JavaTypeProfile profile;
|
|
65 |
@OptionalInput(Anchor) protected AnchoringNode anchor;
|
|
66 |
|
|
67 |
private InstanceOfNode(ObjectStamp checkedStamp, ValueNode object, JavaTypeProfile profile, AnchoringNode anchor) {
|
|
68 |
this(TYPE, checkedStamp, object, profile, anchor);
|
|
69 |
}
|
|
70 |
|
|
71 |
protected InstanceOfNode(NodeClass<? extends InstanceOfNode> c, ObjectStamp checkedStamp, ValueNode object, JavaTypeProfile profile, AnchoringNode anchor) {
|
|
72 |
super(c, object);
|
|
73 |
this.checkedStamp = checkedStamp;
|
|
74 |
this.profile = profile;
|
|
75 |
this.anchor = anchor;
|
|
76 |
assert (profile == null) || (anchor != null) : "profiles must be anchored";
|
|
77 |
assert checkedStamp != null;
|
|
78 |
}
|
|
79 |
|
|
80 |
public static LogicNode createAllowNull(TypeReference type, ValueNode object, JavaTypeProfile profile, AnchoringNode anchor) {
|
|
81 |
if (StampTool.isPointerNonNull(object)) {
|
|
82 |
return create(type, object, profile, anchor);
|
|
83 |
}
|
|
84 |
return createHelper(StampFactory.object(type), object, profile, anchor);
|
|
85 |
}
|
|
86 |
|
|
87 |
public static LogicNode create(TypeReference type, ValueNode object) {
|
|
88 |
return create(type, object, null, null);
|
|
89 |
}
|
|
90 |
|
|
91 |
public static LogicNode create(TypeReference type, ValueNode object, JavaTypeProfile profile, AnchoringNode anchor) {
|
|
92 |
return createHelper(StampFactory.objectNonNull(type), object, profile, anchor);
|
|
93 |
}
|
|
94 |
|
|
95 |
public static LogicNode createHelper(ObjectStamp checkedStamp, ValueNode object, JavaTypeProfile profile, AnchoringNode anchor) {
|
48190
|
96 |
LogicNode synonym = findSynonym(checkedStamp, object, NodeView.DEFAULT);
|
43972
|
97 |
if (synonym != null) {
|
|
98 |
return synonym;
|
|
99 |
} else {
|
|
100 |
return new InstanceOfNode(checkedStamp, object, profile, anchor);
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
public void lower(LoweringTool tool) {
|
|
106 |
tool.getLowerer().lower(this, tool);
|
|
107 |
}
|
|
108 |
|
|
109 |
@Override
|
|
110 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
|
48190
|
111 |
NodeView view = NodeView.from(tool);
|
|
112 |
LogicNode synonym = findSynonym(checkedStamp, forValue, view);
|
43972
|
113 |
if (synonym != null) {
|
|
114 |
return synonym;
|
|
115 |
} else {
|
|
116 |
return this;
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
48190
|
120 |
public static LogicNode findSynonym(ObjectStamp checkedStamp, ValueNode object, NodeView view) {
|
|
121 |
ObjectStamp inputStamp = (ObjectStamp) object.stamp(view);
|
43972
|
122 |
ObjectStamp joinedStamp = (ObjectStamp) checkedStamp.join(inputStamp);
|
|
123 |
|
|
124 |
if (joinedStamp.isEmpty()) {
|
|
125 |
// The check can never succeed, the intersection of the two stamps is empty.
|
|
126 |
return LogicConstantNode.contradiction();
|
|
127 |
} else {
|
|
128 |
ObjectStamp meetStamp = (ObjectStamp) checkedStamp.meet(inputStamp);
|
|
129 |
if (checkedStamp.equals(meetStamp)) {
|
|
130 |
// The check will always succeed, the union of the two stamps is equal to the
|
|
131 |
// checked stamp.
|
|
132 |
return LogicConstantNode.tautology();
|
47798
|
133 |
} else if (checkedStamp.alwaysNull()) {
|
|
134 |
return IsNullNode.create(object);
|
|
135 |
} else if (Objects.equals(checkedStamp.type(), meetStamp.type()) && checkedStamp.isExactType() == meetStamp.isExactType() && checkedStamp.alwaysNull() == meetStamp.alwaysNull()) {
|
43972
|
136 |
assert checkedStamp.nonNull() != inputStamp.nonNull();
|
|
137 |
// The only difference makes the null-ness of the value => simplify the check.
|
|
138 |
if (checkedStamp.nonNull()) {
|
|
139 |
return LogicNegationNode.create(IsNullNode.create(object));
|
|
140 |
} else {
|
|
141 |
return IsNullNode.create(object);
|
|
142 |
}
|
|
143 |
}
|
47798
|
144 |
assert checkedStamp.type() != null;
|
43972
|
145 |
}
|
|
146 |
return null;
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Gets the type being tested.
|
|
151 |
*/
|
|
152 |
public TypeReference type() {
|
|
153 |
return StampTool.typeReferenceOrNull(checkedStamp);
|
|
154 |
}
|
|
155 |
|
|
156 |
public JavaTypeProfile profile() {
|
|
157 |
return profile;
|
|
158 |
}
|
|
159 |
|
|
160 |
@Override
|
|
161 |
public void virtualize(VirtualizerTool tool) {
|
|
162 |
ValueNode alias = tool.getAlias(getValue());
|
48190
|
163 |
TriState fold = tryFold(alias.stamp(NodeView.DEFAULT));
|
43972
|
164 |
if (fold != TriState.UNKNOWN) {
|
|
165 |
tool.replaceWithValue(LogicConstantNode.forBoolean(fold.isTrue(), graph()));
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
@Override
|
|
170 |
public Stamp getSucceedingStampForValue(boolean negated) {
|
|
171 |
if (negated) {
|
|
172 |
return null;
|
|
173 |
} else {
|
|
174 |
return checkedStamp;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
@Override
|
|
179 |
public TriState tryFold(Stamp valueStamp) {
|
|
180 |
if (valueStamp instanceof ObjectStamp) {
|
|
181 |
ObjectStamp inputStamp = (ObjectStamp) valueStamp;
|
|
182 |
ObjectStamp joinedStamp = (ObjectStamp) checkedStamp.join(inputStamp);
|
|
183 |
|
|
184 |
if (joinedStamp.isEmpty()) {
|
|
185 |
// The check can never succeed, the intersection of the two stamps is empty.
|
|
186 |
return TriState.FALSE;
|
|
187 |
} else {
|
|
188 |
ObjectStamp meetStamp = (ObjectStamp) checkedStamp.meet(inputStamp);
|
|
189 |
if (checkedStamp.equals(meetStamp)) {
|
|
190 |
// The check will always succeed, the union of the two stamps is equal to the
|
|
191 |
// checked stamp.
|
|
192 |
return TriState.TRUE;
|
|
193 |
}
|
|
194 |
}
|
|
195 |
}
|
|
196 |
return TriState.UNKNOWN;
|
|
197 |
}
|
|
198 |
|
|
199 |
public boolean allowsNull() {
|
|
200 |
return !checkedStamp.nonNull();
|
|
201 |
}
|
|
202 |
|
|
203 |
public void setProfile(JavaTypeProfile typeProfile, AnchoringNode anchor) {
|
|
204 |
this.profile = typeProfile;
|
|
205 |
updateUsagesInterface(this.anchor, anchor);
|
|
206 |
this.anchor = anchor;
|
|
207 |
assert (profile == null) || (anchor != null) : "profiles must be anchored";
|
|
208 |
}
|
|
209 |
|
|
210 |
public AnchoringNode getAnchor() {
|
|
211 |
return anchor;
|
|
212 |
}
|
47798
|
213 |
|
|
214 |
public ObjectStamp getCheckedStamp() {
|
|
215 |
return checkedStamp;
|
|
216 |
}
|
|
217 |
|
|
218 |
public void strengthenCheckedStamp(ObjectStamp newCheckedStamp) {
|
|
219 |
assert this.checkedStamp.join(newCheckedStamp).equals(newCheckedStamp) : "stamp can only improve";
|
|
220 |
this.checkedStamp = newCheckedStamp;
|
|
221 |
}
|
43972
|
222 |
}
|