</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>The LLVM Pass Framework is an important part of the LLVM system, because LLVM
passes are where most of the interesting parts of the compiler exist. Passes
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>Here we describe how to write the "hello world" of passes. The "Hello" pass
is designed to simply print out the name of non-external functions that exist in
inspects it. The source code and files for this pass are available in the LLVM
source tree in the <tt>lib/Transforms/Hello</tt> directory.</p>
-</div>
-
<!-- ======================================================================= -->
<h3>
<a name="makefile">Setting up the build environment</a>
</h3>
-<div class="doc_text">
+<div>
<p>First, configure and build LLVM. This needs to be done directly inside the
LLVM source tree rather than in a separate objects directory.
<a name="basiccode">Basic code required</a>
</h3>
-<div class="doc_text">
+<div>
<p>Now that we have a way to compile our new pass, we just have to write it.
Start out with:</p>
<a name="running">Running a pass with <tt>opt</tt></a>
</h3>
-<div class="doc_text">
+<div>
<p>Now that you have a brand new shiny shared object file, we can use the
<tt>opt</tt> command to run an LLVM program through your pass. Because you
</div>
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="passtype">Pass classes and requirements</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>One of the first things that you should do when designing a new pass is to
decide what class you should subclass for your pass. The <a
optimize how passes are run, so that the resultant compiler isn't unnecessarily
slow.</p>
-</div>
-
<!-- ======================================================================= -->
<h3>
<a name="ImmutablePass">The <tt>ImmutablePass</tt> class</a>
</h3>
-<div class="doc_text">
+<div>
<p>The most plain and boring type of pass is the "<tt><a
href="http://llvm.org/doxygen/classllvm_1_1ImmutablePass.html">ImmutablePass</a></tt>"
<a name="ModulePass">The <tt>ModulePass</tt> class</a>
</h3>
-<div class="doc_text">
+<div>
<p>The "<tt><a
href="http://llvm.org/doxygen/classllvm_1_1ModulePass.html">ModulePass</a></tt>"
<tt>ModulePass</tt> and overload the <tt>runOnModule</tt> method with the
following signature:</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="runOnModule">The <tt>runOnModule</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> runOnModule(Module &M) = 0;
</div>
+</div>
+
<!-- ======================================================================= -->
<h3>
<a name="CallGraphSCCPass">The <tt>CallGraphSCCPass</tt> class</a>
</h3>
-<div class="doc_text">
+<div>
<p>The "<tt><a
href="http://llvm.org/doxygen/classllvm_1_1CallGraphSCCPass.html">CallGraphSCCPass</a></tt>"
methods described below should return true if they modified the program, or
false if they didn't.</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="doInitialization_scc">
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doInitialization(CallGraph &CG);
<a name="runOnSCC">The <tt>runOnSCC</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> runOnSCC(CallGraphSCC &SCC) = 0;
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doFinalization(CallGraph &CG);
</div>
+</div>
+
<!-- ======================================================================= -->
<h3>
<a name="FunctionPass">The <tt>FunctionPass</tt> class</a>
</h3>
-<div class="doc_text">
+<div>
<p>In contrast to <tt>ModulePass</tt> subclasses, <tt><a
href="http://llvm.org/doxygen/classllvm_1_1Pass.html">FunctionPass</a></tt>
may overload three virtual methods to do their work. All of these methods
should return true if they modified the program, or false if they didn't.</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="doInitialization_mod">
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doInitialization(Module &M);
<a name="runOnFunction">The <tt>runOnFunction</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> runOnFunction(Function &F) = 0;
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doFinalization(Module &M);
</div>
+</div>
+
<!-- ======================================================================= -->
<h3>
<a name="LoopPass">The <tt>LoopPass</tt> class </a>
</h3>
-<div class="doc_text">
+<div>
<p> All <tt>LoopPass</tt> execute on each loop in the function independent of
all of the other loops in the function. <tt>LoopPass</tt> processes loops in
straightforward. <tt>LoopPass</tt>'s may overload three virtual methods to
do their work. All these methods should return true if they modified the
program, or false if they didn't. </p>
-</div>
<!-- _______________________________________________________________________ -->
<h4>
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doInitialization(Loop *, LPPassManager &LPM);
<a name="runOnLoop">The <tt>runOnLoop</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> runOnLoop(Loop *, LPPassManager &LPM) = 0;
<a name="doFinalization_loop">The <tt>doFinalization()</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doFinalization();
</div>
+</div>
+
<!-- ======================================================================= -->
<h3>
<a name="RegionPass">The <tt>RegionPass</tt> class </a>
</h3>
-<div class="doc_text">
+<div>
<p> <tt>RegionPass</tt> is similar to <a href="#LoopPass"><tt>LoopPass</tt></a>,
but executes on each single entry single exit region in the function.
<tt>RegionPass</tt> to implement your own region pass. All these
methods should return true if they modified the program, or false if they didn not.
</p>
-</div>
<!-- _______________________________________________________________________ -->
<h4>
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doInitialization(Region *, RGPassManager &RGM);
<a name="runOnRegion">The <tt>runOnRegion</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> runOnRegion(Region *, RGPassManager &RGM) = 0;
<a name="doFinalization_region">The <tt>doFinalization()</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doFinalization();
</div>
-
+</div>
<!-- ======================================================================= -->
<h3>
<a name="BasicBlockPass">The <tt>BasicBlockPass</tt> class</a>
</h3>
-<div class="doc_text">
+<div>
<p><tt>BasicBlockPass</tt>'s are just like <a
href="#FunctionPass"><tt>FunctionPass</tt></a>'s, except that they must limit
href="#doFinalization_mod"><tt>doFinalization(Module &)</tt></a> methods that <a
href="#FunctionPass"><tt>FunctionPass</tt></a>'s have, but also have the following virtual methods that may also be implemented:</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="doInitialization_fn">
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doInitialization(Function &F);
<a name="runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> runOnBasicBlock(BasicBlock &BB) = 0;
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> doFinalization(Function &F);
</div>
+</div>
+
<!-- ======================================================================= -->
<h3>
<a name="MachineFunctionPass">The <tt>MachineFunctionPass</tt> class</a>
</h3>
-<div class="doc_text">
+<div>
<p>A <tt>MachineFunctionPass</tt> is a part of the LLVM code generator that
executes on the machine-dependent representation of each LLVM function in the
data)</li>
</ol>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="runOnMachineFunction">
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual bool</b> runOnMachineFunction(MachineFunction &MF) = 0;
</div>
+</div>
+
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="registration">Pass registration</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>In the <a href="#basiccode">Hello World</a> example pass we illustrated how
pass registration works, and discussed some of the reasons that it is used and
<p>If you want your pass to be easily dumpable, you should
implement the virtual <tt>print</tt> method:</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="print">The <tt>print</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual void</b> print(std::ostream &O, <b>const</b> Module *M) <b>const</b>;
</div>
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="interaction">Specifying interactions between passes</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>One of the main responsibilities of the <tt>PassManager</tt> is to make sure
that passes interact with each other correctly. Because <tt>PassManager</tt>
href="#getAnalysisUsage">getAnalysisUsage</a></tt> method, it defaults to not
having any prerequisite passes, and invalidating <b>all</b> other passes.</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="getAnalysisUsage">The <tt>getAnalysisUsage</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual void</b> getAnalysisUsage(AnalysisUsage &Info) <b>const</b>;
</a>
</h4>
-<div class="doc_text">
+<div>
<p>
If your pass requires a previous pass to be executed (an analysis for example),
it can use one of these methods to arrange for it to be run before your pass.
</a>
</h4>
-<div class="doc_text">
+<div>
<p>
One of the jobs of the PassManager is to optimize how and when analyses are run.
In particular, it attempts to avoid recomputing data unless it needs to. For
</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<i>// This example modifies the program, but does not modify the CFG</i>
</a>
</h4>
-<div class="doc_text">
+<div>
<p>The <tt>Pass::getAnalysis<></tt> method is automatically inherited by
your class, providing you with access to the passes that you declared that you
</div>
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="analysisgroup">Implementing Analysis Groups</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>Now that we understand the basics of how passes are defined, how they are
used, and how they are required from other passes, it's time to get a little bit
situations like this, the LLVM Pass Infrastructure supports the notion of
Analysis Groups.</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="agconcepts">Analysis Group Concepts</a>
</h4>
-<div class="doc_text">
+<div>
<p>An Analysis Group is a single simple interface that may be implemented by
multiple different passes. Analysis Groups can be given human readable names
<a name="registerag">Using <tt>RegisterAnalysisGroup</tt></a>
</h4>
-<div class="doc_text">
+<div>
<p>The <tt>RegisterAnalysisGroup</tt> template is used to register the analysis
group itself, while the <tt>INITIALIZE_AG_PASS</tt> is used to add pass
</div>
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="passStatistics">Pass Statistics</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>The <a
href="http://llvm.org/doxygen/Statistic_8h-source.html"><tt>Statistic</tt></a>
class is designed to be an easy way to expose various success
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>The <a
href="http://llvm.org/doxygen/PassManager_8h-source.html"><tt>PassManager</tt></a>
<p>Which shows that we don't accidentally invalidate dominator information
anymore, and therefore do not have to compute it twice.</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="releaseMemory">The <tt>releaseMemory</tt> method</a>
</h4>
-<div class="doc_text">
+<div>
<div class="doc_code"><pre>
<b>virtual void</b> releaseMemory();
</div>
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="registering">Registering dynamically loaded passes</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p><i>Size matters</i> when constructing production quality tools using llvm,
both for the purposes of distribution, and for regulating the resident code size
in the tool will be registered at start up. A dynamically loaded pass will
register on load and unregister at unload.</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h3>
<a name="registering_existing">Using existing registries</a>
</h3>
-<div class="doc_text">
+<div>
<p>There are predefined registries to track instruction scheduling
(<tt>RegisterScheduler</tt>) and register allocation (<tt>RegisterRegAlloc</tt>)
<a name="registering_new">Creating new registries</a>
</h3>
-<div class="doc_text">
+<div>
<p>The easiest way to get started is to clone one of the existing registries; we
recommend <tt>llvm/CodeGen/RegAllocRegistry.h</tt>. The key things to modify
</div>
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="debughints">Using GDB with dynamically loaded passes</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>Unfortunately, using GDB with dynamically loaded passes is not as easy as it
should be. First of all, you can't set a breakpoint in a shared object that has
transformation invoked by <tt>opt</tt>, although nothing described here depends
on that.</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="breakpoint">Setting a breakpoint in your pass</a>
</h4>
-<div class="doc_text">
+<div>
<p>First thing you do is start <tt>gdb</tt> on the <tt>opt</tt> process:</p>
<a name="debugmisc">Miscellaneous Problems</a>
</h4>
-<div class="doc_text">
+<div>
<p>Once you have the basics down, there are a couple of problems that GDB has,
some with solutions, some without.</p>
</div>
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="future">Future extensions planned</a>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>Although the LLVM Pass Infrastructure is very capable as it stands, and does
some nifty stuff, there are things we'd like to add in the future. Here is
where we are going:</p>
-</div>
-
<!-- _______________________________________________________________________ -->
<h4>
<a name="SMP">Multithreaded LLVM</a>
</h4>
-<div class="doc_text">
+<div>
<p>Multiple CPU machines are becoming more common and compilation can never be
fast enough: obviously we should allow for a multithreaded compiler. Because of
</div>
+</div>
+
<!-- *********************************************************************** -->
<hr>
<address>