blob: 3e2f167ccbb2b9f016fd9749fb9e0c0c3ba80dc6 [file] [log] [blame]
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html><head><title>Writing an LLVM Pass</title></head>
3
4<!--
5I. General Structure of an LLVM Program
6
7I.1 "What is a 'Value'?": Value & User class
8I.2 Type & Derived Types
9I.3 GlobalVariable, Function
10I.4 BasicBlock
11I.5 Instruction & Subclasses
121.6 Argument
131.7 Constants
14
15III. Useful things to know about the LLVM source base:
16
17III.1 Useful links that introduce the STL
18III.2 isa<>, cast<>, dyn_cast<>
19III.3 Makefiles, useful options
20III.4 How to use opt & analyze to debug stuff
21III.5 How to write a regression test
22III.6 DEBUG() and Statistics (-debug & -stats)
23III.7 The -time-passes option
24III.8 ... more as needed ...
25
26I think that writing Section #1 would be very helpful and that's the most
27stable portion of the sourcebase. #3 can be started on, but will probably
Chris Lattner79910702002-08-22 19:21:04 +000028just grow as time goes on.
Chris Lattnerc6bb8242002-08-08 20:11:18 +000029
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>
Chris Lattner79910702002-08-22 19:21:04 +000074 <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>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000079 <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>
Chris Lattner38c633d2002-08-08 20:23:41 +000089
90 <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000091</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
102The LLVM Pass Framework is an important part of the LLVM system, because LLVM
103passes are where the interesting parts of the compiler exist. Passes perform
104the transformations and optimizations that make up the compiler, they build
105the analysis results that are used by these transformations, and they are, above
106all, a structuring technique for compiler code.<p>
107
108All LLVM passes are subclasses of the <tt><a
109href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt> class, which
110implement functionality by overriding virtual methods inherited from
111<tt>Pass</tt>. Depending on how your pass works, you may be able to inherit
112from the <tt><a
113href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt>
114or <tt><a
115href="http://llvm.cs.uiuc.edu/doxygen/structBasicBlockPass.html">BasicBlockPass</a></tt>,
116which gives the system more information about what your pass does, and how it
117can be combined with other passes. One of the main features of the LLVM Pass
118Framework is that it schedules passes to run in an efficient way based on the
119constraints that your pass has.<p>
120
121We start by showing you how to construct a pass, everything from setting up the
122code, to compiling, loading, and executing it. After the basics are down, more
123advanced 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
133Here we describe how to write the "hello world" of passes. The "Hello" pass is
134designed to simply print out the name of non-external functions that exist in
135the program being compiled. It does not modify the program at all, just
136inspects it. The source code and files for this pass are available in the LLVM
137source 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
147First thing you need to do is create a new directory somewhere in the LLVM
148source 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
150script (Makefile) that will compile the source code for the new pass. To do
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000151this, copy this into "<tt>Makefile</tt>" (be very careful that there are no
152extra space characters at the end of the lines though... that seems to confuse
153<tt>gmake</tt>):<p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000154
155</ul><hr><ul><pre>
156# Makefile for hello pass
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000157
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000158# Path to top level of LLVM heirarchy
Chris Lattner7ce83e52002-08-14 20:07:01 +0000159LEVEL = ../../..
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000160
161# Name of the library to build
Chris Lattner7ce83e52002-08-14 20:07:01 +0000162LIBRARYNAME = hello
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000163
164# Build a dynamically loadable shared object
165SHARED_LIBRARY = 1
166
167# Include the makefile implementation stuff
168include $(LEVEL)/Makefile.common
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000169</pre></ul><hr><ul><p>
170
171This makefile specifies that all of the <tt>.cpp</tt> files in the current
172directory are to be compiled and linked together into a
173<tt>lib/Debug/libhello.so</tt> shared object that can be dynamically loaded by
174the <tt>opt</tt> or <tt>analyze</tt> tools.<p>
175
176Now that we have the build scripts set up, we just need to write the code for
177the 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
187Now that we have a way to compile our new pass, we just have to write it. Start
188out 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
195Which are needed because we are writing a <tt><a
196href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>, and we are
197operating on <tt><a
198href="http://llvm.cs.uiuc.edu/doxygen/classFunction.html">Function</a></tt>'s.<p>
199
200Next 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++
207what the "<tt>static</tt>" keyword is to C (at global scope). It makes the
208things declared inside of the anonymous namespace only visible to the current
209file. If you're not familiar with them, consult a decent C++ book for more
210information.<p>
211
212Next, 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
218This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a
219href="http://llvm.cs.uiuc.edu/doxygen/structFunctionPass.html">FunctionPass</a></tt>.
220The different builting pass subclasses are described in detail <a
221href="#passtype">later</a>, but for now, know that <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s
222operate 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
232We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method, which
233overloads an abstract virtual method inherited from <a
234href="#FunctionPass"><tt>FunctionPass</tt></a>. This is where we are supposed
235to do our thing, so we just print out our message with the name of each
236function.<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
243Lastly, 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
245different ways of <a href="#registration">registering your pass</a>, depending
246on what it is to be used for. For "optimizations" we use the
247<tt>RegisterOpt</tt> template.<p>
248
249As 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
267Now that it's all together, compile the file with a simple "<tt>gmake</tt>"
268command 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
270contained in an anonymous namespace: this reflects the fact that passes are self
271contained units that do not need external interfaces (although they can have
272them) 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
282Now 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
284registered your pass with the <tt>RegisterOpt</tt> template, you will be able to
285use the <tt>opt</tt> tool to access it, once loaded.<p>
286
287To test it, follow the example at the end of the <a
288href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to
289LLVM. We can now run the bytecode file (<tt>hello.bc</tt>) for the program
290through our transformation like this (or course, any bytecode file will
291work):<p>
292
293<pre>
294$ opt -load ../../../lib/Debug/libhello.so -hello &lt; hello.bc &gt; /dev/null
295Hello: __main
296Hello: puts
297Hello: main
298</pre><p>
299
300The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your pass
301as 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
303pass</a>). Because the hello pass does not modify the program in any
304interesting way, we just throw away the result of <tt>opt</tt> (sending it to
305<tt>/dev/null</tt>).<p>
306
307To 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
312OVERVIEW: llvm .bc -&gt; .bc modular optimizer
313
314USAGE: opt [options] &lt;input bytecode&gt;
315
316OPTIONS:
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
329The pass name get added as the information string for your pass, giving some
330documentation to users of <tt>opt</tt>. Now that you have a working pass, you
331would go ahead and make it do the cool transformations you want. Once you get
332it all working and tested, it may become useful to find out how fast your pass
333is. The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command
334line option (<tt>--time-passes</tt>) that allows you to get information about
335the execution time of your pass along with the other passes you queue up. For
336example:<p>
337
338<pre>
339$ opt -load ../../../lib/Debug/libhello.so -hello -time-passes &lt; hello.bc &gt; /dev/null
340Hello: __main
341Hello: puts
342Hello: 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
356As you can see, our implementation above is pretty fast :). The additional
357passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify
358that the LLVM emitted by your pass is still valid and well formed LLVM, which
359hasn't been broken somehow.
360
361Now that you have seen the basics of the mechanics behind passes, we can talk
362about 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
373One of the first things that you should do when designing a new pass is to
374decide what class you should subclass for your pass. The <a
375href="#basiccode">Hello World</a> example uses the <tt><a
376href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we
377did not discuss why or when this should occur. Here we talk about the classes
378available, from the most general to the most specific.<p>
379
Chris Lattner79910702002-08-22 19:21:04 +0000380When choosing a superclass for your Pass, you should choose the <b>most
381specific</b> class possible, while still being able to meet the requirements
382listed. This gives the LLVM Pass Infrastructure information neccesary to
383optimize how passes are run, so that the resultant compiler isn't unneccesarily
384slow.<p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000385
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
395The "<tt><a href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">Pass</a></tt>"
396class 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,
398refering to function bodies in no predictable order, or adding and removing
399functions. Because nothing is known about the behavior of direct <tt>Pass</tt>
400subclasses, no optimization can be done for their execution.<p>
401
402To write a correct <tt>Pass</tt> subclass, derive from <tt>Pass</tt> and
403overload 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
413The <tt>run</tt> method performs the interesting work of the pass, and should
414return true if the module was modified by the transformation, false
415otherwise.<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
426In contrast to direct <tt>Pass</tt> subclasses, direct <tt><a
427href="http://llvm.cs.uiuc.edu/doxygen/classPass.html">FunctionPass</a></tt>
428subclasses do have a predictable, local behavior that can be expected by the
429system. All <tt>FunctionPass</tt> execute on each function in the program
430independant of all of the other functions in the program.
431<tt>FunctionPass</tt>'s do not require that they are executed in a particular
432order, and <tt>FunctionPass</tt>'s do not modify external functions.<p>
433
434To 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
444Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a
445href="#basiccode">Hello World</a> pass for example). <tt>FunctionPass</tt>'s
446may overload three virtual methods to do their work. All of these methods
447should 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>
451method</h4><ul>
452
453<pre>
454 <b>virtual bool</b> doInitialization(Module &amp;M);
455</pre><p>
456
457The <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
459functions, get pointers to functions, etc. The <tt>doInitialize</tt> method is
460designed to do simple initialization type of stuff that does not depend on the
461functions being processed. The <tt>doInitialization</tt> function call is not
462scheduled to overlap with any other pass executions.<p>
463
464A good example of how this method should be used is the <a
465href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a>
466pass. This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into
467platform dependant <tt>malloc()</tt> and <tt>free()</tt> function calls. It
468uses the <tt>doInitialization</tt> method to get a reference to the malloc and
469free 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
478The <tt>runOnFunction</tt> method must be implemented by your subclass to do the
479transformation or analysis work of your pass. As usual, a true value should be
480returned 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
489The <tt>doFinalization</tt> method is an infrequently used method that is called
490when the pass framework has finished calling <a
491href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
492program 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
504href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
505their scope of inspection and modification to a single basic block at a time.
506As 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"
518optimizations. They may override the same <a
519href="#doInitialization"><tt>doInitialization</tt></a> and <a
520href="#doFinalization"><tt>doFinalization</tt></a> methods that <a
521href="#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
531Override this function to do the work of the <tt>BasicBlockPass</tt>. This
532function is not allowed to inspect or modify basic blocks other than the
533parameter, and are not allowed to modify the CFG. A true value must be returned
534if 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
544In the <a href="#basiccode">Hello World</a> example pass we illustrated how pass
545registration works, and discussed some of the reasons that it is used and what
546it does. Here we discuss how and why passes are registered.<p>
547
548Passes can be registered in several different ways. Depending on the general
549classification of the pass, you should use one of the following templates to
550register the pass:<p>
551
552<ul>
553<li><b><tt>RegisterOpt</tt></b> - This template should be used when you are
554registering 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
558registering 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
562registering 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
567multiple or no utilities. This template takes an extra third argument that
568specifies which tools it should be listed in. See the <a
569href="http://llvm.cs.uiuc.edu/doxygen/PassSupport_8h-source.html">PassSupport.h</a>
570file for more information.<p>
571</ul><p>
572
573Regardless of how you register your pass, you must specify at least two
574parameters. The first parameter is the name of the pass that is to be used on
575the command line to specify that the pass should be added to a program (for
576example <tt>opt</tt> or <tt>analyze</tt>). The second argument is the name of
577the pass, which is to be used for the <tt>--help</tt> output of programs, as
578well as for debug output generated by the <tt>--debug-pass</tt> option.<p>
579
580If you pass is constructed by its default constructor, you only ever have to
581pass these two arguments. If, on the other hand, you require other information
582(like target specific information), you must pass an additional argument. This
583argument is a pointer to a function used to create the pass. For an example of
584how this works, look at the <a
585href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations.cpp</a>
586file.<p>
587
588If a pass is registered to be used by the <tt>analyze</tt> utility, you should
589implement 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
598The <tt>print</tt> method must be implemented by "analyses" in order to print a
599human readable version of the analysis results. This is useful for debugging an
600analysis itself, as well as for other people to figure out how an analysis
601works. The <tt>analyze</tt> tool uses this method to generate its output.<p>
602
603The <tt>ostream</tt> parameter specifies the stream to write the results on, and
604the <tt>Module</tt> parameter gives a pointer to the top level module of the
605program that has been analyzed. Note however that this pointer may be null in
606certain circumstances (such as calling the <tt>Pass::dump()</tt> from a
607debugger), so it should only be used to enhance debug output, it should not be
608depended 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
618One of the main responsibilities of the <tt>PassManager</tt> is the make sure
619that passes interact with each other correctly. Because <tt>PassManager</tt>
620tries to <a href="#passmanager">optimize the execution of passes</a> it must
621know how the passes interact with each other and what dependencies exist between
622the various passes. To track this, each pass can declare the set of passes that
623are required to be executed before the current pass, and the passes which are
624invalidated by the current pass.<p>
625
626Typically this functionality is used to require that analysis results are
627computed before your pass is run. Running arbitrary transformation passes can
628invalidate the computed analysis results, which is what the invalidation set
629specifies. If a pass does not implement the <tt><a
630href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
631having 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
641By implementing the <tt>getAnalysisUsage</tt> method, the required and
642invalidated sets may be specified for your transformation. The implementation
643should fill in the <tt><a
644href="http://llvm.cs.uiuc.edu/doxygen/classAnalysisUsage.html">AnalysisUsage</a></tt>
645object with information about which passes are required and not invalidated. To do this, the following set methods are provided by the <tt><a
646href="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
671Some 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
682and:<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
695The <tt>Pass::getAnalysis&lt;&gt;</tt> method is inherited by your class,
696providing you with access to the passes that you declared that you required with
697the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method. It takes
698a single template argument that specifies which pass class you want, and returns
699a 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
706This method call returns a reference to the pass desired. You may get a runtime
707assertion failure if you attempt to get an analysis that you did not declare as
708required in your <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a>
709implementation. This method can be called by your <tt>run*</tt> method
710implementation, or by any other local method invoked by your <tt>run*</tt>
711method.<p>
712
Chris Lattner79910702002-08-22 19:21:04 +0000713<!-- *********************************************************************** -->
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
720Now that we understand the basics of how passes are defined, how the are used,
721and how they are required from other passes, it's time to get a little bit
722fancier. All of the pass relationships that we have seen so far are very
723simple: one pass depends on one other specific pass to be run before it can run.
724For many applications, this is great, for others, more flexibility is
725required.<p>
726
727In particular, some analyses are defined such that there is a single simple
728interface to the analysis results, but multiple ways of calculating them.
729Consider alias analysis for example. The most trivial alias analysis returns
730"may alias" for any alias query. The most sophisticated analysis a
731flow-sensitive, context-sensitive interprocedural analysis that can take a
732significant amount of time to execute (and obviously, there is a lot of room
733between these two extremes for other implementations). To cleanly support
734situations like this, the LLVM Pass Infrastructure supports the notion of
735Analysis Groups.<p>
736
737<!-- _______________________________________________________________________ -->
738</ul><h4><a name="agconcepts"><hr size=0>Analysis Group Concepts</h4><ul>
739
740An Analysis Group is a single simple interface that may be implemented by
741multiple different passes. Analysis Groups can be given human readable names
742just like passes, but unlike passes, they need not derive from the <tt>Pass</tt>
743class. An analysis group may have one or more implementations, one of which is
744the "default" implementation.<p>
745
746Analysis groups are used by client passes just like other passes are: the
747<tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods.
748In order to resolve this requirement, the <a href="#passmanager">PassManager</a>
749scans the available passes to see if any implementations of the analysis group
750are available. If none is available, the default implementation is created for
751the pass to use. All standard rules for <A href="#interaction">interaction
752between passes</a> still apply.<p>
753
754Although <a href="#registration">Pass Registration</a> is optional for normal
755passes, all analysis group implementations must be registered, and must use the
756<A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the
757implementation pool. Also, a default implementation of the interface
758<b>must</b> be registered with <A
759href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.<p>
760
761As a concrete example of an Analysis Group in action, consider the <a
762href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a>
763analysis group. The default implementation of the alias analysis interface (the
764<tt><a
765href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt>
766pass) just does a few simple checks that don't require significant analysis to
767compute (such as: two different globals can never alias each other, etc).
768Passes that use the <tt><a
769href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt>
770interface (for example the <tt><a
771href="http://llvm.cs.uiuc.edu/doxygen/classGCSE.html">gcse</a></tt> pass), do not care which implementation
772of alias analysis is actually provided, they just use the designated
773interface.<p>
774
775From the user's perspective, commands work just like normal. Issuing the
776command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be
777instantiated 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
780hypothetical example) instead.<p>
781
782
783<!-- _______________________________________________________________________ -->
784</ul><h4><a name="registerag"><hr size=0>Using <tt>RegisterAnalysisGroup</tt></h4><ul>
785
786The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
787group itself as well as add pass implementations to the analysis group. First,
788an analysis should be registered, with a human readable name provided for it.
789Unlike registration of passes, there is no command line argument to be specified
790for 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
796Once the analysis is registered, passes can declare that they are valid
797implementations 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
810This just shows a class <tt>FancyAA</tt> that is registered normally, then uses
811the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a
812href="http://llvm.cs.uiuc.edu/doxygen/structAliasAnalysis.html">AliasAnalysis</a></tt>
813analysis group. Every implementation of an analysis group should join using
814this template. A single pass may join multiple different analysis groups with
815no 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
828Here we show how the default implementation is specified (using the extra
829argument to the <tt>RegisterAnalysisGroup</tt> template). There must be exactly
830one default implementation available at all times for an Analysis Group to be
831used. Here we declare that the <tt><a
832href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt>
833pass is the default implementation for the interface.<p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000834
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
843The <a
844href="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
846a list of passes, ensures their <a href="#interaction">prerequisites</a> are set
847up correctly, and then schedules passes to run efficiently. All of the LLVM
848tools that run passes use the <tt>PassManager</tt> for execution of these
849passes.<p>
850
851The <tt>PassManager</tt> does two main things to try to reduce the execution
852time of a series of passes:<p>
853
854<ol>
855<li><b>Share analysis results</b> - The PassManager attempts to avoid
856recomputing analysis results as much as possible. This means keeping track of
857which analyses are available already, which analyses get invalidated, and which
858analyses 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
860it to <a href="#releaseMemory">free memory</a> allocated to holding analysis
861results 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
865of a series of passes by pipelining the passes together. This means that, given
866a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it
867will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on
868the first function, then all of the <a
869href="#FunctionPass"><tt>FunctionPass</tt></a>'s on the second function,
870etc... until the entire program has been run through the passes.<p>
871
872This improves the cache behavior of the compiler, because it is only touching
873the LLVM program representation for a single function at a time, instead of
874traversing the entire program. It reduces the memory consumption of compiler,
875because, for example, only one <a
876href="http://llvm.cs.uiuc.edu/doxygen/structDominatorSet.html"><tt>DominatorSet</tt></a>
877needs to be calculated at a time. This also makes it possible some <a
878href="#SMP">interesting enhancements</a> in the future.<p>
879
880</ol><p>
881
882The effectiveness of the <tt>PassManager</tt> is influenced directly by how much
883information it has about the behaviors of the passes it is scheduling. For
884example, the "preserved" set is intentionally conservative in the face of an
885unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.
886Not implementing when it should be implemented will have the effect of not
887allowing any analysis results to live across the execution of your pass.<p>
888
889The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line
890options that is useful for debugging pass execution, seeing how things work, and
891diagnosing 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>
893option, just type '<tt>opt --help-hidden</tt>').<p>
894
895By using the <tt>--debug-pass=Structure</tt> option, for example, we can see how
896our <a href="#basiccode">Hello World</a> pass interacts with other passes. Lets
897try 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
901Module 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
919This output shows us when passes are constructed and when the analysis results
920are known to be dead (prefixed with '<tt>--</tt>'). Here we see that GCSE uses
921dominator and immediate dominator information to do its job. The LICM pass uses
922natural loop information, which uses dominator sets, but not immediate
923dominators. Because immediate dominators are no longer useful after the GCSE
924pass, it is immediately destroyed. The dominator sets are then reused to
925compute natural loop information, which is then used by the LICM pass.<p>
926
927After the LICM pass, the module verifier runs (which is automatically added by
928the '<tt>opt</tt>' tool), which uses the dominator set to check that the
929resultant LLVM code is well formed. After it finishes, the dominator set
930information is destroyed, after being computed once, and shared by three
931passes.<p>
932
933Lets see how this changes when we run the <a href="#basiccode">Hello World</a>
934pass 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
938Module 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
958Hello: __main
959Hello: puts
960Hello: main
961</pre><p>
962
963Here we see that the <a href="#basiccode">Hello World</a> pass has killed the
964Dominator Set pass, even though it doesn't modify the code at all! To fix this,
965we need to add the following <a
966href="#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
975Now 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
979Pass Arguments: -gcse -hello -licm
980Module 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
998Hello: __main
999Hello: puts
1000Hello: main
1001</pre><p>
1002
1003Which shows that we don't accidentally invalidate dominator information
1004anymore, 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
1014The <tt>PassManager</tt> automatically determines when to compute analysis
1015results, and how long to keep them around for. Because the lifetime of the pass
1016object itself is effectively the entire duration of the compilation process, we
1017need 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
1020If you are writing an analysis or any other pass that retains a significant
1021amount 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
1024internal state. This method is called after the <tt>run*</tt> method for the
1025class, 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
1035Although the LLVM Pass Infrastructure is very capable as it stands, and does
1036some nifty stuff, there are things we'd like to add in the future. Here is
1037where we are going:<p>
1038
1039<!-- _______________________________________________________________________ -->
1040</ul><h4><a name="SMP"><hr size=0>Multithreaded LLVM</h4><ul>
1041
1042Multiple CPU machines are becoming more command and compilation can never be
1043fast enough: obviously we should allow for a multithreaded compiler. Because of
1044the semantics defined for passes above (specifically they cannot maintain state
1045across invocations of their <tt>run*</tt> methods), a nice clean way to
1046implement a multithreaded compiler would be for the <tt>PassManager</tt> class
1047to create multiple instances of each pass object, and allow the seperate
1048instances to be hacking on different parts of the program at the same time.<p>
1049
1050This implementation would prevent each of the passes from having to implement
1051multithreaded constructs, requiring only the LLVM core to have locking in a few
1052places (for global resources). Although this is a simple extension, we simply
1053haven't had time (or multiprocessor machines, thus a reason) to implement this.
1054Despite 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
1060Currently, the <tt>PassManager</tt>'s <tt>run</tt> method takes a <tt><a
1061href="http://llvm.cs.uiuc.edu/doxygen/classModule.html">Module</a></tt> as
1062input, and runs all of the passes on this module. The problem with this
1063approach is that none of the <tt>PassManager</tt> features can be used for
1064timing and debugging the actual <b>loading</b> of the module from disk or
1065standard input.<p>
1066
1067To 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
1069will also allow for streaming of functions out of the bytecode representation,
1070allowing us to avoid holding the entire program in memory at once if we only are
1071dealing with <a href="#FunctionPass">FunctionPass</a>'s.<p>
1072
1073As part of a different issue, eventually the bytecode loader will be extended to
1074allow on-demand loading of functions from the bytecode representation, in order
1075to better support the runtime reoptimizer. The bytecode format is already
1076capable 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
1082Currently it is illegal for a <a href="#Pass"><tt>Pass</tt></a> to require a <a
1083href="#FunctionPass"><tt>FunctionPass</tt></a>. This is because there is only
1084one instance of the <a href="#FunctionPass"><tt>FunctionPass</tt></a> object
1085ever created, thus nowhere to store information for all of the functions in the
1086program at the same time. Although this has come up a couple of times before,
1087this has always been worked around by factoring one big complicated pass into a
1088global and an interprocedural part, both of which are distinct. In the future,
1089it would be nice to have this though.<p>
1090
1091Note that it is no problem for a <a
1092href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a
1093href="#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 -->
Chris Lattner79910702002-08-22 19:21:04 +00001104Last modified: Thu Aug 22 14:19:43 CDT 2002
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001105<!-- hhmts end -->
1106</font></body></html>