Add documentation for AnalysisGroup functionality
[oota-llvm.git] / docs / WritingAnLLVMPass.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><title>Writing an LLVM Pass</title></head>
3
4 <!--
5 I. General Structure of an LLVM Program
6
7 I.1 "What is a 'Value'?": Value & User class
8 I.2 Type & Derived Types
9 I.3 GlobalVariable, Function
10 I.4 BasicBlock
11 I.5 Instruction & Subclasses
12 1.6 Argument
13 1.7 Constants
14
15 III. Useful things to know about the LLVM source base:
16
17 III.1 Useful links that introduce the STL
18 III.2 isa<>, cast<>, dyn_cast<>
19 III.3 Makefiles, useful options
20 III.4 How to use opt & analyze to debug stuff
21 III.5 How to write a regression test
22 III.6 DEBUG() and Statistics (-debug & -stats)
23 III.7 The -time-passes option
24 III.8 ... more as needed ...
25
26 I think that writing Section #1 would be very helpful and that's the most
27 stable portion of the sourcebase.  #3 can be started on, but will probably
28 just grow as time goes on.
29
30 -->
31
32 <body bgcolor=white>
33
34 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
35 <tr><td>&nbsp; <font size=+3 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>Writing an LLVM Pass</b></font></td>
36 </tr></table>
37
38
39 <ol>
40   <li><a href="#introduction">Introduction - What is a pass?</a>
41   <li><a href="#quickstart">Quick Start - Writing hello world</a>
42     <ul>
43     <li><a href="#makefile">Setting up the build environment</a>
44     <li><a href="#basiccode">Basic code required</a>
45     <li><a href="#running">Running a pass with <tt>opt</tt>
46          or <tt>analyze</tt></a>
47     </ul>
48   <li><a href="#passtype">Pass classes and requirements</a>
49      <ul>
50      <li><a href="#Pass">The <tt>Pass</tt> class</a>
51         <ul>
52         <li><a href="#run">The <tt>run</tt> method</a>
53         </ul>
54      <li><a href="#FunctionPass">The <tt>FunctionPass</tt> class</a>
55         <ul>
56         <li><a href="#doInitialization">The <tt>doInitialization</tt> method</a>
57         <li><a href="#runOnFunction">The <tt>runOnFunction</tt> method</a>
58         <li><a href="#doFinalization">The <tt>doFinalization</tt> method</a>
59         </ul>
60      <li><a href="#BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
61         <ul>
62         <li><a href="#runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
63         </ul>
64      </ul>
65   <li><a href="#registration">Pass Registration</a>
66      <ul>
67      <li><a href="#print">The <tt>print</tt> method</a>
68      </ul>
69   <li><a href="#interaction">Specifying interactions between passes</a>
70      <ul>
71      <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a>
72      <li><a href="#getAnalysis">The <tt>getAnalysis</tt> method</a>
73      </ul>
74   <li><a href="#analysisgroup">Implementing Analysis Groups</a>
75      <ul>
76      <li><a href="#agconcepts">Analysis Group Concepts</a>
77      <li><a href="#registerag">Using <tt>RegisterAnalysisGroup</tt></a>
78      </ul>
79   <li><a href="#passmanager">What PassManager does</a>
80     <ul>
81     <li><a href="#releaseMemory">The <tt>releaseMemory</tt> method</a>
82     </ul>
83   <li><a href="#future">Future extensions planned</a>
84     <ul>
85     <li><a href="#SMP">Multithreaded LLVM</a>
86     <li><a href="#ModuleSource">A new <tt>ModuleSource</tt> interface</a>
87     <li><a href="#PassFunctionPass"><tt>Pass</tt>'s requiring <tt>FunctionPass</tt>'s</a>
88     </ul>
89
90   <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p>
91 </ol><p>
92
93
94
95 <!-- *********************************************************************** -->
96 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
97 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
98 <a name="introduction">Introduction - What is a pass?
99 </b></font></td></tr></table><ul>
100 <!-- *********************************************************************** -->
101
102 The LLVM Pass Framework is an important part of the LLVM system, because LLVM
103 passes are where the interesting parts of the compiler exist.  Passes perform
104 the transformations and optimizations that make up the compiler, they build
105 the analysis results that are used by these transformations, and they are, above
106 all, a structuring technique for compiler code.<p>
107
108 All LLVM passes are subclasses of the <tt><a
109 href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt> class, which
110 implement functionality by overriding virtual methods inherited from
111 <tt>Pass</tt>.  Depending on how your pass works, you may be able to inherit
112 from the <tt><a
113 href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt>
114 or <tt><a
115 href="http://llvm.cs.uiuc.edu/doxygen/structBasicBlockPass.html">BasicBlockPass</a></tt>,
116 which gives the system more information about what your pass does, and how it
117 can be combined with other passes.  One of the main features of the LLVM Pass
118 Framework is that it schedules passes to run in an efficient way based on the
119 constraints that your pass has.<p>
120
121 We start by showing you how to construct a pass, everything from setting up the
122 code, to compiling, loading, and executing it.  After the basics are down, more
123 advanced features are discussed.<p>
124
125
126 <!-- *********************************************************************** -->
127 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
128 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
129 <a name="quickstart">Quick Start - Writing hello world
130 </b></font></td></tr></table><ul>
131 <!-- *********************************************************************** -->
132
133 Here we describe how to write the "hello world" of passes.  The "Hello" pass is
134 designed to simply print out the name of non-external functions that exist in
135 the program being compiled.  It does not modify the program at all, just
136 inspects it.  The source code and files for this pass are available in the LLVM
137 source tree in the <tt>lib/Transforms/Hello</tt> directory.<p>
138
139
140 <!-- ======================================================================= -->
141 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
142 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
143 <font color="#EEEEFF" face="Georgia,Palatino"><b>
144 <a name="makefile">Setting up the build environment
145 </b></font></td></tr></table><ul>
146
147 First thing you need to do is create a new directory somewhere in the LLVM
148 source base.  For this example, we'll assume that you made
149 "<tt>lib/Transforms/Hello</tt>".  The first thing you must do is set up a build
150 script (Makefile) that will compile the source code for the new pass.  To do
151 this, copy this into "<tt>Makefile</tt>" (be very careful that there are no
152 extra space characters at the end of the lines though... that seems to confuse
153 <tt>gmake</tt>):<p>
154
155 </ul><hr><ul><pre>
156 # Makefile for hello pass
157
158 # Path to top level of LLVM heirarchy
159 LEVEL = ../../..
160
161 # Name of the library to build
162 LIBRARYNAME = hello
163
164 # Build a dynamically loadable shared object
165 SHARED_LIBRARY = 1
166
167 # Include the makefile implementation stuff
168 include $(LEVEL)/Makefile.common
169 </pre></ul><hr><ul><p>
170
171 This makefile specifies that all of the <tt>.cpp</tt> files in the current
172 directory are to be compiled and linked together into a
173 <tt>lib/Debug/libhello.so</tt> shared object that can be dynamically loaded by
174 the <tt>opt</tt> or <tt>analyze</tt> tools.<p>
175
176 Now that we have the build scripts set up, we just need to write the code for
177 the pass itself.<p>
178
179
180 <!-- ======================================================================= -->
181 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
182 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
183 <font color="#EEEEFF" face="Georgia,Palatino"><b>
184 <a name="basiccode">Basic code required
185 </b></font></td></tr></table><ul>
186
187 Now that we have a way to compile our new pass, we just have to write it.  Start
188 out with:<p>
189
190 <pre>
191 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
192 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
193 </pre>
194
195 Which are needed because we are writing a <tt><a
196 href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>, and we are
197 operating on <tt><a
198 href="http://llvm.cs.uiuc.edu/doxygen/classFunction.html">Function</a></tt>'s.<p>
199
200 Next we have:<p>
201
202 <pre>
203 <b>namespace</b> {
204 </pre><p>
205
206 ... which starts out an anonymous namespace.  Anonymous namespaces are to C++
207 what the "<tt>static</tt>" keyword is to C (at global scope).  It makes the
208 things declared inside of the anonymous namespace only visible to the current
209 file.  If you're not familiar with them, consult a decent C++ book for more
210 information.<p>
211
212 Next, we declare our pass itself:<p>
213
214 <pre>
215   <b>struct</b> Hello : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
216 </pre><p>
217
218 This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a
219 href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt>.
220 The different builting pass subclasses are described in detail <a
221 href="#passtype">later</a>, but for now, know that <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s
222 operate a function at a time.<p>
223
224 <pre>
225     <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &F) {
226       std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
227       <b>return false</b>;
228     }
229   };  <i>// end of struct Hello</i>
230 </pre>
231
232 We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method, which
233 overloads an abstract virtual method inherited from <a
234 href="#FunctionPass"><tt>FunctionPass</tt></a>.  This is where we are supposed
235 to do our thing, so we just print out our message with the name of each
236 function.<p>
237
238 <pre>
239   RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
240 }  <i>// end of anonymous namespace</i>
241 </pre><p>
242
243 Lastly, we register our class <tt>Hello</tt>, giving it a command line argument
244 "<tt>hello</tt>", and a name "<tt>Hello World Pass</tt>".  There are several
245 different ways of <a href="#registration">registering your pass</a>, depending
246 on what it is to be used for.  For "optimizations" we use the
247 <tt>RegisterOpt</tt> template.<p>
248
249 As a whole, the <tt>.cpp</tt> file looks like:<p>
250
251 <pre>
252 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
253 <b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
254
255 <b>namespace</b> {
256   <b>struct Hello</b> : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
257     <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &F) {
258       std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
259       <b>return false</b>;
260     }
261   };
262   
263   RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
264 }
265 </pre><p>
266
267 Now that it's all together, compile the file with a simple "<tt>gmake</tt>"
268 command in the local directory and you should get a new
269 "<tt>lib/Debug/libhello.so</tt> file.  Note that everything in this file is
270 contained in an anonymous namespace: this reflects the fact that passes are self
271 contained units that do not need external interfaces (although they can have
272 them) to be useful.<p>
273
274
275 <!-- ======================================================================= -->
276 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
277 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
278 <font color="#EEEEFF" face="Georgia,Palatino"><b>
279 <a name="running">Running a pass with <tt>opt</tt> or <tt>analyze</tt>
280 </b></font></td></tr></table><ul>
281
282 Now that you have a brand new shiny <tt>.so</tt> file, we can use the
283 <tt>opt</tt> command to run an LLVM program through your pass.  Because you
284 registered your pass with the <tt>RegisterOpt</tt> template, you will be able to
285 use the <tt>opt</tt> tool to access it, once loaded.<p>
286
287 To test it, follow the example at the end of the <a
288 href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to
289 LLVM.  We can now run the bytecode file (<tt>hello.bc</tt>) for the program
290 through our transformation like this (or course, any bytecode file will
291 work):<p>
292
293 <pre>
294 $ opt -load ../../../lib/Debug/libhello.so -hello &lt; hello.bc &gt; /dev/null
295 Hello: __main
296 Hello: puts
297 Hello: main
298 </pre><p>
299
300 The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your pass
301 as a shared object, which makes '<tt>-hello</tt>' a valid command line argument
302 (which is one reason you need to <a href="#registration">register your
303 pass</a>).  Because the hello pass does not modify the program in any
304 interesting way, we just throw away the result of <tt>opt</tt> (sending it to
305 <tt>/dev/null</tt>).<p>
306
307 To see what happened to the other string you registered, try running
308 <tt>opt</tt> with the <tt>--help</tt> option:<p>
309
310 <pre>
311 $ opt -load ../../../lib/Debug/libhello.so --help
312 OVERVIEW: llvm .bc -&gt; .bc modular optimizer
313
314 USAGE: opt [options] &lt;input bytecode&gt;
315
316 OPTIONS:
317   Optimizations available:
318 ...
319     -funcresolve    - Resolve Functions
320     -gcse           - Global Common Subexpression Elimination
321     -globaldce      - Dead Global Elimination
322     <b>-hello          - Hello World Pass</b>
323     -indvars        - Cannonicalize Induction Variables
324     -inline         - Function Integration/Inlining
325     -instcombine    - Combine redundant instructions
326 ...
327 </pre><p>
328
329 The pass name get added as the information string for your pass, giving some
330 documentation to users of <tt>opt</tt>.  Now that you have a working pass, you
331 would go ahead and make it do the cool transformations you want.  Once you get
332 it all working and tested, it may become useful to find out how fast your pass
333 is.  The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command
334 line option (<tt>--time-passes</tt>) that allows you to get information about
335 the execution time of your pass along with the other passes you queue up.  For
336 example:<p>
337
338 <pre>
339 $ opt -load ../../../lib/Debug/libhello.so -hello -time-passes &lt; hello.bc &gt; /dev/null
340 Hello: __main
341 Hello: puts
342 Hello: main
343 ===============================================================================
344                       ... Pass execution timing report ...
345 ===============================================================================
346   Total Execution Time: 0.02 seconds (0.0479059 wall clock)
347
348    ---User Time---   --System Time--   --User+System--   ---Wall Time---  --- Pass Name ---
349    0.0100 (100.0%)   0.0000 (  0.0%)   0.0100 ( 50.0%)   0.0402 ( 84.0%)  Bytecode Writer
350    0.0000 (  0.0%)   0.0100 (100.0%)   0.0100 ( 50.0%)   0.0031 (  6.4%)  Dominator Set Construction
351    0.0000 (  0.0%)   0.0000 (  0.0%)   0.0000 (  0.0%)   0.0013 (  2.7%)  Module Verifier
352  <b>  0.0000 (  0.0%)   0.0000 (  0.0%)   0.0000 (  0.0%)   0.0033 (  6.9%)  Hello World Pass</b>
353    0.0100 (100.0%)   0.0100 (100.0%)   0.0200 (100.0%)   0.0479 (100.0%)  TOTAL
354 </pre><p>
355
356 As you can see, our implementation above is pretty fast :).  The additional
357 passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify
358 that the LLVM emitted by your pass is still valid and well formed LLVM, which
359 hasn't been broken somehow.
360
361 Now that you have seen the basics of the mechanics behind passes, we can talk
362 about some more details of how they work and how to use them.<p>
363
364
365
366 <!-- *********************************************************************** -->
367 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
368 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
369 <a name="passtype">Pass classes and requirements
370 </b></font></td></tr></table><ul>
371 <!-- *********************************************************************** -->
372
373 One of the first things that you should do when designing a new pass is to
374 decide what class you should subclass for your pass.  The <a
375 href="#basiccode">Hello World</a> example uses the <tt><a
376 href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we
377 did not discuss why or when this should occur.  Here we talk about the classes
378 available, from the most general to the most specific.<p>
379
380 When choosing a superclass for your Pass, you should choose the <b>most
381 specific</b> class possible, while still being able to meet the requirements
382 listed.  This gives the LLVM Pass Infrastructure information neccesary to
383 optimize how passes are run, so that the resultant compiler isn't unneccesarily
384 slow.<p>
385
386
387
388 <!-- ======================================================================= -->
389 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
390 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
391 <font color="#EEEEFF" face="Georgia,Palatino"><b>
392 <a name="Pass">The <tt>Pass</tt> class
393 </b></font></td></tr></table><ul>
394
395 The "<tt><a href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>"
396 class is the most general of all superclasses that you can use.  Deriving from
397 <tt>Pass</tt> indicates that your pass uses the entire program as a unit,
398 refering to function bodies in no predictable order, or adding and removing
399 functions.  Because nothing is known about the behavior of direct <tt>Pass</tt>
400 subclasses, no optimization can be done for their execution.<p>
401
402 To write a correct <tt>Pass</tt> subclass, derive from <tt>Pass</tt> and
403 overload the <tt>run</tt> method with the following signature:<p>
404
405 <!-- _______________________________________________________________________ -->
406 </ul><h4><a name="run"><hr size=0>The <tt>run</tt> method</h4><ul>
407
408
409 <pre>
410   <b>virtual bool</b> run(Module &amp;M) = 0;
411 </pre><p>
412
413 The <tt>run</tt> method performs the interesting work of the pass, and should
414 return true if the module was modified by the transformation, false
415 otherwise.<p>
416
417
418
419 <!-- ======================================================================= -->
420 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
421 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
422 <font color="#EEEEFF" face="Georgia,Palatino"><b>
423 <a name="FunctionPass">The <tt>FunctionPass</tt> class
424 </b></font></td></tr></table><ul>
425
426 In contrast to direct <tt>Pass</tt> subclasses, direct <tt><a
427 href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">FunctionPass</a></tt>
428 subclasses do have a predictable, local behavior that can be expected by the
429 system.  All <tt>FunctionPass</tt> execute on each function in the program
430 independant of all of the other functions in the program.
431 <tt>FunctionPass</tt>'s do not require that they are executed in a particular
432 order, and <tt>FunctionPass</tt>'s do not modify external functions.<p>
433
434 To be explicit, <tt>FunctionPass</tt> subclasses are not allowed to:<p>
435
436 <ol>
437 <li>Modify a Function other than the one currently being processed.
438 <li>Add or remove Function's from the current Module.
439 <li>Add or remove global variables from the current Module.
440 <li>Maintain state across invocations of
441     <a href="#runOnFunction"><tt>runOnFunction</tt></a> (including global data)
442 </ol><p>
443
444 Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a
445 href="#basiccode">Hello World</a> pass for example).  <tt>FunctionPass</tt>'s
446 may overload three virtual methods to do their work.  All of these methods
447 should return true if they modified the program, or false if they didn't.<p>
448
449 <!-- _______________________________________________________________________ -->
450 </ul><h4><a name="doInitialization"><hr size=0>The <tt>doInitialization</tt>
451 method</h4><ul>
452
453 <pre>
454   <b>virtual bool</b> doInitialization(Module &amp;M);
455 </pre><p>
456
457 The <tt>doIninitialize</tt> method is allowed to do most of the things that
458 <tt>FunctionPass</tt>'s are not allowed to do.  They can add and remove
459 functions, get pointers to functions, etc.  The <tt>doInitialize</tt> method is
460 designed to do simple initialization type of stuff that does not depend on the
461 functions being processed.  The <tt>doInitialization</tt> function call is not
462 scheduled to overlap with any other pass executions.<p>
463
464 A good example of how this method should be used is the <a
465 href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a>
466 pass.  This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into
467 platform dependant <tt>malloc()</tt> and <tt>free()</tt> function calls.  It
468 uses the <tt>doInitialization</tt> method to get a reference to the malloc and
469 free functions that it needs, adding prototypes to the module if neccesary.<p>
470
471 <!-- _______________________________________________________________________ -->
472 </ul><h4><a name="runOnFunction"><hr size=0>The <tt>runOnFunction</tt> method</h4><ul>
473
474 <pre>
475   <b>virtual bool</b> runOnFunction(Function &amp;F) = 0;
476 </pre><p>
477
478 The <tt>runOnFunction</tt> method must be implemented by your subclass to do the
479 transformation or analysis work of your pass.  As usual, a true value should be
480 returned if the function is modified.<p>
481
482 <!-- _______________________________________________________________________ -->
483 </ul><h4><a name="doFinalization"><hr size=0>The <tt>doFinalization</tt> method</h4><ul>
484
485 <pre>
486   <b>virtual bool</b> doFinalization(Module &amp;M);
487 </pre</p>
488
489 The <tt>doFinalization</tt> method is an infrequently used method that is called
490 when the pass framework has finished calling <a
491 href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
492 program being compiled.<p>
493
494
495
496 <!-- ======================================================================= -->
497 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
498 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
499 <font color="#EEEEFF" face="Georgia,Palatino"><b>
500 <a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
501 </b></font></td></tr></table><ul>
502
503 <tt>BasicBlockPass</tt>'s are just like <a
504 href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
505 their scope of inspection and modification to a single basic block at a time.
506 As such, they are <b>not</b> allowed to do any of the following:<p>
507
508 <ol>
509 <li>Modify or inspect any basic blocks outside of the current one
510 <li>Maintain state across invocations of
511     <a href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a>
512 <li>Modify the constrol flow graph (by altering terminator instructions)
513 <li>Any of the things verboten for
514     <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s.
515 </ol><p>
516
517 <tt>BasicBlockPass</tt>'s are useful for traditional local and "peephole"
518 optimizations.  They may override the same <a
519 href="#doInitialization"><tt>doInitialization</tt></a> and <a
520 href="#doFinalization"><tt>doFinalization</tt></a> methods that <a
521 href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have a
522 <tt>runOnBasicBlock</tt> method:<p>
523
524 <!-- _______________________________________________________________________ -->
525 </ul><h4><a name="runOnBasicBlock"><hr size=0>The <tt>runOnBasicBlock</tt> method</h4><ul>
526
527 <pre>
528   <b>virtual bool</b> runOnBasicBlock(BasicBlock &amp;BB) = 0;
529 </pre><p>
530
531 Override this function to do the work of the <tt>BasicBlockPass</tt>.  This
532 function is not allowed to inspect or modify basic blocks other than the
533 parameter, and are not allowed to modify the CFG.  A true value must be returned
534 if the basic block is modified.<p>
535
536
537 <!-- *********************************************************************** -->
538 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
539 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
540 <a name="registration">Pass registration
541 </b></font></td></tr></table><ul>
542 <!-- *********************************************************************** -->
543
544 In the <a href="#basiccode">Hello World</a> example pass we illustrated how pass
545 registration works, and discussed some of the reasons that it is used and what
546 it does.  Here we discuss how and why passes are registered.<p>
547
548 Passes can be registered in several different ways.  Depending on the general
549 classification of the pass, you should use one of the following templates to
550 register the pass:<p>
551
552 <ul>
553 <li><b><tt>RegisterOpt</tt></b> - This template should be used when you are
554 registering a pass that logically should be available for use in the
555 '<tt>opt</tt>' utility.<p>
556
557 <li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are
558 registering a pass that logically should be available for use in the
559 '<tt>analysis</tt>' utility.<p>
560
561 <li><b><tt>RegisterLLC</tt></b> - This template should be used when you are
562 registering a pass that logically should be available for use in the
563 '<tt>llc</tt>' utility.<p>
564
565 <li><b><tt>RegisterPass</tt></b> - This is the generic form of the
566 <tt>Register*</tt> templates that should be used if you want your pass listed by
567 multiple or no utilities.  This template takes an extra third argument that
568 specifies which tools it should be listed in.  See the <a
569 href="http://llvm.cs.uiuc.edu/doxygen/PassSupport_8h-source.html">PassSupport.h</a>
570 file for more information.<p>
571 </ul><p>
572
573 Regardless of how you register your pass, you must specify at least two
574 parameters.  The first parameter is the name of the pass that is to be used on
575 the command line to specify that the pass should be added to a program (for
576 example <tt>opt</tt> or <tt>analyze</tt>).  The second argument is the name of
577 the pass, which is to be used for the <tt>--help</tt> output of programs, as
578 well as for debug output generated by the <tt>--debug-pass</tt> option.<p>
579
580 If you pass is constructed by its default constructor, you only ever have to
581 pass these two arguments.  If, on the other hand, you require other information
582 (like target specific information), you must pass an additional argument.  This
583 argument is a pointer to a function used to create the pass.  For an example of
584 how this works, look at the <a
585 href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations.cpp</a>
586 file.<p>
587
588 If a pass is registered to be used by the <tt>analyze</tt> utility, you should
589 implement the virtual <tt>print</tt> method:<p>
590
591 <!-- _______________________________________________________________________ -->
592 </ul><h4><a name="print"><hr size=0>The <tt>print</tt> method</h4><ul>
593
594 <pre>
595   <b>virtual void</b> print(std::ostream &amp;O, <b>const</b> Module *M) <b>const</b>;
596 </pre><p>
597
598 The <tt>print</tt> method must be implemented by "analyses" in order to print a
599 human readable version of the analysis results.  This is useful for debugging an
600 analysis itself, as well as for other people to figure out how an analysis
601 works.  The <tt>analyze</tt> tool uses this method to generate its output.<p>
602
603 The <tt>ostream</tt> parameter specifies the stream to write the results on, and
604 the <tt>Module</tt> parameter gives a pointer to the top level module of the
605 program that has been analyzed.  Note however that this pointer may be null in
606 certain circumstances (such as calling the <tt>Pass::dump()</tt> from a
607 debugger), so it should only be used to enhance debug output, it should not be
608 depended on.<p>
609
610
611 <!-- *********************************************************************** -->
612 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
613 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
614 <a name="interaction">Specifying interactions between passes
615 </b></font></td></tr></table><ul>
616 <!-- *********************************************************************** -->
617
618 One of the main responsibilities of the <tt>PassManager</tt> is the make sure
619 that passes interact with each other correctly.  Because <tt>PassManager</tt>
620 tries to <a href="#passmanager">optimize the execution of passes</a> it must
621 know how the passes interact with each other and what dependencies exist between
622 the various passes.  To track this, each pass can declare the set of passes that
623 are required to be executed before the current pass, and the passes which are
624 invalidated by the current pass.<p>
625
626 Typically this functionality is used to require that analysis results are
627 computed before your pass is run.  Running arbitrary transformation passes can
628 invalidate the computed analysis results, which is what the invalidation set
629 specifies.  If a pass does not implement the <tt><a
630 href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
631 having any prerequisite passes, and invalidating <b>all</b> other passes.<p>
632
633
634 <!-- _______________________________________________________________________ -->
635 </ul><h4><a name="getAnalysisUsage"><hr size=0>The <tt>getAnalysisUsage</tt> method</h4><ul>
636
637 <pre>
638   <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;Info) <b>const</b>;
639 </pre><p>
640
641 By implementing the <tt>getAnalysisUsage</tt> method, the required and
642 invalidated sets may be specified for your transformation.  The implementation
643 should fill in the <tt><a
644 href="http://llvm.cs.uiuc.edu/doxygen/classAnalysisUsage.html">AnalysisUsage</a></tt>
645 object with information about which passes are required and not invalidated.  To do this, the following set methods are provided by the <tt><a
646 href="http://llvm.cs.uiuc.edu/doxygen/classAnalysisUsage.html">AnalysisUsage</a></tt> class:<p>
647
648 <pre>
649   <i>// addRequires - Add the specified pass to the required set for your pass.</i>
650   <b>template</b>&lt;<b>class</b> PassClass&gt;
651   AnalysisUsage &amp;AnalysisUsage::addRequired();
652
653   <i>// addPreserved - Add the specified pass to the set of analyses preserved by
654   // this pass</i>
655   <b>template</b>&lt;<b>class</b> PassClass&gt;
656   AnalysisUsage &amp;AnalysisUsage::addPreserved();
657
658   <i>// setPreservesAll - Call this if the pass does not modify its input at all</i>
659   <b>void</b> AnalysisUsage::setPreservesAll();
660
661   <i>// preservesCFG - This function should be called by the pass, iff they do not:
662   //
663   //  1. Add or remove basic blocks from the function
664   //  2. Modify terminator instructions in any way.
665   //
666   //  This is automatically implied for <a href="#BasicBlockPass">BasicBlockPass</a>'s
667   //</i>
668   <b>void</b> AnalysisUsage::preservesCFG();
669 </pre><p>
670
671 Some examples of how to use these methods are:<p>
672
673 <pre>
674   <i>// This is an example implementation from an analysis, which does not modify
675   // the program at all, yet has a prerequisite.</i>
676   <b>void</b> <a href="http://llvm.cs.uiuc.edu/doxygen/structPostDominanceFrontier.html">PostDominanceFrontier</a>::getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
677     AU.setPreservesAll();
678     AU.addRequired&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/structPostDominatorTree.html">PostDominatorTree</a>&gt;();
679   }
680 </pre><p>
681
682 and:<p>
683
684 <pre>
685   <i>// This example modifies the program, but does not modify the CFG</i>
686   <b>void</b> <a href="http://llvm.cs.uiuc.edu/doxygen/structLICM.html">LICM</a>::getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
687     AU.preservesCFG();
688     AU.addRequired&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/classLoopInfo.html">LoopInfo</a>&gt;();
689   }
690 </pre><p>
691
692 <!-- _______________________________________________________________________ -->
693 </ul><h4><a name="getAnalysis"><hr size=0>The <tt>getAnalysis&lt;&gt;</tt> method</h4><ul>
694
695 The <tt>Pass::getAnalysis&lt;&gt;</tt> method is inherited by your class,
696 providing you with access to the passes that you declared that you required with
697 the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.  It takes
698 a single template argument that specifies which pass class you want, and returns
699 a reference to that pass.<p>
700
701 <pre>
702   <b>template</b>&lt;<b>typename</b> PassClass&gt;
703   AnalysisType &amp;getAnalysis();
704 </pre><p>
705
706 This method call returns a reference to the pass desired.  You may get a runtime
707 assertion failure if you attempt to get an analysis that you did not declare as
708 required in your <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a>
709 implementation.  This method can be called by your <tt>run*</tt> method
710 implementation, or by any other local method invoked by your <tt>run*</tt>
711 method.<p>
712
713 <!-- *********************************************************************** -->
714 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
715 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
716 <a name="analysisgroup">Implementing Analysis Groups
717 </b></font></td></tr></table><ul>
718 <!-- *********************************************************************** -->
719
720 Now that we understand the basics of how passes are defined, how the are used,
721 and how they are required from other passes, it's time to get a little bit
722 fancier.  All of the pass relationships that we have seen so far are very
723 simple: one pass depends on one other specific pass to be run before it can run.
724 For many applications, this is great, for others, more flexibility is
725 required.<p>
726
727 In particular, some analyses are defined such that there is a single simple
728 interface to the analysis results, but multiple ways of calculating them.
729 Consider alias analysis for example.  The most trivial alias analysis returns
730 "may alias" for any alias query.  The most sophisticated analysis a
731 flow-sensitive, context-sensitive interprocedural analysis that can take a
732 significant amount of time to execute (and obviously, there is a lot of room
733 between these two extremes for other implementations).  To cleanly support
734 situations like this, the LLVM Pass Infrastructure supports the notion of
735 Analysis Groups.<p>
736
737 <!-- _______________________________________________________________________ -->
738 </ul><h4><a name="agconcepts"><hr size=0>Analysis Group Concepts</h4><ul>
739
740 An Analysis Group is a single simple interface that may be implemented by
741 multiple different passes.  Analysis Groups can be given human readable names
742 just like passes, but unlike passes, they need not derive from the <tt>Pass</tt>
743 class.  An analysis group may have one or more implementations, one of which is
744 the "default" implementation.<p>
745
746 Analysis groups are used by client passes just like other passes are: the
747 <tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods.
748 In order to resolve this requirement, the <a href="#passmanager">PassManager</a>
749 scans the available passes to see if any implementations of the analysis group
750 are available.  If none is available, the default implementation is created for
751 the pass to use.  All standard rules for <A href="#interaction">interaction
752 between passes</a> still apply.<p>
753
754 Although <a href="#registration">Pass Registration</a> is optional for normal
755 passes, all analysis group implementations must be registered, and must use the
756 <A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the
757 implementation pool.  Also, a default implementation of the interface
758 <b>must</b> be registered with <A
759 href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.<p>
760
761 As a concrete example of an Analysis Group in action, consider the <a
762 href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>
763 analysis group.  The default implementation of the alias analysis interface (the
764 <tt><a
765 href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt>
766 pass) just does a few simple checks that don't require significant analysis to
767 compute (such as: two different globals can never alias each other, etc).
768 Passes that use the <tt><a
769 href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt>
770 interface (for example the <tt><a
771 href="http://llvm.cs.uiuc.edu/doxygen/classGCSE.html">gcse</a></tt> pass), do not care which implementation
772 of alias analysis is actually provided, they just use the designated
773 interface.<p>
774
775 From the user's perspective, commands work just like normal.  Issuing the
776 command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be
777 instantiated and added to the pass sequence.  Issuing the command '<tt>opt
778 -somefancyaa -gcse ...</tt>' will cause the <tt>gcse</tt> pass to use the
779 <tt>somefancyaa</tt> alias analysis (which doesn't actually exist, it's just a
780 hypothetical example) instead.<p>
781
782
783 <!-- _______________________________________________________________________ -->
784 </ul><h4><a name="registerag"><hr size=0>Using <tt>RegisterAnalysisGroup</tt></h4><ul>
785
786 The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
787 group itself as well as add pass implementations to the analysis group.  First,
788 an analysis should be registered, with a human readable name provided for it.
789 Unlike registration of passes, there is no command line argument to be specified
790 for the Analysis Group Interface itself, because it is "abstract":<p>
791
792 <pre>
793   <b>static</b> RegisterAnalysisGroup&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>&gt; A("<i>Alias Analysis</i>");
794 </pre><p>
795
796 Once the analysis is registered, passes can declare that they are valid
797 implementations of the interface by using the following code:<p>
798
799 <pre>
800 <b>namespace</b> {
801   //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
802   RegisterOpt&lt;FancyAA&gt;
803   B("<i>somefancyaa</i>", "<i>A more complex alias analysis implementation</i>");
804
805   //<i> Declare that we implement the AliasAnalysis interface</i>
806   RegisterAnalysisGroup&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>, FancyAA&gt; C;
807 }
808 </pre><p>
809
810 This just shows a class <tt>FancyAA</tt> that is registered normally, then uses
811 the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a
812 href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt>
813 analysis group.  Every implementation of an analysis group should join using
814 this template.  A single pass may join multiple different analysis groups with
815 no problem.<p>
816
817 <pre>
818 <b>namespace</b> {
819   //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
820   RegisterOpt&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>&gt;
821   D("<i>basicaa</i>", "<i>Basic Alias Analysis (default AA impl)</i>");
822
823   //<i> Declare that we implement the AliasAnalysis interface</i>
824   RegisterAnalysisGroup&lt;<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>&gt; E;
825 }
826 </pre><p>
827
828 Here we show how the default implementation is specified (using the extra
829 argument to the <tt>RegisterAnalysisGroup</tt> template).  There must be exactly
830 one default implementation available at all times for an Analysis Group to be
831 used.  Here we declare that the <tt><a
832 href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt>
833 pass is the default implementation for the interface.<p>
834
835
836 <!-- *********************************************************************** -->
837 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
838 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
839 <a name="passmanager">What PassManager does
840 </b></font></td></tr></table><ul>
841 <!-- *********************************************************************** -->
842
843 The <a
844 href="http://llvm.cs.uiuc.edu/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a>
845 <a href="http://llvm.cs.uiuc.edu/doxygen/classPassManager.html">class</a> takes
846 a list of passes, ensures their <a href="#interaction">prerequisites</a> are set
847 up correctly, and then schedules passes to run efficiently.  All of the LLVM
848 tools that run passes use the <tt>PassManager</tt> for execution of these
849 passes.<p>
850
851 The <tt>PassManager</tt> does two main things to try to reduce the execution
852 time of a series of passes:<p>
853
854 <ol>
855 <li><b>Share analysis results</b> - The PassManager attempts to avoid
856 recomputing analysis results as much as possible.  This means keeping track of
857 which analyses are available already, which analyses get invalidated, and which
858 analyses are needed to be run for a pass.  An important part of work is that the
859 <tt>PassManager</tt> tracks the exact lifetime of all analysis results, allowing
860 it to <a href="#releaseMemory">free memory</a> allocated to holding analysis
861 results as soon as they are no longer needed.<p>
862
863 <li><b>Pipeline the execution of passes on the program</b> - The
864 <tt>PassManager</tt> attempts to get better cache and memory usage behavior out
865 of a series of passes by pipelining the passes together.  This means that, given
866 a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it
867 will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on
868 the first function, then all of the <a
869 href="#FunctionPass"><tt>FunctionPass</tt></a>'s on the second function,
870 etc... until the entire program has been run through the passes.<p>
871
872 This improves the cache behavior of the compiler, because it is only touching
873 the LLVM program representation for a single function at a time, instead of
874 traversing the entire program.  It reduces the memory consumption of compiler,
875 because, for example, only one <a
876 href="http://llvm.cs.uiuc.edu/doxygen/structDominatorSet.html"><tt>DominatorSet</tt></a>
877 needs to be calculated at a time.  This also makes it possible some <a
878 href="#SMP">interesting enhancements</a> in the future.<p>
879
880 </ol><p>
881
882 The effectiveness of the <tt>PassManager</tt> is influenced directly by how much
883 information it has about the behaviors of the passes it is scheduling.  For
884 example, the "preserved" set is intentionally conservative in the face of an
885 unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.
886 Not implementing when it should be implemented will have the effect of not
887 allowing any analysis results to live across the execution of your pass.<p>
888
889 The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line
890 options that is useful for debugging pass execution, seeing how things work, and
891 diagnosing when you should be preserving more analyses than you currently are
892 (To get information about all of the variants of the <tt>--debug-pass</tt>
893 option, just type '<tt>opt --help-hidden</tt>').<p>
894
895 By using the <tt>--debug-pass=Structure</tt> option, for example, we can see how
896 our <a href="#basiccode">Hello World</a> pass interacts with other passes.  Lets
897 try it out with the <tt>gcse</tt> and <tt>licm</tt> passes:<p>
898
899 <pre>
900 $ opt -load ../../../lib/Debug/libhello.so -gcse -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
901 Module Pass Manager
902   Function Pass Manager
903     Dominator Set Construction
904     Immediate Dominators Construction
905     Global Common Subexpression Elimination
906 --  Immediate Dominators Construction
907 --  Global Common Subexpression Elimination
908     Natural Loop Construction
909     Loop Invariant Code Motion
910 --  Natural Loop Construction
911 --  Loop Invariant Code Motion
912     Module Verifier
913 --  Dominator Set Construction
914 --  Module Verifier
915   Bytecode Writer
916 --Bytecode Writer
917 </pre><p>
918
919 This output shows us when passes are constructed and when the analysis results
920 are known to be dead (prefixed with '<tt>--</tt>').  Here we see that GCSE uses
921 dominator and immediate dominator information to do its job.  The LICM pass uses
922 natural loop information, which uses dominator sets, but not immediate
923 dominators.  Because immediate dominators are no longer useful after the GCSE
924 pass, it is immediately destroyed.  The dominator sets are then reused to
925 compute natural loop information, which is then used by the LICM pass.<p>
926
927 After the LICM pass, the module verifier runs (which is automatically added by
928 the '<tt>opt</tt>' tool), which uses the dominator set to check that the
929 resultant LLVM code is well formed.  After it finishes, the dominator set
930 information is destroyed, after being computed once, and shared by three
931 passes.<p>
932
933 Lets see how this changes when we run the <a href="#basiccode">Hello World</a>
934 pass in between the two passes:<p>
935
936 <pre>
937 $ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
938 Module Pass Manager
939   Function Pass Manager
940     Dominator Set Construction
941     Immediate Dominators Construction
942     Global Common Subexpression Elimination
943 <b>--  Dominator Set Construction</b>
944 --  Immediate Dominators Construction
945 --  Global Common Subexpression Elimination
946 <b>    Hello World Pass
947 --  Hello World Pass
948     Dominator Set Construction</b>
949     Natural Loop Construction
950     Loop Invariant Code Motion
951 --  Natural Loop Construction
952 --  Loop Invariant Code Motion
953     Module Verifier
954 --  Dominator Set Construction
955 --  Module Verifier
956   Bytecode Writer
957 --Bytecode Writer
958 Hello: __main
959 Hello: puts
960 Hello: main
961 </pre><p>
962
963 Here we see that the <a href="#basiccode">Hello World</a> pass has killed the
964 Dominator Set pass, even though it doesn't modify the code at all!  To fix this,
965 we need to add the following <a
966 href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method to our pass:<p>
967
968 <pre>
969     <i>// We don't modify the program, so we preserve all analyses</i>
970     <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
971       AU.setPreservesAll();
972     }
973 </pre><p>
974
975 Now when we run our pass, we get this output:<p>
976
977 <pre>
978 $ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
979 Pass Arguments:  -gcse -hello -licm
980 Module Pass Manager
981   Function Pass Manager
982     Dominator Set Construction
983     Immediate Dominators Construction
984     Global Common Subexpression Elimination
985 --  Immediate Dominators Construction
986 --  Global Common Subexpression Elimination
987     Hello World Pass
988 --  Hello World Pass
989     Natural Loop Construction
990     Loop Invariant Code Motion
991 --  Loop Invariant Code Motion
992 --  Natural Loop Construction
993     Module Verifier
994 --  Dominator Set Construction
995 --  Module Verifier
996   Bytecode Writer
997 --Bytecode Writer
998 Hello: __main
999 Hello: puts
1000 Hello: main
1001 </pre><p>
1002
1003 Which shows that we don't accidentally invalidate dominator information
1004 anymore, and therefore do not have to compute it twice.<p>
1005
1006
1007 <!-- _______________________________________________________________________ -->
1008 </ul><h4><a name="releaseMemory"><hr size=0>The <tt>releaseMemory</tt> method</h4><ul>
1009
1010 <pre>
1011   <b>virtual void</b> releaseMemory();
1012 </pre><p>
1013
1014 The <tt>PassManager</tt> automatically determines when to compute analysis
1015 results, and how long to keep them around for.  Because the lifetime of the pass
1016 object itself is effectively the entire duration of the compilation process, we
1017 need some way to free analysis results when they are no longer useful.  The
1018 <tt>releaseMemory</tt> virtual method is the way to do this.<p>
1019
1020 If you are writing an analysis or any other pass that retains a significant
1021 amount of state (for use by another pass which "requires" your pass and uses the
1022 <a href="#getAnalysis">getAnalysis</a> method) you should implement
1023 <tt>releaseMEmory</tt> to, well, release the memory allocated to maintain this
1024 internal state.  This method is called after the <tt>run*</tt> method for the
1025 class, before the next call of <tt>run*</tt> in your pass.<p>
1026
1027
1028 <!-- *********************************************************************** -->
1029 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
1030 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1031 <a name="future">Future extensions planned
1032 </b></font></td></tr></table><ul>
1033 <!-- *********************************************************************** -->
1034
1035 Although the LLVM Pass Infrastructure is very capable as it stands, and does
1036 some nifty stuff, there are things we'd like to add in the future.  Here is
1037 where we are going:<p>
1038
1039 <!-- _______________________________________________________________________ -->
1040 </ul><h4><a name="SMP"><hr size=0>Multithreaded LLVM</h4><ul>
1041
1042 Multiple CPU machines are becoming more command and compilation can never be
1043 fast enough: obviously we should allow for a multithreaded compiler.  Because of
1044 the semantics defined for passes above (specifically they cannot maintain state
1045 across invocations of their <tt>run*</tt> methods), a nice clean way to
1046 implement a multithreaded compiler would be for the <tt>PassManager</tt> class
1047 to create multiple instances of each pass object, and allow the seperate
1048 instances to be hacking on different parts of the program at the same time.<p>
1049
1050 This implementation would prevent each of the passes from having to implement
1051 multithreaded constructs, requiring only the LLVM core to have locking in a few
1052 places (for global resources).  Although this is a simple extension, we simply
1053 haven't had time (or multiprocessor machines, thus a reason) to implement this.
1054 Despite that, we have kept the LLVM passes SMP ready, and you should too.<p>
1055
1056
1057 <!-- _______________________________________________________________________ -->
1058 </ul><h4><a name="ModuleSource"><hr size=0>A new <tt>ModuleSource</tt> interface</h4><ul>
1059
1060 Currently, the <tt>PassManager</tt>'s <tt>run</tt> method takes a <tt><a
1061 href="http://llvm.cs.uiuc.edu/doxygen/classModule.html">Module</a></tt> as
1062 input, and runs all of the passes on this module.  The problem with this
1063 approach is that none of the <tt>PassManager</tt> features can be used for
1064 timing and debugging the actual <b>loading</b> of the module from disk or
1065 standard input.<p>
1066
1067 To solve this problem, eventually the <tt>PassManger</tt> class will accept a
1068 <tt>ModuleSource</tt> object instead of a Module itself.  When complete, this
1069 will also allow for streaming of functions out of the bytecode representation,
1070 allowing us to avoid holding the entire program in memory at once if we only are
1071 dealing with <a href="#FunctionPass">FunctionPass</a>'s.<p>
1072
1073 As part of a different issue, eventually the bytecode loader will be extended to
1074 allow on-demand loading of functions from the bytecode representation, in order
1075 to better support the runtime reoptimizer.  The bytecode format is already
1076 capable of this, the loader just needs to be reworked a bit.<p>
1077
1078
1079 <!-- _______________________________________________________________________ -->
1080 </ul><h4><a name="PassFunctionPass"><hr size=0><tt>Pass</tt>'s requiring <tt>FunctionPass</tt>'s</h4><ul>
1081
1082 Currently it is illegal for a <a href="#Pass"><tt>Pass</tt></a> to require a <a
1083 href="#FunctionPass"><tt>FunctionPass</tt></a>.  This is because there is only
1084 one instance of the <a href="#FunctionPass"><tt>FunctionPass</tt></a> object
1085 ever created, thus nowhere to store information for all of the functions in the
1086 program at the same time.  Although this has come up a couple of times before,
1087 this has always been worked around by factoring one big complicated pass into a
1088 global and an interprocedural part, both of which are distinct.  In the future,
1089 it would be nice to have this though.<p>
1090
1091 Note that it is no problem for a <a
1092 href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a
1093 href="#Pass"><tt>Pass</tt></a>, only the other way around.<p>
1094
1095
1096 <!-- *********************************************************************** -->
1097 </ul>
1098 <!-- *********************************************************************** -->
1099
1100 <hr><font size-1>
1101 <address><a href="mailto:sabre@nondot.org">Christopher Lattner</a></address>
1102 <!-- Created: Tue Aug  6 15:00:33 CDT 2002 -->
1103 <!-- hhmts start -->
1104 Last modified: Thu Aug 22 14:19:43 CDT 2002
1105 <!-- hhmts end -->
1106 </font></body></html>