Help to debug the miscompilation of a loop into an infinite loop


I found out that dumping the IR to disk and taking it back makes
a miscompile disappear. I would appreciate your help on how
to narrow down such a bug.

Here are more details: when I compile one of the SPEC benchmarks
with clang at -O3, the code is miscompiled producing an infinite loop.
If I split the compilation using -emit-llvm and then finish the compilation
from the .ll file with llc (I verified that the same passes are enabled)
I cannot reproduce the bug exposing the infinite loop.

My guess is that one of the target independent passes is invalidating
the results of an analysis without marking it for re-computation. When
the IR is dumped to disk and read back, the analysis results are forced
to be recomputed, and that makes the bug disappear.

Is there a systematic way to reduce such bugs?
I would like, for example, to invalidate the results of analyses at the end
of a set of passes. Otherwise, is there a way to enable/disable each
optimization pass from clang without the help of -emit-llvm and opt/llc?


First, make sure llc is actually doing the same thing as clang (-O3,
the same relocation model, frame pointer elimination, etc.)

clang can handle .ll files directly; that may be useful in some cases.
Also, the "-mllvm -disable-llvm-optzns" command-line argument to
clang can be useful; it turns off the mid-level optimizers.