LLVM - getAnalysisUsage()

Hi,

I am using llvm-3.8 for my project. Following is my getAnalysisUsage() method:

virtual void getAnalysisUsage(AnalysisUsage &AU) const override
{
   AU.setPreservesAll();
   AU.addRequired<X>();
   AU.addRequired<Y>();
   AU.addRequired<Z>();
}

Now, if I call getAnalysis<X>(*F), instead of invoking just the X
pass, all the passes, i.e., X, Y and Z are being invoked. Could anyone
help me in this regard?

Thanks

Does analysis X require X, Y, and Z itself?

No, X, Y and Z are independent to each other.

This is expected. With the current pass manager, the scheduling is done upfront. The “addRequired” directive tells the pass manager that before even starting to run your pass, it needs to schedule X, Y, and Z.
They will run even without the call to getAnalysis<X>(*F). The new pass manager (not complete yet but almost) will change that.

When running your pass with opt, you can add —debug-pass=Structure to see what the scheduling is.

I hope I didn’t misunderstand your question.

I have added -debug-pass=Structure, and found the following:

ModulePass Manager
    X Analysis
      Unnamed pass: implement Pass::getPassName()
    FunctionPass Manager
      Module Verifier
    Bitcode Writer
Pass Arguments: -x -y -z
  FunctionPass Manager
    X Analysis
    Y Construction
    Z Construction

Even for getAnalysis<Y>(*F) and getAnalysis<Z>(&F), all the passes X,
Y, Z are executed again. In total there are three places where
getAnalysis() directive has been used for X, Y, Z. However, for each
getAnalysis() call, all three passes are scheduled/executed.

This is expected if those analysis are not preserved.

I have added -debug-pass=Structure, and found the following:

ModulePass Manager
   X Analysis
     Unnamed pass: implement Pass::getPassName()
   FunctionPass Manager
     Module Verifier
   Bitcode Writer
Pass Arguments: -x -y -z
FunctionPass Manager
   X Analysis
   Y Construction
   Z Construction

This is suspicious to me, I think you try to anonymize the names but reused "X Analysis” two times (it shows up as a module pass the first time, and in a functionpass manager the second times).

Even for getAnalysis<Y>(*F) and getAnalysis<Z>(&F), all the passes X,
Y, Z are executed again. In total there are three places where
getAnalysis() directive has been used for X, Y, Z. However, for each
getAnalysis() call, all three passes are scheduled/executed.

Again the “getAnalysis()” has nothing to do with scheduling execution. All is decided with the getRequired calls.

Sorry for my typo:

The actual outputs are following:

  ModulePass Manager
    Module pass example
      Unnamed pass: implement Pass::getPassName()
    FunctionPass Manager
      Module Verifier
    Bitcode Writer
Pass Arguments: -functionscalls -domtree -dominancefrontier
  FunctionPass Manager
    Function Call Analysis
    Dominator Tree Construction
    Dominance Frontier Construction

Yes, I got your point that getAnalysis() is not scheduling the
execution. However, I do not want a function-pass to be executed more
than once for a single function. Do you have any suggestions to do
that?

Thanks.

Sorry for my typo:

The actual outputs are following:

ModulePass Manager
   Module pass example
     Unnamed pass: implement Pass::getPassName()
   FunctionPass Manager
     Module Verifier
   Bitcode Writer
Pass Arguments: -functionscalls -domtree -dominancefrontier
FunctionPass Manager
   Function Call Analysis
   Dominator Tree Construction
   Dominance Frontier Construction

The issue is not clear to me on this output?

Yes, I got your point that getAnalysis() is not scheduling the
execution. However, I do not want a function-pass to be executed more
than once for a single function. Do you have any suggestions to do
that?

It has to be marked as preserved by all the other subsequent passes.
There is no other way that I know of.