This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Valid optimization with constant arrays
- From: "Andrew Pinski" <pinskia at gmail dot com>
- To: "GCC Development" <gcc at gcc dot gnu dot org>
- Date: Mon, 17 Nov 2008 18:46:38 -0800
- Subject: Valid optimization with constant arrays
I noticed that for a simple testcase:
int t;
void abort (void);
int f(int t, const int *a)
{
const int b[] = { 1, 2, 3};
if (!t)
return f(1, b);
return b == a;
}
int main(void)
{
if (f(0, 0))
abort ();
return 0;
}
--- CUT ---
That before 4.0 gives a different result from 4.0 and above. Is it
valid in this case to promote b to a static variable and have f return
true when called with f(0, 0) ?
I think C99 does allows this optimization (at least according to the
normative note 112) but C++ does not. I could not find anything in
C++ standard which says the compiler could put the const object in a
read-only section but I could be missing something somewhere.
Thanks,
Andrew Pinski