JDK-8281453 : New optimization: convert ~x into -1-x when ~x is used in an arithmetic expression
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 19
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2022-02-08
  • Updated: 2022-10-13
  • Resolved: 2022-10-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 20
20 b19Fixed
Related Reports
Relates :  
Relates :  
Description
Similar to (~x)+c -> (c-1)-x and ~(x+c) -> (-c-1)-x in JDK-8279607, we can also introduce similar optimizations for subtraction, c-(~x) -> x+(c+1) and ~(c-x) -> x+(-c-1).

To generalize, I convert ~x into -1-x when ~x is used in an arithmetic expression. For example, c-(~x) will be converted into c-(-1-x) which will match other pattern and will be transformed again in next iteration and finally become x+(c+1).

The results of the microbenchmark are as follows:

Baseline:                                                                                                                                         
Benchmark                         Mode  Cnt  Score    Error  Units
NotOpTransformation.baselineInt   avgt   60  0.439 ±  0.001  ns/op
NotOpTransformation.baselineLong  avgt   60  0.439 ±  0.001  ns/op
NotOpTransformation.testInt1      avgt   60  0.603 ±  0.001  ns/op
NotOpTransformation.testInt2      avgt   60  0.603 ±  0.001  ns/op
NotOpTransformation.testLong1     avgt   60  0.658 ±  0.001  ns/op
NotOpTransformation.testLong2     avgt   60  0.658 ±  0.001  ns/op

Patch:
Benchmark                         Mode  Cnt  Score    Error  Units
NotOpTransformation.baselineInt   avgt   60  0.439 ±  0.001  ns/op
NotOpTransformation.baselineLong  avgt   60  0.439 ±  0.001  ns/op
NotOpTransformation.testInt1      avgt   60  0.329 ±  0.001  ns/op
NotOpTransformation.testInt2      avgt   60  0.329 ±  0.001  ns/op
NotOpTransformation.testLong1     avgt   60  0.329 ±  0.001  ns/op
NotOpTransformation.testLong2     avgt   60  0.329 ±  0.001  ns/op
Comments
Changeset: 5dd851d8 Author: Zhiqiang Zang <zhiqiang.zang@utexas.edu> Committer: Vladimir Kozlov <kvn@openjdk.org> Date: 2022-10-06 22:15:20 +0000 URL: https://git.openjdk.org/jdk/commit/5dd851d872c50ef33034c56007c58e6fa69ebd32
06-10-2022

A pull request was submitted for review. URL: https://git.openjdk.java.net/jdk/pull/7376 Date: 2022-02-08 05:51:37 +0000
08-02-2022

Moving to JDK project, for dev review.
08-02-2022