From 5fa8fff8d21d98896957f3a891968c3ffc66c096 Mon Sep 17 00:00:00 2001
From: Jim Laskey
+# Makefile for hello pass # Path to top level of LLVM heirarchy @@ -181,7 +187,7 @@ LOADABLE_MODULE = 1 # Include the makefile implementation stuff include $(LEVEL)/Makefile.common -+This makefile specifies that all of the .cpp files in the current directory are to be compiled and linked together into a @@ -205,10 +211,10 @@ the pass itself.
Now that we have a way to compile our new pass, we just have to write it. Start out with:
-+#include "llvm/Pass.h" #include "llvm/Function.h" -+Which are needed because we are writing a Pass, and @@ -216,18 +222,18 @@ we are operating on Function's.
Next we have:
-+using namespace llvm; -+... which is required because the functions from the include files live in the llvm namespace.
Next we have:
-+namespace { -+... which starts out an anonymous namespace. Anonymous namespaces are to C++ what the "static" keyword is to C (at global scope). It makes the @@ -237,9 +243,9 @@ information.
Next, we declare our pass itself:
-+struct Hello : public FunctionPass { -+
This declares a "Hello" class that is a subclass of FunctionPass. @@ -248,13 +254,13 @@ href="#passtype">later, but for now, know that FunctionPass's operate a function at a time.
-+virtual bool runOnFunction(Function &F) { std::cerr << "Hello: " << F.getName() << "\n"; return false; } }; // end of struct Hello -+We declare a "runOnFunction" method, which overloads an abstract virtual method inherited from FunctionPass. This is where we are supposed to do our thing, so we just print out our message with the name of each function.
-+RegisterOpt<Hello> X("hello", "Hello World Pass"); } // end of anonymous namespace -+Lastly, we register our class Hello, giving it a command line argument "hello", and a name "Hello World Pass". There are @@ -275,7 +281,7 @@ depending on what it is to be used for. For "optimizations" we use the
As a whole, the .cpp file looks like:
-+#include "llvm/Pass.h" #include "llvm/Function.h" @@ -291,7 +297,7 @@ depending on what it is to be used for. For "optimizations" we use the RegisterOpt<Hello> X("hello", "Hello World Pass"); } -+Now that it's all together, compile the file with a simple "gmake" command in the local directory and you should get a new @@ -320,12 +326,12 @@ LLVM. We can now run the bytecode file (hello.bc) for the program through our transformation like this (or course, any bytecode file will work):
-+$ opt -load ../../../Debug/lib/Hello.so -hello < hello.bc > /dev/null Hello: __main Hello: puts Hello: main -+The '-load' option specifies that 'opt' should load your pass as a shared object, which makes '-hello' a valid command line @@ -337,7 +343,7 @@ interesting way, we just throw away the result of opt (sending it to
To see what happened to the other string you registered, try running opt with the --help option:
-+$ opt -load ../../../Debug/lib/Hello.so --help OVERVIEW: llvm .bc -> .bc modular optimizer @@ -354,7 +360,7 @@ OPTIONS: -inline - Function Integration/Inlining -instcombine - Combine redundant instructions ... -+The pass name get added as the information string for your pass, giving some documentation to users of opt. Now that you have a working pass, you @@ -365,7 +371,7 @@ line option (--time-passes) that allows you to get information about the execution time of your pass along with the other passes you queue up. For example:
-+$ opt -load ../../../Debug/lib/Hello.so -hello -time-passes < hello.bc > /dev/null Hello: __main Hello: puts @@ -381,7 +387,7 @@ Hello: main 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL -+As you can see, our implementation above is pretty fast :). The additional passes listed are automatically inserted by the 'opt' tool to verify @@ -467,9 +473,9 @@ following signature:
-+virtual bool runOnModule(Module &M) = 0; -+The runOnModule method performs the interesting work of the pass. It should return true if the module was modified by the transformation and @@ -535,9 +541,9 @@ false if they didn't.
-+virtual bool doInitialization(Module &M); -+The doIninitialize method is allowed to do most of the things that CallGraphSCCPass's are not allowed to do. They can add and remove @@ -556,9 +562,9 @@ fast).
-+virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCCM) = 0; -+The runOnSCC method performs the interesting work of the pass, and should return true if the module was modified by the transformation, false @@ -574,9 +580,9 @@ otherwise.
-+virtual bool doFinalization(Module &M); -+The doFinalization method is an infrequently used method that is called when the pass framework has finished calling
-+virtual bool doInitialization(Module &M); -+The doIninitialize method is allowed to do most of the things that FunctionPass's are not allowed to do. They can add and remove @@ -653,9 +659,9 @@ free functions that it needs, adding prototypes to the module if necessary.
-+virtual bool runOnFunction(Function &F) = 0; -+
The runOnFunction method must be implemented by your subclass to do the transformation or analysis work of your pass. As usual, a true value should @@ -671,9 +677,9 @@ be returned if the function is modified.
-+virtual bool doFinalization(Module &M); -+The doFinalization method is an infrequently used method that is called when the pass framework has finished calling FunctionPass's have, but also have the followi
-+virtual bool doInitialization(Function &F); -+The doIninitialize method is allowed to do most of the things that BasicBlockPass's are not allowed to do, but that @@ -740,9 +746,9 @@ fast).
-+virtual bool runOnBasicBlock(BasicBlock &BB) = 0; -+Override this function to do the work of the BasicBlockPass. This function is not allowed to inspect or modify basic blocks other than the @@ -759,9 +765,9 @@ if the basic block is modified.
-+virtual bool doFinalization(Function &F); -+The doFinalization method is an infrequently used method that is called when the pass framework has finished calling
-+virtual bool runOnMachineFunction(MachineFunction &MF) = 0; -+runOnMachineFunction can be considered the main entry point of a MachineFunctionPass; that is, you should override this method to do the @@ -877,9 +883,9 @@ should implement the virtual print method:
-+virtual void print(std::ostream &O, const Module *M) const; -+The print method must be implemented by "analyses" in order to print a human readable version of the analysis results. This is useful for debugging @@ -927,9 +933,9 @@ having any prerequisite passes, and invalidating all other passes.
-@@ -1163,14 +1169,14 @@ an analysis should be registered, with a human readable name provided for it. Unlike registration of passes, there is no command line argument to be specified for the Analysis Group Interface itself, because it is "abstract": -+virtual void getAnalysisUsage(AnalysisUsage &Info) const; -+By implementing the getAnalysisUsage method, the required and invalidated sets may be specified for your transformation. The implementation @@ -1008,24 +1014,24 @@ the fact that it hacks on the CFG.
-@@ -1042,12 +1048,12 @@ 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: -+// This is an example implementation from an analysis, which does not modify // the program at all, yet has a prerequisite. void PostDominanceFrontier::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); AU.addRequired<PostDominatorTree>(); } -+and:
-++bool LICM::runOnFunction(Function &F) { LoopInfo &LI = getAnalysis<LoopInfo>(); ... } -+This method call returns a reference to the pass desired. You may get a runtime assertion failure if you attempt to get an analysis that you did not @@ -1062,13 +1068,13 @@ If your pass is capable of updating analyses if they exist (e.g., 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. } ... -++static RegisterAnalysisGroup<AliasAnalysis> A("Alias Analysis"); -+Once the analysis is registered, passes can declare that they are valid implementations of the interface by using the following code:
-+namespace { // Analysis Group implementations must be registered normally... RegisterOpt<FancyAA> @@ -1179,7 +1185,7 @@ implementations of the interface by using the following code: // Declare that we implement the AliasAnalysis interface RegisterAnalysisGroup<AliasAnalysis, FancyAA> C; } -+This just shows a class FancyAA that is registered normally, then uses the RegisterAnalysisGroup template to "join" the -
-+namespace { // Analysis Group implementations must be registered normally... RegisterOpt<BasicAliasAnalysis> @@ -1197,7 +1203,7 @@ no problem. // Declare that we implement the AliasAnalysis interface RegisterAnalysisGroup<AliasAnalysis, BasicAliasAnalysis, true> E; } -+Here we show how the default implementation is specified (using the extra argument to the RegisterAnalysisGroup template). There must be exactly @@ -1290,7 +1296,7 @@ option, just type 'opt --help-hidden').
how our Hello World pass interacts with other passes. Lets try it out with the gcse and licm passes:+$ opt -load ../../../Debug/lib/Hello.so -gcse -licm --debug-pass=Structure < hello.bc > /dev/null Module Pass Manager Function Pass Manager @@ -1308,7 +1314,7 @@ Module Pass Manager -- Module Verifier Bytecode Writer --Bytecode Writer -+This output shows us when passes are constructed and when the analysis results are known to be dead (prefixed with '--'). Here we see that @@ -1327,7 +1333,7 @@ passes.
Lets see how this changes when we run the Hello World pass in between the two passes:
-+$ opt -load ../../../Debug/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null Module Pass Manager Function Pass Manager @@ -1352,23 +1358,23 @@ Module Pass Manager Hello: __main Hello: puts Hello: main -+Here we see that the Hello World pass has killed the Dominator Set pass, even though it doesn't modify the code at all! To fix this, we need to add the following getAnalysisUsage method to our pass:
-+// We don't modify the program, so we preserve all analyses virtual void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); } -+Now when we run our pass, we get this output:
-+$ opt -load ../../../Debug/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null Pass Arguments: -gcse -hello -licm Module Pass Manager @@ -1392,7 +1398,7 @@ Module Pass Manager Hello: __main Hello: puts Hello: main -+Which shows that we don't accidentally invalidate dominator information anymore, and therefore do not have to compute it twice.
@@ -1406,9 +1412,9 @@ anymore, and therefore do not have to compute it twice.-+ + + + ++virtual void releaseMemory(); -+The PassManager automatically determines when to compute analysis results, and how long to keep them around for. Because the lifetime of the pass @@ -1425,6 +1431,139 @@ class, before the next call of run* in your pass.
+ ++ + + + +Size matters when constructing production quality tools using llvm, +both for the purposes of distribution, and for regulating the resident code size +when running on the target system. Therefore, it becomes desirable to +selectively use some passes, while omitting others and maintain the flexibility +to change configurations later on. You want to be able to do all this, and, +provide feedback to the user. This is where pass registration comes into +play.
+ +The fundamental mechanisms for pass registration are the +MachinePassRegistry class and subclasses of +MachinePassRegistryNode.
+ +An instance of MachinePassRegistry is used to maintain a list of +MachinePassRegistryNode objects. This instance maintains the list and +communicates additions and deletions to the command line interface.
+ +An instance of MachinePassRegistryNode subclass is used to maintain +information provided about a particular pass. This information includes the +command line name, the command help string and the address of the function used +to create an instance of the pass. A global static constructor of one of these +instances registers with a corresponding MachinePassRegistry, +the static destructor unregisters. Thus a pass that is statically linked +in the tool will be registered at start up. A dynamically loaded pass will +register on load and unregister at unload.
+ ++ ++ + + + + +There are predefined registries to track instruction scheduling +(RegisterScheduler) and register allocation (RegisterRegAlloc) +machine passes. Here we will describe how to register a register +allocator machine pass.
+ +Implement your register allocator machine pass. In your register allocator +.cpp file add the following include;
+ ++ ++ #include ""llvm/CodeGen/RegAllocRegistry.h"" +Also in your register allocator .cpp file, define a creator function in the +form;
+ ++ ++ FunctionPass *createMyRegisterAllocator() { + return new MyRegisterAllocator(); + } +Note that the signature of this function should match the type of +RegisterRegAlloc::FunctionPassCtor. In the same file add the +"installing" declaration, in the form;
+ ++ ++ static RegisterRegAlloc myRegAlloc("myregalloc", + " my register allocator help string", + createMyRegisterAllocator); +Note the two spaces prior to the help string produces a tidy result on the +--help query.
+ ++ ++$ llc --help + ... + -regalloc - Register allocator to use: (default = linearscan) + =linearscan - linear scan register allocator + =local - local register allocator + =simple - simple register allocator + =myregalloc - my register allocator help string + ... +And that's it. The user is now free to use -regalloc=myregalloc as +an option. Registering instruction schedulers is similar except use the +RegisterRegAlloc class. Note that the +RegisterRegAlloc::FunctionPassCtor is significantly different from +RegisterRegAlloc::FunctionPassCtor.
+ +To force the load/linking of your register allocator into the llc/lli tools, +add your creator function's global declaration to "Passes.h" and add a "pseudo" +call line to llvm/Codegen/LinkAllCodegenComponents.h.
+ ++ ++The easiest way to get started is to clone one of the existing registries; we +recommend llvm/CodeGen/RegAllocRegistry.h. The key things to modify +are the class name and the FunctionPassCtor type.
+ +Then you need to declare the registry. Example: if your pass registry is +RegisterMyPasses then define;
+ ++ ++MachinePassRegistry RegisterMyPasses::Registry; +And finally, declare the command line option for your passes. Example:
+ ++ ++ cl::opt<RegisterMyPasses::FunctionPassCtor, false, + RegisterPassParser<RegisterMyPasses> > + MyPassOpt("mypass", + cl::init(&createDefaultMyPass), + cl::desc("my pass option help")); +Here the command option is "mypass", with createDefaultMyPass as the default +creator.
+ +Using GDB with dynamically loaded passes @@ -1454,7 +1593,7 @@ on that.First thing you do is start gdb on the opt process:
-+$ gdb opt GNU gdb 5.0 Copyright 2000 Free Software Foundation, Inc. @@ -1464,7 +1603,7 @@ Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "sparc-sun-solaris2.6"... (gdb) -+Note that opt has a lot of debugging information in it, so it takes time to load. Be patient. Since we cannot set a breakpoint in our pass yet @@ -1474,7 +1613,7 @@ object. The most foolproof way of doing this is to set a breakpoint in PassManager::run and then run the process with the arguments you want:
-+(gdb) break PassManager::run Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70. (gdb) run test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[passoption] @@ -1482,7 +1621,7 @@ Starting program: opt test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[pas Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70 70 bool PassManager::run(Module &M) { return PM->run(M); } (gdb) -+Once the opt stops in the PassManager::run method you are now free to set breakpoints in your pass so that you can trace through execution