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
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
- relates to
-
JDK-8279607 Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x".
-
- Resolved
-
-
JDK-8283094 Add Ideal transformation: x + (con - y) -> (x - y) + con
-
- Resolved
-