Restricting Pointer Aliasing
lastmod 2024-07-19created 2019-02-256 edits
About restricting pointer aliases.
Pointer Aliasing
- [Recommended] Pointer Alias :: ZepehWAVE
Pointer aliasing: when two or more pointer variables hold the same memory location, those pointers are called aliases.
From the compiler's point of view, it is safe to assume that any pointer variables may be aliases of each other.
If pointers a and b are not aliased, the value of a only needs to be loaded once. If they are aliased, a modification to the value pointed to by b may be a modification to the value pointed to by a, so the value of a must be loaded twice.
When the code above is compiled, it becomes the following.
; 포인터 에일리어스가 가능한 상태에서 컴파일된 코드
ld [$o0], %o5 ! Load *a
add %o5, %o5 ! Increment
st %05, [%o0] ! Store *a // 첫 번째 변수 저장
ld [%01], %o4 ! Load *b // 두 번째 변수 읽어들임
add $o4, 1, $o3 ! Increment
st %o3, [%o1] ! Store *b
Because of pointer aliasing, the value must be written (store) to the address pointed to by pointer variable a before reading (load) the value from the address pointed to by pointer variable b. The following is a case without pointer aliasing.
When the code above is compiled, it becomes this.
;포인터 에일리어싱 없이 컴파일된 코드
ld [%o0], %o5 ! Load a[0]
ld [%o0+4], %o4 !Load a[1] // 두 번째 변수를 load
add %o5, 1, %o5 ! Increment
st %o5, [%o0] ! Store a[0] // 첫 번째 변수 저장
add %o4, 1, %o3 ! Increment
st %o3, [%o1] ! Store a[1]
Since the compiler can know that there is no aliasing between the two memory operations, it can reorder instructions to move the memory read earlier, reducing the impact of memory access latency.
The restrict keyword
The restrict keyword tells the compiler that a pointer used for output (destination!) will not point to the same element as the arguments,
and the compiler trusts this notice when performing optimization. This keyword is standard in C but not in C++.
Restricted Pointers
As with the C front end, G++ understands the C99 feature of restricted pointers, specified with the __restrict__, or __restrict type qualifier. Because you cannot compile C++ by specifying the -std=c99 language flag, restrict is not a keyword in C++.
In addition to allowing restricted pointers, you can specify restricted references, which indicate that the reference is not aliased in the local context.
In the body of fn, rptr points to an unaliased integer and rref refers to a (different) unaliased integer.
You may also specify whether a member function’s this pointer is unaliased by using __restrict__ as a member function qualifier.
Within the body of T::fn, this has the effective definition T *__restrict__ const this. Notice that the interpretation of a __restrict__ member function qualifier is different to that of const or volatile qualifier, in that it is applied to the pointer rather than the object. This is consistent with other compilers that implement restricted pointers.
As with all outermost parameter qualifiers, __restrict__ is ignored in function definition matching. This means you only need to specify __restrict__ in a function definition, rather than in a function prototype as well.