This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: attribute pure and gcse
- From: Roger Sayle <roger at eyesopen dot com>
- To: Dara Hazeghi <dhazeghi at yahoo dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Sat, 17 Apr 2004 17:07:02 -0600 (MDT)
- Subject: Re: attribute pure and gcse
On Sat, 17 Apr 2004, Dara Hazeghi wrote:
> (resent after no response on gcc-help@)
>
> according to the gcc 3.3 manual, functions marked pure
> should be candidates for common subexpression
> elimination.
>
> In the following case (which seems a good candidate),
> the second call to 'square' is not eliminated by gcse
> (leastaways not with 3.3 on ppc). Is this expected, or
> should cse be doing better here?
>
> static int square(int a) __attribute__ ((pure));
>
> static int square(int a)
> {
> int j=1, k=1;
> while(j!=a)
> {
> k = j*k;
> j++;
> }
> return k;
> }
>
> int main()
> {
> return square(100) * square(100);
> }
The problem is that in older versions of GCC, including 3.3, the "pure"
attribute wasn't correctly being preserved between the function prototype
and the function definition.
This was fixed by
>> 2003-04-13 Roger Sayle <roger@eyesopen.com>
>>
>> * c-decl.c (duplicate_decls): Preserve pure and malloc attributes.
Indeed, your code is correctly optimized by mainline GCC. I haven't
checked whether this patch made it in time for gcc 3.4, but I can
confirm that it was broken in 3.3.1.
A workaround is to repeat the function prototype including the pure
attribute *after* the function declaration, i.e.
static int square(int a) __attribute__ ((pure));
static int square(int a)
{
int j=1, k=1;
while(j!=a)
{
k = j*k;
j++;
}
return k;
}
static int square(int a) __attribute__ ((pure));
int main()
{
return square(100) * square(100);
}
Which allows the second call to square to be optimized away.
I hope this helps.
Roger
--