This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: aliasing with char*
- From: Tim Prince <tprince at computer dot org>
- To: wouter dot vermaelen at coware dot be
- Cc: gcc-help at gnu dot org
- Date: Mon, 19 Nov 2007 05:55:05 -0800
- Subject: Re: aliasing with char*
- References: <47414EF8.8070101@coware.be>
Wouter Vermaelen wrote:
> Hi,
>
> Consider the following code fragment:
>
> -----------------------
> //typedef char T;
> typedef short T;
>
> int count;
>
> T f(T* p) {
> count += 2;
> T t = *p;
> count += 3;
> return t;
> }
> -----------------------
>
> If T is defined as short this generates the following code (compiled
> with -O3 with recent gcc-4.3 snapshot on linux x86_64):
>
> movswl (%rdi),%eax
> addl $0x5,0x0(%rip)
> retq
>
> With T defined as char it generates this:
>
> mov 0x0(%rip),%edx
> lea 0x2(%rdx),%eax
> add $0x5,%edx
> mov %eax,0x0(%rip)
> movzbl (%rdi),%eax
> mov %edx,0x0(%rip)
> retq
>
> I believe this is because gcc must assume that 'char* p' can potentially
> point to the 'int count' variable.
>
> However in my program I know for sure that the two don't alias (of
> course this is a much simplified version, just to show the problem). I
> tried adding the restrict keyword, but without success. I also couldn't
> find a useful type or variable attribute.
Previous discussions indicate there is a bug in gcc which makes restrict
ineffective for scalars (I don't remember if there is an accepted C
terminology). In my examples, the recommended -fargument-noalias is
effective. I guess it's there to implement Fortran syntax.