One of the major uses of restrict
keyword that was added to C99 is to allow compilers to load something into a register and assume that the register will mirror the state of the variable thus loaded. Given
void foo1(int * restrict a, int * restrict b) {
(*a)++; (*b)++; (*b)+=(*a);
}
a compiler is entitled to assume that the write to (*b)
will not have affected (*a)
, thus avoiding any need to reload (*a)
after it. Does restrict
have any other effects on aliasing? For example, given:
extern void foo2a(int * restrict q);
extern void foo2b(void);
int x;
int foo2(restrict int *q) {
int z=x;
x++; *q++; x++;
foo2a(&z);
x++; *q++; z++;
foo2b();
x++; *q++; z++;
return x+(*q)+z;
}
would a compiler be required to anticipate that the increment of *q
, and the calls to foo2a()
and foo2b()
might all have disturbed x
, and that the calls might be "interested" in the value of x
and *q
? Would a compiler be required to assume that a call to foo2a()
might have persisted its parameter--even though it was marked restrict
, such that foo2b()
could modify z
?
If compilers would be required to operate under worst-case assumptions, despite the restrict
keyword, is there any way to grant permission for a compiler to ignore any normal obligation to store any changes to certain variables prior to a function call and reload it the next time it's needed?