]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Not add initialization for variables been initialized by FEs [PR102359]
authorqing zhao <qing.zhao@oracle.com>
Tue, 5 Oct 2021 23:14:00 +0000 (16:14 -0700)
committerqing zhao <qing.zhao@oracle.com>
Tue, 5 Oct 2021 23:14:00 +0000 (16:14 -0700)
C++ FE creates proxy variables, which have associated DECL_VALUE_EXPR
and have been initialized by FE. For such auto variable, we should not
add initialization when -ftrivial-auto-var-init presents.

PR middle-end/102359

gcc/ChangeLog:

2021-10-05  qing zhao  <qing.zhao@oracle.com>

* gimplify.c (gimplify_decl_expr): Not add initialization for an
auto variable when it has been initialized by frontend.

gcc/testsuite/ChangeLog:

2021-10-05  qing zhao  <qing.zhao@oracle.com>

* g++.dg/pr102359_1.C: New test.
* g++.dg/pr102359_2.C: New test.

gcc/gimplify.c
gcc/testsuite/g++.dg/pr102359_1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/pr102359_2.C [new file with mode: 0644]

index b27776af7c8a8370900ee9dfbae2dd51dffdc364..d8e4b13934952c31005694a8aad238f51eb094b8 100644 (file)
@@ -1872,6 +1872,12 @@ gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
     {
       tree init = DECL_INITIAL (decl);
       bool is_vla = false;
+      /* Check whether a decl has FE created VALUE_EXPR here BEFORE
+        gimplify_vla_decl creates VALUE_EXPR for a vla decl.
+        If the decl has VALUE_EXPR that was created by FE (usually
+        C++FE), it's a proxy varaible, and FE already initialized
+        the VALUE_EXPR of it, we should not initialize it anymore.  */
+      bool decl_had_value_expr_p = DECL_HAS_VALUE_EXPR_P (decl);
 
       poly_uint64 size;
       if (!poly_int_tree_p (DECL_SIZE_UNIT (decl), &size)
@@ -1934,7 +1940,8 @@ gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
       /* When there is no explicit initializer, if the user requested,
         We should insert an artifical initializer for this automatic
         variable.  */
-      else if (is_var_need_auto_init (decl))
+      else if (is_var_need_auto_init (decl)
+              && !decl_had_value_expr_p)
        {
          gimple_add_init_for_auto_var (decl,
                                        flag_auto_var_init,
diff --git a/gcc/testsuite/g++.dg/pr102359_1.C b/gcc/testsuite/g++.dg/pr102359_1.C
new file mode 100644 (file)
index 0000000..da643cd
--- /dev/null
@@ -0,0 +1,13 @@
+/* PR middle-end/102359 ICE gimplification failed since
+   r12-3433-ga25e0b5e6ac8a77a.  */
+/* { dg-do compile } */
+/* { dg-options "-ftrivial-auto-var-init=zero" } */
+/* { dg-require-effective-target c++17 } */
+
+struct A {
+  double a = 111;
+  auto foo() {
+    return [*this] { return a; };
+  }
+};
+int X = A{}.foo()();
diff --git a/gcc/testsuite/g++.dg/pr102359_2.C b/gcc/testsuite/g++.dg/pr102359_2.C
new file mode 100644 (file)
index 0000000..d026d72
--- /dev/null
@@ -0,0 +1,13 @@
+/* PR middle-end/102359 ICE gimplification failed since
+   r12-3433-ga25e0b5e6ac8a77a.  */
+/* { dg-do run} */
+/* { dg-options "-ftrivial-auto-var-init=zero" } */
+/* { dg-require-effective-target c++17 } */
+
+int main()
+{
+ int i = 42;
+ auto l = [=]() mutable { return i; };
+ if (l() != i)
+   __builtin_abort ();
+}