https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104529

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
When I disable the TREE_READONLY (decl) = true; in build_target_expr, on the
array-temp1.C testcase gimple dump changes:
 int f ()
 {
   int D.2491;
-  static const int C.0[10] = {1, 42, 3, 4, 5, 6, 7, 8, 9, 0};
+  const int D.2435[10];
   typedef const int AR[<unknown>];

-  try
-    {
-      D.2491 = C.0[5];
-      return D.2491;
-    }
-  finally
-    {
-      C.0 = {CLOBBER(eol)};
-    }
+  D.2435[0] = 1;
+  D.2435[1] = 42;
+  D.2435[2] = 3;
+  D.2435[3] = 4;
+  D.2435[4] = 5;
+  D.2435[5] = 6;
+  D.2435[6] = 7;
+  D.2435[7] = 8;
+  D.2435[8] = 9;
+  D.2435[9] = 0;
+  D.2491 = D.2435[5];
+  return D.2491;

which seems quite undesirable change.
The spot that cares about TREE_READONLY is exactly in
gimplify_init_constructor:
        /* If a const aggregate variable is being initialized, then it
           should never be a lose to promote the variable to be static.  */
        if (valid_const_initializer
            && num_nonzero_elements > 1
            && TREE_READONLY (object)
            && VAR_P (object)
            && !DECL_REGISTER (object)
            && (flag_merge_constants >= 2 || !TREE_ADDRESSABLE (object))
...

So, the #c5 patch looks wrong from this regard too.
Furthermore, we have that notify_temp_creation mode there and I think we really
don't want to clear TREE_READONLY in that case.

So, I think we need something like:
--- gcc/gimplify.cc.jj  2022-03-03 09:13:16.000000000 +0100
+++ gcc/gimplify.cc     2022-03-03 14:42:00.952959549 +0100
@@ -5120,6 +5120,12 @@ gimplify_init_constructor (tree *expr_p,
          {
            if (notify_temp_creation)
              return GS_OK;
+
+           /* The var will be initialized and so appear on lhs of
+              assignment, it can't be TREE_READONLY anymore.  */
+           if (VAR_P (object))
+             TREE_READONLY (object) = 0;
+
            is_empty_ctor = true;
            break;
          }
@@ -5171,6 +5177,11 @@ gimplify_init_constructor (tree *expr_p,
            break;
          }

+       /* The var will be initialized and so appear on lhs of
+          assignment, it can't be TREE_READONLY anymore.  */
+       if (VAR_P (object) && !notify_temp_creation)
+         TREE_READONLY (object) = 0;
+
        /* If there are "lots" of initialized elements, even discounting
           those that are not address constants (and thus *must* be
           computed at runtime), then partition the constructor into

or so.

Reply via email to