]> git.ipfire.org Git - thirdparty/gcc.git/commit
match.pd: Fold (x + y) >> 1 into IFN_AVG_FLOOR (x, y) for vectors
authorPengfei Li <Pengfei.Li2@arm.com>
Wed, 4 Jun 2025 15:59:44 +0000 (16:59 +0100)
committerTamar Christina <tamar.christina@arm.com>
Wed, 4 Jun 2025 16:01:02 +0000 (17:01 +0100)
commit7bb1933c1fcca8cb99132ff5d08b3f3efca4ff07
tree7e477131b4a1da720a17d8fdc2a375913337c71b
parent54e4f75b7d57e02bba79cd67332bbfdc37d1b321
match.pd: Fold (x + y) >> 1 into IFN_AVG_FLOOR (x, y) for vectors

This patch folds vector expressions of the form (x + y) >> 1 into
IFN_AVG_FLOOR (x, y), reducing instruction count on platforms that
support averaging operations. For example, it can help improve the
codegen on AArch64 from:
        add     v0.4s, v0.4s, v31.4s
        ushr    v0.4s, v0.4s, 1
to:
        uhadd   v0.4s, v0.4s, v31.4s

As this folding is only valid when the most significant bit of each
element in both x and y is known to be zero, this patch checks leading
zero bits of elements in x and y, and extends get_nonzero_bits_1() to
handle uniform vectors. When the input is a uniform vector, the function
now returns the nonzero bits of its element.

Additionally, this patch adds more checks to reject vector types in bit
constant propagation (tree-bit-ccp), since tree-bit-ccp was designed for
scalar values only, and the new vector logic in get_non_zero_bits_1()
could lead to incorrect propagation results.

gcc/ChangeLog:

* match.pd: Add folding rule for vector average.
* tree-ssa-ccp.cc (get_default_value): Reject vector types.
(evaluate_stmt): Reject vector types.
* tree-ssanames.cc (get_nonzero_bits_1): Extend to handle
uniform vectors.

gcc/testsuite/ChangeLog:

* gcc.target/aarch64/acle/uhadd_1.c: New test.
gcc/match.pd
gcc/testsuite/gcc.target/aarch64/acle/uhadd_1.c [new file with mode: 0644]
gcc/tree-ssa-ccp.cc
gcc/tree-ssanames.cc