X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FWritingAnLLVMPass.html;h=66b98b681ab4f1bfc752b68247203046e756fdbb;hb=32dc4d9cd76795f43478e80eec9b1e073adf0f98;hp=3d896cf3a119cac2e82824234234e36f74ba1913;hpb=18aad23ce36f9c1dc14c818a10d69448137ebe07;p=oota-llvm.git diff --git a/docs/WritingAnLLVMPass.html b/docs/WritingAnLLVMPass.html index 3d896cf3a11..66b98b681ab 100644 --- a/docs/WritingAnLLVMPass.html +++ b/docs/WritingAnLLVMPass.html @@ -8,9 +8,9 @@ -
+

Writing an LLVM Pass -

+
  1. Introduction - What is a pass?
  2. @@ -51,6 +51,14 @@
  3. The doFinalization() method
  4. +
  5. The RegionPass class +
  6. The BasicBlockPass class
  7. Implementing Analysis Groups
@@ -1390,14 +1530,14 @@ allowing any analysis results to live across the execution of your pass.

options that is useful for debugging pass execution, seeing how things work, and diagnosing when you should be preserving more analyses than you currently are (To get information about all of the variants of the --debug-pass -option, just type 'opt --help-hidden').

+option, just type 'opt -help-hidden').

By using the --debug-pass=Structure option, for example, we can see 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
+$ opt -load ../../../Debug+Asserts/lib/Hello.so -gcse -licm --debug-pass=Structure < hello.bc > /dev/null
 Module Pass Manager
   Function Pass Manager
     Dominator Set Construction
@@ -1412,8 +1552,8 @@ Module Pass Manager
     Module Verifier
 --  Dominator Set Construction
 --  Module Verifier
-  Bytecode Writer
---Bytecode Writer
+  Bitcode Writer
+--Bitcode Writer
 

This output shows us when passes are constructed and when the analysis @@ -1434,7 +1574,7 @@ passes.

World pass in between the two passes:

-$ opt -load ../../../Debug/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
+$ opt -load ../../../Debug+Asserts/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
 Module Pass Manager
   Function Pass Manager
     Dominator Set Construction
@@ -1453,8 +1593,8 @@ Module Pass Manager
     Module Verifier
 --  Dominator Set Construction
 --  Module Verifier
-  Bytecode Writer
---Bytecode Writer
+  Bitcode Writer
+--Bitcode Writer
 Hello: __main
 Hello: puts
 Hello: main
@@ -1475,7 +1615,7 @@ href="#getAnalysisUsage">getAnalysisUsage method to our pass:

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
+$ opt -load ../../../Debug+Asserts/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
 Pass Arguments:  -gcse -hello -licm
 Module Pass Manager
   Function Pass Manager
@@ -1493,8 +1633,8 @@ Module Pass Manager
     Module Verifier
 --  Dominator Set Construction
 --  Module Verifier
-  Bytecode Writer
---Bytecode Writer
+  Bitcode Writer
+--Bitcode Writer
 Hello: __main
 Hello: puts
 Hello: main
@@ -1503,14 +1643,12 @@ Hello: main
 

Which shows that we don't accidentally invalidate dominator information anymore, and therefore do not have to compute it twice.

-
- - + -
+
   virtual void releaseMemory();
@@ -1525,19 +1663,21 @@ need some way to free analysis results when they are no longer useful.  The
 

If 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.

+
+ - + -
+

Size matters when constructing production quality tools using llvm, both for the purposes of distribution, and for regulating the resident code size @@ -1564,14 +1704,12 @@ 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) @@ -1605,12 +1743,12 @@ form;

Note the two spaces prior to the help string produces a tidy result on the ---help query.

+-help query.

-$ llc --help
+$ llc -help
   ...
-  -regalloc                    - Register allocator to use: (default = linearscan)
+  -regalloc                    - Register allocator to use (default=linearscan)
     =linearscan                -   linear scan register allocator
     =local                     -   local register allocator
     =simple                    -   simple register allocator
@@ -1632,11 +1770,11 @@ 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 @@ -1653,7 +1791,7 @@ MachinePassRegistry RegisterMyPasses::Registry;

   cl::opt<RegisterMyPasses::FunctionPassCtor, false,
-          RegisterPassParser<RegisterMyPasses> >
+          RegisterPassParser<RegisterMyPasses> >
   MyPassOpt("mypass",
             cl::init(&createDefaultMyPass),
             cl::desc("my pass option help")); 
@@ -1664,13 +1802,15 @@ creator.

+
+ - + -
+

Unfortunately, using GDB with dynamically loaded passes is not as easy as it should be. First of all, you can't set a breakpoint in a shared object that has @@ -1682,14 +1822,12 @@ GDB.

transformation invoked by opt, although nothing described here depends on that.

-
- - + -
+

First thing you do is start gdb on the opt process:

@@ -1716,8 +1854,8 @@ want:

 (gdb) break llvm::PassManager::run
 Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
-(gdb) run test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[passoption]
-Starting program: opt test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[passoption]
+(gdb) run test.bc -load $(LLVMTOP)/llvm/Debug+Asserts/lib/[libname].so -[passoption]
+Starting program: opt test.bc -load $(LLVMTOP)/llvm/Debug+Asserts/lib/[libname].so -[passoption]
 Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
 70      bool PassManager::run(Module &M) { return PM->run(M); }
 (gdb)
@@ -1730,11 +1868,11 @@ or do other standard debugging stuff.

- + -
+

Once you have the basics down, there are a couple of problems that GDB has, some with solutions, some without.

@@ -1762,26 +1900,26 @@ href="mailto:sabre@nondot.org">Chris.

+
+ - + -
+

Although the LLVM Pass Infrastructure is very capable as it stands, and does some nifty stuff, there are things we'd like to add in the future. Here is where we are going:

-
- - + -
+

Multiple CPU machines are becoming more common and compilation can never be fast enough: obviously we should allow for a multithreaded compiler. Because of @@ -1799,16 +1937,18 @@ Despite that, we have kept the LLVM passes SMP ready, and you should too.

+
+
Valid CSS! + src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"> Valid HTML 4.01! + src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"> Chris Lattner
- The LLVM Compiler Infrastructure
+ The LLVM Compiler Infrastructure
Last modified: $Date$