https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116815
--- Comment #13 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Dhruv Chawla <[email protected]>: https://gcc.gnu.org/g:3c378398111f7fc3c026b705e3ac088b27d4c307 commit r16-5611-g3c378398111f7fc3c026b705e3ac088b27d4c307 Author: Dhruv Chawla <[email protected]> Date: Wed Jul 23 01:41:51 2025 -0700 Make better use of overflowing operations in max/min(a, add/sub(a, b)) [PR116815] This patch folds the following patterns: - For add: - umax (a, add (a, b)) -> [sum, ovf] = adds (a, b); !ovf ? sum : a - umin (a, add (a, b)) -> [sum, ovf] = adds (a, b); !ovf ? a : sum ... along with the commutated versions: - umax (a, add (b, a)) -> [sum, ovf] = adds (b, a); !ovf ? sum : a - umin (a, add (b, a)) -> [sum, ovf] = adds (b, a); !ovf ? a : sum - For sub: - umax (a, sub (a, b)) -> [diff, udf] = subs (a, b); udf ? diff : a - umin (a, sub (a, b)) -> [diff, udf] = subs (a, b); udf ? a : diff Where ovf is the overflow flag and udf is the underflow flag. adds and subs are generated by generating parallel compare+plus/minus which map to add<mode>3_compareC and sub<mode>3_compare1. This patch is a respin of the patch posted at https://gcc.gnu.org/pipermail/gcc-patches/2025-May/685021.html as per the suggestion to turn it into a target-specific transform by Richard Biener. FIXME: This pattern cannot currently factor multiple occurences of the add expression into a single adds, eg: max (a, a + b) + min (a + b, b) ends up generating two adds instructions. This is something that was lost when going from GIMPLE to target-specific transforms. Bootstrapped and regtested on aarch64-unknown-linux-gnu. Signed-off-by: Dhruv Chawla <[email protected]> PR middle-end/116815 gcc/ChangeLog: * config/aarch64/aarch64.md (*aarch64_plus_within_<optab><mode>3_<ovf_commutate>): New pattern. (*aarch64_minus_within_<optab><mode>3): Likewise. * config/aarch64/iterators.md (ovf_add_cmp): New code attribute. (udf_sub_cmp): Likewise. (UMAXMIN): New code iterator. (ovf_commutate): New iterator. (ovf_comm_opp): New int attribute. gcc/testsuite/ChangeLog: * gcc.target/aarch64/pr116815-1.c: New test. * gcc.target/aarch64/pr116815-2.c: Likewise. * gcc.target/aarch64/pr116815-3.c: Likewise.
