This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

aliasing with char*


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.

Is there a way to convince gcc that the two cannot alias?

Thanks.


Wouter



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]