Can clang detect restricted pointers without the restricted keyword

The restrict keyword is rarely used in c applications and frameworks. Can clang automatically detect pointers that are mutually exclusive and optimize according to such or is the keyword needed for such optimizations?

Finally does macos and iOS have their functions in C optimized like this as well?

Replies

Depending on the context, the compiler may be able to determine that memory accessed via some pointers do not overlap, even without the restrict keyword.

For example, the compiler can determine that pointers to different stack objects cannot overlap. See https://godbolt.org/z/cs7MMoehW

In the function store_restrict, the compiler is able to remove the first store to a (*a = 0) thanks to restrict. The same optimization cannot be done in store_without_restrict. In inline_store, pointers to different stack objects are passed to store_without_restrict and after inlining the compiler can prove the pointers do not overlap and can perform the optimization on the inlined code, without requiring restrict.

Also, for some optimizations, the compiler can generate runtime checks if it cannot prove statically that the accessed memory does not overlap.