public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] middle-end/69482 - not preserving volatile accesses
Date: Mon, 9 Jan 2023 13:31:25 +0100 (CET)	[thread overview]
Message-ID: <20230109123126.2C9BA13583@imap2.suse-dmz.suse.de> (raw)

The following addresses a long standing issue with not preserving
accesses to non-volatile objects through volatile qualified
pointers in the case that object gets expanded to a register.  The
fix is to treat accesses to an object with a volatile qualified
access as forcing that object to memory.  This issue got more
exposed recently so it regressed more since GCC 11.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed to trunk
sofar.

	PR middle-end/69482
	* cfgexpand.cc (discover_nonconstant_array_refs_r): Volatile
	qualified accesses also force objects to memory.

	* gcc.target/i386/pr69482-1.c: New testcase.
	* gcc.target/i386/pr69482-2.c: Likewise.
---
 gcc/cfgexpand.cc                          |  9 +++++++++
 gcc/testsuite/gcc.target/i386/pr69482-1.c | 16 ++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr69482-2.c | 10 ++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr69482-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr69482-2.c

diff --git a/gcc/cfgexpand.cc b/gcc/cfgexpand.cc
index 86783a6b661..25b1558dcb9 100644
--- a/gcc/cfgexpand.cc
+++ b/gcc/cfgexpand.cc
@@ -6291,6 +6291,15 @@ discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
 
   if (IS_TYPE_OR_DECL_P (t))
     *walk_subtrees = 0;
+  else if (REFERENCE_CLASS_P (t) && TREE_THIS_VOLATILE (t))
+    {
+      t = get_base_address (t);
+      if (t && DECL_P (t)
+	  && DECL_MODE (t) != BLKmode
+	  && !TREE_ADDRESSABLE (t))
+	bitmap_set_bit (forced_stack_vars, DECL_UID (t));
+      *walk_subtrees = 0;
+    }
   else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
     {
       while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
diff --git a/gcc/testsuite/gcc.target/i386/pr69482-1.c b/gcc/testsuite/gcc.target/i386/pr69482-1.c
new file mode 100644
index 00000000000..f192261b104
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr69482-1.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O3" } */
+
+static inline void memset_s(void* s, int n) {
+  volatile unsigned char * p = s;
+  for(int i = 0; i < n; ++i) {
+    p[i] = 0;
+  }
+}
+
+void test() {
+  unsigned char x[4];
+  memset_s(x, sizeof x);
+}
+
+/* { dg-final { scan-assembler-times "mov" 4 } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr69482-2.c b/gcc/testsuite/gcc.target/i386/pr69482-2.c
new file mode 100644
index 00000000000..58e89a79333
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr69482-2.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+void bar ()
+{
+  int j;
+  *(volatile int *)&j = 0;
+}
+
+/* { dg-final { scan-assembler-times "mov" 1 } } */
-- 
2.35.3

                 reply	other threads:[~2023-01-09 12:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230109123126.2C9BA13583@imap2.suse-dmz.suse.de \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).