public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] middle-end/100509 - avoid folding constant to aggregate type
@ 2021-05-11 10:47 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2021-05-11 10:47 UTC (permalink / raw)
  To: gcc-patches

When folding a constant initializer looking through aliases to
incompatible types can lead to us trying to fold a constant
to an aggregate type which can't work.  Simply avoid trying
to constant fold non-register typed symbols.

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

2021-05-11  Richard Biener  <rguenther@suse.de>

	PR middle-end/100509
	* gimple-fold.c (fold_gimple_assign): Only call
	get_symbol_constant_value on register type symbols.

	* gcc.dg/pr100509.c: New testcase.
---
 gcc/gimple-fold.c               | 3 ++-
 gcc/testsuite/gcc.dg/pr100509.c | 9 +++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr100509.c

diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index 768ef89d876..6beb4f3d305 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -547,7 +547,8 @@ fold_gimple_assign (gimple_stmt_iterator *si)
 					   CONSTRUCTOR_ELTS (rhs));
 	  }
 
-	else if (DECL_P (rhs))
+	else if (DECL_P (rhs)
+		 && is_gimple_reg_type (TREE_TYPE (rhs)))
 	  return get_symbol_constant_value (rhs);
       }
       break;
diff --git a/gcc/testsuite/gcc.dg/pr100509.c b/gcc/testsuite/gcc.dg/pr100509.c
new file mode 100644
index 00000000000..9405e2a27df
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr100509.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O" } */
+
+struct X {
+  int a;
+};
+const int a = 0;
+static struct X A __attribute__((alias("a")));
+void foo() { struct X b = A; }
-- 
2.26.2

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-05-11 10:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-11 10:47 [PATCH] middle-end/100509 - avoid folding constant to aggregate type Richard Biener

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