ScalarEvolution class returns no valid loop exit count


I have problems to estimate the loop exit count of a simple loop with the ScalarEvolution class.

simple loop:

int a = 0;
for(int i; i < 10; ++i){
a = a + 1;

For the loop analyzation I use the ScalarEvolution class with the following initialization:

void analysis(Function* func)
DominatorTree DT = DominatorTree();

LoopInfoBase<BasicBlock, Loop> LIB;

for(auto&bb :*func){
Loop * L = LIB.getLoopFor(&bb);
if(L != nullptr){
AssumptionCache AC = AssumptionCache(*bb.getParent());
Triple MT(llvm::sys::getDefaultTargetTriple());
TargetLibraryInfoImpl TLII(MT);
TargetLibraryInfoWrapperPass TLI = TargetLibraryInfoWrapperPass(TLII);

LoopInfo LI = LoopInfo(DT);

ScalarEvolution SE = ScalarEvolution(*func, TLI.getTLI(),AC, DT, LI);
BasicBlock * exitingblock = L->getUniqueExitBlock();
const SCEV * exitingcount = SE.getExitCount (L,exitblock );

Unfortunately I get this result by printing the SCEV exitingcount variable:

It has been tested that the exitingblock of the loop was calculated successfully.

The LLVM IR is parsed and analysed without the usual pass framework. For this reason

the use of the getAnalysis() is not possible. The question is wether there
is something I`m doing wrong during the initialization of the ScalarEvolution class? Or is
the use of LLVM analysis classes without the passmanager framework not possible?

Best regards!


Are you sure exitingblock is not null? This should probably be an
assert in getExitCount itself.

Other than that, SCEV normally expects that the loops have been
canonicalized and some basic optimizations have been performed. How
are you obtaining the IR you're passing to analysis?

-- Sanjoy

Hi Sanjoy,

the LLVM IR is parsed with following expression:

string file = “…”;
static LLVMContext cont;
SMDiagnostic diag;
unique_ptr module = parseIRFile(file, diag, cont);

I mixed up the LLVM loop exit block with the loop exiting block in my analyses code. But after
resolving this issue, the exiting block is successfully determined:

BB1: ; preds = %BB3, %BB0
%3 = load i32, i32* %2, align 4
%4 = icmp slt i32 %3, 10
br i1 %4, label %BB2, label %BB4

Nevertheless the loop exiting count can not be calculated with the ScalarEvolution class. I will try to
do the loop optimizations.

Many greetings


Your Loop *L is from a different LoopInfo object than the one you’re giving to ScalarEvolution. So your SE doesn’t know your loop, and can’t tell you anything about it.