43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 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.loop;
|
|
24 |
|
|
25 |
import static org.graalvm.compiler.loop.MathUtil.add;
|
|
26 |
import static org.graalvm.compiler.loop.MathUtil.divBefore;
|
|
27 |
import static org.graalvm.compiler.loop.MathUtil.sub;
|
|
28 |
|
|
29 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
30 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
31 |
import org.graalvm.compiler.loop.InductionVariable.Direction;
|
|
32 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
33 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
34 |
import org.graalvm.compiler.nodes.GuardNode;
|
46640
|
35 |
import org.graalvm.compiler.nodes.IfNode;
|
48190
|
36 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
37 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
38 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
39 |
import org.graalvm.compiler.nodes.calc.CompareNode;
|
|
40 |
import org.graalvm.compiler.nodes.calc.ConditionalNode;
|
|
41 |
import org.graalvm.compiler.nodes.calc.IntegerLessThanNode;
|
|
42 |
import org.graalvm.compiler.nodes.extended.GuardingNode;
|
|
43 |
|
|
44 |
import jdk.vm.ci.code.CodeUtil;
|
|
45 |
import jdk.vm.ci.meta.DeoptimizationAction;
|
|
46 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
|
47 |
import jdk.vm.ci.meta.JavaConstant;
|
|
48 |
|
|
49 |
public class CountedLoopInfo {
|
|
50 |
|
|
51 |
private final LoopEx loop;
|
|
52 |
private InductionVariable iv;
|
|
53 |
private ValueNode end;
|
|
54 |
private boolean oneOff;
|
|
55 |
private AbstractBeginNode body;
|
46640
|
56 |
private IfNode ifNode;
|
43972
|
57 |
|
46640
|
58 |
CountedLoopInfo(LoopEx loop, InductionVariable iv, IfNode ifNode, ValueNode end, boolean oneOff, AbstractBeginNode body) {
|
43972
|
59 |
this.loop = loop;
|
|
60 |
this.iv = iv;
|
|
61 |
this.end = end;
|
|
62 |
this.oneOff = oneOff;
|
|
63 |
this.body = body;
|
46640
|
64 |
this.ifNode = ifNode;
|
43972
|
65 |
}
|
|
66 |
|
|
67 |
public ValueNode maxTripCountNode() {
|
|
68 |
return maxTripCountNode(false);
|
|
69 |
}
|
|
70 |
|
|
71 |
public ValueNode maxTripCountNode(boolean assumePositive) {
|
|
72 |
StructuredGraph graph = iv.valueNode().graph();
|
48190
|
73 |
Stamp stamp = iv.valueNode().stamp(NodeView.DEFAULT);
|
43972
|
74 |
ValueNode range = sub(graph, end, iv.initNode());
|
|
75 |
|
48861
|
76 |
ValueNode max;
|
|
77 |
ValueNode min;
|
43972
|
78 |
ValueNode oneDirection;
|
|
79 |
if (iv.direction() == Direction.Up) {
|
|
80 |
oneDirection = ConstantNode.forIntegerStamp(stamp, 1, graph);
|
48861
|
81 |
max = end;
|
|
82 |
min = iv.initNode();
|
43972
|
83 |
} else {
|
|
84 |
assert iv.direction() == Direction.Down;
|
|
85 |
oneDirection = ConstantNode.forIntegerStamp(stamp, -1, graph);
|
48861
|
86 |
max = iv.initNode();
|
|
87 |
min = end;
|
43972
|
88 |
}
|
|
89 |
if (oneOff) {
|
|
90 |
range = add(graph, range, oneDirection);
|
|
91 |
}
|
|
92 |
// round-away-from-zero divison: (range + stride -/+ 1) / stride
|
47798
|
93 |
ValueNode denominator = range;
|
48190
|
94 |
if (!oneDirection.stamp(NodeView.DEFAULT).equals(iv.strideNode().stamp(NodeView.DEFAULT))) {
|
47798
|
95 |
ValueNode subedRanged = sub(graph, range, oneDirection);
|
|
96 |
denominator = add(graph, subedRanged, iv.strideNode());
|
|
97 |
}
|
43972
|
98 |
ValueNode div = divBefore(graph, loop.entryPoint(), denominator, iv.strideNode());
|
|
99 |
|
|
100 |
if (assumePositive) {
|
|
101 |
return div;
|
|
102 |
}
|
|
103 |
ConstantNode zero = ConstantNode.forIntegerStamp(stamp, 0, graph);
|
48861
|
104 |
return graph.unique(new ConditionalNode(graph.unique(new IntegerLessThanNode(max, min)), zero, div));
|
43972
|
105 |
}
|
|
106 |
|
46459
|
107 |
/**
|
|
108 |
* @return true if the loop has constant bounds and the trip count is representable as a
|
|
109 |
* positive integer.
|
|
110 |
*/
|
43972
|
111 |
public boolean isConstantMaxTripCount() {
|
46459
|
112 |
/*
|
|
113 |
* It's possible that the iteration range is too large to treat this as constant because it
|
|
114 |
* will overflow.
|
|
115 |
*/
|
|
116 |
return (hasConstantBounds() && rawConstantMaxTripCount() >= 0);
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* @return true if the bounds on the iteration space are all constants.
|
|
121 |
*/
|
|
122 |
public boolean hasConstantBounds() {
|
43972
|
123 |
return end instanceof ConstantNode && iv.isConstantInit() && iv.isConstantStride();
|
|
124 |
}
|
|
125 |
|
|
126 |
public long constantMaxTripCount() {
|
46459
|
127 |
assert isConstantMaxTripCount();
|
|
128 |
return rawConstantMaxTripCount();
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Compute the raw value of the trip count for this loop. Since we don't have unsigned values
|
|
133 |
* this may be outside representable positive values.
|
|
134 |
*/
|
|
135 |
protected long rawConstantMaxTripCount() {
|
43972
|
136 |
assert iv.direction() != null;
|
|
137 |
long off = oneOff ? iv.direction() == Direction.Up ? 1 : -1 : 0;
|
46459
|
138 |
long endValue = ((ConstantNode) end).asJavaConstant().asLong();
|
|
139 |
try {
|
|
140 |
// If no overflow occurs then negative values represent a trip count of 0
|
|
141 |
long max = Math.subtractExact(Math.addExact(endValue, off), iv.constantInit()) / iv.constantStride();
|
|
142 |
return Math.max(0, max);
|
|
143 |
} catch (ArithmeticException e) {
|
|
144 |
/*
|
|
145 |
* The computation overflowed to return a negative value. It's possible some
|
|
146 |
* optimization could handle this value as an unsigned and produce the right answer but
|
|
147 |
* we hide this value by default.
|
|
148 |
*/
|
|
149 |
return -1;
|
|
150 |
}
|
43972
|
151 |
}
|
|
152 |
|
|
153 |
public boolean isExactTripCount() {
|
|
154 |
return loop.loopBegin().loopExits().count() == 1;
|
|
155 |
}
|
|
156 |
|
|
157 |
public ValueNode exactTripCountNode() {
|
|
158 |
assert isExactTripCount();
|
|
159 |
return maxTripCountNode();
|
|
160 |
}
|
|
161 |
|
|
162 |
public boolean isConstantExactTripCount() {
|
|
163 |
assert isExactTripCount();
|
|
164 |
return isConstantMaxTripCount();
|
|
165 |
}
|
|
166 |
|
|
167 |
public long constantExactTripCount() {
|
|
168 |
assert isExactTripCount();
|
|
169 |
return constantMaxTripCount();
|
|
170 |
}
|
|
171 |
|
|
172 |
@Override
|
|
173 |
public String toString() {
|
|
174 |
return "iv=" + iv + " until " + end + (oneOff ? iv.direction() == Direction.Up ? "+1" : "-1" : "");
|
|
175 |
}
|
|
176 |
|
|
177 |
public ValueNode getLimit() {
|
|
178 |
return end;
|
|
179 |
}
|
|
180 |
|
46640
|
181 |
public IfNode getLimitTest() {
|
|
182 |
return ifNode;
|
|
183 |
}
|
|
184 |
|
43972
|
185 |
public ValueNode getStart() {
|
|
186 |
return iv.initNode();
|
|
187 |
}
|
|
188 |
|
|
189 |
public boolean isLimitIncluded() {
|
|
190 |
return oneOff;
|
|
191 |
}
|
|
192 |
|
|
193 |
public AbstractBeginNode getBody() {
|
|
194 |
return body;
|
|
195 |
}
|
|
196 |
|
|
197 |
public Direction getDirection() {
|
|
198 |
return iv.direction();
|
|
199 |
}
|
|
200 |
|
|
201 |
public InductionVariable getCounter() {
|
|
202 |
return iv;
|
|
203 |
}
|
|
204 |
|
|
205 |
public GuardingNode getOverFlowGuard() {
|
|
206 |
return loop.loopBegin().getOverflowGuard();
|
|
207 |
}
|
|
208 |
|
|
209 |
public GuardingNode createOverFlowGuard() {
|
|
210 |
GuardingNode overflowGuard = getOverFlowGuard();
|
|
211 |
if (overflowGuard != null) {
|
|
212 |
return overflowGuard;
|
|
213 |
}
|
48190
|
214 |
IntegerStamp stamp = (IntegerStamp) iv.valueNode().stamp(NodeView.DEFAULT);
|
43972
|
215 |
StructuredGraph graph = iv.valueNode().graph();
|
|
216 |
CompareNode cond; // we use a negated guard with a < condition to achieve a >=
|
|
217 |
ConstantNode one = ConstantNode.forIntegerStamp(stamp, 1, graph);
|
|
218 |
if (iv.direction() == Direction.Up) {
|
|
219 |
ValueNode v1 = sub(graph, ConstantNode.forIntegerStamp(stamp, CodeUtil.maxValue(stamp.getBits()), graph), sub(graph, iv.strideNode(), one));
|
|
220 |
if (oneOff) {
|
|
221 |
v1 = sub(graph, v1, one);
|
|
222 |
}
|
|
223 |
cond = graph.unique(new IntegerLessThanNode(v1, end));
|
|
224 |
} else {
|
|
225 |
assert iv.direction() == Direction.Down;
|
|
226 |
ValueNode v1 = add(graph, ConstantNode.forIntegerStamp(stamp, CodeUtil.minValue(stamp.getBits()), graph), sub(graph, one, iv.strideNode()));
|
|
227 |
if (oneOff) {
|
|
228 |
v1 = add(graph, v1, one);
|
|
229 |
}
|
|
230 |
cond = graph.unique(new IntegerLessThanNode(end, v1));
|
|
231 |
}
|
|
232 |
assert graph.getGuardsStage().allowsFloatingGuards();
|
|
233 |
overflowGuard = graph.unique(new GuardNode(cond, AbstractBeginNode.prevBegin(loop.entryPoint()), DeoptimizationReason.LoopLimitCheck, DeoptimizationAction.InvalidateRecompile, true,
|
|
234 |
JavaConstant.NULL_POINTER)); // TODO gd: use speculation
|
|
235 |
loop.loopBegin().setOverflowGuard(overflowGuard);
|
|
236 |
return overflowGuard;
|
|
237 |
}
|
|
238 |
|
|
239 |
public IntegerStamp getStamp() {
|
48190
|
240 |
return (IntegerStamp) iv.valueNode().stamp(NodeView.DEFAULT);
|
43972
|
241 |
}
|
|
242 |
}
|