RFR: 8332856: C2: Add new transform for bool eq/ne (cmp (and (urshift X const1) const2) 0)

Emanuel Peter epeter at openjdk.org
Tue May 28 16:23:06 UTC 2024


On Mon, 20 May 2024 14:15:46 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.

If I compare the two:

bool eq/ne (cmp (and (urshift X 4) 1) 0)
bool ne/eq (cmp (and X 8) 0)

Then I see that the outer part is the same for both:
`bool ne/eq (cmp (...) 0)`

So why would this not just be a optimization for the pattern `AndI(Shift X shift, mask)` -> `AndI(X, new_mask)`?

That would be more general.

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

PR Comment: https://git.openjdk.org/jdk/pull/19310#issuecomment-2135656727


More information about the hotspot-compiler-dev mailing list