43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2009, 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 |
|
|
25 |
import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
|
|
26 |
import org.graalvm.compiler.core.common.type.ObjectStamp;
|
|
27 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
28 |
import org.graalvm.compiler.graph.NodeClass;
|
|
29 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
30 |
import org.graalvm.compiler.nodeinfo.NodeCycles;
|
|
31 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
32 |
import org.graalvm.compiler.nodes.LogicConstantNode;
|
|
33 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
34 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
35 |
import org.graalvm.compiler.nodes.PiNode;
|
|
36 |
import org.graalvm.compiler.nodes.UnaryOpLogicNode;
|
|
37 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
38 |
import org.graalvm.compiler.nodes.spi.LIRLowerable;
|
|
39 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
40 |
import org.graalvm.compiler.nodes.spi.Virtualizable;
|
|
41 |
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
|
|
42 |
import org.graalvm.compiler.nodes.type.StampTool;
|
46344
|
43 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
43972
|
44 |
|
|
45 |
import jdk.vm.ci.meta.TriState;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* An IsNullNode will be true if the supplied value is null, and false if it is non-null.
|
|
49 |
*/
|
|
50 |
@NodeInfo(cycles = NodeCycles.CYCLES_2)
|
46344
|
51 |
public final class IsNullNode extends UnaryOpLogicNode implements LIRLowerable, Virtualizable {
|
43972
|
52 |
|
|
53 |
public static final NodeClass<IsNullNode> TYPE = NodeClass.create(IsNullNode.class);
|
|
54 |
|
|
55 |
public IsNullNode(ValueNode object) {
|
|
56 |
super(TYPE, object);
|
|
57 |
assert object != null;
|
|
58 |
}
|
|
59 |
|
|
60 |
public static LogicNode create(ValueNode forValue) {
|
46371
|
61 |
return canonicalized(null, forValue);
|
43972
|
62 |
}
|
|
63 |
|
|
64 |
public static LogicNode tryCanonicalize(ValueNode forValue) {
|
|
65 |
if (StampTool.isPointerAlwaysNull(forValue)) {
|
|
66 |
return LogicConstantNode.tautology();
|
|
67 |
} else if (StampTool.isPointerNonNull(forValue)) {
|
|
68 |
return LogicConstantNode.contradiction();
|
|
69 |
}
|
|
70 |
return null;
|
|
71 |
}
|
|
72 |
|
|
73 |
@Override
|
|
74 |
public void generate(NodeLIRBuilderTool gen) {
|
|
75 |
// Nothing to do.
|
|
76 |
}
|
|
77 |
|
|
78 |
@Override
|
|
79 |
public boolean verify() {
|
|
80 |
assertTrue(getValue() != null, "is null input must not be null");
|
48190
|
81 |
assertTrue(getValue().stamp(NodeView.DEFAULT) instanceof AbstractPointerStamp, "input must be a pointer not %s", getValue().stamp(NodeView.DEFAULT));
|
43972
|
82 |
return super.verify();
|
|
83 |
}
|
|
84 |
|
|
85 |
@Override
|
|
86 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
|
46371
|
87 |
return canonicalized(this, forValue);
|
|
88 |
}
|
46344
|
89 |
|
46371
|
90 |
private static LogicNode canonicalized(IsNullNode isNullNode, ValueNode forValue) {
|
|
91 |
IsNullNode self = isNullNode;
|
43972
|
92 |
LogicNode result = tryCanonicalize(forValue);
|
46344
|
93 |
if (result != null) {
|
|
94 |
return result;
|
|
95 |
}
|
|
96 |
|
|
97 |
if (forValue instanceof PiNode) {
|
|
98 |
return IsNullNode.create(GraphUtil.skipPi(forValue));
|
|
99 |
}
|
|
100 |
|
|
101 |
if (forValue instanceof ConvertNode) {
|
|
102 |
ConvertNode convertNode = (ConvertNode) forValue;
|
|
103 |
if (convertNode.mayNullCheckSkipConversion()) {
|
|
104 |
return IsNullNode.create(convertNode.getValue());
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
46371
|
108 |
if (self == null) {
|
|
109 |
self = new IsNullNode(GraphUtil.skipPi(forValue));
|
|
110 |
}
|
|
111 |
return self;
|
43972
|
112 |
}
|
|
113 |
|
|
114 |
@Override
|
|
115 |
public void virtualize(VirtualizerTool tool) {
|
|
116 |
ValueNode alias = tool.getAlias(getValue());
|
48190
|
117 |
TriState fold = tryFold(alias.stamp(NodeView.DEFAULT));
|
43972
|
118 |
if (fold != TriState.UNKNOWN) {
|
|
119 |
tool.replaceWithValue(LogicConstantNode.forBoolean(fold.isTrue(), graph()));
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
@Override
|
|
124 |
public Stamp getSucceedingStampForValue(boolean negated) {
|
47798
|
125 |
// Ignore any more precise input stamp since canonicalization will skip through PiNodes
|
48190
|
126 |
AbstractPointerStamp pointerStamp = (AbstractPointerStamp) getValue().stamp(NodeView.DEFAULT).unrestricted();
|
46344
|
127 |
return negated ? pointerStamp.asNonNull() : pointerStamp.asAlwaysNull();
|
43972
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
public TriState tryFold(Stamp valueStamp) {
|
|
132 |
if (valueStamp instanceof ObjectStamp) {
|
|
133 |
ObjectStamp objectStamp = (ObjectStamp) valueStamp;
|
|
134 |
if (objectStamp.alwaysNull()) {
|
|
135 |
return TriState.TRUE;
|
|
136 |
} else if (objectStamp.nonNull()) {
|
|
137 |
return TriState.FALSE;
|
|
138 |
}
|
|
139 |
}
|
|
140 |
return TriState.UNKNOWN;
|
|
141 |
}
|
|
142 |
}
|