c++: Tweak VLA representation.
authorJason Merrill <jason@redhat.com>
Mon, 11 May 2020 18:05:46 +0000 (14:05 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 11 May 2020 18:50:41 +0000 (14:50 -0400)
If we put the SAVE_EXPR for a VLA size inside the MINUS_EXPR rather than
outside, it will work better with constant folding.

The equivalent change was made in the C front-end in 2004, in commit
r0-64535-g8b0b9aefd29dfe6398857bcf5628662e2f0e21f6

gcc/cp/ChangeLog
2020-05-11  Jason Merrill  <jason@redhat.com>

* decl.c (compute_array_index_type_loc): Stabilize before building
the MINUS_EXPR.

gcc/cp/ChangeLog
gcc/cp/decl.c

index ab5e6ab..9831c25 100644 (file)
@@ -1,5 +1,10 @@
 2020-05-11  Jason Merrill  <jason@redhat.com>
 
+       * decl.c (compute_array_index_type_loc): Stabilize before building
+       the MINUS_EXPR.
+
+2020-05-11  Jason Merrill  <jason@redhat.com>
+
        * decl.c (grokdeclarator): Adjust deprecated_state here.
        (start_decl): Not here.
 
index adf9465..dea1ba0 100644 (file)
@@ -10449,6 +10449,15 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size,
     itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
   else
     {
+      if (!TREE_CONSTANT (size))
+       {
+         /* A variable sized array.  Arrange for the SAVE_EXPR on the inside
+            of the MINUS_EXPR, which allows the -1 to get folded with the +1
+            that happens when building TYPE_SIZE.  */
+         size = variable_size (size);
+         stabilize_vla_size (size);
+       }
+
       /* Compute the index of the largest element in the array.  It is
         one less than the number of elements in the array.  We save
         and restore PROCESSING_TEMPLATE_DECL so that computations in
@@ -10466,11 +10475,6 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size,
 
       if (!TREE_CONSTANT (itype))
        {
-         /* A variable sized array.  */
-         itype = variable_size (itype);
-
-         stabilize_vla_size (itype);
-
          if (sanitize_flags_p (SANITIZE_VLA)
              && current_function_decl != NULL_TREE)
            {