I must be missing some language rule, since unless the variable (or the ptr) is volatile or atomic, I thought that a C or C++ compiler usually might move the store-to-memory out of the loop as well. (In the example given in the Twitter post.)
EDIT: And I'd think that on the other hand, if either var or ptr are volatile or atomic, the load can't be moved out of the loop either.
For easier reference, this is the example given:
EDIT 2: Taking the example literally, I'd think an optmizing compiler might unroll the loop, discover it will terminate in the second iteration the latest, and replace it with:
(no loop)
...and if both ptr and *ptr are not volatile or atomic, do all that without having to "prove" anything.
EDIT: And I'd think that on the other hand, if either var or ptr are volatile or atomic, the load can't be moved out of the loop either.
For easier reference, this is the example given:
Code:
loop { var = *ptr; if (var) break; *ptr= var + 1; }
EDIT 2: Taking the example literally, I'd think an optmizing compiler might unroll the loop, discover it will terminate in the second iteration the latest, and replace it with:
Code:
var = *ptr; if (!var) *ptr = 1;
...and if both ptr and *ptr are not volatile or atomic, do all that without having to "prove" anything.
Comment