RFR: 8279607: Existing optimization "~x+1" -> "-x" can be generalized to "~x+c" -> "(c-1)-x".

Zhiqiang Zang duke at openjdk.java.net
Fri Jan 7 17:28:55 UTC 2022


On Thu, 16 Dec 2021 00:02:12 GMT, Zhiqiang Zang <duke at openjdk.java.net> wrote:

> Hi all,
> 
> Existing optimization `~x+1 -> -x` can be generalized to `~x+c -> (c-1)-x`. I included both microbenchmark and jtreg tests.
> 
> 
> // Convert (~x+c) into (c-1)-x. Note there isn't a bitwise not
> // bytecode, "~x" would typically represented as "x^(-1)", so (~x+c)
> // will be (x^(-1))+c.
> if (op1 == Op_Xor(bt) &&
>     (in2->Opcode() == Op_ConI || in2->Opcode() == Op_ConL) &&
>     phase->type(in1->in(2)) == TypeInteger::minus_1(bt)) {
>   Node* c_minus_one = phase->makecon(add_ring(phase->type(in(2)), TypeInteger::minus_1(bt)));
>   return SubNode::make(c_minus_one, in1->in(1), bt);
> }
> 
> 
> Thank you for reviewing.

Attach benchmark results:

Baseline:

Benchmark                       Mode  Cnt  Score   Error  Units
AddIdealNotXPlusC.baselineInt   avgt   60  1.479 ± 0.004  ns/op
AddIdealNotXPlusC.baselineLong  avgt   60  1.479 ± 0.006  ns/op
AddIdealNotXPlusC.testInt       avgt   60  3.142 ± 0.019  ns/op
AddIdealNotXPlusC.testLong      avgt   60  3.150 ± 0.019  ns/op


Patch:

Benchmark                       Mode  Cnt  Score   Error  Units
AddIdealNotXPlusC.baselineInt   avgt   60  1.489 ± 0.008  ns/op
AddIdealNotXPlusC.baselineLong  avgt   60  1.488 ± 0.007  ns/op
AddIdealNotXPlusC.testInt       avgt   60  0.343 ± 0.001  ns/op
AddIdealNotXPlusC.testLong      avgt   60  0.343 ± 0.001  ns/op

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

PR: https://git.openjdk.java.net/jdk/pull/6858


More information about the hotspot-compiler-dev mailing list