blob: a56297fd58d08f40b7bcd04a94f6b87c001cb9ba [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 Lattnerb693cb02006-08-27 23:18:52 +000021 <li><a href="#running">Running a pass with <tt>opt</tt></a></li>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000022 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000023 <li><a href="#passtype">Pass classes and requirements</a>
24 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000025 <li><a href="#ImmutablePass">The <tt>ImmutablePass</tt> class</a></li>
Chris Lattner3c11c042004-09-20 04:36:29 +000026 <li><a href="#ModulePass">The <tt>ModulePass</tt> class</a>
Chris Lattner34ceebe2002-08-08 20:11:18 +000027 <ul>
Chris Lattner3c11c042004-09-20 04:36:29 +000028 <li><a href="#runOnModule">The <tt>runOnModule</tt> method</a></li>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000029 </ul></li>
Chris Lattner9e4be6f2004-09-18 06:39:35 +000030 <li><a href="#CallGraphSCCPass">The <tt>CallGraphSCCPass</tt> class</a>
31 <ul>
Devang Patel40ec0802007-01-23 22:56:28 +000032 <li><a href="#doInitialization_scc">The <tt>doInitialization(CallGraph
Chris Lattner9e4be6f2004-09-18 06:39:35 +000033 &amp;)</tt> method</a></li>
34 <li><a href="#runOnSCC">The <tt>runOnSCC</tt> method</a></li>
Devang Patel40ec0802007-01-23 22:56:28 +000035 <li><a href="#doFinalization_scc">The <tt>doFinalization(CallGraph
Chris Lattner9e4be6f2004-09-18 06:39:35 +000036 &amp;)</tt> method</a></li>
37 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000038 <li><a href="#FunctionPass">The <tt>FunctionPass</tt> class</a>
39 <ul>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000040 <li><a href="#doInitialization_mod">The <tt>doInitialization(Module
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000041 &amp;)</tt> method</a></li>
42 <li><a href="#runOnFunction">The <tt>runOnFunction</tt> method</a></li>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000043 <li><a href="#doFinalization_mod">The <tt>doFinalization(Module
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000044 &amp;)</tt> method</a></li>
45 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000046 <li><a href="#BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
47 <ul>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000048 <li><a href="#doInitialization_fn">The <tt>doInitialization(Function
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000049 &amp;)</tt> method</a></li>
50 <li><a href="#runOnBasicBlock">The <tt>runOnBasicBlock</tt>
51 method</a></li>
Chris Lattner5e08e70f2002-09-12 17:06:43 +000052 <li><a href="#doFinalization_fn">The <tt>doFinalization(Function
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000053 &amp;)</tt> method</a></li>
54 </ul></li>
Brian Gaeke110f07b2003-07-17 18:53:20 +000055 <li><a href="#MachineFunctionPass">The <tt>MachineFunctionPass</tt>
56 class</a>
Brian Gaeke673ef4d2003-07-22 20:53:20 +000057 <ul>
58 <li><a href="#runOnMachineFunction">The
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000059 <tt>runOnMachineFunction(MachineFunction &amp;)</tt> method</a></li>
60 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000061 </ul>
62 <li><a href="#registration">Pass Registration</a>
63 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000064 <li><a href="#print">The <tt>print</tt> method</a></li>
65 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000066 <li><a href="#interaction">Specifying interactions between passes</a>
67 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000068 <li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt>
69 method</a></li>
Chris Lattner8e871652004-03-17 21:09:55 +000070 <li><a href="#AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a></li>
71 <li><a href="#AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a></li>
72 <li><a href="#AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a></li>
73 <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 +000074 </ul></li>
Chris Lattner8d8a9282002-08-22 19:21:04 +000075 <li><a href="#analysisgroup">Implementing Analysis Groups</a>
76 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000077 <li><a href="#agconcepts">Analysis Group Concepts</a></li>
78 <li><a href="#registerag">Using <tt>RegisterAnalysisGroup</tt></a></li>
79 </ul></li>
Tanya Lattnerfb966ba2004-11-19 01:25:14 +000080 <li><a href="#passStatistics">Pass Statistics</a>
Chris Lattner34ceebe2002-08-08 20:11:18 +000081 <li><a href="#passmanager">What PassManager does</a>
82 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000083 <li><a href="#releaseMemory">The <tt>releaseMemory</tt> method</a></li>
84 </ul></li>
Jim Laskey575df8b2006-08-04 18:10:12 +000085 <li><a href="#registering">Registering dynamically loaded passes</a>
86 <ul>
87 <li><a href="#registering_existing">Using existing registries</a></li>
88 <li><a href="#registering_new">Creating new registries</a></li>
89 </ul></li>
Chris Lattner8a97c8a2002-09-06 02:02:58 +000090 <li><a href="#debughints">Using GDB with dynamically loaded passes</a>
91 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000092 <li><a href="#breakpoint">Setting a breakpoint in your pass</a></li>
93 <li><a href="#debugmisc">Miscellaneous Problems</a></li>
94 </ul></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +000095 <li><a href="#future">Future extensions planned</a>
96 <ul>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000097 <li><a href="#SMP">Multithreaded LLVM</a></li>
Chris Lattner3c11c042004-09-20 04:36:29 +000098 <li><a href="#PassFunctionPass"><tt>ModulePass</tt>es requiring
Misha Brukmanf5edf1e2004-01-15 18:34:11 +000099 <tt>FunctionPass</tt>es</a></li>
100 </ul></li>
101</ol>
Chris Lattner56d9e222002-08-08 20:23:41 +0000102
Chris Lattner020e1fc2004-05-23 21:07:27 +0000103<div class="doc_author">
Chris Lattner4bde9fb2006-08-11 16:37:02 +0000104 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a> and
Jim Laskey6e8a2f42007-03-14 19:32:21 +0000105 <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000106</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000107
108<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000109<div class="doc_section">
110 <a name="introduction">Introduction - What is a pass?</a>
111</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000112<!-- *********************************************************************** -->
113
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000114<div class="doc_text">
115
116<p>The LLVM Pass Framework is an important part of the LLVM system, because LLVM
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000117passes are where most of the interesting parts of the compiler exist. Passes
118perform the transformations and optimizations that make up the compiler, they
119build the analysis results that are used by these transformations, and they are,
120above all, a structuring technique for compiler code.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000121
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000122<p>All LLVM passes are subclasses of the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000123href="http://llvm.org/doxygen/classllvm_1_1Pass.html">Pass</a></tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000124class, which implement functionality by overriding virtual methods inherited
Chris Lattner3c11c042004-09-20 04:36:29 +0000125from <tt>Pass</tt>. Depending on how your pass works, you should inherit from
126the <tt><a href="#ModulePass">ModulePass</a></tt>, <tt><a
127href="#CallGraphSCCPass">CallGraphSCCPass</a></tt>, <tt><a
128href="#FunctionPass">FunctionPass</a></tt>, or <tt><a
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000129href="#BasicBlockPass">BasicBlockPass</a></tt> classes, which gives the system
130more information about what your pass does, and how it can be combined with
131other passes. One of the main features of the LLVM Pass Framework is that it
132schedules passes to run in an efficient way based on the constraints that your
133pass meets (which are indicated by which class they derive from).</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000134
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000135<p>We start by showing you how to construct a pass, everything from setting up
136the code, to compiling, loading, and executing it. After the basics are down,
137more advanced features are discussed.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000138
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000139</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000140
141<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000142<div class="doc_section">
143 <a name="quickstart">Quick Start - Writing hello world</a>
144</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000145<!-- *********************************************************************** -->
146
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000147<div class="doc_text">
148
149<p>Here we describe how to write the "hello world" of passes. The "Hello" pass
150is designed to simply print out the name of non-external functions that exist in
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000151the program being compiled. It does not modify the program at all, it just
Chris Lattner34ceebe2002-08-08 20:11:18 +0000152inspects it. The source code and files for this pass are available in the LLVM
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000153source tree in the <tt>lib/Transforms/Hello</tt> directory.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000154
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000155</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000156
157<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000158<div class="doc_subsection">
159 <a name="makefile">Setting up the build environment</a>
160</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000161
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000162<div class="doc_text">
163
Reid Spencera1799682004-12-11 05:12:57 +0000164 <p>First, you need to create a new directory somewhere in the LLVM source
165 base. For this example, we'll assume that you made
166 <tt>lib/Transforms/Hello</tt>. Next, you must set up a build script
167 (Makefile) that will compile the source code for the new pass. To do this,
168 copy the following into <tt>Makefile</tt>:</p>
169 <hr/>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000170
Jim Laskey575df8b2006-08-04 18:10:12 +0000171<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000172# Makefile for hello pass
Chris Lattner34ceebe2002-08-08 20:11:18 +0000173
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000174# Path to top level of LLVM heirarchy
Chris Lattner0c5e2932002-08-14 20:07:01 +0000175LEVEL = ../../..
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000176
177# Name of the library to build
Reid Spencer4a1ab182005-01-11 05:16:23 +0000178LIBRARYNAME = Hello
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000179
Reid Spencer4a1ab182005-01-11 05:16:23 +0000180# Make the shared library become a loadable module so the tools can
181# dlopen/dlsym on the resulting library.
Robert Bocchino19cbea12006-01-06 22:49:23 +0000182LOADABLE_MODULE = 1
Reid Spencer4a1ab182005-01-11 05:16:23 +0000183
Reid Spencer197d7bf2006-08-08 01:48:17 +0000184# Tell the build system which LLVM libraries your pass needs. You'll probably
185# need at least LLVMSystem.a, LLVMSupport.a, LLVMCore.a but possibly several
186# others too.
187LLVMLIBS = LLVMCore.a LLVMSupport.a LLVMSystem.a
188
Chris Lattnereb4ba3c2002-08-14 20:06:13 +0000189# Include the makefile implementation stuff
190include $(LEVEL)/Makefile.common
Jim Laskey575df8b2006-08-04 18:10:12 +0000191</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000192
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000193<p>This makefile specifies that all of the <tt>.cpp</tt> files in the current
Chris Lattner34ceebe2002-08-08 20:11:18 +0000194directory are to be compiled and linked together into a
Reid Spencer4a1ab182005-01-11 05:16:23 +0000195<tt>Debug/lib/Hello.so</tt> shared object that can be dynamically loaded by
Chris Lattnerb693cb02006-08-27 23:18:52 +0000196the <tt>opt</tt> or <tt>bugpoint</tt> tools via their <tt>-load</tt> options.
Reid Spencer4a1ab182005-01-11 05:16:23 +0000197If your operating system uses a suffix other than .so (such as windows or
198Mac OS/X), the appropriate extension will be used.</p>
John Criswell2edea842004-01-26 21:26:54 +0000199
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000200<p>Now that we have the build scripts set up, we just need to write the code for
201the pass itself.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000202
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000203</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000204
205<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000206<div class="doc_subsection">
207 <a name="basiccode">Basic code required</a>
208</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000209
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000210<div class="doc_text">
211
212<p>Now that we have a way to compile our new pass, we just have to write it.
213Start out with:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000214
Jim Laskey575df8b2006-08-04 18:10:12 +0000215<div class="doc_code"><pre>
Reid Spencerca058542006-03-14 05:39:39 +0000216<b>#include</b> "<a href="http://llvm.org/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
217<b>#include</b> "<a href="http://llvm.org/doxygen/Function_8h-source.html">llvm/Function.h</a>"
Jim Laskey575df8b2006-08-04 18:10:12 +0000218</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000219
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000220<p>Which are needed because we are writing a <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000221href="http://llvm.org/doxygen/classllvm_1_1Pass.html">Pass</a></tt>, and
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000222we are operating on <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000223href="http://llvm.org/doxygen/classllvm_1_1Function.html">Function</a></tt>'s.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000224
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000225<p>Next we have:</p>
Jim Laskey575df8b2006-08-04 18:10:12 +0000226<div class="doc_code"><pre>
Jonathan Mantonaff4a6a2004-06-30 18:10:30 +0000227<b>using namespace llvm;</b>
Jim Laskey575df8b2006-08-04 18:10:12 +0000228</pre></div>
Jonathan Mantonaff4a6a2004-06-30 18:10:30 +0000229<p>... which is required because the functions from the include files
230live in the llvm namespace.
231</p>
232
233<p>Next we have:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000234
Jim Laskey575df8b2006-08-04 18:10:12 +0000235<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000236<b>namespace</b> {
Jim Laskey575df8b2006-08-04 18:10:12 +0000237</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000238
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000239<p>... which starts out an anonymous namespace. Anonymous namespaces are to C++
Chris Lattner34ceebe2002-08-08 20:11:18 +0000240what the "<tt>static</tt>" keyword is to C (at global scope). It makes the
241things declared inside of the anonymous namespace only visible to the current
242file. If you're not familiar with them, consult a decent C++ book for more
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000243information.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000244
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000245<p>Next, we declare our pass itself:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000246
Jim Laskey575df8b2006-08-04 18:10:12 +0000247<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000248 <b>struct</b> Hello : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
Jim Laskey575df8b2006-08-04 18:10:12 +0000249</pre></div><p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000250
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000251<p>This declares a "<tt>Hello</tt>" class that is a subclass of <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000252href="http://llvm.org/doxygen/classllvm_1_1FunctionPass.html">FunctionPass</a></tt>.
Chris Lattnera818d922002-09-09 03:48:46 +0000253The different builtin pass subclasses are described in detail <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000254href="#passtype">later</a>, but for now, know that <a
255href="#FunctionPass"><tt>FunctionPass</tt></a>'s operate a function at a
256time.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000257
Jim Laskey575df8b2006-08-04 18:10:12 +0000258<div class="doc_code"><pre>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000259 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000260 llvm::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
Chris Lattner34ceebe2002-08-08 20:11:18 +0000261 <b>return false</b>;
262 }
263 }; <i>// end of struct Hello</i>
Jim Laskey575df8b2006-08-04 18:10:12 +0000264</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000265
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000266<p>We declare a "<a href="#runOnFunction"><tt>runOnFunction</tt></a>" method,
267which overloads an abstract virtual method inherited from <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000268href="#FunctionPass"><tt>FunctionPass</tt></a>. This is where we are supposed
269to do our thing, so we just print out our message with the name of each
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000270function.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000271
Jim Laskey575df8b2006-08-04 18:10:12 +0000272<div class="doc_code"><pre>
Chris Lattnerb693cb02006-08-27 23:18:52 +0000273 RegisterPass&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
Chris Lattner34ceebe2002-08-08 20:11:18 +0000274} <i>// end of anonymous namespace</i>
Jim Laskey575df8b2006-08-04 18:10:12 +0000275</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000276
Chris Lattnerb693cb02006-08-27 23:18:52 +0000277<p>Lastly, we <a href="#registration">register our class</a> <tt>Hello</tt>,
278giving it a command line
279argument "<tt>hello</tt>", and a name "<tt>Hello World Pass</tt>".</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000280
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000281<p>As a whole, the <tt>.cpp</tt> file looks like:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000282
Jim Laskey575df8b2006-08-04 18:10:12 +0000283<div class="doc_code"><pre>
Reid Spencerca058542006-03-14 05:39:39 +0000284<b>#include</b> "<a href="http://llvm.org/doxygen/Pass_8h-source.html">llvm/Pass.h</a>"
285<b>#include</b> "<a href="http://llvm.org/doxygen/Function_8h-source.html">llvm/Function.h</a>"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000286
Jonathan Mantonaff4a6a2004-06-30 18:10:30 +0000287<b>using namespace llvm;</b>
288
Chris Lattner34ceebe2002-08-08 20:11:18 +0000289<b>namespace</b> {
290 <b>struct Hello</b> : <b>public</b> <a href="#FunctionPass">FunctionPass</a> {
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000291 <b>virtual bool</b> <a href="#runOnFunction">runOnFunction</a>(Function &amp;F) {
Bill Wendling22e978a2006-12-07 20:04:42 +0000292 llvm::cerr &lt;&lt; "<i>Hello: </i>" &lt;&lt; F.getName() &lt;&lt; "\n";
Chris Lattner34ceebe2002-08-08 20:11:18 +0000293 <b>return false</b>;
294 }
295 };
296
Chris Lattnerb693cb02006-08-27 23:18:52 +0000297 RegisterPass&lt;Hello&gt; X("<i>hello</i>", "<i>Hello World Pass</i>");
Chris Lattner34ceebe2002-08-08 20:11:18 +0000298}
Jim Laskey575df8b2006-08-04 18:10:12 +0000299</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000300
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000301<p>Now that it's all together, compile the file with a simple "<tt>gmake</tt>"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000302command in the local directory and you should get a new
Reid Spencer4a1ab182005-01-11 05:16:23 +0000303"<tt>Debug/lib/Hello.so</tt> file. Note that everything in this file is
Chris Lattner34ceebe2002-08-08 20:11:18 +0000304contained in an anonymous namespace: this reflects the fact that passes are self
305contained units that do not need external interfaces (although they can have
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000306them) to be useful.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000307
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000308</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000309
310<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000311<div class="doc_subsection">
Chris Lattnerb693cb02006-08-27 23:18:52 +0000312 <a name="running">Running a pass with <tt>opt</tt></a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000313</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000314
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000315<div class="doc_text">
316
John Criswell2edea842004-01-26 21:26:54 +0000317<p>Now that you have a brand new shiny shared object file, we can use the
Chris Lattner34ceebe2002-08-08 20:11:18 +0000318<tt>opt</tt> command to run an LLVM program through your pass. Because you
Chris Lattnerb693cb02006-08-27 23:18:52 +0000319registered your pass with the <tt>RegisterPass</tt> template, you will be able to
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000320use the <tt>opt</tt> tool to access it, once loaded.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000321
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000322<p>To test it, follow the example at the end of the <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000323href="GettingStarted.html">Getting Started Guide</a> to compile "Hello World" to
324LLVM. We can now run the bytecode file (<tt>hello.bc</tt>) for the program
325through our transformation like this (or course, any bytecode file will
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000326work):</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000327
Jim Laskey575df8b2006-08-04 18:10:12 +0000328<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +0000329$ opt -load ../../../Debug/lib/Hello.so -hello &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +0000330Hello: __main
331Hello: puts
332Hello: main
Jim Laskey575df8b2006-08-04 18:10:12 +0000333</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000334
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000335<p>The '<tt>-load</tt>' option specifies that '<tt>opt</tt>' should load your
336pass as a shared object, which makes '<tt>-hello</tt>' a valid command line
337argument (which is one reason you need to <a href="#registration">register your
Chris Lattner34ceebe2002-08-08 20:11:18 +0000338pass</a>). Because the hello pass does not modify the program in any
339interesting way, we just throw away the result of <tt>opt</tt> (sending it to
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000340<tt>/dev/null</tt>).</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000341
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000342<p>To see what happened to the other string you registered, try running
343<tt>opt</tt> with the <tt>--help</tt> option:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000344
Jim Laskey575df8b2006-08-04 18:10:12 +0000345<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +0000346$ opt -load ../../../Debug/lib/Hello.so --help
Chris Lattner34ceebe2002-08-08 20:11:18 +0000347OVERVIEW: llvm .bc -&gt; .bc modular optimizer
348
349USAGE: opt [options] &lt;input bytecode&gt;
350
351OPTIONS:
352 Optimizations available:
353...
354 -funcresolve - Resolve Functions
355 -gcse - Global Common Subexpression Elimination
356 -globaldce - Dead Global Elimination
357 <b>-hello - Hello World Pass</b>
Chris Lattner216c7b82003-09-10 05:29:43 +0000358 -indvars - Canonicalize Induction Variables
Chris Lattner34ceebe2002-08-08 20:11:18 +0000359 -inline - Function Integration/Inlining
360 -instcombine - Combine redundant instructions
361...
Jim Laskey575df8b2006-08-04 18:10:12 +0000362</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000363
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000364<p>The pass name get added as the information string for your pass, giving some
Chris Lattner34ceebe2002-08-08 20:11:18 +0000365documentation to users of <tt>opt</tt>. Now that you have a working pass, you
366would go ahead and make it do the cool transformations you want. Once you get
367it all working and tested, it may become useful to find out how fast your pass
368is. The <a href="#passManager"><tt>PassManager</tt></a> provides a nice command
369line option (<tt>--time-passes</tt>) that allows you to get information about
370the execution time of your pass along with the other passes you queue up. For
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000371example:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000372
Jim Laskey575df8b2006-08-04 18:10:12 +0000373<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +0000374$ opt -load ../../../Debug/lib/Hello.so -hello -time-passes &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +0000375Hello: __main
376Hello: puts
377Hello: main
378===============================================================================
379 ... Pass execution timing report ...
380===============================================================================
381 Total Execution Time: 0.02 seconds (0.0479059 wall clock)
382
383 ---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name ---
384 0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bytecode Writer
385 0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction
386 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier
387 <b> 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass</b>
388 0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL
Jim Laskey575df8b2006-08-04 18:10:12 +0000389</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000390
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000391<p>As you can see, our implementation above is pretty fast :). The additional
Chris Lattner34ceebe2002-08-08 20:11:18 +0000392passes listed are automatically inserted by the '<tt>opt</tt>' tool to verify
393that the LLVM emitted by your pass is still valid and well formed LLVM, which
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000394hasn't been broken somehow.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000395
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000396<p>Now that you have seen the basics of the mechanics behind passes, we can talk
397about some more details of how they work and how to use them.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000398
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000399</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000400
401<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000402<div class="doc_section">
403 <a name="passtype">Pass classes and requirements</a>
404</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000405<!-- *********************************************************************** -->
406
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000407<div class="doc_text">
408
409<p>One of the first things that you should do when designing a new pass is to
Chris Lattner34ceebe2002-08-08 20:11:18 +0000410decide what class you should subclass for your pass. The <a
411href="#basiccode">Hello World</a> example uses the <tt><a
412href="#FunctionPass">FunctionPass</a></tt> class for its implementation, but we
413did not discuss why or when this should occur. Here we talk about the classes
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000414available, from the most general to the most specific.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000415
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000416<p>When choosing a superclass for your Pass, you should choose the <b>most
Chris Lattner8d8a9282002-08-22 19:21:04 +0000417specific</b> class possible, while still being able to meet the requirements
Misha Brukman7eb05a12003-08-18 14:43:39 +0000418listed. This gives the LLVM Pass Infrastructure information necessary to
Chris Lattner8d8a9282002-08-22 19:21:04 +0000419optimize how passes are run, so that the resultant compiler isn't unneccesarily
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000420slow.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000421
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000422</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000423
Chris Lattner9720dda2002-09-25 22:31:38 +0000424<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000425<div class="doc_subsection">
426 <a name="ImmutablePass">The <tt>ImmutablePass</tt> class</a>
427</div>
Chris Lattner9720dda2002-09-25 22:31:38 +0000428
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000429<div class="doc_text">
430
431<p>The most plain and boring type of pass is the "<tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000432href="http://llvm.org/doxygen/classllvm_1_1ImmutablePass.html">ImmutablePass</a></tt>"
Chris Lattner9720dda2002-09-25 22:31:38 +0000433class. This pass type is used for passes that do not have to be run, do not
434change state, and never need to be updated. This is not a normal type of
435transformation or analysis, but can provide information about the current
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000436compiler configuration.</p>
Chris Lattner9720dda2002-09-25 22:31:38 +0000437
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000438<p>Although this pass class is very infrequently used, it is important for
Chris Lattner9720dda2002-09-25 22:31:38 +0000439providing information about the current target machine being compiled for, and
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000440other static information that can affect the various transformations.</p>
Chris Lattner9720dda2002-09-25 22:31:38 +0000441
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000442<p><tt>ImmutablePass</tt>es never invalidate other transformations, are never
443invalidated, and are never "run".</p>
Chris Lattner9720dda2002-09-25 22:31:38 +0000444
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000445</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000446
447<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000448<div class="doc_subsection">
Chris Lattner3c11c042004-09-20 04:36:29 +0000449 <a name="ModulePass">The <tt>ModulePass</tt> class</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000450</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000451
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000452<div class="doc_text">
453
454<p>The "<tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000455href="http://llvm.org/doxygen/classllvm_1_1ModulePass.html">ModulePass</a></tt>"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000456class is the most general of all superclasses that you can use. Deriving from
Chris Lattner3c11c042004-09-20 04:36:29 +0000457<tt>ModulePass</tt> indicates that your pass uses the entire program as a unit,
Chris Lattner34ceebe2002-08-08 20:11:18 +0000458refering to function bodies in no predictable order, or adding and removing
Chris Lattner3c11c042004-09-20 04:36:29 +0000459functions. Because nothing is known about the behavior of <tt>ModulePass</tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000460subclasses, no optimization can be done for their execution.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000461
Chris Lattner3c11c042004-09-20 04:36:29 +0000462<p>To write a correct <tt>ModulePass</tt> subclass, derive from
463<tt>ModulePass</tt> and overload the <tt>runOnModule</tt> method with the
464following signature:</p>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000465
466</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000467
468<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000469<div class="doc_subsubsection">
Chris Lattner3c11c042004-09-20 04:36:29 +0000470 <a name="runOnModule">The <tt>runOnModule</tt> method</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000471</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000472
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000473<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000474
Jim Laskey575df8b2006-08-04 18:10:12 +0000475<div class="doc_code"><pre>
Chris Lattner3c11c042004-09-20 04:36:29 +0000476 <b>virtual bool</b> runOnModule(Module &amp;M) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000477</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000478
John Criswella3f51102005-07-15 19:25:12 +0000479<p>The <tt>runOnModule</tt> method performs the interesting work of the pass.
480It should return true if the module was modified by the transformation and
481false otherwise.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000482
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000483</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000484
485<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000486<div class="doc_subsection">
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000487 <a name="CallGraphSCCPass">The <tt>CallGraphSCCPass</tt> class</a>
488</div>
489
490<div class="doc_text">
491
492<p>The "<tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000493href="http://llvm.org/doxygen/classllvm_1_1CallGraphSCCPass.html">CallGraphSCCPass</a></tt>"
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000494is used by passes that need to traverse the program bottom-up on the call graph
495(callees before callers). Deriving from CallGraphSCCPass provides some
496mechanics for building and traversing the CallGraph, but also allows the system
497to optimize execution of CallGraphSCCPass's. If your pass meets the
498requirements outlined below, and doesn't meet the requirements of a <tt><a
499href="#FunctionPass">FunctionPass</a></tt> or <tt><a
500href="#BasicBlockPass">BasicBlockPass</a></tt>, you should derive from
501<tt>CallGraphSCCPass</tt>.</p>
502
503<p><b>TODO</b>: explain briefly what SCC, Tarjan's algo, and B-U mean.</p>
504
505<p>To be explicit, <tt>CallGraphSCCPass</tt> subclasses are:</p>
506
507<ol>
508
509<li>... <em>not allowed</em> to modify any <tt>Function</tt>s that are not in
510the current SCC.</li>
511
512<li>... <em>allowed</em> to inspect any Function's other than those in the
513current SCC and the direct callees of the SCC.</li>
514
515<li>... <em>required</em> to preserve the current CallGraph object, updating it
516to reflect any changes made to the program.</li>
517
518<li>... <em>not allowed</em> to add or remove SCC's from the current Module,
519though they may change the contents of an SCC.</li>
520
521<li>... <em>allowed</em> to add or remove global variables from the current
522Module.</li>
523
524<li>... <em>allowed</em> to maintain state across invocations of
525 <a href="#runOnSCC"><tt>runOnSCC</tt></a> (including global data).</li>
526</ol>
527
528<p>Implementing a <tt>CallGraphSCCPass</tt> is slightly tricky in some cases
529because it has to handle SCCs with more than one node in it. All of the virtual
530methods described below should return true if they modified the program, or
531false if they didn't.</p>
532
533</div>
534
535<!-- _______________________________________________________________________ -->
536<div class="doc_subsubsection">
Devang Patel40ec0802007-01-23 22:56:28 +0000537 <a name="doInitialization_scc">The <tt>doInitialization(CallGraph &amp;)</tt>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000538 method</a>
539</div>
540
541<div class="doc_text">
542
Jim Laskey575df8b2006-08-04 18:10:12 +0000543<div class="doc_code"><pre>
Devang Patel40ec0802007-01-23 22:56:28 +0000544 <b>virtual bool</b> doInitialization(CallGraph &amp;CG);
Jim Laskey575df8b2006-08-04 18:10:12 +0000545</pre></div>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000546
547<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
548<tt>CallGraphSCCPass</tt>'s are not allowed to do. They can add and remove
549functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
550is designed to do simple initialization type of stuff that does not depend on
551the SCCs being processed. The <tt>doInitialization</tt> method call is not
552scheduled to overlap with any other pass executions (thus it should be very
553fast).</p>
554
555</div>
556
557<!-- _______________________________________________________________________ -->
558<div class="doc_subsubsection">
559 <a name="runOnSCC">The <tt>runOnSCC</tt> method</a>
560</div>
561
562<div class="doc_text">
563
Jim Laskey575df8b2006-08-04 18:10:12 +0000564<div class="doc_code"><pre>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000565 <b>virtual bool</b> runOnSCC(const std::vector&lt;CallGraphNode *&gt; &amp;SCCM) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000566</pre></div>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000567
568<p>The <tt>runOnSCC</tt> method performs the interesting work of the pass, and
569should return true if the module was modified by the transformation, false
570otherwise.</p>
571
572</div>
573
574<!-- _______________________________________________________________________ -->
575<div class="doc_subsubsection">
Devang Patel40ec0802007-01-23 22:56:28 +0000576 <a name="doFinalization_scc">The <tt>doFinalization(CallGraph
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000577 &amp;)</tt> method</a>
578</div>
579
580<div class="doc_text">
581
Jim Laskey575df8b2006-08-04 18:10:12 +0000582<div class="doc_code"><pre>
Devang Patel40ec0802007-01-23 22:56:28 +0000583 <b>virtual bool</b> doFinalization(CallGraph &amp;CG);
Jim Laskey575df8b2006-08-04 18:10:12 +0000584</pre></div>
Chris Lattner9e4be6f2004-09-18 06:39:35 +0000585
586<p>The <tt>doFinalization</tt> method is an infrequently used method that is
587called when the pass framework has finished calling <a
588href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
589program being compiled.</p>
590
591</div>
592
593<!-- ======================================================================= -->
594<div class="doc_subsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000595 <a name="FunctionPass">The <tt>FunctionPass</tt> class</a>
596</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000597
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000598<div class="doc_text">
599
Chris Lattner3c11c042004-09-20 04:36:29 +0000600<p>In contrast to <tt>ModulePass</tt> subclasses, <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000601href="http://llvm.org/doxygen/classllvm_1_1Pass.html">FunctionPass</a></tt>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000602subclasses do have a predictable, local behavior that can be expected by the
603system. All <tt>FunctionPass</tt> execute on each function in the program
Misha Brukmanbe372b92003-08-21 22:14:26 +0000604independent of all of the other functions in the program.
Chris Lattner34ceebe2002-08-08 20:11:18 +0000605<tt>FunctionPass</tt>'s do not require that they are executed in a particular
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000606order, and <tt>FunctionPass</tt>'s do not modify external functions.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000607
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000608<p>To be explicit, <tt>FunctionPass</tt> subclasses are not allowed to:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000609
610<ol>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000611<li>Modify a Function other than the one currently being processed.</li>
612<li>Add or remove Function's from the current Module.</li>
613<li>Add or remove global variables from the current Module.</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000614<li>Maintain state across invocations of
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000615 <a href="#runOnFunction"><tt>runOnFunction</tt></a> (including global data)</li>
616</ol>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000617
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000618<p>Implementing a <tt>FunctionPass</tt> is usually straightforward (See the <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000619href="#basiccode">Hello World</a> pass for example). <tt>FunctionPass</tt>'s
620may overload three virtual methods to do their work. All of these methods
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000621should return true if they modified the program, or false if they didn't.</p>
622
623</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000624
625<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000626<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000627 <a name="doInitialization_mod">The <tt>doInitialization(Module &amp;)</tt>
628 method</a>
629</div>
630
631<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000632
Jim Laskey575df8b2006-08-04 18:10:12 +0000633<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000634 <b>virtual bool</b> doInitialization(Module &amp;M);
Jim Laskey575df8b2006-08-04 18:10:12 +0000635</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000636
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000637<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattner34ceebe2002-08-08 20:11:18 +0000638<tt>FunctionPass</tt>'s are not allowed to do. They can add and remove
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000639functions, get pointers to functions, etc. The <tt>doInitialization</tt> method
640is designed to do simple initialization type of stuff that does not depend on
641the functions being processed. The <tt>doInitialization</tt> method call is not
642scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000643fast).</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000644
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000645<p>A good example of how this method should be used is the <a
Reid Spencerca058542006-03-14 05:39:39 +0000646href="http://llvm.org/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations</a>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000647pass. This pass converts <tt>malloc</tt> and <tt>free</tt> instructions into
Misha Brukmanbe372b92003-08-21 22:14:26 +0000648platform dependent <tt>malloc()</tt> and <tt>free()</tt> function calls. It
Chris Lattner34ceebe2002-08-08 20:11:18 +0000649uses the <tt>doInitialization</tt> method to get a reference to the malloc and
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000650free functions that it needs, adding prototypes to the module if necessary.</p>
651
652</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000653
654<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000655<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000656 <a name="runOnFunction">The <tt>runOnFunction</tt> method</a>
657</div>
658
659<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000660
Jim Laskey575df8b2006-08-04 18:10:12 +0000661<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000662 <b>virtual bool</b> runOnFunction(Function &amp;F) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000663</pre></div><p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000664
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000665<p>The <tt>runOnFunction</tt> method must be implemented by your subclass to do
666the transformation or analysis work of your pass. As usual, a true value should
667be returned if the function is modified.</p>
668
669</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000670
671<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000672<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000673 <a name="doFinalization_mod">The <tt>doFinalization(Module
674 &amp;)</tt> method</a>
675</div>
676
677<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000678
Jim Laskey575df8b2006-08-04 18:10:12 +0000679<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000680 <b>virtual bool</b> doFinalization(Module &amp;M);
Jim Laskey575df8b2006-08-04 18:10:12 +0000681</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000682
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000683<p>The <tt>doFinalization</tt> method is an infrequently used method that is
684called when the pass framework has finished calling <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000685href="#runOnFunction"><tt>runOnFunction</tt></a> for every function in the
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000686program being compiled.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000687
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000688</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000689
690<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000691<div class="doc_subsection">
692 <a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
693</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000694
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000695<div class="doc_text">
696
697<p><tt>BasicBlockPass</tt>'s are just like <a
Chris Lattner34ceebe2002-08-08 20:11:18 +0000698href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
699their scope of inspection and modification to a single basic block at a time.
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000700As such, they are <b>not</b> allowed to do any of the following:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000701
702<ol>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000703<li>Modify or inspect any basic blocks outside of the current one</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000704<li>Maintain state across invocations of
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000705 <a href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a></li>
Reid Spencera1799682004-12-11 05:12:57 +0000706<li>Modify the control flow graph (by altering terminator instructions)</li>
707<li>Any of the things forbidden for
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000708 <a href="#FunctionPass"><tt>FunctionPass</tt></a>es.</li>
709</ol>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000710
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000711<p><tt>BasicBlockPass</tt>es are useful for traditional local and "peephole"
Chris Lattner34ceebe2002-08-08 20:11:18 +0000712optimizations. They may override the same <a
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000713href="#doInitialization_mod"><tt>doInitialization(Module &amp;)</tt></a> and <a
714href="#doFinalization_mod"><tt>doFinalization(Module &amp;)</tt></a> methods that <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000715href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have the following virtual methods that may also be implemented:</p>
716
717</div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000718
719<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000720<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000721 <a name="doInitialization_fn">The <tt>doInitialization(Function
722 &amp;)</tt> method</a>
723</div>
724
725<div class="doc_text">
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000726
Jim Laskey575df8b2006-08-04 18:10:12 +0000727<div class="doc_code"><pre>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000728 <b>virtual bool</b> doInitialization(Function &amp;F);
Jim Laskey575df8b2006-08-04 18:10:12 +0000729</pre></div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000730
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000731<p>The <tt>doIninitialize</tt> method is allowed to do most of the things that
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000732<tt>BasicBlockPass</tt>'s are not allowed to do, but that
733<tt>FunctionPass</tt>'s can. The <tt>doInitialization</tt> method is designed
Reid Spencera1799682004-12-11 05:12:57 +0000734to do simple initialization that does not depend on the
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000735BasicBlocks being processed. The <tt>doInitialization</tt> method call is not
736scheduled to overlap with any other pass executions (thus it should be very
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000737fast).</p>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000738
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000739</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000740
741<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000742<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000743 <a name="runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
744</div>
745
746<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000747
Jim Laskey575df8b2006-08-04 18:10:12 +0000748<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000749 <b>virtual bool</b> runOnBasicBlock(BasicBlock &amp;BB) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000750</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000751
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000752<p>Override this function to do the work of the <tt>BasicBlockPass</tt>. This
Chris Lattner34ceebe2002-08-08 20:11:18 +0000753function is not allowed to inspect or modify basic blocks other than the
754parameter, and are not allowed to modify the CFG. A true value must be returned
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000755if the basic block is modified.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000756
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000757</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000758
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000759<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000760<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000761 <a name="doFinalization_fn">The <tt>doFinalization(Function &amp;)</tt>
762 method</a>
763</div>
764
765<div class="doc_text">
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000766
Jim Laskey575df8b2006-08-04 18:10:12 +0000767<div class="doc_code"><pre>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000768 <b>virtual bool</b> doFinalization(Function &amp;F);
Jim Laskey575df8b2006-08-04 18:10:12 +0000769</pre></div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000770
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000771<p>The <tt>doFinalization</tt> method is an infrequently used method that is
772called when the pass framework has finished calling <a
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000773href="#runOnBasicBlock"><tt>runOnBasicBlock</tt></a> for every BasicBlock in the
774program being compiled. This can be used to perform per-function
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000775finalization.</p>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000776
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000777</div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000778
Brian Gaeke110f07b2003-07-17 18:53:20 +0000779<!-- ======================================================================= -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000780<div class="doc_subsection">
781 <a name="MachineFunctionPass">The <tt>MachineFunctionPass</tt> class</a>
782</div>
Brian Gaeke110f07b2003-07-17 18:53:20 +0000783
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000784<div class="doc_text">
785
Chris Lattner8e871652004-03-17 21:09:55 +0000786<p>A <tt>MachineFunctionPass</tt> is a part of the LLVM code generator that
787executes on the machine-dependent representation of each LLVM function in the
788program. A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all
Brian Gaeke110f07b2003-07-17 18:53:20 +0000789the restrictions that apply to a <tt>FunctionPass</tt> also apply to it.
Chris Lattner8e871652004-03-17 21:09:55 +0000790<tt>MachineFunctionPass</tt>es also have additional restrictions. In particular,
791<tt>MachineFunctionPass</tt>es are not allowed to do any of the following:</p>
Brian Gaeke110f07b2003-07-17 18:53:20 +0000792
793<ol>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000794<li>Modify any LLVM Instructions, BasicBlocks or Functions.</li>
795<li>Modify a MachineFunction other than the one currently being processed.</li>
796<li>Add or remove MachineFunctions from the current Module.</li>
797<li>Add or remove global variables from the current Module.</li>
798<li>Maintain state across invocations of <a
799href="#runOnMachineFunction"><tt>runOnMachineFunction</tt></a> (including global
800data)</li>
801</ol>
Brian Gaeke110f07b2003-07-17 18:53:20 +0000802
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000803</div>
Chris Lattner5e08e70f2002-09-12 17:06:43 +0000804
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000805<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +0000806<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000807 <a name="runOnMachineFunction">The <tt>runOnMachineFunction(MachineFunction
808 &amp;MF)</tt> method</a>
809</div>
810
811<div class="doc_text">
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000812
Jim Laskey575df8b2006-08-04 18:10:12 +0000813<div class="doc_code"><pre>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000814 <b>virtual bool</b> runOnMachineFunction(MachineFunction &amp;MF) = 0;
Jim Laskey575df8b2006-08-04 18:10:12 +0000815</pre></div>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000816
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000817<p><tt>runOnMachineFunction</tt> can be considered the main entry point of a
818<tt>MachineFunctionPass</tt>; that is, you should override this method to do the
819work of your <tt>MachineFunctionPass</tt>.</p>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000820
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000821<p>The <tt>runOnMachineFunction</tt> method is called on every
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000822<tt>MachineFunction</tt> in a <tt>Module</tt>, so that the
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000823<tt>MachineFunctionPass</tt> may perform optimizations on the machine-dependent
824representation of the function. If you want to get at the LLVM <tt>Function</tt>
825for the <tt>MachineFunction</tt> you're working on, use
826<tt>MachineFunction</tt>'s <tt>getFunction()</tt> accessor method -- but
827remember, you may not modify the LLVM <tt>Function</tt> or its contents from a
828<tt>MachineFunctionPass</tt>.</p>
829
830</div>
Brian Gaeke673ef4d2003-07-22 20:53:20 +0000831
Chris Lattner34ceebe2002-08-08 20:11:18 +0000832<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000833<div class="doc_section">
834 <a name="registration">Pass registration</a>
835</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000836<!-- *********************************************************************** -->
837
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000838<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000839
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000840<p>In the <a href="#basiccode">Hello World</a> example pass we illustrated how
841pass registration works, and discussed some of the reasons that it is used and
842what it does. Here we discuss how and why passes are registered.</p>
843
Chris Lattnerb693cb02006-08-27 23:18:52 +0000844<p>As we saw above, passes are registered with the <b><tt>RegisterPass</tt></b>
845template, which requires you to pass at least two
Chris Lattner34ceebe2002-08-08 20:11:18 +0000846parameters. The first parameter is the name of the pass that is to be used on
847the command line to specify that the pass should be added to a program (for
Chris Lattnerb693cb02006-08-27 23:18:52 +0000848example, with <tt>opt</tt> or <tt>bugpoint</tt>). The second argument is the
849name of the pass, which is to be used for the <tt>--help</tt> output of
850programs, as
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000851well as for debug output generated by the <tt>--debug-pass</tt> option.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000852
Chris Lattnerb693cb02006-08-27 23:18:52 +0000853<p>If you want your pass to be easily dumpable, you should
854implement the virtual <tt>print</tt> method:</p>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000855
856</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000857
858<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000859<div class="doc_subsubsection">
860 <a name="print">The <tt>print</tt> method</a>
861</div>
862
863<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000864
Jim Laskey575df8b2006-08-04 18:10:12 +0000865<div class="doc_code"><pre>
Bill Wendling22e978a2006-12-07 20:04:42 +0000866 <b>virtual void</b> print(llvm::OStream &amp;O, <b>const</b> Module *M) <b>const</b>;
Jim Laskey575df8b2006-08-04 18:10:12 +0000867</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000868
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000869<p>The <tt>print</tt> method must be implemented by "analyses" in order to print
870a human readable version of the analysis results. This is useful for debugging
871an analysis itself, as well as for other people to figure out how an analysis
Chris Lattnerb693cb02006-08-27 23:18:52 +0000872works. Use the <tt>opt -analyze</tt> argument to invoke this method.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000873
Bill Wendling22e978a2006-12-07 20:04:42 +0000874<p>The <tt>llvm::OStream</tt> parameter specifies the stream to write the results on,
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000875and the <tt>Module</tt> parameter gives a pointer to the top level module of the
Chris Lattner34ceebe2002-08-08 20:11:18 +0000876program that has been analyzed. Note however that this pointer may be null in
877certain circumstances (such as calling the <tt>Pass::dump()</tt> from a
878debugger), so it should only be used to enhance debug output, it should not be
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000879depended on.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000880
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000881</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000882
883<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000884<div class="doc_section">
885 <a name="interaction">Specifying interactions between passes</a>
886</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000887<!-- *********************************************************************** -->
888
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000889<div class="doc_text">
890
891<p>One of the main responsibilities of the <tt>PassManager</tt> is the make sure
Chris Lattner34ceebe2002-08-08 20:11:18 +0000892that passes interact with each other correctly. Because <tt>PassManager</tt>
893tries to <a href="#passmanager">optimize the execution of passes</a> it must
894know how the passes interact with each other and what dependencies exist between
895the various passes. To track this, each pass can declare the set of passes that
896are required to be executed before the current pass, and the passes which are
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000897invalidated by the current pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000898
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000899<p>Typically this functionality is used to require that analysis results are
Chris Lattner34ceebe2002-08-08 20:11:18 +0000900computed before your pass is run. Running arbitrary transformation passes can
901invalidate the computed analysis results, which is what the invalidation set
902specifies. If a pass does not implement the <tt><a
903href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000904having any prerequisite passes, and invalidating <b>all</b> other passes.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000905
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000906</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000907
908<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000909<div class="doc_subsubsection">
910 <a name="getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a>
911</div>
912
913<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000914
Jim Laskey575df8b2006-08-04 18:10:12 +0000915<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000916 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;Info) <b>const</b>;
Jim Laskey575df8b2006-08-04 18:10:12 +0000917</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000918
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000919<p>By implementing the <tt>getAnalysisUsage</tt> method, the required and
Chris Lattner34ceebe2002-08-08 20:11:18 +0000920invalidated sets may be specified for your transformation. The implementation
921should fill in the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +0000922href="http://llvm.org/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +0000923object with information about which passes are required and not invalidated. To
Chris Lattner8e871652004-03-17 21:09:55 +0000924do this, a pass may call any of the following methods on the AnalysisUsage
925object:</p>
926</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000927
Chris Lattner8e871652004-03-17 21:09:55 +0000928<!-- _______________________________________________________________________ -->
929<div class="doc_subsubsection">
930 <a name="AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a>
931</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000932
Chris Lattner8e871652004-03-17 21:09:55 +0000933<div class="doc_text">
934<p>
Reid Spencera1799682004-12-11 05:12:57 +0000935If your pass requires a previous pass to be executed (an analysis for example),
Chris Lattner8e871652004-03-17 21:09:55 +0000936it can use one of these methods to arrange for it to be run before your pass.
937LLVM has many different types of analyses and passes that can be required,
Reid Spencera1799682004-12-11 05:12:57 +0000938spanning the range from <tt>DominatorSet</tt> to <tt>BreakCriticalEdges</tt>.
939Requiring <tt>BreakCriticalEdges</tt>, for example, guarantees that there will
Chris Lattner8e871652004-03-17 21:09:55 +0000940be no critical edges in the CFG when your pass has been run.
941</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000942
Chris Lattner8e871652004-03-17 21:09:55 +0000943<p>
944Some analyses chain to other analyses to do their job. For example, an <a
945href="AliasAnalysis.html">AliasAnalysis</a> implementation is required to <a
946href="AliasAnalysis.html#chaining">chain</a> to other alias analysis passes. In
947cases where analyses chain, the <tt>addRequiredTransitive</tt> method should be
948used instead of the <tt>addRequired</tt> method. This informs the PassManager
949that the transitively required pass should be alive as long as the requiring
950pass is.
951</p>
952</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000953
Chris Lattner8e871652004-03-17 21:09:55 +0000954<!-- _______________________________________________________________________ -->
955<div class="doc_subsubsection">
956 <a name="AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a>
957</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000958
Chris Lattner8e871652004-03-17 21:09:55 +0000959<div class="doc_text">
960<p>
961One of the jobs of the PassManager is to optimize how and when analyses are run.
962In particular, it attempts to avoid recomputing data unless it needs to. For
963this reason, passes are allowed to declare that they preserve (i.e., they don't
964invalidate) an existing analysis if it's available. For example, a simple
Reid Spencera1799682004-12-11 05:12:57 +0000965constant folding pass would not modify the CFG, so it can't possibly affect the
Chris Lattner8e871652004-03-17 21:09:55 +0000966results of dominator analysis. By default, all passes are assumed to invalidate
967all others.
968</p>
969
970<p>
971The <tt>AnalysisUsage</tt> class provides several methods which are useful in
972certain circumstances that are related to <tt>addPreserved</tt>. In particular,
973the <tt>setPreservesAll</tt> method can be called to indicate that the pass does
974not modify the LLVM program at all (which is true for analyses), and the
975<tt>setPreservesCFG</tt> method can be used by transformations that change
976instructions in the program but do not modify the CFG or terminator instructions
977(note that this property is implicitly set for <a
978href="#BasicBlockPass">BasicBlockPass</a>'s).
979</p>
980
981<p>
982<tt>addPreserved</tt> is particularly useful for transformations like
983<tt>BreakCriticalEdges</tt>. This pass knows how to update a small set of loop
984and dominator related analyses if they exist, so it can preserve them, despite
985the fact that it hacks on the CFG.
986</p>
987</div>
988
989<!-- _______________________________________________________________________ -->
990<div class="doc_subsubsection">
991 <a name="AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a>
992</div>
993
994<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +0000995
Jim Laskey575df8b2006-08-04 18:10:12 +0000996<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +0000997 <i>// This is an example implementation from an analysis, which does not modify
998 // the program at all, yet has a prerequisite.</i>
Reid Spencerca058542006-03-14 05:39:39 +0000999 <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 +00001000 AU.setPreservesAll();
Reid Spencerca058542006-03-14 05:39:39 +00001001 AU.addRequired&lt;<a href="http://llvm.org/doxygen/classllvm_1_1PostDominatorTree.html">PostDominatorTree</a>&gt;();
Chris Lattner34ceebe2002-08-08 20:11:18 +00001002 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001003</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001004
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001005<p>and:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001006
Jim Laskey575df8b2006-08-04 18:10:12 +00001007<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001008 <i>// This example modifies the program, but does not modify the CFG</i>
Reid Spencerca058542006-03-14 05:39:39 +00001009 <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 +00001010 AU.setPreservesCFG();
Reid Spencerca058542006-03-14 05:39:39 +00001011 AU.addRequired&lt;<a href="http://llvm.org/doxygen/classllvm_1_1LoopInfo.html">LoopInfo</a>&gt;();
Chris Lattner34ceebe2002-08-08 20:11:18 +00001012 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001013</pre></div>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001014
1015</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001016
1017<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001018<div class="doc_subsubsection">
Chris Lattner8e871652004-03-17 21:09:55 +00001019 <a name="getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> and <tt>getAnalysisToUpdate&lt;&gt;</tt> methods</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001020</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001021
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001022<div class="doc_text">
1023
Chris Lattner8e871652004-03-17 21:09:55 +00001024<p>The <tt>Pass::getAnalysis&lt;&gt;</tt> method is automatically inherited by
1025your class, providing you with access to the passes that you declared that you
1026required with the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a>
1027method. It takes a single template argument that specifies which pass class you
1028want, and returns a reference to that pass. For example:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001029
Jim Laskey575df8b2006-08-04 18:10:12 +00001030<div class="doc_code"><pre>
Chris Lattnerbaf3f622004-03-17 21:33:32 +00001031 bool LICM::runOnFunction(Function &amp;F) {
Chris Lattner8e871652004-03-17 21:09:55 +00001032 LoopInfo &amp;LI = getAnalysis&lt;LoopInfo&gt;();
1033 ...
1034 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001035</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001036
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001037<p>This method call returns a reference to the pass desired. You may get a
1038runtime assertion failure if you attempt to get an analysis that you did not
1039declare as required in your <a
1040href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> implementation. This
1041method can be called by your <tt>run*</tt> method implementation, or by any
1042other local method invoked by your <tt>run*</tt> method.</p>
1043
Chris Lattner8e871652004-03-17 21:09:55 +00001044<p>
1045If your pass is capable of updating analyses if they exist (e.g.,
1046<tt>BreakCriticalEdges</tt>, as described above), you can use the
1047<tt>getAnalysisToUpdate</tt> method, which returns a pointer to the analysis if
1048it is active. For example:</p>
1049
Jim Laskey575df8b2006-08-04 18:10:12 +00001050<div class="doc_code"><pre>
Chris Lattner8e871652004-03-17 21:09:55 +00001051 ...
1052 if (DominatorSet *DS = getAnalysisToUpdate&lt;DominatorSet&gt;()) {
1053 <i>// A DominatorSet is active. This code will update it.</i>
1054 }
1055 ...
Jim Laskey575df8b2006-08-04 18:10:12 +00001056</pre></div>
Chris Lattner8e871652004-03-17 21:09:55 +00001057
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001058</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001059
Chris Lattner8d8a9282002-08-22 19:21:04 +00001060<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001061<div class="doc_section">
1062 <a name="analysisgroup">Implementing Analysis Groups</a>
1063</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001064<!-- *********************************************************************** -->
1065
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001066<div class="doc_text">
1067
1068<p>Now that we understand the basics of how passes are defined, how the are
1069used, and how they are required from other passes, it's time to get a little bit
Chris Lattner8d8a9282002-08-22 19:21:04 +00001070fancier. All of the pass relationships that we have seen so far are very
1071simple: one pass depends on one other specific pass to be run before it can run.
1072For many applications, this is great, for others, more flexibility is
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001073required.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001074
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001075<p>In particular, some analyses are defined such that there is a single simple
Chris Lattner8d8a9282002-08-22 19:21:04 +00001076interface to the analysis results, but multiple ways of calculating them.
1077Consider alias analysis for example. The most trivial alias analysis returns
1078"may alias" for any alias query. The most sophisticated analysis a
1079flow-sensitive, context-sensitive interprocedural analysis that can take a
1080significant amount of time to execute (and obviously, there is a lot of room
1081between these two extremes for other implementations). To cleanly support
1082situations like this, the LLVM Pass Infrastructure supports the notion of
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001083Analysis Groups.</p>
1084
1085</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001086
1087<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001088<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001089 <a name="agconcepts">Analysis Group Concepts</a>
1090</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001091
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001092<div class="doc_text">
1093
1094<p>An Analysis Group is a single simple interface that may be implemented by
Chris Lattner8d8a9282002-08-22 19:21:04 +00001095multiple different passes. Analysis Groups can be given human readable names
1096just like passes, but unlike passes, they need not derive from the <tt>Pass</tt>
1097class. An analysis group may have one or more implementations, one of which is
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001098the "default" implementation.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001099
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001100<p>Analysis groups are used by client passes just like other passes are: the
Chris Lattner8d8a9282002-08-22 19:21:04 +00001101<tt>AnalysisUsage::addRequired()</tt> and <tt>Pass::getAnalysis()</tt> methods.
1102In order to resolve this requirement, the <a href="#passmanager">PassManager</a>
1103scans the available passes to see if any implementations of the analysis group
1104are available. If none is available, the default implementation is created for
1105the pass to use. All standard rules for <A href="#interaction">interaction
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001106between passes</a> still apply.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001107
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001108<p>Although <a href="#registration">Pass Registration</a> is optional for normal
Chris Lattner8d8a9282002-08-22 19:21:04 +00001109passes, all analysis group implementations must be registered, and must use the
1110<A href="#registerag"><tt>RegisterAnalysisGroup</tt></a> template to join the
1111implementation pool. Also, a default implementation of the interface
1112<b>must</b> be registered with <A
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001113href="#registerag"><tt>RegisterAnalysisGroup</tt></a>.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001114
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001115<p>As a concrete example of an Analysis Group in action, consider the <a
Reid Spencerca058542006-03-14 05:39:39 +00001116href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001117analysis group. The default implementation of the alias analysis interface (the
1118<tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001119href="http://llvm.org/doxygen/structBasicAliasAnalysis.html">basicaa</a></tt>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001120pass) just does a few simple checks that don't require significant analysis to
1121compute (such as: two different globals can never alias each other, etc).
1122Passes that use the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001123href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001124interface (for example the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001125href="http://llvm.org/doxygen/structGCSE.html">gcse</a></tt> pass), do
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001126not care which implementation of alias analysis is actually provided, they just
1127use the designated interface.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001128
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001129<p>From the user's perspective, commands work just like normal. Issuing the
Chris Lattner8d8a9282002-08-22 19:21:04 +00001130command '<tt>opt -gcse ...</tt>' will cause the <tt>basicaa</tt> class to be
1131instantiated and added to the pass sequence. Issuing the command '<tt>opt
1132-somefancyaa -gcse ...</tt>' will cause the <tt>gcse</tt> pass to use the
1133<tt>somefancyaa</tt> alias analysis (which doesn't actually exist, it's just a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001134hypothetical example) instead.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001135
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001136</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001137
1138<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001139<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001140 <a name="registerag">Using <tt>RegisterAnalysisGroup</tt></a>
1141</div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001142
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001143<div class="doc_text">
1144
1145<p>The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
Chris Lattner8d8a9282002-08-22 19:21:04 +00001146group itself as well as add pass implementations to the analysis group. First,
1147an analysis should be registered, with a human readable name provided for it.
1148Unlike registration of passes, there is no command line argument to be specified
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001149for the Analysis Group Interface itself, because it is "abstract":</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001150
Jim Laskey575df8b2006-08-04 18:10:12 +00001151<div class="doc_code"><pre>
Reid Spencerca058542006-03-14 05:39:39 +00001152 <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 +00001153</pre></div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001154
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001155<p>Once the analysis is registered, passes can declare that they are valid
1156implementations of the interface by using the following code:</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001157
Jim Laskey575df8b2006-08-04 18:10:12 +00001158<div class="doc_code"><pre>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001159<b>namespace</b> {
1160 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
Chris Lattnerb693cb02006-08-27 23:18:52 +00001161 RegisterPass&lt;FancyAA&gt;
Chris Lattner8d8a9282002-08-22 19:21:04 +00001162 B("<i>somefancyaa</i>", "<i>A more complex alias analysis implementation</i>");
1163
1164 //<i> Declare that we implement the AliasAnalysis interface</i>
Chris Lattnerea3c2b22006-08-28 00:45:38 +00001165 RegisterAnalysisGroup&lt;<a href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>&gt; C(B);
Chris Lattner8d8a9282002-08-22 19:21:04 +00001166}
Jim Laskey575df8b2006-08-04 18:10:12 +00001167</pre></div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001168
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001169<p>This just shows a class <tt>FancyAA</tt> that is registered normally, then
1170uses the <tt>RegisterAnalysisGroup</tt> template to "join" the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001171href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a></tt>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001172analysis group. Every implementation of an analysis group should join using
1173this template. A single pass may join multiple different analysis groups with
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001174no problem.</p>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001175
Jim Laskey575df8b2006-08-04 18:10:12 +00001176<div class="doc_code"><pre>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001177<b>namespace</b> {
1178 //<i> Analysis Group implementations <b>must</b> be registered normally...</i>
Chris Lattnerb693cb02006-08-27 23:18:52 +00001179 RegisterPass&lt;<a href="http://llvm.org/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a>&gt;
Chris Lattner8d8a9282002-08-22 19:21:04 +00001180 D("<i>basicaa</i>", "<i>Basic Alias Analysis (default AA impl)</i>");
1181
1182 //<i> Declare that we implement the AliasAnalysis interface</i>
Chris Lattnerea3c2b22006-08-28 00:45:38 +00001183 RegisterAnalysisGroup&lt;<a href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a>, <b>true</b>&gt; E(D);
Chris Lattner8d8a9282002-08-22 19:21:04 +00001184}
Jim Laskey575df8b2006-08-04 18:10:12 +00001185</pre></div>
Chris Lattner8d8a9282002-08-22 19:21:04 +00001186
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001187<p>Here we show how the default implementation is specified (using the extra
Chris Lattner8d8a9282002-08-22 19:21:04 +00001188argument to the <tt>RegisterAnalysisGroup</tt> template). There must be exactly
1189one default implementation available at all times for an Analysis Group to be
1190used. Here we declare that the <tt><a
Reid Spencerca058542006-03-14 05:39:39 +00001191href="http://llvm.org/doxygen/structBasicAliasAnalysis.html">BasicAliasAnalysis</a></tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001192pass is the default implementation for the interface.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001193
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001194</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001195
1196<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001197<div class="doc_section">
Tanya Lattnerfb966ba2004-11-19 01:25:14 +00001198 <a name="passStatistics">Pass Statistics</a>
1199</div>
1200<!-- *********************************************************************** -->
1201
1202<div class="doc_text">
Tanya Lattner0b40ee62004-11-19 01:26:37 +00001203<p>The <a
Reid Spencerca058542006-03-14 05:39:39 +00001204href="http://llvm.org/doxygen/Statistic_8h-source.html"><tt>Statistic</tt></a>
John Criswell3e149a22005-11-28 23:25:41 +00001205class is designed to be an easy way to expose various success
Tanya Lattnerfb966ba2004-11-19 01:25:14 +00001206metrics from passes. These statistics are printed at the end of a
1207run, when the -stats command line option is enabled on the command
Tanya Lattner0b40ee62004-11-19 01:26:37 +00001208line. 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 +00001209
1210</div>
1211
1212
1213<!-- *********************************************************************** -->
1214<div class="doc_section">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001215 <a name="passmanager">What PassManager does</a>
1216</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001217<!-- *********************************************************************** -->
1218
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001219<div class="doc_text">
1220
1221<p>The <a
Reid Spencerca058542006-03-14 05:39:39 +00001222href="http://llvm.org/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001223<a
Reid Spencerca058542006-03-14 05:39:39 +00001224href="http://llvm.org/doxygen/classllvm_1_1PassManager.html">class</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001225takes a list of passes, ensures their <a href="#interaction">prerequisites</a>
1226are set up correctly, and then schedules passes to run efficiently. All of the
1227LLVM tools that run passes use the <tt>PassManager</tt> for execution of these
1228passes.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001229
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001230<p>The <tt>PassManager</tt> does two main things to try to reduce the execution
1231time of a series of passes:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001232
1233<ol>
1234<li><b>Share analysis results</b> - The PassManager attempts to avoid
1235recomputing analysis results as much as possible. This means keeping track of
1236which analyses are available already, which analyses get invalidated, and which
1237analyses are needed to be run for a pass. An important part of work is that the
1238<tt>PassManager</tt> tracks the exact lifetime of all analysis results, allowing
1239it to <a href="#releaseMemory">free memory</a> allocated to holding analysis
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001240results as soon as they are no longer needed.</li>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001241
1242<li><b>Pipeline the execution of passes on the program</b> - The
1243<tt>PassManager</tt> attempts to get better cache and memory usage behavior out
1244of a series of passes by pipelining the passes together. This means that, given
1245a series of consequtive <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s, it
1246will execute all of the <a href="#FunctionPass"><tt>FunctionPass</tt></a>'s on
1247the first function, then all of the <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001248href="#FunctionPass"><tt>FunctionPass</tt></a>es on the second function,
1249etc... until the entire program has been run through the passes.
Chris Lattner34ceebe2002-08-08 20:11:18 +00001250
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001251<p>This improves the cache behavior of the compiler, because it is only touching
Chris Lattner34ceebe2002-08-08 20:11:18 +00001252the LLVM program representation for a single function at a time, instead of
1253traversing the entire program. It reduces the memory consumption of compiler,
1254because, for example, only one <a
Reid Spencerca058542006-03-14 05:39:39 +00001255href="http://llvm.org/doxygen/classllvm_1_1DominatorSet.html"><tt>DominatorSet</tt></a>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001256needs to be calculated at a time. This also makes it possible some <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001257href="#SMP">interesting enhancements</a> in the future.</p></li>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001258
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001259</ol>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001260
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001261<p>The effectiveness of the <tt>PassManager</tt> is influenced directly by how
1262much information it has about the behaviors of the passes it is scheduling. For
Chris Lattner34ceebe2002-08-08 20:11:18 +00001263example, the "preserved" set is intentionally conservative in the face of an
1264unimplemented <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method.
1265Not implementing when it should be implemented will have the effect of not
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001266allowing any analysis results to live across the execution of your pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001267
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001268<p>The <tt>PassManager</tt> class exposes a <tt>--debug-pass</tt> command line
Chris Lattner34ceebe2002-08-08 20:11:18 +00001269options that is useful for debugging pass execution, seeing how things work, and
1270diagnosing when you should be preserving more analyses than you currently are
1271(To get information about all of the variants of the <tt>--debug-pass</tt>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001272option, just type '<tt>opt --help-hidden</tt>').</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001273
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001274<p>By using the <tt>--debug-pass=Structure</tt> option, for example, we can see
1275how our <a href="#basiccode">Hello World</a> pass interacts with other passes.
1276Lets try it out with the <tt>gcse</tt> and <tt>licm</tt> passes:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001277
Jim Laskey575df8b2006-08-04 18:10:12 +00001278<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +00001279$ opt -load ../../../Debug/lib/Hello.so -gcse -licm --debug-pass=Structure &lt; hello.bc &gt; /dev/null
Chris Lattner34ceebe2002-08-08 20:11:18 +00001280Module Pass Manager
1281 Function Pass Manager
1282 Dominator Set Construction
1283 Immediate Dominators Construction
1284 Global Common Subexpression Elimination
1285-- Immediate Dominators Construction
1286-- Global Common Subexpression Elimination
1287 Natural Loop Construction
1288 Loop Invariant Code Motion
1289-- Natural Loop Construction
1290-- Loop Invariant Code Motion
1291 Module Verifier
1292-- Dominator Set Construction
1293-- Module Verifier
1294 Bytecode Writer
1295--Bytecode Writer
Jim Laskey575df8b2006-08-04 18:10:12 +00001296</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001297
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001298<p>This output shows us when passes are constructed and when the analysis
1299results are known to be dead (prefixed with '<tt>--</tt>'). Here we see that
1300GCSE uses dominator and immediate dominator information to do its job. The LICM
1301pass uses natural loop information, which uses dominator sets, but not immediate
Chris Lattner34ceebe2002-08-08 20:11:18 +00001302dominators. Because immediate dominators are no longer useful after the GCSE
1303pass, it is immediately destroyed. The dominator sets are then reused to
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001304compute natural loop information, which is then used by the LICM pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001305
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001306<p>After the LICM pass, the module verifier runs (which is automatically added
1307by the '<tt>opt</tt>' tool), which uses the dominator set to check that the
Chris Lattner34ceebe2002-08-08 20:11:18 +00001308resultant LLVM code is well formed. After it finishes, the dominator set
1309information is destroyed, after being computed once, and shared by three
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001310passes.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001311
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001312<p>Lets see how this changes when we run the <a href="#basiccode">Hello
1313World</a> pass in between the two passes:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001314
Jim Laskey575df8b2006-08-04 18:10:12 +00001315<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +00001316$ 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 +00001317Module Pass Manager
1318 Function Pass Manager
1319 Dominator Set Construction
1320 Immediate Dominators Construction
1321 Global Common Subexpression Elimination
1322<b>-- Dominator Set Construction</b>
1323-- Immediate Dominators Construction
1324-- Global Common Subexpression Elimination
1325<b> Hello World Pass
1326-- Hello World Pass
1327 Dominator Set Construction</b>
1328 Natural Loop Construction
1329 Loop Invariant Code Motion
1330-- Natural Loop Construction
1331-- Loop Invariant Code Motion
1332 Module Verifier
1333-- Dominator Set Construction
1334-- Module Verifier
1335 Bytecode Writer
1336--Bytecode Writer
1337Hello: __main
1338Hello: puts
1339Hello: main
Jim Laskey575df8b2006-08-04 18:10:12 +00001340</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001341
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001342<p>Here we see that the <a href="#basiccode">Hello World</a> pass has killed the
Chris Lattner34ceebe2002-08-08 20:11:18 +00001343Dominator Set pass, even though it doesn't modify the code at all! To fix this,
1344we need to add the following <a
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001345href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method to our pass:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001346
Jim Laskey575df8b2006-08-04 18:10:12 +00001347<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001348 <i>// We don't modify the program, so we preserve all analyses</i>
1349 <b>virtual void</b> getAnalysisUsage(AnalysisUsage &amp;AU) <b>const</b> {
1350 AU.setPreservesAll();
1351 }
Jim Laskey575df8b2006-08-04 18:10:12 +00001352</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001353
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001354<p>Now when we run our pass, we get this output:</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001355
Jim Laskey575df8b2006-08-04 18:10:12 +00001356<div class="doc_code"><pre>
Reid Spencer4a1ab182005-01-11 05:16:23 +00001357$ 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 +00001358Pass Arguments: -gcse -hello -licm
1359Module Pass Manager
1360 Function Pass Manager
1361 Dominator Set Construction
1362 Immediate Dominators Construction
1363 Global Common Subexpression Elimination
1364-- Immediate Dominators Construction
1365-- Global Common Subexpression Elimination
1366 Hello World Pass
1367-- Hello World Pass
1368 Natural Loop Construction
1369 Loop Invariant Code Motion
1370-- Loop Invariant Code Motion
1371-- Natural Loop Construction
1372 Module Verifier
1373-- Dominator Set Construction
1374-- Module Verifier
1375 Bytecode Writer
1376--Bytecode Writer
1377Hello: __main
1378Hello: puts
1379Hello: main
Jim Laskey575df8b2006-08-04 18:10:12 +00001380</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001381
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001382<p>Which shows that we don't accidentally invalidate dominator information
1383anymore, and therefore do not have to compute it twice.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001384
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001385</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001386
1387<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001388<div class="doc_subsubsection">
1389 <a name="releaseMemory">The <tt>releaseMemory</tt> method</a>
1390</div>
1391
1392<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +00001393
Jim Laskey575df8b2006-08-04 18:10:12 +00001394<div class="doc_code"><pre>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001395 <b>virtual void</b> releaseMemory();
Jim Laskey575df8b2006-08-04 18:10:12 +00001396</pre></div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001397
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001398<p>The <tt>PassManager</tt> automatically determines when to compute analysis
Chris Lattner34ceebe2002-08-08 20:11:18 +00001399results, and how long to keep them around for. Because the lifetime of the pass
1400object itself is effectively the entire duration of the compilation process, we
1401need some way to free analysis results when they are no longer useful. The
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001402<tt>releaseMemory</tt> virtual method is the way to do this.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001403
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001404<p>If you are writing an analysis or any other pass that retains a significant
Chris Lattner34ceebe2002-08-08 20:11:18 +00001405amount of state (for use by another pass which "requires" your pass and uses the
1406<a href="#getAnalysis">getAnalysis</a> method) you should implement
1407<tt>releaseMEmory</tt> to, well, release the memory allocated to maintain this
1408internal state. This method is called after the <tt>run*</tt> method for the
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001409class, before the next call of <tt>run*</tt> in your pass.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001410
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001411</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001412
1413<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001414<div class="doc_section">
Jim Laskey575df8b2006-08-04 18:10:12 +00001415 <a name="registering">Registering dynamically loaded passes</a>
1416</div>
1417<!-- *********************************************************************** -->
1418
1419<div class="doc_text">
1420
1421<p><i>Size matters</i> when constructing production quality tools using llvm,
1422both for the purposes of distribution, and for regulating the resident code size
1423when running on the target system. Therefore, it becomes desirable to
1424selectively use some passes, while omitting others and maintain the flexibility
1425to change configurations later on. You want to be able to do all this, and,
1426provide feedback to the user. This is where pass registration comes into
1427play.</p>
1428
1429<p>The fundamental mechanisms for pass registration are the
1430<tt>MachinePassRegistry</tt> class and subclasses of
1431<tt>MachinePassRegistryNode</tt>.</p>
1432
1433<p>An instance of <tt>MachinePassRegistry</tt> is used to maintain a list of
1434<tt>MachinePassRegistryNode</tt> objects. This instance maintains the list and
1435communicates additions and deletions to the command line interface.</p>
1436
1437<p>An instance of <tt>MachinePassRegistryNode</tt> subclass is used to maintain
1438information provided about a particular pass. This information includes the
1439command line name, the command help string and the address of the function used
1440to create an instance of the pass. A global static constructor of one of these
1441instances <i>registers</i> with a corresponding <tt>MachinePassRegistry</tt>,
1442the static destructor <i>unregisters</i>. Thus a pass that is statically linked
1443in the tool will be registered at start up. A dynamically loaded pass will
1444register on load and unregister at unload.</p>
1445
1446</div>
1447
1448<!-- _______________________________________________________________________ -->
1449<div class="doc_subsection">
1450 <a name="registering_existing">Using existing registries</a>
1451</div>
1452
1453<div class="doc_text">
1454
1455<p>There are predefined registries to track instruction scheduling
1456(<tt>RegisterScheduler</tt>) and register allocation (<tt>RegisterRegAlloc</tt>)
1457machine passes. Here we will describe how to <i>register</i> a register
1458allocator machine pass.</p>
1459
1460<p>Implement your register allocator machine pass. In your register allocator
1461.cpp file add the following include;</p>
1462
1463<div class="doc_code"><pre>
Chris Lattner4bde9fb2006-08-11 16:37:02 +00001464 #include "llvm/CodeGen/RegAllocRegistry.h"
Jim Laskey575df8b2006-08-04 18:10:12 +00001465</pre></div>
1466
1467<p>Also in your register allocator .cpp file, define a creator function in the
1468form; </p>
1469
1470<div class="doc_code"><pre>
1471 FunctionPass *createMyRegisterAllocator() {
1472 return new MyRegisterAllocator();
1473 }
1474</pre></div>
1475
1476<p>Note that the signature of this function should match the type of
1477<tt>RegisterRegAlloc::FunctionPassCtor</tt>. In the same file add the
1478"installing" declaration, in the form;</p>
1479
1480<div class="doc_code"><pre>
1481 static RegisterRegAlloc myRegAlloc("myregalloc",
1482 " my register allocator help string",
1483 createMyRegisterAllocator);
1484</pre></div>
1485
1486<p>Note the two spaces prior to the help string produces a tidy result on the
1487--help query.</p>
1488
1489<div class="doc_code"><pre>
1490$ llc --help
1491 ...
1492 -regalloc - Register allocator to use: (default = linearscan)
1493 =linearscan - linear scan register allocator
1494 =local - local register allocator
1495 =simple - simple register allocator
1496 =myregalloc - my register allocator help string
1497 ...
1498</pre></div>
1499
1500<p>And that's it. The user is now free to use <tt>-regalloc=myregalloc</tt> as
1501an option. Registering instruction schedulers is similar except use the
Chris Lattner4bde9fb2006-08-11 16:37:02 +00001502<tt>RegisterScheduler</tt> class. Note that the
1503<tt>RegisterScheduler::FunctionPassCtor</tt> is significantly different from
Jim Laskey575df8b2006-08-04 18:10:12 +00001504<tt>RegisterRegAlloc::FunctionPassCtor</tt>.</p>
1505
1506<p>To force the load/linking of your register allocator into the llc/lli tools,
1507add your creator function's global declaration to "Passes.h" and add a "pseudo"
1508call line to <tt>llvm/Codegen/LinkAllCodegenComponents.h</tt>.</p>
1509
1510</div>
1511
1512
1513<!-- _______________________________________________________________________ -->
1514<div class="doc_subsection">
1515 <a name="registering_new">Creating new registries</a>
1516</div>
1517
1518<div class="doc_text">
1519
1520<p>The easiest way to get started is to clone one of the existing registries; we
1521recommend <tt>llvm/CodeGen/RegAllocRegistry.h</tt>. The key things to modify
1522are the class name and the <tt>FunctionPassCtor</tt> type.</p>
1523
1524<p>Then you need to declare the registry. Example: if your pass registry is
1525<tt>RegisterMyPasses</tt> then define;</p>
1526
1527<div class="doc_code"><pre>
1528MachinePassRegistry RegisterMyPasses::Registry;
1529</pre></div>
1530
1531<p>And finally, declare the command line option for your passes. Example:</p>
1532
1533<div class="doc_code"><pre>
1534 cl::opt&lt;RegisterMyPasses::FunctionPassCtor, false,
1535 RegisterPassParser&lt;RegisterMyPasses&gt &gt
1536 MyPassOpt("mypass",
1537 cl::init(&amp;createDefaultMyPass),
1538 cl::desc("my pass option help"));
1539</pre></div>
1540
1541<p>Here the command option is "mypass", with createDefaultMyPass as the default
1542creator.</p>
1543
1544</div>
1545
1546<!-- *********************************************************************** -->
1547<div class="doc_section">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001548 <a name="debughints">Using GDB with dynamically loaded passes</a>
1549</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001550<!-- *********************************************************************** -->
1551
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001552<div class="doc_text">
1553
1554<p>Unfortunately, using GDB with dynamically loaded passes is not as easy as it
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001555should be. First of all, you can't set a breakpoint in a shared object that has
1556not been loaded yet, and second of all there are problems with inlined functions
1557in shared objects. Here are some suggestions to debugging your pass with
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001558GDB.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001559
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001560<p>For sake of discussion, I'm going to assume that you are debugging a
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001561transformation invoked by <tt>opt</tt>, although nothing described here depends
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001562on that.</p>
1563
1564</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001565
1566<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001567<div class="doc_subsubsection">
1568 <a name="breakpoint">Setting a breakpoint in your pass</a>
1569</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001570
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001571<div class="doc_text">
1572
1573<p>First thing you do is start <tt>gdb</tt> on the <tt>opt</tt> process:</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001574
Jim Laskey575df8b2006-08-04 18:10:12 +00001575<div class="doc_code"><pre>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001576$ <b>gdb opt</b>
1577GNU gdb 5.0
1578Copyright 2000 Free Software Foundation, Inc.
1579GDB is free software, covered by the GNU General Public License, and you are
1580welcome to change it and/or distribute copies of it under certain conditions.
1581Type "show copying" to see the conditions.
1582There is absolutely no warranty for GDB. Type "show warranty" for details.
1583This GDB was configured as "sparc-sun-solaris2.6"...
1584(gdb)
Jim Laskey575df8b2006-08-04 18:10:12 +00001585</pre></div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001586
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001587<p>Note that <tt>opt</tt> has a lot of debugging information in it, so it takes
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001588time to load. Be patient. Since we cannot set a breakpoint in our pass yet
1589(the shared object isn't loaded until runtime), we must execute the process, and
1590have it stop before it invokes our pass, but after it has loaded the shared
1591object. The most foolproof way of doing this is to set a breakpoint in
1592<tt>PassManager::run</tt> and then run the process with the arguments you
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001593want:</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001594
Jim Laskey575df8b2006-08-04 18:10:12 +00001595<div class="doc_code"><pre>
Reid Spencer48cf6a62006-09-28 16:53:47 +00001596(gdb) <b>break llvm::PassManager::run</b>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001597Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
Chris Lattner07275182005-04-21 04:52:37 +00001598(gdb) <b>run test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[passoption]</b>
1599Starting program: opt test.bc -load $(LLVMTOP)/llvm/Debug/lib/[libname].so -[passoption]
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001600Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
160170 bool PassManager::run(Module &amp;M) { return PM-&gt;run(M); }
1602(gdb)
Jim Laskey575df8b2006-08-04 18:10:12 +00001603</pre></div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001604
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001605<p>Once the <tt>opt</tt> stops in the <tt>PassManager::run</tt> method you are
1606now free to set breakpoints in your pass so that you can trace through execution
1607or do other standard debugging stuff.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001608
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001609</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001610
1611<!-- _______________________________________________________________________ -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001612<div class="doc_subsubsection">
1613 <a name="debugmisc">Miscellaneous Problems</a>
1614</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001615
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001616<div class="doc_text">
1617
1618<p>Once you have the basics down, there are a couple of problems that GDB has,
1619some with solutions, some without.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001620
1621<ul>
1622<li>Inline functions have bogus stack information. In general, GDB does a
1623pretty good job getting stack traces and stepping through inline functions.
1624When a pass is dynamically loaded however, it somehow completely loses this
1625capability. The only solution I know of is to de-inline a function (move it
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001626from the body of a class to a .cpp file).</li>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001627
1628<li>Restarting the program breaks breakpoints. After following the information
1629above, you have succeeded in getting some breakpoints planted in your pass. Nex
1630thing you know, you restart the program (i.e., you type '<tt>run</tt>' again),
1631and you start getting errors about breakpoints being unsettable. The only way I
1632have found to "fix" this problem is to <tt>delete</tt> the breakpoints that are
1633already set in your pass, run the program, and re-set the breakpoints once
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001634execution stops in <tt>PassManager::run</tt>.</li>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001635
1636</ul>
1637
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001638<p>Hopefully these tips will help with common case debugging situations. If
1639you'd like to contribute some tips of your own, just contact <a
1640href="mailto:sabre@nondot.org">Chris</a>.</p>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001641
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001642</div>
Chris Lattner8a97c8a2002-09-06 02:02:58 +00001643
1644<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001645<div class="doc_section">
1646 <a name="future">Future extensions planned</a>
1647</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001648<!-- *********************************************************************** -->
1649
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001650<div class="doc_text">
1651
1652<p>Although the LLVM Pass Infrastructure is very capable as it stands, and does
Chris Lattner34ceebe2002-08-08 20:11:18 +00001653some nifty stuff, there are things we'd like to add in the future. Here is
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001654where we are going:</p>
1655
1656</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001657
1658<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001659<div class="doc_subsubsection">
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001660 <a name="SMP">Multithreaded LLVM</a>
1661</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001662
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001663<div class="doc_text">
1664
1665<p>Multiple CPU machines are becoming more common and compilation can never be
Chris Lattner34ceebe2002-08-08 20:11:18 +00001666fast enough: obviously we should allow for a multithreaded compiler. Because of
1667the semantics defined for passes above (specifically they cannot maintain state
1668across invocations of their <tt>run*</tt> methods), a nice clean way to
1669implement a multithreaded compiler would be for the <tt>PassManager</tt> class
Misha Brukman7fdaab42003-07-14 17:20:40 +00001670to create multiple instances of each pass object, and allow the separate
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001671instances to be hacking on different parts of the program at the same time.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001672
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001673<p>This implementation would prevent each of the passes from having to implement
Chris Lattner34ceebe2002-08-08 20:11:18 +00001674multithreaded constructs, requiring only the LLVM core to have locking in a few
1675places (for global resources). Although this is a simple extension, we simply
1676haven't had time (or multiprocessor machines, thus a reason) to implement this.
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001677Despite that, we have kept the LLVM passes SMP ready, and you should too.</p>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001678
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001679</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001680
1681<!-- _______________________________________________________________________ -->
Chris Lattner8e871652004-03-17 21:09:55 +00001682<div class="doc_subsubsection">
Chris Lattner3c11c042004-09-20 04:36:29 +00001683<a name="PassFunctionPass"><tt>ModulePass</tt>es requiring <tt>FunctionPass</tt>es</a>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001684</div>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001685
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001686<div class="doc_text">
Chris Lattner34ceebe2002-08-08 20:11:18 +00001687
Chris Lattner3c11c042004-09-20 04:36:29 +00001688<p>Currently it is illegal for a <a href="#ModulePass"><tt>ModulePass</tt></a>
1689to require a <a href="#FunctionPass"><tt>FunctionPass</tt></a>. This is because
1690there is only one instance of the <a
1691href="#FunctionPass"><tt>FunctionPass</tt></a> object ever created, thus nowhere
1692to store information for all of the functions in the program at the same time.
1693Although this has come up a couple of times before, this has always been worked
1694around by factoring one big complicated pass into a global and an
1695interprocedural part, both of which are distinct. In the future, it would be
1696nice to have this though.</p>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001697
1698<p>Note that it is no problem for a <a
Chris Lattner34ceebe2002-08-08 20:11:18 +00001699href="#FunctionPass"><tt>FunctionPass</tt></a> to require the results of a <a
Chris Lattner3c11c042004-09-20 04:36:29 +00001700href="#ModulePass"><tt>ModulePass</tt></a>, only the other way around.</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<!-- *********************************************************************** -->
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001705<hr>
1706<address>
1707 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1708 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1709 <a href="http://validator.w3.org/check/referer"><img
1710 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
Chris Lattner34ceebe2002-08-08 20:11:18 +00001711
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001712 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencerca058542006-03-14 05:39:39 +00001713 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Misha Brukmanf5edf1e2004-01-15 18:34:11 +00001714 Last modified: $Date$
1715</address>
1716
1717</body>
1718</html>