4 ms·
Interesting, it seems Clang and GCC handle the following example differently: i = 0; while (i != 1) i ^= 2; return i; GCC turns it into an in
by messe 4y ago
Interesting, it seems Clang and GCC handle the following example differently:
i = 0;
while (i != 1)
i ^= 2;
return i;
GCC turns it into an infinite loop[1]:
.L2:
b .L2
Whereas Clang (and, MSVC) remove the loop entirely[2][3], and just return 1:
mov w0, #1
ret
[1]: https://godbolt.org/z/Efv6sG1n5 https://godbolt.org/z/Efv6sG1n5
[2]: https://godbolt.org/z/5Yj8K5vzr https://godbolt.org/z/5Yj8K5vzr
[3]: https://godbolt.org/z/M6vq5KKnj https://godbolt.org/z/M6vq5KKnj
- moonchild 4y agoGCC does take advantage of the forward progress guarantee in other cases, but seemingly only in c++ mode; see https://godbolt.org/z/Won5zETKx https://godbolt.org/z/Won5zETKx. I wonder why. C provides forward progress guarantees only for loops, whereas C++ provides them for all code. Perhaps GCC did not want to bother to annotate individual control edges (as llvm does with 'mustprogress'), and they perform such optimisations after canonicalising loops and other irregular mechanisms for control flow into a homogenous CFG.