Tell GCC functions do not modify struct

Yangfl mmyangfl@gmail.com
Fri Mar 11 07:03:47 GMT 2022


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.

Thanks


More information about the Gcc-help mailing list