public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add divide by zero side effect.
@ 2022-05-17 18:39 Andrew MacLeod
  2022-05-18  6:28 ` Richard Biener
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Andrew MacLeod @ 2022-05-17 18:39 UTC (permalink / raw)
  To: gcc-patches

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

I haven't checked this patch in yet.  This implements a side effect that 
the divisor cannot be 0 after a divide executes. This allows us to fold 
the divide away:

a = b / c;
if (c == 0)
   dead();

This bootstraps on x86_64-pc-linux-gnu with no regressions, but I first 
wanted to check to see if there are some flags or conditions that should 
e checked in order NOT to do this optimization.  I am guessing there is 
probably something :-)    Anyway, this is how we straightforwardly add 
side effects now.

Does the patch conditions need tweaking to apply the side effect?

Andrew


[-- Attachment #2: zero.diff --]
[-- Type: text/x-patch, Size: 1950 bytes --]

commit 3bbcccf2ddd4d50cc5febf630bd8b55a45688352
Author: Andrew MacLeod <amacleod@redhat.com>
Date:   Mon Apr 4 16:13:57 2022 -0400

    Add divide by zero side effect.
    
    After a divide, we know the divisor is not zero.
    
            gcc/
            * gimple-range-side-effect.cc (stmt_side_effects::stmt_side_effects):
            Add support for all divides.
    
            gcc/testsuite/
            * gcc.dg/tree-ssa/evrp-zero.c: New.

diff --git a/gcc/gimple-range-side-effect.cc b/gcc/gimple-range-side-effect.cc
index 2c8c77dc569..548e4bea313 100644
--- a/gcc/gimple-range-side-effect.cc
+++ b/gcc/gimple-range-side-effect.cc
@@ -116,6 +116,23 @@ stmt_side_effects::stmt_side_effects (gimple *s)
     walk_stmt_load_store_ops (s, (void *)this, non_null_loadstore,
 			      non_null_loadstore);
 
+  if (is_a<gassign *> (s))
+    {
+      switch (gimple_assign_rhs_code (s))
+	{
+	case TRUNC_DIV_EXPR:
+	case CEIL_DIV_EXPR:
+	case FLOOR_DIV_EXPR:
+	case ROUND_DIV_EXPR:
+	case EXACT_DIV_EXPR:
+	  // Divide means operand 2 is not zero after this stmt.
+	  if (gimple_range_ssa_p (gimple_assign_rhs2 (s)))
+	    add_nonzero (gimple_assign_rhs2 (s));
+	  break;
+	default:
+	  break;
+	}
+    }
 }
 
 // -------------------------------------------------------------------------
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/evrp-zero.c b/gcc/testsuite/gcc.dg/tree-ssa/evrp-zero.c
new file mode 100644
index 00000000000..2b76e449c9b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/evrp-zero.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-evrp " } */
+
+/* Side effects of divide are that the divisor cannot be 0. */
+
+void dead (int);
+
+void
+f1 (int a, int c) {
+  int b = a;
+  if (a / c > 10)
+    b = c;
+
+  if (c == 0)
+    dead (b);
+}
+
+
+void
+f2 (int a, int c) {
+  int nz = c == 0;
+  int b = a / c;
+  if (nz)
+    dead (0);
+}
+
+
+/* { dg-final { scan-tree-dump-not "dead" "evrp" } } */

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

end of thread, other threads:[~2022-05-30 12:24 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-17 18:39 [PATCH] Add divide by zero side effect Andrew MacLeod
2022-05-18  6:28 ` Richard Biener
2022-05-18 13:20   ` Andrew MacLeod
2022-05-18 18:13 ` Segher Boessenkool
2022-05-18 20:24   ` Andrew MacLeod
2022-05-18 20:40     ` Segher Boessenkool
2022-05-19 13:22       ` Andrew MacLeod
2022-05-19 22:23         ` Segher Boessenkool
2022-05-20  2:14           ` Andrew MacLeod
2022-05-20  6:25             ` Richard Biener
2022-05-20  6:38               ` Alexander Monakov
2022-05-20  8:04                 ` Richard Biener
2022-05-20  8:17                   ` Alexander Monakov
2022-05-20 11:49                     ` Richard Biener
2022-05-22 18:55                       ` Alexander Monakov
2022-05-20  8:11                 ` Eric Botcazou
2022-05-20 14:39                   ` Segher Boessenkool
2022-05-20 19:18                     ` Bernhard Reutner-Fischer
2022-05-25 14:35                       ` [COMMITTED] Use infer instead of side-effect for ranges Andrew MacLeod
2022-05-25 16:29                         ` Segher Boessenkool
2022-05-20  8:13               ` [PATCH] Add divide by zero side effect Eric Botcazou
2022-05-20 12:09     ` Alexandre Oliva
2022-05-27 19:33 ` Andi Kleen
2022-05-27 19:56   ` Andrew MacLeod
2022-05-28 11:52     ` Eric Gallager
2022-05-30 12:24   ` 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).