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. gcc/testsuite/ChangeLog * gcc.dg/fold-ctz-1.c: New test case. Thanks in advance, Roger --
diff --git a/gcc/match.pd b/gcc/match.pd index 6818856..d6d61eb 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -9056,6 +9056,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* CTZ simplifications. */ (for ctz (CTZ) + /* ctz (-x) => ctz (x). */ + (simplify + (ctz (nop_convert?@0 (negate @1))) + (with { tree t = TREE_TYPE (@0); } + (ctz (convert:t @1)))) (for op (ge gt le lt) cmp (eq eq ne ne) (simplify diff --git a/gcc/testsuite/gcc.dg/fold-ctz-1.c b/gcc/testsuite/gcc.dg/fold-ctz-1.c new file mode 100644 index 0000000..dcc444c --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-ctz-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int foo(int x) +{ + return __builtin_ctz (-x); +} + +/* { dg-final { scan-tree-dump-not "-x_" "optimized"} } */