blob: 239dd67ea7aa8628bd9c27b5082c096b49629901 [file] [log] [blame]
Misha Brukmanc5402402004-01-15 18:34:11 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Reid Spencer7fa6d522005-01-11 05:16:23 +00005 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Misha Brukmanc5402402004-01-15 18:34:11 +00006 <title>Writing an LLVM Pass</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
8</head>
9<body>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000010
Misha Brukmanc5402402004-01-15 18:34:11 +000011<div class="doc_title">
12 Writing an LLVM Pass
13</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000014
15<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +000016 <li><a href="#introduction">Introduction - What is a pass?</a></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000017 <li><a href="#quickstart">Quick Start - Writing hello world</a>
18 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000019 <li><a href="#makefile">Setting up the build environment</a></li>
20 <li><a href="#basiccode">Basic code required</a></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000021 <li><a href="#running">Running a pass with <tt>opt</tt>
Chris Lattner8fdb2462004-09-18 06:39:35 +000022 or <tt>analyze</tt></a></li>
Misha Brukmanc5402402004-01-15 18:34:11 +000023 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000024 <li><a href="#passtype">Pass classes and requirements</a>
25 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000026 <li><a href="#ImmutablePass">The <tt>ImmutablePass</tt> class</a></li>
Chris Lattnerf6278922004-09-20 04:36:29 +000027 <li><a href="#ModulePass">The <tt>ModulePass</tt> class</a>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000028 <ul>
Chris Lattnerf6278922004-09-20 04:36:29 +000029 <li><a href="#runOnModule">The <tt>runOnModule</tt> method</a></li>
Misha Brukmanc5402402004-01-15 18:34:11 +000030 </ul></li>
Chris Lattner8fdb2462004-09-18 06:39:35 +000031 <li><a href="#CallGraphSCCPass">The <tt>CallGraphSCCPass</tt> class</a>
32 <ul>
33 <li><a href="#doInitialization_scc">The <tt>doInitialization(Module
34 &amp;)</tt> method</a></li>
35 <li><a href="#runOnSCC">The <tt>runOnSCC</tt> method</a></li>
36 <li><a href="#doFinalization_scc">The <tt>doFinalization(Module
37 &amp;)</tt> method</a></li>
38 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000039 <li><a href="#FunctionPass">The <tt>FunctionPass</tt> class</a>
40 <ul>
Chris Lattnerd0713f92002-09-12 17:06:43 +000041 <li><a href="#doInitialization_mod">The <tt>doInitialization(Module
Misha Brukmanc5402402004-01-15 18:34:11 +000042 &amp;)</tt> method</a></li>
43 <li><a href="#runOnFunction">The <tt>runOnFunction</tt> method</a></li>
Chris Lattnerd0713f92002-09-12 17:06:43 +000044 <li><a href="#doFinalization_mod">The <tt>doFinalization(Module
Misha Brukmanc5402402004-01-15 18:34:11 +000045 &amp;)</tt> method</a></li>
46 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000047 <li><a href="#BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
48 <ul>
Chris Lattnerd0713f92002-09-12 17:06:43 +000049 <li><a href="#doInitialization_fn">The <tt>doInitialization(Function
Misha Brukmanc5402402004-01-15 18:34:11 +000050 &amp;)</tt> method</a></li>
51 <li><a href="#runOnBasicBlock">The <tt>runOnBasicBlock</tt>
52 method</a></li>
Chris Lattnerd0713f92002-09-12 17:06:43 +000053 <li><a href="#doFinalization_fn">The <tt>doFinalization(Function
Misha Brukmanc5402402004-01-15 18:34:11 +000054 &amp;)</tt> method</a></li>
55 </ul></li>
Brian Gaekecab8b6f2003-07-17 18:53:20 +000056 <li><a href="#MachineFunctionPass">The <tt>MachineFunctionPass</tt>
57 class</a>
Brian Gaeke6a33f362003-07-22 20:53:20 +000058 <ul>
59 <li><a href="#runOnMachineFunction">The
Misha Brukmanc5402402004-01-15 18:34:11 +000060 <tt>runOnMachineFunction(MachineFunction &amp;)</tt> method</a></li>
61 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000062 </ul>
63 <li><a href="#registration">Pass Registration</a>
64 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000065 <li><a href="#print">The <tt>print</tt> method</a></li>
66 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000067 <li><a href="#interaction">Specifying interactions between passes</a>
68 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000069 <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt>
70 method</a></li>
Chris Lattner89256272004-03-17 21:09:55 +000071 <li><a href="#AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a></li>
72 <li><a href="#AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a></li>
73 <li><a href="#AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a></li>
74 <li><a href="#getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> and <tt>getAnalysisToUpdate&lt;&gt;</tt> methods</a></li>
Misha Brukmanc5402402004-01-15 18:34:11 +000075 </ul></li>
Chris Lattner79910702002-08-22 19:21:04 +000076 <li><a href="#analysisgroup">Implementing Analysis Groups</a>
77 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000078 <li><a href="#agconcepts">Analysis Group Concepts</a></li>
79 <li><a href="#registerag">Using <tt>RegisterAnalysisGroup</tt></a></li>
80 </ul></li>
Tanya Lattnerd5383652004-11-19 01:25:14 +000081 <li><a href="#passStatistics">Pass Statistics</a>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000082 <li><a href="#passmanager">What PassManager does</a>
83 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000084 <li><a href="#releaseMemory">The <tt>releaseMemory</tt> method</a></li>
85 </ul></li>
Chris Lattner480e2ef2002-09-06 02:02:58 +000086 <li><a href="#debughints">Using GDB with dynamically loaded passes</a>
87 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000088 <li><a href="#breakpoint">Setting a breakpoint in your pass</a></li>
89 <li><a href="#debugmisc">Miscellaneous Problems</a></li>
90 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000091 <li><a href="#future">Future extensions planned</a>
92 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000093 <li><a href="#SMP">Multithreaded LLVM</a></li>
Chris Lattnerf6278922004-09-20 04:36:29 +000094 <li><a href="#PassFunctionPass"><tt>ModulePass</tt>es requiring
Misha Brukmanc5402402004-01-15 18:34:11 +000095 <tt>FunctionPass</tt>es</a></li>
96 </ul></li>
97</ol>
Chris Lattner38c633d2002-08-08 20:23:41 +000098
Chris Lattner7911ce22004-05-23 21:07:27 +000099<div class="doc_author">
100 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
Misha Brukmanc5402402004-01-15 18:34:11 +0000101</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000102
103<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000104<div class="doc_section">
105 <a name="introduction">Introduction - What is a pass?</a>
106</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000107<!-- *********************************************************************** -->
108
Misha Brukmanc5402402004-01-15 18:34:11 +0000109<div class="doc_text">
110
111<p>The LLVM Pass Framework is an important part of the LLVM system, because LLVM
Chris Lattner8fdb2462004-09-18 06:39:35 +0000112passes are where most of the interesting parts of the compiler exist. Passes
113perform the transformations and optimizations that make up the compiler, they
114build the analysis results that are used by these transformations, and they are,
115above all, a structuring technique for compiler code.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000116
Misha Brukmanc5402402004-01-15 18:34:11 +0000117<p>All LLVM passes are subclasses of the <tt><a
118href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Pass.html">Pass</a></tt>
119class, which implement functionality by overriding virtual methods inherited
Chris Lattnerf6278922004-09-20 04:36:29 +0000120from <tt>Pass</tt>. Depending on how your pass works, you should inherit from
121the <tt><a href="#ModulePass">ModulePass</a></tt>, <tt><a
122href="#CallGraphSCCPass">CallGraphSCCPass</a></tt>, <tt><a
123href="#FunctionPass">FunctionPass</a></tt>, or <tt><a
Chris Lattner8fdb2462004-09-18 06:39:35 +0000124href="#BasicBlockPass">BasicBlockPass</a></tt> classes, which gives the system
125more information about what your pass does, and how it can be combined with
126other passes. One of the main features of the LLVM Pass Framework is that it
127schedules passes to run in an efficient way based on the constraints that your
128pass meets (which are indicated by which class they derive from).</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000129
Misha Brukmanc5402402004-01-15 18:34:11 +0000130<p>We start by showing you how to construct a pass, everything from setting up
131the code, to compiling, loading, and executing it. After the basics are down,
132more advanced features are discussed.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000133
Misha Brukmanc5402402004-01-15 18:34:11 +0000134</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000135
136<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000137<div class="doc_section">
138 <a name="quickstart">Quick Start - Writing hello world</a>
139</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000140<!-- *********************************************************************** -->
141
Misha Brukmanc5402402004-01-15 18:34:11 +0000142<div class="doc_text">
143
144<p>Here we describe how to write the "hello world" of passes. The "Hello" pass
145is designed to simply print out the name of non-external functions that exist in
Chris Lattner8fdb2462004-09-18 06:39:35 +0000146the program being compiled. It does not modify the program at all, it just
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000147inspects it. The source code and files for this pass are available in the LLVM
Misha Brukmanc5402402004-01-15 18:34:11 +0000148source tree in the <tt>lib/Transforms/Hello</tt> directory.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000149
Misha Brukmanc5402402004-01-15 18:34:11 +0000150</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000151
152<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000153<div class="doc_subsection">
154 <a name="makefile">Setting up the build environment</a>
155</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000156
Misha Brukmanc5402402004-01-15 18:34:11 +0000157<div class="doc_text">
158
Reid Spencer1bc19342004-12-11 05:12:57 +0000159 <p>First, you need to create a new directory somewhere in the LLVM source
160 base. For this example, we'll assume that you made
161 <tt>lib/Transforms/Hello</tt>. Next, you must set up a build script
162 (Makefile) that will compile the source code for the new pass. To do this,
163 copy the following into <tt>Makefile</tt>:</p>
164 <hr/>
Misha Brukmanc5402402004-01-15 18:34:11 +0000165
166<pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000167# Makefile for hello pass
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000168
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000169# Path to top level of LLVM heirarchy
Chris Lattner7ce83e52002-08-14 20:07:01 +0000170LEVEL = ../../..
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000171
172# Name of the library to build
Reid Spencer7fa6d522005-01-11 05:16:23 +0000173LIBRARYNAME = Hello
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000174
Reid Spencer7fa6d522005-01-11 05:16:23 +0000175# Build a dynamically linkable shared object
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000176SHARED_LIBRARY = 1
177
Reid Spencer7fa6d522005-01-11 05:16:23 +0000178# Make the shared library become a loadable module so the tools can
179# dlopen/dlsym on the resulting library.
180LOADABLE_MODULE
181
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000182# Include the makefile implementation stuff
183include $(LEVEL)/Makefile.common
Misha Brukmanc5402402004-01-15 18:34:11 +0000184</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000185
Misha Brukmanc5402402004-01-15 18:34:11 +0000186<p>This makefile specifies that all of the <tt>.cpp</tt> files in the current
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000187directory are to be compiled and linked together into a
Reid Spencer7fa6d522005-01-11 05:16:23 +0000188<tt>Debug/lib/Hello.so</tt> shared object that can be dynamically loaded by
189the <tt>opt</tt> or <tt>analyze</tt> tools via their <tt>-load</tt> options.
190If your operating system uses a suffix other than .so (such as windows or
191Mac OS/X), the appropriate extension will be used.</p>
John Criswellf9c78652004-01-26 21:26:54 +0000192
Misha Brukmanc5402402004-01-15 18:34:11 +0000193<p>Now that we have the build scripts set up, we just need to write the code for
194the pass itself.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000195
Misha Brukmanc5402402004-01-15 18:34:11 +0000196</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000197
198<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000199<div class="doc_subsection">
200 <a name="basiccode">Basic code required</a>
201</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000202
Misha Brukmanc5402402004-01-15 18:34:11 +0000203<div class="doc_text">
204
205<p>Now that we have a way to compile our new pass, we just have to write it.
206Start out with:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000207
208<pre>
209<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
210<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
211</pre>
212
Misha Brukmanc5402402004-01-15 18:34:11 +0000213<p>Which are needed because we are writing a <tt><a
214href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Pass.html">Pass</a></tt>, and
215we are operating on <tt><a
216href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Function.html">Function</a></tt>'s.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000217
Misha Brukmanc5402402004-01-15 18:34:11 +0000218<p>Next we have:</p>
Jonathan Manton65acb302004-06-30 18:10:30 +0000219<pre>
220<b>using namespace llvm;</b>
221</pre>
222<p>... which is required because the functions from the include files
223live in the llvm namespace.
224</p>
225
226<p>Next we have:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000227
228<pre>
229<b>namespace</b> {
Misha Brukmanc5402402004-01-15 18:34:11 +0000230</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000231
Misha Brukmanc5402402004-01-15 18:34:11 +0000232<p>... which starts out an anonymous namespace. Anonymous namespaces are to C++
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000233what the "<tt>static</tt>" keyword is to C (at global scope). It makes the
234things declared inside of the anonymous namespace only visible to the current
235file. If you're not familiar with them, consult a decent C++ book for more
Misha Brukmanc5402402004-01-15 18:34:11 +0000236information.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000237
Misha Brukmanc5402402004-01-15 18:34:11 +0000238<p>Next, we declare our pass itself:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000239
240<pre>
241 <b>struct</b> Hello : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
242</pre><p>
243
Misha Brukmanc5402402004-01-15 18:34:11 +0000244<p>This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a
245href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1FunctionPass.html">FunctionPass</a></tt>.
Chris Lattnerd6ea9262002-09-09 03:48:46 +0000246The different builtin pass subclasses are described in detail <a
Misha Brukmanc5402402004-01-15 18:34:11 +0000247href="#passtype">later</a>, but for now, know that <a
248href="#FunctionPass"><tt>FunctionPass</tt></a>'s operate a function at a
249time.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000250
251<pre>
Misha Brukmanc5402402004-01-15 18:34:11 +0000252 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000253 std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
254 <b>return false</b>;
255 }
256 }; <i>// end of struct Hello</i>
257</pre>
258
Misha Brukmanc5402402004-01-15 18:34:11 +0000259<p>We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method,
260which overloads an abstract virtual method inherited from <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000261href="#FunctionPass"><tt>FunctionPass</tt></a>. This is where we are supposed
262to do our thing, so we just print out our message with the name of each
Misha Brukmanc5402402004-01-15 18:34:11 +0000263function.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000264
265<pre>
266 RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
267} <i>// end of anonymous namespace</i>
Misha Brukmanc5402402004-01-15 18:34:11 +0000268</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000269
Misha Brukmanc5402402004-01-15 18:34:11 +0000270<p>Lastly, we register our class <tt>Hello</tt>, giving it a command line
271argument "<tt>hello</tt>", and a name "<tt>Hello World Pass</tt>". There are
272several different ways of <a href="#registration">registering your pass</a>,
273depending on what it is to be used for. For "optimizations" we use the
274<tt>RegisterOpt</tt> template.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000275
Misha Brukmanc5402402004-01-15 18:34:11 +0000276<p>As a whole, the <tt>.cpp</tt> file looks like:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000277
278<pre>
279<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
280<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
281
Jonathan Manton65acb302004-06-30 18:10:30 +0000282<b>using namespace llvm;</b>
283
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000284<b>namespace</b> {
285 <b>struct Hello</b> : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
Misha Brukmanc5402402004-01-15 18:34:11 +0000286 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000287 std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
288 <b>return false</b>;
289 }
290 };
291
292 RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
293}
Misha Brukmanc5402402004-01-15 18:34:11 +0000294</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000295
Misha Brukmanc5402402004-01-15 18:34:11 +0000296<p>Now that it's all together, compile the file with a simple "<tt>gmake</tt>"
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000297command in the local directory and you should get a new
Reid Spencer7fa6d522005-01-11 05:16:23 +0000298"<tt>Debug/lib/Hello.so</tt> file. Note that everything in this file is
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000299contained in an anonymous namespace: this reflects the fact that passes are self
300contained units that do not need external interfaces (although they can have
Misha Brukmanc5402402004-01-15 18:34:11 +0000301them) to be useful.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000302
Misha Brukmanc5402402004-01-15 18:34:11 +0000303</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000304
305<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000306<div class="doc_subsection">
307 <a name="running">Running a pass with <tt>opt</tt> or <tt>analyze</tt></a>
308</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000309
Misha Brukmanc5402402004-01-15 18:34:11 +0000310<div class="doc_text">
311
John Criswellf9c78652004-01-26 21:26:54 +0000312<p>Now that you have a brand new shiny shared object file, we can use the
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000313<tt>opt</tt> command to run an LLVM program through your pass. Because you
314registered your pass with the <tt>RegisterOpt</tt> template, you will be able to
Misha Brukmanc5402402004-01-15 18:34:11 +0000315use the <tt>opt</tt> tool to access it, once loaded.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000316
Misha Brukmanc5402402004-01-15 18:34:11 +0000317<p>To test it, follow the example at the end of the <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000318href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to
319LLVM. We can now run the bytecode file (<tt>hello.bc</tt>) for the program
320through our transformation like this (or course, any bytecode file will
Misha Brukmanc5402402004-01-15 18:34:11 +0000321work):</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000322
323<pre>
Reid Spencer7fa6d522005-01-11 05:16:23 +0000324$ opt -load ../../../Debug/lib/Hello.so -hello &lt; hello.bc &gt; /dev/null
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000325Hello: __main
326Hello: puts
327Hello: main
Misha Brukmanc5402402004-01-15 18:34:11 +0000328</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000329
Misha Brukmanc5402402004-01-15 18:34:11 +0000330<p>The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your
331pass as a shared object, which makes '<tt>-hello</tt>' a valid command line
332argument (which is one reason you need to <a href="#registration">register your
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000333pass</a>). Because the hello pass does not modify the program in any
334interesting way, we just throw away the result of <tt>opt</tt> (sending it to
Misha Brukmanc5402402004-01-15 18:34:11 +0000335<tt>/dev/null</tt>).</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000336
Misha Brukmanc5402402004-01-15 18:34:11 +0000337<p>To see what happened to the other string you registered, try running
338<tt>opt</tt> with the <tt>--help</tt> option:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000339
340<pre>
Reid Spencer7fa6d522005-01-11 05:16:23 +0000341$ opt -load ../../../Debug/lib/Hello.so --help
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000342OVERVIEW: llvm .bc -&gt; .bc modular optimizer
343
344USAGE: opt [options] &lt;input bytecode&gt;
345
346OPTIONS:
347 Optimizations available:
348...
349 -funcresolve - Resolve Functions
350 -gcse - Global Common Subexpression Elimination
351 -globaldce - Dead Global Elimination
352 <b>-hello - Hello World Pass</b>
Chris Lattner065a6162003-09-10 05:29:43 +0000353 -indvars - Canonicalize Induction Variables
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000354 -inline - Function Integration/Inlining
355 -instcombine - Combine redundant instructions
356...
Misha Brukmanc5402402004-01-15 18:34:11 +0000357</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000358
Misha Brukmanc5402402004-01-15 18:34:11 +0000359<p>The pass name get added as the information string for your pass, giving some
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000360documentation to users of <tt>opt</tt>. Now that you have a working pass, you
361would go ahead and make it do the cool transformations you want. Once you get
362it all working and tested, it may become useful to find out how fast your pass
363is. The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command
364line option (<tt>--time-passes</tt>) that allows you to get information about
365the execution time of your pass along with the other passes you queue up. For
Misha Brukmanc5402402004-01-15 18:34:11 +0000366example:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000367
368<pre>
Reid Spencer7fa6d522005-01-11 05:16:23 +0000369$ opt -load ../../../Debug/lib/Hello.so -hello -time-passes &lt; hello.bc &gt; /dev/null
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000370Hello: __main
371Hello: puts
372Hello: main
373===============================================================================
374 ... Pass execution timing report ...
375===============================================================================
376 Total Execution Time: 0.02 seconds (0.0479059 wall clock)
377
378 ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name ---
379 0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bytecode Writer
380 0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction
381 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier
382 <b> 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass</b>
383 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL
Misha Brukmanc5402402004-01-15 18:34:11 +0000384</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000385
Misha Brukmanc5402402004-01-15 18:34:11 +0000386<p>As you can see, our implementation above is pretty fast :). The additional
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000387passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify
388that the LLVM emitted by your pass is still valid and well formed LLVM, which
Misha Brukmanc5402402004-01-15 18:34:11 +0000389hasn't been broken somehow.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000390
Misha Brukmanc5402402004-01-15 18:34:11 +0000391<p>Now that you have seen the basics of the mechanics behind passes, we can talk
392about some more details of how they work and how to use them.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000393
Misha Brukmanc5402402004-01-15 18:34:11 +0000394</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000395
396<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000397<div class="doc_section">
398 <a name="passtype">Pass classes and requirements</a>
399</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000400<!-- *********************************************************************** -->
401
Misha Brukmanc5402402004-01-15 18:34:11 +0000402<div class="doc_text">
403
404<p>One of the first things that you should do when designing a new pass is to
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000405decide what class you should subclass for your pass. The <a
406href="#basiccode">Hello World</a> example uses the <tt><a
407href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we
408did not discuss why or when this should occur. Here we talk about the classes
Misha Brukmanc5402402004-01-15 18:34:11 +0000409available, from the most general to the most specific.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000410
Misha Brukmanc5402402004-01-15 18:34:11 +0000411<p>When choosing a superclass for your Pass, you should choose the <b>most
Chris Lattner79910702002-08-22 19:21:04 +0000412specific</b> class possible, while still being able to meet the requirements
Misha Brukman5560c9d2003-08-18 14:43:39 +0000413listed. This gives the LLVM Pass Infrastructure information necessary to
Chris Lattner79910702002-08-22 19:21:04 +0000414optimize how passes are run, so that the resultant compiler isn't unneccesarily
Misha Brukmanc5402402004-01-15 18:34:11 +0000415slow.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000416
Misha Brukmanc5402402004-01-15 18:34:11 +0000417</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000418
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000419<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000420<div class="doc_subsection">
421 <a name="ImmutablePass">The <tt>ImmutablePass</tt> class</a>
422</div>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000423
Misha Brukmanc5402402004-01-15 18:34:11 +0000424<div class="doc_text">
425
426<p>The most plain and boring type of pass is the "<tt><a
427href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1ImmutablePass.html">ImmutablePass</a></tt>"
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000428class. This pass type is used for passes that do not have to be run, do not
429change state, and never need to be updated. This is not a normal type of
430transformation or analysis, but can provide information about the current
Misha Brukmanc5402402004-01-15 18:34:11 +0000431compiler configuration.</p>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000432
Misha Brukmanc5402402004-01-15 18:34:11 +0000433<p>Although this pass class is very infrequently used, it is important for
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000434providing information about the current target machine being compiled for, and
Misha Brukmanc5402402004-01-15 18:34:11 +0000435other static information that can affect the various transformations.</p>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000436
Misha Brukmanc5402402004-01-15 18:34:11 +0000437<p><tt>ImmutablePass</tt>es never invalidate other transformations, are never
438invalidated, and are never "run".</p>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000439
Misha Brukmanc5402402004-01-15 18:34:11 +0000440</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000441
442<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000443<div class="doc_subsection">
Chris Lattnerf6278922004-09-20 04:36:29 +0000444 <a name="ModulePass">The <tt>ModulePass</tt> class</a>
Misha Brukmanc5402402004-01-15 18:34:11 +0000445</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000446
Misha Brukmanc5402402004-01-15 18:34:11 +0000447<div class="doc_text">
448
449<p>The "<tt><a
Chris Lattnerf6278922004-09-20 04:36:29 +0000450href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1ModulePass.html">ModulePass</a></tt>"
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000451class is the most general of all superclasses that you can use. Deriving from
Chris Lattnerf6278922004-09-20 04:36:29 +0000452<tt>ModulePass</tt> indicates that your pass uses the entire program as a unit,
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000453refering to function bodies in no predictable order, or adding and removing
Chris Lattnerf6278922004-09-20 04:36:29 +0000454functions. Because nothing is known about the behavior of <tt>ModulePass</tt>
Misha Brukmanc5402402004-01-15 18:34:11 +0000455subclasses, no optimization can be done for their execution.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000456
Chris Lattnerf6278922004-09-20 04:36:29 +0000457<p>To write a correct <tt>ModulePass</tt> subclass, derive from
458<tt>ModulePass</tt> and overload the <tt>runOnModule</tt> method with the
459following signature:</p>
Misha Brukmanc5402402004-01-15 18:34:11 +0000460
461</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000462
463<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000464<div class="doc_subsubsection">
Chris Lattnerf6278922004-09-20 04:36:29 +0000465 <a name="runOnModule">The <tt>runOnModule</tt> method</a>
Misha Brukmanc5402402004-01-15 18:34:11 +0000466</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000467
Misha Brukmanc5402402004-01-15 18:34:11 +0000468<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000469
470<pre>
Chris Lattnerf6278922004-09-20 04:36:29 +0000471 <b>virtual bool</b> runOnModule(Module &amp;M) = 0;
Misha Brukmanc5402402004-01-15 18:34:11 +0000472</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000473
Chris Lattnerf6278922004-09-20 04:36:29 +0000474<p>The <tt>runOnModule</tt> method performs the interesting work of the pass,
475and should return true if the module was modified by the transformation, false
Misha Brukmanc5402402004-01-15 18:34:11 +0000476otherwise.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000477
Misha Brukmanc5402402004-01-15 18:34:11 +0000478</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000479
480<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000481<div class="doc_subsection">
Chris Lattner8fdb2462004-09-18 06:39:35 +0000482 <a name="CallGraphSCCPass">The <tt>CallGraphSCCPass</tt> class</a>
483</div>
484
485<div class="doc_text">
486
487<p>The "<tt><a
488href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1CallGraphSCCPass.html">CallGraphSCCPass</a></tt>"
489is used by passes that need to traverse the program bottom-up on the call graph
490(callees before callers). Deriving from CallGraphSCCPass provides some
491mechanics for building and traversing the CallGraph, but also allows the system
492to optimize execution of CallGraphSCCPass's. If your pass meets the
493requirements outlined below, and doesn't meet the requirements of a <tt><a
494href="#FunctionPass">FunctionPass</a></tt> or <tt><a
495href="#BasicBlockPass">BasicBlockPass</a></tt>, you should derive from
496<tt>CallGraphSCCPass</tt>.</p>
497
498<p><b>TODO</b>: explain briefly what SCC, Tarjan's algo, and B-U mean.</p>
499
500<p>To be explicit, <tt>CallGraphSCCPass</tt> subclasses are:</p>
501
502<ol>
503
504<li>... <em>not allowed</em> to modify any <tt>Function</tt>s that are not in
505the current SCC.</li>
506
507<li>... <em>allowed</em> to inspect any Function's other than those in the
508current SCC and the direct callees of the SCC.</li>
509
510<li>... <em>required</em> to preserve the current CallGraph object, updating it
511to reflect any changes made to the program.</li>
512
513<li>... <em>not allowed</em> to add or remove SCC's from the current Module,
514though they may change the contents of an SCC.</li>
515
516<li>... <em>allowed</em> to add or remove global variables from the current
517Module.</li>
518
519<li>... <em>allowed</em> to maintain state across invocations of
520 <a href="#runOnSCC"><tt>runOnSCC</tt></a> (including global data).</li>
521</ol>
522
523<p>Implementing a <tt>CallGraphSCCPass</tt> is slightly tricky in some cases
524because it has to handle SCCs with more than one node in it. All of the virtual
525methods described below should return true if they modified the program, or
526false if they didn't.</p>
527
528</div>
529
530<!-- _______________________________________________________________________ -->
531<div class="doc_subsubsection">
532 <a name="doInitialization_scc">The <tt>doInitialization(Module &amp;)</tt>
533 method</a>
534</div>
535
536<div class="doc_text">
537
538<pre>
539 <b>virtual bool</b> doInitialization(Module &amp;M);
540</pre>
541
542<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
543<tt>CallGraphSCCPass</tt>'s are not allowed to do. They can add and remove
544functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
545is designed to do simple initialization type of stuff that does not depend on
546the SCCs being processed. The <tt>doInitialization</tt> method call is not
547scheduled to overlap with any other pass executions (thus it should be very
548fast).</p>
549
550</div>
551
552<!-- _______________________________________________________________________ -->
553<div class="doc_subsubsection">
554 <a name="runOnSCC">The <tt>runOnSCC</tt> method</a>
555</div>
556
557<div class="doc_text">
558
559<pre>
560 <b>virtual bool</b> runOnSCC(const std::vector&lt;CallGraphNode *&gt; &amp;SCCM) = 0;
561</pre>
562
563<p>The <tt>runOnSCC</tt> method performs the interesting work of the pass, and
564should return true if the module was modified by the transformation, false
565otherwise.</p>
566
567</div>
568
569<!-- _______________________________________________________________________ -->
570<div class="doc_subsubsection">
571 <a name="doFinalization_scc">The <tt>doFinalization(Module
572 &amp;)</tt> method</a>
573</div>
574
575<div class="doc_text">
576
577<pre>
578 <b>virtual bool</b> doFinalization(Module &amp;M);
579</pre>
580
581<p>The <tt>doFinalization</tt> method is an infrequently used method that is
582called when the pass framework has finished calling <a
583href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
584program being compiled.</p>
585
586</div>
587
588<!-- ======================================================================= -->
589<div class="doc_subsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000590 <a name="FunctionPass">The <tt>FunctionPass</tt> class</a>
591</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000592
Misha Brukmanc5402402004-01-15 18:34:11 +0000593<div class="doc_text">
594
Chris Lattnerf6278922004-09-20 04:36:29 +0000595<p>In contrast to <tt>ModulePass</tt> subclasses, <tt><a
Misha Brukmanc5402402004-01-15 18:34:11 +0000596href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Pass.html">FunctionPass</a></tt>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000597subclasses do have a predictable, local behavior that can be expected by the
598system. All <tt>FunctionPass</tt> execute on each function in the program
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000599independent of all of the other functions in the program.
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000600<tt>FunctionPass</tt>'s do not require that they are executed in a particular
Misha Brukmanc5402402004-01-15 18:34:11 +0000601order, and <tt>FunctionPass</tt>'s do not modify external functions.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000602
Misha Brukmanc5402402004-01-15 18:34:11 +0000603<p>To be explicit, <tt>FunctionPass</tt> subclasses are not allowed to:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000604
605<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +0000606<li>Modify a Function other than the one currently being processed.</li>
607<li>Add or remove Function's from the current Module.</li>
608<li>Add or remove global variables from the current Module.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000609<li>Maintain state across invocations of
Misha Brukmanc5402402004-01-15 18:34:11 +0000610 <a href="#runOnFunction"><tt>runOnFunction</tt></a> (including global data)</li>
611</ol>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000612
Misha Brukmanc5402402004-01-15 18:34:11 +0000613<p>Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000614href="#basiccode">Hello World</a> pass for example). <tt>FunctionPass</tt>'s
615may overload three virtual methods to do their work. All of these methods
Misha Brukmanc5402402004-01-15 18:34:11 +0000616should return true if they modified the program, or false if they didn't.</p>
617
618</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000619
620<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +0000621<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000622 <a name="doInitialization_mod">The <tt>doInitialization(Module &amp;)</tt>
623 method</a>
624</div>
625
626<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000627
628<pre>
629 <b>virtual bool</b> doInitialization(Module &amp;M);
Misha Brukmanc5402402004-01-15 18:34:11 +0000630</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000631
Misha Brukmanc5402402004-01-15 18:34:11 +0000632<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000633<tt>FunctionPass</tt>'s are not allowed to do. They can add and remove
Chris Lattnerd0713f92002-09-12 17:06:43 +0000634functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
635is designed to do simple initialization type of stuff that does not depend on
636the functions being processed. The <tt>doInitialization</tt> method call is not
637scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanc5402402004-01-15 18:34:11 +0000638fast).</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000639
Misha Brukmanc5402402004-01-15 18:34:11 +0000640<p>A good example of how this method should be used is the <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000641href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a>
642pass. This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000643platform dependent <tt>malloc()</tt> and <tt>free()</tt> function calls. It
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000644uses the <tt>doInitialization</tt> method to get a reference to the malloc and
Misha Brukmanc5402402004-01-15 18:34:11 +0000645free functions that it needs, adding prototypes to the module if necessary.</p>
646
647</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000648
649<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +0000650<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000651 <a name="runOnFunction">The <tt>runOnFunction</tt> method</a>
652</div>
653
654<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000655
656<pre>
657 <b>virtual bool</b> runOnFunction(Function &amp;F) = 0;
658</pre><p>
659
Misha Brukmanc5402402004-01-15 18:34:11 +0000660<p>The <tt>runOnFunction</tt> method must be implemented by your subclass to do
661the transformation or analysis work of your pass. As usual, a true value should
662be returned if the function is modified.</p>
663
664</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000665
666<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +0000667<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000668 <a name="doFinalization_mod">The <tt>doFinalization(Module
669 &amp;)</tt> method</a>
670</div>
671
672<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000673
674<pre>
675 <b>virtual bool</b> doFinalization(Module &amp;M);
Misha Brukmanc5402402004-01-15 18:34:11 +0000676</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000677
Misha Brukmanc5402402004-01-15 18:34:11 +0000678<p>The <tt>doFinalization</tt> method is an infrequently used method that is
679called when the pass framework has finished calling <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000680href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
Misha Brukmanc5402402004-01-15 18:34:11 +0000681program being compiled.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000682
Misha Brukmanc5402402004-01-15 18:34:11 +0000683</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000684
685<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000686<div class="doc_subsection">
687 <a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
688</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000689
Misha Brukmanc5402402004-01-15 18:34:11 +0000690<div class="doc_text">
691
692<p><tt>BasicBlockPass</tt>'s are just like <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000693href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
694their scope of inspection and modification to a single basic block at a time.
Misha Brukmanc5402402004-01-15 18:34:11 +0000695As such, they are <b>not</b> allowed to do any of the following:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000696
697<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +0000698<li>Modify or inspect any basic blocks outside of the current one</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000699<li>Maintain state across invocations of
Misha Brukmanc5402402004-01-15 18:34:11 +0000700 <a href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a></li>
Reid Spencer1bc19342004-12-11 05:12:57 +0000701<li>Modify the control flow graph (by altering terminator instructions)</li>
702<li>Any of the things forbidden for
Misha Brukmanc5402402004-01-15 18:34:11 +0000703 <a href="#FunctionPass"><tt>FunctionPass</tt></a>es.</li>
704</ol>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000705
Misha Brukmanc5402402004-01-15 18:34:11 +0000706<p><tt>BasicBlockPass</tt>es are useful for traditional local and "peephole"
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000707optimizations. They may override the same <a
Chris Lattnerd0713f92002-09-12 17:06:43 +0000708href="#doInitialization_mod"><tt>doInitialization(Module &amp;)</tt></a> and <a
709href="#doFinalization_mod"><tt>doFinalization(Module &amp;)</tt></a> methods that <a
Misha Brukmanc5402402004-01-15 18:34:11 +0000710href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have the following virtual methods that may also be implemented:</p>
711
712</div>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000713
714<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +0000715<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000716 <a name="doInitialization_fn">The <tt>doInitialization(Function
717 &amp;)</tt> method</a>
718</div>
719
720<div class="doc_text">
Chris Lattnerd0713f92002-09-12 17:06:43 +0000721
722<pre>
723 <b>virtual bool</b> doInitialization(Function &amp;F);
Misha Brukmanc5402402004-01-15 18:34:11 +0000724</pre>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000725
Misha Brukmanc5402402004-01-15 18:34:11 +0000726<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattnerd0713f92002-09-12 17:06:43 +0000727<tt>BasicBlockPass</tt>'s are not allowed to do, but that
728<tt>FunctionPass</tt>'s can. The <tt>doInitialization</tt> method is designed
Reid Spencer1bc19342004-12-11 05:12:57 +0000729to do simple initialization that does not depend on the
Chris Lattnerd0713f92002-09-12 17:06:43 +0000730BasicBlocks being processed. The <tt>doInitialization</tt> method call is not
731scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanc5402402004-01-15 18:34:11 +0000732fast).</p>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000733
Misha Brukmanc5402402004-01-15 18:34:11 +0000734</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000735
736<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +0000737<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000738 <a name="runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
739</div>
740
741<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000742
743<pre>
744 <b>virtual bool</b> runOnBasicBlock(BasicBlock &amp;BB) = 0;
Misha Brukmanc5402402004-01-15 18:34:11 +0000745</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000746
Misha Brukmanc5402402004-01-15 18:34:11 +0000747<p>Override this function to do the work of the <tt>BasicBlockPass</tt>. This
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000748function is not allowed to inspect or modify basic blocks other than the
749parameter, and are not allowed to modify the CFG. A true value must be returned
Misha Brukmanc5402402004-01-15 18:34:11 +0000750if the basic block is modified.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000751
Misha Brukmanc5402402004-01-15 18:34:11 +0000752</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000753
Chris Lattnerd0713f92002-09-12 17:06:43 +0000754<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +0000755<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000756 <a name="doFinalization_fn">The <tt>doFinalization(Function &amp;)</tt>
757 method</a>
758</div>
759
760<div class="doc_text">
Chris Lattnerd0713f92002-09-12 17:06:43 +0000761
762<pre>
763 <b>virtual bool</b> doFinalization(Function &amp;F);
Misha Brukmanc5402402004-01-15 18:34:11 +0000764</pre>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000765
Misha Brukmanc5402402004-01-15 18:34:11 +0000766<p>The <tt>doFinalization</tt> method is an infrequently used method that is
767called when the pass framework has finished calling <a
Chris Lattnerd0713f92002-09-12 17:06:43 +0000768href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a> for every BasicBlock in the
769program being compiled. This can be used to perform per-function
Misha Brukmanc5402402004-01-15 18:34:11 +0000770finalization.</p>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000771
Misha Brukmanc5402402004-01-15 18:34:11 +0000772</div>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000773
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000774<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000775<div class="doc_subsection">
776 <a name="MachineFunctionPass">The <tt>MachineFunctionPass</tt> class</a>
777</div>
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000778
Misha Brukmanc5402402004-01-15 18:34:11 +0000779<div class="doc_text">
780
Chris Lattner89256272004-03-17 21:09:55 +0000781<p>A <tt>MachineFunctionPass</tt> is a part of the LLVM code generator that
782executes on the machine-dependent representation of each LLVM function in the
783program. A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000784the restrictions that apply to a <tt>FunctionPass</tt> also apply to it.
Chris Lattner89256272004-03-17 21:09:55 +0000785<tt>MachineFunctionPass</tt>es also have additional restrictions. In particular,
786<tt>MachineFunctionPass</tt>es are not allowed to do any of the following:</p>
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000787
788<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +0000789<li>Modify any LLVM Instructions, BasicBlocks or Functions.</li>
790<li>Modify a MachineFunction other than the one currently being processed.</li>
791<li>Add or remove MachineFunctions from the current Module.</li>
792<li>Add or remove global variables from the current Module.</li>
793<li>Maintain state across invocations of <a
794href="#runOnMachineFunction"><tt>runOnMachineFunction</tt></a> (including global
795data)</li>
796</ol>
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000797
Misha Brukmanc5402402004-01-15 18:34:11 +0000798</div>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000799
Brian Gaeke6a33f362003-07-22 20:53:20 +0000800<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +0000801<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +0000802 <a name="runOnMachineFunction">The <tt>runOnMachineFunction(MachineFunction
803 &amp;MF)</tt> method</a>
804</div>
805
806<div class="doc_text">
Brian Gaeke6a33f362003-07-22 20:53:20 +0000807
808<pre>
809 <b>virtual bool</b> runOnMachineFunction(MachineFunction &amp;MF) = 0;
Misha Brukmanc5402402004-01-15 18:34:11 +0000810</pre>
Brian Gaeke6a33f362003-07-22 20:53:20 +0000811
Misha Brukmanc5402402004-01-15 18:34:11 +0000812<p><tt>runOnMachineFunction</tt> can be considered the main entry point of a
813<tt>MachineFunctionPass</tt>; that is, you should override this method to do the
814work of your <tt>MachineFunctionPass</tt>.</p>
Brian Gaeke6a33f362003-07-22 20:53:20 +0000815
Misha Brukmanc5402402004-01-15 18:34:11 +0000816<p>The <tt>runOnMachineFunction</tt> method is called on every
Brian Gaeke6a33f362003-07-22 20:53:20 +0000817<tt>MachineFunction</tt> in a <tt>Module</tt>, so that the
Misha Brukmanc5402402004-01-15 18:34:11 +0000818<tt>MachineFunctionPass</tt> may perform optimizations on the machine-dependent
819representation of the function. If you want to get at the LLVM <tt>Function</tt>
820for the <tt>MachineFunction</tt> you're working on, use
821<tt>MachineFunction</tt>'s <tt>getFunction()</tt> accessor method -- but
822remember, you may not modify the LLVM <tt>Function</tt> or its contents from a
823<tt>MachineFunctionPass</tt>.</p>
824
825</div>
Brian Gaeke6a33f362003-07-22 20:53:20 +0000826
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000827<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000828<div class="doc_section">
829 <a name="registration">Pass registration</a>
830</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000831<!-- *********************************************************************** -->
832
Misha Brukmanc5402402004-01-15 18:34:11 +0000833<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000834
Misha Brukmanc5402402004-01-15 18:34:11 +0000835<p>In the <a href="#basiccode">Hello World</a> example pass we illustrated how
836pass registration works, and discussed some of the reasons that it is used and
837what it does. Here we discuss how and why passes are registered.</p>
838
839<p>Passes can be registered in several different ways. Depending on the general
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000840classification of the pass, you should use one of the following templates to
Misha Brukmanc5402402004-01-15 18:34:11 +0000841register the pass:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000842
843<ul>
844<li><b><tt>RegisterOpt</tt></b> - This template should be used when you are
845registering a pass that logically should be available for use in the
Misha Brukmanc5402402004-01-15 18:34:11 +0000846'<tt>opt</tt>' utility.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000847
848<li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are
849registering a pass that logically should be available for use in the
Chris Lattner89256272004-03-17 21:09:55 +0000850'<tt>analyze</tt>' utility.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000851
852<li><b><tt>RegisterPass</tt></b> - This is the generic form of the
853<tt>Register*</tt> templates that should be used if you want your pass listed by
854multiple or no utilities. This template takes an extra third argument that
855specifies which tools it should be listed in. See the <a
856href="http://llvm.cs.uiuc.edu/doxygen/PassSupport_8h-source.html">PassSupport.h</a>
Misha Brukmanc5402402004-01-15 18:34:11 +0000857file for more information.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000858
Misha Brukmanc5402402004-01-15 18:34:11 +0000859</ul>
860
861<p>Regardless of how you register your pass, you must specify at least two
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000862parameters. The first parameter is the name of the pass that is to be used on
863the command line to specify that the pass should be added to a program (for
864example <tt>opt</tt> or <tt>analyze</tt>). The second argument is the name of
865the pass, which is to be used for the <tt>--help</tt> output of programs, as
Misha Brukmanc5402402004-01-15 18:34:11 +0000866well as for debug output generated by the <tt>--debug-pass</tt> option.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000867
Misha Brukmanc5402402004-01-15 18:34:11 +0000868<p>If a pass is registered to be used by the <tt>analyze</tt> utility, you
869should implement the virtual <tt>print</tt> method:</p>
870
871</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000872
873<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000874<div class="doc_subsubsection">
875 <a name="print">The <tt>print</tt> method</a>
876</div>
877
878<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000879
880<pre>
881 <b>virtual void</b> print(std::ostream &amp;O, <b>const</b> Module *M) <b>const</b>;
Misha Brukmanc5402402004-01-15 18:34:11 +0000882</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000883
Misha Brukmanc5402402004-01-15 18:34:11 +0000884<p>The <tt>print</tt> method must be implemented by "analyses" in order to print
885a human readable version of the analysis results. This is useful for debugging
886an analysis itself, as well as for other people to figure out how an analysis
887works. The <tt>analyze</tt> tool uses this method to generate its output.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000888
Misha Brukmanc5402402004-01-15 18:34:11 +0000889<p>The <tt>ostream</tt> parameter specifies the stream to write the results on,
890and the <tt>Module</tt> parameter gives a pointer to the top level module of the
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000891program that has been analyzed. Note however that this pointer may be null in
892certain circumstances (such as calling the <tt>Pass::dump()</tt> from a
893debugger), so it should only be used to enhance debug output, it should not be
Misha Brukmanc5402402004-01-15 18:34:11 +0000894depended on.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000895
Misha Brukmanc5402402004-01-15 18:34:11 +0000896</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000897
898<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000899<div class="doc_section">
900 <a name="interaction">Specifying interactions between passes</a>
901</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000902<!-- *********************************************************************** -->
903
Misha Brukmanc5402402004-01-15 18:34:11 +0000904<div class="doc_text">
905
906<p>One of the main responsibilities of the <tt>PassManager</tt> is the make sure
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000907that passes interact with each other correctly. Because <tt>PassManager</tt>
908tries to <a href="#passmanager">optimize the execution of passes</a> it must
909know how the passes interact with each other and what dependencies exist between
910the various passes. To track this, each pass can declare the set of passes that
911are required to be executed before the current pass, and the passes which are
Misha Brukmanc5402402004-01-15 18:34:11 +0000912invalidated by the current pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000913
Misha Brukmanc5402402004-01-15 18:34:11 +0000914<p>Typically this functionality is used to require that analysis results are
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000915computed before your pass is run. Running arbitrary transformation passes can
916invalidate the computed analysis results, which is what the invalidation set
917specifies. If a pass does not implement the <tt><a
918href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
Misha Brukmanc5402402004-01-15 18:34:11 +0000919having any prerequisite passes, and invalidating <b>all</b> other passes.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000920
Misha Brukmanc5402402004-01-15 18:34:11 +0000921</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000922
923<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000924<div class="doc_subsubsection">
925 <a name="getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a>
926</div>
927
928<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000929
930<pre>
931 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;Info) <b>const</b>;
Misha Brukmanc5402402004-01-15 18:34:11 +0000932</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000933
Misha Brukmanc5402402004-01-15 18:34:11 +0000934<p>By implementing the <tt>getAnalysisUsage</tt> method, the required and
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000935invalidated sets may be specified for your transformation. The implementation
936should fill in the <tt><a
Misha Brukmanc5402402004-01-15 18:34:11 +0000937href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
938object with information about which passes are required and not invalidated. To
Chris Lattner89256272004-03-17 21:09:55 +0000939do this, a pass may call any of the following methods on the AnalysisUsage
940object:</p>
941</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000942
Chris Lattner89256272004-03-17 21:09:55 +0000943<!-- _______________________________________________________________________ -->
944<div class="doc_subsubsection">
945 <a name="AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a>
946</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000947
Chris Lattner89256272004-03-17 21:09:55 +0000948<div class="doc_text">
949<p>
Reid Spencer1bc19342004-12-11 05:12:57 +0000950If your pass requires a previous pass to be executed (an analysis for example),
Chris Lattner89256272004-03-17 21:09:55 +0000951it can use one of these methods to arrange for it to be run before your pass.
952LLVM has many different types of analyses and passes that can be required,
Reid Spencer1bc19342004-12-11 05:12:57 +0000953spanning the range from <tt>DominatorSet</tt> to <tt>BreakCriticalEdges</tt>.
954Requiring <tt>BreakCriticalEdges</tt>, for example, guarantees that there will
Chris Lattner89256272004-03-17 21:09:55 +0000955be no critical edges in the CFG when your pass has been run.
956</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000957
Chris Lattner89256272004-03-17 21:09:55 +0000958<p>
959Some analyses chain to other analyses to do their job. For example, an <a
960href="AliasAnalysis.html">AliasAnalysis</a> implementation is required to <a
961href="AliasAnalysis.html#chaining">chain</a> to other alias analysis passes. In
962cases where analyses chain, the <tt>addRequiredTransitive</tt> method should be
963used instead of the <tt>addRequired</tt> method. This informs the PassManager
964that the transitively required pass should be alive as long as the requiring
965pass is.
966</p>
967</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000968
Chris Lattner89256272004-03-17 21:09:55 +0000969<!-- _______________________________________________________________________ -->
970<div class="doc_subsubsection">
971 <a name="AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a>
972</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000973
Chris Lattner89256272004-03-17 21:09:55 +0000974<div class="doc_text">
975<p>
976One of the jobs of the PassManager is to optimize how and when analyses are run.
977In particular, it attempts to avoid recomputing data unless it needs to. For
978this reason, passes are allowed to declare that they preserve (i.e., they don't
979invalidate) an existing analysis if it's available. For example, a simple
Reid Spencer1bc19342004-12-11 05:12:57 +0000980constant folding pass would not modify the CFG, so it can't possibly affect the
Chris Lattner89256272004-03-17 21:09:55 +0000981results of dominator analysis. By default, all passes are assumed to invalidate
982all others.
983</p>
984
985<p>
986The <tt>AnalysisUsage</tt> class provides several methods which are useful in
987certain circumstances that are related to <tt>addPreserved</tt>. In particular,
988the <tt>setPreservesAll</tt> method can be called to indicate that the pass does
989not modify the LLVM program at all (which is true for analyses), and the
990<tt>setPreservesCFG</tt> method can be used by transformations that change
991instructions in the program but do not modify the CFG or terminator instructions
992(note that this property is implicitly set for <a
993href="#BasicBlockPass">BasicBlockPass</a>'s).
994</p>
995
996<p>
997<tt>addPreserved</tt> is particularly useful for transformations like
998<tt>BreakCriticalEdges</tt>. This pass knows how to update a small set of loop
999and dominator related analyses if they exist, so it can preserve them, despite
1000the fact that it hacks on the CFG.
1001</p>
1002</div>
1003
1004<!-- _______________________________________________________________________ -->
1005<div class="doc_subsubsection">
1006 <a name="AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a>
1007</div>
1008
1009<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001010
1011<pre>
1012 <i>// This is an example implementation from an analysis, which does not modify
1013 // the program at all, yet has a prerequisite.</i>
Misha Brukmanc5402402004-01-15 18:34:11 +00001014 <b>void</b> <a href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1PostDominanceFrontier.html">PostDominanceFrontier</a>::getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001015 AU.setPreservesAll();
Misha Brukmanc5402402004-01-15 18:34:11 +00001016 AU.addRequired&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1PostDominatorTree.html">PostDominatorTree</a>&gt;();
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001017 }
Misha Brukmanc5402402004-01-15 18:34:11 +00001018</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001019
Misha Brukmanc5402402004-01-15 18:34:11 +00001020<p>and:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001021
1022<pre>
1023 <i>// This example modifies the program, but does not modify the CFG</i>
1024 <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 +00001025 AU.setPreservesCFG();
Misha Brukmanc5402402004-01-15 18:34:11 +00001026 AU.addRequired&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1LoopInfo.html">LoopInfo</a>&gt;();
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001027 }
Misha Brukmanc5402402004-01-15 18:34:11 +00001028</pre>
1029
1030</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001031
1032<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001033<div class="doc_subsubsection">
Chris Lattner89256272004-03-17 21:09:55 +00001034 <a name="getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> and <tt>getAnalysisToUpdate&lt;&gt;</tt> methods</a>
Misha Brukmanc5402402004-01-15 18:34:11 +00001035</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001036
Misha Brukmanc5402402004-01-15 18:34:11 +00001037<div class="doc_text">
1038
Chris Lattner89256272004-03-17 21:09:55 +00001039<p>The <tt>Pass::getAnalysis&lt;&gt;</tt> method is automatically inherited by
1040your class, providing you with access to the passes that you declared that you
1041required with the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a>
1042method. It takes a single template argument that specifies which pass class you
1043want, and returns a reference to that pass. For example:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001044
1045<pre>
Chris Lattnera5776302004-03-17 21:33:32 +00001046 bool LICM::runOnFunction(Function &amp;F) {
Chris Lattner89256272004-03-17 21:09:55 +00001047 LoopInfo &amp;LI = getAnalysis&lt;LoopInfo&gt;();
1048 ...
1049 }
Misha Brukmanc5402402004-01-15 18:34:11 +00001050</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001051
Misha Brukmanc5402402004-01-15 18:34:11 +00001052<p>This method call returns a reference to the pass desired. You may get a
1053runtime assertion failure if you attempt to get an analysis that you did not
1054declare as required in your <a
1055href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> implementation. This
1056method can be called by your <tt>run*</tt> method implementation, or by any
1057other local method invoked by your <tt>run*</tt> method.</p>
1058
Chris Lattner89256272004-03-17 21:09:55 +00001059<p>
1060If your pass is capable of updating analyses if they exist (e.g.,
1061<tt>BreakCriticalEdges</tt>, as described above), you can use the
1062<tt>getAnalysisToUpdate</tt> method, which returns a pointer to the analysis if
1063it is active. For example:</p>
1064
1065<pre>
1066 ...
1067 if (DominatorSet *DS = getAnalysisToUpdate&lt;DominatorSet&gt;()) {
1068 <i>// A DominatorSet is active. This code will update it.</i>
1069 }
1070 ...
1071</pre>
1072
Misha Brukmanc5402402004-01-15 18:34:11 +00001073</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001074
Chris Lattner79910702002-08-22 19:21:04 +00001075<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001076<div class="doc_section">
1077 <a name="analysisgroup">Implementing Analysis Groups</a>
1078</div>
Chris Lattner79910702002-08-22 19:21:04 +00001079<!-- *********************************************************************** -->
1080
Misha Brukmanc5402402004-01-15 18:34:11 +00001081<div class="doc_text">
1082
1083<p>Now that we understand the basics of how passes are defined, how the are
1084used, and how they are required from other passes, it's time to get a little bit
Chris Lattner79910702002-08-22 19:21:04 +00001085fancier. All of the pass relationships that we have seen so far are very
1086simple: one pass depends on one other specific pass to be run before it can run.
1087For many applications, this is great, for others, more flexibility is
Misha Brukmanc5402402004-01-15 18:34:11 +00001088required.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001089
Misha Brukmanc5402402004-01-15 18:34:11 +00001090<p>In particular, some analyses are defined such that there is a single simple
Chris Lattner79910702002-08-22 19:21:04 +00001091interface to the analysis results, but multiple ways of calculating them.
1092Consider alias analysis for example. The most trivial alias analysis returns
1093"may alias" for any alias query. The most sophisticated analysis a
1094flow-sensitive, context-sensitive interprocedural analysis that can take a
1095significant amount of time to execute (and obviously, there is a lot of room
1096between these two extremes for other implementations). To cleanly support
1097situations like this, the LLVM Pass Infrastructure supports the notion of
Misha Brukmanc5402402004-01-15 18:34:11 +00001098Analysis Groups.</p>
1099
1100</div>
Chris Lattner79910702002-08-22 19:21:04 +00001101
1102<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +00001103<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +00001104 <a name="agconcepts">Analysis Group Concepts</a>
1105</div>
Chris Lattner79910702002-08-22 19:21:04 +00001106
Misha Brukmanc5402402004-01-15 18:34:11 +00001107<div class="doc_text">
1108
1109<p>An Analysis Group is a single simple interface that may be implemented by
Chris Lattner79910702002-08-22 19:21:04 +00001110multiple different passes. Analysis Groups can be given human readable names
1111just like passes, but unlike passes, they need not derive from the <tt>Pass</tt>
1112class. An analysis group may have one or more implementations, one of which is
Misha Brukmanc5402402004-01-15 18:34:11 +00001113the "default" implementation.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001114
Misha Brukmanc5402402004-01-15 18:34:11 +00001115<p>Analysis groups are used by client passes just like other passes are: the
Chris Lattner79910702002-08-22 19:21:04 +00001116<tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods.
1117In order to resolve this requirement, the <a href="#passmanager">PassManager</a>
1118scans the available passes to see if any implementations of the analysis group
1119are available. If none is available, the default implementation is created for
1120the pass to use. All standard rules for <A href="#interaction">interaction
Misha Brukmanc5402402004-01-15 18:34:11 +00001121between passes</a> still apply.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001122
Misha Brukmanc5402402004-01-15 18:34:11 +00001123<p>Although <a href="#registration">Pass Registration</a> is optional for normal
Chris Lattner79910702002-08-22 19:21:04 +00001124passes, all analysis group implementations must be registered, and must use the
1125<A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the
1126implementation pool. Also, a default implementation of the interface
1127<b>must</b> be registered with <A
Misha Brukmanc5402402004-01-15 18:34:11 +00001128href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001129
Misha Brukmanc5402402004-01-15 18:34:11 +00001130<p>As a concrete example of an Analysis Group in action, consider the <a
1131href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>
Chris Lattner79910702002-08-22 19:21:04 +00001132analysis group. The default implementation of the alias analysis interface (the
1133<tt><a
1134href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt>
1135pass) just does a few simple checks that don't require significant analysis to
1136compute (such as: two different globals can never alias each other, etc).
1137Passes that use the <tt><a
Misha Brukmanc5402402004-01-15 18:34:11 +00001138href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner79910702002-08-22 19:21:04 +00001139interface (for example the <tt><a
Misha Brukmand15516e2004-06-03 23:39:36 +00001140href="http://llvm.cs.uiuc.edu/doxygen/structGCSE.html">gcse</a></tt> pass), do
Misha Brukmanc5402402004-01-15 18:34:11 +00001141not care which implementation of alias analysis is actually provided, they just
1142use the designated interface.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001143
Misha Brukmanc5402402004-01-15 18:34:11 +00001144<p>From the user's perspective, commands work just like normal. Issuing the
Chris Lattner79910702002-08-22 19:21:04 +00001145command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be
1146instantiated and added to the pass sequence. Issuing the command '<tt>opt
1147-somefancyaa -gcse ...</tt>' will cause the <tt>gcse</tt> pass to use the
1148<tt>somefancyaa</tt> alias analysis (which doesn't actually exist, it's just a
Misha Brukmanc5402402004-01-15 18:34:11 +00001149hypothetical example) instead.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001150
Misha Brukmanc5402402004-01-15 18:34:11 +00001151</div>
Chris Lattner79910702002-08-22 19:21:04 +00001152
1153<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +00001154<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +00001155 <a name="registerag">Using <tt>RegisterAnalysisGroup</tt></a>
1156</div>
Chris Lattner79910702002-08-22 19:21:04 +00001157
Misha Brukmanc5402402004-01-15 18:34:11 +00001158<div class="doc_text">
1159
1160<p>The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
Chris Lattner79910702002-08-22 19:21:04 +00001161group itself as well as add pass implementations to the analysis group. First,
1162an analysis should be registered, with a human readable name provided for it.
1163Unlike registration of passes, there is no command line argument to be specified
Misha Brukmanc5402402004-01-15 18:34:11 +00001164for the Analysis Group Interface itself, because it is "abstract":</p>
Chris Lattner79910702002-08-22 19:21:04 +00001165
1166<pre>
Misha Brukmanc5402402004-01-15 18:34:11 +00001167 <b>static</b> RegisterAnalysisGroup&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>&gt; A("<i>Alias Analysis</i>");
1168</pre>
Chris Lattner79910702002-08-22 19:21:04 +00001169
Misha Brukmanc5402402004-01-15 18:34:11 +00001170<p>Once the analysis is registered, passes can declare that they are valid
1171implementations of the interface by using the following code:</p>
Chris Lattner79910702002-08-22 19:21:04 +00001172
1173<pre>
1174<b>namespace</b> {
1175 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
1176 RegisterOpt&lt;FancyAA&gt;
1177 B("<i>somefancyaa</i>", "<i>A more complex alias analysis implementation</i>");
1178
1179 //<i> Declare that we implement the AliasAnalysis interface</i>
Misha Brukmanc5402402004-01-15 18:34:11 +00001180 RegisterAnalysisGroup&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>, FancyAA&gt; C;
Chris Lattner79910702002-08-22 19:21:04 +00001181}
Misha Brukmanc5402402004-01-15 18:34:11 +00001182</pre>
Chris Lattner79910702002-08-22 19:21:04 +00001183
Misha Brukmanc5402402004-01-15 18:34:11 +00001184<p>This just shows a class <tt>FancyAA</tt> that is registered normally, then
1185uses the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a
1186href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner79910702002-08-22 19:21:04 +00001187analysis group. Every implementation of an analysis group should join using
1188this template. A single pass may join multiple different analysis groups with
Misha Brukmanc5402402004-01-15 18:34:11 +00001189no problem.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001190
1191<pre>
1192<b>namespace</b> {
1193 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
1194 RegisterOpt&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>&gt;
1195 D("<i>basicaa</i>", "<i>Basic Alias Analysis (default AA impl)</i>");
1196
1197 //<i> Declare that we implement the AliasAnalysis interface</i>
Misha Brukmanc5402402004-01-15 18:34:11 +00001198 RegisterAnalysisGroup&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>, <a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>, <b>true</b>&gt; E;
Chris Lattner79910702002-08-22 19:21:04 +00001199}
Misha Brukmanc5402402004-01-15 18:34:11 +00001200</pre>
Chris Lattner79910702002-08-22 19:21:04 +00001201
Misha Brukmanc5402402004-01-15 18:34:11 +00001202<p>Here we show how the default implementation is specified (using the extra
Chris Lattner79910702002-08-22 19:21:04 +00001203argument to the <tt>RegisterAnalysisGroup</tt> template). There must be exactly
1204one default implementation available at all times for an Analysis Group to be
1205used. Here we declare that the <tt><a
1206href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt>
Misha Brukmanc5402402004-01-15 18:34:11 +00001207pass is the default implementation for the interface.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001208
Misha Brukmanc5402402004-01-15 18:34:11 +00001209</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001210
1211<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001212<div class="doc_section">
Tanya Lattnerd5383652004-11-19 01:25:14 +00001213 <a name="passStatistics">Pass Statistics</a>
1214</div>
1215<!-- *********************************************************************** -->
1216
1217<div class="doc_text">
Tanya Lattner05f8d5f2004-11-19 01:26:37 +00001218<p>The <a
Tanya Lattner54dc8b22004-12-08 18:13:51 +00001219href="http://llvm.cs.uiuc.edu/doxygen/Statistic_8h-source.html"><tt>Statistic</tt></a>
Tanya Lattnerd5383652004-11-19 01:25:14 +00001220class, is designed to be an easy way to expose various success
1221metrics from passes. These statistics are printed at the end of a
1222run, when the -stats command line option is enabled on the command
Tanya Lattner05f8d5f2004-11-19 01:26:37 +00001223line. See the <a href="http://llvm.org/docs/ProgrammersManual.html#Statistic">Statistics section</a> in the Programmer's Manual for details.
Tanya Lattnerd5383652004-11-19 01:25:14 +00001224
1225</div>
1226
1227
1228<!-- *********************************************************************** -->
1229<div class="doc_section">
Misha Brukmanc5402402004-01-15 18:34:11 +00001230 <a name="passmanager">What PassManager does</a>
1231</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001232<!-- *********************************************************************** -->
1233
Misha Brukmanc5402402004-01-15 18:34:11 +00001234<div class="doc_text">
1235
1236<p>The <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001237href="http://llvm.cs.uiuc.edu/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a>
Misha Brukmanc5402402004-01-15 18:34:11 +00001238<a
1239href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1PassManager.html">class</a>
1240takes a list of passes, ensures their <a href="#interaction">prerequisites</a>
1241are set up correctly, and then schedules passes to run efficiently. All of the
1242LLVM tools that run passes use the <tt>PassManager</tt> for execution of these
1243passes.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001244
Misha Brukmanc5402402004-01-15 18:34:11 +00001245<p>The <tt>PassManager</tt> does two main things to try to reduce the execution
1246time of a series of passes:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001247
1248<ol>
1249<li><b>Share analysis results</b> - The PassManager attempts to avoid
1250recomputing analysis results as much as possible. This means keeping track of
1251which analyses are available already, which analyses get invalidated, and which
1252analyses are needed to be run for a pass. An important part of work is that the
1253<tt>PassManager</tt> tracks the exact lifetime of all analysis results, allowing
1254it to <a href="#releaseMemory">free memory</a> allocated to holding analysis
Misha Brukmanc5402402004-01-15 18:34:11 +00001255results as soon as they are no longer needed.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001256
1257<li><b>Pipeline the execution of passes on the program</b> - The
1258<tt>PassManager</tt> attempts to get better cache and memory usage behavior out
1259of a series of passes by pipelining the passes together. This means that, given
1260a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it
1261will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on
1262the first function, then all of the <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001263href="#FunctionPass"><tt>FunctionPass</tt></a>es on the second function,
1264etc... until the entire program has been run through the passes.
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001265
Misha Brukmanc5402402004-01-15 18:34:11 +00001266<p>This improves the cache behavior of the compiler, because it is only touching
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001267the LLVM program representation for a single function at a time, instead of
1268traversing the entire program. It reduces the memory consumption of compiler,
1269because, for example, only one <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001270href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1DominatorSet.html"><tt>DominatorSet</tt></a>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001271needs to be calculated at a time. This also makes it possible some <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001272href="#SMP">interesting enhancements</a> in the future.</p></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001273
Misha Brukmanc5402402004-01-15 18:34:11 +00001274</ol>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001275
Misha Brukmanc5402402004-01-15 18:34:11 +00001276<p>The effectiveness of the <tt>PassManager</tt> is influenced directly by how
1277much information it has about the behaviors of the passes it is scheduling. For
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001278example, the "preserved" set is intentionally conservative in the face of an
1279unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.
1280Not implementing when it should be implemented will have the effect of not
Misha Brukmanc5402402004-01-15 18:34:11 +00001281allowing any analysis results to live across the execution of your pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001282
Misha Brukmanc5402402004-01-15 18:34:11 +00001283<p>The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001284options that is useful for debugging pass execution, seeing how things work, and
1285diagnosing when you should be preserving more analyses than you currently are
1286(To get information about all of the variants of the <tt>--debug-pass</tt>
Misha Brukmanc5402402004-01-15 18:34:11 +00001287option, just type '<tt>opt --help-hidden</tt>').</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001288
Misha Brukmanc5402402004-01-15 18:34:11 +00001289<p>By using the <tt>--debug-pass=Structure</tt> option, for example, we can see
1290how our <a href="#basiccode">Hello World</a> pass interacts with other passes.
1291Lets try it out with the <tt>gcse</tt> and <tt>licm</tt> passes:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001292
1293<pre>
Reid Spencer7fa6d522005-01-11 05:16:23 +00001294$ opt -load ../../../Debug/lib/Hello.so -gcse -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001295Module Pass Manager
1296 Function Pass Manager
1297 Dominator Set Construction
1298 Immediate Dominators Construction
1299 Global Common Subexpression Elimination
1300-- Immediate Dominators Construction
1301-- Global Common Subexpression Elimination
1302 Natural Loop Construction
1303 Loop Invariant Code Motion
1304-- Natural Loop Construction
1305-- Loop Invariant Code Motion
1306 Module Verifier
1307-- Dominator Set Construction
1308-- Module Verifier
1309 Bytecode Writer
1310--Bytecode Writer
Misha Brukmanc5402402004-01-15 18:34:11 +00001311</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001312
Misha Brukmanc5402402004-01-15 18:34:11 +00001313<p>This output shows us when passes are constructed and when the analysis
1314results are known to be dead (prefixed with '<tt>--</tt>'). Here we see that
1315GCSE uses dominator and immediate dominator information to do its job. The LICM
1316pass uses natural loop information, which uses dominator sets, but not immediate
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001317dominators. Because immediate dominators are no longer useful after the GCSE
1318pass, it is immediately destroyed. The dominator sets are then reused to
Misha Brukmanc5402402004-01-15 18:34:11 +00001319compute natural loop information, which is then used by the LICM pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001320
Misha Brukmanc5402402004-01-15 18:34:11 +00001321<p>After the LICM pass, the module verifier runs (which is automatically added
1322by the '<tt>opt</tt>' tool), which uses the dominator set to check that the
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001323resultant LLVM code is well formed. After it finishes, the dominator set
1324information is destroyed, after being computed once, and shared by three
Misha Brukmanc5402402004-01-15 18:34:11 +00001325passes.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001326
Misha Brukmanc5402402004-01-15 18:34:11 +00001327<p>Lets see how this changes when we run the <a href="#basiccode">Hello
1328World</a> pass in between the two passes:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001329
1330<pre>
Reid Spencer7fa6d522005-01-11 05:16:23 +00001331$ opt -load ../../../Debug/lib/Hello.so -gcse -hello -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001332Module Pass Manager
1333 Function Pass Manager
1334 Dominator Set Construction
1335 Immediate Dominators Construction
1336 Global Common Subexpression Elimination
1337<b>-- Dominator Set Construction</b>
1338-- Immediate Dominators Construction
1339-- Global Common Subexpression Elimination
1340<b> Hello World Pass
1341-- Hello World Pass
1342 Dominator Set Construction</b>
1343 Natural Loop Construction
1344 Loop Invariant Code Motion
1345-- Natural Loop Construction
1346-- Loop Invariant Code Motion
1347 Module Verifier
1348-- Dominator Set Construction
1349-- Module Verifier
1350 Bytecode Writer
1351--Bytecode Writer
1352Hello: __main
1353Hello: puts
1354Hello: main
Misha Brukmanc5402402004-01-15 18:34:11 +00001355</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001356
Misha Brukmanc5402402004-01-15 18:34:11 +00001357<p>Here we see that the <a href="#basiccode">Hello World</a> pass has killed the
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001358Dominator Set pass, even though it doesn't modify the code at all! To fix this,
1359we need to add the following <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001360href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method to our pass:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001361
1362<pre>
1363 <i>// We don't modify the program, so we preserve all analyses</i>
1364 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
1365 AU.setPreservesAll();
1366 }
Misha Brukmanc5402402004-01-15 18:34:11 +00001367</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001368
Misha Brukmanc5402402004-01-15 18:34:11 +00001369<p>Now when we run our pass, we get this output:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001370
1371<pre>
Reid Spencer7fa6d522005-01-11 05:16:23 +00001372$ opt -load ../../../Debug/lib/Hello.so -gcse -hello -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001373Pass Arguments: -gcse -hello -licm
1374Module Pass Manager
1375 Function Pass Manager
1376 Dominator Set Construction
1377 Immediate Dominators Construction
1378 Global Common Subexpression Elimination
1379-- Immediate Dominators Construction
1380-- Global Common Subexpression Elimination
1381 Hello World Pass
1382-- Hello World Pass
1383 Natural Loop Construction
1384 Loop Invariant Code Motion
1385-- Loop Invariant Code Motion
1386-- Natural Loop Construction
1387 Module Verifier
1388-- Dominator Set Construction
1389-- Module Verifier
1390 Bytecode Writer
1391--Bytecode Writer
1392Hello: __main
1393Hello: puts
1394Hello: main
Misha Brukmanc5402402004-01-15 18:34:11 +00001395</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001396
Misha Brukmanc5402402004-01-15 18:34:11 +00001397<p>Which shows that we don't accidentally invalidate dominator information
1398anymore, and therefore do not have to compute it twice.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001399
Misha Brukmanc5402402004-01-15 18:34:11 +00001400</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001401
1402<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001403<div class="doc_subsubsection">
1404 <a name="releaseMemory">The <tt>releaseMemory</tt> method</a>
1405</div>
1406
1407<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001408
1409<pre>
1410 <b>virtual void</b> releaseMemory();
Misha Brukmanc5402402004-01-15 18:34:11 +00001411</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001412
Misha Brukmanc5402402004-01-15 18:34:11 +00001413<p>The <tt>PassManager</tt> automatically determines when to compute analysis
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001414results, and how long to keep them around for. Because the lifetime of the pass
1415object itself is effectively the entire duration of the compilation process, we
1416need some way to free analysis results when they are no longer useful. The
Misha Brukmanc5402402004-01-15 18:34:11 +00001417<tt>releaseMemory</tt> virtual method is the way to do this.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001418
Misha Brukmanc5402402004-01-15 18:34:11 +00001419<p>If you are writing an analysis or any other pass that retains a significant
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001420amount of state (for use by another pass which "requires" your pass and uses the
1421<a href="#getAnalysis">getAnalysis</a> method) you should implement
1422<tt>releaseMEmory</tt> to, well, release the memory allocated to maintain this
1423internal state. This method is called after the <tt>run*</tt> method for the
Misha Brukmanc5402402004-01-15 18:34:11 +00001424class, before the next call of <tt>run*</tt> in your pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001425
Misha Brukmanc5402402004-01-15 18:34:11 +00001426</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001427
1428<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001429<div class="doc_section">
1430 <a name="debughints">Using GDB with dynamically loaded passes</a>
1431</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001432<!-- *********************************************************************** -->
1433
Misha Brukmanc5402402004-01-15 18:34:11 +00001434<div class="doc_text">
1435
1436<p>Unfortunately, using GDB with dynamically loaded passes is not as easy as it
Chris Lattner480e2ef2002-09-06 02:02:58 +00001437should be. First of all, you can't set a breakpoint in a shared object that has
1438not been loaded yet, and second of all there are problems with inlined functions
1439in shared objects. Here are some suggestions to debugging your pass with
Misha Brukmanc5402402004-01-15 18:34:11 +00001440GDB.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001441
Misha Brukmanc5402402004-01-15 18:34:11 +00001442<p>For sake of discussion, I'm going to assume that you are debugging a
Chris Lattner480e2ef2002-09-06 02:02:58 +00001443transformation invoked by <tt>opt</tt>, although nothing described here depends
Misha Brukmanc5402402004-01-15 18:34:11 +00001444on that.</p>
1445
1446</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001447
1448<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001449<div class="doc_subsubsection">
1450 <a name="breakpoint">Setting a breakpoint in your pass</a>
1451</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001452
Misha Brukmanc5402402004-01-15 18:34:11 +00001453<div class="doc_text">
1454
1455<p>First thing you do is start <tt>gdb</tt> on the <tt>opt</tt> process:</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001456
1457<pre>
1458$ <b>gdb opt</b>
1459GNU gdb 5.0
1460Copyright 2000 Free Software Foundation, Inc.
1461GDB is free software, covered by the GNU General Public License, and you are
1462welcome to change it and/or distribute copies of it under certain conditions.
1463Type "show copying" to see the conditions.
1464There is absolutely no warranty for GDB. Type "show warranty" for details.
1465This GDB was configured as "sparc-sun-solaris2.6"...
1466(gdb)
Misha Brukmanc5402402004-01-15 18:34:11 +00001467</pre>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001468
Misha Brukmanc5402402004-01-15 18:34:11 +00001469<p>Note that <tt>opt</tt> has a lot of debugging information in it, so it takes
Chris Lattner480e2ef2002-09-06 02:02:58 +00001470time to load. Be patient. Since we cannot set a breakpoint in our pass yet
1471(the shared object isn't loaded until runtime), we must execute the process, and
1472have it stop before it invokes our pass, but after it has loaded the shared
1473object. The most foolproof way of doing this is to set a breakpoint in
1474<tt>PassManager::run</tt> and then run the process with the arguments you
Misha Brukmanc5402402004-01-15 18:34:11 +00001475want:</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001476
1477<pre>
1478(gdb) <b>break PassManager::run</b>
1479Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
Chris Lattnerfafe6302003-10-23 19:48:11 +00001480(gdb) <b>run test.bc -load $(LLVMTOP)/llvm/lib/Debug/[libname].so -[passoption]</b>
1481Starting program: opt test.bc -load $(LLVMTOP)/llvm/lib/Debug/[libname].so -[passoption]
Chris Lattner480e2ef2002-09-06 02:02:58 +00001482Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
148370 bool PassManager::run(Module &amp;M) { return PM-&gt;run(M); }
1484(gdb)
Misha Brukmanc5402402004-01-15 18:34:11 +00001485</pre>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001486
Misha Brukmanc5402402004-01-15 18:34:11 +00001487<p>Once the <tt>opt</tt> stops in the <tt>PassManager::run</tt> method you are
1488now free to set breakpoints in your pass so that you can trace through execution
1489or do other standard debugging stuff.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001490
Misha Brukmanc5402402004-01-15 18:34:11 +00001491</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001492
1493<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001494<div class="doc_subsubsection">
1495 <a name="debugmisc">Miscellaneous Problems</a>
1496</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001497
Misha Brukmanc5402402004-01-15 18:34:11 +00001498<div class="doc_text">
1499
1500<p>Once you have the basics down, there are a couple of problems that GDB has,
1501some with solutions, some without.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001502
1503<ul>
1504<li>Inline functions have bogus stack information. In general, GDB does a
1505pretty good job getting stack traces and stepping through inline functions.
1506When a pass is dynamically loaded however, it somehow completely loses this
1507capability. The only solution I know of is to de-inline a function (move it
Misha Brukmanc5402402004-01-15 18:34:11 +00001508from the body of a class to a .cpp file).</li>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001509
1510<li>Restarting the program breaks breakpoints. After following the information
1511above, you have succeeded in getting some breakpoints planted in your pass. Nex
1512thing you know, you restart the program (i.e., you type '<tt>run</tt>' again),
1513and you start getting errors about breakpoints being unsettable. The only way I
1514have found to "fix" this problem is to <tt>delete</tt> the breakpoints that are
1515already set in your pass, run the program, and re-set the breakpoints once
Misha Brukmanc5402402004-01-15 18:34:11 +00001516execution stops in <tt>PassManager::run</tt>.</li>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001517
1518</ul>
1519
Misha Brukmanc5402402004-01-15 18:34:11 +00001520<p>Hopefully these tips will help with common case debugging situations. If
1521you'd like to contribute some tips of your own, just contact <a
1522href="mailto:sabre@nondot.org">Chris</a>.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001523
Misha Brukmanc5402402004-01-15 18:34:11 +00001524</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001525
1526<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001527<div class="doc_section">
1528 <a name="future">Future extensions planned</a>
1529</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001530<!-- *********************************************************************** -->
1531
Misha Brukmanc5402402004-01-15 18:34:11 +00001532<div class="doc_text">
1533
1534<p>Although the LLVM Pass Infrastructure is very capable as it stands, and does
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001535some nifty stuff, there are things we'd like to add in the future. Here is
Misha Brukmanc5402402004-01-15 18:34:11 +00001536where we are going:</p>
1537
1538</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001539
1540<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +00001541<div class="doc_subsubsection">
Misha Brukmanc5402402004-01-15 18:34:11 +00001542 <a name="SMP">Multithreaded LLVM</a>
1543</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001544
Misha Brukmanc5402402004-01-15 18:34:11 +00001545<div class="doc_text">
1546
1547<p>Multiple CPU machines are becoming more common and compilation can never be
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001548fast enough: obviously we should allow for a multithreaded compiler. Because of
1549the semantics defined for passes above (specifically they cannot maintain state
1550across invocations of their <tt>run*</tt> methods), a nice clean way to
1551implement a multithreaded compiler would be for the <tt>PassManager</tt> class
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001552to create multiple instances of each pass object, and allow the separate
Misha Brukmanc5402402004-01-15 18:34:11 +00001553instances to be hacking on different parts of the program at the same time.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001554
Misha Brukmanc5402402004-01-15 18:34:11 +00001555<p>This implementation would prevent each of the passes from having to implement
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001556multithreaded constructs, requiring only the LLVM core to have locking in a few
1557places (for global resources). Although this is a simple extension, we simply
1558haven't had time (or multiprocessor machines, thus a reason) to implement this.
Misha Brukmanc5402402004-01-15 18:34:11 +00001559Despite that, we have kept the LLVM passes SMP ready, and you should too.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001560
Misha Brukmanc5402402004-01-15 18:34:11 +00001561</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001562
1563<!-- _______________________________________________________________________ -->
Chris Lattner89256272004-03-17 21:09:55 +00001564<div class="doc_subsubsection">
Chris Lattnerf6278922004-09-20 04:36:29 +00001565<a name="PassFunctionPass"><tt>ModulePass</tt>es requiring <tt>FunctionPass</tt>es</a>
Misha Brukmanc5402402004-01-15 18:34:11 +00001566</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001567
Misha Brukmanc5402402004-01-15 18:34:11 +00001568<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001569
Chris Lattnerf6278922004-09-20 04:36:29 +00001570<p>Currently it is illegal for a <a href="#ModulePass"><tt>ModulePass</tt></a>
1571to require a <a href="#FunctionPass"><tt>FunctionPass</tt></a>. This is because
1572there is only one instance of the <a
1573href="#FunctionPass"><tt>FunctionPass</tt></a> object ever created, thus nowhere
1574to store information for all of the functions in the program at the same time.
1575Although this has come up a couple of times before, this has always been worked
1576around by factoring one big complicated pass into a global and an
1577interprocedural part, both of which are distinct. In the future, it would be
1578nice to have this though.</p>
Misha Brukmanc5402402004-01-15 18:34:11 +00001579
1580<p>Note that it is no problem for a <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001581href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a
Chris Lattnerf6278922004-09-20 04:36:29 +00001582href="#ModulePass"><tt>ModulePass</tt></a>, only the other way around.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001583
Misha Brukmanc5402402004-01-15 18:34:11 +00001584</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001585
1586<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001587<hr>
1588<address>
1589 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1590 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1591 <a href="http://validator.w3.org/check/referer"><img
1592 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001593
Misha Brukmanc5402402004-01-15 18:34:11 +00001594 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1595 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
1596 Last modified: $Date$
1597</address>
1598
1599</body>
1600</html>