Skip to content

Commit 9d6a61f

Browse files
8371558: C2: Missing optimization opportunity in AbsNode::Ideal
Reviewed-by: thartmann, rcastanedalo, chagedorn
1 parent 10220ed commit 9d6a61f

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/hotspot/share/opto/phaseX.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,16 @@ void PhaseIterGVN::add_users_of_use_to_worklist(Node* n, Node* use, Unique_Node_
25962596
}
25972597
}
25982598
}
2599+
// Check for "abs(0-x)" into "abs(x)" conversion
2600+
if (use->is_Sub()) {
2601+
for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
2602+
Node* u = use->fast_out(i2);
2603+
if (u->Opcode() == Op_AbsD || u->Opcode() == Op_AbsF ||
2604+
u->Opcode() == Op_AbsL || u->Opcode() == Op_AbsI) {
2605+
worklist.push(u);
2606+
}
2607+
}
2608+
}
25992609
auto enqueue_init_mem_projs = [&](ProjNode* proj) {
26002610
add_users_to_worklist0(proj, worklist);
26012611
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2025, 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+
24+
/*
25+
* @test
26+
* @bug 8371558
27+
* @summary An expression of the form "abs(0-x)" should be transformed to "abs(x)".
28+
* This test ensures that updates to the Sub node’s inputs propagate as
29+
* expected and that the optimization is not missed.
30+
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation -Xcomp
31+
* -XX:CompileCommand=compileonly,compiler.c2.TestMissingOptAbsZeroMinusX::test*
32+
* -XX:VerifyIterativeGVN=1110 compiler.c2.TestMissingOptAbsZeroMinusX
33+
* @run main compiler.c2.TestMissingOptAbsZeroMinusX
34+
*
35+
*/
36+
37+
package compiler.c2;
38+
39+
public class TestMissingOptAbsZeroMinusX {
40+
static int a;
41+
static boolean b;
42+
43+
public static void main(String[] strArr) {
44+
// no known reproducer for AbsL
45+
testAbsI();
46+
testAbsF();
47+
testAbsD();
48+
}
49+
50+
static void testAbsI() {
51+
int d = 4;
52+
for (int i = 8; i < 133; i += 3) {
53+
d -= a;
54+
b = (d != Math.abs(d));
55+
for (long f = 3; f < 127; f++) {
56+
for (int e = 1; e < 3; e++) {}
57+
}
58+
d = 0;
59+
}
60+
}
61+
62+
static void testAbsF() {
63+
float d = 12.3f;
64+
for (int i = 8; i < 133; i += 3) {
65+
d -= a;
66+
b = (d != Math.abs(d));
67+
for (long f = 3; f < 127; f++) {
68+
for (int e = 1; e < 3; e++) {}
69+
}
70+
d = 0.0f;
71+
}
72+
}
73+
74+
static void testAbsD() {
75+
double d = 12.3;
76+
for (int i = 8; i < 133; i += 3) {
77+
d -= a;
78+
b = (d != Math.abs(d));
79+
for (long f = 3; f < 127; f++) {
80+
for (int e = 1; e < 3; e++) {}
81+
}
82+
d = 0.0;
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)