public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jan Hubicka <hubicka@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-5379] Fix modref wrt __builtin_assume_aligned
Date: Thu, 18 Nov 2021 17:42:05 +0000 (GMT)	[thread overview]
Message-ID: <20211118174205.418363858422@sourceware.org> (raw)

https://gcc.gnu.org/g:c331a75d49b6043399f5ccce72a02ccf3b0ddc56

commit r12-5379-gc331a75d49b6043399f5ccce72a02ccf3b0ddc56
Author: Jan Hubicka <jh@suse.cz>
Date:   Thu Nov 18 18:41:43 2021 +0100

    Fix modref wrt __builtin_assume_aligned
    
    __builtin_assume_aligned has bit contraictionary fnspec description "1cX "
    which means that parameter 1 is returned but also unused.  PTA code takes
    precedence to parameter being returned, while modref takes the info that
    parameter is unused.  This patch tweaks modref to follow PTA semantics (as
    suggested by Richard in the PR log)
    
    gcc/ChangeLog:
    
    2021-11-18  Jan Hubicka  <hubicka@ucw.cz>
    
            PR ipa/103266
            * ipa-modref.c (modref_eaf_analysis::merge_call_lhs_flags): Unused
            parameter may still be returned.
            (modref_eaf_analysis::analyze_ssa_name): Call merge_call_lhs_flags
            even for unused function args.
    
    gcc/testsuite/ChangeLog:
    
    2021-11-18  Jan Hubicka  <hubicka@ucw.cz>
    
            PR ipa/103266
            * g++.dg/torture/pr103266.C: New test.

Diff:
---
 gcc/ipa-modref.c                        | 32 ++++++++++++++++++++++----------
 gcc/testsuite/g++.dg/torture/pr103266.C | 23 +++++++++++++++++++++++
 2 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/gcc/ipa-modref.c b/gcc/ipa-modref.c
index c94f0589d44..e5d2b11025a 100644
--- a/gcc/ipa-modref.c
+++ b/gcc/ipa-modref.c
@@ -2033,10 +2033,7 @@ modref_eaf_analysis::merge_call_lhs_flags (gcall *call, int arg,
 					   bool indirect)
 {
   int index = SSA_NAME_VERSION (name);
-
-  /* If value is not returned at all, do nothing.  */
-  if (!direct && !indirect)
-    return;
+  bool returned_directly = false;
 
   /* If there is no return value, no flags are affected.  */
   if (!gimple_call_lhs (call))
@@ -2047,10 +2044,23 @@ modref_eaf_analysis::merge_call_lhs_flags (gcall *call, int arg,
   if (arg >= 0)
     {
       int flags = gimple_call_return_flags (call);
-      if ((flags & ERF_RETURNS_ARG)
-	  && (flags & ERF_RETURN_ARG_MASK) != arg)
-	return;
+      if (flags & ERF_RETURNS_ARG)
+	{
+	  if ((flags & ERF_RETURN_ARG_MASK) == arg)
+	    returned_directly = true;
+	  else
+	   return;
+	}
+    }
+  /* Make ERF_RETURNS_ARG overwrite EAF_UNUSED.  */
+  if (returned_directly)
+    {
+      direct = true;
+      indirect = false;
     }
+  /* If value is not returned at all, do nothing.  */
+  else if (!direct && !indirect)
+    return;
 
   /* If return value is SSA name determine its flags.  */
   if (TREE_CODE (gimple_call_lhs (call)) == SSA_NAME)
@@ -2273,11 +2283,13 @@ modref_eaf_analysis::analyze_ssa_name (tree name)
 		if (gimple_call_arg (call, i) == name)
 		  {
 		    int call_flags = gimple_call_arg_flags (call, i);
-		    if (!ignore_retval && !(call_flags & EAF_UNUSED))
+		    if (!ignore_retval)
 		      merge_call_lhs_flags
 			      (call, i, name,
-			       !(call_flags & EAF_NOT_RETURNED_DIRECTLY),
-			       !(call_flags & EAF_NOT_RETURNED_INDIRECTLY));
+			       !(call_flags & (EAF_NOT_RETURNED_DIRECTLY
+					       | EAF_UNUSED)),
+			       !(call_flags & (EAF_NOT_RETURNED_INDIRECTLY
+					       | EAF_UNUSED)));
 		    if (!(ecf_flags & (ECF_CONST | ECF_NOVOPS)))
 		      {
 			call_flags = callee_to_caller_flags
diff --git a/gcc/testsuite/g++.dg/torture/pr103266.C b/gcc/testsuite/g++.dg/torture/pr103266.C
new file mode 100644
index 00000000000..d5c13f50979
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr103266.C
@@ -0,0 +1,23 @@
+// { dg-do run }
+/* { dg-additional-options "-std=c++14" } */
+    typedef unsigned int u32;
+    typedef unsigned char u8;
+
+    static u32 pu8to32(const u8 * p8) __attribute__((noinline));
+    static u32 pu8to32(const u8 * p8) {
+      u32 v;
+    
+      __builtin_memcpy(&v, __builtin_assume_aligned(p8, 1), sizeof(u32));
+    
+      return v;
+    }
+    
+    int main(void) {
+      // dse1 throws this store away
+      u8 d[sizeof(u32)] = {
+          0x07, 0x00, 0x00, 0x07,
+      };
+    
+      if (pu8to32(d) != 0x07000007)
+        __builtin_trap();
+    }


                 reply	other threads:[~2021-11-18 17:42 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=20211118174205.418363858422@sourceware.org \
    --to=hubicka@gcc.gnu.org \
    --cc=gcc-cvs@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).