RFR: 8261008: Optimize Xor
Bernardo Sulzbach
github.com+8271090+bernardosulzbach at openjdk.java.net
Mon Mar 1 13:43:48 UTC 2021
On Mon, 1 Mar 2021 10:40:51 GMT, Tobias Hartmann <thartmann at openjdk.org> wrote:
>> This patch canonicalizes 'Xor' to constant zero when both inputs are the
>> same.
>>
>> It's not quite easy to measure the performance change between 'xor' and
>> constant zero, the later is typically a single 'mov' in generated code.
>> But given by this transformation, c2 may perform some other more
>> powerful optimizations.
>>
>> This was tested with the micro benchmark below. Loop in this case is
>> properly removed and the performance increases significantly.
>>
>> public void xorTheSame(MyState s, Blackhole bh) {
>> int x = s.in1;
>> int y = s.in2;
>> for (int i = 0; i < 5000; i++) {
>> y = x ^ x;
>> x = y ^ y;
>> }
>> bh.consume(x);
>> }
>>
>> [Test]
>> All jtreg tests passed without new failure.
>
> Just wondering, why is `x ^ x` special? Shouldn't we then also optimize `x - x`, `x | x` and others?
If we agree that this optimization is desired, then it makes sense to also write optimizations for `x - x` and `x | x`.
However, how often do these patterns occur in real code? Can you provide some examples (of source code from OSS projects) where `x ^ x`, `x - x`, or `x | x` occur?
-------------
PR: https://git.openjdk.java.net/jdk/pull/2776
More information about the hotspot-compiler-dev
mailing list