dalibocai
dalibocai

Reputation: 2347

The use of getSmallConstantTripCount method of Loop in LLVM

In my pass, I add LoopInfo as a required pass. Then I'd like to print the constant loop trip count of each loop if it has one. However, every time I called getSmallConstantTripCount, it returns 0, even for a very simple loop:

for(i=0; i<3; ++i) {;}  

Any idea why?

Upvotes: 2

Views: 1424

Answers (1)

Nick Lewycky
Nick Lewycky

Reputation: 316

LLVM has a principle of making each part do the least amount of work. LoopInfo::getSmallConstantTripCount doesn't do any fancy analysis, it looks for a simple loop with a single backedge that increments a value by 1 each time and is compared using != against a constant integer.

When you compile the code you wrote at -O0, every "i < 3" actually causes a load from memory to read in the latest value of 'i'. LoopInfo certainly isn't going to do the type of analysis necessary to figure out that the memory accesses aren't needed, that's the job of "opt -mem2reg". Try running that optimization, and maybe -instcombine -loopsimplify -loop-rotate over the code to get it into the shape the getSmallConstantTripCount will handle.

Upvotes: 5

Related Questions