LoopInfo Analysis Fails on Module Pass

Hi,

I am trying to obtain LoopInfo Analysis from a Module Pass. My code goes like:

    void SomePass::someFunction (Module &M) {

    for (auto &F : M) {

           LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>(F).getLoopInfo(); //doing this gives me error shown below

           for (auto &B : F) {

                       //some use of LI
             }
         }

      }

   The runtime error message that I am getting is:

   opt: /usr/lib/llvm-10/include/llvm/PassAnalysisSupport.h:262: AnalysisType& llvm::Pass::getAnalysisID(llvm::AnalysisID, llvm::Function&) [with AnalysisType = llvm::LoopInfoWrapperPass; llvm::AnalysisID = const void*]: Assertion `ResultPass && "Unable to find requested analysis info"' failed.

  I also have defined the analysis usage function as follows:

      /*void getAnalysisUsage(AnalysisUsage &AU) const
     {
             //AU.setPreservesAll(); // I am actually instrumenting the IR, so I am not sure this is required. The error occurs regardless.
             AU.addRequired<LoopInfoWrapperPass>();
     }*/

   Is there anything that I am missing here?

   Thanks,
   Bodhi

For the legacy pass manager, it is not intended to invoke passes for
inner structure units (here: LoopInfo is a FunctionPass) from larger
structure passes (here: ModulePass). However, there is an exception
for invoking FunctionPasses from ModulePasses call "on-the-fly" but
which keeps only the analysis of a single function active at a time.

In your case, the LoopInfoWrapper pass must have been registered
before it can be used. Call

  PassRegistry &Registry = *PassRegistry::getPassRegistry();
  initializeAnalysis(Registry);

to do so.

Michael