Aliasing of arrays

Alexander Cherepanov ch3root@openwall.com
Wed Nov 30 13:19:00 GMT 2016


Hi!

Pascal Cuoq communicated to me the following example:

int ar1(int (*p)[3], int (*q)[3])
{
   (*p)[0] = 1;
   (*q)[1] = 2;
   return (*p)[0];
}

gcc of versions 4.9.2 and 7.0.0 20161129 optimize it with -O2 on the 
premise that elements with different indices don't alias:

0000000000000000 <ar1>:
    0:	c7 47 0c 01 00 00 00 	movl   $0x1,0xc(%rdi)
    7:	b8 01 00 00 00       	mov    $0x1,%eax
    c:	c7 46 10 02 00 00 00 	movl   $0x2,0x10(%rsi)
   13:	c3                   	retq

That's fine. But then I expect that gcc will also assume that arrays of 
different known lengths don't alias, i.e. that gcc will optimize this 
example:

int ar2(int (*p)[8], int (*q)[7]) {
   (*p)[3] = 1;
   (*q)[3] = 2;
   return (*p)[3];
}

But this is not optimized:

0000000000000020 <ar2>:
   20:	c7 47 0c 01 00 00 00 	movl   $0x1,0xc(%rdi)
   27:	c7 46 0c 02 00 00 00 	movl   $0x2,0xc(%rsi)
   2e:	8b 47 0c             	mov    0xc(%rdi),%eax

Is this behavior fully intentional, is the first example optimized too 
aggressively, is an optimization missed in the second example, or is the 
situation more complex?

-- 
Alexander Cherepanov



More information about the Gcc mailing list