Question about restrict pointers

Alexey Salmin alexey.salmin@gmail.com
Thu Apr 22 11:01:00 GMT 2010


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



More information about the Gcc-help mailing list