public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] Fix PR89518
Date: Fri, 03 May 2019 10:45:00 -0000	[thread overview]
Message-ID: <alpine.LSU.2.20.1905031244570.10704@zhemvz.fhfr.qr> (raw)


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" } } */

                 reply	other threads:[~2019-05-03 10:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LSU.2.20.1905031244570.10704@zhemvz.fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).