https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109393
--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Philipp Tomsich <ptoms...@gcc.gnu.org>: https://gcc.gnu.org/g:08b8341f209be7c7e301853bdbbcad4f8e1695f5 commit r15-3862-g08b8341f209be7c7e301853bdbbcad4f8e1695f5 Author: Konstantinos Eleftheriou <konstantinos.elefther...@vrull.eu> Date: Thu Sep 5 15:59:59 2024 +0200 match: Change (A * B) + (-C) to (B - C/A) * A, if C multiple of A [PR109393] The following function: int foo(int *a, int j) { int k = j - 1; return a[j - 1] == a[k]; } does not fold to `return 1;` using -O2 or higher. The cause of this is that the expression `4 * j + (-4)` for the index computation is not folded to `4 * (j - 1)`. Existing simplifications that handle similar cases are applied when A == C, which is not the case in this instance. A previous attempt to address this issue is https://gcc.gnu.org/pipermail/gcc-patches/2024-April/649896.html This patch adds the following simplification in match.pd: (A * B) + (-C) -> (B - C/A) * A, if C a multiple of A which also handles cases where the index is j - 2, j - 3, etc. Bootstrapped for all languages and regression tested on x86-64 and aarch64. PR tree-optimization/109393 gcc/ChangeLog: * match.pd: (A * B) + (-C) -> (B - C/A) * A, if C a multiple of A. gcc/testsuite/ChangeLog: * gcc.dg/pr109393.c: New test. Tested-by: Christoph Müllner <christoph.muell...@vrull.eu> Signed-off-by: Philipp Tomsich <philipp.toms...@vrull.eu> Signed-off-by: Konstantinos Eleftheriou <konstantinos.elefther...@vrull.eu>