Bug 35360

Summary: Static (base/offset/size rule) should be extended to handle array elements
Product: gcc Reporter: davidxl <xinliangli>
Component: middle-endAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: enhancement CC: gcc-bugs, rguenth
Priority: P3 Keywords: alias, missed-optimization
Version: 4.3.0   
Target Milestone: 4.9.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2010-07-13 10:55:27
Bug Depends on:    
Bug Blocks: 35358    

Description davidxl 2008-02-25 05:19:38 UTC
// David Li
the two references in the loop are not aliased. Not optimized by gcc

struct A{
 int a;
 int b;
};

struct A aa[100];


void foo(int n, int i, int j)
{
  int k = 0;
  for (k = 0; k < n; k++)
  {
      aa[i].a += aa[j].b;
  }
}
Comment 1 Richard Biener 2008-02-25 11:44:45 UTC
Confirmed.  MEM_REF might help here as we'd get

  off_1 = IDX <0 + i * 8>
  MEM_REF <aa, off_1>

and

  off_2 = IDX <4 + j * 8>
  MEM_REF <aa, off_2>

where the alias-oracle should be able to see that the accesses cannot overlap.
Comment 2 Richard Biener 2008-03-14 21:29:10 UTC
Zdeneks lim/store-motion rewrite should help as well.
Comment 3 Richard Biener 2009-04-03 12:32:48 UTC
Re-confirmed.
Comment 4 Andrew Pinski 2009-06-09 02:40:23 UTC
Here is another testcase that depends on PRE/FRE (really VN) getting the more correct answer:
struct A{
 int a;
 int b;
};

struct A aa[100];


void foo(int n, int i, int j)
{
  aa[j].a = 0;
  aa[i].b = 1;
  if (aa[j].a != 0)
    link_error();
}
Comment 5 Steven Bosscher 2010-07-13 10:55:27 UTC
Test case of comment #4 still fails with r162134.

Not sure what is expected for the test case of comment #0, but I'm assuming the point is that the store of the reduction should be sunk. That doesn't happen as of r162134, this is what comes out of the compiler at -O3:


;; Function foo (foo)

Removing basic block 6
foo (int n, int i, int j)
{
  int prephitmp.3;
  int k;
  int D.1988;

<bb 2>:
  if (n_4(D) > 0)
    goto <bb 3>;
  else
    goto <bb 5>;

<bb 3>:
  prephitmp.3_14 = aa[i_5(D)].a;

<bb 4>:
  # k_16 = PHI <k_10(4), 0(3)>
  # prephitmp.3_18 = PHI <prephitmp.3_9(4), prephitmp.3_14(3)>
  D.1988_8 = aa[j_7(D)].b;
  prephitmp.3_9 = prephitmp.3_18 + D.1988_8;
  aa[i_5(D)].a = prephitmp.3_9;
  k_10 = k_16 + 1;
  if (k_10 != n_4(D))
    goto <bb 4>;
  else
    goto <bb 5>;

<bb 5>:
  return;

}
Comment 6 Andrew Pinski 2021-07-26 09:02:35 UTC
Both testcases are fixed in 4.9.0 by r0-122892 and there was a few testcases added that were similar to this bug report already.