How a module pass can use analysis information?

I am trying to write a llvm module pass by overriding the runOnModule(Module &M), but my pass needs the RegioinInfo analysis to find regions to get my pass working.

I tried to do the following in my module pass:

RegionInfoPass RIP;
if (RIP.runOnFunction(*F1)) {
assert(false && “Failed to run runOnFunction”);
RegionInfo *RI = &RIP.getAnalysis().getRegionInfo();

It compiles without problem, but when I run it, I got the following errors:

AnalysisType&llvm::Pass::getAnalysis() const [with AnalysisType = llvm::RegionInfoPass]:
Assertion `Resolver && “Pass has not been inserted into a PassManager object!”’ failed.

I also tried to use RGPassManager as below:

RGPassManager RGPM;
if (RGPM.runOnFunction(*F1)) {
assert(false && “Failed to run runOnFunction”);

Again, it compiles without problem, but when I run it, I got the same errors as above.

Is anyone can show me what is the correct way for a module pass to use RegionInfo analysis?


Using the legacy pass manager, it is generally not possible to use
lower-level passes (The only exception is that module passes can use
function passes using `getOnTheFlyPass`).

You can still create your own pass manager hierachy. An example is here:

You need to create a whole stack of pass managers. Unfortunately I
don't know how to reuse the dependent analyses from the 'main' pass
manager stack (eg. TargetLibraryInfoWrapperPass)