This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: how do I promise const to the optimizer?


Marco Manfredini <mldb@gmx.net> writes:

> 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? 

Unfortunately a const pointer in C/C++ doesn't mean "the value this
points to can not change."  It only means "the value this points to
can not be changed via this pointer."  So there is no way to express
what you want in the standard language.  Nor does gcc provide an
extension along these lines.

gcc does provide the extension of annotating fun with __attribute__
((pure)).  If fun is indeed a pure function, then gcc might be perform
the optimization you want.  I haven't tried.  Of course, if fun is not
pure, that won't help.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]