This testcase:

--cut here--
char table[256];

int test(void) {

  char val = 0;
  int i;

  for (i = 0; i < 10; i++)
    val += table[i];

  return val;
}
--cut here--

compiles using gcc-4.2 -O2 to:

test:
        movzbl  table, %eax
        movl    $1, %edx
        .p2align 4,,7
.L2:
        addb    table(%edx), %al
        addl    $1, %edx
        cmpl    $10, %edx
        jne     .L2
        movsbl  %al,%eax
        ret

but using gcc-4.3 -O2 to:

test:
        xorl    %edx, %edx
        xorl    %eax, %eax
        .p2align 4,,7
        .p2align 3
.L2:
        addb    table(%edx), %al
        addl    $1, %edx
        cmpl    $10, %edx
        jne     .L2
        movsbl  %al,%eax
        ret

Note that gcc-4.3 initializes summation variable to zero, where gcc-4.2
initializes summation variable to first array member, saving one loop
iteration.

The difference is already present on tree level, where:

gcc-4.2:

test ()
{
  int i;
  char val;

<bb 2>:
  val = (char) (unsigned char) MEM[symbol: table];
  i = 1;

<L0>:;
  val = (char) ((unsigned char) val + (unsigned char) MEM[symbol: table, index:
(unsigned int) i]);
  i = i + 1;
  if (i != 10) goto <L0>; else goto <L2>;

<L2>:;
  return (int) val;

}

gcc-4.3:

test ()
{
  int i;
  char val;
  unsigned char D.1186;

<bb 2>:
  i = 0;
  val = 0;

<bb 3>:
  D.1186 = (unsigned char) val + (unsigned char) MEM[symbol: table, index:
(unsigned int) i];
  val = (char) D.1186;
  i = i + 1;
  if (i != 10)
    goto <bb 3>;
  else
    goto <bb 4>;

<bb 4>:
  return (int) val;

}


-- 
           Summary: Summing ariable should be initialized to the first
                    member before the loop
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ubizjak at gmail dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34723

Reply via email to