> On 05/15/2013 11:01 AM, Richard Biener wrote: > >Now - if there would ever be an architecture where special call-site > >preparation > >is required for a callee to write to global memory then marking a function > >'const' > >when it does in fact write to global memory then GCC may choose to optimize > >the call site to not do that call-site preparation. At least that > >would be valid according to the current documentation. > > That's a good point. > > The more immediate concern is that the compiler could apply the > const attribute to the function definition itself and deduct that > code paths with global memory references are unreachable. > Apparently, this is something that Clang does in some cases.
It is bit crazy idea though :) Do you have reference to the corresponding thread? I wonder what would be motivations for this. BTW we deduce all loops to be finite within const/pure functions that is also bit crazy effect of the attribute. The memoization you mention is IMO not really safe even with current GCC. With bit of trickery one can convince GCC to early inline the memoizing const function in some cases and not in others. Optimizers will then expect your memoizing cache to not change across the non-inlined calls that may lead to wrong code. At the moment I can not think of anything that would break if you had pure/const function modifying global memory and restoring it before returning. Honza