4 ms·
The function either returns 1 or loops infinitely. So if you assume that infinite loops are undefined behavior, the optimization is valid. I only know what the
by nwellnhof 7y ago
The function either returns 1 or loops infinitely. So if you assume that infinite loops are undefined behavior, the optimization is valid. I only know what the C11 standard says about infinite loops:
> An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate.
But this only applies to some iteration statements, not recursive functions. I don't know about C++ rules but since this optimization is also applied when compiling as C code (https://godbolt.org/z/K3uT2G https://godbolt.org/z/K3uT2G), it seems to be invalid.
- olliej 7y agoI believe infinite recursion is also UB, but alternatively if clang converts to a tail call, it can then be converted to a loop. If the loop is infinite then you’re back in UB land.
- nwellnhof 7y ago> if clang converts to a tail call, it can then be converted to a loop Yes, that's probably what happens internally. > If the loop is infinite then you’re back in UB land. I don't think so. At some point, clang forgets that the loop actually comes from a recursive function call, so it shouldn't be allowed to assume a finite loop.