* [PATCH] Fix PR89518
@ 2019-05-03 10:45 Richard Biener
0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2019-05-03 10:45 UTC (permalink / raw)
To: gcc-patches
Bootstrapped and tested on x86_64-unknown-linux-gnu, applied.
Richard.
2019-05-03 Richard Biener <rguenther@suse.de>
PR middle-end/89518
* match.pd: Add pattern to optimize (A / B) * B + (A % B) to A.
* gcc.dg/pr89518.c: New testcase.
Index: gcc/match.pd
===================================================================
--- gcc/match.pd (revision 269242)
+++ gcc/match.pd (working copy)
@@ -2729,6 +2729,13 @@ (define_operator_list COND_TERNARY
(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
Index: gcc/testsuite/gcc.dg/pr89518.c
===================================================================
--- gcc/testsuite/gcc.dg/pr89518.c (nonexistent)
+++ gcc/testsuite/gcc.dg/pr89518.c (working copy)
@@ -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" } } */
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2019-05-03 10:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-03 10:45 [PATCH] Fix PR89518 Richard Biener
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).