Bug 66379 - SCCVN doesn't handle aggregate array element accesses very well
Summary: SCCVN doesn't handle aggregate array element accesses very well
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Richard Biener
URL:
Keywords:
Depends on:
Blocks: 66142
  Show dependency treegraph
 
Reported: 2015-06-02 13:31 UTC by Richard Biener
Modified: 2021-05-04 13:17 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work: 10.1.0
Known to fail: 8.4.1, 9.3.1
Last reconfirmed: 2015-11-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Biener 2015-06-02 13:31:08 UTC
struct X { int i; int j; };

struct X a[128];

int foo (int i)
{
  struct X x;
  x.i = i;
  x.j = 0;
  a[i] = x;
  return a[i].i;
}

should ideally optimize to return i;

But first, FRE is confused by SRA:

  <bb 2>:
  _4 = &a[i_2(D)];
  MEM[(struct X *)_4] = i_2(D);
  MEM[(struct X *)_4 + 4B] = 0;
  _6 = a[i_2(D)].i;
  return _6;

and second (if SRA is disabled),

  <bb 2>:
  x.i = i_2(D);
  x.j = 0;
  a[i_2(D)] = x;
  _6 = a[i_2(D)].i;
  x ={v} {CLOBBER};
  return _6;

the aggregate copy lookthrough code is too simple here (offset-based
stuff just bails out with variable indexes).  Using stmt_kills_ref_p
helps for this case but it does not for the testcase in PR66142.

@@ -1879,7 +1895,7 @@ vn_reference_lookup_3 (ao_ref *ref, tree
               || handled_component_p (gimple_assign_rhs1 (def_stmt))))
     {
       tree base2;
-      HOST_WIDE_INT offset2, size2, maxsize2;
+      HOST_WIDE_INT maxsize2;
       int i, j;
       auto_vec<vn_reference_op_s> rhs;
       vn_reference_op_t vro;
@@ -1890,8 +1906,6 @@ vn_reference_lookup_3 (ao_ref *ref, tree
 
       /* See if the assignment kills REF.  */
       base2 = ao_ref_base (&lhs_ref);
-      offset2 = lhs_ref.offset;
-      size2 = lhs_ref.size;
       maxsize2 = lhs_ref.max_size;
       if (maxsize2 == -1
          || (base != base2
@@ -1900,8 +1914,7 @@ vn_reference_lookup_3 (ao_ref *ref, tree
                  || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
                  || !tree_int_cst_equal (TREE_OPERAND (base, 1),
                                          TREE_OPERAND (base2, 1))))
-         || offset2 > offset
-         || offset2 + size2 < offset + maxsize)
+         || !stmt_kills_ref_p (def_stmt, ref))
        return (void *)-1;
 
       /* Find the common base of ref and the lhs.  lhs_ops already
Comment 1 Richard Biener 2015-11-26 12:27:30 UTC
Works now with -fno-tree-sra as the patch was committed but SRA still confuses FRE so this is a testcase for the "stupid address-forwarding" issue.
Comment 2 Richard Biener 2021-05-04 13:17:05 UTC
Fixed in GCC 10+.