]> gcc.gnu.org Git - gcc.git/commitdiff
Improve DSE to handle stores before __builtin_unreachable ()
authorRichard Biener <rguenther@suse.de>
Mon, 19 Jun 2023 12:19:47 +0000 (14:19 +0200)
committerRichard Biener <rguenther@suse.de>
Tue, 20 Jun 2023 10:48:24 +0000 (12:48 +0200)
DSE isn't good at identifying program points that end lifetime
of variables that are not associated with virtual operands.  But
at least for those that end basic-blocks we can handle the simple
case where this ending is in the same basic-block as the definition
we want to elide.  That should catch quite some common cases already.

* tree-ssa-dse.cc (dse_classify_store): When we found
no defs and the basic-block with the original definition
ends in __builtin_unreachable[_trap] the store is dead.

* gcc.dg/tree-ssa/ssa-dse-47.c: New testcase.
* c-c++-common/asan/pr106558.c: Avoid undefined behavior
due to missing return.

gcc/testsuite/c-c++-common/asan/pr106558.c
gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c [new file with mode: 0644]
gcc/tree-ssa-dse.cc

index d82b2dc7a83d9bf9c414b7cad1726cca1757b273..c8cefdf09ff2bdac28a8540ebea3da289c776715 100644 (file)
@@ -8,7 +8,7 @@ int **c = &b;
 int d[1];
 int *e = &d[1];
 
-static int f(int *g) {
+static void f(int *g) {
   *b = e;
   *c = e;
   *b = 2;
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-47.c
new file mode 100644 (file)
index 0000000..659f1d0
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-dse1-details" } */
+
+int a;
+int b[3];
+void test()
+{
+  if (a > 0)
+    {
+      b[0] = 0;
+      b[1] = 1;
+      b[2] = 2;
+      __builtin_unreachable ();
+    }
+}
+
+/* { dg-final { scan-tree-dump-times "Deleted dead store" 3 "dse1" } } */
index eabe8ba452299bc65f82bb3a320e00158dbadaa0..3c7a2e9992d767afb2d748e08e1cc27ec50f7cd8 100644 (file)
@@ -1118,7 +1118,26 @@ dse_classify_store (ao_ref *ref, gimple *stmt,
       if (defs.is_empty ())
        {
          if (ref_may_alias_global_p (ref, false))
-           return DSE_STORE_LIVE;
+           {
+             basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (defvar));
+             /* Assume that BUILT_IN_UNREACHABLE and BUILT_IN_UNREACHABLE_TRAP
+                do not need to keep (global) memory side-effects live.
+                We do not have virtual operands on BUILT_IN_UNREACHABLE
+                but we can do poor mans reachability when the last
+                definition we want to elide is in the block that ends
+                in such a call.  */
+             if (EDGE_COUNT (def_bb->succs) == 0)
+               if (gcall *last = dyn_cast <gcall *> (*gsi_last_bb (def_bb)))
+                 if (gimple_call_builtin_p (last, BUILT_IN_UNREACHABLE)
+                     || gimple_call_builtin_p (last,
+                                               BUILT_IN_UNREACHABLE_TRAP))
+                   {
+                     if (by_clobber_p)
+                       *by_clobber_p = false;
+                     return DSE_STORE_DEAD;
+                   }
+             return DSE_STORE_LIVE;
+           }
 
          if (by_clobber_p)
            *by_clobber_p = false;
This page took 0.085962 seconds and 5 git commands to generate.