From 839d0860cf808e7457a5e57d745830023d57998b Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Fri, 3 May 2019 10:46:13 +0000 Subject: [PATCH] re PR tree-optimization/89518 (missed optimisation for array address calculations) 2019-05-03 Richard Biener PR middle-end/89518 * match.pd: Add pattern to optimize (A / B) * B + (A % B) to A. * gcc.dg/pr89518.c: New testcase. From-SVN: r270846 --- gcc/ChangeLog | 5 +++++ gcc/match.pd | 7 +++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr89518.c | 14 ++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr89518.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index e9def2fc5d7..d57cd7c034e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2019-05-03 Richard Biener + + PR middle-end/89518 + * match.pd: Add pattern to optimize (A / B) * B + (A % B) to A. + 2019-05-03 Richard Biener PR middle-end/87314 diff --git a/gcc/match.pd b/gcc/match.pd index ec03a680e33..5e4a4dc51a2 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2736,6 +2736,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (mult (convert1? (exact_div @0 @@1)) (convert2? @1)) (convert @0)) +/* Simplify (A / B) * B + (A % B) -> A. */ +(for div (trunc_div ceil_div floor_div round_div) + mod (trunc_mod ceil_mod floor_mod round_mod) + (simplify + (plus:c (mult:c (div @0 @1) @1) (mod @0 @1)) + @0)) + /* ((X /[ex] A) +- B) * A --> X +- A * B. */ (for op (plus minus) (simplify diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0e23e5741d5..74a33bec11d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-05-03 Richard Biener + + PR middle-end/89518 + * gcc.dg/pr89518.c: New testcase. + 2019-05-03 Richard Biener PR middle-end/87314 diff --git a/gcc/testsuite/gcc.dg/pr89518.c b/gcc/testsuite/gcc.dg/pr89518.c new file mode 100644 index 00000000000..5b289c20543 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr89518.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-original" } */ + +unsigned foo (unsigned a, unsigned b) +{ + return a/b * b + a%b; +} + +int bar (int a, int b) +{ + return a/b * b + a%b; +} + +/* { dg-final { scan-tree-dump-times "return a;" 2 "original" } } */ -- 2.30.2