public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] ipa-sra: Fix address escape case when detecting Fortran descriptors
@ 2022-12-14 15:18 Martin Jambor
  2022-12-14 15:34 ` Jan Hubicka
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Jambor @ 2022-12-14 15:18 UTC (permalink / raw)
  To: GCC Patches; +Cc: Jan Hubicka, Jan Hubicka

Hi,

The discussion about scan_expr_access in ipa-sra.cc brought my
attention to a missing case of handling an ADDR_EXPR.  As the added
testcase shows, the heuristics which looks for parameters which are
local variables that are only written to and passed by reference in
calls can miss a case where the address of the variable in question is
stored elsewhere in an assignment.

This patch adds that case to the function and also adds the
optimization that Richi suggested, i.e. bailing out early on simple
SSA_NAMEs and constant trees.

The patch is undergoing bootstrap and testing on an x86_64-linux right
now.  OK if it passes?

Thanks,

Martin


gcc/ChangeLog:

2022-12-14  Martin Jambor  <mjambor@suse.cz>

	* ipa-sra.cc (loaded_decls): Adjust comment.
	(scan_expr_access): Also detect assignments of address of local
	variables to a variable.  Bail out early on SSA_NAMEs and
	constants as an optimization.

gcc/testsuite/ChangeLog:

2022-12-14  Martin Jambor  <mjambor@suse.cz>

	* gcc.dg/ipa/ipa-sra-29.c: New test.
---
 gcc/ipa-sra.cc                        | 16 ++++++++++-
 gcc/testsuite/gcc.dg/ipa/ipa-sra-29.c | 38 +++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/ipa/ipa-sra-29.c

diff --git a/gcc/ipa-sra.cc b/gcc/ipa-sra.cc
index 93f5e34b15c..bcabdedfc6c 100644
--- a/gcc/ipa-sra.cc
+++ b/gcc/ipa-sra.cc
@@ -592,7 +592,8 @@ namespace {
 
 hash_map<tree, gensum_param_desc *> *decl2desc;
 
-/* All local DECLs ever loaded from.  */
+/* All local DECLs ever loaded from of and of those that have their address
+   assigned to a variable.  */
 
 hash_set <tree> *loaded_decls;
 
@@ -1743,6 +1744,19 @@ scan_expr_access (tree expr, gimple *stmt, isra_scan_context ctx,
   bool deref = false;
   bool reverse;
 
+  if (TREE_CODE (expr) == ADDR_EXPR)
+    {
+      if (ctx == ISRA_CTX_ARG)
+	return;
+      tree t = get_base_address (TREE_OPERAND (expr, 0));
+      if (TREE_CODE (t) == VAR_DECL && !TREE_STATIC (t))
+	loaded_decls->add (t);
+      return;
+    }
+  if (TREE_CODE (expr) == SSA_NAME
+      || CONSTANT_CLASS_P (expr))
+    return;
+
   if (TREE_CODE (expr) == BIT_FIELD_REF
       || TREE_CODE (expr) == IMAGPART_EXPR
       || TREE_CODE (expr) == REALPART_EXPR)
diff --git a/gcc/testsuite/gcc.dg/ipa/ipa-sra-29.c b/gcc/testsuite/gcc.dg/ipa/ipa-sra-29.c
new file mode 100644
index 00000000000..aee45ea0e8f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/ipa-sra-29.c
@@ -0,0 +1,38 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-ipa-sra-details"  } */
+
+struct S
+{
+  float f;
+  int i;
+  void *p;
+};
+
+extern struct S *gp;
+int baz (float);
+
+static int
+__attribute__((noinline))
+bar (struct S *p)
+{
+  if (p->i != 6)
+    __builtin_abort ();
+
+  return baz(p->f);
+}
+
+int
+foo (void)
+{
+  struct S s;
+
+  gp = &s;
+  s.f = 7.4;
+  s.i = 6;
+  s.p = &s;
+
+  bar (&s);
+  return 0;
+}
+
+/* { dg-final { scan-ipa-dump-not "Variable constructed just to be passed to calls" "sra" } } */
-- 
2.38.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ipa-sra: Fix address escape case when detecting Fortran descriptors
  2022-12-14 15:18 [PATCH] ipa-sra: Fix address escape case when detecting Fortran descriptors Martin Jambor
@ 2022-12-14 15:34 ` Jan Hubicka
  0 siblings, 0 replies; 2+ messages in thread
From: Jan Hubicka @ 2022-12-14 15:34 UTC (permalink / raw)
  To: Martin Jambor; +Cc: GCC Patches

> Hi,
> 
> The discussion about scan_expr_access in ipa-sra.cc brought my
> attention to a missing case of handling an ADDR_EXPR.  As the added
> testcase shows, the heuristics which looks for parameters which are
> local variables that are only written to and passed by reference in
> calls can miss a case where the address of the variable in question is
> stored elsewhere in an assignment.
> 
> This patch adds that case to the function and also adds the
> optimization that Richi suggested, i.e. bailing out early on simple
> SSA_NAMEs and constant trees.
> 
> The patch is undergoing bootstrap and testing on an x86_64-linux right
> now.  OK if it passes?
> 
> Thanks,
> 
> Martin
> 
> 
> gcc/ChangeLog:
> 
> 2022-12-14  Martin Jambor  <mjambor@suse.cz>
> 
> 	* ipa-sra.cc (loaded_decls): Adjust comment.
> 	(scan_expr_access): Also detect assignments of address of local
> 	variables to a variable.  Bail out early on SSA_NAMEs and
> 	constants as an optimization.
> 
> gcc/testsuite/ChangeLog:
> 
> 2022-12-14  Martin Jambor  <mjambor@suse.cz>
> 
> 	* gcc.dg/ipa/ipa-sra-29.c: New test.

OK,
Thanks!
Honza

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-12-14 15:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-14 15:18 [PATCH] ipa-sra: Fix address escape case when detecting Fortran descriptors Martin Jambor
2022-12-14 15:34 ` Jan Hubicka

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).