43972
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2012, 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.loop;
|
|
26 |
|
54084
|
27 |
import static java.lang.Math.abs;
|
49451
|
28 |
import static org.graalvm.compiler.loop.MathUtil.unsignedDivBefore;
|
54084
|
29 |
import static org.graalvm.compiler.nodes.calc.BinaryArithmeticNode.add;
|
|
30 |
import static org.graalvm.compiler.nodes.calc.BinaryArithmeticNode.sub;
|
43972
|
31 |
|
|
32 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
33 |
import org.graalvm.compiler.core.common.type.Stamp;
|
49451
|
34 |
import org.graalvm.compiler.core.common.util.UnsignedLong;
|
49873
|
35 |
import org.graalvm.compiler.debug.DebugCloseable;
|
43972
|
36 |
import org.graalvm.compiler.loop.InductionVariable.Direction;
|
|
37 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
38 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
39 |
import org.graalvm.compiler.nodes.GuardNode;
|
46640
|
40 |
import org.graalvm.compiler.nodes.IfNode;
|
54084
|
41 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
42 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
43 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
44 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
45 |
import org.graalvm.compiler.nodes.calc.ConditionalNode;
|
49451
|
46 |
import org.graalvm.compiler.nodes.calc.NegateNode;
|
43972
|
47 |
import org.graalvm.compiler.nodes.extended.GuardingNode;
|
54084
|
48 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
58299
|
49 |
import org.graalvm.compiler.nodes.util.IntegerHelper;
|
|
50 |
import org.graalvm.compiler.nodes.util.SignedIntegerHelper;
|
|
51 |
import org.graalvm.compiler.nodes.util.UnsignedIntegerHelper;
|
43972
|
52 |
|
|
53 |
import jdk.vm.ci.meta.DeoptimizationAction;
|
|
54 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
50858
|
55 |
import jdk.vm.ci.meta.SpeculationLog;
|
43972
|
56 |
|
|
57 |
public class CountedLoopInfo {
|
|
58 |
|
|
59 |
private final LoopEx loop;
|
|
60 |
private InductionVariable iv;
|
|
61 |
private ValueNode end;
|
|
62 |
private boolean oneOff;
|
|
63 |
private AbstractBeginNode body;
|
46640
|
64 |
private IfNode ifNode;
|
58299
|
65 |
private final boolean unsigned;
|
43972
|
66 |
|
58299
|
67 |
CountedLoopInfo(LoopEx loop, InductionVariable iv, IfNode ifNode, ValueNode end, boolean oneOff, AbstractBeginNode body, boolean unsigned) {
|
52578
|
68 |
assert iv.direction() != null;
|
43972
|
69 |
this.loop = loop;
|
|
70 |
this.iv = iv;
|
|
71 |
this.end = end;
|
|
72 |
this.oneOff = oneOff;
|
|
73 |
this.body = body;
|
46640
|
74 |
this.ifNode = ifNode;
|
58299
|
75 |
this.unsigned = unsigned;
|
43972
|
76 |
}
|
|
77 |
|
49451
|
78 |
/**
|
|
79 |
* Returns a node that computes the maximum trip count of this loop. That is the trip count of
|
|
80 |
* this loop assuming it is not exited by an other exit than the {@linkplain #getLimitTest()
|
|
81 |
* count check}.
|
|
82 |
*
|
|
83 |
* This count is exact if {@link #isExactTripCount()} returns true.
|
|
84 |
*
|
|
85 |
* THIS VALUE SHOULD BE TREATED AS UNSIGNED.
|
|
86 |
*/
|
43972
|
87 |
public ValueNode maxTripCountNode() {
|
|
88 |
return maxTripCountNode(false);
|
|
89 |
}
|
|
90 |
|
58299
|
91 |
public boolean isUnsignedCheck() {
|
|
92 |
return this.unsigned;
|
|
93 |
}
|
|
94 |
|
49451
|
95 |
/**
|
|
96 |
* Returns a node that computes the maximum trip count of this loop. That is the trip count of
|
|
97 |
* this loop assuming it is not exited by an other exit than the {@linkplain #getLimitTest()
|
|
98 |
* count check}.
|
|
99 |
*
|
|
100 |
* This count is exact if {@link #isExactTripCount()} returns true.
|
|
101 |
*
|
|
102 |
* THIS VALUE SHOULD BE TREATED AS UNSIGNED.
|
|
103 |
*
|
54084
|
104 |
* @param assumeLoopEntered if true the check that the loop is entered at all will be omitted.
|
49451
|
105 |
*/
|
54084
|
106 |
public ValueNode maxTripCountNode(boolean assumeLoopEntered) {
|
43972
|
107 |
StructuredGraph graph = iv.valueNode().graph();
|
48190
|
108 |
Stamp stamp = iv.valueNode().stamp(NodeView.DEFAULT);
|
43972
|
109 |
|
48861
|
110 |
ValueNode max;
|
|
111 |
ValueNode min;
|
49451
|
112 |
ValueNode absStride;
|
43972
|
113 |
if (iv.direction() == Direction.Up) {
|
49451
|
114 |
absStride = iv.strideNode();
|
48861
|
115 |
max = end;
|
|
116 |
min = iv.initNode();
|
43972
|
117 |
} else {
|
|
118 |
assert iv.direction() == Direction.Down;
|
54084
|
119 |
absStride = NegateNode.create(iv.strideNode(), NodeView.DEFAULT);
|
48861
|
120 |
max = iv.initNode();
|
|
121 |
min = end;
|
43972
|
122 |
}
|
54084
|
123 |
ValueNode range = sub(max, min);
|
49451
|
124 |
|
58877
|
125 |
ConstantNode one = ConstantNode.forIntegerStamp(stamp, 1, graph);
|
43972
|
126 |
if (oneOff) {
|
54084
|
127 |
range = add(range, one);
|
43972
|
128 |
}
|
|
129 |
// round-away-from-zero divison: (range + stride -/+ 1) / stride
|
58877
|
130 |
ValueNode denominator = add(graph, range, sub(absStride, one), NodeView.DEFAULT);
|
50330
|
131 |
ValueNode div = unsignedDivBefore(graph, loop.entryPoint(), denominator, absStride, null);
|
43972
|
132 |
|
54084
|
133 |
if (assumeLoopEntered) {
|
|
134 |
return graph.addOrUniqueWithInputs(div);
|
43972
|
135 |
}
|
58877
|
136 |
ConstantNode zero = ConstantNode.forIntegerStamp(stamp, 0, graph);
|
58299
|
137 |
// This check is "wide": it looks like min <= max
|
|
138 |
// That's OK even if the loop is strict (`!isLimitIncluded()`)
|
|
139 |
// because in this case, `div` will be zero when min == max
|
|
140 |
LogicNode noEntryCheck = getCounterIntegerHelper().createCompareNode(max, min, NodeView.DEFAULT);
|
54084
|
141 |
return graph.addOrUniqueWithInputs(ConditionalNode.create(noEntryCheck, zero, div, NodeView.DEFAULT));
|
43972
|
142 |
}
|
|
143 |
|
46459
|
144 |
/**
|
58877
|
145 |
* Determine if the loop might be entered. Returns {@code false} if we can tell statically that
|
|
146 |
* the loop cannot be entered; returns {@code true} if the loop might possibly be entered,
|
|
147 |
* including in the case where we cannot be sure statically.
|
|
148 |
*
|
|
149 |
* @return false if the loop can definitely not be entered, true otherwise
|
|
150 |
*/
|
|
151 |
public boolean loopMightBeEntered() {
|
|
152 |
Stamp stamp = iv.valueNode().stamp(NodeView.DEFAULT);
|
|
153 |
|
|
154 |
ValueNode max;
|
|
155 |
ValueNode min;
|
|
156 |
if (iv.direction() == Direction.Up) {
|
|
157 |
max = end;
|
|
158 |
min = iv.initNode();
|
|
159 |
} else {
|
|
160 |
assert iv.direction() == Direction.Down;
|
|
161 |
max = iv.initNode();
|
|
162 |
min = end;
|
|
163 |
}
|
|
164 |
if (oneOff) {
|
|
165 |
max = add(max, ConstantNode.forIntegerStamp(stamp, 1));
|
|
166 |
}
|
|
167 |
|
|
168 |
LogicNode entryCheck = getCounterIntegerHelper().createCompareNode(min, max, NodeView.DEFAULT);
|
|
169 |
if (entryCheck.isContradiction()) {
|
|
170 |
// We can definitely not enter this loop.
|
|
171 |
return false;
|
|
172 |
} else {
|
|
173 |
// We don't know for sure that the loop can't be entered, so assume it can.
|
|
174 |
return true;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
/**
|
49451
|
179 |
* @return true if the loop has constant bounds.
|
46459
|
180 |
*/
|
43972
|
181 |
public boolean isConstantMaxTripCount() {
|
|
182 |
return end instanceof ConstantNode && iv.isConstantInit() && iv.isConstantStride();
|
|
183 |
}
|
|
184 |
|
49451
|
185 |
public UnsignedLong constantMaxTripCount() {
|
46459
|
186 |
assert isConstantMaxTripCount();
|
49451
|
187 |
return new UnsignedLong(rawConstantMaxTripCount());
|
46459
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
49451
|
191 |
* Compute the raw value of the trip count for this loop. THIS IS AN UNSIGNED VALUE;
|
46459
|
192 |
*/
|
49451
|
193 |
private long rawConstantMaxTripCount() {
|
43972
|
194 |
assert iv.direction() != null;
|
49451
|
195 |
long endValue = end.asJavaConstant().asLong();
|
|
196 |
long initValue = iv.constantInit();
|
|
197 |
long range;
|
|
198 |
long absStride;
|
58299
|
199 |
IntegerHelper helper = getCounterIntegerHelper(64);
|
49451
|
200 |
if (iv.direction() == Direction.Up) {
|
58299
|
201 |
if (helper.compare(endValue, initValue) < 0) {
|
49451
|
202 |
return 0;
|
|
203 |
}
|
|
204 |
range = endValue - iv.constantInit();
|
|
205 |
absStride = iv.constantStride();
|
|
206 |
} else {
|
52578
|
207 |
assert iv.direction() == Direction.Down;
|
58299
|
208 |
if (helper.compare(initValue, endValue) < 0) {
|
49451
|
209 |
return 0;
|
|
210 |
}
|
|
211 |
range = iv.constantInit() - endValue;
|
|
212 |
absStride = -iv.constantStride();
|
46459
|
213 |
}
|
49451
|
214 |
if (oneOff) {
|
|
215 |
range += 1;
|
|
216 |
}
|
|
217 |
long denominator = range + absStride - 1;
|
|
218 |
return Long.divideUnsigned(denominator, absStride);
|
43972
|
219 |
}
|
|
220 |
|
58299
|
221 |
public IntegerHelper getCounterIntegerHelper() {
|
|
222 |
IntegerStamp stamp = (IntegerStamp) iv.valueNode().stamp(NodeView.DEFAULT);
|
|
223 |
return getCounterIntegerHelper(stamp.getBits());
|
|
224 |
}
|
|
225 |
|
|
226 |
public IntegerHelper getCounterIntegerHelper(int bits) {
|
|
227 |
IntegerHelper helper;
|
|
228 |
if (isUnsignedCheck()) {
|
|
229 |
helper = new UnsignedIntegerHelper(bits);
|
|
230 |
} else {
|
|
231 |
helper = new SignedIntegerHelper(bits);
|
|
232 |
}
|
|
233 |
return helper;
|
|
234 |
}
|
|
235 |
|
43972
|
236 |
public boolean isExactTripCount() {
|
54084
|
237 |
return loop.loop().getNaturalExits().size() == 1;
|
43972
|
238 |
}
|
|
239 |
|
|
240 |
public ValueNode exactTripCountNode() {
|
|
241 |
assert isExactTripCount();
|
|
242 |
return maxTripCountNode();
|
|
243 |
}
|
|
244 |
|
|
245 |
public boolean isConstantExactTripCount() {
|
|
246 |
assert isExactTripCount();
|
|
247 |
return isConstantMaxTripCount();
|
|
248 |
}
|
|
249 |
|
49451
|
250 |
public UnsignedLong constantExactTripCount() {
|
43972
|
251 |
assert isExactTripCount();
|
|
252 |
return constantMaxTripCount();
|
|
253 |
}
|
|
254 |
|
|
255 |
@Override
|
|
256 |
public String toString() {
|
|
257 |
return "iv=" + iv + " until " + end + (oneOff ? iv.direction() == Direction.Up ? "+1" : "-1" : "");
|
|
258 |
}
|
|
259 |
|
|
260 |
public ValueNode getLimit() {
|
|
261 |
return end;
|
|
262 |
}
|
|
263 |
|
46640
|
264 |
public IfNode getLimitTest() {
|
|
265 |
return ifNode;
|
|
266 |
}
|
|
267 |
|
43972
|
268 |
public ValueNode getStart() {
|
|
269 |
return iv.initNode();
|
|
270 |
}
|
|
271 |
|
|
272 |
public boolean isLimitIncluded() {
|
|
273 |
return oneOff;
|
|
274 |
}
|
|
275 |
|
|
276 |
public AbstractBeginNode getBody() {
|
|
277 |
return body;
|
|
278 |
}
|
|
279 |
|
54084
|
280 |
public AbstractBeginNode getCountedExit() {
|
|
281 |
if (getLimitTest().trueSuccessor() == getBody()) {
|
|
282 |
return getLimitTest().falseSuccessor();
|
|
283 |
} else {
|
|
284 |
assert getLimitTest().falseSuccessor() == getBody();
|
|
285 |
return getLimitTest().trueSuccessor();
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
43972
|
289 |
public Direction getDirection() {
|
|
290 |
return iv.direction();
|
|
291 |
}
|
|
292 |
|
|
293 |
public InductionVariable getCounter() {
|
|
294 |
return iv;
|
|
295 |
}
|
|
296 |
|
|
297 |
public GuardingNode getOverFlowGuard() {
|
|
298 |
return loop.loopBegin().getOverflowGuard();
|
|
299 |
}
|
|
300 |
|
54084
|
301 |
public boolean counterNeverOverflows() {
|
|
302 |
if (iv.isConstantStride() && abs(iv.constantStride()) == 1) {
|
|
303 |
return true;
|
|
304 |
}
|
|
305 |
IntegerStamp endStamp = (IntegerStamp) end.stamp(NodeView.DEFAULT);
|
|
306 |
ValueNode strideNode = iv.strideNode();
|
|
307 |
IntegerStamp strideStamp = (IntegerStamp) strideNode.stamp(NodeView.DEFAULT);
|
|
308 |
GraphUtil.tryKillUnused(strideNode);
|
58299
|
309 |
IntegerHelper integerHelper = getCounterIntegerHelper();
|
54084
|
310 |
if (getDirection() == Direction.Up) {
|
58299
|
311 |
long max = integerHelper.maxValue();
|
|
312 |
return integerHelper.compare(endStamp.upperBound(), max - (strideStamp.upperBound() - 1) - (oneOff ? 1 : 0)) <= 0;
|
54084
|
313 |
} else if (getDirection() == Direction.Down) {
|
58299
|
314 |
long min = integerHelper.minValue();
|
|
315 |
return integerHelper.compare(min + (1 - strideStamp.lowerBound()) + (oneOff ? 1 : 0), endStamp.lowerBound()) <= 0;
|
54084
|
316 |
}
|
|
317 |
return false;
|
|
318 |
}
|
|
319 |
|
49873
|
320 |
@SuppressWarnings("try")
|
43972
|
321 |
public GuardingNode createOverFlowGuard() {
|
|
322 |
GuardingNode overflowGuard = getOverFlowGuard();
|
54084
|
323 |
if (overflowGuard != null || counterNeverOverflows()) {
|
43972
|
324 |
return overflowGuard;
|
|
325 |
}
|
49873
|
326 |
try (DebugCloseable position = loop.loopBegin().withNodeSourcePosition()) {
|
|
327 |
IntegerStamp stamp = (IntegerStamp) iv.valueNode().stamp(NodeView.DEFAULT);
|
58299
|
328 |
IntegerHelper integerHelper = getCounterIntegerHelper();
|
49873
|
329 |
StructuredGraph graph = iv.valueNode().graph();
|
54084
|
330 |
LogicNode cond; // we use a negated guard with a < condition to achieve a >=
|
49873
|
331 |
ConstantNode one = ConstantNode.forIntegerStamp(stamp, 1, graph);
|
|
332 |
if (iv.direction() == Direction.Up) {
|
58299
|
333 |
ValueNode v1 = sub(ConstantNode.forIntegerStamp(stamp, integerHelper.maxValue()), sub(iv.strideNode(), one));
|
49873
|
334 |
if (oneOff) {
|
54084
|
335 |
v1 = sub(v1, one);
|
49873
|
336 |
}
|
58299
|
337 |
cond = graph.addOrUniqueWithInputs(integerHelper.createCompareNode(v1, end, NodeView.DEFAULT));
|
49873
|
338 |
} else {
|
|
339 |
assert iv.direction() == Direction.Down;
|
58299
|
340 |
ValueNode v1 = add(ConstantNode.forIntegerStamp(stamp, integerHelper.minValue()), sub(one, iv.strideNode()));
|
49873
|
341 |
if (oneOff) {
|
54084
|
342 |
v1 = add(v1, one);
|
49873
|
343 |
}
|
58299
|
344 |
cond = graph.addOrUniqueWithInputs(integerHelper.createCompareNode(end, v1, NodeView.DEFAULT));
|
43972
|
345 |
}
|
49873
|
346 |
assert graph.getGuardsStage().allowsFloatingGuards();
|
|
347 |
overflowGuard = graph.unique(new GuardNode(cond, AbstractBeginNode.prevBegin(loop.entryPoint()), DeoptimizationReason.LoopLimitCheck, DeoptimizationAction.InvalidateRecompile, true,
|
50858
|
348 |
SpeculationLog.NO_SPECULATION, null)); // TODO gd: use speculation
|
49873
|
349 |
loop.loopBegin().setOverflowGuard(overflowGuard);
|
|
350 |
return overflowGuard;
|
43972
|
351 |
}
|
|
352 |
}
|
|
353 |
|
|
354 |
public IntegerStamp getStamp() {
|
48190
|
355 |
return (IntegerStamp) iv.valueNode().stamp(NodeView.DEFAULT);
|
43972
|
356 |
}
|
|
357 |
}
|