43972
|
1 |
/*
|
46344
|
2 |
* Copyright (c) 2014, 2017, 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 |
*/
|
|
23 |
package org.graalvm.compiler.phases.common;
|
|
24 |
|
46344
|
25 |
import static org.graalvm.compiler.core.common.GraalOptions.OptImplicitNullChecks;
|
|
26 |
|
43972
|
27 |
import java.util.List;
|
|
28 |
|
46640
|
29 |
import org.graalvm.compiler.debug.CounterKey;
|
|
30 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
31 |
import org.graalvm.compiler.graph.Node;
|
|
32 |
import org.graalvm.compiler.nodeinfo.InputType;
|
|
33 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
34 |
import org.graalvm.compiler.nodes.AbstractDeoptimizeNode;
|
|
35 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
36 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
37 |
import org.graalvm.compiler.nodes.BeginNode;
|
46762
|
38 |
import org.graalvm.compiler.nodes.CompressionNode;
|
43972
|
39 |
import org.graalvm.compiler.nodes.DeoptimizeNode;
|
46344
|
40 |
import org.graalvm.compiler.nodes.DeoptimizingFixedWithNextNode;
|
43972
|
41 |
import org.graalvm.compiler.nodes.DynamicDeoptimizeNode;
|
|
42 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
43 |
import org.graalvm.compiler.nodes.IfNode;
|
|
44 |
import org.graalvm.compiler.nodes.LogicNode;
|
|
45 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
46 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
47 |
import org.graalvm.compiler.nodes.ValuePhiNode;
|
|
48 |
import org.graalvm.compiler.nodes.calc.IsNullNode;
|
|
49 |
import org.graalvm.compiler.nodes.extended.NullCheckNode;
|
46344
|
50 |
import org.graalvm.compiler.nodes.memory.FixedAccessNode;
|
|
51 |
import org.graalvm.compiler.nodes.memory.address.AddressNode;
|
43972
|
52 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
48861
|
53 |
import org.graalvm.compiler.options.Option;
|
|
54 |
import org.graalvm.compiler.options.OptionKey;
|
|
55 |
import org.graalvm.compiler.options.OptionType;
|
43972
|
56 |
import org.graalvm.compiler.phases.BasePhase;
|
|
57 |
import org.graalvm.compiler.phases.tiers.LowTierContext;
|
|
58 |
|
|
59 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
|
60 |
import jdk.vm.ci.meta.JavaConstant;
|
|
61 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
62 |
|
|
63 |
public class UseTrappingNullChecksPhase extends BasePhase<LowTierContext> {
|
|
64 |
|
46640
|
65 |
private static final CounterKey counterTrappingNullCheck = DebugContext.counter("TrappingNullCheck");
|
|
66 |
private static final CounterKey counterTrappingNullCheckExistingRead = DebugContext.counter("TrappingNullCheckExistingRead");
|
|
67 |
private static final CounterKey counterTrappingNullCheckUnreached = DebugContext.counter("TrappingNullCheckUnreached");
|
|
68 |
private static final CounterKey counterTrappingNullCheckDynamicDeoptimize = DebugContext.counter("TrappingNullCheckDynamicDeoptimize");
|
43972
|
69 |
|
48861
|
70 |
public static class Options {
|
|
71 |
|
|
72 |
// @formatter:off
|
|
73 |
@Option(help = "Use traps for null checks instead of explicit null-checks", type = OptionType.Expert)
|
|
74 |
public static final OptionKey<Boolean> UseTrappingNullChecks = new OptionKey<>(true);
|
|
75 |
// @formatter:on
|
|
76 |
}
|
|
77 |
|
43972
|
78 |
@Override
|
|
79 |
protected void run(StructuredGraph graph, LowTierContext context) {
|
48861
|
80 |
if (!Options.UseTrappingNullChecks.getValue(graph.getOptions()) || context.getTarget().implicitNullCheckLimit <= 0) {
|
43972
|
81 |
return;
|
|
82 |
}
|
|
83 |
assert graph.getGuardsStage().areFrameStatesAtDeopts();
|
|
84 |
|
46344
|
85 |
long implicitNullCheckLimit = context.getTarget().implicitNullCheckLimit;
|
43972
|
86 |
for (DeoptimizeNode deopt : graph.getNodes(DeoptimizeNode.TYPE)) {
|
48861
|
87 |
tryUseTrappingNullCheck(deopt, deopt.predecessor(), deopt.getReason(), deopt.getSpeculation(), implicitNullCheckLimit);
|
43972
|
88 |
}
|
|
89 |
for (DynamicDeoptimizeNode deopt : graph.getNodes(DynamicDeoptimizeNode.TYPE)) {
|
46344
|
90 |
tryUseTrappingNullCheck(context.getMetaAccess(), deopt, implicitNullCheckLimit);
|
43972
|
91 |
}
|
46344
|
92 |
|
43972
|
93 |
}
|
|
94 |
|
46344
|
95 |
private static void tryUseTrappingNullCheck(MetaAccessProvider metaAccessProvider, DynamicDeoptimizeNode deopt, long implicitNullCheckLimit) {
|
43972
|
96 |
Node predecessor = deopt.predecessor();
|
|
97 |
if (predecessor instanceof AbstractMergeNode) {
|
|
98 |
AbstractMergeNode merge = (AbstractMergeNode) predecessor;
|
|
99 |
|
|
100 |
// Process each predecessor at the merge, unpacking the reasons and speculations as
|
|
101 |
// needed.
|
|
102 |
ValueNode reason = deopt.getActionAndReason();
|
|
103 |
ValuePhiNode reasonPhi = null;
|
|
104 |
List<ValueNode> reasons = null;
|
|
105 |
int expectedPhis = 0;
|
|
106 |
|
|
107 |
if (reason instanceof ValuePhiNode) {
|
|
108 |
reasonPhi = (ValuePhiNode) reason;
|
|
109 |
if (reasonPhi.merge() != merge) {
|
|
110 |
return;
|
|
111 |
}
|
|
112 |
reasons = reasonPhi.values().snapshot();
|
|
113 |
expectedPhis++;
|
|
114 |
} else if (!reason.isConstant()) {
|
|
115 |
return;
|
|
116 |
}
|
|
117 |
|
|
118 |
ValueNode speculation = deopt.getSpeculation();
|
|
119 |
ValuePhiNode speculationPhi = null;
|
|
120 |
List<ValueNode> speculations = null;
|
|
121 |
if (speculation instanceof ValuePhiNode) {
|
|
122 |
speculationPhi = (ValuePhiNode) speculation;
|
|
123 |
if (speculationPhi.merge() != merge) {
|
|
124 |
return;
|
|
125 |
}
|
|
126 |
speculations = speculationPhi.values().snapshot();
|
|
127 |
expectedPhis++;
|
|
128 |
}
|
|
129 |
|
|
130 |
if (merge.phis().count() != expectedPhis) {
|
|
131 |
return;
|
|
132 |
}
|
|
133 |
|
|
134 |
int index = 0;
|
|
135 |
for (AbstractEndNode end : merge.cfgPredecessors().snapshot()) {
|
|
136 |
ValueNode thisReason = reasons != null ? reasons.get(index) : reason;
|
|
137 |
ValueNode thisSpeculation = speculations != null ? speculations.get(index++) : speculation;
|
|
138 |
if (!thisReason.isConstant() || !thisSpeculation.isConstant() || !thisSpeculation.asConstant().equals(JavaConstant.NULL_POINTER)) {
|
|
139 |
continue;
|
|
140 |
}
|
|
141 |
DeoptimizationReason deoptimizationReason = metaAccessProvider.decodeDeoptReason(thisReason.asJavaConstant());
|
46344
|
142 |
tryUseTrappingNullCheck(deopt, end.predecessor(), deoptimizationReason, null, implicitNullCheckLimit);
|
43972
|
143 |
}
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
46344
|
147 |
private static void tryUseTrappingNullCheck(AbstractDeoptimizeNode deopt, Node predecessor, DeoptimizationReason deoptimizationReason, JavaConstant speculation, long implicitNullCheckLimit) {
|
43972
|
148 |
if (deoptimizationReason != DeoptimizationReason.NullCheckException && deoptimizationReason != DeoptimizationReason.UnreachedCode) {
|
|
149 |
return;
|
|
150 |
}
|
|
151 |
if (speculation != null && !speculation.equals(JavaConstant.NULL_POINTER)) {
|
|
152 |
return;
|
|
153 |
}
|
|
154 |
if (predecessor instanceof AbstractMergeNode) {
|
|
155 |
AbstractMergeNode merge = (AbstractMergeNode) predecessor;
|
|
156 |
if (merge.phis().isEmpty()) {
|
|
157 |
for (AbstractEndNode end : merge.cfgPredecessors().snapshot()) {
|
46344
|
158 |
checkPredecessor(deopt, end.predecessor(), deoptimizationReason, implicitNullCheckLimit);
|
43972
|
159 |
}
|
|
160 |
}
|
|
161 |
} else if (predecessor instanceof AbstractBeginNode) {
|
46344
|
162 |
checkPredecessor(deopt, predecessor, deoptimizationReason, implicitNullCheckLimit);
|
43972
|
163 |
}
|
|
164 |
}
|
|
165 |
|
46344
|
166 |
private static void checkPredecessor(AbstractDeoptimizeNode deopt, Node predecessor, DeoptimizationReason deoptimizationReason, long implicitNullCheckLimit) {
|
43972
|
167 |
Node current = predecessor;
|
|
168 |
AbstractBeginNode branch = null;
|
|
169 |
while (current instanceof AbstractBeginNode) {
|
|
170 |
branch = (AbstractBeginNode) current;
|
|
171 |
if (branch.anchored().isNotEmpty()) {
|
|
172 |
// some input of the deopt framestate is anchored to this branch
|
|
173 |
return;
|
|
174 |
}
|
|
175 |
current = current.predecessor();
|
|
176 |
}
|
|
177 |
if (current instanceof IfNode) {
|
|
178 |
IfNode ifNode = (IfNode) current;
|
|
179 |
if (branch != ifNode.trueSuccessor()) {
|
|
180 |
return;
|
|
181 |
}
|
|
182 |
LogicNode condition = ifNode.condition();
|
|
183 |
if (condition instanceof IsNullNode) {
|
46344
|
184 |
replaceWithTrappingNullCheck(deopt, ifNode, condition, deoptimizationReason, implicitNullCheckLimit);
|
43972
|
185 |
}
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
46344
|
189 |
private static void replaceWithTrappingNullCheck(AbstractDeoptimizeNode deopt, IfNode ifNode, LogicNode condition, DeoptimizationReason deoptimizationReason, long implicitNullCheckLimit) {
|
46640
|
190 |
DebugContext debug = deopt.getDebug();
|
|
191 |
counterTrappingNullCheck.increment(debug);
|
43972
|
192 |
if (deopt instanceof DynamicDeoptimizeNode) {
|
46640
|
193 |
counterTrappingNullCheckDynamicDeoptimize.increment(debug);
|
43972
|
194 |
}
|
|
195 |
if (deoptimizationReason == DeoptimizationReason.UnreachedCode) {
|
46640
|
196 |
counterTrappingNullCheckUnreached.increment(debug);
|
43972
|
197 |
}
|
|
198 |
IsNullNode isNullNode = (IsNullNode) condition;
|
|
199 |
AbstractBeginNode nonTrappingContinuation = ifNode.falseSuccessor();
|
|
200 |
AbstractBeginNode trappingContinuation = ifNode.trueSuccessor();
|
46344
|
201 |
|
|
202 |
DeoptimizingFixedWithNextNode trappingNullCheck = null;
|
|
203 |
FixedNode nextNonTrapping = nonTrappingContinuation.next();
|
|
204 |
ValueNode value = isNullNode.getValue();
|
|
205 |
if (OptImplicitNullChecks.getValue(ifNode.graph().getOptions()) && implicitNullCheckLimit > 0) {
|
|
206 |
if (nextNonTrapping instanceof FixedAccessNode) {
|
|
207 |
FixedAccessNode fixedAccessNode = (FixedAccessNode) nextNonTrapping;
|
|
208 |
if (fixedAccessNode.canNullCheck()) {
|
|
209 |
AddressNode address = fixedAccessNode.getAddress();
|
|
210 |
ValueNode base = address.getBase();
|
|
211 |
ValueNode index = address.getIndex();
|
46762
|
212 |
// allow for architectures which cannot fold an
|
|
213 |
// intervening uncompress out of the address chain
|
|
214 |
if (base != null && base instanceof CompressionNode) {
|
|
215 |
base = ((CompressionNode) base).getValue();
|
|
216 |
}
|
|
217 |
if (index != null && index instanceof CompressionNode) {
|
|
218 |
index = ((CompressionNode) index).getValue();
|
|
219 |
}
|
46344
|
220 |
if (((base == value && index == null) || (base == null && index == value)) && address.getMaxConstantDisplacement() < implicitNullCheckLimit) {
|
|
221 |
// Opportunity for implicit null check as part of an existing read found!
|
|
222 |
fixedAccessNode.setStateBefore(deopt.stateBefore());
|
|
223 |
fixedAccessNode.setNullCheck(true);
|
|
224 |
deopt.graph().removeSplit(ifNode, nonTrappingContinuation);
|
|
225 |
trappingNullCheck = fixedAccessNode;
|
46640
|
226 |
counterTrappingNullCheckExistingRead.increment(debug);
|
46344
|
227 |
}
|
|
228 |
}
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
if (trappingNullCheck == null) {
|
|
233 |
// Need to add a null check node.
|
|
234 |
trappingNullCheck = deopt.graph().add(new NullCheckNode(value));
|
|
235 |
deopt.graph().replaceSplit(ifNode, trappingNullCheck, nonTrappingContinuation);
|
|
236 |
}
|
|
237 |
|
43972
|
238 |
trappingNullCheck.setStateBefore(deopt.stateBefore());
|
|
239 |
|
|
240 |
/*
|
|
241 |
* We now have the pattern NullCheck/BeginNode/... It's possible some node is using the
|
|
242 |
* BeginNode as a guard input, so replace guard users of the Begin with the NullCheck and
|
|
243 |
* then remove the Begin from the graph.
|
|
244 |
*/
|
|
245 |
nonTrappingContinuation.replaceAtUsages(InputType.Guard, trappingNullCheck);
|
46344
|
246 |
|
43972
|
247 |
if (nonTrappingContinuation instanceof BeginNode) {
|
46344
|
248 |
GraphUtil.unlinkFixedNode(nonTrappingContinuation);
|
43972
|
249 |
nonTrappingContinuation.safeDelete();
|
|
250 |
}
|
|
251 |
|
|
252 |
GraphUtil.killCFG(trappingContinuation);
|
|
253 |
GraphUtil.tryKillUnused(isNullNode);
|
|
254 |
}
|
|
255 |
}
|