Skip to content

Commit ecece8f

Browse files
committed
Improve AbsNode canonicalization
Add an extra canonicalization to AbsNode with NegateNode inputs, and also add unit tests. Also fix an incorrect return value in its create factory method.
1 parent 2602091 commit ecece8f

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2021, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package org.graalvm.compiler.core.test;
26+
27+
import org.graalvm.compiler.nodes.StructuredGraph;
28+
import org.graalvm.compiler.nodes.calc.AbsNode;
29+
import org.graalvm.compiler.nodes.calc.NegateNode;
30+
import org.junit.Assert;
31+
import org.junit.Test;
32+
33+
public class AbsCanonicalizationTest extends GraalCompilerTest {
34+
public static double absReference(double x) {
35+
return Math.abs(x);
36+
}
37+
38+
public static double absAbs(double x) {
39+
return Math.abs(Math.abs(x));
40+
}
41+
42+
public static double absNegate(double x) {
43+
return Math.abs(-x);
44+
}
45+
46+
@Test
47+
public void testAbsNegate() {
48+
StructuredGraph graph = parseEager("absNegate", StructuredGraph.AllowAssumptions.YES);
49+
createInliningPhase().apply(graph, getDefaultHighTierContext());
50+
createCanonicalizerPhase().apply(graph, getProviders());
51+
52+
StructuredGraph referenceGraph = parseEager("absReference", StructuredGraph.AllowAssumptions.YES);
53+
assertEquals(referenceGraph, graph);
54+
Assert.assertEquals(0, graph.getNodes().filter(NegateNode.class).count());
55+
56+
testAgainstExpected(graph.method(), new Result(absNegate(-Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE);
57+
testAgainstExpected(graph.method(), new Result(absNegate(0d), null), (Object) null, 0d);
58+
testAgainstExpected(graph.method(), new Result(absNegate(Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE);
59+
}
60+
61+
@Test
62+
public void testAbsAbs() {
63+
StructuredGraph graph = parseEager("absAbs", StructuredGraph.AllowAssumptions.YES);
64+
createInliningPhase().apply(graph, getDefaultHighTierContext());
65+
createCanonicalizerPhase().apply(graph, getProviders());
66+
67+
StructuredGraph referenceGraph = parseEager("absReference", StructuredGraph.AllowAssumptions.YES);
68+
assertEquals(referenceGraph, graph);
69+
Assert.assertEquals(1, graph.getNodes().filter(AbsNode.class).count());
70+
71+
testAgainstExpected(graph.method(), new Result(absAbs(-Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE);
72+
testAgainstExpected(graph.method(), new Result(absAbs(0d), null), (Object) null, 0d);
73+
testAgainstExpected(graph.method(), new Result(absAbs(Double.MAX_VALUE), null), (Object) null, Double.MAX_VALUE);
74+
}
75+
}

compiler/src/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/calc/AbsNode.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static ValueNode create(ValueNode value, NodeView view) {
5656
if (synonym != null) {
5757
return synonym;
5858
}
59-
return new NegateNode(value);
59+
return new AbsNode(value);
6060
}
6161

6262
protected static ValueNode findSynonym(ValueNode forValue, NodeView view) {
@@ -65,6 +65,14 @@ protected static ValueNode findSynonym(ValueNode forValue, NodeView view) {
6565
if (synonym != null) {
6666
return synonym;
6767
}
68+
if (forValue instanceof AbsNode) {
69+
return forValue;
70+
}
71+
// abs(-x) => abs(x)
72+
if (forValue instanceof NegateNode) {
73+
NegateNode negate = (NegateNode) forValue;
74+
return AbsNode.create(negate.getValue(), view);
75+
}
6876
return null;
6977
}
7078

@@ -79,8 +87,9 @@ public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
7987
if (ret != this) {
8088
return ret;
8189
}
82-
if (forValue instanceof AbsNode) {
83-
return forValue;
90+
ValueNode synonym = findSynonym(forValue, NodeView.from(tool));
91+
if (synonym != null) {
92+
return synonym;
8493
}
8594
return this;
8695
}

0 commit comments

Comments
 (0)