Hi,
I’m surprised by the result of compiling the following lines of code:
for (int i = 0; i < RANDOM_CHUNKS; i++) {
for (int j = 0; j < RANDOM_CHUNK_SIZE; j++) {
random_text[i][j] = (int)(ran()*256);
}
}
The problem happens when -fsanitize=undefined, -fno-sanitize-recover and -O3 are enabled. In this case, UndefinedBehaviorSanitizer inserts check for array index out of bounds, and for cast-to-int overflow. The loop unswitching pass presumably tries to move these out of the loops. Thereby, the loop condition of the outer loop gets duplicated, and one of the copies branches to a dead block.
This is a problem at the interplay between multiple optimization passes. Maybe there isn’t an easy solution. But I thought I’d post it here since people might be interested.
Attached are a complete C file reproducing the problem, as well as the resulting Bitcode. Compilation was done using clang -Wall -g -flto -O3 -fsanitize=undefined -fno-sanitize-recover -c weird_loop.c
Cheers,
Jonas
weird_loop.c (311 Bytes)
weird_loop.o.ll (11.8 KB)