public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Question about restrict pointers
@ 2010-04-22 11:01 Alexey Salmin
  2010-04-24 11:37 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Salmin @ 2010-04-22 11:01 UTC (permalink / raw)
  To: gcc-help

Hello. I have a simple question about restrict pointers. Consider the
following code:

salmin@salmin:~$ cat restrict0.c
void f(int *a, const int *b) {
        *a++ = *b + 1;
        *a++ = *b + 1;
}
salmin@salmin:~$ ./systemroot/bin/gcc-trunk-158628 -S -O4 -std=gnu99 restrict0.c
salmin@salmin:~$ grep -v '[.:]' restrict0.s
        movl    (%rsi), %eax
        addl    $1, %eax
        movl    %eax, (%rdi)
        movl    (%rsi), %eax
        addl    $1, %eax
        movl    %eax, 4(%rdi)
        ret

We have mov-add-mov twice here because if (a==b) then "*b" will be
modified by the first statement. It's clear.
However if we add a "restrict" keyword to the definition of b like
that it affects nothing:

salmin@salmin:~$ cat restrict1.c
void f(int *a, const int *restrict b) {
        *a++ = *b + 1;
        *a++ = *b + 1;
}
salmin@salmin:~$ ./systemroot/bin/gcc-trunk-158628 -S -O4 -std=gnu99 restrict1.c
salmin@salmin:~$ grep -v '[.:]' restrict1.s
        movl    (%rsi), %eax
        addl    $1, %eax
        movl    %eax, (%rdi)
        movl    (%rsi), %eax
        addl    $1, %eax
        movl    %eax, 4(%rdi)
        ret

As far as I understand the "restrict" concept "const int *restrict b"
guarantee that "*b" will not be modified by "*a++ = *b + 1;".
Another thing I don't understand is why adding the "restrict" keyword
to the definition of "a" helps:

salmin@salmin:~$ cat restrict2.c
void f(int *restrict a, const int *restrict b) {
        *a++ = *b + 1;
        *a++ = *b + 1;
}
salmin@salmin:~$ ./systemroot/bin/gcc-trunk-158628 -S -O4 -std=gnu99 restrict2.c
salmin@salmin:~$ grep -v '[.:]' restrict2.s
        movl    (%rsi), %eax
        addl    $1, %eax
        movl    %eax, (%rdi)
        movl    %eax, 4(%rdi)
        ret


Is that an unimplemented optimization or I just don't understand the
restrict concept?

Alexey

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

* Re: Question about restrict pointers
  2010-04-22 11:01 Question about restrict pointers Alexey Salmin
@ 2010-04-24 11:37 ` Ian Lance Taylor
  2010-04-24 18:32   ` Manuel López-Ibáñez
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Lance Taylor @ 2010-04-24 11:37 UTC (permalink / raw)
  To: Alexey Salmin; +Cc: gcc-help

Alexey Salmin <alexey.salmin@gmail.com> writes:

> However if we add a "restrict" keyword to the definition of b like
> that it affects nothing:


> As far as I understand the "restrict" concept "const int *restrict b"
> guarantee that "*b" will not be modified by "*a++ = *b + 1;".

I believe you are correct, and that this is an unimplemented
optimization.

Ian

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

* Re: Question about restrict pointers
  2010-04-24 11:37 ` Ian Lance Taylor
@ 2010-04-24 18:32   ` Manuel López-Ibáñez
  0 siblings, 0 replies; 3+ messages in thread
From: Manuel López-Ibáñez @ 2010-04-24 18:32 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Alexey Salmin, gcc-help

On 24 April 2010 03:06, Ian Lance Taylor <iant@google.com> wrote:
> Alexey Salmin <alexey.salmin@gmail.com> writes:
>
>> However if we add a "restrict" keyword to the definition of b like
>> that it affects nothing:
>
>
>> As far as I understand the "restrict" concept "const int *restrict b"
>> guarantee that "*b" will not be modified by "*a++ = *b + 1;".
>
> I believe you are correct, and that this is an unimplemented
> optimization.

Therefore, you should open a PR about it in bugzilla please.
Otherwise, I will get overlooked.

Cheers,

Manuel.

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

end of thread, other threads:[~2010-04-24 14:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-22 11:01 Question about restrict pointers Alexey Salmin
2010-04-24 11:37 ` Ian Lance Taylor
2010-04-24 18:32   ` Manuel López-Ibáñez

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).