From: Chris Lattner This makefile specifies that all of the .cpp files in the current
directory are to be compiled and linked together into a
lib/Debug/libhello.so shared object that can be dynamically loaded by
-the opt or analyze tools.
-Note that the suffix of the shared library may differ from the example above if
-your system uses a different suffix by default.
-
@@ -171,12 +174,9 @@ include $(LEVEL)/Makefile.common
Now that we have the build scripts set up, we just need to write the code for the pass itself.
@@ -487,7 +487,7 @@ should return true if they modified the program, or false if they didn't. -A MachineFunctionPass executes on the machine-dependent -representation of each LLVM function in the program, -independent of all of the other functions in the program. -A MachineFunctionPass is also a FunctionPass, so all +
A MachineFunctionPass is a part of the LLVM code generator that +executes on the machine-dependent representation of each LLVM function in the +program. A MachineFunctionPass is also a FunctionPass, so all the restrictions that apply to a FunctionPass also apply to it. -MachineFunctionPasses also have additional restrictions. In -particular, MachineFunctionPasses are not allowed to do any of -the following:
+MachineFunctionPasses also have additional restrictions. In particular, +MachineFunctionPasses are not allowed to do any of the following:If you pass is constructed by its default constructor, you only ever have to -pass these two arguments. If, on the other hand, you require other information -(like target specific information), you must pass an additional argument. This -argument is a pointer to a function used to create the pass. For an example of -how this works, look at the LowerAllocations.cpp -file.
-If a pass is registered to be used by the analyze utility, you should implement the virtual print method:
@@ -819,34 +805,77 @@ invalidated sets may be specified for your transformation. The implementation should fill in the AnalysisUsage object with information about which passes are required and not invalidated. To -do this, the following set methods are provided by the AnalysisUsage -class: +do this, a pass may call any of the following methods on the AnalysisUsage +object: +- // addRequires - Add the specified pass to the required set for your pass. - template<class PassClass> - AnalysisUsage &AnalysisUsage::addRequired(); - - // addPreserved - Add the specified pass to the set of analyses preserved by - // this pass - template<class PassClass> - AnalysisUsage &AnalysisUsage::addPreserved(); - - // setPreservesAll - Call this if the pass does not modify its input at all - void AnalysisUsage::setPreservesAll(); - - // setPreservesCFG - This function should be called by the pass, iff they do not: - // - // 1. Add or remove basic blocks from the function - // 2. Modify terminator instructions in any way. - // - // This is automatically implied for BasicBlockPass's - // - void AnalysisUsage::setPreservesCFG(); -+ + + +
+If you pass requires a previous pass to be executed (an analysis for example), +it can use one of these methods to arrange for it to be run before your pass. +LLVM has many different types of analyses and passes that can be required, +spaning the range from DominatorSet to BreakCriticalEdges. +requiring BreakCriticalEdges, for example, guarantees that there will +be no critical edges in the CFG when your pass has been run. +
+ ++Some analyses chain to other analyses to do their job. For example, an AliasAnalysis implementation is required to chain to other alias analysis passes. In +cases where analyses chain, the addRequiredTransitive method should be +used instead of the addRequired method. This informs the PassManager +that the transitively required pass should be alive as long as the requiring +pass is. +
++One of the jobs of the PassManager is to optimize how and when analyses are run. +In particular, it attempts to avoid recomputing data unless it needs to. For +this reason, passes are allowed to declare that they preserve (i.e., they don't +invalidate) an existing analysis if it's available. For example, a simple +constant folding pass would not modify the CFG, so it can't possible effect the +results of dominator analysis. By default, all passes are assumed to invalidate +all others. +
+ ++The AnalysisUsage class provides several methods which are useful in +certain circumstances that are related to addPreserved. In particular, +the setPreservesAll method can be called to indicate that the pass does +not modify the LLVM program at all (which is true for analyses), and the +setPreservesCFG method can be used by transformations that change +instructions in the program but do not modify the CFG or terminator instructions +(note that this property is implicitly set for BasicBlockPass's). +
+ ++addPreserved is particularly useful for transformations like +BreakCriticalEdges. This pass knows how to update a small set of loop +and dominator related analyses if they exist, so it can preserve them, despite +the fact that it hacks on the CFG. +
+Some examples of how to use these methods are:
+// This is an example implementation from an analysis, which does not modify @@ -871,20 +900,22 @@ class:-@@ -924,7 +969,7 @@ Analysis Groups.The Pass::getAnalysis<> method is inherited by your class, -providing you with access to the passes that you declared that you required with -the getAnalysisUsage method. It takes -a single template argument that specifies which pass class you want, and returns -a reference to that pass.
+The Pass::getAnalysis<> method is automatically inherited by +your class, providing you with access to the passes that you declared that you +required with the getAnalysisUsage +method. It takes a single template argument that specifies which pass class you +want, and returns a reference to that pass. For example:
- template<typename PassClass> - AnalysisType &getAnalysis(); + bool LICM::runOnFunction(Function &F) { + LoopInfo &LI = getAnalysis<LoopInfo>(); + ... + }This method call returns a reference to the pass desired. You may get a @@ -894,6 +925,20 @@ href="#getAnalysisUsage">getAnalysisUsage implementation. This method can be called by your run* method implementation, or by any other local method invoked by your run* method.
++If your pass is capable of updating analyses if they exist (e.g., +BreakCriticalEdges, as described above), you can use the +getAnalysisToUpdate method, which returns a pointer to the analysis if +it is active. For example:
+ ++ ... + if (DominatorSet *DS = getAnalysisToUpdate<DominatorSet>()) { + // A DominatorSet is active. This code will update it. + } + ... ++