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/107115 - avoid bogus redundant store removal during RTL expansion
Date: Thu, 6 Oct 2022 12:19:53 +0000 (UTC)	[thread overview]
Message-ID: <20221006121953.lV9xAlAB5PyR-m3G9jszS7nGoRO09JeR65QI3O6NBBk@z> (raw)

The following preserves the (premature) redundant store removal
done in store_expr by appropriately guarding it with
mems_same_for_tbaa_p.  The testcase added needs scheduling disabled
for now since there's a similar bug there still present.

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

	PR middle-end/107115
	* expr.cc (store_expr): Check mems_same_for_tbaa_p before
	eliding a seemingly redundant store.

	* gcc.dg/torture/pr107115.c: New testcase.
---
 gcc/expr.cc                             |  4 ++-
 gcc/testsuite/gcc.dg/torture/pr107115.c | 37 +++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr107115.c

diff --git a/gcc/expr.cc b/gcc/expr.cc
index 80bb1b8a4c5..ba627f176a7 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -6207,7 +6207,9 @@ store_expr (tree exp, rtx target, int call_param_p,
 
   if ((! rtx_equal_p (temp, target)
        || (temp != target && (side_effects_p (temp)
-			      || side_effects_p (target))))
+			      || side_effects_p (target)
+			      || (MEM_P (temp)
+				  && !mems_same_for_tbaa_p (temp, target)))))
       && TREE_CODE (exp) != ERROR_MARK
       /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
 	 but TARGET is not valid memory reference, TEMP will differ
diff --git a/gcc/testsuite/gcc.dg/torture/pr107115.c b/gcc/testsuite/gcc.dg/torture/pr107115.c
new file mode 100644
index 00000000000..5f7b6ff01e1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr107115.c
@@ -0,0 +1,37 @@
+/* { dg-do run } */
+/* PR/107115 */
+/* { dg-additional-options "-fno-schedule-insns -fno-schedule-insns2" } */
+
+#include <stdlib.h>
+
+void test1(long *p1)
+{
+  p1[0] = 1;
+}
+long test2(long long *p2, int index1, int index2)
+{
+  p2[index1] = 2;
+  return p2[index2];
+}
+long test3(long *p3, int index2, long value)
+{
+  p3[index2] = 3;
+  p3[index2] = value;
+  return p3[0];
+}
+long test4(void *p4, int index1, int index2)
+{
+  test1(p4);
+  long temp = test2(p4, index1, index2);
+  return test3(p4, index2, temp);
+}
+long (*volatile vtest)(void *, int, int) = test4;
+int main(void)
+{
+  void *pp = malloc(sizeof (long) + sizeof(long long));
+  if (!pp) abort();
+  long result = vtest(pp, 0, 0);
+  if (*(long *)pp != 2 || result != 2)
+    __builtin_abort ();
+  return 0;
+}
-- 
2.35.3

                 reply	other threads:[~2022-10-06 12:19 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=20221006121953.lV9xAlAB5PyR-m3G9jszS7nGoRO09JeR65QI3O6NBBk@z \
    --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).