public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexey Lapshin <alexey.lapshin@espressif.com>
To: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Cc: "Michael Xiao (XIAOXUFENG)" <xiaoxufeng@espressif.com>,
	Alexey Gerenkov <alexey.gerenkov@espressif.com>,
	Anton Maklakov <anton.maklakov@espressif.com>,
	"d@dcepelik.cz" <d@dcepelik.cz>,
	"hubicka@ucw.cz" <hubicka@ucw.cz>,
	Ivan Grokhotkov <ivan@espressif.com>
Subject: [RFC] tree-optimization: fix optimize-out variables passed into func to alloc
Date: Tue, 17 Jan 2023 15:35:45 +0000	[thread overview]
Message-ID: <18ac74c8afb663aa0dc2503a571d0d17ebb2e759.camel@espressif.com> (raw)

After updating to GCC newer than 11.4.0 we found that some code started
to fail if it was built with size optimization (-Os).
You can find testsuite for reproduction in the attached patch.

The simplified version affected code looks like this:

void alloc_function (unsigned char **data_p) {
  *data_p = malloc (8);
  assert(*data_p != NULL);
}
int main () {
  int *data;
  alloc_function (&data);
  printf ("data pointer is %p", data); // prints NULL(compile with -Os)
}

If the type of passed argument is equal to the type in alloc_function
declaration it works perfectly. Also helps change one or both types to
void.

I found that issue started to appear from commit
d119f34c952f8718fdbabc63e2f369a16e92fa07
if-statement which leads to this issue was found and after being
removed seems it works well.

Could you please elaborate on what cases exactly this checking should
optimize?
I think it should also contain at least one more check for accessing
variable's memory to write..


---
 gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c | 17 +++++++++++++++++
 gcc/tree-ssa-alias.cc                         |  2 --
 2 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
new file mode 100644
index 00000000000..b30c1cedcb9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/alloc-in-func.c
@@ -0,0 +1,17 @@
+/* { dg-do run } */
+/* { dg-options "-Os" } */
+
+#define assert(x) if (!(x)) __builtin_abort ()
+
+static inline void alloc_function (unsigned char **data_p)
+{
+    *data_p = (unsigned char *) __builtin_malloc (10);
+    assert (*data_p != (void *)0);
+}
+
+int main ()
+{
+    int *data = (void *)0;
+    alloc_function ((unsigned char **) &data);
+    assert (data != (void *)0);
+}
diff --git a/gcc/tree-ssa-alias.cc b/gcc/tree-ssa-alias.cc
index b8f107dfa52..9068db300e5 100644
--- a/gcc/tree-ssa-alias.cc
+++ b/gcc/tree-ssa-alias.cc
@@ -2608,8 +2608,6 @@ modref_may_conflict (const gcall *stmt,
 	  if (num_tests >= max_tests)
 	    return true;
 	  alias_stats.modref_tests++;
-	  if (!alias_sets_conflict_p (base_set, base_node->base))
-	    continue;
 	  num_tests++;
 	}
 
-- 
2.34.1


             reply	other threads:[~2023-01-17 15:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17 15:35 Alexey Lapshin [this message]
2023-01-17 21:40 ` Andrew Pinski
2023-01-18  7:05   ` Richard Biener

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=18ac74c8afb663aa0dc2503a571d0d17ebb2e759.camel@espressif.com \
    --to=alexey.lapshin@espressif.com \
    --cc=alexey.gerenkov@espressif.com \
    --cc=anton.maklakov@espressif.com \
    --cc=d@dcepelik.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hubicka@ucw.cz \
    --cc=ivan@espressif.com \
    --cc=xiaoxufeng@espressif.com \
    /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).