@@ -5949,6 +5949,32 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(cmp (popcount @0) integer_zerop)
(rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
+/* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
+(for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
+ BUILT_IN_POPCOUNTIMAX)
+ parity (BUILT_IN_PARITY BUILT_IN_PARITYL BUILT_IN_PARITYLL
+ BUILT_IN_PARITYIMAX)
+ (simplify
+ (bit_and (popcount @0) integer_onep)
+ (parity @0)))
+
+/* PARITY simplifications. */
+(for parity (BUILT_IN_PARITY BUILT_IN_PARITYL BUILT_IN_PARITYLL
+ BUILT_IN_PARITYIMAX)
+ /* parity(~X) is parity(X). */
+ (simplify
+ (parity (bit_not @0))
+ (parity @0))
+ /* parity(X&1) is nop_expr(X&1). */
+ (simplify
+ (parity @0)
+ (if (tree_nonzero_bits (@0) == 1)
+ (convert @0)))
+ /* parity(X)^parity(Y) is parity(X^Y). */
+ (simplify
+ (bit_xor (parity:s @0) (parity:s @1))
+ (parity (bit_xor @0 @1))))
+
#if GIMPLE
/* 64- and 32-bits branchless implementations of popcount are detected: