This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Fw: Possible missed optimization opportunity with const?
- From: Toshi Morita <tm314159 at yahoo dot com>
- To: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 31 Aug 2016 08:57:12 +0000 (UTC)
- Subject: Fw: Possible missed optimization opportunity with const?
- Authentication-results: sourceware.org; auth=none
- References: <1637972460.15325965.1471393300211.JavaMail.yahoo.ref@mail.yahoo.com> <1637972460.15325965.1471393300211.JavaMail.yahoo@mail.yahoo.com> <2c496f2e.1b46.15698e63833.Coremail.lh_mouse@126.com> <CAEwic4aRutFFCeb-gdKYJPiUnwtzXHx4+T4CrydSQkJ1Zra-dA@mail.gmail.com> <839193122.1815820.1472544155134@mail.yahoo.com>
- Reply-to: Toshi Morita <tm314159 at yahoo dot com>
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