RFR: 8332856: C2: Add new transform for bool eq/ne (cmp (and (urshift X const1) const2) 0) [v2]
Jasmine Karthikeyan
jkarthikeyan at openjdk.org
Sat Jun 29 18:15:19 UTC 2024
On Tue, 28 May 2024 20:11:35 GMT, Tobias Hotz <duke at openjdk.org> wrote:
>> This PR adds a new ideal optimization for the following pattern:
>>
>> public boolean testFunc(int a) {
>> int mask = 0b101;
>> int shift = 12;
>> return ((a >> shift) & mask) == 0;
>> }
>>
>> Where the mask and shift are constant values and a is a variable. For this optimization to work, the right shift has to be idealized to a unsinged right shift earlier in the pipeline, which here: https://github.com/openjdk/jdk/blob/b92bd671835c37cff58e2cdcecd0fe4277557d7f/src/hotspot/share/opto/mulnode.cpp#L731
>> If the shift is already an unsiged bit shift, it works as well.
>> On AMD64 CPUs, this means that this whole line computation can be reduced to a simple `test` instruction.
>
> Tobias Hotz has updated the pull request incrementally with two additional commits since the last revision:
>
> - LF endings...
> - Add a benchmark to measure effect of new ideal transformation
I have mostly stylistic comments here, I think the patch itself looks good. Also, the copyright on `subnode.cpp` should be updated to 2024.
src/hotspot/share/opto/subnode.cpp line 1627:
> 1625:
> 1626: // Change "bool eq/ne (cmp (and (urshift X 4) 1) 0)" into "bool ne/eq (cmp (and X 8) 0)".
> 1627: // Note: rshift gets converted to urshift in and Ideal
Suggestion:
// Note: rshift gets converted to urshift in AndNode Ideal
src/hotspot/share/opto/subnode.cpp line 1633:
> 1631: const TypeInt* shift_val_type = phase->type(cmp1->in(1)->in(2))->isa_int();
> 1632: const TypeInt* mask_type = phase->type(cmp1->in(2))->isa_int();
> 1633: if (shift_val_type && shift_val_type->is_con() && mask_type && mask_type->is_con()) {
Suggestion:
if (shift_val_type != nullptr && shift_val_type->is_con() && mask_type != nullptr && mask_type->is_con()) {
test/micro/org/openjdk/bench/vm/compiler/ShiftAndCmpZeroIdeal.java line 2:
> 1: /*
> 2: * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
Suggestion:
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
-------------
PR Review: https://git.openjdk.org/jdk/pull/19310#pullrequestreview-2149762686
PR Review Comment: https://git.openjdk.org/jdk/pull/19310#discussion_r1659925210
PR Review Comment: https://git.openjdk.org/jdk/pull/19310#discussion_r1659925311
PR Review Comment: https://git.openjdk.org/jdk/pull/19310#discussion_r1659925357
More information about the hotspot-compiler-dev
mailing list