49 ms·
I wonder if these estimations of integer overflow cost have taken into account all the possible optimizations where the compiler could safely remove overflow ch
by dezgeg 12y ago
I wonder if these estimations of integer overflow cost have taken into account all the possible optimizations where the compiler could safely remove overflow checks. For example, consider:
void memzero_range(char* array, int start, int end) {
for(int i = start; i < end; i++)
array[i] = 0;
}
In that example, a sufficiently smart compiler could just check for the `start > end` case once at the start of the loop, and remove the overflow check from the body. Perhaps this is something that today's Clang/GCC can already do?
- gsnedders 12y agoRewriting it as follows, with an overflow check: void memzero_range(char* array, int start, int end) { int i = start; while (i < end) { array[i] = 0; if (i == INT_MAX) { assert("overflow!"); } i++; } } Clang does indeed manage to see that the check is equivalent to a start > end case (with -O1) and hoists it out of the loop. It also then rewrites the body of the loop to use the LLVM memset intrinsic!