title: LLVM error accessing loopinfo in function pass
body: <p>I'm trying to get loop information from IR by writing a function pass. So I followed some examples and wrote like following. I'm not very familiar with writing passes and pass managers.    </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "llvm/LLVMContext.h"
  #include "llvm/Module.h"
  #include "llvm/Function.h"
  #include "llvm/BasicBlock.h"
  #include "llvm/Analysis/LoopInfo.h"
  #include "llvm/Support/IRReader.h"
  #include "llvm/Support/SourceMgr.h"
  #include "llvm/Support/raw_ostream.h"
  #include "llvm/Analysis/LoopInfo.h"
  #include "llvm/Analysis/LoopPass.h"
  #include "llvm/Pass.h"
  #include "llvm/PassManager.h"
  
  using namespace llvm;
  
  namespace {
      class DetectLoop: public FunctionPass {
      public:
          DetectLoop() : FunctionPass(ID) {}
  
          static char ID;
  
          virtual void getAnalysisUsage(AnalysisUsage &amp;AU) const {
              AU.addRequired&lt;LoopInfo&gt;();//I'm not sure if it's correct here *****1*****
          }
  
          virtual bool runOnFunction(Function &amp;F) {
              if (!F.isDeclaration())
                  LoopInfo &amp;li = getAnalysis&lt;LoopInfo&gt;(F);//*****2*****
              for (Function::iterator I = F.begin(); I != F.end(); I++) {
                  BasicBlock *BB = I;
                  for (BasicBlock::iterator BI = BB-&gt;begin(), BE = BB-&gt;end(); BI != BE;) {
                      Instruction &amp;I = *BI++;
                      //did noting inside
                  }
              }
              return false;
          }
      };
  }
  
  char DetectLoop::ID = 0;
  
  int main(int argc, char** argv)
  {
      if (argc &lt; 2) {
          errs() &lt;&lt; "Expected an argument - IR file name\n";
          exit(1);
      }
  
      SMDiagnostic Err;
      std::cout&lt;&lt;argv[1]&lt;&lt;std::endl;
      Module *Mod = ParseIRFile(argv[1], Err, getGlobalContext());
      Err.Print(argv[0], errs());
  
  
      if (Mod) {
          PassManager PM;
          PM.add(new DetectLoop());
          PM.add(new LoopInfo());//*****3*****
          PM.run(*Mod);
      }
      else {
          std::cout &lt;&lt; "Mod is null" &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>While I was running this program, it just showed me <code>segmentation error(core dumped)</code>,</p>
  
  <p>but when I commented out <strong>addRequired</strong> the error msg I got was</p>
  
  <pre><code>IRparser: PassManager.cpp:1200: virtual llvm::Pass* llvm::PMDataManager::getOnTheFlyPass
  (llvm::Pass*, llvm::AnalysisID, llvm::Function&amp;): Assertion `0 &amp;&amp; "Unable to find on the fly pass"' failed.
  Stack dump:
  0.  Running pass 'Function Pass Manager' on module '../../testcase/forloop1.ll'.
  1.  Running pass 'Unnamed pass: implement Pass::getPassName()' on function '@main'
  Aborted (core dumped)
  </code></pre>
  
  <p>I have marked 3 places I'm not sure which is correct or not. Can anyone help me with that?</p>