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

            Bug ID: 93102
           Summary: [optimization] is it legal to avoid accessing const
                    local array from stack ?
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhongyunde at huawei dot com
  Target Milestone: ---

test code:

int foo (int m, int n)
{
  const int C00002029[10] = {0,1,2,3,2,3,0,1,2,3};

  int index, sum = 0;

  for (index=m; index<n; index ++)
    sum += C00002029[index];

  return sum;
}

in general, we may store the const value {0,1,2,3,2,3,0,1,2,3} in rodata
section, then use memcpy to assign the local array C00002029.

But as we known, in above code, the array is only accessed one time, so can we
optimize it such as following logic? this can avoid initialization, and the
performance can be better.

const int C00002029_xxx[10] = {0,1,2,3,2,3,0,1,2,3};
int foo (int m, int n)
{
  int index, sum = 0;

  for (index=m; index<n; index ++)
    sum += C00002029_xxx[index];

  return sum;
}

Reply via email to