RFR: 8273021: C2: Improve Add and Xor ideal optimizations [v3]

Tobias Hartmann thartmann at openjdk.java.net
Fri Sep 10 06:18:06 UTC 2021


On Fri, 10 Sep 2021 02:21:59 GMT, Yi Yang <yyang at openjdk.org> wrote:

>> test/hotspot/jtreg/compiler/c2/TestAddXorIdeal.java line 77:
>> 
>>> 75:         Random random = new Random();
>>> 76:         int n = 0;
>>> 77:         long n1 = 0;
>> 
>> Should be declared in the loop.
>
> Do you mean declared within loop body? I've changed but it looks like a perference problem.

Yes, in Java, local variables should be declared as close as possible to the point they are first used at (see, for example, [Google's Java Style Guide](https://google.github.io/styleguide/javaguide.html#s4.8.2-variable-declarations)). The declaration does not affect performance.

Here's how I would write the loop to improve readability:

for (int j = 0; j < 50_000; j++) {
  int i = random.nextInt();
  long l = random.nextLong();
  Asserts.assertTrue(test1(i) == -i);
  Asserts.assertTrue(test2(i) == -i);
  Asserts.assertTrue(test3(l) == -l);
  ...

Summary: No need to use negative initial value for loop induction variable (as it is not even used), increase number of iterations to ensure C2 compilation (you are running without `-Xbatch`), use same random numbers per loop iteration, use more intuitive variable names.

But these are just code style details, feel free to ignore.

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

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


More information about the hotspot-compiler-dev mailing list