How to identify the first BB in a loop of non-constant trip count?

Hi,

The compilation of the following code

void f(int *a, int b, int n)
{
int i;
for(i=0; i< n; ++i)
a[i] = b[i];
}

gives the IR:

define void @f(i32* nocapture %a, i32* nocapture %b, i32 %n) nounwind {
%1 = icmp sgt i32 %n, 0
br i1 %1, label %.lr.ph, label %._crit_edge

.lr.ph: ; preds = %0
%tmp = zext i32 %n to i64
br label %2

; :2 ; preds = %2, %.lr.ph
%indvar = phi i64 [ 0, %.lr.ph ], [ %indvar.next, %2 ]
%scevgep = getelementptr i32* %a, i64 %indvar
%scevgep2 = getelementptr i32* %b, i64 %indvar
%3 = load i32* %scevgep2, align 4, !tbaa !0
store i32 %3, i32* %scevgep, align 4, !tbaa !0
%indvar.next = add i64 %indvar, 1
%exitcond = icmp eq i64 %indvar.next, %tmp
br i1 %exitcond, label %._crit_edge, label %2

._crit_edge: ; preds = %2, %0
ret void
}

My question is how can I identify the first BB in a loop of non-constant trip count? In this example, it is the first BB of the function, which decides whether going to the loop body or not. What’s the name of such BBs? For example, some BBs have the name preheader or header, …

Bo

Hi,

you can use LoopInfo for this:

LoopInfo& LI = getAnalysis<LoopInfo>();

You can ask LoopInfo for all kinds of information (e.g. which loop a block belongs to or iterate over the loop tree). It can return an object of type Loop which can be queried for the loop header (first block to be executed), preheader (unique block outside the loop from which the header can be reached), latch (block from which a back edge leads back to the header, exit blocks, etc.

Cheers,
Ralf

P.S. Be sure to add AU.addRequired<LoopInfo>() to getAnalysisUsage() and use the INITIALIZE_PASS_DEPENDENCY macro. Have a look at other passes in the LLVM source that use loop info.