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  <rguent...@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

Reply via email to