RFR: 8351515: C2 incorrectly removes double negation for double and float

Manuel Hässig duke at openjdk.org
Fri Mar 21 10:25:32 UTC 2025


# Issue Summary

A fuzzer run discovered that code of the form `0 - (0 - x)` yields the wrong result for `x = -0.0`. According to the [Java Language Spec 15.8.2](https://docs.oracle.com/javase/specs/jls/se23/html/jls-15.html#jls-15.18.2) the sum of floating point zeroes of opposite sign is positive zero. Hence, `(0 - (0 - (-0.0))` must result in `+0.0`, but due to the folding of all double negations in `SubNode::Identity` this was optimized to `-0.0`

# Changeset overview

To fix this issue, I excluded floating point numbers from the folding of double negations, which also includes `Float16` values. This might seem excessive at first glance, but we do not track range information for floating point types. Hence, we could still perform the folding of the double negation if a floating point constant is not `-0.0`. However, constant folding already takes care of this.

Changes:
 - IR-Framework: fix `IRNode.SUB`not matching `SubHFNode`
 - Add a regression IR-test
 - Exclude floating point `SubNodes` from folding double negations

# Testing
- [Github Actions](https://github.com/mhaessig/jdk/actions/runs/13989207348)
- `tier1` through `tier5` plus Oracle internal testing

-------------

Commit messages:
 - subnode: do not remove double negation of floating point numbers
 - Add regression test for JDK-8351515
 - Add SUB_HF nodes to generic sub node matching

Changes: https://git.openjdk.org/jdk/pull/24150/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=24150&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8351515
  Stats: 88 lines in 3 files changed: 83 ins; 0 del; 5 mod
  Patch: https://git.openjdk.org/jdk/pull/24150.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/24150/head:pull/24150

PR: https://git.openjdk.org/jdk/pull/24150


More information about the hotspot-compiler-dev mailing list