6 ms·
The optimizer doesn't work that way. It assumes the code does not do UB and then removes branches that can never be taken if the code executes no UB. That's di
by dom0 9y ago
The optimizer doesn't work that way. It assumes the code does not do UB and then removes branches that can never be taken if the code executes no UB.
That's different from "This is UB, and now I do ...".
- cousin_it 9y agoI think we deserve a compiler warning whenever a source line of code (not a line generated by the preprocessor) gets removed due to UB exploitation. All such cases are worth the programmer's attention, because they are either useless lines or bugs.
- quantdev 9y agoIt's more complicated than this, unfortunately. Imagine an int variable x and two expressions f(x) and g(x) of x. Now imagine an if statement with condition f(x) < g(x). Precisely because incrementing the largest int is undefined behavior, the compiler may be able to simplify the if condition because, for example, it can assume x+1 > x is always true. So it's not necessary for the line to be useless or wrong for undefined behavior rules to trigger compiler optimizations.
- deleted 9y ago[deleted]
- caf 9y agoYou must also account for the case where the source line removed was only useless because of the specific context it had been inlined to by an earlier stage of the optimiser.
- deleted 9y ago[deleted]