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