How to use data structure of another pass

In LLVM, we generally use data structure of another pass by implementing getAnalysisUsage (AnalysisUsage &AU) method. Suppose SOURCE/lib/Transform directory I have added one pass named as pass_1, which calculates all expressions in a program and stores all of them in an Expr array(take it now for granted). Now I added another pass named as pass_2 in the same directory to print all these expression. How do I do this?

Soumya_Prasad_Ukil wrote:

     In LLVM, we generally use data structure of another pass by implementing getAnalysisUsage (AnalysisUsage &AU) method. Suppose SOURCE/lib/Transform directory I have added one pass named as pass_1, which calculates all expressions in a program and stores all of them in an Expr array(take it now for granted). Now I added another pass named as pass_2 in the same directory to print all these expression. How do I do this?

The pass_1 pass will need to implement a method that returns the array of expressions (or a reference to it). The pass_2 pass can then use getAnalysis<pass_1>() to get a reference to the pass_1 object and call this method on the pass_1 object to get the array of expressions.

-- John T.

Can you tell me where pass_1 is defined? You have used it inside getAnalysis<pass_1>(). Is it the name we generally give to a pass while registry? In this case, don’t I need to use getAnalysisUsage (AnalysisUsage &AU) function?

Soumya_Prasad_Ukil wrote:

Can you tell me where pass_1 is defined?

The class definition for pass_1 should be defined in a header file so that it can be #include'ed into code that needs to know how to use pass_1.

You have used it inside getAnalysis<pass_1>(). Is it the name we generally give to a pass while registry? In this case, don't I need to use getAnalysisUsage (AnalysisUsage &AU) function?

pass_1 is the name of the C++ class that implements the first pass.

To use the analysis results of pass_1, you need to do two things:

1) The pass using pass_1 (pass_2, in this case) needs to indicate that it requires pass_1. To do this, it calls AU.addRequired<pass_1>() in its getAnalysisUsage() method.

2) It needs to get a C++ reference (or pointer) to the pass_1 pass when its runOnModule (or runOnFunction, runOnBasicBlock, etc.) method is called. To do this, it must call getAnalysis<pass_1>().

-- John T.