This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Why the const attribute cannot work?
- From: "=?gb2312?B?s8K/0g==?=" <chenken at mprc dot pku dot edu dot cn>
- To: <gcc at gcc dot gnu dot org>
- Date: Fri, 8 Mar 2002 14:30:18 +0800
- Subject: Why the const attribute cannot work?
Dear Colleagues:
As told by Using and Porting GNU CC , functions with const attribute can
be subject to common subexpression elimination and loop optimzation just as
an arithmetic operator would be.But when involving the program below ,I
found GCC cannot deal with it laconically
int muladd(int a,int b) __attribute__ ((const));
int muladd(int a, int b)
{
return a*b+b;
}
main()
{
int x = 1, y = 2, z = 3;
int result = muladd(x,y) * (muladd(x,y) + muladd(x,y) - 1 + x * y) -
x * y;
if (result != 34) abort();
exit(0);
}
it will call function muladd three times instead of only once.When I add -O2
option, the compiler with the x86 backend can recognize the common muladd
function and optimize the code, but the compiler with the arm backend still
cannot recognize it and still call the function three times.
I donnot know why the arm compiler cannot do the common muladd function
elimination, and why the compilers with x86 and arm backends treat the same
program differently by this question.As we know, the common expression
elimination will be done in the frontend, which is independent of the target
machine.
By the way, the version I use is 2.9.5.
Thanks for your help.