Tell GCC functions do not modify struct
Martin Sebor
msebor@gmail.com
Mon Mar 14 21:43:57 GMT 2022
On 3/11/22 00:03, Yangfl via Gcc-help wrote:
> Hello Everyone,
>
> I'm playing with GCC optimization and come up with a quite common scenario:
>
> struct A { int a; };
>
> void print_a(const struct A *);
>
> int test() {
> const struct A a = {3};
> print_a(&a);
> return a.a == 3;
> }
>
> GCC always products a load operation of a.a, that's reasonable. But my
> question is, how to tell GCC that the function is guaranteed to not
> modify the struct?
>
> I've tried `access` attribute, but that does not help. Of course I can
> use __builtin_unreachable around the function call to make a promise,
> but that is not a struct-independent solution.
GCC doesn't yet use the access attribute for optimization.
The solution in standard C is to declare the pointer argument restrict:
void print_a (const struct A * restrict);
Regrettably, GCC doesn't yet take advantage of this notation for
optimization either. A request for this enhancement is being
tracked in pr81009:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81009
Martin
>
> Thanks
More information about the Gcc-help
mailing list