X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=docs%2FWritingAnLLVMPass.html;h=f531a74a687c0047ca6ff3fdf5fee3daa9c432cc;hb=9e6d1d1f5034347d237941f1bf08fba5c1583cd3;hp=c967000a9e08ea86a0681d27f5a3d80d229c21e7;hpb=04367bfc20c021c4105abf0c33b86d55f782d1e8;p=oota-llvm.git diff --git a/docs/WritingAnLLVMPass.html b/docs/WritingAnLLVMPass.html index c967000a9e0..f531a74a687 100644 --- a/docs/WritingAnLLVMPass.html +++ b/docs/WritingAnLLVMPass.html @@ -78,7 +78,8 @@
# Makefile for hello pass -# Path to top level of LLVM heirarchy +# Path to top level of LLVM hierarchy LEVEL = ../../.. # Name of the library to build @@ -222,12 +223,14 @@ Start out with:#include "llvm/Pass.h" #include "llvm/Function.h" +#include "llvm/Support/raw_ostream.h"Which are needed because we are writing a Pass, and +href="http://llvm.org/doxygen/classllvm_1_1Pass.html">Pass, we are operating on Function's.
+href="http://llvm.org/doxygen/classllvm_1_1Function.html">Function's, +and we will be doing some printing.Next we have:
@@ -264,7 +267,7 @@ time.static char ID; - Hello() : FunctionPass((intptr_t)&ID) {} + Hello() : FunctionPass(&ID) {}
This declares pass identifier used by LLVM to identify pass. This allows LLVM to @@ -272,7 +275,7 @@ avoid using expensive C++ runtime information.
@@ -481,11 +492,17 @@ invalidated, and are never "run". href="http://llvm.org/doxygen/classllvm_1_1ModulePass.html">ModulePass" class is the most general of all superclasses that you can use. Deriving from ModulePass indicates that your pass uses the entire program as a unit, -refering to function bodies in no predictable order, or adding and removing +referring to function bodies in no predictable order, or adding and removing functions. Because nothing is known about the behavior of ModulePass -subclasses, no optimization can be done for their execution. A module pass -can use function level passes (e.g. dominators) using getAnalysis interface - getAnalysis<DominatorTree>(Function). +subclasses, no optimization can be done for their execution. + +virtual bool runOnFunction(Function &F) { - llvm::cerr << "Hello: " << F.getName() << "\n"; + errs() << "Hello: " << F.getName() << "\n"; return false; } }; // end of struct Hello @@ -292,19 +295,26 @@ function. initialization value is not important.- RegisterPass<Hello> X("hello", "Hello World Pass"); + RegisterPass<Hello> X("hello", "Hello World Pass", + false /* Only looks at CFG */, + false /* Analysis Pass */); } // end of anonymous namespaceLastly, we register our class Hello, giving it a command line -argument "hello", and a name "Hello World Pass".
+argument "hello", and a name "Hello World Pass". +Last two RegisterPass arguments are optional. Their default value is false. +If a pass walks CFG without modifying it then third argument is set to true. +If a pass is an analysis pass, for example dominator tree pass, then true +is supplied as fourth argument.As a whole, the .cpp file looks like:
@@ -442,7 +453,7 @@ available, from the most general to the most specific.#include "llvm/Pass.h" #include "llvm/Function.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -312,14 +322,15 @@ argument "hello", and a name "Hello World Pass". struct Hello : public FunctionPass { static char ID; - Hello() : FunctionPass((intptr_t)&ID) {} + Hello() : FunctionPass(&ID) {} virtual bool runOnFunction(Function &F) { - llvm::cerr << "Hello: " << F.getName() << "\n"; + errs() << "Hello: " << F.getName() << "\n"; return false; } }; + char Hello::ID = 0; RegisterPass<Hello> X("hello", "Hello World Pass"); }When choosing a superclass for your Pass, you should choose the most specific class possible, while still being able to meet the requirements listed. This gives the LLVM Pass Infrastructure information necessary to -optimize how passes are run, so that the resultant compiler isn't unneccesarily +optimize how passes are run, so that the resultant compiler isn't unnecessarily slow.
A module pass can use function level passes (e.g. dominators) using +the getAnalysis interface +getAnalysis<DominatorTree>(llvm::Function *) to provide the +function to retrieve analysis result for, if the function pass does not require +any module or immutable passes. Note that this can only be done for functions for which the +analysis ran, e.g. in the case of dominators you should only ask for the +DominatorTree for function definitions, not declarations.
To write a correct ModulePass subclass, derive from ModulePass and overload the runOnModule method with the @@ -970,7 +987,7 @@ implement the virtual print method:
- virtual void print(llvm::OStream &O, const Module *M) const; + virtual void print(std::ostream &O, const Module *M) const;The print method must be implemented by "analyses" in order to print @@ -995,7 +1012,7 @@ depended on.
-One of the main responsibilities of the PassManager is the make sure +
One of the main responsibilities of the PassManager is to make sure that passes interact with each other correctly. Because PassManager tries to optimize the execution of passes it must know how the passes interact with each other and what dependencies exist between @@ -1123,7 +1140,8 @@ the fact that it hacks on the CFG.
- The getAnalysis<> and getAnalysisToUpdate<> methods + The getAnalysis<> and +getAnalysisIfAvailable<> methods@@ -1165,12 +1183,12 @@ before returning a reference to the desired pass.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:
+getAnalysisIfAvailable method, which returns a pointer to the analysis +if it is active. For example:... - if (DominatorSet *DS = getAnalysisToUpdate<DominatorSet>()) { + if (DominatorSet *DS = getAnalysisIfAvailable<DominatorSet>()) { // A DominatorSet is active. This code will update it. } ... @@ -1186,7 +1204,7 @@ it is active. For example:-Now that we understand the basics of how passes are defined, how the are +
Now that we understand the basics of how passes are defined, how they are used, and how they are required from other passes, it's time to get a little bit fancier. All of the pass relationships that we have seen so far are very simple: one pass depends on one other specific pass to be run before it can run. @@ -1375,7 +1393,8 @@ the LLVM program representation for a single function at a time, instead of traversing the entire program. It reduces the memory consumption of compiler, because, for example, only one DominatorSet -needs to be calculated at a time. This also makes it possible some interesting enhancements in the future.
@@ -1526,7 +1545,7 @@ need some way to free analysis results when they are no longer useful. TheIf you are writing an analysis or any other pass that retains a significant amount of state (for use by another pass which "requires" your pass and uses the getAnalysis method) you should implement -releaseMEmory to, well, release the memory allocated to maintain this +releaseMemory to, well, release the memory allocated to maintain this internal state. This method is called after the run* method for the class, before the next call of run* in your pass.
@@ -1654,7 +1673,7 @@ MachinePassRegistry RegisterMyPasses::Registry;cl::opt<RegisterMyPasses::FunctionPassCtor, false, - RegisterPassParser<RegisterMyPasses> > + RegisterPassParser<RegisterMyPasses> > MyPassOpt("mypass", cl::init(&createDefaultMyPass), cl::desc("my pass option help")); @@ -1804,9 +1823,9 @@ Despite that, we have kept the LLVM passes SMP ready, and you should too.
+ src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"> + src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"> Chris Lattner
The LLVM Compiler Infrastructure