43972
|
1 |
/*
|
58877
|
2 |
* Copyright (c) 2015, 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 |
|
|
27 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
28 |
import org.graalvm.compiler.nodes.FixedNode;
|
48190
|
29 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
30 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
31 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
32 |
import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode;
|
48190
|
33 |
import org.graalvm.compiler.nodes.calc.FixedBinaryNode;
|
43972
|
34 |
import org.graalvm.compiler.nodes.calc.SignedDivNode;
|
49451
|
35 |
import org.graalvm.compiler.nodes.calc.UnsignedDivNode;
|
50330
|
36 |
import org.graalvm.compiler.nodes.extended.GuardingNode;
|
49451
|
37 |
|
|
38 |
import java.util.function.BiFunction;
|
43972
|
39 |
|
|
40 |
/**
|
|
41 |
* Utility methods to perform integer math with some obvious constant folding first.
|
|
42 |
*/
|
|
43 |
public class MathUtil {
|
|
44 |
private static boolean isConstantOne(ValueNode v1) {
|
48190
|
45 |
return v1.isConstant() && v1.stamp(NodeView.DEFAULT) instanceof IntegerStamp && v1.asJavaConstant().asLong() == 1;
|
43972
|
46 |
}
|
|
47 |
|
|
48 |
private static boolean isConstantZero(ValueNode v1) {
|
48190
|
49 |
return v1.isConstant() && v1.stamp(NodeView.DEFAULT) instanceof IntegerStamp && v1.asJavaConstant().asLong() == 0;
|
43972
|
50 |
}
|
|
51 |
|
|
52 |
public static ValueNode add(StructuredGraph graph, ValueNode v1, ValueNode v2) {
|
|
53 |
if (isConstantZero(v1)) {
|
|
54 |
return v2;
|
|
55 |
}
|
|
56 |
if (isConstantZero(v2)) {
|
|
57 |
return v1;
|
|
58 |
}
|
48190
|
59 |
return BinaryArithmeticNode.add(graph, v1, v2, NodeView.DEFAULT);
|
43972
|
60 |
}
|
|
61 |
|
|
62 |
public static ValueNode mul(StructuredGraph graph, ValueNode v1, ValueNode v2) {
|
|
63 |
if (isConstantOne(v1)) {
|
|
64 |
return v2;
|
|
65 |
}
|
|
66 |
if (isConstantOne(v2)) {
|
|
67 |
return v1;
|
|
68 |
}
|
48190
|
69 |
return BinaryArithmeticNode.mul(graph, v1, v2, NodeView.DEFAULT);
|
43972
|
70 |
}
|
|
71 |
|
|
72 |
public static ValueNode sub(StructuredGraph graph, ValueNode v1, ValueNode v2) {
|
|
73 |
if (isConstantZero(v2)) {
|
|
74 |
return v1;
|
|
75 |
}
|
48190
|
76 |
return BinaryArithmeticNode.sub(graph, v1, v2, NodeView.DEFAULT);
|
43972
|
77 |
}
|
|
78 |
|
50330
|
79 |
public static ValueNode divBefore(StructuredGraph graph, FixedNode before, ValueNode dividend, ValueNode divisor, GuardingNode zeroCheck) {
|
|
80 |
return fixedDivBefore(graph, before, dividend, divisor, (dend, sor) -> SignedDivNode.create(dend, sor, zeroCheck, NodeView.DEFAULT));
|
49451
|
81 |
}
|
|
82 |
|
50330
|
83 |
public static ValueNode unsignedDivBefore(StructuredGraph graph, FixedNode before, ValueNode dividend, ValueNode divisor, GuardingNode zeroCheck) {
|
|
84 |
return fixedDivBefore(graph, before, dividend, divisor, (dend, sor) -> UnsignedDivNode.create(dend, sor, zeroCheck, NodeView.DEFAULT));
|
49451
|
85 |
}
|
|
86 |
|
|
87 |
private static ValueNode fixedDivBefore(StructuredGraph graph, FixedNode before, ValueNode dividend, ValueNode divisor, BiFunction<ValueNode, ValueNode, ValueNode> createDiv) {
|
43972
|
88 |
if (isConstantOne(divisor)) {
|
|
89 |
return dividend;
|
|
90 |
}
|
54084
|
91 |
ValueNode div = createDiv.apply(dividend, divisor);
|
48190
|
92 |
if (div instanceof FixedBinaryNode) {
|
49451
|
93 |
FixedBinaryNode fixedDiv = (FixedBinaryNode) div;
|
|
94 |
if (before.predecessor() instanceof FixedBinaryNode) {
|
|
95 |
FixedBinaryNode binaryPredecessor = (FixedBinaryNode) before.predecessor();
|
|
96 |
if (fixedDiv.dataFlowEquals(binaryPredecessor)) {
|
58877
|
97 |
if (fixedDiv.isAlive()) {
|
|
98 |
fixedDiv.safeDelete();
|
|
99 |
}
|
49451
|
100 |
return binaryPredecessor;
|
|
101 |
}
|
|
102 |
}
|
54084
|
103 |
graph.addBeforeFixed(before, graph.addOrUniqueWithInputs(fixedDiv));
|
48190
|
104 |
}
|
43972
|
105 |
return div;
|
|
106 |
}
|
|
107 |
}
|