Skip to content

Commit 5591f8a

Browse files
committed
8351515: C2 incorrectly removes double negation for double and float
Reviewed-by: thartmann, chagedorn
1 parent 56a4ffa commit 5591f8a

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

src/hotspot/share/opto/subnode.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ Node* SubNode::Identity(PhaseGVN* phase) {
5353
assert(in(1) != this, "Must already have called Value");
5454
assert(in(2) != this, "Must already have called Value");
5555

56-
// Remove double negation
57-
const Type *zero = add_id();
58-
if( phase->type( in(1) )->higher_equal( zero ) &&
56+
const Type* zero = add_id();
57+
58+
// Remove double negation if it is not a floating point number since negation
59+
// is not the same as subtraction for floating point numbers
60+
// (cf. JLS § 15.15.4). `0-(0-(-0.0))` must be equal to positive 0.0 according to
61+
// JLS § 15.8.2, but would result in -0.0 if this folding would be applied.
62+
if (phase->type(in(1))->higher_equal(zero) &&
5963
in(2)->Opcode() == Opcode() &&
60-
phase->type( in(2)->in(1) )->higher_equal( zero ) ) {
64+
phase->type(in(2)->in(1))->higher_equal(zero) &&
65+
!phase->type(in(2)->in(2))->is_floatingpoint()) {
6166
return in(2)->in(2);
6267
}
6368

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 8351515
27+
* @summary The compiler must not fold `0-(0-x)` for x: float | double
28+
* @library /test/lib /
29+
* @modules jdk.incubator.vector
30+
* @run driver compiler.floatingpoint.TestSubNodeFloatDoubleNegation
31+
*/
32+
package compiler.floatingpoint;
33+
34+
import compiler.lib.ir_framework.*;
35+
import jdk.incubator.vector.Float16;
36+
import jdk.test.lib.Asserts;
37+
38+
public class TestSubNodeFloatDoubleNegation {
39+
40+
public static void main(String[] args) {
41+
TestFramework.runWithFlags("--add-modules=jdk.incubator.vector", "-XX:CompileCommand=inline,jdk.incubator.vector.Float16::*");
42+
}
43+
44+
@Run(test = { "testHalfFloat", "testFloat", "testDouble" })
45+
public static void assertResults() {
46+
short halfFloatRes = Float16.float16ToShortBits(testHalfFloat(Float16.valueOf(-0.0f)));
47+
int floatRes = Float.floatToIntBits(testFloat(-0.0f));
48+
long doubleRes = Double.doubleToLongBits(testDouble(-0.0));
49+
50+
Asserts.assertEQ((short) 0, halfFloatRes);
51+
Asserts.assertEQ((int) 0, floatRes);
52+
Asserts.assertEQ((long) 0, doubleRes);
53+
}
54+
55+
@Test
56+
// Match a generic SubNode, because scalar Float16 operations are often
57+
// performed as float operations.
58+
@IR(counts = { IRNode.SUB, "2" })
59+
// Checks that the subtractions in 0 - (0 - hf) do not get eliminiated
60+
public static Float16 testHalfFloat(Float16 hf) {
61+
return Float16.subtract(
62+
Float16.shortBitsToFloat16((short) 0),
63+
Float16.subtract(Float16.shortBitsToFloat16((short) 0), hf));
64+
}
65+
66+
@Test
67+
@IR(counts = { IRNode.SUB_F, "2" })
68+
// Checks that the subtractions in 0 - (0 - f) do not get eliminated
69+
public static float testFloat(float f) {
70+
return 0 - (0 - f);
71+
}
72+
73+
@Test
74+
@IR(counts = { IRNode.SUB_D, "2" })
75+
// Checks that the subtractions in 0 - (0 - d) do not get eliminated
76+
public static double testDouble(double d) {
77+
return 0 - (0 - d);
78+
}
79+
80+
}

test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ public class IRNode {
18331833

18341834
public static final String SUB = PREFIX + "SUB" + POSTFIX;
18351835
static {
1836-
beforeMatchingNameRegex(SUB, "Sub(I|L|F|D)");
1836+
beforeMatchingNameRegex(SUB, "Sub(I|L|F|D|HF)");
18371837
}
18381838

18391839
public static final String SUB_D = PREFIX + "SUB_D" + POSTFIX;

0 commit comments

Comments
 (0)