1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><title>Writing an LLVM Pass</title></head>
6 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
7 <tr><td> <font size=+3 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>Writing an LLVM Pass</b></font></td>
12 <li><a href="#introduction">Introduction - What is a pass?</a>
13 <li><a href="#quickstart">Quick Start - Writing hello world</a>
15 <li><a href="#makefile">Setting up the build environment</a>
16 <li><a href="#basiccode">Basic code required</a>
17 <li><a href="#running">Running a pass with <tt>opt</tt>
18 or <tt>analyze</tt></a>
20 <li><a href="#passtype">Pass classes and requirements</a>
22 <li><a href="#ImmutablePass">The <tt>ImmutablePass</tt> class</a>
23 <li><a href="#Pass">The <tt>Pass</tt> class</a>
25 <li><a href="#run">The <tt>run</tt> method</a>
27 <li><a href="#FunctionPass">The <tt>FunctionPass</tt> class</a>
29 <li><a href="#doInitialization_mod">The <tt>doInitialization(Module
30 &)</tt> method</a>
31 <li><a href="#runOnFunction">The <tt>runOnFunction</tt> method</a>
32 <li><a href="#doFinalization_mod">The <tt>doFinalization(Module
33 &)</tt> method</a>
35 <li><a href="#BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
37 <li><a href="#doInitialization_fn">The <tt>doInitialization(Function
38 &)</tt> method</a>
39 <li><a href="#runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
40 <li><a href="#doFinalization_fn">The <tt>doFinalization(Function
41 &)</tt> method</a>
43 <li><a href="#MachineFunctionPass">The <tt>MachineFunctionPass</tt>
46 <li><a href="#registration">Pass Registration</a>
48 <li><a href="#print">The <tt>print</tt> method</a>
50 <li><a href="#interaction">Specifying interactions between passes</a>
52 <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a>
53 <li><a href="#getAnalysis">The <tt>getAnalysis</tt> method</a>
55 <li><a href="#analysisgroup">Implementing Analysis Groups</a>
57 <li><a href="#agconcepts">Analysis Group Concepts</a>
58 <li><a href="#registerag">Using <tt>RegisterAnalysisGroup</tt></a>
60 <li><a href="#passmanager">What PassManager does</a>
62 <li><a href="#releaseMemory">The <tt>releaseMemory</tt> method</a>
64 <li><a href="#debughints">Using GDB with dynamically loaded passes</a>
66 <li><a href="#breakpoint">Setting a breakpoint in your pass
67 <li><a href="#debugmisc">Miscellaneous Problems
69 <li><a href="#future">Future extensions planned</a>
71 <li><a href="#SMP">Multithreaded LLVM</a>
72 <li><a href="#ModuleSource">A new <tt>ModuleSource</tt> interface</a>
73 <li><a href="#PassFunctionPass"><tt>Pass</tt>'s requiring <tt>FunctionPass</tt>'s</a>
76 <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p>
81 <!-- *********************************************************************** -->
82 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
83 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
84 <a name="introduction">Introduction - What is a pass?
85 </b></font></td></tr></table><ul>
86 <!-- *********************************************************************** -->
88 The LLVM Pass Framework is an important part of the LLVM system, because LLVM
89 passes are where the interesting parts of the compiler exist. Passes perform
90 the transformations and optimizations that make up the compiler, they build
91 the analysis results that are used by these transformations, and they are, above
92 all, a structuring technique for compiler code.<p>
94 All LLVM passes are subclasses of the <tt><a
95 href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt> class, which
96 implement functionality by overriding virtual methods inherited from
97 <tt>Pass</tt>. Depending on how your pass works, you may be able to inherit
99 href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt>
101 href="http://llvm.cs.uiuc.edu/doxygen/structBasicBlockPass.html">BasicBlockPass</a></tt>,
102 which gives the system more information about what your pass does, and how it
103 can be combined with other passes. One of the main features of the LLVM Pass
104 Framework is that it schedules passes to run in an efficient way based on the
105 constraints that your pass has.<p>
107 We start by showing you how to construct a pass, everything from setting up the
108 code, to compiling, loading, and executing it. After the basics are down, more
109 advanced features are discussed.<p>
112 <!-- *********************************************************************** -->
113 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
114 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
115 <a name="quickstart">Quick Start - Writing hello world
116 </b></font></td></tr></table><ul>
117 <!-- *********************************************************************** -->
119 Here we describe how to write the "hello world" of passes. The "Hello" pass is
120 designed to simply print out the name of non-external functions that exist in
121 the program being compiled. It does not modify the program at all, just
122 inspects it. The source code and files for this pass are available in the LLVM
123 source tree in the <tt>lib/Transforms/Hello</tt> directory.<p>
126 <!-- ======================================================================= -->
127 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
128 <tr><td> </td><td width="100%">
129 <font color="#EEEEFF" face="Georgia,Palatino"><b>
130 <a name="makefile">Setting up the build environment
131 </b></font></td></tr></table><ul>
133 First thing you need to do is create a new directory somewhere in the LLVM
134 source base. For this example, we'll assume that you made
135 "<tt>lib/Transforms/Hello</tt>". The first thing you must do is set up a build
136 script (Makefile) that will compile the source code for the new pass. To do
137 this, copy this into "<tt>Makefile</tt>":<p>
140 # Makefile for hello pass
142 # Path to top level of LLVM heirarchy
145 # Name of the library to build
148 # Build a dynamically loadable shared object
151 # Include the makefile implementation stuff
152 include $(LEVEL)/Makefile.common
153 </pre></ul><hr><ul><p>
155 This makefile specifies that all of the <tt>.cpp</tt> files in the current
156 directory are to be compiled and linked together into a
157 <tt>lib/Debug/libhello.so</tt> shared object that can be dynamically loaded by
158 the <tt>opt</tt> or <tt>analyze</tt> tools.<p>
160 Now that we have the build scripts set up, we just need to write the code for
164 <!-- ======================================================================= -->
165 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
166 <tr><td> </td><td width="100%">
167 <font color="#EEEEFF" face="Georgia,Palatino"><b>
168 <a name="basiccode">Basic code required
169 </b></font></td></tr></table><ul>
171 Now that we have a way to compile our new pass, we just have to write it. Start
175 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
176 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
179 Which are needed because we are writing a <tt><a
180 href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>, and we are
182 href="http://llvm.cs.uiuc.edu/doxygen/classFunction.html">Function</a></tt>'s.<p>
190 ... which starts out an anonymous namespace. Anonymous namespaces are to C++
191 what the "<tt>static</tt>" keyword is to C (at global scope). It makes the
192 things declared inside of the anonymous namespace only visible to the current
193 file. If you're not familiar with them, consult a decent C++ book for more
196 Next, we declare our pass itself:<p>
199 <b>struct</b> Hello : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
202 This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a
203 href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt>.
204 The different builtin pass subclasses are described in detail <a
205 href="#passtype">later</a>, but for now, know that <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s
206 operate a function at a time.<p>
209 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &F) {
210 std::cerr << "<i>Hello: </i>" << F.getName() << "\n";
213 }; <i>// end of struct Hello</i>
216 We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method, which
217 overloads an abstract virtual method inherited from <a
218 href="#FunctionPass"><tt>FunctionPass</tt></a>. This is where we are supposed
219 to do our thing, so we just print out our message with the name of each
223 RegisterOpt<Hello> X("<i>hello</i>", "<i>Hello World Pass</i>");
224 } <i>// end of anonymous namespace</i>
227 Lastly, we register our class <tt>Hello</tt>, giving it a command line argument
228 "<tt>hello</tt>", and a name "<tt>Hello World Pass</tt>". There are several
229 different ways of <a href="#registration">registering your pass</a>, depending
230 on what it is to be used for. For "optimizations" we use the
231 <tt>RegisterOpt</tt> template.<p>
233 As a whole, the <tt>.cpp</tt> file looks like:<p>
236 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
237 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
240 <b>struct Hello</b> : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
241 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &F) {
242 std::cerr << "<i>Hello: </i>" << F.getName() << "\n";
247 RegisterOpt<Hello> X("<i>hello</i>", "<i>Hello World Pass</i>");
251 Now that it's all together, compile the file with a simple "<tt>gmake</tt>"
252 command in the local directory and you should get a new
253 "<tt>lib/Debug/libhello.so</tt> file. Note that everything in this file is
254 contained in an anonymous namespace: this reflects the fact that passes are self
255 contained units that do not need external interfaces (although they can have
256 them) to be useful.<p>
259 <!-- ======================================================================= -->
260 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
261 <tr><td> </td><td width="100%">
262 <font color="#EEEEFF" face="Georgia,Palatino"><b>
263 <a name="running">Running a pass with <tt>opt</tt> or <tt>analyze</tt>
264 </b></font></td></tr></table><ul>
266 Now that you have a brand new shiny <tt>.so</tt> file, we can use the
267 <tt>opt</tt> command to run an LLVM program through your pass. Because you
268 registered your pass with the <tt>RegisterOpt</tt> template, you will be able to
269 use the <tt>opt</tt> tool to access it, once loaded.<p>
271 To test it, follow the example at the end of the <a
272 href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to
273 LLVM. We can now run the bytecode file (<tt>hello.bc</tt>) for the program
274 through our transformation like this (or course, any bytecode file will
278 $ opt -load ../../../lib/Debug/libhello.so -hello < hello.bc > /dev/null
284 The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your pass
285 as a shared object, which makes '<tt>-hello</tt>' a valid command line argument
286 (which is one reason you need to <a href="#registration">register your
287 pass</a>). Because the hello pass does not modify the program in any
288 interesting way, we just throw away the result of <tt>opt</tt> (sending it to
289 <tt>/dev/null</tt>).<p>
291 To see what happened to the other string you registered, try running
292 <tt>opt</tt> with the <tt>--help</tt> option:<p>
295 $ opt -load ../../../lib/Debug/libhello.so --help
296 OVERVIEW: llvm .bc -> .bc modular optimizer
298 USAGE: opt [options] <input bytecode>
301 Optimizations available:
303 -funcresolve - Resolve Functions
304 -gcse - Global Common Subexpression Elimination
305 -globaldce - Dead Global Elimination
306 <b>-hello - Hello World Pass</b>
307 -indvars - Cannonicalize Induction Variables
308 -inline - Function Integration/Inlining
309 -instcombine - Combine redundant instructions
313 The pass name get added as the information string for your pass, giving some
314 documentation to users of <tt>opt</tt>. Now that you have a working pass, you
315 would go ahead and make it do the cool transformations you want. Once you get
316 it all working and tested, it may become useful to find out how fast your pass
317 is. The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command
318 line option (<tt>--time-passes</tt>) that allows you to get information about
319 the execution time of your pass along with the other passes you queue up. For
323 $ opt -load ../../../lib/Debug/libhello.so -hello -time-passes < hello.bc > /dev/null
327 ===============================================================================
328 ... Pass execution timing report ...
329 ===============================================================================
330 Total Execution Time: 0.02 seconds (0.0479059 wall clock)
332 ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name ---
333 0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bytecode Writer
334 0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction
335 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier
336 <b> 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass</b>
337 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL
340 As you can see, our implementation above is pretty fast :). The additional
341 passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify
342 that the LLVM emitted by your pass is still valid and well formed LLVM, which
343 hasn't been broken somehow.
345 Now that you have seen the basics of the mechanics behind passes, we can talk
346 about some more details of how they work and how to use them.<p>
350 <!-- *********************************************************************** -->
351 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
352 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
353 <a name="passtype">Pass classes and requirements
354 </b></font></td></tr></table><ul>
355 <!-- *********************************************************************** -->
357 One of the first things that you should do when designing a new pass is to
358 decide what class you should subclass for your pass. The <a
359 href="#basiccode">Hello World</a> example uses the <tt><a
360 href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we
361 did not discuss why or when this should occur. Here we talk about the classes
362 available, from the most general to the most specific.<p>
364 When choosing a superclass for your Pass, you should choose the <b>most
365 specific</b> class possible, while still being able to meet the requirements
366 listed. This gives the LLVM Pass Infrastructure information neccesary to
367 optimize how passes are run, so that the resultant compiler isn't unneccesarily
371 <!-- ======================================================================= -->
372 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
373 <tr><td> </td><td width="100%">
374 <font color="#EEEEFF" face="Georgia,Palatino"><b>
375 <a name="ImmutablePass">The <tt>ImmutablePass</tt> class
376 </b></font></td></tr></table><ul>
378 The most plain and boring type of pass is the "<tt><a
379 href="http://llvm.cs.uiuc.edu/doxygen/structImmutablePass.html">ImmutablePass</a></tt>"
380 class. This pass type is used for passes that do not have to be run, do not
381 change state, and never need to be updated. This is not a normal type of
382 transformation or analysis, but can provide information about the current
383 compiler configuration.<p>
385 Although this pass class is very infrequently used, it is important for
386 providing information about the current target machine being compiled for, and
387 other static information that can affect the various transformations.<p>
389 <tt>ImmutablePass</tt>'s never invalidate other transformations, are never
390 invalidated, and are never "run".<p>
393 <!-- ======================================================================= -->
394 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
395 <tr><td> </td><td width="100%">
396 <font color="#EEEEFF" face="Georgia,Palatino"><b>
397 <a name="Pass">The <tt>Pass</tt> class
398 </b></font></td></tr></table><ul>
400 The "<tt><a href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>"
401 class is the most general of all superclasses that you can use. Deriving from
402 <tt>Pass</tt> indicates that your pass uses the entire program as a unit,
403 refering to function bodies in no predictable order, or adding and removing
404 functions. Because nothing is known about the behavior of direct <tt>Pass</tt>
405 subclasses, no optimization can be done for their execution.<p>
407 To write a correct <tt>Pass</tt> subclass, derive from <tt>Pass</tt> and
408 overload the <tt>run</tt> method with the following signature:<p>
410 <!-- _______________________________________________________________________ -->
411 </ul><h4><a name="run"><hr size=0>The <tt>run</tt> method</h4><ul>
415 <b>virtual bool</b> run(Module &M) = 0;
418 The <tt>run</tt> method performs the interesting work of the pass, and should
419 return true if the module was modified by the transformation, false
424 <!-- ======================================================================= -->
425 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
426 <tr><td> </td><td width="100%">
427 <font color="#EEEEFF" face="Georgia,Palatino"><b>
428 <a name="FunctionPass">The <tt>FunctionPass</tt> class
429 </b></font></td></tr></table><ul>
431 In contrast to direct <tt>Pass</tt> subclasses, direct <tt><a
432 href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">FunctionPass</a></tt>
433 subclasses do have a predictable, local behavior that can be expected by the
434 system. All <tt>FunctionPass</tt> execute on each function in the program
435 independant of all of the other functions in the program.
436 <tt>FunctionPass</tt>'s do not require that they are executed in a particular
437 order, and <tt>FunctionPass</tt>'s do not modify external functions.<p>
439 To be explicit, <tt>FunctionPass</tt> subclasses are not allowed to:<p>
442 <li>Modify a Function other than the one currently being processed.
443 <li>Add or remove Function's from the current Module.
444 <li>Add or remove global variables from the current Module.
445 <li>Maintain state across invocations of
446 <a href="#runOnFunction"><tt>runOnFunction</tt></a> (including global data)
449 Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a
450 href="#basiccode">Hello World</a> pass for example). <tt>FunctionPass</tt>'s
451 may overload three virtual methods to do their work. All of these methods
452 should return true if they modified the program, or false if they didn't.<p>
454 <!-- _______________________________________________________________________ -->
455 </ul><h4><a name="doInitialization_mod"><hr size=0>The
456 <tt>doInitialization(Module &)</tt> method</h4><ul>
459 <b>virtual bool</b> doInitialization(Module &M);
462 The <tt>doIninitialize</tt> method is allowed to do most of the things that
463 <tt>FunctionPass</tt>'s are not allowed to do. They can add and remove
464 functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
465 is designed to do simple initialization type of stuff that does not depend on
466 the functions being processed. The <tt>doInitialization</tt> method call is not
467 scheduled to overlap with any other pass executions (thus it should be very
470 A good example of how this method should be used is the <a
471 href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a>
472 pass. This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into
473 platform dependant <tt>malloc()</tt> and <tt>free()</tt> function calls. It
474 uses the <tt>doInitialization</tt> method to get a reference to the malloc and
475 free functions that it needs, adding prototypes to the module if neccesary.<p>
477 <!-- _______________________________________________________________________ -->
478 </ul><h4><a name="runOnFunction"><hr size=0>The <tt>runOnFunction</tt> method</h4><ul>
481 <b>virtual bool</b> runOnFunction(Function &F) = 0;
484 The <tt>runOnFunction</tt> method must be implemented by your subclass to do the
485 transformation or analysis work of your pass. As usual, a true value should be
486 returned if the function is modified.<p>
488 <!-- _______________________________________________________________________ -->
489 </ul><h4><a name="doFinalization_mod"><hr size=0>The <tt>doFinalization(Module &)</tt> method</h4><ul>
492 <b>virtual bool</b> doFinalization(Module &M);
495 The <tt>doFinalization</tt> method is an infrequently used method that is called
496 when the pass framework has finished calling <a
497 href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
498 program being compiled.<p>
502 <!-- ======================================================================= -->
503 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
504 <tr><td> </td><td width="100%">
505 <font color="#EEEEFF" face="Georgia,Palatino"><b>
506 <a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
507 </b></font></td></tr></table><ul>
509 <tt>BasicBlockPass</tt>'s are just like <a
510 href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
511 their scope of inspection and modification to a single basic block at a time.
512 As such, they are <b>not</b> allowed to do any of the following:<p>
515 <li>Modify or inspect any basic blocks outside of the current one
516 <li>Maintain state across invocations of
517 <a href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a>
518 <li>Modify the constrol flow graph (by altering terminator instructions)
519 <li>Any of the things verboten for
520 <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s.
523 <tt>BasicBlockPass</tt>'s are useful for traditional local and "peephole"
524 optimizations. They may override the same <a
525 href="#doInitialization_mod"><tt>doInitialization(Module &)</tt></a> and <a
526 href="#doFinalization_mod"><tt>doFinalization(Module &)</tt></a> methods that <a
527 href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have the following virtual methods that may also be implemented:<p>
529 <!-- _______________________________________________________________________ -->
530 </ul><h4><a name="doInitialization_fn"><hr size=0>The
531 <tt>doInitialization(Function &)</tt> method</h4><ul>
534 <b>virtual bool</b> doInitialization(Function &F);
537 The <tt>doIninitialize</tt> method is allowed to do most of the things that
538 <tt>BasicBlockPass</tt>'s are not allowed to do, but that
539 <tt>FunctionPass</tt>'s can. The <tt>doInitialization</tt> method is designed
540 to do simple initialization type of stuff that does not depend on the
541 BasicBlocks being processed. The <tt>doInitialization</tt> method call is not
542 scheduled to overlap with any other pass executions (thus it should be very
546 <!-- _______________________________________________________________________ -->
547 </ul><h4><a name="runOnBasicBlock"><hr size=0>The <tt>runOnBasicBlock</tt> method</h4><ul>
550 <b>virtual bool</b> runOnBasicBlock(BasicBlock &BB) = 0;
553 Override this function to do the work of the <tt>BasicBlockPass</tt>. This
554 function is not allowed to inspect or modify basic blocks other than the
555 parameter, and are not allowed to modify the CFG. A true value must be returned
556 if the basic block is modified.<p>
559 <!-- _______________________________________________________________________ -->
560 </ul><h4><a name="doFinalization_fn"><hr size=0>The <tt>doFinalization(Function
561 &)</tt> method</h4><ul>
564 <b>virtual bool</b> doFinalization(Function &F);
567 The <tt>doFinalization</tt> method is an infrequently used method that is called
568 when the pass framework has finished calling <a
569 href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a> for every BasicBlock in the
570 program being compiled. This can be used to perform per-function
574 <!-- ======================================================================= -->
575 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
576 <tr><td> </td><td width="100%">
577 <font color="#EEEEFF" face="Georgia,Palatino"><b>
578 <a name="MachineFunctionPass">The <tt>MachineFunctionPass</tt> class
579 </b></font></td></tr></table><ul>
581 A <tt>MachineFunctionPass</tt> executes on the machine-dependent
582 representation of each LLVM function in the program,
583 independent of all of the other functions in the program.
584 A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all
585 the restrictions that apply to a <tt>FunctionPass</tt> also apply to it.
586 <tt>MachineFunctionPass</tt>es also have additional restrictions. In
587 particular, <tt>MachineFunctionPass</tt>es are not allowed to do any of
591 <li>Modify any LLVM Instructions, BasicBlocks or Functions.
592 <li>Modify a MachineFunction other than the one currently being processed.
593 <li>Add or remove MachineFunctions from the current Module.
594 <li>Add or remove global variables from the current Module.
595 <li>Maintain state across invocations of
596 <a href="#runOnMachineFunction"><tt>runOnMachineFunction</tt></a> (including global data)
600 <!-- *********************************************************************** -->
601 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
602 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
603 <a name="registration">Pass registration
604 </b></font></td></tr></table><ul>
605 <!-- *********************************************************************** -->
607 In the <a href="#basiccode">Hello World</a> example pass we illustrated how pass
608 registration works, and discussed some of the reasons that it is used and what
609 it does. Here we discuss how and why passes are registered.<p>
611 Passes can be registered in several different ways. Depending on the general
612 classification of the pass, you should use one of the following templates to
613 register the pass:<p>
616 <li><b><tt>RegisterOpt</tt></b> - This template should be used when you are
617 registering a pass that logically should be available for use in the
618 '<tt>opt</tt>' utility.<p>
620 <li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are
621 registering a pass that logically should be available for use in the
622 '<tt>analysis</tt>' utility.<p>
624 <li><b><tt>RegisterLLC</tt></b> - This template should be used when you are
625 registering a pass that logically should be available for use in the
626 '<tt>llc</tt>' utility.<p>
628 <li><b><tt>RegisterPass</tt></b> - This is the generic form of the
629 <tt>Register*</tt> templates that should be used if you want your pass listed by
630 multiple or no utilities. This template takes an extra third argument that
631 specifies which tools it should be listed in. See the <a
632 href="http://llvm.cs.uiuc.edu/doxygen/PassSupport_8h-source.html">PassSupport.h</a>
633 file for more information.<p>
636 Regardless of how you register your pass, you must specify at least two
637 parameters. The first parameter is the name of the pass that is to be used on
638 the command line to specify that the pass should be added to a program (for
639 example <tt>opt</tt> or <tt>analyze</tt>). The second argument is the name of
640 the pass, which is to be used for the <tt>--help</tt> output of programs, as
641 well as for debug output generated by the <tt>--debug-pass</tt> option.<p>
643 If you pass is constructed by its default constructor, you only ever have to
644 pass these two arguments. If, on the other hand, you require other information
645 (like target specific information), you must pass an additional argument. This
646 argument is a pointer to a function used to create the pass. For an example of
647 how this works, look at the <a
648 href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations.cpp</a>
651 If a pass is registered to be used by the <tt>analyze</tt> utility, you should
652 implement the virtual <tt>print</tt> method:<p>
654 <!-- _______________________________________________________________________ -->
655 </ul><h4><a name="print"><hr size=0>The <tt>print</tt> method</h4><ul>
658 <b>virtual void</b> print(std::ostream &O, <b>const</b> Module *M) <b>const</b>;
661 The <tt>print</tt> method must be implemented by "analyses" in order to print a
662 human readable version of the analysis results. This is useful for debugging an
663 analysis itself, as well as for other people to figure out how an analysis
664 works. The <tt>analyze</tt> tool uses this method to generate its output.<p>
666 The <tt>ostream</tt> parameter specifies the stream to write the results on, and
667 the <tt>Module</tt> parameter gives a pointer to the top level module of the
668 program that has been analyzed. Note however that this pointer may be null in
669 certain circumstances (such as calling the <tt>Pass::dump()</tt> from a
670 debugger), so it should only be used to enhance debug output, it should not be
674 <!-- *********************************************************************** -->
675 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
676 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
677 <a name="interaction">Specifying interactions between passes
678 </b></font></td></tr></table><ul>
679 <!-- *********************************************************************** -->
681 One of the main responsibilities of the <tt>PassManager</tt> is the make sure
682 that passes interact with each other correctly. Because <tt>PassManager</tt>
683 tries to <a href="#passmanager">optimize the execution of passes</a> it must
684 know how the passes interact with each other and what dependencies exist between
685 the various passes. To track this, each pass can declare the set of passes that
686 are required to be executed before the current pass, and the passes which are
687 invalidated by the current pass.<p>
689 Typically this functionality is used to require that analysis results are
690 computed before your pass is run. Running arbitrary transformation passes can
691 invalidate the computed analysis results, which is what the invalidation set
692 specifies. If a pass does not implement the <tt><a
693 href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
694 having any prerequisite passes, and invalidating <b>all</b> other passes.<p>
697 <!-- _______________________________________________________________________ -->
698 </ul><h4><a name="getAnalysisUsage"><hr size=0>The <tt>getAnalysisUsage</tt> method</h4><ul>
701 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &Info) <b>const</b>;
704 By implementing the <tt>getAnalysisUsage</tt> method, the required and
705 invalidated sets may be specified for your transformation. The implementation
706 should fill in the <tt><a
707 href="http://llvm.cs.uiuc.edu/doxygen/classAnalysisUsage.html">AnalysisUsage</a></tt>
708 object with information about which passes are required and not invalidated. To do this, the following set methods are provided by the <tt><a
709 href="http://llvm.cs.uiuc.edu/doxygen/classAnalysisUsage.html">AnalysisUsage</a></tt> class:<p>
712 <i>// addRequires - Add the specified pass to the required set for your pass.</i>
713 <b>template</b><<b>class</b> PassClass>
714 AnalysisUsage &AnalysisUsage::addRequired();
716 <i>// addPreserved - Add the specified pass to the set of analyses preserved by
718 <b>template</b><<b>class</b> PassClass>
719 AnalysisUsage &AnalysisUsage::addPreserved();
721 <i>// setPreservesAll - Call this if the pass does not modify its input at all</i>
722 <b>void</b> AnalysisUsage::setPreservesAll();
724 <i>// setPreservesCFG - This function should be called by the pass, iff they do not:
726 // 1. Add or remove basic blocks from the function
727 // 2. Modify terminator instructions in any way.
729 // This is automatically implied for <a href="#BasicBlockPass">BasicBlockPass</a>'s
731 <b>void</b> AnalysisUsage::setPreservesCFG();
734 Some examples of how to use these methods are:<p>
737 <i>// This is an example implementation from an analysis, which does not modify
738 // the program at all, yet has a prerequisite.</i>
739 <b>void</b> <a href="http://llvm.cs.uiuc.edu/doxygen/structPostDominanceFrontier.html">PostDominanceFrontier</a>::getAnalysisUsage(AnalysisUsage &AU) <b>const</b> {
740 AU.setPreservesAll();
741 AU.addRequired<<a href="http://llvm.cs.uiuc.edu/doxygen/structPostDominatorTree.html">PostDominatorTree</a>>();
748 <i>// This example modifies the program, but does not modify the CFG</i>
749 <b>void</b> <a href="http://llvm.cs.uiuc.edu/doxygen/structLICM.html">LICM</a>::getAnalysisUsage(AnalysisUsage &AU) <b>const</b> {
750 AU.setPreservesCFG();
751 AU.addRequired<<a href="http://llvm.cs.uiuc.edu/doxygen/classLoopInfo.html">LoopInfo</a>>();
755 <!-- _______________________________________________________________________ -->
756 </ul><h4><a name="getAnalysis"><hr size=0>The <tt>getAnalysis<></tt> method</h4><ul>
758 The <tt>Pass::getAnalysis<></tt> method is inherited by your class,
759 providing you with access to the passes that you declared that you required with
760 the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method. It takes
761 a single template argument that specifies which pass class you want, and returns
762 a reference to that pass.<p>
765 <b>template</b><<b>typename</b> PassClass>
766 AnalysisType &getAnalysis();
769 This method call returns a reference to the pass desired. You may get a runtime
770 assertion failure if you attempt to get an analysis that you did not declare as
771 required in your <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a>
772 implementation. This method can be called by your <tt>run*</tt> method
773 implementation, or by any other local method invoked by your <tt>run*</tt>
776 <!-- *********************************************************************** -->
777 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
778 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
779 <a name="analysisgroup">Implementing Analysis Groups
780 </b></font></td></tr></table><ul>
781 <!-- *********************************************************************** -->
783 Now that we understand the basics of how passes are defined, how the are used,
784 and how they are required from other passes, it's time to get a little bit
785 fancier. All of the pass relationships that we have seen so far are very
786 simple: one pass depends on one other specific pass to be run before it can run.
787 For many applications, this is great, for others, more flexibility is
790 In particular, some analyses are defined such that there is a single simple
791 interface to the analysis results, but multiple ways of calculating them.
792 Consider alias analysis for example. The most trivial alias analysis returns
793 "may alias" for any alias query. The most sophisticated analysis a
794 flow-sensitive, context-sensitive interprocedural analysis that can take a
795 significant amount of time to execute (and obviously, there is a lot of room
796 between these two extremes for other implementations). To cleanly support
797 situations like this, the LLVM Pass Infrastructure supports the notion of
800 <!-- _______________________________________________________________________ -->
801 </ul><h4><a name="agconcepts"><hr size=0>Analysis Group Concepts</h4><ul>
803 An Analysis Group is a single simple interface that may be implemented by
804 multiple different passes. Analysis Groups can be given human readable names
805 just like passes, but unlike passes, they need not derive from the <tt>Pass</tt>
806 class. An analysis group may have one or more implementations, one of which is
807 the "default" implementation.<p>
809 Analysis groups are used by client passes just like other passes are: the
810 <tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods.
811 In order to resolve this requirement, the <a href="#passmanager">PassManager</a>
812 scans the available passes to see if any implementations of the analysis group
813 are available. If none is available, the default implementation is created for
814 the pass to use. All standard rules for <A href="#interaction">interaction
815 between passes</a> still apply.<p>
817 Although <a href="#registration">Pass Registration</a> is optional for normal
818 passes, all analysis group implementations must be registered, and must use the
819 <A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the
820 implementation pool. Also, a default implementation of the interface
821 <b>must</b> be registered with <A
822 href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.<p>
824 As a concrete example of an Analysis Group in action, consider the <a
825 href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>
826 analysis group. The default implementation of the alias analysis interface (the
828 href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt>
829 pass) just does a few simple checks that don't require significant analysis to
830 compute (such as: two different globals can never alias each other, etc).
831 Passes that use the <tt><a
832 href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt>
833 interface (for example the <tt><a
834 href="http://llvm.cs.uiuc.edu/doxygen/classGCSE.html">gcse</a></tt> pass), do not care which implementation
835 of alias analysis is actually provided, they just use the designated
838 From the user's perspective, commands work just like normal. Issuing the
839 command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be
840 instantiated and added to the pass sequence. Issuing the command '<tt>opt
841 -somefancyaa -gcse ...</tt>' will cause the <tt>gcse</tt> pass to use the
842 <tt>somefancyaa</tt> alias analysis (which doesn't actually exist, it's just a
843 hypothetical example) instead.<p>
846 <!-- _______________________________________________________________________ -->
847 </ul><h4><a name="registerag"><hr size=0>Using <tt>RegisterAnalysisGroup</tt></h4><ul>
849 The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
850 group itself as well as add pass implementations to the analysis group. First,
851 an analysis should be registered, with a human readable name provided for it.
852 Unlike registration of passes, there is no command line argument to be specified
853 for the Analysis Group Interface itself, because it is "abstract":<p>
856 <b>static</b> RegisterAnalysisGroup<<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>> A("<i>Alias Analysis</i>");
859 Once the analysis is registered, passes can declare that they are valid
860 implementations of the interface by using the following code:<p>
864 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
865 RegisterOpt<FancyAA>
866 B("<i>somefancyaa</i>", "<i>A more complex alias analysis implementation</i>");
868 //<i> Declare that we implement the AliasAnalysis interface</i>
869 RegisterAnalysisGroup<<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>, FancyAA> C;
873 This just shows a class <tt>FancyAA</tt> that is registered normally, then uses
874 the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a
875 href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt>
876 analysis group. Every implementation of an analysis group should join using
877 this template. A single pass may join multiple different analysis groups with
882 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
883 RegisterOpt<<a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>>
884 D("<i>basicaa</i>", "<i>Basic Alias Analysis (default AA impl)</i>");
886 //<i> Declare that we implement the AliasAnalysis interface</i>
887 RegisterAnalysisGroup<<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>, <a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>, <b>true</b>> E;
891 Here we show how the default implementation is specified (using the extra
892 argument to the <tt>RegisterAnalysisGroup</tt> template). There must be exactly
893 one default implementation available at all times for an Analysis Group to be
894 used. Here we declare that the <tt><a
895 href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt>
896 pass is the default implementation for the interface.<p>
899 <!-- *********************************************************************** -->
900 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
901 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
902 <a name="passmanager">What PassManager does
903 </b></font></td></tr></table><ul>
904 <!-- *********************************************************************** -->
907 href="http://llvm.cs.uiuc.edu/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a>
908 <a href="http://llvm.cs.uiuc.edu/doxygen/classPassManager.html">class</a> takes
909 a list of passes, ensures their <a href="#interaction">prerequisites</a> are set
910 up correctly, and then schedules passes to run efficiently. All of the LLVM
911 tools that run passes use the <tt>PassManager</tt> for execution of these
914 The <tt>PassManager</tt> does two main things to try to reduce the execution
915 time of a series of passes:<p>
918 <li><b>Share analysis results</b> - The PassManager attempts to avoid
919 recomputing analysis results as much as possible. This means keeping track of
920 which analyses are available already, which analyses get invalidated, and which
921 analyses are needed to be run for a pass. An important part of work is that the
922 <tt>PassManager</tt> tracks the exact lifetime of all analysis results, allowing
923 it to <a href="#releaseMemory">free memory</a> allocated to holding analysis
924 results as soon as they are no longer needed.<p>
926 <li><b>Pipeline the execution of passes on the program</b> - The
927 <tt>PassManager</tt> attempts to get better cache and memory usage behavior out
928 of a series of passes by pipelining the passes together. This means that, given
929 a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it
930 will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on
931 the first function, then all of the <a
932 href="#FunctionPass"><tt>FunctionPass</tt></a>'s on the second function,
933 etc... until the entire program has been run through the passes.<p>
935 This improves the cache behavior of the compiler, because it is only touching
936 the LLVM program representation for a single function at a time, instead of
937 traversing the entire program. It reduces the memory consumption of compiler,
938 because, for example, only one <a
939 href="http://llvm.cs.uiuc.edu/doxygen/structDominatorSet.html"><tt>DominatorSet</tt></a>
940 needs to be calculated at a time. This also makes it possible some <a
941 href="#SMP">interesting enhancements</a> in the future.<p>
945 The effectiveness of the <tt>PassManager</tt> is influenced directly by how much
946 information it has about the behaviors of the passes it is scheduling. For
947 example, the "preserved" set is intentionally conservative in the face of an
948 unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.
949 Not implementing when it should be implemented will have the effect of not
950 allowing any analysis results to live across the execution of your pass.<p>
952 The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line
953 options that is useful for debugging pass execution, seeing how things work, and
954 diagnosing when you should be preserving more analyses than you currently are
955 (To get information about all of the variants of the <tt>--debug-pass</tt>
956 option, just type '<tt>opt --help-hidden</tt>').<p>
958 By using the <tt>--debug-pass=Structure</tt> option, for example, we can see how
959 our <a href="#basiccode">Hello World</a> pass interacts with other passes. Lets
960 try it out with the <tt>gcse</tt> and <tt>licm</tt> passes:<p>
963 $ opt -load ../../../lib/Debug/libhello.so -gcse -licm --debug-pass=Structure < hello.bc > /dev/null
965 Function Pass Manager
966 Dominator Set Construction
967 Immediate Dominators Construction
968 Global Common Subexpression Elimination
969 -- Immediate Dominators Construction
970 -- Global Common Subexpression Elimination
971 Natural Loop Construction
972 Loop Invariant Code Motion
973 -- Natural Loop Construction
974 -- Loop Invariant Code Motion
976 -- Dominator Set Construction
982 This output shows us when passes are constructed and when the analysis results
983 are known to be dead (prefixed with '<tt>--</tt>'). Here we see that GCSE uses
984 dominator and immediate dominator information to do its job. The LICM pass uses
985 natural loop information, which uses dominator sets, but not immediate
986 dominators. Because immediate dominators are no longer useful after the GCSE
987 pass, it is immediately destroyed. The dominator sets are then reused to
988 compute natural loop information, which is then used by the LICM pass.<p>
990 After the LICM pass, the module verifier runs (which is automatically added by
991 the '<tt>opt</tt>' tool), which uses the dominator set to check that the
992 resultant LLVM code is well formed. After it finishes, the dominator set
993 information is destroyed, after being computed once, and shared by three
996 Lets see how this changes when we run the <a href="#basiccode">Hello World</a>
997 pass in between the two passes:<p>
1000 $ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1002 Function Pass Manager
1003 Dominator Set Construction
1004 Immediate Dominators Construction
1005 Global Common Subexpression Elimination
1006 <b>-- Dominator Set Construction</b>
1007 -- Immediate Dominators Construction
1008 -- Global Common Subexpression Elimination
1009 <b> Hello World Pass
1011 Dominator Set Construction</b>
1012 Natural Loop Construction
1013 Loop Invariant Code Motion
1014 -- Natural Loop Construction
1015 -- Loop Invariant Code Motion
1017 -- Dominator Set Construction
1026 Here we see that the <a href="#basiccode">Hello World</a> pass has killed the
1027 Dominator Set pass, even though it doesn't modify the code at all! To fix this,
1028 we need to add the following <a
1029 href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method to our pass:<p>
1032 <i>// We don't modify the program, so we preserve all analyses</i>
1033 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &AU) <b>const</b> {
1034 AU.setPreservesAll();
1038 Now when we run our pass, we get this output:<p>
1041 $ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1042 Pass Arguments: -gcse -hello -licm
1044 Function Pass Manager
1045 Dominator Set Construction
1046 Immediate Dominators Construction
1047 Global Common Subexpression Elimination
1048 -- Immediate Dominators Construction
1049 -- Global Common Subexpression Elimination
1052 Natural Loop Construction
1053 Loop Invariant Code Motion
1054 -- Loop Invariant Code Motion
1055 -- Natural Loop Construction
1057 -- Dominator Set Construction
1066 Which shows that we don't accidentally invalidate dominator information
1067 anymore, and therefore do not have to compute it twice.<p>
1070 <!-- _______________________________________________________________________ -->
1071 </ul><h4><a name="releaseMemory"><hr size=0>The <tt>releaseMemory</tt> method</h4><ul>
1074 <b>virtual void</b> releaseMemory();
1077 The <tt>PassManager</tt> automatically determines when to compute analysis
1078 results, and how long to keep them around for. Because the lifetime of the pass
1079 object itself is effectively the entire duration of the compilation process, we
1080 need some way to free analysis results when they are no longer useful. The
1081 <tt>releaseMemory</tt> virtual method is the way to do this.<p>
1083 If you are writing an analysis or any other pass that retains a significant
1084 amount of state (for use by another pass which "requires" your pass and uses the
1085 <a href="#getAnalysis">getAnalysis</a> method) you should implement
1086 <tt>releaseMEmory</tt> to, well, release the memory allocated to maintain this
1087 internal state. This method is called after the <tt>run*</tt> method for the
1088 class, before the next call of <tt>run*</tt> in your pass.<p>
1091 <!-- *********************************************************************** -->
1092 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
1093 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1094 <a name="debughints">Using GDB with dynamically loaded passes
1095 </b></font></td></tr></table><ul>
1096 <!-- *********************************************************************** -->
1098 Unfortunately, using GDB with dynamically loaded passes is not as easy as it
1099 should be. First of all, you can't set a breakpoint in a shared object that has
1100 not been loaded yet, and second of all there are problems with inlined functions
1101 in shared objects. Here are some suggestions to debugging your pass with
1104 For sake of discussion, I'm going to assume that you are debugging a
1105 transformation invoked by <tt>opt</tt>, although nothing described here depends
1108 <!-- _______________________________________________________________________ -->
1109 </ul><h4><a name="breakpoint"><hr size=0>Setting a breakpoint in your pass</h4><ul>
1111 First thing you do is start <tt>gdb</tt> on the <tt>opt</tt> process:<p>
1116 Copyright 2000 Free Software Foundation, Inc.
1117 GDB is free software, covered by the GNU General Public License, and you are
1118 welcome to change it and/or distribute copies of it under certain conditions.
1119 Type "show copying" to see the conditions.
1120 There is absolutely no warranty for GDB. Type "show warranty" for details.
1121 This GDB was configured as "sparc-sun-solaris2.6"...
1125 Note that <tt>opt</tt> has a lot of debugging information in it, so it takes
1126 time to load. Be patient. Since we cannot set a breakpoint in our pass yet
1127 (the shared object isn't loaded until runtime), we must execute the process, and
1128 have it stop before it invokes our pass, but after it has loaded the shared
1129 object. The most foolproof way of doing this is to set a breakpoint in
1130 <tt>PassManager::run</tt> and then run the process with the arguments you
1134 (gdb) <b>break PassManager::run</b>
1135 Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
1136 (gdb) <b>run test.bc -load /shared/lattner/cvs/llvm/lib/Debug/[libname].so -[passoption]</b>
1137 Starting program: /shared/lattner/cvs/llvm/tools/Debug/opt test.bc
1138 -load /shared/lattner/cvs/llvm/lib/Debug/[libname].so -[passoption]
1139 Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
1140 70 bool PassManager::run(Module &M) { return PM->run(M); }
1144 Once the <tt>opt</tt> stops in the <tt>PassManager::run</tt> method you are now
1145 free to set breakpoints in your pass so that you can trace through execution or
1146 do other standard debugging stuff.<p>
1149 <!-- _______________________________________________________________________ -->
1150 </ul><h4><a name="debugmisc"><hr size=0>Miscellaneous Problems</h4><ul>
1152 Once you have the basics down, there are a couple of problems that GDB has, some
1153 with solutions, some without.<p>
1156 <li>Inline functions have bogus stack information. In general, GDB does a
1157 pretty good job getting stack traces and stepping through inline functions.
1158 When a pass is dynamically loaded however, it somehow completely loses this
1159 capability. The only solution I know of is to de-inline a function (move it
1160 from the body of a class to a .cpp file).<p>
1162 <li>Restarting the program breaks breakpoints. After following the information
1163 above, you have succeeded in getting some breakpoints planted in your pass. Nex
1164 thing you know, you restart the program (i.e., you type '<tt>run</tt>' again),
1165 and you start getting errors about breakpoints being unsettable. The only way I
1166 have found to "fix" this problem is to <tt>delete</tt> the breakpoints that are
1167 already set in your pass, run the program, and re-set the breakpoints once
1168 execution stops in <tt>PassManager::run</tt>.<p>
1172 Hopefully these tips will help with common case debugging situations. If you'd
1173 like to contribute some tips of your own, just contact <a
1174 href="mailto:sabre@nondot.org">Chris</a>.<p>
1177 <!-- *********************************************************************** -->
1178 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
1179 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1180 <a name="future">Future extensions planned
1181 </b></font></td></tr></table><ul>
1182 <!-- *********************************************************************** -->
1184 Although the LLVM Pass Infrastructure is very capable as it stands, and does
1185 some nifty stuff, there are things we'd like to add in the future. Here is
1186 where we are going:<p>
1188 <!-- _______________________________________________________________________ -->
1189 </ul><h4><a name="SMP"><hr size=0>Multithreaded LLVM</h4><ul>
1191 Multiple CPU machines are becoming more common and compilation can never be
1192 fast enough: obviously we should allow for a multithreaded compiler. Because of
1193 the semantics defined for passes above (specifically they cannot maintain state
1194 across invocations of their <tt>run*</tt> methods), a nice clean way to
1195 implement a multithreaded compiler would be for the <tt>PassManager</tt> class
1196 to create multiple instances of each pass object, and allow the separate
1197 instances to be hacking on different parts of the program at the same time.<p>
1199 This implementation would prevent each of the passes from having to implement
1200 multithreaded constructs, requiring only the LLVM core to have locking in a few
1201 places (for global resources). Although this is a simple extension, we simply
1202 haven't had time (or multiprocessor machines, thus a reason) to implement this.
1203 Despite that, we have kept the LLVM passes SMP ready, and you should too.<p>
1206 <!-- _______________________________________________________________________ -->
1207 </ul><h4><a name="ModuleSource"><hr size=0>A new <tt>ModuleSource</tt> interface</h4><ul>
1209 Currently, the <tt>PassManager</tt>'s <tt>run</tt> method takes a <tt><a
1210 href="http://llvm.cs.uiuc.edu/doxygen/classModule.html">Module</a></tt> as
1211 input, and runs all of the passes on this module. The problem with this
1212 approach is that none of the <tt>PassManager</tt> features can be used for
1213 timing and debugging the actual <b>loading</b> of the module from disk or
1216 To solve this problem, eventually the <tt>PassManger</tt> class will accept a
1217 <tt>ModuleSource</tt> object instead of a Module itself. When complete, this
1218 will also allow for streaming of functions out of the bytecode representation,
1219 allowing us to avoid holding the entire program in memory at once if we only are
1220 dealing with <a href="#FunctionPass">FunctionPass</a>'s.<p>
1222 As part of a different issue, eventually the bytecode loader will be extended to
1223 allow on-demand loading of functions from the bytecode representation, in order
1224 to better support the runtime reoptimizer. The bytecode format is already
1225 capable of this, the loader just needs to be reworked a bit.<p>
1228 <!-- _______________________________________________________________________ -->
1229 </ul><h4><a name="PassFunctionPass"><hr size=0><tt>Pass</tt>'s requiring <tt>FunctionPass</tt>'s</h4><ul>
1231 Currently it is illegal for a <a href="#Pass"><tt>Pass</tt></a> to require a <a
1232 href="#FunctionPass"><tt>FunctionPass</tt></a>. This is because there is only
1233 one instance of the <a href="#FunctionPass"><tt>FunctionPass</tt></a> object
1234 ever created, thus nowhere to store information for all of the functions in the
1235 program at the same time. Although this has come up a couple of times before,
1236 this has always been worked around by factoring one big complicated pass into a
1237 global and an interprocedural part, both of which are distinct. In the future,
1238 it would be nice to have this though.<p>
1240 Note that it is no problem for a <a
1241 href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a
1242 href="#Pass"><tt>Pass</tt></a>, only the other way around.<p>
1245 <!-- *********************************************************************** -->
1247 <!-- *********************************************************************** -->
1250 <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
1251 <!-- Created: Tue Aug 6 15:00:33 CDT 2002 -->
1252 <!-- hhmts start -->
1253 Last modified: Mon Jul 14 12:12:53 CDT 2003
1255 </font></body></html>