I'm trying to understand how the loop trip count calculation happens in LLVM using the Scalar Evolution analysis. However, I can't get a simple test case to work.
I have the following test program:
// bug.cpp
int main()
{
for (int i = 0; i < 16; i++) {
// do nothing
}
return 0;
}
Which I compile like this:
$ clang++ -O0 -emit-llvm -c -o bug.bc bug.cpp
And analyze like this:
$ opt -analyze -indvars -loop-simplify -scalar-evolution < bug.bc
But the output of the scalar evolution is this:
...
Determining loop execution counts for: @main
Loop %for.cond: Unpredictable backedge-taken count.
Loop %for.cond: Unpredictable max backedge-taken count.
Why is this very simple loop have an unpredictable trip count? Am I doing something wrong in invoking the analysis? This is LLVM 3.4.
Solved with the help of this related question:
The use of getSmallConstantTripCount method of Loop in LLVM
Add mem2reg
as an optimization pass and the trip count is correctly computed.
$ opt -analyze -mem2reg -indvars -loop-simplify -scalar-evolution < bug.bc