X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FWritingAnLLVMPass.html;h=f531a74a687c0047ca6ff3fdf5fee3daa9c432cc;hb=9e6d1d1f5034347d237941f1bf08fba5c1583cd3;hp=07e736da691e55ae8a2aa1325e72dc0c9e2205f1;hpb=c758209153ca0f6da6737f25ada269c573fba456;p=oota-llvm.git diff --git a/docs/WritingAnLLVMPass.html b/docs/WritingAnLLVMPass.html index 07e736da691..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.
virtual bool runOnFunction(Function &F) { - llvm::cerr << "Hello: " << F.getName() << "\n"; + errs() << "Hello: " << F.getName() << "\n"; return false; } }; // end of struct Hello @@ -311,6 +314,7 @@ is supplied as fourth argument.@@ -488,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. + +#include "llvm/Pass.h" #include "llvm/Function.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -318,10 +322,10 @@ is supplied as fourth argument. 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; } }; @@ -449,7 +453,7 @@ available, from the most general to the most specific.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 @@ -977,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 @@ -1130,7 +1140,8 @@ the fact that it hacks on the CFG.
- The getAnalysis<> and getAnalysisToUpdate<> methods + The getAnalysis<> and +getAnalysisIfAvailable<> methods@@ -1172,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. } ... @@ -1534,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.
@@ -1662,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")); @@ -1812,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