The following two functions are equivalent (especially after loop unrolling):

void
foo(const int *restrict a, int *restrict b, int *restrict c)
{
  b[0] += a[0];
  c[0] += a[0];

  b[1] += a[1];
  c[1] += a[1];
}

void
bar(const int *restrict a, int *restrict b, int *restrict c)
{
  for (int i = 0; i < 2; ++i)
    {
      b[i] += a[i];
      c[i] += a[i];
    }
}


However gcc forgets about 'restrict' after the first iteration of the loop, and
foo() and bar() produce different code:

foo:
        pushl   %ebx
        movl    8(%esp), %ebx
        movl    12(%esp), %eax
        movl    16(%esp), %edx
        movl    (%ebx), %ecx
        addl    %ecx, (%eax)
        addl    %ecx, (%edx)     ;; Correct: no reloading of (%ebx) is needed.
        movl    4(%ebx), %ecx
        addl    %ecx, 4(%eax)
        addl    %ecx, 4(%edx)    ;; Correct: no reloading of 4(%ebx) is needed.
        popl    %ebx
        ret

bar:
        pushl   %ebx
        movl    8(%esp), %ebx
        movl    12(%esp), %edx
        movl    16(%esp), %ecx
        movl    (%ebx), %eax
        addl    %eax, (%edx)
        addl    %eax, (%ecx)    ;; Correct: no reloading of (%ebx) is needed.
        movl    4(%ebx), %eax
        addl    %eax, 4(%edx)
        movl    4(%ebx), %eax   ;; BUG: unnecessary reloading of 4(%ebx).
        addl    %eax, 4(%ecx)
        popl    %ebx
        ret

For any number of iterations only the first iteration honors the 'restrict'
qualifier.  This is wrong, because 'restrict' is a property of a pointer, not
data, so if p and q pointers reference different objects, then (p + OFF1) and
(q + OFF2) also expected to reference different objects.  Correct assembler for
foo() supports that.


-- 
           Summary: 'restrict' is forgotten after loop unrolling
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tomash dot brechko 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=32273

Reply via email to