This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
how do I promise const to the optimizer?
- From: Marco Manfredini <mldb at gmx dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 25 Apr 2007 14:15:45 +0200
- Subject: how do I promise const to the optimizer?
I have an optimization issue, which can be demonstrated with this example:
typedef struct
{
char tag;
char data[255]; // too expensive to copy!
} val;
extern int fun(const val * __restrict__ p,int i);
int bar(const val *__restrict__ x, const int t)
{
return fun(x,x->tag*t)+fun(x,x->tag*t);
}
The problem is, assembly reveals that the multiplication is done twice and gcc
is obviously right by doing this, because it can't know whether calling fun()
changes the contents of x by aliasing or casting.
However, I know, by chance, that after calling fun() (which may have all kinds
of side-effects) x will always be unchanged, and as such x->tag*t can be
subject to CSE. How do I apply such a strong guarantee?
I can't do the CSE by hand, because in the actual code the situation arises
from inlining overloaded operators in c++.
Marco