[LLVMdev] Keeping a std::vector<Loop*> in a ModulePass

Bilyan Borisov bilyan.borisov at gmail.com
Sat Dec 13 06:20:52 PST 2014


To whom it may concern,

I'm writing a LLVM ModulePass as a part of my undergraduate dissertation
and I need to keep a list of pointers to all Loops within a Module.
I've tried the following bit of code (inside a runOnModule() method in my
ModulePass, I also have getAnalysisUsage setup properly with LoopInfo added
as required)

  std::vector<Loop*> loops;

  for (Module::iterator F = M.begin(), FEND = M.end(); F != FEND; ++F){
    if (!(*F).isDeclaration()){
      LoopInfo& LI = getAnalysis<LoopInfo>(*F);
      for(LoopInfo::iterator l = LI.begin(), lend = LI.end(); l != lend;
++l){
        loops.push_back(*l);
      }
    }
  }

However, if I try to print out the loops with

  for(auto l: loops){
    errs() << *l << "\n";
  }

I get runtime errors. Now, I understand why this is happening - the
LoopInfo object is not living between iterations of the outermost loop and
thus all pointers are invalidated. I tried keep a pair std::pair<Function*,
int> of a function pointer and the corresponding position of the loop in
the list from LI.begin() to LI.end() but this causes runtime exceptions as
well. The code for that looks like this:

  std::vector<std::pair<Function*,int>> loops;
  for (Module::iterator F = M.begin(), FEND = M.end(); F != FEND; ++F){
    if (!(*F).isDeclaration()){
      LoopInfo& LI = getAnalysis<LoopInfo>(*F);
      int count = 0;
      for(LoopInfo::iterator l = LI.begin(), lend = LI.end(); l != lend;
++l){
        if ((*l)->getLoopDepth() == 0) errs() << **l <<"\n";
        loops.push_back(std::make_pair(F,count));
        ++count;
      }
    }
  }
  for(auto l: loops){
    LoopInfo& LI = getAnalysis<LoopInfo>(*l.first);
    errs() << *(*(LI.begin()) + l.second) << "\n";
  }

However, no luck again. Is there any way to keep pointers to all loops in a
Module Pass?

Thanks,

Bilyan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141213/6bb2428e/attachment.html>


More information about the llvm-dev mailing list