blob: eaa8606ccce5988bb6bb6d5fa61ced0e4f96c3fa [file] [log] [blame]
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Reid Spencer4a1ab182005-01-11 05:16:23 +00005 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00006 <title>Writing an LLVM Pass</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
8</head>
9<body>
Chris Lattner34ceebe2002-08-08 20:11:18 +000010
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000011<div class="doc_title">
12 Writing an LLVM Pass
13</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +000014
15<ol>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000016 <li><a href="#introduction">Introduction - What is a pass?</a></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000017 <li><a href="#quickstart">Quick Start - Writing hello world</a>
18 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000019 <li><a href="#makefile">Setting up the build environment</a></li>
20 <li><a href="#basiccode">Basic code required</a></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000021 <li><a href="#running">Running a pass with <tt>opt</tt>
Chris Lattner9e4be6f2004-09-18 06:39:35 +000022 or <tt>analyze</tt></a></li>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000023 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000024 <li><a href="#passtype">Pass classes and requirements</a>
25 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000026 <li><a href="#ImmutablePass">The <tt>ImmutablePass</tt> class</a></li>
Chris Lattner3c11c042004-09-20 04:36:29 +000027 <li><a href="#ModulePass">The <tt>ModulePass</tt> class</a>
Chris Lattner34ceebe2002-08-08 20:11:18 +000028 <ul>
Chris Lattner3c11c042004-09-20 04:36:29 +000029 <li><a href="#runOnModule">The <tt>runOnModule</tt> method</a></li>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000030 </ul></li>
Chris Lattner9e4be6f2004-09-18 06:39:35 +000031 <li><a href="#CallGraphSCCPass">The <tt>CallGraphSCCPass</tt> class</a>
32 <ul>
33 <li><a href="#doInitialization_scc">The <tt>doInitialization(Module
34 &amp;)</tt> method</a></li>
35 <li><a href="#runOnSCC">The <tt>runOnSCC</tt> method</a></li>
36 <li><a href="#doFinalization_scc">The <tt>doFinalization(Module
37 &amp;)</tt> method</a></li>
38 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000039 <li><a href="#FunctionPass">The <tt>FunctionPass</tt> class</a>
40 <ul>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000041 <li><a href="#doInitialization_mod">The <tt>doInitialization(Module
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000042 &amp;)</tt> method</a></li>
43 <li><a href="#runOnFunction">The <tt>runOnFunction</tt> method</a></li>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000044 <li><a href="#doFinalization_mod">The <tt>doFinalization(Module
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000045 &amp;)</tt> method</a></li>
46 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000047 <li><a href="#BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
48 <ul>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000049 <li><a href="#doInitialization_fn">The <tt>doInitialization(Function
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000050 &amp;)</tt> method</a></li>
51 <li><a href="#runOnBasicBlock">The <tt>runOnBasicBlock</tt>
52 method</a></li>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000053 <li><a href="#doFinalization_fn">The <tt>doFinalization(Function
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000054 &amp;)</tt> method</a></li>
55 </ul></li>
Brian Gaeke110f07b2003-07-17 18:53:20 +000056 <li><a href="#MachineFunctionPass">The <tt>MachineFunctionPass</tt>
57 class</a>
Brian Gaeke673ef4d2003-07-22 20:53:20 +000058 <ul>
59 <li><a href="#runOnMachineFunction">The
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000060 <tt>runOnMachineFunction(MachineFunction &amp;)</tt> method</a></li>
61 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000062 </ul>
63 <li><a href="#registration">Pass Registration</a>
64 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000065 <li><a href="#print">The <tt>print</tt> method</a></li>
66 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000067 <li><a href="#interaction">Specifying interactions between passes</a>
68 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000069 <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt>
70 method</a></li>
Chris Lattner8e871652004-03-17 21:09:55 +000071 <li><a href="#AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a></li>
72 <li><a href="#AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a></li>
73 <li><a href="#AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a></li>
74 <li><a href="#getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> and <tt>getAnalysisToUpdate&lt;&gt;</tt> methods</a></li>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000075 </ul></li>
Chris Lattner8d8a9282002-08-22 19:21:04 +000076 <li><a href="#analysisgroup">Implementing Analysis Groups</a>
77 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000078 <li><a href="#agconcepts">Analysis Group Concepts</a></li>
79 <li><a href="#registerag">Using <tt>RegisterAnalysisGroup</tt></a></li>
80 </ul></li>
Tanya Lattnerfb966ba2004-11-19 01:25:14 +000081 <li><a href="#passStatistics">Pass Statistics</a>
Chris Lattner34ceebe2002-08-08 20:11:18 +000082 <li><a href="#passmanager">What PassManager does</a>
83 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000084 <li><a href="#releaseMemory">The <tt>releaseMemory</tt> method</a></li>
85 </ul></li>
Jim Laskey575df8b2006-08-04 18:10:12 +000086 <li><a href="#registering">Registering dynamically loaded passes</a>
87 <ul>
88 <li><a href="#registering_existing">Using existing registries</a></li>
89 <li><a href="#registering_new">Creating new registries</a></li>
90 </ul></li>
Chris Lattner8a97c8a2002-09-06 02:02:58 +000091 <li><a href="#debughints">Using GDB with dynamically loaded passes</a>
92 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000093 <li><a href="#breakpoint">Setting a breakpoint in your pass</a></li>
94 <li><a href="#debugmisc">Miscellaneous Problems</a></li>
95 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000096 <li><a href="#future">Future extensions planned</a>
97 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000098 <li><a href="#SMP">Multithreaded LLVM</a></li>
Chris Lattner3c11c042004-09-20 04:36:29 +000099 <li><a href="#PassFunctionPass"><tt>ModulePass</tt>es requiring
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000100 <tt>FunctionPass</tt>es</a></li>
101 </ul></li>
102</ol>
Chris Lattner56d9e222002-08-08 20:23:41 +0000103
Chris Lattner020e1fc2004-05-23 21:07:27 +0000104<div class="doc_author">
Jim Laskey575df8b2006-08-04 18:10:12 +0000105 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>,
106 <a href="mailto:jlaskey@apple.com">Jim Laskey</a></p>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000107</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000108
109<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000110<div class="doc_section">
111 <a name="introduction">Introduction - What is a pass?</a>
112</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000113<!-- *********************************************************************** -->
114
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000115<div class="doc_text">
116
117<p>The LLVM Pass Framework is an important part of the LLVM system, because LLVM
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000118passes are where most of the interesting parts of the compiler exist. Passes
119perform the transformations and optimizations that make up the compiler, they
120build the analysis results that are used by these transformations, and they are,
121above all, a structuring technique for compiler code.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000122
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000123<p>All LLVM passes are subclasses of the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000124href="http://llvm.org/doxygen/classllvm_1_1Pass.html">Pass</a></tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000125class, which implement functionality by overriding virtual methods inherited
Chris Lattner3c11c042004-09-20 04:36:29 +0000126from <tt>Pass</tt>. Depending on how your pass works, you should inherit from
127the <tt><a href="#ModulePass">ModulePass</a></tt>, <tt><a
128href="#CallGraphSCCPass">CallGraphSCCPass</a></tt>, <tt><a
129href="#FunctionPass">FunctionPass</a></tt>, or <tt><a
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000130href="#BasicBlockPass">BasicBlockPass</a></tt> classes, which gives the system
131more information about what your pass does, and how it can be combined with
132other passes. One of the main features of the LLVM Pass Framework is that it
133schedules passes to run in an efficient way based on the constraints that your
134pass meets (which are indicated by which class they derive from).</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000135
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000136<p>We start by showing you how to construct a pass, everything from setting up
137the code, to compiling, loading, and executing it. After the basics are down,
138more advanced features are discussed.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000139
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000140</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000141
142<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000143<div class="doc_section">
144 <a name="quickstart">Quick Start - Writing hello world</a>
145</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000146<!-- *********************************************************************** -->
147
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000148<div class="doc_text">
149
150<p>Here we describe how to write the "hello world" of passes. The "Hello" pass
151is designed to simply print out the name of non-external functions that exist in
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000152the program being compiled. It does not modify the program at all, it just
Chris Lattner34ceebe2002-08-08 20:11:18 +0000153inspects it. The source code and files for this pass are available in the LLVM
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000154source tree in the <tt>lib/Transforms/Hello</tt> directory.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000155
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000156</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000157
158<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000159<div class="doc_subsection">
160 <a name="makefile">Setting up the build environment</a>
161</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000162
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000163<div class="doc_text">
164
Reid Spencera1799682004-12-11 05:12:57 +0000165 <p>First, you need to create a new directory somewhere in the LLVM source
166 base. For this example, we'll assume that you made
167 <tt>lib/Transforms/Hello</tt>. Next, you must set up a build script
168 (Makefile) that will compile the source code for the new pass. To do this,
169 copy the following into <tt>Makefile</tt>:</p>
170 <hr/>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000171
Jim Laskey575df8b2006-08-04 18:10:12 +0000172<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000173# Makefile for hello pass
Chris Lattner34ceebe2002-08-08 20:11:18 +0000174
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000175# Path to top level of LLVM heirarchy
Chris Lattner0c5e2932002-08-14 20:07:01 +0000176LEVEL = ../../..
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000177
178# Name of the library to build
Reid Spencer4a1ab182005-01-11 05:16:23 +0000179LIBRARYNAME = Hello
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000180
Reid Spencer4a1ab182005-01-11 05:16:23 +0000181# Make the shared library become a loadable module so the tools can
182# dlopen/dlsym on the resulting library.
Robert Bocchino19cbea12006-01-06 22:49:23 +0000183LOADABLE_MODULE = 1
Reid Spencer4a1ab182005-01-11 05:16:23 +0000184
Reid Spencer197d7bf2006-08-08 01:48:17 +0000185# Tell the build system which LLVM libraries your pass needs. You'll probably
186# need at least LLVMSystem.a, LLVMSupport.a, LLVMCore.a but possibly several
187# others too.
188LLVMLIBS = LLVMCore.a LLVMSupport.a LLVMSystem.a
189
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000190# Include the makefile implementation stuff
191include $(LEVEL)/Makefile.common
Jim Laskey575df8b2006-08-04 18:10:12 +0000192</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000193
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000194<p>This makefile specifies that all of the <tt>.cpp</tt> files in the current
Chris Lattner34ceebe2002-08-08 20:11:18 +0000195directory are to be compiled and linked together into a
Reid Spencer4a1ab182005-01-11 05:16:23 +0000196<tt>Debug/lib/Hello.so</tt> shared object that can be dynamically loaded by
197the <tt>opt</tt> or <tt>analyze</tt> tools via their <tt>-load</tt> options.
198If your operating system uses a suffix other than .so (such as windows or
199Mac OS/X), the appropriate extension will be used.</p>
John Criswell2edea842004-01-26 21:26:54 +0000200
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000201<p>Now that we have the build scripts set up, we just need to write the code for
202the pass itself.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000203
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000204</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000205
206<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000207<div class="doc_subsection">
208 <a name="basiccode">Basic code required</a>
209</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000210
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000211<div class="doc_text">
212
213<p>Now that we have a way to compile our new pass, we just have to write it.
214Start out with:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000215
Jim Laskey575df8b2006-08-04 18:10:12 +0000216<div class="doc_code"><pre>
Reid Spencerca058542006-03-14 05:39:39 +0000217<b>#include</b> "<a href="http://llvm.org/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
218<b>#include</b> "<a href="http://llvm.org/doxygen/Function_8h-source.html">llvm/Function.h</a>"
Jim Laskey575df8b2006-08-04 18:10:12 +0000219</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000220
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000221<p>Which are needed because we are writing a <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000222href="http://llvm.org/doxygen/classllvm_1_1Pass.html">Pass</a></tt>, and
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000223we are operating on <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000224href="http://llvm.org/doxygen/classllvm_1_1Function.html">Function</a></tt>'s.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000225
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000226<p>Next we have:</p>
Jim Laskey575df8b2006-08-04 18:10:12 +0000227<div class="doc_code"><pre>
Jonathan Mantonaff4a6a2004-06-30 18:10:30 +0000228<b>using namespace llvm;</b>
Jim Laskey575df8b2006-08-04 18:10:12 +0000229</pre></div>
Jonathan Mantonaff4a6a2004-06-30 18:10:30 +0000230<p>... which is required because the functions from the include files
231live in the llvm namespace.
232</p>
233
234<p>Next we have:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000235
Jim Laskey575df8b2006-08-04 18:10:12 +0000236<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000237<b>namespace</b> {
Jim Laskey575df8b2006-08-04 18:10:12 +0000238</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000239
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000240<p>... which starts out an anonymous namespace. Anonymous namespaces are to C++
Chris Lattner34ceebe2002-08-08 20:11:18 +0000241what the "<tt>static</tt>" keyword is to C (at global scope). It makes the
242things declared inside of the anonymous namespace only visible to the current
243file. If you're not familiar with them, consult a decent C++ book for more
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000244information.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000245
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000246<p>Next, we declare our pass itself:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000247
Jim Laskey575df8b2006-08-04 18:10:12 +0000248<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000249 <b>struct</b> Hello : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
Jim Laskey575df8b2006-08-04 18:10:12 +0000250</pre></div><p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000251
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000252<p>This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000253href="http://llvm.org/doxygen/classllvm_1_1FunctionPass.html">FunctionPass</a></tt>.
Chris Lattnera818d922002-09-09 03:48:46 +0000254The different builtin pass subclasses are described in detail <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000255href="#passtype">later</a>, but for now, know that <a
256href="#FunctionPass"><tt>FunctionPass</tt></a>'s operate a function at a
257time.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000258
Jim Laskey575df8b2006-08-04 18:10:12 +0000259<div class="doc_code"><pre>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000260 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Chris Lattner34ceebe2002-08-08 20:11:18 +0000261 std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
262 <b>return false</b>;
263 }
264 }; <i>// end of struct Hello</i>
Jim Laskey575df8b2006-08-04 18:10:12 +0000265</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000266
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000267<p>We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method,
268which overloads an abstract virtual method inherited from <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000269href="#FunctionPass"><tt>FunctionPass</tt></a>. This is where we are supposed
270to do our thing, so we just print out our message with the name of each
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000271function.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000272
Jim Laskey575df8b2006-08-04 18:10:12 +0000273<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000274 RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
275} <i>// end of anonymous namespace</i>
Jim Laskey575df8b2006-08-04 18:10:12 +0000276</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000277
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000278<p>Lastly, we register our class <tt>Hello</tt>, giving it a command line
279argument "<tt>hello</tt>", and a name "<tt>Hello World Pass</tt>". There are
280several different ways of <a href="#registration">registering your pass</a>,
281depending on what it is to be used for. For "optimizations" we use the
282<tt>RegisterOpt</tt> template.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000283
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000284<p>As a whole, the <tt>.cpp</tt> file looks like:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000285
Jim Laskey575df8b2006-08-04 18:10:12 +0000286<div class="doc_code"><pre>
Reid Spencerca058542006-03-14 05:39:39 +0000287<b>#include</b> "<a href="http://llvm.org/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
288<b>#include</b> "<a href="http://llvm.org/doxygen/Function_8h-source.html">llvm/Function.h</a>"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000289
Jonathan Mantonaff4a6a2004-06-30 18:10:30 +0000290<b>using namespace llvm;</b>
291
Chris Lattner34ceebe2002-08-08 20:11:18 +0000292<b>namespace</b> {
293 <b>struct Hello</b> : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000294 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Chris Lattner34ceebe2002-08-08 20:11:18 +0000295 std::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
296 <b>return false</b>;
297 }
298 };
299
300 RegisterOpt&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
301}
Jim Laskey575df8b2006-08-04 18:10:12 +0000302</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000303
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000304<p>Now that it's all together, compile the file with a simple "<tt>gmake</tt>"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000305command in the local directory and you should get a new
Reid Spencer4a1ab182005-01-11 05:16:23 +0000306"<tt>Debug/lib/Hello.so</tt> file. Note that everything in this file is
Chris Lattner34ceebe2002-08-08 20:11:18 +0000307contained in an anonymous namespace: this reflects the fact that passes are self
308contained units that do not need external interfaces (although they can have
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000309them) to be useful.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000310
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000311</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000312
313<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000314<div class="doc_subsection">
315 <a name="running">Running a pass with <tt>opt</tt> or <tt>analyze</tt></a>
316</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000317
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000318<div class="doc_text">
319
John Criswell2edea842004-01-26 21:26:54 +0000320<p>Now that you have a brand new shiny shared object file, we can use the
Chris Lattner34ceebe2002-08-08 20:11:18 +0000321<tt>opt</tt> command to run an LLVM program through your pass. Because you
322registered your pass with the <tt>RegisterOpt</tt> template, you will be able to
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000323use the <tt>opt</tt> tool to access it, once loaded.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000324
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000325<p>To test it, follow the example at the end of the <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000326href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to
327LLVM. We can now run the bytecode file (<tt>hello.bc</tt>) for the program
328through our transformation like this (or course, any bytecode file will
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000329work):</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000330
Jim Laskey575df8b2006-08-04 18:10:12 +0000331<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +0000332$ opt -load ../../../Debug/lib/Hello.so -hello &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +0000333Hello: __main
334Hello: puts
335Hello: main
Jim Laskey575df8b2006-08-04 18:10:12 +0000336</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000337
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000338<p>The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your
339pass as a shared object, which makes '<tt>-hello</tt>' a valid command line
340argument (which is one reason you need to <a href="#registration">register your
Chris Lattner34ceebe2002-08-08 20:11:18 +0000341pass</a>). Because the hello pass does not modify the program in any
342interesting way, we just throw away the result of <tt>opt</tt> (sending it to
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000343<tt>/dev/null</tt>).</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000344
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000345<p>To see what happened to the other string you registered, try running
346<tt>opt</tt> with the <tt>--help</tt> option:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000347
Jim Laskey575df8b2006-08-04 18:10:12 +0000348<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +0000349$ opt -load ../../../Debug/lib/Hello.so --help
Chris Lattner34ceebe2002-08-08 20:11:18 +0000350OVERVIEW: llvm .bc -&gt; .bc modular optimizer
351
352USAGE: opt [options] &lt;input bytecode&gt;
353
354OPTIONS:
355 Optimizations available:
356...
357 -funcresolve - Resolve Functions
358 -gcse - Global Common Subexpression Elimination
359 -globaldce - Dead Global Elimination
360 <b>-hello - Hello World Pass</b>
Chris Lattner216c7b82003-09-10 05:29:43 +0000361 -indvars - Canonicalize Induction Variables
Chris Lattner34ceebe2002-08-08 20:11:18 +0000362 -inline - Function Integration/Inlining
363 -instcombine - Combine redundant instructions
364...
Jim Laskey575df8b2006-08-04 18:10:12 +0000365</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000366
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000367<p>The pass name get added as the information string for your pass, giving some
Chris Lattner34ceebe2002-08-08 20:11:18 +0000368documentation to users of <tt>opt</tt>. Now that you have a working pass, you
369would go ahead and make it do the cool transformations you want. Once you get
370it all working and tested, it may become useful to find out how fast your pass
371is. The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command
372line option (<tt>--time-passes</tt>) that allows you to get information about
373the execution time of your pass along with the other passes you queue up. For
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000374example:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000375
Jim Laskey575df8b2006-08-04 18:10:12 +0000376<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +0000377$ opt -load ../../../Debug/lib/Hello.so -hello -time-passes &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +0000378Hello: __main
379Hello: puts
380Hello: main
381===============================================================================
382 ... Pass execution timing report ...
383===============================================================================
384 Total Execution Time: 0.02 seconds (0.0479059 wall clock)
385
386 ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name ---
387 0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bytecode Writer
388 0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction
389 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier
390 <b> 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass</b>
391 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL
Jim Laskey575df8b2006-08-04 18:10:12 +0000392</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000393
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000394<p>As you can see, our implementation above is pretty fast :). The additional
Chris Lattner34ceebe2002-08-08 20:11:18 +0000395passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify
396that the LLVM emitted by your pass is still valid and well formed LLVM, which
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000397hasn't been broken somehow.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000398
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000399<p>Now that you have seen the basics of the mechanics behind passes, we can talk
400about some more details of how they work and how to use them.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000401
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000402</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000403
404<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000405<div class="doc_section">
406 <a name="passtype">Pass classes and requirements</a>
407</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000408<!-- *********************************************************************** -->
409
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000410<div class="doc_text">
411
412<p>One of the first things that you should do when designing a new pass is to
Chris Lattner34ceebe2002-08-08 20:11:18 +0000413decide what class you should subclass for your pass. The <a
414href="#basiccode">Hello World</a> example uses the <tt><a
415href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we
416did not discuss why or when this should occur. Here we talk about the classes
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000417available, from the most general to the most specific.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000418
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000419<p>When choosing a superclass for your Pass, you should choose the <b>most
Chris Lattner8d8a9282002-08-22 19:21:04 +0000420specific</b> class possible, while still being able to meet the requirements
Misha Brukman7eb05a12003-08-18 14:43:39 +0000421listed. This gives the LLVM Pass Infrastructure information necessary to
Chris Lattner8d8a9282002-08-22 19:21:04 +0000422optimize how passes are run, so that the resultant compiler isn't unneccesarily
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000423slow.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000424
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000425</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000426
Chris Lattner9720dda2002-09-25 22:31:38 +0000427<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000428<div class="doc_subsection">
429 <a name="ImmutablePass">The <tt>ImmutablePass</tt> class</a>
430</div>
Chris Lattner9720dda2002-09-25 22:31:38 +0000431
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000432<div class="doc_text">
433
434<p>The most plain and boring type of pass is the "<tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000435href="http://llvm.org/doxygen/classllvm_1_1ImmutablePass.html">ImmutablePass</a></tt>"
Chris Lattner9720dda2002-09-25 22:31:38 +0000436class. This pass type is used for passes that do not have to be run, do not
437change state, and never need to be updated. This is not a normal type of
438transformation or analysis, but can provide information about the current
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000439compiler configuration.</p>
Chris Lattner9720dda2002-09-25 22:31:38 +0000440
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000441<p>Although this pass class is very infrequently used, it is important for
Chris Lattner9720dda2002-09-25 22:31:38 +0000442providing information about the current target machine being compiled for, and
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000443other static information that can affect the various transformations.</p>
Chris Lattner9720dda2002-09-25 22:31:38 +0000444
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000445<p><tt>ImmutablePass</tt>es never invalidate other transformations, are never
446invalidated, and are never "run".</p>
Chris Lattner9720dda2002-09-25 22:31:38 +0000447
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000448</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000449
450<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000451<div class="doc_subsection">
Chris Lattner3c11c042004-09-20 04:36:29 +0000452 <a name="ModulePass">The <tt>ModulePass</tt> class</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000453</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000454
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000455<div class="doc_text">
456
457<p>The "<tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000458href="http://llvm.org/doxygen/classllvm_1_1ModulePass.html">ModulePass</a></tt>"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000459class is the most general of all superclasses that you can use. Deriving from
Chris Lattner3c11c042004-09-20 04:36:29 +0000460<tt>ModulePass</tt> indicates that your pass uses the entire program as a unit,
Chris Lattner34ceebe2002-08-08 20:11:18 +0000461refering to function bodies in no predictable order, or adding and removing
Chris Lattner3c11c042004-09-20 04:36:29 +0000462functions. Because nothing is known about the behavior of <tt>ModulePass</tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000463subclasses, no optimization can be done for their execution.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000464
Chris Lattner3c11c042004-09-20 04:36:29 +0000465<p>To write a correct <tt>ModulePass</tt> subclass, derive from
466<tt>ModulePass</tt> and overload the <tt>runOnModule</tt> method with the
467following signature:</p>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000468
469</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000470
471<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000472<div class="doc_subsubsection">
Chris Lattner3c11c042004-09-20 04:36:29 +0000473 <a name="runOnModule">The <tt>runOnModule</tt> method</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000474</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000475
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000476<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000477
Jim Laskey575df8b2006-08-04 18:10:12 +0000478<div class="doc_code"><pre>
Chris Lattner3c11c042004-09-20 04:36:29 +0000479 <b>virtual bool</b> runOnModule(Module &amp;M) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000480</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000481
John Criswella3f51102005-07-15 19:25:12 +0000482<p>The <tt>runOnModule</tt> method performs the interesting work of the pass.
483It should return true if the module was modified by the transformation and
484false otherwise.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000485
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000486</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000487
488<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000489<div class="doc_subsection">
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000490 <a name="CallGraphSCCPass">The <tt>CallGraphSCCPass</tt> class</a>
491</div>
492
493<div class="doc_text">
494
495<p>The "<tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000496href="http://llvm.org/doxygen/classllvm_1_1CallGraphSCCPass.html">CallGraphSCCPass</a></tt>"
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000497is used by passes that need to traverse the program bottom-up on the call graph
498(callees before callers). Deriving from CallGraphSCCPass provides some
499mechanics for building and traversing the CallGraph, but also allows the system
500to optimize execution of CallGraphSCCPass's. If your pass meets the
501requirements outlined below, and doesn't meet the requirements of a <tt><a
502href="#FunctionPass">FunctionPass</a></tt> or <tt><a
503href="#BasicBlockPass">BasicBlockPass</a></tt>, you should derive from
504<tt>CallGraphSCCPass</tt>.</p>
505
506<p><b>TODO</b>: explain briefly what SCC, Tarjan's algo, and B-U mean.</p>
507
508<p>To be explicit, <tt>CallGraphSCCPass</tt> subclasses are:</p>
509
510<ol>
511
512<li>... <em>not allowed</em> to modify any <tt>Function</tt>s that are not in
513the current SCC.</li>
514
515<li>... <em>allowed</em> to inspect any Function's other than those in the
516current SCC and the direct callees of the SCC.</li>
517
518<li>... <em>required</em> to preserve the current CallGraph object, updating it
519to reflect any changes made to the program.</li>
520
521<li>... <em>not allowed</em> to add or remove SCC's from the current Module,
522though they may change the contents of an SCC.</li>
523
524<li>... <em>allowed</em> to add or remove global variables from the current
525Module.</li>
526
527<li>... <em>allowed</em> to maintain state across invocations of
528 <a href="#runOnSCC"><tt>runOnSCC</tt></a> (including global data).</li>
529</ol>
530
531<p>Implementing a <tt>CallGraphSCCPass</tt> is slightly tricky in some cases
532because it has to handle SCCs with more than one node in it. All of the virtual
533methods described below should return true if they modified the program, or
534false if they didn't.</p>
535
536</div>
537
538<!-- _______________________________________________________________________ -->
539<div class="doc_subsubsection">
540 <a name="doInitialization_scc">The <tt>doInitialization(Module &amp;)</tt>
541 method</a>
542</div>
543
544<div class="doc_text">
545
Jim Laskey575df8b2006-08-04 18:10:12 +0000546<div class="doc_code"><pre>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000547 <b>virtual bool</b> doInitialization(Module &amp;M);
Jim Laskey575df8b2006-08-04 18:10:12 +0000548</pre></div>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000549
550<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
551<tt>CallGraphSCCPass</tt>'s are not allowed to do. They can add and remove
552functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
553is designed to do simple initialization type of stuff that does not depend on
554the SCCs being processed. The <tt>doInitialization</tt> method call is not
555scheduled to overlap with any other pass executions (thus it should be very
556fast).</p>
557
558</div>
559
560<!-- _______________________________________________________________________ -->
561<div class="doc_subsubsection">
562 <a name="runOnSCC">The <tt>runOnSCC</tt> method</a>
563</div>
564
565<div class="doc_text">
566
Jim Laskey575df8b2006-08-04 18:10:12 +0000567<div class="doc_code"><pre>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000568 <b>virtual bool</b> runOnSCC(const std::vector&lt;CallGraphNode *&gt; &amp;SCCM) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000569</pre></div>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000570
571<p>The <tt>runOnSCC</tt> method performs the interesting work of the pass, and
572should return true if the module was modified by the transformation, false
573otherwise.</p>
574
575</div>
576
577<!-- _______________________________________________________________________ -->
578<div class="doc_subsubsection">
579 <a name="doFinalization_scc">The <tt>doFinalization(Module
580 &amp;)</tt> method</a>
581</div>
582
583<div class="doc_text">
584
Jim Laskey575df8b2006-08-04 18:10:12 +0000585<div class="doc_code"><pre>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000586 <b>virtual bool</b> doFinalization(Module &amp;M);
Jim Laskey575df8b2006-08-04 18:10:12 +0000587</pre></div>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000588
589<p>The <tt>doFinalization</tt> method is an infrequently used method that is
590called when the pass framework has finished calling <a
591href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
592program being compiled.</p>
593
594</div>
595
596<!-- ======================================================================= -->
597<div class="doc_subsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000598 <a name="FunctionPass">The <tt>FunctionPass</tt> class</a>
599</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000600
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000601<div class="doc_text">
602
Chris Lattner3c11c042004-09-20 04:36:29 +0000603<p>In contrast to <tt>ModulePass</tt> subclasses, <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000604href="http://llvm.org/doxygen/classllvm_1_1Pass.html">FunctionPass</a></tt>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000605subclasses do have a predictable, local behavior that can be expected by the
606system. All <tt>FunctionPass</tt> execute on each function in the program
Misha Brukmanbe372b92003-08-21 22:14:26 +0000607independent of all of the other functions in the program.
Chris Lattner34ceebe2002-08-08 20:11:18 +0000608<tt>FunctionPass</tt>'s do not require that they are executed in a particular
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000609order, and <tt>FunctionPass</tt>'s do not modify external functions.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000610
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000611<p>To be explicit, <tt>FunctionPass</tt> subclasses are not allowed to:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000612
613<ol>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000614<li>Modify a Function other than the one currently being processed.</li>
615<li>Add or remove Function's from the current Module.</li>
616<li>Add or remove global variables from the current Module.</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000617<li>Maintain state across invocations of
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000618 <a href="#runOnFunction"><tt>runOnFunction</tt></a> (including global data)</li>
619</ol>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000620
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000621<p>Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000622href="#basiccode">Hello World</a> pass for example). <tt>FunctionPass</tt>'s
623may overload three virtual methods to do their work. All of these methods
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000624should return true if they modified the program, or false if they didn't.</p>
625
626</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000627
628<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000629<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000630 <a name="doInitialization_mod">The <tt>doInitialization(Module &amp;)</tt>
631 method</a>
632</div>
633
634<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000635
Jim Laskey575df8b2006-08-04 18:10:12 +0000636<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000637 <b>virtual bool</b> doInitialization(Module &amp;M);
Jim Laskey575df8b2006-08-04 18:10:12 +0000638</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000639
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000640<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattner34ceebe2002-08-08 20:11:18 +0000641<tt>FunctionPass</tt>'s are not allowed to do. They can add and remove
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000642functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
643is designed to do simple initialization type of stuff that does not depend on
644the functions being processed. The <tt>doInitialization</tt> method call is not
645scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000646fast).</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000647
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000648<p>A good example of how this method should be used is the <a
Reid Spencerca058542006-03-14 05:39:39 +0000649href="http://llvm.org/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000650pass. This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into
Misha Brukmanbe372b92003-08-21 22:14:26 +0000651platform dependent <tt>malloc()</tt> and <tt>free()</tt> function calls. It
Chris Lattner34ceebe2002-08-08 20:11:18 +0000652uses the <tt>doInitialization</tt> method to get a reference to the malloc and
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000653free functions that it needs, adding prototypes to the module if necessary.</p>
654
655</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000656
657<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000658<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000659 <a name="runOnFunction">The <tt>runOnFunction</tt> method</a>
660</div>
661
662<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000663
Jim Laskey575df8b2006-08-04 18:10:12 +0000664<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000665 <b>virtual bool</b> runOnFunction(Function &amp;F) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000666</pre></div><p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000667
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000668<p>The <tt>runOnFunction</tt> method must be implemented by your subclass to do
669the transformation or analysis work of your pass. As usual, a true value should
670be returned if the function is modified.</p>
671
672</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000673
674<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000675<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000676 <a name="doFinalization_mod">The <tt>doFinalization(Module
677 &amp;)</tt> method</a>
678</div>
679
680<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000681
Jim Laskey575df8b2006-08-04 18:10:12 +0000682<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000683 <b>virtual bool</b> doFinalization(Module &amp;M);
Jim Laskey575df8b2006-08-04 18:10:12 +0000684</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000685
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000686<p>The <tt>doFinalization</tt> method is an infrequently used method that is
687called when the pass framework has finished calling <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000688href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000689program being compiled.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000690
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000691</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000692
693<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000694<div class="doc_subsection">
695 <a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
696</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000697
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000698<div class="doc_text">
699
700<p><tt>BasicBlockPass</tt>'s are just like <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000701href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
702their scope of inspection and modification to a single basic block at a time.
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000703As such, they are <b>not</b> allowed to do any of the following:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000704
705<ol>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000706<li>Modify or inspect any basic blocks outside of the current one</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000707<li>Maintain state across invocations of
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000708 <a href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a></li>
Reid Spencera1799682004-12-11 05:12:57 +0000709<li>Modify the control flow graph (by altering terminator instructions)</li>
710<li>Any of the things forbidden for
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000711 <a href="#FunctionPass"><tt>FunctionPass</tt></a>es.</li>
712</ol>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000713
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000714<p><tt>BasicBlockPass</tt>es are useful for traditional local and "peephole"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000715optimizations. They may override the same <a
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000716href="#doInitialization_mod"><tt>doInitialization(Module &amp;)</tt></a> and <a
717href="#doFinalization_mod"><tt>doFinalization(Module &amp;)</tt></a> methods that <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000718href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have the following virtual methods that may also be implemented:</p>
719
720</div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000721
722<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000723<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000724 <a name="doInitialization_fn">The <tt>doInitialization(Function
725 &amp;)</tt> method</a>
726</div>
727
728<div class="doc_text">
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000729
Jim Laskey575df8b2006-08-04 18:10:12 +0000730<div class="doc_code"><pre>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000731 <b>virtual bool</b> doInitialization(Function &amp;F);
Jim Laskey575df8b2006-08-04 18:10:12 +0000732</pre></div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000733
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000734<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000735<tt>BasicBlockPass</tt>'s are not allowed to do, but that
736<tt>FunctionPass</tt>'s can. The <tt>doInitialization</tt> method is designed
Reid Spencera1799682004-12-11 05:12:57 +0000737to do simple initialization that does not depend on the
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000738BasicBlocks being processed. The <tt>doInitialization</tt> method call is not
739scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000740fast).</p>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000741
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000742</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000743
744<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000745<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000746 <a name="runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
747</div>
748
749<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000750
Jim Laskey575df8b2006-08-04 18:10:12 +0000751<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000752 <b>virtual bool</b> runOnBasicBlock(BasicBlock &amp;BB) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000753</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000754
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000755<p>Override this function to do the work of the <tt>BasicBlockPass</tt>. This
Chris Lattner34ceebe2002-08-08 20:11:18 +0000756function is not allowed to inspect or modify basic blocks other than the
757parameter, and are not allowed to modify the CFG. A true value must be returned
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000758if the basic block is modified.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000759
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000760</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000761
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000762<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000763<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000764 <a name="doFinalization_fn">The <tt>doFinalization(Function &amp;)</tt>
765 method</a>
766</div>
767
768<div class="doc_text">
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000769
Jim Laskey575df8b2006-08-04 18:10:12 +0000770<div class="doc_code"><pre>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000771 <b>virtual bool</b> doFinalization(Function &amp;F);
Jim Laskey575df8b2006-08-04 18:10:12 +0000772</pre></div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000773
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000774<p>The <tt>doFinalization</tt> method is an infrequently used method that is
775called when the pass framework has finished calling <a
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000776href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a> for every BasicBlock in the
777program being compiled. This can be used to perform per-function
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000778finalization.</p>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000779
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000780</div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000781
Brian Gaeke110f07b2003-07-17 18:53:20 +0000782<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000783<div class="doc_subsection">
784 <a name="MachineFunctionPass">The <tt>MachineFunctionPass</tt> class</a>
785</div>
Brian Gaeke110f07b2003-07-17 18:53:20 +0000786
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000787<div class="doc_text">
788
Chris Lattner8e871652004-03-17 21:09:55 +0000789<p>A <tt>MachineFunctionPass</tt> is a part of the LLVM code generator that
790executes on the machine-dependent representation of each LLVM function in the
791program. A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all
Brian Gaeke110f07b2003-07-17 18:53:20 +0000792the restrictions that apply to a <tt>FunctionPass</tt> also apply to it.
Chris Lattner8e871652004-03-17 21:09:55 +0000793<tt>MachineFunctionPass</tt>es also have additional restrictions. In particular,
794<tt>MachineFunctionPass</tt>es are not allowed to do any of the following:</p>
Brian Gaeke110f07b2003-07-17 18:53:20 +0000795
796<ol>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000797<li>Modify any LLVM Instructions, BasicBlocks or Functions.</li>
798<li>Modify a MachineFunction other than the one currently being processed.</li>
799<li>Add or remove MachineFunctions from the current Module.</li>
800<li>Add or remove global variables from the current Module.</li>
801<li>Maintain state across invocations of <a
802href="#runOnMachineFunction"><tt>runOnMachineFunction</tt></a> (including global
803data)</li>
804</ol>
Brian Gaeke110f07b2003-07-17 18:53:20 +0000805
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000806</div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000807
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000808<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000809<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000810 <a name="runOnMachineFunction">The <tt>runOnMachineFunction(MachineFunction
811 &amp;MF)</tt> method</a>
812</div>
813
814<div class="doc_text">
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000815
Jim Laskey575df8b2006-08-04 18:10:12 +0000816<div class="doc_code"><pre>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000817 <b>virtual bool</b> runOnMachineFunction(MachineFunction &amp;MF) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000818</pre></div>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000819
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000820<p><tt>runOnMachineFunction</tt> can be considered the main entry point of a
821<tt>MachineFunctionPass</tt>; that is, you should override this method to do the
822work of your <tt>MachineFunctionPass</tt>.</p>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000823
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000824<p>The <tt>runOnMachineFunction</tt> method is called on every
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000825<tt>MachineFunction</tt> in a <tt>Module</tt>, so that the
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000826<tt>MachineFunctionPass</tt> may perform optimizations on the machine-dependent
827representation of the function. If you want to get at the LLVM <tt>Function</tt>
828for the <tt>MachineFunction</tt> you're working on, use
829<tt>MachineFunction</tt>'s <tt>getFunction()</tt> accessor method -- but
830remember, you may not modify the LLVM <tt>Function</tt> or its contents from a
831<tt>MachineFunctionPass</tt>.</p>
832
833</div>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000834
Chris Lattner34ceebe2002-08-08 20:11:18 +0000835<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000836<div class="doc_section">
837 <a name="registration">Pass registration</a>
838</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000839<!-- *********************************************************************** -->
840
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000841<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000842
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000843<p>In the <a href="#basiccode">Hello World</a> example pass we illustrated how
844pass registration works, and discussed some of the reasons that it is used and
845what it does. Here we discuss how and why passes are registered.</p>
846
847<p>Passes can be registered in several different ways. Depending on the general
Chris Lattner34ceebe2002-08-08 20:11:18 +0000848classification of the pass, you should use one of the following templates to
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000849register the pass:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000850
851<ul>
852<li><b><tt>RegisterOpt</tt></b> - This template should be used when you are
853registering a pass that logically should be available for use in the
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000854'<tt>opt</tt>' utility.</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000855
856<li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are
857registering a pass that logically should be available for use in the
Chris Lattner8e871652004-03-17 21:09:55 +0000858'<tt>analyze</tt>' utility.</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000859
860<li><b><tt>RegisterPass</tt></b> - This is the generic form of the
861<tt>Register*</tt> templates that should be used if you want your pass listed by
862multiple or no utilities. This template takes an extra third argument that
863specifies which tools it should be listed in. See the <a
Reid Spencerca058542006-03-14 05:39:39 +0000864href="http://llvm.org/doxygen/PassSupport_8h-source.html">PassSupport.h</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000865file for more information.</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000866
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000867</ul>
868
869<p>Regardless of how you register your pass, you must specify at least two
Chris Lattner34ceebe2002-08-08 20:11:18 +0000870parameters. The first parameter is the name of the pass that is to be used on
871the command line to specify that the pass should be added to a program (for
872example <tt>opt</tt> or <tt>analyze</tt>). The second argument is the name of
873the pass, which is to be used for the <tt>--help</tt> output of programs, as
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000874well as for debug output generated by the <tt>--debug-pass</tt> option.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000875
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000876<p>If a pass is registered to be used by the <tt>analyze</tt> utility, you
877should implement the virtual <tt>print</tt> method:</p>
878
879</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000880
881<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000882<div class="doc_subsubsection">
883 <a name="print">The <tt>print</tt> method</a>
884</div>
885
886<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000887
Jim Laskey575df8b2006-08-04 18:10:12 +0000888<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000889 <b>virtual void</b> print(std::ostream &amp;O, <b>const</b> Module *M) <b>const</b>;
Jim Laskey575df8b2006-08-04 18:10:12 +0000890</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000891
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000892<p>The <tt>print</tt> method must be implemented by "analyses" in order to print
893a human readable version of the analysis results. This is useful for debugging
894an analysis itself, as well as for other people to figure out how an analysis
895works. The <tt>analyze</tt> tool uses this method to generate its output.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000896
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000897<p>The <tt>ostream</tt> parameter specifies the stream to write the results on,
898and the <tt>Module</tt> parameter gives a pointer to the top level module of the
Chris Lattner34ceebe2002-08-08 20:11:18 +0000899program that has been analyzed. Note however that this pointer may be null in
900certain circumstances (such as calling the <tt>Pass::dump()</tt> from a
901debugger), so it should only be used to enhance debug output, it should not be
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000902depended on.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000903
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000904</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000905
906<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000907<div class="doc_section">
908 <a name="interaction">Specifying interactions between passes</a>
909</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000910<!-- *********************************************************************** -->
911
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000912<div class="doc_text">
913
914<p>One of the main responsibilities of the <tt>PassManager</tt> is the make sure
Chris Lattner34ceebe2002-08-08 20:11:18 +0000915that passes interact with each other correctly. Because <tt>PassManager</tt>
916tries to <a href="#passmanager">optimize the execution of passes</a> it must
917know how the passes interact with each other and what dependencies exist between
918the various passes. To track this, each pass can declare the set of passes that
919are required to be executed before the current pass, and the passes which are
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000920invalidated by the current pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000921
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000922<p>Typically this functionality is used to require that analysis results are
Chris Lattner34ceebe2002-08-08 20:11:18 +0000923computed before your pass is run. Running arbitrary transformation passes can
924invalidate the computed analysis results, which is what the invalidation set
925specifies. If a pass does not implement the <tt><a
926href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000927having any prerequisite passes, and invalidating <b>all</b> other passes.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000928
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000929</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000930
931<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000932<div class="doc_subsubsection">
933 <a name="getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a>
934</div>
935
936<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000937
Jim Laskey575df8b2006-08-04 18:10:12 +0000938<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000939 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;Info) <b>const</b>;
Jim Laskey575df8b2006-08-04 18:10:12 +0000940</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000941
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000942<p>By implementing the <tt>getAnalysisUsage</tt> method, the required and
Chris Lattner34ceebe2002-08-08 20:11:18 +0000943invalidated sets may be specified for your transformation. The implementation
944should fill in the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000945href="http://llvm.org/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000946object with information about which passes are required and not invalidated. To
Chris Lattner8e871652004-03-17 21:09:55 +0000947do this, a pass may call any of the following methods on the AnalysisUsage
948object:</p>
949</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000950
Chris Lattner8e871652004-03-17 21:09:55 +0000951<!-- _______________________________________________________________________ -->
952<div class="doc_subsubsection">
953 <a name="AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a>
954</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000955
Chris Lattner8e871652004-03-17 21:09:55 +0000956<div class="doc_text">
957<p>
Reid Spencera1799682004-12-11 05:12:57 +0000958If your pass requires a previous pass to be executed (an analysis for example),
Chris Lattner8e871652004-03-17 21:09:55 +0000959it can use one of these methods to arrange for it to be run before your pass.
960LLVM has many different types of analyses and passes that can be required,
Reid Spencera1799682004-12-11 05:12:57 +0000961spanning the range from <tt>DominatorSet</tt> to <tt>BreakCriticalEdges</tt>.
962Requiring <tt>BreakCriticalEdges</tt>, for example, guarantees that there will
Chris Lattner8e871652004-03-17 21:09:55 +0000963be no critical edges in the CFG when your pass has been run.
964</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000965
Chris Lattner8e871652004-03-17 21:09:55 +0000966<p>
967Some analyses chain to other analyses to do their job. For example, an <a
968href="AliasAnalysis.html">AliasAnalysis</a> implementation is required to <a
969href="AliasAnalysis.html#chaining">chain</a> to other alias analysis passes. In
970cases where analyses chain, the <tt>addRequiredTransitive</tt> method should be
971used instead of the <tt>addRequired</tt> method. This informs the PassManager
972that the transitively required pass should be alive as long as the requiring
973pass is.
974</p>
975</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000976
Chris Lattner8e871652004-03-17 21:09:55 +0000977<!-- _______________________________________________________________________ -->
978<div class="doc_subsubsection">
979 <a name="AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a>
980</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000981
Chris Lattner8e871652004-03-17 21:09:55 +0000982<div class="doc_text">
983<p>
984One of the jobs of the PassManager is to optimize how and when analyses are run.
985In particular, it attempts to avoid recomputing data unless it needs to. For
986this reason, passes are allowed to declare that they preserve (i.e., they don't
987invalidate) an existing analysis if it's available. For example, a simple
Reid Spencera1799682004-12-11 05:12:57 +0000988constant folding pass would not modify the CFG, so it can't possibly affect the
Chris Lattner8e871652004-03-17 21:09:55 +0000989results of dominator analysis. By default, all passes are assumed to invalidate
990all others.
991</p>
992
993<p>
994The <tt>AnalysisUsage</tt> class provides several methods which are useful in
995certain circumstances that are related to <tt>addPreserved</tt>. In particular,
996the <tt>setPreservesAll</tt> method can be called to indicate that the pass does
997not modify the LLVM program at all (which is true for analyses), and the
998<tt>setPreservesCFG</tt> method can be used by transformations that change
999instructions in the program but do not modify the CFG or terminator instructions
1000(note that this property is implicitly set for <a
1001href="#BasicBlockPass">BasicBlockPass</a>'s).
1002</p>
1003
1004<p>
1005<tt>addPreserved</tt> is particularly useful for transformations like
1006<tt>BreakCriticalEdges</tt>. This pass knows how to update a small set of loop
1007and dominator related analyses if they exist, so it can preserve them, despite
1008the fact that it hacks on the CFG.
1009</p>
1010</div>
1011
1012<!-- _______________________________________________________________________ -->
1013<div class="doc_subsubsection">
1014 <a name="AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a>
1015</div>
1016
1017<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +00001018
Jim Laskey575df8b2006-08-04 18:10:12 +00001019<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001020 <i>// This is an example implementation from an analysis, which does not modify
1021 // the program at all, yet has a prerequisite.</i>
Reid Spencerca058542006-03-14 05:39:39 +00001022 <b>void</b> <a href="http://llvm.org/doxygen/classllvm_1_1PostDominanceFrontier.html">PostDominanceFrontier</a>::getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
Chris Lattner34ceebe2002-08-08 20:11:18 +00001023 AU.setPreservesAll();
Reid Spencerca058542006-03-14 05:39:39 +00001024 AU.addRequired&lt;<a href="http://llvm.org/doxygen/classllvm_1_1PostDominatorTree.html">PostDominatorTree</a>&gt;();
Chris Lattner34ceebe2002-08-08 20:11:18 +00001025 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001026</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001027
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001028<p>and:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001029
Jim Laskey575df8b2006-08-04 18:10:12 +00001030<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001031 <i>// This example modifies the program, but does not modify the CFG</i>
Reid Spencerca058542006-03-14 05:39:39 +00001032 <b>void</b> <a href="http://llvm.org/doxygen/structLICM.html">LICM</a>::getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
Chris Lattnera8c82c22002-10-21 19:57:59 +00001033 AU.setPreservesCFG();
Reid Spencerca058542006-03-14 05:39:39 +00001034 AU.addRequired&lt;<a href="http://llvm.org/doxygen/classllvm_1_1LoopInfo.html">LoopInfo</a>&gt;();
Chris Lattner34ceebe2002-08-08 20:11:18 +00001035 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001036</pre></div>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001037
1038</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001039
1040<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001041<div class="doc_subsubsection">
Chris Lattner8e871652004-03-17 21:09:55 +00001042 <a name="getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> and <tt>getAnalysisToUpdate&lt;&gt;</tt> methods</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001043</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001044
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001045<div class="doc_text">
1046
Chris Lattner8e871652004-03-17 21:09:55 +00001047<p>The <tt>Pass::getAnalysis&lt;&gt;</tt> method is automatically inherited by
1048your class, providing you with access to the passes that you declared that you
1049required with the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a>
1050method. It takes a single template argument that specifies which pass class you
1051want, and returns a reference to that pass. For example:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001052
Jim Laskey575df8b2006-08-04 18:10:12 +00001053<div class="doc_code"><pre>
Chris Lattnerbaf3f622004-03-17 21:33:32 +00001054 bool LICM::runOnFunction(Function &amp;F) {
Chris Lattner8e871652004-03-17 21:09:55 +00001055 LoopInfo &amp;LI = getAnalysis&lt;LoopInfo&gt;();
1056 ...
1057 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001058</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001059
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001060<p>This method call returns a reference to the pass desired. You may get a
1061runtime assertion failure if you attempt to get an analysis that you did not
1062declare as required in your <a
1063href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> implementation. This
1064method can be called by your <tt>run*</tt> method implementation, or by any
1065other local method invoked by your <tt>run*</tt> method.</p>
1066
Chris Lattner8e871652004-03-17 21:09:55 +00001067<p>
1068If your pass is capable of updating analyses if they exist (e.g.,
1069<tt>BreakCriticalEdges</tt>, as described above), you can use the
1070<tt>getAnalysisToUpdate</tt> method, which returns a pointer to the analysis if
1071it is active. For example:</p>
1072
Jim Laskey575df8b2006-08-04 18:10:12 +00001073<div class="doc_code"><pre>
Chris Lattner8e871652004-03-17 21:09:55 +00001074 ...
1075 if (DominatorSet *DS = getAnalysisToUpdate&lt;DominatorSet&gt;()) {
1076 <i>// A DominatorSet is active. This code will update it.</i>
1077 }
1078 ...
Jim Laskey575df8b2006-08-04 18:10:12 +00001079</pre></div>
Chris Lattner8e871652004-03-17 21:09:55 +00001080
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001081</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001082
Chris Lattner8d8a9282002-08-22 19:21:04 +00001083<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001084<div class="doc_section">
1085 <a name="analysisgroup">Implementing Analysis Groups</a>
1086</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001087<!-- *********************************************************************** -->
1088
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001089<div class="doc_text">
1090
1091<p>Now that we understand the basics of how passes are defined, how the are
1092used, and how they are required from other passes, it's time to get a little bit
Chris Lattner8d8a9282002-08-22 19:21:04 +00001093fancier. All of the pass relationships that we have seen so far are very
1094simple: one pass depends on one other specific pass to be run before it can run.
1095For many applications, this is great, for others, more flexibility is
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001096required.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001097
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001098<p>In particular, some analyses are defined such that there is a single simple
Chris Lattner8d8a9282002-08-22 19:21:04 +00001099interface to the analysis results, but multiple ways of calculating them.
1100Consider alias analysis for example. The most trivial alias analysis returns
1101"may alias" for any alias query. The most sophisticated analysis a
1102flow-sensitive, context-sensitive interprocedural analysis that can take a
1103significant amount of time to execute (and obviously, there is a lot of room
1104between these two extremes for other implementations). To cleanly support
1105situations like this, the LLVM Pass Infrastructure supports the notion of
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001106Analysis Groups.</p>
1107
1108</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001109
1110<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001111<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001112 <a name="agconcepts">Analysis Group Concepts</a>
1113</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001114
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001115<div class="doc_text">
1116
1117<p>An Analysis Group is a single simple interface that may be implemented by
Chris Lattner8d8a9282002-08-22 19:21:04 +00001118multiple different passes. Analysis Groups can be given human readable names
1119just like passes, but unlike passes, they need not derive from the <tt>Pass</tt>
1120class. An analysis group may have one or more implementations, one of which is
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001121the "default" implementation.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001122
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001123<p>Analysis groups are used by client passes just like other passes are: the
Chris Lattner8d8a9282002-08-22 19:21:04 +00001124<tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods.
1125In order to resolve this requirement, the <a href="#passmanager">PassManager</a>
1126scans the available passes to see if any implementations of the analysis group
1127are available. If none is available, the default implementation is created for
1128the pass to use. All standard rules for <A href="#interaction">interaction
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001129between passes</a> still apply.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001130
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001131<p>Although <a href="#registration">Pass Registration</a> is optional for normal
Chris Lattner8d8a9282002-08-22 19:21:04 +00001132passes, all analysis group implementations must be registered, and must use the
1133<A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the
1134implementation pool. Also, a default implementation of the interface
1135<b>must</b> be registered with <A
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001136href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001137
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001138<p>As a concrete example of an Analysis Group in action, consider the <a
Reid Spencerca058542006-03-14 05:39:39 +00001139href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001140analysis group. The default implementation of the alias analysis interface (the
1141<tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001142href="http://llvm.org/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001143pass) just does a few simple checks that don't require significant analysis to
1144compute (such as: two different globals can never alias each other, etc).
1145Passes that use the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001146href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001147interface (for example the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001148href="http://llvm.org/doxygen/structGCSE.html">gcse</a></tt> pass), do
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001149not care which implementation of alias analysis is actually provided, they just
1150use the designated interface.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001151
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001152<p>From the user's perspective, commands work just like normal. Issuing the
Chris Lattner8d8a9282002-08-22 19:21:04 +00001153command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be
1154instantiated and added to the pass sequence. Issuing the command '<tt>opt
1155-somefancyaa -gcse ...</tt>' will cause the <tt>gcse</tt> pass to use the
1156<tt>somefancyaa</tt> alias analysis (which doesn't actually exist, it's just a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001157hypothetical example) instead.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001158
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001159</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001160
1161<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001162<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001163 <a name="registerag">Using <tt>RegisterAnalysisGroup</tt></a>
1164</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001165
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001166<div class="doc_text">
1167
1168<p>The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
Chris Lattner8d8a9282002-08-22 19:21:04 +00001169group itself as well as add pass implementations to the analysis group. First,
1170an analysis should be registered, with a human readable name provided for it.
1171Unlike registration of passes, there is no command line argument to be specified
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001172for the Analysis Group Interface itself, because it is "abstract":</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001173
Jim Laskey575df8b2006-08-04 18:10:12 +00001174<div class="doc_code"><pre>
Reid Spencerca058542006-03-14 05:39:39 +00001175 <b>static</b> RegisterAnalysisGroup&lt;<a href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>&gt; A("<i>Alias Analysis</i>");
Jim Laskey575df8b2006-08-04 18:10:12 +00001176</pre></div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001177
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001178<p>Once the analysis is registered, passes can declare that they are valid
1179implementations of the interface by using the following code:</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001180
Jim Laskey575df8b2006-08-04 18:10:12 +00001181<div class="doc_code"><pre>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001182<b>namespace</b> {
1183 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
1184 RegisterOpt&lt;FancyAA&gt;
1185 B("<i>somefancyaa</i>", "<i>A more complex alias analysis implementation</i>");
1186
1187 //<i> Declare that we implement the AliasAnalysis interface</i>
Reid Spencerca058542006-03-14 05:39:39 +00001188 RegisterAnalysisGroup&lt;<a href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>, FancyAA&gt; C;
Chris Lattner8d8a9282002-08-22 19:21:04 +00001189}
Jim Laskey575df8b2006-08-04 18:10:12 +00001190</pre></div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001191
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001192<p>This just shows a class <tt>FancyAA</tt> that is registered normally, then
1193uses the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001194href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001195analysis group. Every implementation of an analysis group should join using
1196this template. A single pass may join multiple different analysis groups with
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001197no problem.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001198
Jim Laskey575df8b2006-08-04 18:10:12 +00001199<div class="doc_code"><pre>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001200<b>namespace</b> {
1201 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
Reid Spencerca058542006-03-14 05:39:39 +00001202 RegisterOpt&lt;<a href="http://llvm.org/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>&gt;
Chris Lattner8d8a9282002-08-22 19:21:04 +00001203 D("<i>basicaa</i>", "<i>Basic Alias Analysis (default AA impl)</i>");
1204
1205 //<i> Declare that we implement the AliasAnalysis interface</i>
Reid Spencerca058542006-03-14 05:39:39 +00001206 RegisterAnalysisGroup&lt;<a href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>, <a href="http://llvm.org/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>, <b>true</b>&gt; E;
Chris Lattner8d8a9282002-08-22 19:21:04 +00001207}
Jim Laskey575df8b2006-08-04 18:10:12 +00001208</pre></div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001209
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001210<p>Here we show how the default implementation is specified (using the extra
Chris Lattner8d8a9282002-08-22 19:21:04 +00001211argument to the <tt>RegisterAnalysisGroup</tt> template). There must be exactly
1212one default implementation available at all times for an Analysis Group to be
1213used. Here we declare that the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001214href="http://llvm.org/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001215pass is the default implementation for the interface.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001216
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001217</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001218
1219<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001220<div class="doc_section">
Tanya Lattnerfb966ba2004-11-19 01:25:14 +00001221 <a name="passStatistics">Pass Statistics</a>
1222</div>
1223<!-- *********************************************************************** -->
1224
1225<div class="doc_text">
Tanya Lattner0b40ee62004-11-19 01:26:37 +00001226<p>The <a
Reid Spencerca058542006-03-14 05:39:39 +00001227href="http://llvm.org/doxygen/Statistic_8h-source.html"><tt>Statistic</tt></a>
John Criswell3e149a22005-11-28 23:25:41 +00001228class is designed to be an easy way to expose various success
Tanya Lattnerfb966ba2004-11-19 01:25:14 +00001229metrics from passes. These statistics are printed at the end of a
1230run, when the -stats command line option is enabled on the command
Tanya Lattner0b40ee62004-11-19 01:26:37 +00001231line. See the <a href="http://llvm.org/docs/ProgrammersManual.html#Statistic">Statistics section</a> in the Programmer's Manual for details.
Tanya Lattnerfb966ba2004-11-19 01:25:14 +00001232
1233</div>
1234
1235
1236<!-- *********************************************************************** -->
1237<div class="doc_section">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001238 <a name="passmanager">What PassManager does</a>
1239</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001240<!-- *********************************************************************** -->
1241
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001242<div class="doc_text">
1243
1244<p>The <a
Reid Spencerca058542006-03-14 05:39:39 +00001245href="http://llvm.org/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001246<a
Reid Spencerca058542006-03-14 05:39:39 +00001247href="http://llvm.org/doxygen/classllvm_1_1PassManager.html">class</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001248takes a list of passes, ensures their <a href="#interaction">prerequisites</a>
1249are set up correctly, and then schedules passes to run efficiently. All of the
1250LLVM tools that run passes use the <tt>PassManager</tt> for execution of these
1251passes.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001252
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001253<p>The <tt>PassManager</tt> does two main things to try to reduce the execution
1254time of a series of passes:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001255
1256<ol>
1257<li><b>Share analysis results</b> - The PassManager attempts to avoid
1258recomputing analysis results as much as possible. This means keeping track of
1259which analyses are available already, which analyses get invalidated, and which
1260analyses are needed to be run for a pass. An important part of work is that the
1261<tt>PassManager</tt> tracks the exact lifetime of all analysis results, allowing
1262it to <a href="#releaseMemory">free memory</a> allocated to holding analysis
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001263results as soon as they are no longer needed.</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001264
1265<li><b>Pipeline the execution of passes on the program</b> - The
1266<tt>PassManager</tt> attempts to get better cache and memory usage behavior out
1267of a series of passes by pipelining the passes together. This means that, given
1268a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it
1269will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on
1270the first function, then all of the <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001271href="#FunctionPass"><tt>FunctionPass</tt></a>es on the second function,
1272etc... until the entire program has been run through the passes.
Chris Lattner34ceebe2002-08-08 20:11:18 +00001273
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001274<p>This improves the cache behavior of the compiler, because it is only touching
Chris Lattner34ceebe2002-08-08 20:11:18 +00001275the LLVM program representation for a single function at a time, instead of
1276traversing the entire program. It reduces the memory consumption of compiler,
1277because, for example, only one <a
Reid Spencerca058542006-03-14 05:39:39 +00001278href="http://llvm.org/doxygen/classllvm_1_1DominatorSet.html"><tt>DominatorSet</tt></a>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001279needs to be calculated at a time. This also makes it possible some <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001280href="#SMP">interesting enhancements</a> in the future.</p></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001281
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001282</ol>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001283
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001284<p>The effectiveness of the <tt>PassManager</tt> is influenced directly by how
1285much information it has about the behaviors of the passes it is scheduling. For
Chris Lattner34ceebe2002-08-08 20:11:18 +00001286example, the "preserved" set is intentionally conservative in the face of an
1287unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.
1288Not implementing when it should be implemented will have the effect of not
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001289allowing any analysis results to live across the execution of your pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001290
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001291<p>The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line
Chris Lattner34ceebe2002-08-08 20:11:18 +00001292options that is useful for debugging pass execution, seeing how things work, and
1293diagnosing when you should be preserving more analyses than you currently are
1294(To get information about all of the variants of the <tt>--debug-pass</tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001295option, just type '<tt>opt --help-hidden</tt>').</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001296
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001297<p>By using the <tt>--debug-pass=Structure</tt> option, for example, we can see
1298how our <a href="#basiccode">Hello World</a> pass interacts with other passes.
1299Lets try it out with the <tt>gcse</tt> and <tt>licm</tt> passes:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001300
Jim Laskey575df8b2006-08-04 18:10:12 +00001301<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +00001302$ opt -load ../../../Debug/lib/Hello.so -gcse -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +00001303Module Pass Manager
1304 Function Pass Manager
1305 Dominator Set Construction
1306 Immediate Dominators Construction
1307 Global Common Subexpression Elimination
1308-- Immediate Dominators Construction
1309-- Global Common Subexpression Elimination
1310 Natural Loop Construction
1311 Loop Invariant Code Motion
1312-- Natural Loop Construction
1313-- Loop Invariant Code Motion
1314 Module Verifier
1315-- Dominator Set Construction
1316-- Module Verifier
1317 Bytecode Writer
1318--Bytecode Writer
Jim Laskey575df8b2006-08-04 18:10:12 +00001319</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001320
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001321<p>This output shows us when passes are constructed and when the analysis
1322results are known to be dead (prefixed with '<tt>--</tt>'). Here we see that
1323GCSE uses dominator and immediate dominator information to do its job. The LICM
1324pass uses natural loop information, which uses dominator sets, but not immediate
Chris Lattner34ceebe2002-08-08 20:11:18 +00001325dominators. Because immediate dominators are no longer useful after the GCSE
1326pass, it is immediately destroyed. The dominator sets are then reused to
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001327compute natural loop information, which is then used by the LICM pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001328
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001329<p>After the LICM pass, the module verifier runs (which is automatically added
1330by the '<tt>opt</tt>' tool), which uses the dominator set to check that the
Chris Lattner34ceebe2002-08-08 20:11:18 +00001331resultant LLVM code is well formed. After it finishes, the dominator set
1332information is destroyed, after being computed once, and shared by three
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001333passes.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001334
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001335<p>Lets see how this changes when we run the <a href="#basiccode">Hello
1336World</a> pass in between the two passes:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001337
Jim Laskey575df8b2006-08-04 18:10:12 +00001338<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +00001339$ opt -load ../../../Debug/lib/Hello.so -gcse -hello -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +00001340Module Pass Manager
1341 Function Pass Manager
1342 Dominator Set Construction
1343 Immediate Dominators Construction
1344 Global Common Subexpression Elimination
1345<b>-- Dominator Set Construction</b>
1346-- Immediate Dominators Construction
1347-- Global Common Subexpression Elimination
1348<b> Hello World Pass
1349-- Hello World Pass
1350 Dominator Set Construction</b>
1351 Natural Loop Construction
1352 Loop Invariant Code Motion
1353-- Natural Loop Construction
1354-- Loop Invariant Code Motion
1355 Module Verifier
1356-- Dominator Set Construction
1357-- Module Verifier
1358 Bytecode Writer
1359--Bytecode Writer
1360Hello: __main
1361Hello: puts
1362Hello: main
Jim Laskey575df8b2006-08-04 18:10:12 +00001363</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001364
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001365<p>Here we see that the <a href="#basiccode">Hello World</a> pass has killed the
Chris Lattner34ceebe2002-08-08 20:11:18 +00001366Dominator Set pass, even though it doesn't modify the code at all! To fix this,
1367we need to add the following <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001368href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method to our pass:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001369
Jim Laskey575df8b2006-08-04 18:10:12 +00001370<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001371 <i>// We don't modify the program, so we preserve all analyses</i>
1372 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
1373 AU.setPreservesAll();
1374 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001375</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001376
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001377<p>Now when we run our pass, we get this output:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001378
Jim Laskey575df8b2006-08-04 18:10:12 +00001379<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +00001380$ opt -load ../../../Debug/lib/Hello.so -gcse -hello -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +00001381Pass Arguments: -gcse -hello -licm
1382Module Pass Manager
1383 Function Pass Manager
1384 Dominator Set Construction
1385 Immediate Dominators Construction
1386 Global Common Subexpression Elimination
1387-- Immediate Dominators Construction
1388-- Global Common Subexpression Elimination
1389 Hello World Pass
1390-- Hello World Pass
1391 Natural Loop Construction
1392 Loop Invariant Code Motion
1393-- Loop Invariant Code Motion
1394-- Natural Loop Construction
1395 Module Verifier
1396-- Dominator Set Construction
1397-- Module Verifier
1398 Bytecode Writer
1399--Bytecode Writer
1400Hello: __main
1401Hello: puts
1402Hello: main
Jim Laskey575df8b2006-08-04 18:10:12 +00001403</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001404
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001405<p>Which shows that we don't accidentally invalidate dominator information
1406anymore, and therefore do not have to compute it twice.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001407
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001408</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001409
1410<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001411<div class="doc_subsubsection">
1412 <a name="releaseMemory">The <tt>releaseMemory</tt> method</a>
1413</div>
1414
1415<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +00001416
Jim Laskey575df8b2006-08-04 18:10:12 +00001417<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001418 <b>virtual void</b> releaseMemory();
Jim Laskey575df8b2006-08-04 18:10:12 +00001419</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001420
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001421<p>The <tt>PassManager</tt> automatically determines when to compute analysis
Chris Lattner34ceebe2002-08-08 20:11:18 +00001422results, and how long to keep them around for. Because the lifetime of the pass
1423object itself is effectively the entire duration of the compilation process, we
1424need some way to free analysis results when they are no longer useful. The
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001425<tt>releaseMemory</tt> virtual method is the way to do this.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001426
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001427<p>If you are writing an analysis or any other pass that retains a significant
Chris Lattner34ceebe2002-08-08 20:11:18 +00001428amount of state (for use by another pass which "requires" your pass and uses the
1429<a href="#getAnalysis">getAnalysis</a> method) you should implement
1430<tt>releaseMEmory</tt> to, well, release the memory allocated to maintain this
1431internal state. This method is called after the <tt>run*</tt> method for the
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001432class, before the next call of <tt>run*</tt> in your pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001433
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001434</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001435
1436<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001437<div class="doc_section">
Jim Laskey575df8b2006-08-04 18:10:12 +00001438 <a name="registering">Registering dynamically loaded passes</a>
1439</div>
1440<!-- *********************************************************************** -->
1441
1442<div class="doc_text">
1443
1444<p><i>Size matters</i> when constructing production quality tools using llvm,
1445both for the purposes of distribution, and for regulating the resident code size
1446when running on the target system. Therefore, it becomes desirable to
1447selectively use some passes, while omitting others and maintain the flexibility
1448to change configurations later on. You want to be able to do all this, and,
1449provide feedback to the user. This is where pass registration comes into
1450play.</p>
1451
1452<p>The fundamental mechanisms for pass registration are the
1453<tt>MachinePassRegistry</tt> class and subclasses of
1454<tt>MachinePassRegistryNode</tt>.</p>
1455
1456<p>An instance of <tt>MachinePassRegistry</tt> is used to maintain a list of
1457<tt>MachinePassRegistryNode</tt> objects. This instance maintains the list and
1458communicates additions and deletions to the command line interface.</p>
1459
1460<p>An instance of <tt>MachinePassRegistryNode</tt> subclass is used to maintain
1461information provided about a particular pass. This information includes the
1462command line name, the command help string and the address of the function used
1463to create an instance of the pass. A global static constructor of one of these
1464instances <i>registers</i> with a corresponding <tt>MachinePassRegistry</tt>,
1465the static destructor <i>unregisters</i>. Thus a pass that is statically linked
1466in the tool will be registered at start up. A dynamically loaded pass will
1467register on load and unregister at unload.</p>
1468
1469</div>
1470
1471<!-- _______________________________________________________________________ -->
1472<div class="doc_subsection">
1473 <a name="registering_existing">Using existing registries</a>
1474</div>
1475
1476<div class="doc_text">
1477
1478<p>There are predefined registries to track instruction scheduling
1479(<tt>RegisterScheduler</tt>) and register allocation (<tt>RegisterRegAlloc</tt>)
1480machine passes. Here we will describe how to <i>register</i> a register
1481allocator machine pass.</p>
1482
1483<p>Implement your register allocator machine pass. In your register allocator
1484.cpp file add the following include;</p>
1485
1486<div class="doc_code"><pre>
1487 #include ""llvm/CodeGen/RegAllocRegistry.h""
1488</pre></div>
1489
1490<p>Also in your register allocator .cpp file, define a creator function in the
1491form; </p>
1492
1493<div class="doc_code"><pre>
1494 FunctionPass *createMyRegisterAllocator() {
1495 return new MyRegisterAllocator();
1496 }
1497</pre></div>
1498
1499<p>Note that the signature of this function should match the type of
1500<tt>RegisterRegAlloc::FunctionPassCtor</tt>. In the same file add the
1501"installing" declaration, in the form;</p>
1502
1503<div class="doc_code"><pre>
1504 static RegisterRegAlloc myRegAlloc("myregalloc",
1505 " my register allocator help string",
1506 createMyRegisterAllocator);
1507</pre></div>
1508
1509<p>Note the two spaces prior to the help string produces a tidy result on the
1510--help query.</p>
1511
1512<div class="doc_code"><pre>
1513$ llc --help
1514 ...
1515 -regalloc - Register allocator to use: (default = linearscan)
1516 =linearscan - linear scan register allocator
1517 =local - local register allocator
1518 =simple - simple register allocator
1519 =myregalloc - my register allocator help string
1520 ...
1521</pre></div>
1522
1523<p>And that's it. The user is now free to use <tt>-regalloc=myregalloc</tt> as
1524an option. Registering instruction schedulers is similar except use the
1525<tt>RegisterRegAlloc</tt> class. Note that the
1526<tt>RegisterRegAlloc::FunctionPassCtor</tt> is significantly different from
1527<tt>RegisterRegAlloc::FunctionPassCtor</tt>.</p>
1528
1529<p>To force the load/linking of your register allocator into the llc/lli tools,
1530add your creator function's global declaration to "Passes.h" and add a "pseudo"
1531call line to <tt>llvm/Codegen/LinkAllCodegenComponents.h</tt>.</p>
1532
1533</div>
1534
1535
1536<!-- _______________________________________________________________________ -->
1537<div class="doc_subsection">
1538 <a name="registering_new">Creating new registries</a>
1539</div>
1540
1541<div class="doc_text">
1542
1543<p>The easiest way to get started is to clone one of the existing registries; we
1544recommend <tt>llvm/CodeGen/RegAllocRegistry.h</tt>. The key things to modify
1545are the class name and the <tt>FunctionPassCtor</tt> type.</p>
1546
1547<p>Then you need to declare the registry. Example: if your pass registry is
1548<tt>RegisterMyPasses</tt> then define;</p>
1549
1550<div class="doc_code"><pre>
1551MachinePassRegistry RegisterMyPasses::Registry;
1552</pre></div>
1553
1554<p>And finally, declare the command line option for your passes. Example:</p>
1555
1556<div class="doc_code"><pre>
1557 cl::opt&lt;RegisterMyPasses::FunctionPassCtor, false,
1558 RegisterPassParser&lt;RegisterMyPasses&gt &gt
1559 MyPassOpt("mypass",
1560 cl::init(&amp;createDefaultMyPass),
1561 cl::desc("my pass option help"));
1562</pre></div>
1563
1564<p>Here the command option is "mypass", with createDefaultMyPass as the default
1565creator.</p>
1566
1567</div>
1568
1569<!-- *********************************************************************** -->
1570<div class="doc_section">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001571 <a name="debughints">Using GDB with dynamically loaded passes</a>
1572</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001573<!-- *********************************************************************** -->
1574
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001575<div class="doc_text">
1576
1577<p>Unfortunately, using GDB with dynamically loaded passes is not as easy as it
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001578should be. First of all, you can't set a breakpoint in a shared object that has
1579not been loaded yet, and second of all there are problems with inlined functions
1580in shared objects. Here are some suggestions to debugging your pass with
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001581GDB.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001582
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001583<p>For sake of discussion, I'm going to assume that you are debugging a
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001584transformation invoked by <tt>opt</tt>, although nothing described here depends
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001585on that.</p>
1586
1587</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001588
1589<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001590<div class="doc_subsubsection">
1591 <a name="breakpoint">Setting a breakpoint in your pass</a>
1592</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001593
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001594<div class="doc_text">
1595
1596<p>First thing you do is start <tt>gdb</tt> on the <tt>opt</tt> process:</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001597
Jim Laskey575df8b2006-08-04 18:10:12 +00001598<div class="doc_code"><pre>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001599$ <b>gdb opt</b>
1600GNU gdb 5.0
1601Copyright 2000 Free Software Foundation, Inc.
1602GDB is free software, covered by the GNU General Public License, and you are
1603welcome to change it and/or distribute copies of it under certain conditions.
1604Type "show copying" to see the conditions.
1605There is absolutely no warranty for GDB. Type "show warranty" for details.
1606This GDB was configured as "sparc-sun-solaris2.6"...
1607(gdb)
Jim Laskey575df8b2006-08-04 18:10:12 +00001608</pre></div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001609
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001610<p>Note that <tt>opt</tt> has a lot of debugging information in it, so it takes
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001611time to load. Be patient. Since we cannot set a breakpoint in our pass yet
1612(the shared object isn't loaded until runtime), we must execute the process, and
1613have it stop before it invokes our pass, but after it has loaded the shared
1614object. The most foolproof way of doing this is to set a breakpoint in
1615<tt>PassManager::run</tt> and then run the process with the arguments you
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001616want:</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001617
Jim Laskey575df8b2006-08-04 18:10:12 +00001618<div class="doc_code"><pre>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001619(gdb) <b>break PassManager::run</b>
1620Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
Chris Lattner07275182005-04-21 04:52:37 +00001621(gdb) <b>run test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[passoption]</b>
1622Starting program: opt test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[passoption]
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001623Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
162470 bool PassManager::run(Module &amp;M) { return PM-&gt;run(M); }
1625(gdb)
Jim Laskey575df8b2006-08-04 18:10:12 +00001626</pre></div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001627
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001628<p>Once the <tt>opt</tt> stops in the <tt>PassManager::run</tt> method you are
1629now free to set breakpoints in your pass so that you can trace through execution
1630or do other standard debugging stuff.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001631
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001632</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001633
1634<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001635<div class="doc_subsubsection">
1636 <a name="debugmisc">Miscellaneous Problems</a>
1637</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001638
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001639<div class="doc_text">
1640
1641<p>Once you have the basics down, there are a couple of problems that GDB has,
1642some with solutions, some without.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001643
1644<ul>
1645<li>Inline functions have bogus stack information. In general, GDB does a
1646pretty good job getting stack traces and stepping through inline functions.
1647When a pass is dynamically loaded however, it somehow completely loses this
1648capability. The only solution I know of is to de-inline a function (move it
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001649from the body of a class to a .cpp file).</li>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001650
1651<li>Restarting the program breaks breakpoints. After following the information
1652above, you have succeeded in getting some breakpoints planted in your pass. Nex
1653thing you know, you restart the program (i.e., you type '<tt>run</tt>' again),
1654and you start getting errors about breakpoints being unsettable. The only way I
1655have found to "fix" this problem is to <tt>delete</tt> the breakpoints that are
1656already set in your pass, run the program, and re-set the breakpoints once
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001657execution stops in <tt>PassManager::run</tt>.</li>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001658
1659</ul>
1660
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001661<p>Hopefully these tips will help with common case debugging situations. If
1662you'd like to contribute some tips of your own, just contact <a
1663href="mailto:sabre@nondot.org">Chris</a>.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001664
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001665</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001666
1667<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001668<div class="doc_section">
1669 <a name="future">Future extensions planned</a>
1670</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001671<!-- *********************************************************************** -->
1672
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001673<div class="doc_text">
1674
1675<p>Although the LLVM Pass Infrastructure is very capable as it stands, and does
Chris Lattner34ceebe2002-08-08 20:11:18 +00001676some nifty stuff, there are things we'd like to add in the future. Here is
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001677where we are going:</p>
1678
1679</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001680
1681<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001682<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001683 <a name="SMP">Multithreaded LLVM</a>
1684</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001685
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001686<div class="doc_text">
1687
1688<p>Multiple CPU machines are becoming more common and compilation can never be
Chris Lattner34ceebe2002-08-08 20:11:18 +00001689fast enough: obviously we should allow for a multithreaded compiler. Because of
1690the semantics defined for passes above (specifically they cannot maintain state
1691across invocations of their <tt>run*</tt> methods), a nice clean way to
1692implement a multithreaded compiler would be for the <tt>PassManager</tt> class
Misha Brukman7fdaab42003-07-14 17:20:40 +00001693to create multiple instances of each pass object, and allow the separate
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001694instances to be hacking on different parts of the program at the same time.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001695
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001696<p>This implementation would prevent each of the passes from having to implement
Chris Lattner34ceebe2002-08-08 20:11:18 +00001697multithreaded constructs, requiring only the LLVM core to have locking in a few
1698places (for global resources). Although this is a simple extension, we simply
1699haven't had time (or multiprocessor machines, thus a reason) to implement this.
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001700Despite that, we have kept the LLVM passes SMP ready, and you should too.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001701
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001702</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001703
1704<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001705<div class="doc_subsubsection">
Chris Lattner3c11c042004-09-20 04:36:29 +00001706<a name="PassFunctionPass"><tt>ModulePass</tt>es requiring <tt>FunctionPass</tt>es</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001707</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001708
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001709<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +00001710
Chris Lattner3c11c042004-09-20 04:36:29 +00001711<p>Currently it is illegal for a <a href="#ModulePass"><tt>ModulePass</tt></a>
1712to require a <a href="#FunctionPass"><tt>FunctionPass</tt></a>. This is because
1713there is only one instance of the <a
1714href="#FunctionPass"><tt>FunctionPass</tt></a> object ever created, thus nowhere
1715to store information for all of the functions in the program at the same time.
1716Although this has come up a couple of times before, this has always been worked
1717around by factoring one big complicated pass into a global and an
1718interprocedural part, both of which are distinct. In the future, it would be
1719nice to have this though.</p>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001720
1721<p>Note that it is no problem for a <a
Chris Lattner34ceebe2002-08-08 20:11:18 +00001722href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a
Chris Lattner3c11c042004-09-20 04:36:29 +00001723href="#ModulePass"><tt>ModulePass</tt></a>, only the other way around.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001724
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001725</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001726
1727<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001728<hr>
1729<address>
1730 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1731 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1732 <a href="http://validator.w3.org/check/referer"><img
1733 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001734
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001735 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencerca058542006-03-14 05:39:39 +00001736 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001737 Last modified: $Date$
1738</address>
1739
1740</body>
1741</html>