public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexey Salmin <alexey.salmin@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Question about restrict pointers
Date: Thu, 22 Apr 2010 11:01:00 -0000	[thread overview]
Message-ID: <o2q87a8dc11004212348odde40fdcn2f444d26d3c82bd4@mail.gmail.com> (raw)

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

             reply	other threads:[~2010-04-22  6:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-22 11:01 Alexey Salmin [this message]
2010-04-24 11:37 ` Ian Lance Taylor
2010-04-24 18:32   ` Manuel López-Ibáñez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=o2q87a8dc11004212348odde40fdcn2f444d26d3c82bd4@mail.gmail.com \
    --to=alexey.salmin@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).