RFR: 8261008: Optimize Xor

Eric Liu github.com+10482586+therealeliu at openjdk.java.net
Mon Mar 1 09:42:02 UTC 2021


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.

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

Commit messages:
 - 8261008: Optimize Xor

Changes: https://git.openjdk.java.net/jdk/pull/2776/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2776&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8261008
  Stats: 26 lines in 2 files changed: 25 ins; 0 del; 1 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2776.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2776/head:pull/2776

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


More information about the hotspot-compiler-dev mailing list