Fw: Possible missed optimization opportunity with const?
Toshi Morita
tm314159@yahoo.com
Wed Aug 31 09:01:00 GMT 2016
Resending to the list, because I didn't see this show up in the archives.
IWhen this code is compiled with gcc-4.8.2 using -O2:
#include <stdio.h>
extern const int foo;
const int * const pfoo = &foo;
extern void bar(void);
int main(void)
{
int a, b;
a = *pfoo;
bar();
b = *pfoo;
printf("a: %d, b: %d\n", a, b);
}
The two reads of foo are optimized down to one read, as expected:
test.c.025t.esra:
...
a_2 = foo;
bar ();
b_4 = foo;
...
test.c.026t.fre1
...
a_2 = foo;
bar ();
b_4 = a_2;
...
However, if the definition of pfoo is changed to:
const int * const pfoo = (const int * const 0x1234);
the optimization seems to fail:
test.c.025t.esra:
a_3 = MEM[(const int *)4660B];
bar ();
b_6 = MEM[(const int *)4660B];
...
test.c.026t.fre1
(no change)
The output code seems to have two reads of 0x1234 as well:
test:
pushq %rbx
movl 4660, %ebx
call bar
movl %ebx, %edx
movl 4660, %ecx
movl $.LC0, %esi
popq %rbx
movl $1, %edi
xorl %eax, %eax
jmp __print_chk
I'm assuming "movl 4660, %ebx" is an indirect reference in GNU syntax and not an immediate reference.
This seems odd. Is this correct?
Toshi
More information about the Gcc-help
mailing list