Skip to content
Snippets Groups Projects
Commit 08b8341f authored by Konstantinos Eleftheriou's avatar Konstantinos Eleftheriou Committed by Philipp Tomsich
Browse files

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: default avatarChristoph Müllner <christoph.muellner@vrull.eu>
Signed-off-by: default avatarPhilipp Tomsich <philipp.tomsich@vrull.eu>
Signed-off-by: default avatarKonstantinos Eleftheriou <konstantinos.eleftheriou@vrull.eu>
parent af8ff004
No related branches found
No related tags found
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment