2 ms·
> What sort of optimizations does this assumption enable? Does it allow the compiler to freely reorder or parallelize the code? Consider this code: void foo
by MauranKilom 7y ago
> What sort of optimizations does this assumption enable? Does it allow the compiler to freely reorder or parallelize the code?
Consider this code:
void foo(int* data, const int& count)
{
for (int i = 0; i < count; ++i)
data[i]++;
}
The compiler has to account for the possibility of `count` being inside of `data`. It has to reload `count` from memory at every iteration. This also prevents e.g. auto-vecorization:
Compare https://godbolt.org/z/sNRKv4 https://godbolt.org/z/sNRKv4 vs https://godbolt.org/z/iJq5yn https://godbolt.org/z/iJq5yn.
Similar story if you manipulate data with multiple arrays in play: Unless the compiler knows (via `restrict`, type-based alias analysis or just seeing the definition) that two objects are non-overlapping, it has to correctly handle the worst case. See also this related discussion: https://news.ycombinator.com/item?id=20800076 https://news.ycombinator.com/item?id=20800076