Hi,

As mentioned in the bug report, I reopened this bug since the previous patch:

commit r13-1875-gff26f0ba68fe6e870f315d0601b596f889b89680
Author: Richard Biener <rguent...@suse.de>
Date:   Thu Jul 28 10:07:32 2022 +0200

    middle-end/106457 - improve array_at_struct_end_p for array objects
    Array references to array objects are never at struct end.


Didn’t resolve this bug.

This is a new patch, and my current work on -fstrict-flex-array depends on this 
patch.

Please take a look at the patch and let me know whether it’s good for 
committing.

Thanks.

Qing.


======================================

[PATCH] middle-end/106457 - improve array_at_struct_end_p for array
 objects (PR106457)

Array references are not handled correctly by current array_at_struct_end_p,
for the following array references:

Example 1: (from gcc/testsuite/gcc.dg/torture/pr50067-[1|2].c):

short a[32] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
 ... = (*((char(*)[32])&a[0]))[i+8];  // this array reference

Example 2: (from gcc/testsuite/gcc.target/aarch64/vadd_reduc-2.c):

int test (uint8_t *p, uint32_t t[1][1], int n) {
  for (int i = 0; i < 4; i++, p++)
    t[i][0] = ...;  // this array reference
...
}

Example 3: (from gcc/testsuite/g++.dg/debug/debug5.C):

  int a = 1;
  int b = 1;
  int e[a][b];
  e[0][0] = 0;  // this array reference

All the above array references are identified as TRUE by the current
array_at_struct_end_p, therefore treated as flexible array members.
Obviously, they are just simple array references, not an array refs
to the last field of a struture. The current array_at_struct_end_p handles
such array references incorrectly.

In order to handle array references correctly, we could recursively check
its first operand if it's a MEM_REF or COMPONENT_REF and stop as FALSE
when otherwise. This resolved all the issues for ARRAY_REF.

bootstrapped and regression tested on both X86 and Aarch64.
Multiple testing cases behave differently due to array_at_struct_end_p now
behave correctly (return FALSE now, then they are not flexible array member
anymore). Adjust these testing cases.

There is one regression for gcc/target/aarch64/vadd_reduc-2.c is left
unresolved since the loop transformation is changed due to the changed behavior
of array_at_struct_end_p, simple adjustment of the testing case doesnt work.
I will file a bug to record this regression.

gcc/ChangeLog:

        PR middle-end/106457
        * tree.cc (array_at_struct_end_p): Handle array objects recursively
        through its first operand.

gcc/testsuite/ChangeLog:

        PR middle-end/106457
        * gcc.dg/torture/pr50067-1.c: Add -Wno-aggressive-loop-optimizations
        to suppress warnings.
        * gcc.dg/torture/pr50067-2.c: Likewise.
        * gcc.target/aarch64/vadd_reduc-2.c: Likewise.
        * gcc.target/i386/pr104059.c: Likewise.


The complete patch is at:


Attachment: 0001-middle-end-106457-improve-array_at_struct_end_p-for-.patch
Description: 0001-middle-end-106457-improve-array_at_struct_end_p-for-.patch

Reply via email to