This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Possible missed optimization opportunity with const?
- From: Toshi Morita <tm314159 at yahoo dot com>
- To: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Wed, 17 Aug 2016 00:21:40 +0000 (UTC)
- Subject: Possible missed optimization opportunity with const?
- Authentication-results: sourceware.org; auth=none
- References: <1637972460.15325965.1471393300211.JavaMail.yahoo.ref@mail.yahoo.com>
- Reply-to: Toshi Morita <tm314159 at yahoo dot com>
I was involved in a discussion over the semantics of "const" in C, and the following code was posted:
#include <stdio.h>
int foo = 0;
const int *pfoo = &foo;
void bar (void)
{
foo +=3D;
}
int main(void)
{
int a, b;
a = *pfoo;
bar();
b = *pfoo;
printf("a: %d, b: %d\n", a, b);
}
This code when compiled with gcc 4.8.2 using the optimization option -O3 produces:
a: 0, b: 1
So it appears even though pfoo is a const int *, the value *pfoo is read twice.
Would it be valid for the code to print a:0, b: 0?
If so, is this a missed optimization opportunity?
Toshi