On Tue, Jul 23, 2024 at 9:30 AM Roger Sayle <ro...@nextmovesoftware.com> wrote:
>
>
> The subject line pretty much says it all; the count-trailing-zeros function
> of -X produces the same result as count-trailing-zeros of X.  This
> transformation eliminates a negation which may potentially overflow with
> an equivalent expression that doesn't [much like the analogous abs(-X)
> simplification in match.pd].  Likewise, the undefined at zero remains
> undefined.
>
> I'd noticed this equivalence, which isn't mentioned in Hacker's Delight,
> investigating whether ranger's non_zero_bits can help determine whether
> an integer variable may be converted to a floating point type exactly
> (without raising FE_INEXACT), but it turns out this observation isn't
> novel, as (disappointingly) LLVM already performs this same folding.
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check, both with and without --target_board=unix{-m32}
> with no new failures.  Ok for mainline?
>
> 2024-07-23  Roger Sayle  <ro...@nextmovesoftware.com>
>
> gcc/ChangeLog
>         * match.pd (ctz (-X) => ctz (X)): New simplification.
```
int f1(int a)
{
        return __builtin_ctz(__builtin_abs(a));
}
```
Should also be handled. Though this might be good to handle in the
backprop pass (gimple-ssa-backprop.cc) which handles sign changes like
this already.

Thanks,
Andrew Pinski

>
> gcc/testsuite/ChangeLog
>         * gcc.dg/fold-ctz-1.c: New test case.
>
>
> Thanks in advance,
> Roger
> --
>

Reply via email to