blob: 3570717b75e19c9ca1c825182c4c80e0f7aa920d [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>
5 <title>Writing an LLVM Pass</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8<body>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00009
Misha Brukmanc5402402004-01-15 18:34:11 +000010<div class="doc_title">
11 Writing an LLVM Pass
12</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000013
14<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +000015 <li><a href="#introduction">Introduction - What is a pass?</a></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000016 <li><a href="#quickstart">Quick Start - Writing hello world</a>
17 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000018 <li><a href="#makefile">Setting up the build environment</a></li>
19 <li><a href="#basiccode">Basic code required</a></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000020 <li><a href="#running">Running a pass with <tt>opt</tt>
Misha Brukmanc5402402004-01-15 18:34:11 +000021 or <tt>analyze</tt></a></li>
22 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000023 <li><a href="#passtype">Pass classes and requirements</a>
24 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000025 <li><a href="#ImmutablePass">The <tt>ImmutablePass</tt> class</a></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000026 <li><a href="#Pass">The <tt>Pass</tt> class</a>
27 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000028 <li><a href="#run">The <tt>run</tt> method</a></li>
29 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000030 <li><a href="#FunctionPass">The <tt>FunctionPass</tt> class</a>
31 <ul>
Chris Lattnerd0713f92002-09-12 17:06:43 +000032 <li><a href="#doInitialization_mod">The <tt>doInitialization(Module
Misha Brukmanc5402402004-01-15 18:34:11 +000033 &amp;)</tt> method</a></li>
34 <li><a href="#runOnFunction">The <tt>runOnFunction</tt> method</a></li>
Chris Lattnerd0713f92002-09-12 17:06:43 +000035 <li><a href="#doFinalization_mod">The <tt>doFinalization(Module
Misha Brukmanc5402402004-01-15 18:34:11 +000036 &amp;)</tt> method</a></li>
37 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000038 <li><a href="#BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
39 <ul>
Chris Lattnerd0713f92002-09-12 17:06:43 +000040 <li><a href="#doInitialization_fn">The <tt>doInitialization(Function
Misha Brukmanc5402402004-01-15 18:34:11 +000041 &amp;)</tt> method</a></li>
42 <li><a href="#runOnBasicBlock">The <tt>runOnBasicBlock</tt>
43 method</a></li>
Chris Lattnerd0713f92002-09-12 17:06:43 +000044 <li><a href="#doFinalization_fn">The <tt>doFinalization(Function
Misha Brukmanc5402402004-01-15 18:34:11 +000045 &amp;)</tt> method</a></li>
46 </ul></li>
Brian Gaekecab8b6f2003-07-17 18:53:20 +000047 <li><a href="#MachineFunctionPass">The <tt>MachineFunctionPass</tt>
48 class</a>
Brian Gaeke6a33f362003-07-22 20:53:20 +000049 <ul>
50 <li><a href="#runOnMachineFunction">The
Misha Brukmanc5402402004-01-15 18:34:11 +000051 <tt>runOnMachineFunction(MachineFunction &amp;)</tt> method</a></li>
52 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000053 </ul>
54 <li><a href="#registration">Pass Registration</a>
55 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000056 <li><a href="#print">The <tt>print</tt> method</a></li>
57 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000058 <li><a href="#interaction">Specifying interactions between passes</a>
59 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000060 <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt>
61 method</a></li>
62 <li><a href="#getAnalysis">The <tt>getAnalysis</tt> method</a></li>
63 </ul></li>
Chris Lattner79910702002-08-22 19:21:04 +000064 <li><a href="#analysisgroup">Implementing Analysis Groups</a>
65 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000066 <li><a href="#agconcepts">Analysis Group Concepts</a></li>
67 <li><a href="#registerag">Using <tt>RegisterAnalysisGroup</tt></a></li>
68 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000069 <li><a href="#passmanager">What PassManager does</a>
70 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000071 <li><a href="#releaseMemory">The <tt>releaseMemory</tt> method</a></li>
72 </ul></li>
Chris Lattner480e2ef2002-09-06 02:02:58 +000073 <li><a href="#debughints">Using GDB with dynamically loaded passes</a>
74 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000075 <li><a href="#breakpoint">Setting a breakpoint in your pass</a></li>
76 <li><a href="#debugmisc">Miscellaneous Problems</a></li>
77 </ul></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000078 <li><a href="#future">Future extensions planned</a>
79 <ul>
Misha Brukmanc5402402004-01-15 18:34:11 +000080 <li><a href="#SMP">Multithreaded LLVM</a></li>
81 <li><a href="#ModuleSource">A new <tt>ModuleSource</tt> interface</a></li>
82 <li><a href="#PassFunctionPass"><tt>Pass</tt>es requiring
83 <tt>FunctionPass</tt>es</a></li>
84 </ul></li>
85</ol>
Chris Lattner38c633d2002-08-08 20:23:41 +000086
Misha Brukmanc5402402004-01-15 18:34:11 +000087<div class="doc_text">
Chris Lattner38c633d2002-08-08 20:23:41 +000088 <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p>
Misha Brukmanc5402402004-01-15 18:34:11 +000089</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000090
91<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +000092<div class="doc_section">
93 <a name="introduction">Introduction - What is a pass?</a>
94</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +000095<!-- *********************************************************************** -->
96
Misha Brukmanc5402402004-01-15 18:34:11 +000097<div class="doc_text">
98
99<p>The LLVM Pass Framework is an important part of the LLVM system, because LLVM
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000100passes are where the interesting parts of the compiler exist. Passes perform
101the transformations and optimizations that make up the compiler, they build
102the analysis results that are used by these transformations, and they are, above
Misha Brukmanc5402402004-01-15 18:34:11 +0000103all, a structuring technique for compiler code.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000104
Misha Brukmanc5402402004-01-15 18:34:11 +0000105<p>All LLVM passes are subclasses of the <tt><a
106href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Pass.html">Pass</a></tt>
107class, which implement functionality by overriding virtual methods inherited
108from <tt>Pass</tt>. Depending on how your pass works, you may be able to
109inherit from the <tt><a
110href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1FunctionPass.html">FunctionPass</a></tt>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000111or <tt><a
Misha Brukmanc5402402004-01-15 18:34:11 +0000112href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1BasicBlockPass.html">BasicBlockPass</a></tt>,
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000113which gives the system more information about what your pass does, and how it
114can be combined with other passes. One of the main features of the LLVM Pass
115Framework is that it schedules passes to run in an efficient way based on the
Misha Brukmanc5402402004-01-15 18:34:11 +0000116constraints that your pass has.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000117
Misha Brukmanc5402402004-01-15 18:34:11 +0000118<p>We start by showing you how to construct a pass, everything from setting up
119the code, to compiling, loading, and executing it. After the basics are down,
120more advanced features are discussed.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000121
Misha Brukmanc5402402004-01-15 18:34:11 +0000122</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000123
124<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000125<div class="doc_section">
126 <a name="quickstart">Quick Start - Writing hello world</a>
127</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000128<!-- *********************************************************************** -->
129
Misha Brukmanc5402402004-01-15 18:34:11 +0000130<div class="doc_text">
131
132<p>Here we describe how to write the "hello world" of passes. The "Hello" pass
133is designed to simply print out the name of non-external functions that exist in
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000134the program being compiled. It does not modify the program at all, just
135inspects it. The source code and files for this pass are available in the LLVM
Misha Brukmanc5402402004-01-15 18:34:11 +0000136source tree in the <tt>lib/Transforms/Hello</tt> directory.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000137
Misha Brukmanc5402402004-01-15 18:34:11 +0000138</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000139
140<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000141<div class="doc_subsection">
142 <a name="makefile">Setting up the build environment</a>
143</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000144
Misha Brukmanc5402402004-01-15 18:34:11 +0000145<div class="doc_text">
146
147<p>First thing you need to do is create a new directory somewhere in the LLVM
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000148source base. For this example, we'll assume that you made
149"<tt>lib/Transforms/Hello</tt>". The first thing you must do is set up a build
150script (Makefile) that will compile the source code for the new pass. To do
Misha Brukmanc5402402004-01-15 18:34:11 +0000151this, copy this into "<tt>Makefile</tt>":</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000152
Misha Brukmanc5402402004-01-15 18:34:11 +0000153<hr>
154
155<pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000156# Makefile for hello pass
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000157
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000158# Path to top level of LLVM heirarchy
Chris Lattner7ce83e52002-08-14 20:07:01 +0000159LEVEL = ../../..
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000160
161# Name of the library to build
Chris Lattner7ce83e52002-08-14 20:07:01 +0000162LIBRARYNAME = hello
Chris Lattner17a4c3e2002-08-14 20:06:13 +0000163
164# Build a dynamically loadable shared object
165SHARED_LIBRARY = 1
166
167# Include the makefile implementation stuff
168include $(LEVEL)/Makefile.common
Misha Brukmanc5402402004-01-15 18:34:11 +0000169</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000170
Misha Brukmanc5402402004-01-15 18:34:11 +0000171<p>This makefile specifies that all of the <tt>.cpp</tt> files in the current
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000172directory are to be compiled and linked together into a
173<tt>lib/Debug/libhello.so</tt> shared object that can be dynamically loaded by
Misha Brukmanc5402402004-01-15 18:34:11 +0000174the <tt>opt</tt> or <tt>analyze</tt> tools.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000175
Misha Brukmanc5402402004-01-15 18:34:11 +0000176<p>Now that we have the build scripts set up, we just need to write the code for
177the pass itself.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000178
Misha Brukmanc5402402004-01-15 18:34:11 +0000179</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000180
181<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000182<div class="doc_subsection">
183 <a name="basiccode">Basic code required</a>
184</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000185
Misha Brukmanc5402402004-01-15 18:34:11 +0000186<div class="doc_text">
187
188<p>Now that we have a way to compile our new pass, we just have to write it.
189Start out with:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000190
191<pre>
192<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
193<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
194</pre>
195
Misha Brukmanc5402402004-01-15 18:34:11 +0000196<p>Which are needed because we are writing a <tt><a
197href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Pass.html">Pass</a></tt>, and
198we are operating on <tt><a
199href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Function.html">Function</a></tt>'s.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000200
Misha Brukmanc5402402004-01-15 18:34:11 +0000201<p>Next we have:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000202
203<pre>
204<b>namespace</b> {
Misha Brukmanc5402402004-01-15 18:34:11 +0000205</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000206
Misha Brukmanc5402402004-01-15 18:34:11 +0000207<p>... which starts out an anonymous namespace. Anonymous namespaces are to C++
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000208what the "<tt>static</tt>" keyword is to C (at global scope). It makes the
209things declared inside of the anonymous namespace only visible to the current
210file. If you're not familiar with them, consult a decent C++ book for more
Misha Brukmanc5402402004-01-15 18:34:11 +0000211information.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000212
Misha Brukmanc5402402004-01-15 18:34:11 +0000213<p>Next, we declare our pass itself:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000214
215<pre>
216 <b>struct</b> Hello : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
217</pre><p>
218
Misha Brukmanc5402402004-01-15 18:34:11 +0000219<p>This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a
220href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1FunctionPass.html">FunctionPass</a></tt>.
Chris Lattnerd6ea9262002-09-09 03:48:46 +0000221The different builtin pass subclasses are described in detail <a
Misha Brukmanc5402402004-01-15 18:34:11 +0000222href="#passtype">later</a>, but for now, know that <a
223href="#FunctionPass"><tt>FunctionPass</tt></a>'s operate a function at a
224time.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000225
226<pre>
Misha Brukmanc5402402004-01-15 18:34:11 +0000227 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000228 std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
229 <b>return false</b>;
230 }
231 }; <i>// end of struct Hello</i>
232</pre>
233
Misha Brukmanc5402402004-01-15 18:34:11 +0000234<p>We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method,
235which overloads an abstract virtual method inherited from <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000236href="#FunctionPass"><tt>FunctionPass</tt></a>. This is where we are supposed
237to do our thing, so we just print out our message with the name of each
Misha Brukmanc5402402004-01-15 18:34:11 +0000238function.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000239
240<pre>
241 RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
242} <i>// end of anonymous namespace</i>
Misha Brukmanc5402402004-01-15 18:34:11 +0000243</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000244
Misha Brukmanc5402402004-01-15 18:34:11 +0000245<p>Lastly, we register our class <tt>Hello</tt>, giving it a command line
246argument "<tt>hello</tt>", and a name "<tt>Hello World Pass</tt>". There are
247several different ways of <a href="#registration">registering your pass</a>,
248depending on what it is to be used for. For "optimizations" we use the
249<tt>RegisterOpt</tt> template.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000250
Misha Brukmanc5402402004-01-15 18:34:11 +0000251<p>As a whole, the <tt>.cpp</tt> file looks like:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000252
253<pre>
254<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
255<b>#include</b> "<a href="http://llvm.cs.uiuc.edu/doxygen/Function_8h-source.html">llvm/Function.h</a>"
256
257<b>namespace</b> {
258 <b>struct Hello</b> : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
Misha Brukmanc5402402004-01-15 18:34:11 +0000259 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000260 std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
261 <b>return false</b>;
262 }
263 };
264
265 RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
266}
Misha Brukmanc5402402004-01-15 18:34:11 +0000267</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000268
Misha Brukmanc5402402004-01-15 18:34:11 +0000269<p>Now that it's all together, compile the file with a simple "<tt>gmake</tt>"
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000270command in the local directory and you should get a new
271"<tt>lib/Debug/libhello.so</tt> file. Note that everything in this file is
272contained in an anonymous namespace: this reflects the fact that passes are self
273contained units that do not need external interfaces (although they can have
Misha Brukmanc5402402004-01-15 18:34:11 +0000274them) to be useful.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000275
Misha Brukmanc5402402004-01-15 18:34:11 +0000276</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000277
278<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000279<div class="doc_subsection">
280 <a name="running">Running a pass with <tt>opt</tt> or <tt>analyze</tt></a>
281</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000282
Misha Brukmanc5402402004-01-15 18:34:11 +0000283<div class="doc_text">
284
285<p>Now that you have a brand new shiny <tt>.so</tt> file, we can use the
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000286<tt>opt</tt> command to run an LLVM program through your pass. Because you
287registered your pass with the <tt>RegisterOpt</tt> template, you will be able to
Misha Brukmanc5402402004-01-15 18:34:11 +0000288use the <tt>opt</tt> tool to access it, once loaded.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000289
Misha Brukmanc5402402004-01-15 18:34:11 +0000290<p>To test it, follow the example at the end of the <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000291href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to
292LLVM. We can now run the bytecode file (<tt>hello.bc</tt>) for the program
293through our transformation like this (or course, any bytecode file will
Misha Brukmanc5402402004-01-15 18:34:11 +0000294work):</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000295
296<pre>
297$ opt -load ../../../lib/Debug/libhello.so -hello &lt; hello.bc &gt; /dev/null
298Hello: __main
299Hello: puts
300Hello: main
Misha Brukmanc5402402004-01-15 18:34:11 +0000301</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000302
Misha Brukmanc5402402004-01-15 18:34:11 +0000303<p>The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your
304pass as a shared object, which makes '<tt>-hello</tt>' a valid command line
305argument (which is one reason you need to <a href="#registration">register your
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000306pass</a>). Because the hello pass does not modify the program in any
307interesting way, we just throw away the result of <tt>opt</tt> (sending it to
Misha Brukmanc5402402004-01-15 18:34:11 +0000308<tt>/dev/null</tt>).</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000309
Misha Brukmanc5402402004-01-15 18:34:11 +0000310<p>To see what happened to the other string you registered, try running
311<tt>opt</tt> with the <tt>--help</tt> option:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000312
313<pre>
314$ opt -load ../../../lib/Debug/libhello.so --help
315OVERVIEW: llvm .bc -&gt; .bc modular optimizer
316
317USAGE: opt [options] &lt;input bytecode&gt;
318
319OPTIONS:
320 Optimizations available:
321...
322 -funcresolve - Resolve Functions
323 -gcse - Global Common Subexpression Elimination
324 -globaldce - Dead Global Elimination
325 <b>-hello - Hello World Pass</b>
Chris Lattner065a6162003-09-10 05:29:43 +0000326 -indvars - Canonicalize Induction Variables
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000327 -inline - Function Integration/Inlining
328 -instcombine - Combine redundant instructions
329...
Misha Brukmanc5402402004-01-15 18:34:11 +0000330</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000331
Misha Brukmanc5402402004-01-15 18:34:11 +0000332<p>The pass name get added as the information string for your pass, giving some
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000333documentation to users of <tt>opt</tt>. Now that you have a working pass, you
334would go ahead and make it do the cool transformations you want. Once you get
335it all working and tested, it may become useful to find out how fast your pass
336is. The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command
337line option (<tt>--time-passes</tt>) that allows you to get information about
338the execution time of your pass along with the other passes you queue up. For
Misha Brukmanc5402402004-01-15 18:34:11 +0000339example:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000340
341<pre>
342$ opt -load ../../../lib/Debug/libhello.so -hello -time-passes &lt; hello.bc &gt; /dev/null
343Hello: __main
344Hello: puts
345Hello: main
346===============================================================================
347 ... Pass execution timing report ...
348===============================================================================
349 Total Execution Time: 0.02 seconds (0.0479059 wall clock)
350
351 ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name ---
352 0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bytecode Writer
353 0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction
354 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier
355 <b> 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass</b>
356 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL
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>As you can see, our implementation above is pretty fast :). The additional
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000360passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify
361that the LLVM emitted by your pass is still valid and well formed LLVM, which
Misha Brukmanc5402402004-01-15 18:34:11 +0000362hasn't been broken somehow.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000363
Misha Brukmanc5402402004-01-15 18:34:11 +0000364<p>Now that you have seen the basics of the mechanics behind passes, we can talk
365about some more details of how they work and how to use them.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000366
Misha Brukmanc5402402004-01-15 18:34:11 +0000367</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000368
369<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000370<div class="doc_section">
371 <a name="passtype">Pass classes and requirements</a>
372</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000373<!-- *********************************************************************** -->
374
Misha Brukmanc5402402004-01-15 18:34:11 +0000375<div class="doc_text">
376
377<p>One of the first things that you should do when designing a new pass is to
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000378decide what class you should subclass for your pass. The <a
379href="#basiccode">Hello World</a> example uses the <tt><a
380href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we
381did not discuss why or when this should occur. Here we talk about the classes
Misha Brukmanc5402402004-01-15 18:34:11 +0000382available, from the most general to the most specific.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000383
Misha Brukmanc5402402004-01-15 18:34:11 +0000384<p>When choosing a superclass for your Pass, you should choose the <b>most
Chris Lattner79910702002-08-22 19:21:04 +0000385specific</b> class possible, while still being able to meet the requirements
Misha Brukman5560c9d2003-08-18 14:43:39 +0000386listed. This gives the LLVM Pass Infrastructure information necessary to
Chris Lattner79910702002-08-22 19:21:04 +0000387optimize how passes are run, so that the resultant compiler isn't unneccesarily
Misha Brukmanc5402402004-01-15 18:34:11 +0000388slow.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000389
Misha Brukmanc5402402004-01-15 18:34:11 +0000390</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000391
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000392<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000393<div class="doc_subsection">
394 <a name="ImmutablePass">The <tt>ImmutablePass</tt> class</a>
395</div>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000396
Misha Brukmanc5402402004-01-15 18:34:11 +0000397<div class="doc_text">
398
399<p>The most plain and boring type of pass is the "<tt><a
400href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1ImmutablePass.html">ImmutablePass</a></tt>"
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000401class. This pass type is used for passes that do not have to be run, do not
402change state, and never need to be updated. This is not a normal type of
403transformation or analysis, but can provide information about the current
Misha Brukmanc5402402004-01-15 18:34:11 +0000404compiler configuration.</p>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000405
Misha Brukmanc5402402004-01-15 18:34:11 +0000406<p>Although this pass class is very infrequently used, it is important for
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000407providing information about the current target machine being compiled for, and
Misha Brukmanc5402402004-01-15 18:34:11 +0000408other static information that can affect the various transformations.</p>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000409
Misha Brukmanc5402402004-01-15 18:34:11 +0000410<p><tt>ImmutablePass</tt>es never invalidate other transformations, are never
411invalidated, and are never "run".</p>
Chris Lattnerf004f9a2002-09-25 22:31:38 +0000412
Misha Brukmanc5402402004-01-15 18:34:11 +0000413</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000414
415<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000416<div class="doc_subsection">
417 <a name="Pass">The <tt>Pass</tt> class</a>
418</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000419
Misha Brukmanc5402402004-01-15 18:34:11 +0000420<div class="doc_text">
421
422<p>The "<tt><a
423href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Pass.html">Pass</a></tt>"
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000424class is the most general of all superclasses that you can use. Deriving from
425<tt>Pass</tt> indicates that your pass uses the entire program as a unit,
426refering to function bodies in no predictable order, or adding and removing
427functions. Because nothing is known about the behavior of direct <tt>Pass</tt>
Misha Brukmanc5402402004-01-15 18:34:11 +0000428subclasses, no optimization can be done for their execution.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000429
Misha Brukmanc5402402004-01-15 18:34:11 +0000430<p>To write a correct <tt>Pass</tt> subclass, derive from <tt>Pass</tt> and
431overload the <tt>run</tt> method with the following signature:</p>
432
433</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000434
435<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000436<div class="doc_subsubsection">
437 <a name="run">The <tt>run</tt> method</a>
438</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000439
Misha Brukmanc5402402004-01-15 18:34:11 +0000440<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000441
442<pre>
443 <b>virtual bool</b> run(Module &amp;M) = 0;
Misha Brukmanc5402402004-01-15 18:34:11 +0000444</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000445
Misha Brukmanc5402402004-01-15 18:34:11 +0000446<p>The <tt>run</tt> method performs the interesting work of the pass, and should
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000447return true if the module was modified by the transformation, false
Misha Brukmanc5402402004-01-15 18:34:11 +0000448otherwise.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000449
Misha Brukmanc5402402004-01-15 18:34:11 +0000450</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000451
452<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000453<div class="doc_subsection">
454 <a name="FunctionPass">The <tt>FunctionPass</tt> class</a>
455</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000456
Misha Brukmanc5402402004-01-15 18:34:11 +0000457<div class="doc_text">
458
459<p>In contrast to direct <tt>Pass</tt> subclasses, direct <tt><a
460href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1Pass.html">FunctionPass</a></tt>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000461subclasses do have a predictable, local behavior that can be expected by the
462system. All <tt>FunctionPass</tt> execute on each function in the program
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000463independent of all of the other functions in the program.
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000464<tt>FunctionPass</tt>'s do not require that they are executed in a particular
Misha Brukmanc5402402004-01-15 18:34:11 +0000465order, and <tt>FunctionPass</tt>'s do not modify external functions.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000466
Misha Brukmanc5402402004-01-15 18:34:11 +0000467<p>To be explicit, <tt>FunctionPass</tt> subclasses are not allowed to:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000468
469<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +0000470<li>Modify a Function other than the one currently being processed.</li>
471<li>Add or remove Function's from the current Module.</li>
472<li>Add or remove global variables from the current Module.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000473<li>Maintain state across invocations of
Misha Brukmanc5402402004-01-15 18:34:11 +0000474 <a href="#runOnFunction"><tt>runOnFunction</tt></a> (including global data)</li>
475</ol>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000476
Misha Brukmanc5402402004-01-15 18:34:11 +0000477<p>Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000478href="#basiccode">Hello World</a> pass for example). <tt>FunctionPass</tt>'s
479may overload three virtual methods to do their work. All of these methods
Misha Brukmanc5402402004-01-15 18:34:11 +0000480should return true if they modified the program, or false if they didn't.</p>
481
482</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000483
484<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000485<div class="subsubsection">
486 <a name="doInitialization_mod">The <tt>doInitialization(Module &amp;)</tt>
487 method</a>
488</div>
489
490<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000491
492<pre>
493 <b>virtual bool</b> doInitialization(Module &amp;M);
Misha Brukmanc5402402004-01-15 18:34:11 +0000494</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000495
Misha Brukmanc5402402004-01-15 18:34:11 +0000496<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000497<tt>FunctionPass</tt>'s are not allowed to do. They can add and remove
Chris Lattnerd0713f92002-09-12 17:06:43 +0000498functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
499is designed to do simple initialization type of stuff that does not depend on
500the functions being processed. The <tt>doInitialization</tt> method call is not
501scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanc5402402004-01-15 18:34:11 +0000502fast).</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000503
Misha Brukmanc5402402004-01-15 18:34:11 +0000504<p>A good example of how this method should be used is the <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000505href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a>
506pass. This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into
Misha Brukmanef6a6a62003-08-21 22:14:26 +0000507platform dependent <tt>malloc()</tt> and <tt>free()</tt> function calls. It
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000508uses the <tt>doInitialization</tt> method to get a reference to the malloc and
Misha Brukmanc5402402004-01-15 18:34:11 +0000509free functions that it needs, adding prototypes to the module if necessary.</p>
510
511</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000512
513<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000514<div class="subsubsection">
515 <a name="runOnFunction">The <tt>runOnFunction</tt> method</a>
516</div>
517
518<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000519
520<pre>
521 <b>virtual bool</b> runOnFunction(Function &amp;F) = 0;
522</pre><p>
523
Misha Brukmanc5402402004-01-15 18:34:11 +0000524<p>The <tt>runOnFunction</tt> method must be implemented by your subclass to do
525the transformation or analysis work of your pass. As usual, a true value should
526be returned if the function is modified.</p>
527
528</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000529
530<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000531<div class="subsubsection">
532 <a name="doFinalization_mod">The <tt>doFinalization(Module
533 &amp;)</tt> method</a>
534</div>
535
536<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000537
538<pre>
539 <b>virtual bool</b> doFinalization(Module &amp;M);
Misha Brukmanc5402402004-01-15 18:34:11 +0000540</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000541
Misha Brukmanc5402402004-01-15 18:34:11 +0000542<p>The <tt>doFinalization</tt> method is an infrequently used method that is
543called when the pass framework has finished calling <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000544href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
Misha Brukmanc5402402004-01-15 18:34:11 +0000545program being compiled.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000546
Misha Brukmanc5402402004-01-15 18:34:11 +0000547</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000548
549<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000550<div class="doc_subsection">
551 <a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
552</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000553
Misha Brukmanc5402402004-01-15 18:34:11 +0000554<div class="doc_text">
555
556<p><tt>BasicBlockPass</tt>'s are just like <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000557href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
558their scope of inspection and modification to a single basic block at a time.
Misha Brukmanc5402402004-01-15 18:34:11 +0000559As such, they are <b>not</b> allowed to do any of the following:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000560
561<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +0000562<li>Modify or inspect any basic blocks outside of the current one</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000563<li>Maintain state across invocations of
Misha Brukmanc5402402004-01-15 18:34:11 +0000564 <a href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a></li>
565<li>Modify the constrol flow graph (by altering terminator instructions)</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000566<li>Any of the things verboten for
Misha Brukmanc5402402004-01-15 18:34:11 +0000567 <a href="#FunctionPass"><tt>FunctionPass</tt></a>es.</li>
568</ol>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000569
Misha Brukmanc5402402004-01-15 18:34:11 +0000570<p><tt>BasicBlockPass</tt>es are useful for traditional local and "peephole"
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000571optimizations. They may override the same <a
Chris Lattnerd0713f92002-09-12 17:06:43 +0000572href="#doInitialization_mod"><tt>doInitialization(Module &amp;)</tt></a> and <a
573href="#doFinalization_mod"><tt>doFinalization(Module &amp;)</tt></a> methods that <a
Misha Brukmanc5402402004-01-15 18:34:11 +0000574href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have the following virtual methods that may also be implemented:</p>
575
576</div>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000577
578<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000579<div class="subsubsection">
580 <a name="doInitialization_fn">The <tt>doInitialization(Function
581 &amp;)</tt> method</a>
582</div>
583
584<div class="doc_text">
Chris Lattnerd0713f92002-09-12 17:06:43 +0000585
586<pre>
587 <b>virtual bool</b> doInitialization(Function &amp;F);
Misha Brukmanc5402402004-01-15 18:34:11 +0000588</pre>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000589
Misha Brukmanc5402402004-01-15 18:34:11 +0000590<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattnerd0713f92002-09-12 17:06:43 +0000591<tt>BasicBlockPass</tt>'s are not allowed to do, but that
592<tt>FunctionPass</tt>'s can. The <tt>doInitialization</tt> method is designed
593to do simple initialization type of stuff that does not depend on the
594BasicBlocks being processed. The <tt>doInitialization</tt> method call is not
595scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanc5402402004-01-15 18:34:11 +0000596fast).</p>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000597
Misha Brukmanc5402402004-01-15 18:34:11 +0000598</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000599
600<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000601<div class="subsubsection">
602 <a name="runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
603</div>
604
605<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000606
607<pre>
608 <b>virtual bool</b> runOnBasicBlock(BasicBlock &amp;BB) = 0;
Misha Brukmanc5402402004-01-15 18:34:11 +0000609</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000610
Misha Brukmanc5402402004-01-15 18:34:11 +0000611<p>Override this function to do the work of the <tt>BasicBlockPass</tt>. This
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000612function is not allowed to inspect or modify basic blocks other than the
613parameter, and are not allowed to modify the CFG. A true value must be returned
Misha Brukmanc5402402004-01-15 18:34:11 +0000614if the basic block is modified.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000615
Misha Brukmanc5402402004-01-15 18:34:11 +0000616</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000617
Chris Lattnerd0713f92002-09-12 17:06:43 +0000618<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000619<div class="subsubsection">
620 <a name="doFinalization_fn">The <tt>doFinalization(Function &amp;)</tt>
621 method</a>
622</div>
623
624<div class="doc_text">
Chris Lattnerd0713f92002-09-12 17:06:43 +0000625
626<pre>
627 <b>virtual bool</b> doFinalization(Function &amp;F);
Misha Brukmanc5402402004-01-15 18:34:11 +0000628</pre>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000629
Misha Brukmanc5402402004-01-15 18:34:11 +0000630<p>The <tt>doFinalization</tt> method is an infrequently used method that is
631called when the pass framework has finished calling <a
Chris Lattnerd0713f92002-09-12 17:06:43 +0000632href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a> for every BasicBlock in the
633program being compiled. This can be used to perform per-function
Misha Brukmanc5402402004-01-15 18:34:11 +0000634finalization.</p>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000635
Misha Brukmanc5402402004-01-15 18:34:11 +0000636</div>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000637
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000638<!-- ======================================================================= -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000639<div class="doc_subsection">
640 <a name="MachineFunctionPass">The <tt>MachineFunctionPass</tt> class</a>
641</div>
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000642
Misha Brukmanc5402402004-01-15 18:34:11 +0000643<div class="doc_text">
644
645<p>A <tt>MachineFunctionPass</tt> executes on the machine-dependent
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000646representation of each LLVM function in the program,
647independent of all of the other functions in the program.
648A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all
649the restrictions that apply to a <tt>FunctionPass</tt> also apply to it.
650<tt>MachineFunctionPass</tt>es also have additional restrictions. In
651particular, <tt>MachineFunctionPass</tt>es are not allowed to do any of
Misha Brukmanc5402402004-01-15 18:34:11 +0000652the following:</p>
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000653
654<ol>
Misha Brukmanc5402402004-01-15 18:34:11 +0000655<li>Modify any LLVM Instructions, BasicBlocks or Functions.</li>
656<li>Modify a MachineFunction other than the one currently being processed.</li>
657<li>Add or remove MachineFunctions from the current Module.</li>
658<li>Add or remove global variables from the current Module.</li>
659<li>Maintain state across invocations of <a
660href="#runOnMachineFunction"><tt>runOnMachineFunction</tt></a> (including global
661data)</li>
662</ol>
Brian Gaekecab8b6f2003-07-17 18:53:20 +0000663
Misha Brukmanc5402402004-01-15 18:34:11 +0000664</div>
Chris Lattnerd0713f92002-09-12 17:06:43 +0000665
Brian Gaeke6a33f362003-07-22 20:53:20 +0000666<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000667<div class="subsubsection">
668 <a name="runOnMachineFunction">The <tt>runOnMachineFunction(MachineFunction
669 &amp;MF)</tt> method</a>
670</div>
671
672<div class="doc_text">
Brian Gaeke6a33f362003-07-22 20:53:20 +0000673
674<pre>
675 <b>virtual bool</b> runOnMachineFunction(MachineFunction &amp;MF) = 0;
Misha Brukmanc5402402004-01-15 18:34:11 +0000676</pre>
Brian Gaeke6a33f362003-07-22 20:53:20 +0000677
Misha Brukmanc5402402004-01-15 18:34:11 +0000678<p><tt>runOnMachineFunction</tt> can be considered the main entry point of a
679<tt>MachineFunctionPass</tt>; that is, you should override this method to do the
680work of your <tt>MachineFunctionPass</tt>.</p>
Brian Gaeke6a33f362003-07-22 20:53:20 +0000681
Misha Brukmanc5402402004-01-15 18:34:11 +0000682<p>The <tt>runOnMachineFunction</tt> method is called on every
Brian Gaeke6a33f362003-07-22 20:53:20 +0000683<tt>MachineFunction</tt> in a <tt>Module</tt>, so that the
Misha Brukmanc5402402004-01-15 18:34:11 +0000684<tt>MachineFunctionPass</tt> may perform optimizations on the machine-dependent
685representation of the function. If you want to get at the LLVM <tt>Function</tt>
686for the <tt>MachineFunction</tt> you're working on, use
687<tt>MachineFunction</tt>'s <tt>getFunction()</tt> accessor method -- but
688remember, you may not modify the LLVM <tt>Function</tt> or its contents from a
689<tt>MachineFunctionPass</tt>.</p>
690
691</div>
Brian Gaeke6a33f362003-07-22 20:53:20 +0000692
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000693<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000694<div class="doc_section">
695 <a name="registration">Pass registration</a>
696</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000697<!-- *********************************************************************** -->
698
Misha Brukmanc5402402004-01-15 18:34:11 +0000699<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000700
Misha Brukmanc5402402004-01-15 18:34:11 +0000701<p>In the <a href="#basiccode">Hello World</a> example pass we illustrated how
702pass registration works, and discussed some of the reasons that it is used and
703what it does. Here we discuss how and why passes are registered.</p>
704
705<p>Passes can be registered in several different ways. Depending on the general
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000706classification of the pass, you should use one of the following templates to
Misha Brukmanc5402402004-01-15 18:34:11 +0000707register the pass:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000708
709<ul>
710<li><b><tt>RegisterOpt</tt></b> - This template should be used when you are
711registering a pass that logically should be available for use in the
Misha Brukmanc5402402004-01-15 18:34:11 +0000712'<tt>opt</tt>' utility.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000713
714<li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are
715registering a pass that logically should be available for use in the
Misha Brukmanc5402402004-01-15 18:34:11 +0000716'<tt>analysis</tt>' utility.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000717
718<li><b><tt>RegisterLLC</tt></b> - This template should be used when you are
719registering a pass that logically should be available for use in the
Misha Brukmanc5402402004-01-15 18:34:11 +0000720'<tt>llc</tt>' utility.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000721
722<li><b><tt>RegisterPass</tt></b> - This is the generic form of the
723<tt>Register*</tt> templates that should be used if you want your pass listed by
724multiple or no utilities. This template takes an extra third argument that
725specifies which tools it should be listed in. See the <a
726href="http://llvm.cs.uiuc.edu/doxygen/PassSupport_8h-source.html">PassSupport.h</a>
Misha Brukmanc5402402004-01-15 18:34:11 +0000727file for more information.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000728
Misha Brukmanc5402402004-01-15 18:34:11 +0000729</ul>
730
731<p>Regardless of how you register your pass, you must specify at least two
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000732parameters. The first parameter is the name of the pass that is to be used on
733the command line to specify that the pass should be added to a program (for
734example <tt>opt</tt> or <tt>analyze</tt>). The second argument is the name of
735the pass, which is to be used for the <tt>--help</tt> output of programs, as
Misha Brukmanc5402402004-01-15 18:34:11 +0000736well as for debug output generated by the <tt>--debug-pass</tt> option.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000737
Misha Brukmanc5402402004-01-15 18:34:11 +0000738<p>If you pass is constructed by its default constructor, you only ever have to
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000739pass these two arguments. If, on the other hand, you require other information
740(like target specific information), you must pass an additional argument. This
741argument is a pointer to a function used to create the pass. For an example of
742how this works, look at the <a
743href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations.cpp</a>
Misha Brukmanc5402402004-01-15 18:34:11 +0000744file.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000745
Misha Brukmanc5402402004-01-15 18:34:11 +0000746<p>If a pass is registered to be used by the <tt>analyze</tt> utility, you
747should implement the virtual <tt>print</tt> method:</p>
748
749</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000750
751<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000752<div class="doc_subsubsection">
753 <a name="print">The <tt>print</tt> method</a>
754</div>
755
756<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000757
758<pre>
759 <b>virtual void</b> print(std::ostream &amp;O, <b>const</b> Module *M) <b>const</b>;
Misha Brukmanc5402402004-01-15 18:34:11 +0000760</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000761
Misha Brukmanc5402402004-01-15 18:34:11 +0000762<p>The <tt>print</tt> method must be implemented by "analyses" in order to print
763a human readable version of the analysis results. This is useful for debugging
764an analysis itself, as well as for other people to figure out how an analysis
765works. The <tt>analyze</tt> tool uses this method to generate its output.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000766
Misha Brukmanc5402402004-01-15 18:34:11 +0000767<p>The <tt>ostream</tt> parameter specifies the stream to write the results on,
768and the <tt>Module</tt> parameter gives a pointer to the top level module of the
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000769program that has been analyzed. Note however that this pointer may be null in
770certain circumstances (such as calling the <tt>Pass::dump()</tt> from a
771debugger), so it should only be used to enhance debug output, it should not be
Misha Brukmanc5402402004-01-15 18:34:11 +0000772depended on.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000773
Misha Brukmanc5402402004-01-15 18:34:11 +0000774</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000775
776<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000777<div class="doc_section">
778 <a name="interaction">Specifying interactions between passes</a>
779</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000780<!-- *********************************************************************** -->
781
Misha Brukmanc5402402004-01-15 18:34:11 +0000782<div class="doc_text">
783
784<p>One of the main responsibilities of the <tt>PassManager</tt> is the make sure
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000785that passes interact with each other correctly. Because <tt>PassManager</tt>
786tries to <a href="#passmanager">optimize the execution of passes</a> it must
787know how the passes interact with each other and what dependencies exist between
788the various passes. To track this, each pass can declare the set of passes that
789are required to be executed before the current pass, and the passes which are
Misha Brukmanc5402402004-01-15 18:34:11 +0000790invalidated by the current pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000791
Misha Brukmanc5402402004-01-15 18:34:11 +0000792<p>Typically this functionality is used to require that analysis results are
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000793computed before your pass is run. Running arbitrary transformation passes can
794invalidate the computed analysis results, which is what the invalidation set
795specifies. If a pass does not implement the <tt><a
796href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
Misha Brukmanc5402402004-01-15 18:34:11 +0000797having any prerequisite passes, and invalidating <b>all</b> other passes.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000798
Misha Brukmanc5402402004-01-15 18:34:11 +0000799</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000800
801<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000802<div class="doc_subsubsection">
803 <a name="getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a>
804</div>
805
806<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000807
808<pre>
809 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;Info) <b>const</b>;
Misha Brukmanc5402402004-01-15 18:34:11 +0000810</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000811
Misha Brukmanc5402402004-01-15 18:34:11 +0000812<p>By implementing the <tt>getAnalysisUsage</tt> method, the required and
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000813invalidated sets may be specified for your transformation. The implementation
814should fill in the <tt><a
Misha Brukmanc5402402004-01-15 18:34:11 +0000815href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
816object with information about which passes are required and not invalidated. To
817do this, the following set methods are provided by the <tt><a
818href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
819class:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000820
821<pre>
822 <i>// addRequires - Add the specified pass to the required set for your pass.</i>
823 <b>template</b>&lt;<b>class</b> PassClass&gt;
824 AnalysisUsage &amp;AnalysisUsage::addRequired();
825
826 <i>// addPreserved - Add the specified pass to the set of analyses preserved by
827 // this pass</i>
828 <b>template</b>&lt;<b>class</b> PassClass&gt;
829 AnalysisUsage &amp;AnalysisUsage::addPreserved();
830
831 <i>// setPreservesAll - Call this if the pass does not modify its input at all</i>
832 <b>void</b> AnalysisUsage::setPreservesAll();
833
Chris Lattner8291e042002-10-21 19:57:59 +0000834 <i>// setPreservesCFG - This function should be called by the pass, iff they do not:
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000835 //
836 // 1. Add or remove basic blocks from the function
837 // 2. Modify terminator instructions in any way.
838 //
839 // This is automatically implied for <a href="#BasicBlockPass">BasicBlockPass</a>'s
840 //</i>
Chris Lattner8291e042002-10-21 19:57:59 +0000841 <b>void</b> AnalysisUsage::setPreservesCFG();
Misha Brukmanc5402402004-01-15 18:34:11 +0000842</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000843
Misha Brukmanc5402402004-01-15 18:34:11 +0000844<p>Some examples of how to use these methods are:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000845
846<pre>
847 <i>// This is an example implementation from an analysis, which does not modify
848 // the program at all, yet has a prerequisite.</i>
Misha Brukmanc5402402004-01-15 18:34:11 +0000849 <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 +0000850 AU.setPreservesAll();
Misha Brukmanc5402402004-01-15 18:34:11 +0000851 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 +0000852 }
Misha Brukmanc5402402004-01-15 18:34:11 +0000853</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000854
Misha Brukmanc5402402004-01-15 18:34:11 +0000855<p>and:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000856
857<pre>
858 <i>// This example modifies the program, but does not modify the CFG</i>
859 <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 +0000860 AU.setPreservesCFG();
Misha Brukmanc5402402004-01-15 18:34:11 +0000861 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 +0000862 }
Misha Brukmanc5402402004-01-15 18:34:11 +0000863</pre>
864
865</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000866
867<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000868<div class="doc_subsubsection">
869 <a name="getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> method</a>
870</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000871
Misha Brukmanc5402402004-01-15 18:34:11 +0000872<div class="doc_text">
873
874<p>The <tt>Pass::getAnalysis&lt;&gt;</tt> method is inherited by your class,
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000875providing you with access to the passes that you declared that you required with
876the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method. It takes
877a single template argument that specifies which pass class you want, and returns
Misha Brukmanc5402402004-01-15 18:34:11 +0000878a reference to that pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000879
880<pre>
881 <b>template</b>&lt;<b>typename</b> PassClass&gt;
882 AnalysisType &amp;getAnalysis();
Misha Brukmanc5402402004-01-15 18:34:11 +0000883</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000884
Misha Brukmanc5402402004-01-15 18:34:11 +0000885<p>This method call returns a reference to the pass desired. You may get a
886runtime assertion failure if you attempt to get an analysis that you did not
887declare as required in your <a
888href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> implementation. This
889method can be called by your <tt>run*</tt> method implementation, or by any
890other local method invoked by your <tt>run*</tt> method.</p>
891
892</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +0000893
Chris Lattner79910702002-08-22 19:21:04 +0000894<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000895<div class="doc_section">
896 <a name="analysisgroup">Implementing Analysis Groups</a>
897</div>
Chris Lattner79910702002-08-22 19:21:04 +0000898<!-- *********************************************************************** -->
899
Misha Brukmanc5402402004-01-15 18:34:11 +0000900<div class="doc_text">
901
902<p>Now that we understand the basics of how passes are defined, how the are
903used, and how they are required from other passes, it's time to get a little bit
Chris Lattner79910702002-08-22 19:21:04 +0000904fancier. All of the pass relationships that we have seen so far are very
905simple: one pass depends on one other specific pass to be run before it can run.
906For many applications, this is great, for others, more flexibility is
Misha Brukmanc5402402004-01-15 18:34:11 +0000907required.</p>
Chris Lattner79910702002-08-22 19:21:04 +0000908
Misha Brukmanc5402402004-01-15 18:34:11 +0000909<p>In particular, some analyses are defined such that there is a single simple
Chris Lattner79910702002-08-22 19:21:04 +0000910interface to the analysis results, but multiple ways of calculating them.
911Consider alias analysis for example. The most trivial alias analysis returns
912"may alias" for any alias query. The most sophisticated analysis a
913flow-sensitive, context-sensitive interprocedural analysis that can take a
914significant amount of time to execute (and obviously, there is a lot of room
915between these two extremes for other implementations). To cleanly support
916situations like this, the LLVM Pass Infrastructure supports the notion of
Misha Brukmanc5402402004-01-15 18:34:11 +0000917Analysis Groups.</p>
918
919</div>
Chris Lattner79910702002-08-22 19:21:04 +0000920
921<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000922<div class="subsubsection">
923 <a name="agconcepts">Analysis Group Concepts</a>
924</div>
Chris Lattner79910702002-08-22 19:21:04 +0000925
Misha Brukmanc5402402004-01-15 18:34:11 +0000926<div class="doc_text">
927
928<p>An Analysis Group is a single simple interface that may be implemented by
Chris Lattner79910702002-08-22 19:21:04 +0000929multiple different passes. Analysis Groups can be given human readable names
930just like passes, but unlike passes, they need not derive from the <tt>Pass</tt>
931class. An analysis group may have one or more implementations, one of which is
Misha Brukmanc5402402004-01-15 18:34:11 +0000932the "default" implementation.</p>
Chris Lattner79910702002-08-22 19:21:04 +0000933
Misha Brukmanc5402402004-01-15 18:34:11 +0000934<p>Analysis groups are used by client passes just like other passes are: the
Chris Lattner79910702002-08-22 19:21:04 +0000935<tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods.
936In order to resolve this requirement, the <a href="#passmanager">PassManager</a>
937scans the available passes to see if any implementations of the analysis group
938are available. If none is available, the default implementation is created for
939the pass to use. All standard rules for <A href="#interaction">interaction
Misha Brukmanc5402402004-01-15 18:34:11 +0000940between passes</a> still apply.</p>
Chris Lattner79910702002-08-22 19:21:04 +0000941
Misha Brukmanc5402402004-01-15 18:34:11 +0000942<p>Although <a href="#registration">Pass Registration</a> is optional for normal
Chris Lattner79910702002-08-22 19:21:04 +0000943passes, all analysis group implementations must be registered, and must use the
944<A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the
945implementation pool. Also, a default implementation of the interface
946<b>must</b> be registered with <A
Misha Brukmanc5402402004-01-15 18:34:11 +0000947href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.</p>
Chris Lattner79910702002-08-22 19:21:04 +0000948
Misha Brukmanc5402402004-01-15 18:34:11 +0000949<p>As a concrete example of an Analysis Group in action, consider the <a
950href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>
Chris Lattner79910702002-08-22 19:21:04 +0000951analysis group. The default implementation of the alias analysis interface (the
952<tt><a
953href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt>
954pass) just does a few simple checks that don't require significant analysis to
955compute (such as: two different globals can never alias each other, etc).
956Passes that use the <tt><a
Misha Brukmanc5402402004-01-15 18:34:11 +0000957href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner79910702002-08-22 19:21:04 +0000958interface (for example the <tt><a
Misha Brukmanc5402402004-01-15 18:34:11 +0000959href="http://llvm.cs.uiuc.edu/doxygen/classGCSE.html">gcse</a></tt> pass), do
960not care which implementation of alias analysis is actually provided, they just
961use the designated interface.</p>
Chris Lattner79910702002-08-22 19:21:04 +0000962
Misha Brukmanc5402402004-01-15 18:34:11 +0000963<p>From the user's perspective, commands work just like normal. Issuing the
Chris Lattner79910702002-08-22 19:21:04 +0000964command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be
965instantiated and added to the pass sequence. Issuing the command '<tt>opt
966-somefancyaa -gcse ...</tt>' will cause the <tt>gcse</tt> pass to use the
967<tt>somefancyaa</tt> alias analysis (which doesn't actually exist, it's just a
Misha Brukmanc5402402004-01-15 18:34:11 +0000968hypothetical example) instead.</p>
Chris Lattner79910702002-08-22 19:21:04 +0000969
Misha Brukmanc5402402004-01-15 18:34:11 +0000970</div>
Chris Lattner79910702002-08-22 19:21:04 +0000971
972<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +0000973<div class="subsubsection">
974 <a name="registerag">Using <tt>RegisterAnalysisGroup</tt></a>
975</div>
Chris Lattner79910702002-08-22 19:21:04 +0000976
Misha Brukmanc5402402004-01-15 18:34:11 +0000977<div class="doc_text">
978
979<p>The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
Chris Lattner79910702002-08-22 19:21:04 +0000980group itself as well as add pass implementations to the analysis group. First,
981an analysis should be registered, with a human readable name provided for it.
982Unlike registration of passes, there is no command line argument to be specified
Misha Brukmanc5402402004-01-15 18:34:11 +0000983for the Analysis Group Interface itself, because it is "abstract":</p>
Chris Lattner79910702002-08-22 19:21:04 +0000984
985<pre>
Misha Brukmanc5402402004-01-15 18:34:11 +0000986 <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>");
987</pre>
Chris Lattner79910702002-08-22 19:21:04 +0000988
Misha Brukmanc5402402004-01-15 18:34:11 +0000989<p>Once the analysis is registered, passes can declare that they are valid
990implementations of the interface by using the following code:</p>
Chris Lattner79910702002-08-22 19:21:04 +0000991
992<pre>
993<b>namespace</b> {
994 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
995 RegisterOpt&lt;FancyAA&gt;
996 B("<i>somefancyaa</i>", "<i>A more complex alias analysis implementation</i>");
997
998 //<i> Declare that we implement the AliasAnalysis interface</i>
Misha Brukmanc5402402004-01-15 18:34:11 +0000999 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 +00001000}
Misha Brukmanc5402402004-01-15 18:34:11 +00001001</pre>
Chris Lattner79910702002-08-22 19:21:04 +00001002
Misha Brukmanc5402402004-01-15 18:34:11 +00001003<p>This just shows a class <tt>FancyAA</tt> that is registered normally, then
1004uses the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a
1005href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner79910702002-08-22 19:21:04 +00001006analysis group. Every implementation of an analysis group should join using
1007this template. A single pass may join multiple different analysis groups with
Misha Brukmanc5402402004-01-15 18:34:11 +00001008no problem.</p>
Chris Lattner79910702002-08-22 19:21:04 +00001009
1010<pre>
1011<b>namespace</b> {
1012 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
1013 RegisterOpt&lt;<a href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>&gt;
1014 D("<i>basicaa</i>", "<i>Basic Alias Analysis (default AA impl)</i>");
1015
1016 //<i> Declare that we implement the AliasAnalysis interface</i>
Misha Brukmanc5402402004-01-15 18:34:11 +00001017 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 +00001018}
Misha Brukmanc5402402004-01-15 18:34:11 +00001019</pre>
Chris Lattner79910702002-08-22 19:21:04 +00001020
Misha Brukmanc5402402004-01-15 18:34:11 +00001021<p>Here we show how the default implementation is specified (using the extra
Chris Lattner79910702002-08-22 19:21:04 +00001022argument to the <tt>RegisterAnalysisGroup</tt> template). There must be exactly
1023one default implementation available at all times for an Analysis Group to be
1024used. Here we declare that the <tt><a
1025href="http://llvm.cs.uiuc.edu/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt>
Misha Brukmanc5402402004-01-15 18:34:11 +00001026pass is the default implementation for the interface.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001027
Misha Brukmanc5402402004-01-15 18:34:11 +00001028</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001029
1030<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001031<div class="doc_section">
1032 <a name="passmanager">What PassManager does</a>
1033</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001034<!-- *********************************************************************** -->
1035
Misha Brukmanc5402402004-01-15 18:34:11 +00001036<div class="doc_text">
1037
1038<p>The <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001039href="http://llvm.cs.uiuc.edu/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a>
Misha Brukmanc5402402004-01-15 18:34:11 +00001040<a
1041href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1PassManager.html">class</a>
1042takes a list of passes, ensures their <a href="#interaction">prerequisites</a>
1043are set up correctly, and then schedules passes to run efficiently. All of the
1044LLVM tools that run passes use the <tt>PassManager</tt> for execution of these
1045passes.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001046
Misha Brukmanc5402402004-01-15 18:34:11 +00001047<p>The <tt>PassManager</tt> does two main things to try to reduce the execution
1048time of a series of passes:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001049
1050<ol>
1051<li><b>Share analysis results</b> - The PassManager attempts to avoid
1052recomputing analysis results as much as possible. This means keeping track of
1053which analyses are available already, which analyses get invalidated, and which
1054analyses are needed to be run for a pass. An important part of work is that the
1055<tt>PassManager</tt> tracks the exact lifetime of all analysis results, allowing
1056it to <a href="#releaseMemory">free memory</a> allocated to holding analysis
Misha Brukmanc5402402004-01-15 18:34:11 +00001057results as soon as they are no longer needed.</li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001058
1059<li><b>Pipeline the execution of passes on the program</b> - The
1060<tt>PassManager</tt> attempts to get better cache and memory usage behavior out
1061of a series of passes by pipelining the passes together. This means that, given
1062a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it
1063will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on
1064the first function, then all of the <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001065href="#FunctionPass"><tt>FunctionPass</tt></a>es on the second function,
1066etc... until the entire program has been run through the passes.
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001067
Misha Brukmanc5402402004-01-15 18:34:11 +00001068<p>This improves the cache behavior of the compiler, because it is only touching
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001069the LLVM program representation for a single function at a time, instead of
1070traversing the entire program. It reduces the memory consumption of compiler,
1071because, for example, only one <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001072href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1DominatorSet.html"><tt>DominatorSet</tt></a>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001073needs to be calculated at a time. This also makes it possible some <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001074href="#SMP">interesting enhancements</a> in the future.</p></li>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001075
Misha Brukmanc5402402004-01-15 18:34:11 +00001076</ol>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001077
Misha Brukmanc5402402004-01-15 18:34:11 +00001078<p>The effectiveness of the <tt>PassManager</tt> is influenced directly by how
1079much information it has about the behaviors of the passes it is scheduling. For
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001080example, the "preserved" set is intentionally conservative in the face of an
1081unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.
1082Not implementing when it should be implemented will have the effect of not
Misha Brukmanc5402402004-01-15 18:34:11 +00001083allowing any analysis results to live across the execution of your pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001084
Misha Brukmanc5402402004-01-15 18:34:11 +00001085<p>The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001086options that is useful for debugging pass execution, seeing how things work, and
1087diagnosing when you should be preserving more analyses than you currently are
1088(To get information about all of the variants of the <tt>--debug-pass</tt>
Misha Brukmanc5402402004-01-15 18:34:11 +00001089option, just type '<tt>opt --help-hidden</tt>').</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001090
Misha Brukmanc5402402004-01-15 18:34:11 +00001091<p>By using the <tt>--debug-pass=Structure</tt> option, for example, we can see
1092how our <a href="#basiccode">Hello World</a> pass interacts with other passes.
1093Lets try it out with the <tt>gcse</tt> and <tt>licm</tt> passes:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001094
1095<pre>
1096$ opt -load ../../../lib/Debug/libhello.so -gcse -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
1097Module Pass Manager
1098 Function Pass Manager
1099 Dominator Set Construction
1100 Immediate Dominators Construction
1101 Global Common Subexpression Elimination
1102-- Immediate Dominators Construction
1103-- Global Common Subexpression Elimination
1104 Natural Loop Construction
1105 Loop Invariant Code Motion
1106-- Natural Loop Construction
1107-- Loop Invariant Code Motion
1108 Module Verifier
1109-- Dominator Set Construction
1110-- Module Verifier
1111 Bytecode Writer
1112--Bytecode Writer
Misha Brukmanc5402402004-01-15 18:34:11 +00001113</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001114
Misha Brukmanc5402402004-01-15 18:34:11 +00001115<p>This output shows us when passes are constructed and when the analysis
1116results are known to be dead (prefixed with '<tt>--</tt>'). Here we see that
1117GCSE uses dominator and immediate dominator information to do its job. The LICM
1118pass uses natural loop information, which uses dominator sets, but not immediate
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001119dominators. Because immediate dominators are no longer useful after the GCSE
1120pass, it is immediately destroyed. The dominator sets are then reused to
Misha Brukmanc5402402004-01-15 18:34:11 +00001121compute natural loop information, which is then used by the LICM pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001122
Misha Brukmanc5402402004-01-15 18:34:11 +00001123<p>After the LICM pass, the module verifier runs (which is automatically added
1124by the '<tt>opt</tt>' tool), which uses the dominator set to check that the
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001125resultant LLVM code is well formed. After it finishes, the dominator set
1126information is destroyed, after being computed once, and shared by three
Misha Brukmanc5402402004-01-15 18:34:11 +00001127passes.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001128
Misha Brukmanc5402402004-01-15 18:34:11 +00001129<p>Lets see how this changes when we run the <a href="#basiccode">Hello
1130World</a> pass in between the two passes:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001131
1132<pre>
1133$ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
1134Module Pass Manager
1135 Function Pass Manager
1136 Dominator Set Construction
1137 Immediate Dominators Construction
1138 Global Common Subexpression Elimination
1139<b>-- Dominator Set Construction</b>
1140-- Immediate Dominators Construction
1141-- Global Common Subexpression Elimination
1142<b> Hello World Pass
1143-- Hello World Pass
1144 Dominator Set Construction</b>
1145 Natural Loop Construction
1146 Loop Invariant Code Motion
1147-- Natural Loop Construction
1148-- Loop Invariant Code Motion
1149 Module Verifier
1150-- Dominator Set Construction
1151-- Module Verifier
1152 Bytecode Writer
1153--Bytecode Writer
1154Hello: __main
1155Hello: puts
1156Hello: main
Misha Brukmanc5402402004-01-15 18:34:11 +00001157</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001158
Misha Brukmanc5402402004-01-15 18:34:11 +00001159<p>Here we see that the <a href="#basiccode">Hello World</a> pass has killed the
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001160Dominator Set pass, even though it doesn't modify the code at all! To fix this,
1161we need to add the following <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001162href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method to our pass:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001163
1164<pre>
1165 <i>// We don't modify the program, so we preserve all analyses</i>
1166 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
1167 AU.setPreservesAll();
1168 }
Misha Brukmanc5402402004-01-15 18:34:11 +00001169</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001170
Misha Brukmanc5402402004-01-15 18:34:11 +00001171<p>Now when we run our pass, we get this output:</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001172
1173<pre>
Misha Brukmanc5402402004-01-15 18:34:11 +00001174$ opt -load ../../../lib/Debug/libhello.so -gcse -hello -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001175Pass Arguments: -gcse -hello -licm
1176Module Pass Manager
1177 Function Pass Manager
1178 Dominator Set Construction
1179 Immediate Dominators Construction
1180 Global Common Subexpression Elimination
1181-- Immediate Dominators Construction
1182-- Global Common Subexpression Elimination
1183 Hello World Pass
1184-- Hello World Pass
1185 Natural Loop Construction
1186 Loop Invariant Code Motion
1187-- Loop Invariant Code Motion
1188-- Natural Loop Construction
1189 Module Verifier
1190-- Dominator Set Construction
1191-- Module Verifier
1192 Bytecode Writer
1193--Bytecode Writer
1194Hello: __main
1195Hello: puts
1196Hello: main
Misha Brukmanc5402402004-01-15 18:34:11 +00001197</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001198
Misha Brukmanc5402402004-01-15 18:34:11 +00001199<p>Which shows that we don't accidentally invalidate dominator information
1200anymore, and therefore do not have to compute it twice.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001201
Misha Brukmanc5402402004-01-15 18:34:11 +00001202</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001203
1204<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001205<div class="doc_subsubsection">
1206 <a name="releaseMemory">The <tt>releaseMemory</tt> method</a>
1207</div>
1208
1209<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001210
1211<pre>
1212 <b>virtual void</b> releaseMemory();
Misha Brukmanc5402402004-01-15 18:34:11 +00001213</pre>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001214
Misha Brukmanc5402402004-01-15 18:34:11 +00001215<p>The <tt>PassManager</tt> automatically determines when to compute analysis
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001216results, and how long to keep them around for. Because the lifetime of the pass
1217object itself is effectively the entire duration of the compilation process, we
1218need some way to free analysis results when they are no longer useful. The
Misha Brukmanc5402402004-01-15 18:34:11 +00001219<tt>releaseMemory</tt> virtual method is the way to do this.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001220
Misha Brukmanc5402402004-01-15 18:34:11 +00001221<p>If you are writing an analysis or any other pass that retains a significant
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001222amount of state (for use by another pass which "requires" your pass and uses the
1223<a href="#getAnalysis">getAnalysis</a> method) you should implement
1224<tt>releaseMEmory</tt> to, well, release the memory allocated to maintain this
1225internal state. This method is called after the <tt>run*</tt> method for the
Misha Brukmanc5402402004-01-15 18:34:11 +00001226class, before the next call of <tt>run*</tt> in your pass.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001227
Misha Brukmanc5402402004-01-15 18:34:11 +00001228</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001229
1230<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001231<div class="doc_section">
1232 <a name="debughints">Using GDB with dynamically loaded passes</a>
1233</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001234<!-- *********************************************************************** -->
1235
Misha Brukmanc5402402004-01-15 18:34:11 +00001236<div class="doc_text">
1237
1238<p>Unfortunately, using GDB with dynamically loaded passes is not as easy as it
Chris Lattner480e2ef2002-09-06 02:02:58 +00001239should be. First of all, you can't set a breakpoint in a shared object that has
1240not been loaded yet, and second of all there are problems with inlined functions
1241in shared objects. Here are some suggestions to debugging your pass with
Misha Brukmanc5402402004-01-15 18:34:11 +00001242GDB.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001243
Misha Brukmanc5402402004-01-15 18:34:11 +00001244<p>For sake of discussion, I'm going to assume that you are debugging a
Chris Lattner480e2ef2002-09-06 02:02:58 +00001245transformation invoked by <tt>opt</tt>, although nothing described here depends
Misha Brukmanc5402402004-01-15 18:34:11 +00001246on that.</p>
1247
1248</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001249
1250<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001251<div class="doc_subsubsection">
1252 <a name="breakpoint">Setting a breakpoint in your pass</a>
1253</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001254
Misha Brukmanc5402402004-01-15 18:34:11 +00001255<div class="doc_text">
1256
1257<p>First thing you do is start <tt>gdb</tt> on the <tt>opt</tt> process:</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001258
1259<pre>
1260$ <b>gdb opt</b>
1261GNU gdb 5.0
1262Copyright 2000 Free Software Foundation, Inc.
1263GDB is free software, covered by the GNU General Public License, and you are
1264welcome to change it and/or distribute copies of it under certain conditions.
1265Type "show copying" to see the conditions.
1266There is absolutely no warranty for GDB. Type "show warranty" for details.
1267This GDB was configured as "sparc-sun-solaris2.6"...
1268(gdb)
Misha Brukmanc5402402004-01-15 18:34:11 +00001269</pre>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001270
Misha Brukmanc5402402004-01-15 18:34:11 +00001271<p>Note that <tt>opt</tt> has a lot of debugging information in it, so it takes
Chris Lattner480e2ef2002-09-06 02:02:58 +00001272time to load. Be patient. Since we cannot set a breakpoint in our pass yet
1273(the shared object isn't loaded until runtime), we must execute the process, and
1274have it stop before it invokes our pass, but after it has loaded the shared
1275object. The most foolproof way of doing this is to set a breakpoint in
1276<tt>PassManager::run</tt> and then run the process with the arguments you
Misha Brukmanc5402402004-01-15 18:34:11 +00001277want:</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001278
1279<pre>
1280(gdb) <b>break PassManager::run</b>
1281Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
Chris Lattnerfafe6302003-10-23 19:48:11 +00001282(gdb) <b>run test.bc -load $(LLVMTOP)/llvm/lib/Debug/[libname].so -[passoption]</b>
1283Starting program: opt test.bc -load $(LLVMTOP)/llvm/lib/Debug/[libname].so -[passoption]
Chris Lattner480e2ef2002-09-06 02:02:58 +00001284Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
128570 bool PassManager::run(Module &amp;M) { return PM-&gt;run(M); }
1286(gdb)
Misha Brukmanc5402402004-01-15 18:34:11 +00001287</pre>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001288
Misha Brukmanc5402402004-01-15 18:34:11 +00001289<p>Once the <tt>opt</tt> stops in the <tt>PassManager::run</tt> method you are
1290now free to set breakpoints in your pass so that you can trace through execution
1291or do other standard debugging stuff.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001292
Misha Brukmanc5402402004-01-15 18:34:11 +00001293</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001294
1295<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001296<div class="doc_subsubsection">
1297 <a name="debugmisc">Miscellaneous Problems</a>
1298</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001299
Misha Brukmanc5402402004-01-15 18:34:11 +00001300<div class="doc_text">
1301
1302<p>Once you have the basics down, there are a couple of problems that GDB has,
1303some with solutions, some without.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001304
1305<ul>
1306<li>Inline functions have bogus stack information. In general, GDB does a
1307pretty good job getting stack traces and stepping through inline functions.
1308When a pass is dynamically loaded however, it somehow completely loses this
1309capability. The only solution I know of is to de-inline a function (move it
Misha Brukmanc5402402004-01-15 18:34:11 +00001310from the body of a class to a .cpp file).</li>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001311
1312<li>Restarting the program breaks breakpoints. After following the information
1313above, you have succeeded in getting some breakpoints planted in your pass. Nex
1314thing you know, you restart the program (i.e., you type '<tt>run</tt>' again),
1315and you start getting errors about breakpoints being unsettable. The only way I
1316have found to "fix" this problem is to <tt>delete</tt> the breakpoints that are
1317already set in your pass, run the program, and re-set the breakpoints once
Misha Brukmanc5402402004-01-15 18:34:11 +00001318execution stops in <tt>PassManager::run</tt>.</li>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001319
1320</ul>
1321
Misha Brukmanc5402402004-01-15 18:34:11 +00001322<p>Hopefully these tips will help with common case debugging situations. If
1323you'd like to contribute some tips of your own, just contact <a
1324href="mailto:sabre@nondot.org">Chris</a>.</p>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001325
Misha Brukmanc5402402004-01-15 18:34:11 +00001326</div>
Chris Lattner480e2ef2002-09-06 02:02:58 +00001327
1328<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001329<div class="doc_section">
1330 <a name="future">Future extensions planned</a>
1331</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001332<!-- *********************************************************************** -->
1333
Misha Brukmanc5402402004-01-15 18:34:11 +00001334<div class="doc_text">
1335
1336<p>Although the LLVM Pass Infrastructure is very capable as it stands, and does
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001337some nifty stuff, there are things we'd like to add in the future. Here is
Misha Brukmanc5402402004-01-15 18:34:11 +00001338where we are going:</p>
1339
1340</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001341
1342<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001343<div class="subsubsection">
1344 <a name="SMP">Multithreaded LLVM</a>
1345</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001346
Misha Brukmanc5402402004-01-15 18:34:11 +00001347<div class="doc_text">
1348
1349<p>Multiple CPU machines are becoming more common and compilation can never be
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001350fast enough: obviously we should allow for a multithreaded compiler. Because of
1351the semantics defined for passes above (specifically they cannot maintain state
1352across invocations of their <tt>run*</tt> methods), a nice clean way to
1353implement a multithreaded compiler would be for the <tt>PassManager</tt> class
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001354to create multiple instances of each pass object, and allow the separate
Misha Brukmanc5402402004-01-15 18:34:11 +00001355instances to be hacking on different parts of the program at the same time.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001356
Misha Brukmanc5402402004-01-15 18:34:11 +00001357<p>This implementation would prevent each of the passes from having to implement
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001358multithreaded constructs, requiring only the LLVM core to have locking in a few
1359places (for global resources). Although this is a simple extension, we simply
1360haven't had time (or multiprocessor machines, thus a reason) to implement this.
Misha Brukmanc5402402004-01-15 18:34:11 +00001361Despite that, we have kept the LLVM passes SMP ready, and you should too.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001362
Misha Brukmanc5402402004-01-15 18:34:11 +00001363</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001364
1365<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001366<div class="subsubsection">
1367 <a name="ModuleSource">A new <tt>ModuleSource</tt> interface</a>
1368</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001369
Misha Brukmanc5402402004-01-15 18:34:11 +00001370<div class="doc_text">
1371
1372<p>Currently, the <tt>PassManager</tt>'s <tt>run</tt> method takes a <tt><a
1373href="http://llvm.cs.uiuc.edu/doxygen/structllvm_1_1Module.html">Module</a></tt>
1374as input, and runs all of the passes on this module. The problem with this
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001375approach is that none of the <tt>PassManager</tt> features can be used for
1376timing and debugging the actual <b>loading</b> of the module from disk or
Misha Brukmanc5402402004-01-15 18:34:11 +00001377standard input.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001378
Misha Brukmanc5402402004-01-15 18:34:11 +00001379<p>To solve this problem, eventually the <tt>PassManger</tt> class will accept a
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001380<tt>ModuleSource</tt> object instead of a Module itself. When complete, this
1381will also allow for streaming of functions out of the bytecode representation,
1382allowing us to avoid holding the entire program in memory at once if we only are
Misha Brukmanc5402402004-01-15 18:34:11 +00001383dealing with <a href="#FunctionPass">FunctionPass</a>es.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001384
Misha Brukmanc5402402004-01-15 18:34:11 +00001385<p>As part of a different issue, eventually the bytecode loader will be extended
1386to allow on-demand loading of functions from the bytecode representation, in
1387order to better support the runtime reoptimizer. The bytecode format is already
1388capable of this, the loader just needs to be reworked a bit.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001389
Misha Brukmanc5402402004-01-15 18:34:11 +00001390</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001391
1392<!-- _______________________________________________________________________ -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001393<div class="subsubsection">
1394<a name="PassFunctionPass"><tt>Pass</tt>'s requiring <tt>FunctionPass</tt>'s</a>
1395</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001396
Misha Brukmanc5402402004-01-15 18:34:11 +00001397<div class="doc_text">
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001398
Misha Brukmanc5402402004-01-15 18:34:11 +00001399<p>Currently it is illegal for a <a href="#Pass"><tt>Pass</tt></a> to require a
1400<a href="#FunctionPass"><tt>FunctionPass</tt></a>. This is because there is
1401only one instance of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>
1402object ever created, thus nowhere to store information for all of the functions
1403in the program at the same time. Although this has come up a couple of times
1404before, this has always been worked around by factoring one big complicated pass
1405into a global and an interprocedural part, both of which are distinct. In the
1406future, it would be nice to have this though.</p>
1407
1408<p>Note that it is no problem for a <a
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001409href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a
Misha Brukmanc5402402004-01-15 18:34:11 +00001410href="#Pass"><tt>Pass</tt></a>, only the other way around.</p>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001411
Misha Brukmanc5402402004-01-15 18:34:11 +00001412</div>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001413
1414<!-- *********************************************************************** -->
Misha Brukmanc5402402004-01-15 18:34:11 +00001415<hr>
1416<address>
1417 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1418 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1419 <a href="http://validator.w3.org/check/referer"><img
1420 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
Chris Lattnerc6bb8242002-08-08 20:11:18 +00001421
Misha Brukmanc5402402004-01-15 18:34:11 +00001422 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1423 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
1424 Last modified: $Date$
1425</address>
1426
1427</body>
1428</html>