Search code examples
loopsllvmbitcode

Extracting loops in llvm bitcode from functions/modules


Is it possible to extract from llvm bitcode the loop(s) inside a function or module? I'm migrating an LLVM LoopPass to an independent bitcode reader/writer application. The major reason for the migration is the hardship of debugging opt passes, but there are other reasons too. I've searched the API of Function/Module/BasicBlock, but couldn't find anything close. Since the LoopPass does this exactly, I'm pretty sure it's possible right? I mean something like this:

Function f;
for (auto loop = f.getLoops().begin(); loop != f.getLoops().end(); loop++)
{
    // process loop here
}

Solution

  • What you want is called LoopInfo and you create it via a dominator tree:

    DominatorTree dt(function);
    LoopInfo li(dt);
    for(Instruction * foo : bar) {
      Loop * l = li.getLoopFor(foo->getParent());