public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-6931] match.pd: Fix up 1 / X for unsigned X optimization [PR104280]
@ 2022-01-29 16:55 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-01-29 16:55 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a1544878966020d1f7a640b35d1f7a5f0e055624

commit r12-6931-ga1544878966020d1f7a640b35d1f7a5f0e055624
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sat Jan 29 17:54:43 2022 +0100

    match.pd: Fix up 1 / X for unsigned X optimization [PR104280]
    
    On Fri, Jan 28, 2022 at 11:38:23AM -0700, Jeff Law wrote:
    > Thanks.  Given the original submission and most of the review work was done
    > prior to stage3 closing, I went ahead and installed this on the trunk.
    
    Unfortunately this breaks quite a lot of things.
    The main problem is that GIMPLE allows EQ_EXPR etc. only with BOOLEAN_TYPE
    or with TYPE_PRECISION == 1 integral type (or vector boolean).
    Violating this causes verification failures in tree-cfg.cc in some cases,
    in other cases wrong-code issues because before it is verified we e.g.
    transform
    1U / x
    into
    x == 1U
    and later into
    x (because we assume that == type must be one of the above cases and
    when it is the same type as the type of the first operand, for boolean-ish
    cases it should be equivalent).
    
    Fixed by changing that
    (eq @1 { build_one_cst (type); })
    into
    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
    Note, I'm not 100% sure if :boolean_type_node is required in that case,
    I see some spots in match.pd that look exactly like this, while there is
    e.g. (convert (le ...)) that supposedly does the right thing too.
    The signed integer 1/X case doesn't need changes changes, for
    (cond (le ...) ...)
    le gets correctly boolean_type_node and cond should use type.
    I've also reformatted it, some lines were too long, match.pd uses
    indentation by 1 column instead of 2 etc.
    
    2022-01-29  Jakub Jelinek  <jakub@redhat.com>
                Andrew Pinski  <apinski@marvell.com>
    
            PR tree-optimization/104279
            PR tree-optimization/104280
            PR tree-optimization/104281
            * match.pd (1 / X -> X == 1 for unsigned X): Build eq with
            boolean_type_node and convert to type.  Formatting fixes.
    
            * gcc.dg/torture/pr104279.c: New test.
            * gcc.dg/torture/pr104280.c: New test.
            * gcc.dg/torture/pr104281.c: New test.

Diff:
---
 gcc/match.pd                            | 28 ++++++++++++++++------------
 gcc/testsuite/gcc.dg/torture/pr104279.c | 12 ++++++++++++
 gcc/testsuite/gcc.dg/torture/pr104280.c | 16 ++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr104281.c | 22 ++++++++++++++++++++++
 4 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 0544ddd91b3..b942cb2930a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -435,18 +435,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && TYPE_UNSIGNED (type))
    (trunc_divmod @0 @1))))
 
- /* 1 / X -> X == 1 for unsigned integer X.
-    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
-    But not for 1 / 0 so that we can get proper warnings and errors,
-    and not for 1-bit integers as they are edge cases better handled elsewhere. */
-(simplify
-  (trunc_div integer_onep@0 @1)
-  (if (INTEGRAL_TYPE_P (type) && !integer_zerop (@1) && TYPE_PRECISION (type) > 1)
-    (if (TYPE_UNSIGNED (type))
-      (eq @1 { build_one_cst (type); })
-      (with { tree utype = unsigned_type_for (type); }
-        (cond (le (plus (convert:utype @1) { build_one_cst (utype); }) { build_int_cst (utype, 2); })
-          @1 { build_zero_cst (type); })))))
+/* 1 / X -> X == 1 for unsigned integer X.
+   1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
+   But not for 1 / 0 so that we can get proper warnings and errors,
+   and not for 1-bit integers as they are edge cases better handled
+   elsewhere.  */
+(simplify
+ (trunc_div integer_onep@0 @1)
+ (if (INTEGRAL_TYPE_P (type)
+      && !integer_zerop (@1)
+      && TYPE_PRECISION (type) > 1)
+  (if (TYPE_UNSIGNED (type))
+   (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
+   (with { tree utype = unsigned_type_for (type); }
+    (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
+	      { build_int_cst (utype, 2); })
+     @1 { build_zero_cst (type); })))))
 
 /* Combine two successive divisions.  Note that combining ceil_div
    and floor_div is trickier and combining round_div even more so.  */
diff --git a/gcc/testsuite/gcc.dg/torture/pr104279.c b/gcc/testsuite/gcc.dg/torture/pr104279.c
new file mode 100644
index 00000000000..7c4fc20b4d6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr104279.c
@@ -0,0 +1,12 @@
+/* PR tree-optimization/104279 */
+/* { dg-do compile } */
+
+unsigned a, b;
+
+int
+main ()
+{
+  b = ~(0 || ~0);
+  a = ~b / ~a;
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr104280.c b/gcc/testsuite/gcc.dg/torture/pr104280.c
new file mode 100644
index 00000000000..c5a1d9bcd27
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr104280.c
@@ -0,0 +1,16 @@
+/* PR tree-optimization/104280 */
+/* { dg-do run } */
+
+int
+foo (unsigned b, int c)
+{
+  return b / c;
+}
+
+int
+main ()
+{
+  if (foo (1, 2) != 0)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr104281.c b/gcc/testsuite/gcc.dg/torture/pr104281.c
new file mode 100644
index 00000000000..35c8e208578
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr104281.c
@@ -0,0 +1,22 @@
+/* PR tree-optimization/104281 */
+/* { dg-do run } */
+
+unsigned a = 1;
+int b, c = 2;
+long d;
+
+int
+main ()
+{
+  while (1)
+    {
+      int m = a;
+    L:
+      a = ~(-(m || b & d));
+      b = ((1 ^ a) / c);
+      if (b)
+	goto L;
+      break;
+    }
+  return 0;
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-29 16:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-29 16:55 [gcc r12-6931] match.pd: Fix up 1 / X for unsigned X optimization [PR104280] Jakub Jelinek

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