public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR tree-optimization/102232 Adding a missing pattern to match.pd
@ 2021-11-09  4:11 Navid Rahimi
  2021-11-09  4:24 ` Navid Rahimi
  2021-11-09 10:36 ` Richard Biener
  0 siblings, 2 replies; 7+ messages in thread
From: Navid Rahimi @ 2021-11-09  4:11 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 582 bytes --]

Hi GCC community,

This patch will add the missed pattern described in bug 102232 [1] to the match.pd. The testcase will test whether the multiplication and division has been removed from the code or not. The correctness proof for this pattern is here [2] in case anyone is curious.

PR tree-optimization/102232
      * match.pd (x * (1 + y / x) - y) -> (x - y % x): New optimization.
      * gcc.dg/tree-ssa/pr102232.c: testcase for this optimization.


1) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102232
2) https://alive2.llvm.org/ce/z/2VScjD

Best wishes,
Navid.

[-- Attachment #2: 0001-PR-tree-optimization-102232.patch --]
[-- Type: application/octet-stream, Size: 2454 bytes --]

From 20dcbad29f4d41cce09ac799e9b5093167f3aaa0 Mon Sep 17 00:00:00 2001
From: Navid Rahimi <navidrahimi@microsoft.com>
Date: Mon, 8 Nov 2021 13:57:19 -0800
Subject: [PATCH] PR tree-optimization/102232

	* match.pd (x * (1 + y / x) - y) -> (x - y % x): New optimization.
	* gcc.dg/tree-ssa/pr102232.c: testcase for this optimization.
---
 gcc/match.pd                             |  7 ++++
 gcc/testsuite/gcc.dg/tree-ssa/pr102232.c | 52 ++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr102232.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 71cf6f9df0a..37c01e79d97 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1295,6 +1295,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
  (bit_xor @0 @1))
 
+/* x * (1 + y / x) - y -> x - y % x */
+(simplify
+ (minus (mult:cs @0 (plus:cs integer_onep (trunc_div:s @1 @0))) @1)
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+      && types_match (@0, @1))
+  (minus @0 (trunc_mod @1 @0))))
+
 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
 (simplify
  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr102232.c b/gcc/testsuite/gcc.dg/tree-ssa/pr102232.c
new file mode 100644
index 00000000000..63fe37c0936
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr102232.c
@@ -0,0 +1,52 @@
+/* PR tree-optimization/102232 */
+/* { dg-do run } */
+/* { dg-options "-O3 -fdump-tree-optimized" } */
+
+int __attribute__ ((noipa)) foo (int a, int b)
+{
+  return b * (1 + a / b) - a;
+}
+
+int
+main (void)
+{
+  // few randomly generated test cases
+  if (foo (71856034, 238) != 212)
+    {
+      return 1;
+    }
+  if (foo (71856034, 10909) != 1549)
+    {
+      return 1;
+    }
+  if (foo (20350, 1744) != 578)
+    {
+      return 1;
+    }
+  if (foo (444813, 88563) != 86565)
+    {
+      return 1;
+    }
+  if (foo (112237, 63004) != 13771)
+    {
+      return 1;
+    }
+  if (foo (68268386, 787116) != 210706)
+    {
+      return 1;
+    }
+  if (foo (-444813, 88563) != 90561)
+    {
+      return 1;
+    }
+  if (foo (-68268386, 787116) != 1363526)
+    {
+      return 1;
+    }
+
+  return 0;
+}
+
+/* Verify that multiplication and division has been removed.  */
+/* { dg-final { scan-tree-dump-not " \\* " "optimized" } } */
+/* { dg-final { scan-tree-dump-not " / " "optimized" } } */
\ No newline at end of file
-- 
2.25.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-11-23  3:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-09  4:11 [PATCH] PR tree-optimization/102232 Adding a missing pattern to match.pd Navid Rahimi
2021-11-09  4:24 ` Navid Rahimi
2021-11-09 10:36 ` Richard Biener
2021-11-09 16:25   ` [EXTERNAL] " Navid Rahimi
2021-11-10  8:35     ` Richard Biener
2021-11-23  3:09       ` Jeff Law
2021-11-23  3:10         ` Navid Rahimi

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).