public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/32273]  New: 'restrict' is forgotten after loop unrolling
@ 2007-06-10 14:47 tomash dot brechko at gmail dot com
  2007-06-10 20:07 ` [Bug middle-end/32273] " rguenth at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: tomash dot brechko at gmail dot com @ 2007-06-10 14:47 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-10-01 14:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-10 14:47 [Bug c/32273] New: 'restrict' is forgotten after loop unrolling tomash dot brechko at gmail dot com
2007-06-10 20:07 ` [Bug middle-end/32273] " rguenth at gcc dot gnu dot org
2007-06-10 22:41 ` dberlin at gcc dot gnu dot org
2007-06-10 22:55 ` pinskia at gcc dot gnu dot org
2007-06-11  0:22 ` pinskia at gcc dot gnu dot org
2007-06-16  5:49 ` [Bug tree-optimization/32273] " pinskia at gcc dot gnu dot org
2007-06-24  3:38 ` pinskia at gcc dot gnu dot org
2008-10-01 14:37 ` rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).