c - Does `restrict` affect aliasing of passed pointers to anything but each other -
one of major uses of restrict keyword added c99 allow compilers load register , assume register mirror state of variable loaded. given
void foo1(int * restrict a, int * restrict b) { (*a)++; (*b)++; (*b)+=(*a); } a compiler entitled assume write (*b) not have affected (*a), avoiding need reload (*a) after it. restrict have other effects on aliasing? 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 compiler required anticipate increment of *q, , calls foo2a() , foo2b() might have disturbed x, , calls might "interested" in value of x , *q? compiler required assume call foo2a() might have persisted parameter--even though marked restrict, such foo2b() modify z?
if compilers required operate under worst-case assumptions, despite restrict keyword, there way grant permission compiler ignore normal obligation store changes variables prior function call , reload next time it's needed?
to answer question in title: yes. restrict qualified pointer means guarantee whole object in question can accessed through pointer alone. means can't alias file scope objects of same type, e.g.
for rest of question, mixing things. restrict no means guarantee of caller. caller doesn't "see" restrict keyword, type qualification not part of interface. whether or not file scope variable may have changed when returning call has nothing restrict.
restrict guarantee caller gives callee may use information internally optimization.
Comments
Post a Comment