Prevent -Og from deleting stores to write-only variables
authorRichard Sandiford <richard.sandiford@arm.com>
Mon, 29 Jul 2019 08:46:54 +0000 (08:46 +0000)
committerRichard Sandiford <rsandifo@gcc.gnu.org>
Mon, 29 Jul 2019 08:46:54 +0000 (08:46 +0000)
This patch prevents -Og from deleting stores to write-only variables,
so that the values are still available when debugging.  This seems
more convenient than forcing users to use __attribute__((used))
(probably conditionally, if it's not something they want in release
builds).

2019-07-29  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
* tree-cfg.c (execute_fixup_cfg): Don't delete stores to write-only
variables for -Og.

gcc/testsuite/
* c-c++-common/guality/Og-static-wo-1.c: New test.
* g++.dg/guality/guality.exp: Separate the c-c++-common tests into
"Og" and "general" tests.  Run the latter at -O0 and -Og only.
* gcc.dg/guality/guality.exp: Likewise.

From-SVN: r273870

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/guality/Og-static-wo-1.c [new file with mode: 0644]
gcc/testsuite/g++.dg/guality/guality.exp
gcc/testsuite/gcc.dg/guality/guality.exp
gcc/tree-cfg.c

index 650201a640c1165b6b0be379cbb093803ce05599..3eb52020e68295c91ab2effa8c1966c456e61405 100644 (file)
@@ -1,3 +1,8 @@
+2019-07-29  Richard Sandiford  <richard.sandiford@arm.com>
+
+       * tree-cfg.c (execute_fixup_cfg): Don't delete stores to write-only
+       variables for -Og.
+
 2019-07-29  Richard Sandiford  <richard.sandiford@arm.com>
 
        * doc/sourcebuild.texi (check-function-bodies): Document.
index f896434fc0bc9978e97ab6627001c7f77ba3c610..1dd2819e9e842925c361feca09fad892a2fe5f1b 100644 (file)
@@ -1,3 +1,10 @@
+2019-07-29  Richard Sandiford  <richard.sandiford@arm.com>
+
+       * c-c++-common/guality/Og-static-wo-1.c: New test.
+       * g++.dg/guality/guality.exp: Separate the c-c++-common tests into
+       "Og" and "general" tests.  Run the latter at -O0 and -Og only.
+       * gcc.dg/guality/guality.exp: Likewise.
+
 2019-07-29  Richard Sandiford  <richard.sandiford@arm.com>
 
        * lib/scanasm.exp (parse_function_bodies, check_function_body)
diff --git a/gcc/testsuite/c-c++-common/guality/Og-static-wo-1.c b/gcc/testsuite/c-c++-common/guality/Og-static-wo-1.c
new file mode 100644 (file)
index 0000000..a4c7f30
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do run } */
+/* { dg-options "-g" } */
+
+#include "../../gcc.dg/nop.h"
+
+static int x = 0;
+
+int
+main (void)
+{
+  asm volatile (NOP);          /* { dg-final { gdb-test . "x" "0" } } */
+  x = 1;
+  asm volatile (NOP);          /* { dg-final { gdb-test . "x" "1" } } */
+  return 0;
+}
index 757b20b61e24e8df263ec708daade879e77a05bc..33571f1f28fec92a421fd93550fbebecf18f6774 100644 (file)
@@ -65,8 +65,22 @@ if {[check_guality "
     return 0;
   }
 "]} {
-  gcc-dg-runtest [lsort [glob $srcdir/$subdir/*.C]] "" ""
-  gcc-dg-runtest [lsort [glob $srcdir/c-c++-common/guality/*.c]] "" ""
+    set general [list]
+    set Og [list]
+    foreach file [lsort [glob $srcdir/c-c++-common/guality/*.c]] {
+       switch -glob -- [file tail $file] {
+           Og-* { lappend Og $file }
+           * { lappend general $file }
+       }
+    }
+
+    gcc-dg-runtest [lsort [glob $srcdir/$subdir/*.C]] "" ""
+    gcc-dg-runtest $general "" ""
+    set-torture-options \
+       [list "-O0" "-Og"] \
+       [list {}] \
+       [list "-Og -flto"]
+    gcc-dg-runtest $Og "" ""
 }
 
 if [info exists guality_gdb_name] {
index ca77a446f86be97bf5e482eb4eafeb8b95c2f071..89cd896d05c3457fa8cca7f9be490bf96a8f1a07 100644 (file)
@@ -80,8 +80,22 @@ if {[check_guality "
     return 0;
   }
 "]} {
-  gcc-dg-runtest [lsort [glob $srcdir/$subdir/*.c]] "" ""
-  gcc-dg-runtest [lsort [glob $srcdir/c-c++-common/guality/*.c]] "" "-Wc++-compat"
+    set general [list]
+    set Og [list]
+    foreach file [lsort [glob $srcdir/c-c++-common/guality/*.c]] {
+       switch -glob -- [file tail $file] {
+           Og-* { lappend Og $file }
+           * { lappend general $file }
+       }
+    }
+
+    gcc-dg-runtest [lsort [glob $srcdir/$subdir/*.c]] "" ""
+    gcc-dg-runtest $general "" "-Wc++-compat"
+    set-torture-options \
+       [list "-O0" "-Og"] \
+       [list {}] \
+       [list "-Og -flto"]
+    gcc-dg-runtest $Og "" "-Wc++-compat"
 }
 
 if [info exists guality_gdb_name] {
index 54ee63add4707441b565526875bcabdc6cda4166..2648663204b8b4cbeb339c22816b07b17b7acf82 100644 (file)
@@ -9579,7 +9579,8 @@ execute_fixup_cfg (void)
             Keep access when store has side effect, i.e. in case when source
             is volatile.  */
          if (gimple_store_p (stmt)
-             && !gimple_has_side_effects (stmt))
+             && !gimple_has_side_effects (stmt)
+             && !optimize_debug)
            {
              tree lhs = get_base_address (gimple_get_lhs (stmt));