[LLVMdev] Loop unswitching creates dead code

Jonas Wagner jonas.wagner at epfl.ch
Mon Apr 7 09:24:10 PDT 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140407/9d48be17/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: weird_loop.c
Type: text/x-csrc
Size: 311 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140407/9d48be17/attachment.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: weird_loop.o.ll
Type: application/octet-stream
Size: 12089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140407/9d48be17/attachment.obj>


More information about the llvm-dev mailing list