From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26017 invoked by alias); 22 Apr 2010 06:48:43 -0000 Received: (qmail 26007 invoked by uid 22791); 22 Apr 2010 06:48:41 -0000 X-SWARE-Spam-Status: No, hits=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SARE_MSGID_LONG45,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from fg-out-1718.google.com (HELO fg-out-1718.google.com) (72.14.220.155) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 22 Apr 2010 06:48:36 +0000 Received: by fg-out-1718.google.com with SMTP id l26so134102fgb.8 for ; Wed, 21 Apr 2010 23:48:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.239.164.140 with HTTP; Wed, 21 Apr 2010 23:48:13 -0700 (PDT) From: Alexey Salmin Date: Thu, 22 Apr 2010 11:01:00 -0000 Received: by 10.239.185.15 with SMTP id a15mr831606hbh.132.1271918913089; Wed, 21 Apr 2010 23:48:33 -0700 (PDT) Message-ID: Subject: Question about restrict pointers To: gcc-help@gcc.gnu.org Content-Type: text/plain; charset=ISO-8859-1 X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2010-04/txt/msg00237.txt.bz2 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