43972
|
1 |
/*
|
58533
|
2 |
* Copyright (c) 2009, 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.nodes.calc;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
|
|
28 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
|
|
29 |
|
|
30 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
|
|
31 |
import org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp;
|
52578
|
32 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
43972
|
33 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
34 |
import org.graalvm.compiler.debug.GraalError;
|
|
35 |
import org.graalvm.compiler.graph.Graph;
|
|
36 |
import org.graalvm.compiler.graph.Node;
|
|
37 |
import org.graalvm.compiler.graph.NodeClass;
|
|
38 |
import org.graalvm.compiler.graph.iterators.NodePredicate;
|
|
39 |
import org.graalvm.compiler.graph.spi.Canonicalizable;
|
|
40 |
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
|
|
41 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
42 |
import org.graalvm.compiler.nodes.ArithmeticOperation;
|
|
43 |
import org.graalvm.compiler.nodes.ConstantNode;
|
48190
|
44 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
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.spi.ArithmeticLIRLowerable;
|
|
49 |
import org.graalvm.compiler.nodes.spi.NodeValueMap;
|
|
50 |
|
|
51 |
import jdk.vm.ci.meta.Constant;
|
|
52 |
|
|
53 |
@NodeInfo(cycles = CYCLES_1, size = SIZE_1)
|
|
54 |
public abstract class BinaryArithmeticNode<OP> extends BinaryNode implements ArithmeticOperation, ArithmeticLIRLowerable, Canonicalizable.Binary<ValueNode> {
|
|
55 |
|
|
56 |
@SuppressWarnings("rawtypes") public static final NodeClass<BinaryArithmeticNode> TYPE = NodeClass.create(BinaryArithmeticNode.class);
|
|
57 |
|
58533
|
58 |
protected BinaryArithmeticNode(NodeClass<? extends BinaryArithmeticNode<OP>> c, BinaryOp<OP> opForStampComputation, ValueNode x, ValueNode y) {
|
|
59 |
super(c, opForStampComputation.foldStamp(x.stamp(NodeView.DEFAULT), y.stamp(NodeView.DEFAULT)), x, y);
|
43972
|
60 |
}
|
|
61 |
|
58533
|
62 |
public static ArithmeticOpTable getArithmeticOpTable(ValueNode forValue) {
|
|
63 |
return ArithmeticOpTable.forStamp(forValue.stamp(NodeView.DEFAULT));
|
43972
|
64 |
}
|
|
65 |
|
58533
|
66 |
protected abstract BinaryOp<OP> getOp(ArithmeticOpTable table);
|
|
67 |
|
43972
|
68 |
protected final BinaryOp<OP> getOp(ValueNode forX, ValueNode forY) {
|
58533
|
69 |
ArithmeticOpTable table = getArithmeticOpTable(forX);
|
|
70 |
assert table.equals(getArithmeticOpTable(forY));
|
|
71 |
return getOp(table);
|
43972
|
72 |
}
|
|
73 |
|
|
74 |
@Override
|
|
75 |
public final BinaryOp<OP> getArithmeticOp() {
|
|
76 |
return getOp(getX(), getY());
|
|
77 |
}
|
|
78 |
|
|
79 |
public boolean isAssociative() {
|
|
80 |
return getArithmeticOp().isAssociative();
|
|
81 |
}
|
|
82 |
|
|
83 |
@Override
|
|
84 |
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
|
48190
|
85 |
NodeView view = NodeView.from(tool);
|
|
86 |
ValueNode result = tryConstantFold(getOp(forX, forY), forX, forY, stamp(view), view);
|
43972
|
87 |
if (result != null) {
|
|
88 |
return result;
|
|
89 |
}
|
54084
|
90 |
if (forX instanceof ConditionalNode && forY.isConstant() && forX.hasExactlyOneUsage()) {
|
|
91 |
ConditionalNode conditionalNode = (ConditionalNode) forX;
|
|
92 |
BinaryOp<OP> arithmeticOp = getArithmeticOp();
|
|
93 |
ConstantNode trueConstant = tryConstantFold(arithmeticOp, conditionalNode.trueValue(), forY, this.stamp(view), view);
|
|
94 |
if (trueConstant != null) {
|
|
95 |
ConstantNode falseConstant = tryConstantFold(arithmeticOp, conditionalNode.falseValue(), forY, this.stamp(view), view);
|
|
96 |
if (falseConstant != null) {
|
|
97 |
// @formatter:off
|
|
98 |
/* The arithmetic is folded into a constant on both sides of the conditional.
|
|
99 |
* Example:
|
|
100 |
* (cond ? -5 : 5) + 100
|
|
101 |
* canonicalizes to:
|
|
102 |
* (cond ? 95 : 105)
|
|
103 |
*/
|
|
104 |
// @formatter:on
|
|
105 |
return ConditionalNode.create(conditionalNode.condition, trueConstant,
|
|
106 |
falseConstant, view);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
43972
|
110 |
return this;
|
|
111 |
}
|
|
112 |
|
48190
|
113 |
@SuppressWarnings("unused")
|
|
114 |
public static <OP> ConstantNode tryConstantFold(BinaryOp<OP> op, ValueNode forX, ValueNode forY, Stamp stamp, NodeView view) {
|
43972
|
115 |
if (forX.isConstant() && forY.isConstant()) {
|
|
116 |
Constant ret = op.foldConstant(forX.asConstant(), forY.asConstant());
|
47798
|
117 |
if (ret != null) {
|
|
118 |
return ConstantNode.forPrimitive(stamp, ret);
|
|
119 |
}
|
43972
|
120 |
}
|
|
121 |
return null;
|
|
122 |
}
|
|
123 |
|
|
124 |
@Override
|
|
125 |
public Stamp foldStamp(Stamp stampX, Stamp stampY) {
|
48190
|
126 |
assert stampX.isCompatible(x.stamp(NodeView.DEFAULT)) && stampY.isCompatible(y.stamp(NodeView.DEFAULT));
|
43972
|
127 |
return getArithmeticOp().foldStamp(stampX, stampY);
|
|
128 |
}
|
|
129 |
|
48190
|
130 |
public static ValueNode add(StructuredGraph graph, ValueNode v1, ValueNode v2, NodeView view) {
|
|
131 |
return graph.addOrUniqueWithInputs(AddNode.create(v1, v2, view));
|
43972
|
132 |
}
|
|
133 |
|
48190
|
134 |
public static ValueNode add(ValueNode v1, ValueNode v2, NodeView view) {
|
|
135 |
return AddNode.create(v1, v2, view);
|
43972
|
136 |
}
|
|
137 |
|
54084
|
138 |
public static ValueNode add(ValueNode v1, ValueNode v2) {
|
|
139 |
return add(v1, v2, NodeView.DEFAULT);
|
|
140 |
}
|
|
141 |
|
48190
|
142 |
public static ValueNode mul(StructuredGraph graph, ValueNode v1, ValueNode v2, NodeView view) {
|
|
143 |
return graph.addOrUniqueWithInputs(MulNode.create(v1, v2, view));
|
43972
|
144 |
}
|
|
145 |
|
48190
|
146 |
public static ValueNode mul(ValueNode v1, ValueNode v2, NodeView view) {
|
|
147 |
return MulNode.create(v1, v2, view);
|
43972
|
148 |
}
|
|
149 |
|
54084
|
150 |
public static ValueNode mul(ValueNode v1, ValueNode v2) {
|
|
151 |
return mul(v1, v2, NodeView.DEFAULT);
|
|
152 |
}
|
|
153 |
|
48190
|
154 |
public static ValueNode sub(StructuredGraph graph, ValueNode v1, ValueNode v2, NodeView view) {
|
|
155 |
return graph.addOrUniqueWithInputs(SubNode.create(v1, v2, view));
|
43972
|
156 |
}
|
|
157 |
|
48190
|
158 |
public static ValueNode sub(ValueNode v1, ValueNode v2, NodeView view) {
|
|
159 |
return SubNode.create(v1, v2, view);
|
43972
|
160 |
}
|
|
161 |
|
54084
|
162 |
public static ValueNode sub(ValueNode v1, ValueNode v2) {
|
|
163 |
return sub(v1, v2, NodeView.DEFAULT);
|
|
164 |
}
|
|
165 |
|
52578
|
166 |
public static ValueNode branchlessMin(ValueNode v1, ValueNode v2, NodeView view) {
|
|
167 |
if (v1.isDefaultConstant() && !v2.isDefaultConstant()) {
|
|
168 |
return branchlessMin(v2, v1, view);
|
|
169 |
}
|
|
170 |
int bits = ((IntegerStamp) v1.stamp(view)).getBits();
|
|
171 |
assert ((IntegerStamp) v2.stamp(view)).getBits() == bits;
|
|
172 |
ValueNode t1 = sub(v1, v2, view);
|
|
173 |
ValueNode t2 = RightShiftNode.create(t1, bits - 1, view);
|
|
174 |
ValueNode t3 = AndNode.create(t1, t2, view);
|
|
175 |
return add(v2, t3, view);
|
|
176 |
}
|
|
177 |
|
|
178 |
public static ValueNode branchlessMax(ValueNode v1, ValueNode v2, NodeView view) {
|
|
179 |
if (v1.isDefaultConstant() && !v2.isDefaultConstant()) {
|
|
180 |
return branchlessMax(v2, v1, view);
|
|
181 |
}
|
|
182 |
int bits = ((IntegerStamp) v1.stamp(view)).getBits();
|
|
183 |
assert ((IntegerStamp) v2.stamp(view)).getBits() == bits;
|
|
184 |
if (v2.isDefaultConstant()) {
|
|
185 |
// prefer a & ~(a>>31) to a - (a & (a>>31))
|
|
186 |
return AndNode.create(v1, NotNode.create(RightShiftNode.create(v1, bits - 1, view)), view);
|
|
187 |
} else {
|
|
188 |
ValueNode t1 = sub(v1, v2, view);
|
|
189 |
ValueNode t2 = RightShiftNode.create(t1, bits - 1, view);
|
|
190 |
ValueNode t3 = AndNode.create(t1, t2, view);
|
|
191 |
return sub(v1, t3, view);
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
43972
|
195 |
private enum ReassociateMatch {
|
|
196 |
x,
|
|
197 |
y;
|
|
198 |
|
|
199 |
public ValueNode getValue(BinaryNode binary) {
|
|
200 |
switch (this) {
|
|
201 |
case x:
|
|
202 |
return binary.getX();
|
|
203 |
case y:
|
|
204 |
return binary.getY();
|
|
205 |
default:
|
|
206 |
throw GraalError.shouldNotReachHere();
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
public ValueNode getOtherValue(BinaryNode binary) {
|
|
211 |
switch (this) {
|
|
212 |
case x:
|
|
213 |
return binary.getY();
|
|
214 |
case y:
|
|
215 |
return binary.getX();
|
|
216 |
default:
|
|
217 |
throw GraalError.shouldNotReachHere();
|
|
218 |
}
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
private static ReassociateMatch findReassociate(BinaryNode binary, NodePredicate criterion) {
|
|
223 |
boolean resultX = criterion.apply(binary.getX());
|
|
224 |
boolean resultY = criterion.apply(binary.getY());
|
|
225 |
if (resultX && !resultY) {
|
|
226 |
return ReassociateMatch.x;
|
|
227 |
}
|
|
228 |
if (!resultX && resultY) {
|
|
229 |
return ReassociateMatch.y;
|
|
230 |
}
|
|
231 |
return null;
|
|
232 |
}
|
|
233 |
|
|
234 |
//@formatter:off
|
|
235 |
/*
|
|
236 |
* In reassociate, complexity comes from the handling of IntegerSub (non commutative) which can
|
|
237 |
* be mixed with IntegerAdd. It first tries to find m1, m2 which match the criterion :
|
|
238 |
* (a o m2) o m1
|
|
239 |
* (m2 o a) o m1
|
|
240 |
* m1 o (a o m2)
|
|
241 |
* m1 o (m2 o a)
|
|
242 |
* It then produces 4 boolean for the -/+ cases:
|
|
243 |
* invertA : should the final expression be like *-a (rather than a+*)
|
|
244 |
* aSub : should the final expression be like a-* (rather than a+*)
|
|
245 |
* invertM1 : should the final expression contain -m1
|
|
246 |
* invertM2 : should the final expression contain -m2
|
|
247 |
*
|
|
248 |
*/
|
|
249 |
//@formatter:on
|
|
250 |
/**
|
|
251 |
* Tries to re-associate values which satisfy the criterion. For example with a constantness
|
|
252 |
* criterion: {@code (a + 2) + 1 => a + (1 + 2)}
|
|
253 |
* <p>
|
|
254 |
* This method accepts only {@linkplain BinaryOp#isAssociative() associative} operations such as
|
|
255 |
* +, -, *, &, | and ^
|
|
256 |
*
|
|
257 |
* @param forY
|
|
258 |
* @param forX
|
|
259 |
*/
|
48190
|
260 |
public static ValueNode reassociate(BinaryArithmeticNode<?> node, NodePredicate criterion, ValueNode forX, ValueNode forY, NodeView view) {
|
43972
|
261 |
assert node.getOp(forX, forY).isAssociative();
|
|
262 |
ReassociateMatch match1 = findReassociate(node, criterion);
|
|
263 |
if (match1 == null) {
|
|
264 |
return node;
|
|
265 |
}
|
|
266 |
ValueNode otherValue = match1.getOtherValue(node);
|
|
267 |
boolean addSub = false;
|
|
268 |
boolean subAdd = false;
|
|
269 |
if (otherValue.getClass() != node.getClass()) {
|
|
270 |
if (node instanceof AddNode && otherValue instanceof SubNode) {
|
|
271 |
addSub = true;
|
|
272 |
} else if (node instanceof SubNode && otherValue instanceof AddNode) {
|
|
273 |
subAdd = true;
|
|
274 |
} else {
|
|
275 |
return node;
|
|
276 |
}
|
|
277 |
}
|
|
278 |
BinaryNode other = (BinaryNode) otherValue;
|
|
279 |
ReassociateMatch match2 = findReassociate(other, criterion);
|
|
280 |
if (match2 == null) {
|
|
281 |
return node;
|
|
282 |
}
|
|
283 |
boolean invertA = false;
|
|
284 |
boolean aSub = false;
|
|
285 |
boolean invertM1 = false;
|
|
286 |
boolean invertM2 = false;
|
|
287 |
if (addSub) {
|
|
288 |
invertM2 = match2 == ReassociateMatch.y;
|
|
289 |
invertA = !invertM2;
|
|
290 |
} else if (subAdd) {
|
|
291 |
invertA = invertM2 = match1 == ReassociateMatch.x;
|
|
292 |
invertM1 = !invertM2;
|
|
293 |
} else if (node instanceof SubNode && other instanceof SubNode) {
|
|
294 |
invertA = match1 == ReassociateMatch.x ^ match2 == ReassociateMatch.x;
|
|
295 |
aSub = match1 == ReassociateMatch.y && match2 == ReassociateMatch.y;
|
|
296 |
invertM1 = match1 == ReassociateMatch.y && match2 == ReassociateMatch.x;
|
|
297 |
invertM2 = match1 == ReassociateMatch.x && match2 == ReassociateMatch.x;
|
|
298 |
}
|
|
299 |
assert !(invertM1 && invertM2) && !(invertA && aSub);
|
|
300 |
ValueNode m1 = match1.getValue(node);
|
|
301 |
ValueNode m2 = match2.getValue(other);
|
|
302 |
ValueNode a = match2.getOtherValue(other);
|
|
303 |
if (node instanceof AddNode || node instanceof SubNode) {
|
46344
|
304 |
ValueNode associated;
|
43972
|
305 |
if (invertM1) {
|
48190
|
306 |
associated = BinaryArithmeticNode.sub(m2, m1, view);
|
43972
|
307 |
} else if (invertM2) {
|
48190
|
308 |
associated = BinaryArithmeticNode.sub(m1, m2, view);
|
43972
|
309 |
} else {
|
48190
|
310 |
associated = BinaryArithmeticNode.add(m1, m2, view);
|
43972
|
311 |
}
|
|
312 |
if (invertA) {
|
48190
|
313 |
return BinaryArithmeticNode.sub(associated, a, view);
|
43972
|
314 |
}
|
|
315 |
if (aSub) {
|
48190
|
316 |
return BinaryArithmeticNode.sub(a, associated, view);
|
43972
|
317 |
}
|
48190
|
318 |
return BinaryArithmeticNode.add(a, associated, view);
|
43972
|
319 |
} else if (node instanceof MulNode) {
|
48190
|
320 |
return BinaryArithmeticNode.mul(a, AddNode.mul(m1, m2, view), view);
|
43972
|
321 |
} else if (node instanceof AndNode) {
|
|
322 |
return new AndNode(a, new AndNode(m1, m2));
|
|
323 |
} else if (node instanceof OrNode) {
|
|
324 |
return new OrNode(a, new OrNode(m1, m2));
|
|
325 |
} else if (node instanceof XorNode) {
|
|
326 |
return new XorNode(a, new XorNode(m1, m2));
|
|
327 |
} else {
|
|
328 |
throw GraalError.shouldNotReachHere();
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
/**
|
|
333 |
* Ensure a canonical ordering of inputs for commutative nodes to improve GVN results. Order the
|
|
334 |
* inputs by increasing {@link Node#id} and call {@link Graph#findDuplicate(Node)} on the node
|
|
335 |
* if it's currently in a graph. It's assumed that if there was a constant on the left it's been
|
|
336 |
* moved to the right by other code and that ordering is left alone.
|
|
337 |
*
|
|
338 |
* @return the original node or another node with the same input ordering
|
|
339 |
*/
|
|
340 |
@SuppressWarnings("deprecation")
|
|
341 |
public BinaryNode maybeCommuteInputs() {
|
|
342 |
assert this instanceof BinaryCommutative;
|
46344
|
343 |
if (!y.isConstant() && (x.isConstant() || x.getId() > y.getId())) {
|
43972
|
344 |
ValueNode tmp = x;
|
|
345 |
x = y;
|
|
346 |
y = tmp;
|
|
347 |
if (graph() != null) {
|
|
348 |
// See if this node already exists
|
|
349 |
BinaryNode duplicate = graph().findDuplicate(this);
|
|
350 |
if (duplicate != null) {
|
|
351 |
return duplicate;
|
|
352 |
}
|
|
353 |
}
|
|
354 |
}
|
|
355 |
return this;
|
|
356 |
}
|
|
357 |
|
|
358 |
/**
|
|
359 |
* Determines if it would be better to swap the inputs in order to produce better assembly code.
|
|
360 |
* First we try to pick a value which is dead after this use. If both values are dead at this
|
|
361 |
* use then we try pick an induction variable phi to encourage the phi to live in a single
|
|
362 |
* register.
|
|
363 |
*
|
|
364 |
* @param nodeValueMap
|
|
365 |
* @return true if inputs should be swapped, false otherwise
|
|
366 |
*/
|
|
367 |
protected boolean shouldSwapInputs(NodeValueMap nodeValueMap) {
|
|
368 |
final boolean xHasOtherUsages = getX().hasUsagesOtherThan(this, nodeValueMap);
|
|
369 |
final boolean yHasOtherUsages = getY().hasUsagesOtherThan(this, nodeValueMap);
|
|
370 |
|
|
371 |
if (!getY().isConstant() && !yHasOtherUsages) {
|
|
372 |
if (xHasOtherUsages == yHasOtherUsages) {
|
|
373 |
return getY() instanceof ValuePhiNode && getY().inputs().contains(this);
|
|
374 |
} else {
|
|
375 |
return true;
|
|
376 |
}
|
|
377 |
}
|
|
378 |
return false;
|
|
379 |
}
|
|
380 |
|
|
381 |
}
|