kiznit wrote:
Do you have an example where you want two pointers of different types to be aliased? I don't believe I've run into this myself.
That's definitely the less common case; I mostly want to be able to declare that two pointers are definitely not aliased. Still, there are cases, especially in OS development, where you want to violate the strict aliasing rule. IIRC, the Linux kernel to this day sets -fno-strict-aliasing, because there are enough key points where type punning is necessary.
kiznit wrote:
As for your second use case, two pointes of the same type not aliasing each other, wouldn't __builtin_assume( ptr1 != ptr2 ) work?
No, because ptr1 != ptr2 is not a sufficient condition for non-aliasing, at least in C/C++. For example:
Code:
void foo(char *ptr1) {
char *ptr2 = &ptr1[1];
assert(ptr1 != ptr2); // does not fail
// but, ptr2[2] = 3 makes ptr1[1] == 3
// so the pointers still may alias
}
So, the compiler can't safely infer just from that assert/assume that the pointers don't alias, which means it won't.