[Bug middle-end/124630] popcnt (a ^ (a - 1)) -1 is ctz when a is known non-zero

liuhongt at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Mar 25 09:15:25 GMT 2026


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124630

Hongtao Liu <liuhongt at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|enhancement                 |normal

--- Comment #1 from Hongtao Liu <liuhongt at gcc dot gnu.org> ---
+/* popcount (X ^ (X - 1)) - 1 is CTZ (X) when X is nonzero.  */
+(simplify
+  (minus (POPCOUNT (bit_xor:c tree_expr_nonzero_p@0
+                             (plus @0 integer_minus_onep)))
+        integer_onep)
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
+                                         OPTIMIZE_FOR_SPEED))
+   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
+    (CTZ:type (convert:utype @0)))))


More information about the Gcc-bugs mailing list