LoopInfo of a basic block

Hi all,

How Can I get the Loops around an Instruction?

I know I can get the basic block of an instruction using inst.getParent() which returns a BasicBlock*. Now I want to use the getLoopFor(BasicBlock) method of the class LoopInfo.
But I dont know how to get the LoopInfo.

BasicBlock* bb = inst.getParent();

(… what should I add here?)

Loop* innerloop = LI → getLoopFor(bb):

Thanks.

Two steps:

  1. In your pass’s getAnalysisUsage(), request LoopInfo.

virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired();
}

  1. In your runOnLoop() method get the analysis.

LoopPass *LI = &getAnalysis();

Now, you can do LI->getLoopFor(…)

To add to what Devang has said, you may find the doxygen docs on LoopInfo helpful:

– John T.

Thanks Devang and John. My pass is actually a loop pass, not a function pass. So, I couldnt override the getAnalysisUsage.
I am in a loop pass, I know the outermost loop. Now How can I get the loopInfo from here? I couldn’t find helpful methods in the LoopInfo class documents.

Thanks Devang and John. My pass is actually a loop pass, not a function pass. So, I couldnt override the getAnalysisUsage.

A LoopPass is no different from a FunctionPass here. You can use getAnalysisUsage and getAnalysis in a LoopPass.

Actually it didnt require the getAnalysisUsage in the loop pass. I was just able to call the getAnalysis in my runOnLoop. That solved my problem.

Thanks guys.