On 10/10/23 06:28, Roger Sayle wrote:

This patch is the middle-end piece of an improvement to PRs 101955 and
106245, that adds a missing simplification to the RTL optimizers.
This transformation is to simplify (char)(x << 7) != 0 as x & 1.
Technically, the cast can be any truncation, where shift is by one
less than the narrower type's precision, setting the most significant
(only) bit from the least significant bit.

This transformation applies to any target, but it's easy to see
(and add a new test case) on x86, where the following function:

int f(int a) { return (a << 31) >> 31; }

currently gets compiled with -O2 to:

foo:    movl    %edi, %eax
         sall    $7, %eax
         sarb    $7, %al
         movsbl  %al, %eax
         ret

but with this patch, we now generate the slightly simpler.

foo:    movl    %edi, %eax
         sall    $31, %eax
         sarl    $31, %eax
         ret


This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check with no new failures.  Ok for mainline?


2023-10-10  Roger Sayle  <ro...@nextmovesoftware.com>

gcc/ChangeLog
         PR middle-end/101955
         PR tree-optimization/106245
         * simplify-rtx.c (simplify_relational_operation_1): Simplify
         the RTL (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) to (and:SI x 1).

gcc/testsuite/ChangeLog
         * gcc.target/i386/pr106245-1.c: New test case.
OK.  Thanks!  I must admit, I'm a bit surprised this wasn't already handled.

jeff

Reply via email to