Skip to content

Commit 3c5f220

Browse files
author
John Criswell
committed
Slurping up Revision 1.25 of this document, which is described as:
"Rewrite the second on AnalysisUsage usage. This documents the new addRequiredTransitive member that Misha added, and explains the whole concept a lot better. Also, the document used incorrect "subsubsection" tags instead of "doc_subsubsection" which this fixes." llvm-svn: 12476
1 parent 3f024e9 commit 3c5f220

File tree

1 file changed

+118
-73
lines changed

1 file changed

+118
-73
lines changed

llvm/docs/WritingAnLLVMPass.html

Lines changed: 118 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@
5959
<ul>
6060
<li><a href="#getAnalysisUsage">The <tt>getAnalysisUsage</tt>
6161
method</a></li>
62-
<li><a href="#getAnalysis">The <tt>getAnalysis</tt> method</a></li>
62+
<li><a href="#AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a></li>
63+
<li><a href="#AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a></li>
64+
<li><a href="#AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a></li>
65+
<li><a href="#getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> and <tt>getAnalysisToUpdate&lt;&gt;</tt> methods</a></li>
6366
</ul></li>
6467
<li><a href="#analysisgroup">Implementing Analysis Groups</a>
6568
<ul>
@@ -171,12 +174,9 @@
171174
<p>This makefile specifies that all of the <tt>.cpp</tt> files in the current
172175
directory are to be compiled and linked together into a
173176
<tt>lib/Debug/libhello.so</tt> shared object that can be dynamically loaded by
174-
the <tt>opt</tt> or <tt>analyze</tt> tools.</p>
175-
176-
<p>
177-
Note that the suffix of the shared library may differ from the example above if
178-
your system uses a different suffix by default.
179-
</p>
177+
the <tt>opt</tt> or <tt>analyze</tt> tools. If your operating system uses a
178+
suffix other than .so (such as windows of Mac OS/X), the appropriate extension
179+
will be used.</p>
180180

181181
<p>Now that we have the build scripts set up, we just need to write the code for
182182
the pass itself.</p>
@@ -487,7 +487,7 @@
487487
</div>
488488

489489
<!-- _______________________________________________________________________ -->
490-
<div class="subsubsection">
490+
<div class="doc_subsubsection">
491491
<a name="doInitialization_mod">The <tt>doInitialization(Module &amp;)</tt>
492492
method</a>
493493
</div>
@@ -516,7 +516,7 @@
516516
</div>
517517

518518
<!-- _______________________________________________________________________ -->
519-
<div class="subsubsection">
519+
<div class="doc_subsubsection">
520520
<a name="runOnFunction">The <tt>runOnFunction</tt> method</a>
521521
</div>
522522

@@ -533,7 +533,7 @@
533533
</div>
534534

535535
<!-- _______________________________________________________________________ -->
536-
<div class="subsubsection">
536+
<div class="doc_subsubsection">
537537
<a name="doFinalization_mod">The <tt>doFinalization(Module
538538
&amp;)</tt> method</a>
539539
</div>
@@ -581,7 +581,7 @@
581581
</div>
582582

583583
<!-- _______________________________________________________________________ -->
584-
<div class="subsubsection">
584+
<div class="doc_subsubsection">
585585
<a name="doInitialization_fn">The <tt>doInitialization(Function
586586
&amp;)</tt> method</a>
587587
</div>
@@ -603,7 +603,7 @@
603603
</div>
604604

605605
<!-- _______________________________________________________________________ -->
606-
<div class="subsubsection">
606+
<div class="doc_subsubsection">
607607
<a name="runOnBasicBlock">The <tt>runOnBasicBlock</tt> method</a>
608608
</div>
609609

@@ -621,7 +621,7 @@
621621
</div>
622622

623623
<!-- _______________________________________________________________________ -->
624-
<div class="subsubsection">
624+
<div class="doc_subsubsection">
625625
<a name="doFinalization_fn">The <tt>doFinalization(Function &amp;)</tt>
626626
method</a>
627627
</div>
@@ -647,14 +647,12 @@
647647

648648
<div class="doc_text">
649649

650-
<p>A <tt>MachineFunctionPass</tt> executes on the machine-dependent
651-
representation of each LLVM function in the program,
652-
independent of all of the other functions in the program.
653-
A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all
650+
<p>A <tt>MachineFunctionPass</tt> is a part of the LLVM code generator that
651+
executes on the machine-dependent representation of each LLVM function in the
652+
program. A <tt>MachineFunctionPass</tt> is also a <tt>FunctionPass</tt>, so all
654653
the restrictions that apply to a <tt>FunctionPass</tt> also apply to it.
655-
<tt>MachineFunctionPass</tt>es also have additional restrictions. In
656-
particular, <tt>MachineFunctionPass</tt>es are not allowed to do any of
657-
the following:</p>
654+
<tt>MachineFunctionPass</tt>es also have additional restrictions. In particular,
655+
<tt>MachineFunctionPass</tt>es are not allowed to do any of the following:</p>
658656

659657
<ol>
660658
<li>Modify any LLVM Instructions, BasicBlocks or Functions.</li>
@@ -669,7 +667,7 @@
669667
</div>
670668

671669
<!-- _______________________________________________________________________ -->
672-
<div class="subsubsection">
670+
<div class="doc_subsubsection">
673671
<a name="runOnMachineFunction">The <tt>runOnMachineFunction(MachineFunction
674672
&amp;MF)</tt> method</a>
675673
</div>
@@ -718,11 +716,7 @@
718716

719717
<li><b><tt>RegisterAnalysis</tt></b> - This template should be used when you are
720718
registering a pass that logically should be available for use in the
721-
'<tt>analysis</tt>' utility.</li>
722-
723-
<li><b><tt>RegisterLLC</tt></b> - This template should be used when you are
724-
registering a pass that logically should be available for use in the
725-
'<tt>llc</tt>' utility.</li>
719+
'<tt>analyze</tt>' utility.</li>
726720

727721
<li><b><tt>RegisterPass</tt></b> - This is the generic form of the
728722
<tt>Register*</tt> templates that should be used if you want your pass listed by
@@ -740,14 +734,6 @@
740734
the pass, which is to be used for the <tt>--help</tt> output of programs, as
741735
well as for debug output generated by the <tt>--debug-pass</tt> option.</p>
742736

743-
<p>If you pass is constructed by its default constructor, you only ever have to
744-
pass these two arguments. If, on the other hand, you require other information
745-
(like target specific information), you must pass an additional argument. This
746-
argument is a pointer to a function used to create the pass. For an example of
747-
how this works, look at the <a
748-
href="http://llvm.cs.uiuc.edu/doxygen/LowerAllocations_8cpp-source.html">LowerAllocations.cpp</a>
749-
file.</p>
750-
751737
<p>If a pass is registered to be used by the <tt>analyze</tt> utility, you
752738
should implement the virtual <tt>print</tt> method:</p>
753739

@@ -819,34 +805,77 @@
819805
should fill in the <tt><a
820806
href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
821807
object with information about which passes are required and not invalidated. To
822-
do this, the following set methods are provided by the <tt><a
823-
href="http://llvm.cs.uiuc.edu/doxygen/classllvm_1_1AnalysisUsage.html">AnalysisUsage</a></tt>
824-
class:</p>
808+
do this, a pass may call any of the following methods on the AnalysisUsage
809+
object:</p>
810+
</div>
825811

826-
<pre>
827-
<i>// addRequires - Add the specified pass to the required set for your pass.</i>
828-
<b>template</b>&lt;<b>class</b> PassClass&gt;
829-
AnalysisUsage &amp;AnalysisUsage::addRequired();
830-
831-
<i>// addPreserved - Add the specified pass to the set of analyses preserved by
832-
// this pass</i>
833-
<b>template</b>&lt;<b>class</b> PassClass&gt;
834-
AnalysisUsage &amp;AnalysisUsage::addPreserved();
835-
836-
<i>// setPreservesAll - Call this if the pass does not modify its input at all</i>
837-
<b>void</b> AnalysisUsage::setPreservesAll();
838-
839-
<i>// setPreservesCFG - This function should be called by the pass, iff they do not:
840-
//
841-
// 1. Add or remove basic blocks from the function
842-
// 2. Modify terminator instructions in any way.
843-
//
844-
// This is automatically implied for <a href="#BasicBlockPass">BasicBlockPass</a>'s
845-
//</i>
846-
<b>void</b> AnalysisUsage::setPreservesCFG();
847-
</pre>
812+
<!-- _______________________________________________________________________ -->
813+
<div class="doc_subsubsection">
814+
<a name="AU::addRequired">The <tt>AnalysisUsage::addRequired&lt;&gt;</tt> and <tt>AnalysisUsage::addRequiredTransitive&lt;&gt;</tt> methods</a>
815+
</div>
816+
817+
<div class="doc_text">
818+
<p>
819+
If you pass requires a previous pass to be executed (an analysis for example),
820+
it can use one of these methods to arrange for it to be run before your pass.
821+
LLVM has many different types of analyses and passes that can be required,
822+
spaning the range from <tt>DominatorSet</tt> to <tt>BreakCriticalEdges</tt>.
823+
requiring <tt>BreakCriticalEdges</tt>, for example, guarantees that there will
824+
be no critical edges in the CFG when your pass has been run.
825+
</p>
826+
827+
<p>
828+
Some analyses chain to other analyses to do their job. For example, an <a
829+
href="AliasAnalysis.html">AliasAnalysis</a> implementation is required to <a
830+
href="AliasAnalysis.html#chaining">chain</a> to other alias analysis passes. In
831+
cases where analyses chain, the <tt>addRequiredTransitive</tt> method should be
832+
used instead of the <tt>addRequired</tt> method. This informs the PassManager
833+
that the transitively required pass should be alive as long as the requiring
834+
pass is.
835+
</p>
836+
</div>
837+
838+
<!-- _______________________________________________________________________ -->
839+
<div class="doc_subsubsection">
840+
<a name="AU::addPreserved">The <tt>AnalysisUsage::addPreserved&lt;&gt;</tt> method</a>
841+
</div>
842+
843+
<div class="doc_text">
844+
<p>
845+
One of the jobs of the PassManager is to optimize how and when analyses are run.
846+
In particular, it attempts to avoid recomputing data unless it needs to. For
847+
this reason, passes are allowed to declare that they preserve (i.e., they don't
848+
invalidate) an existing analysis if it's available. For example, a simple
849+
constant folding pass would not modify the CFG, so it can't possible effect the
850+
results of dominator analysis. By default, all passes are assumed to invalidate
851+
all others.
852+
</p>
853+
854+
<p>
855+
The <tt>AnalysisUsage</tt> class provides several methods which are useful in
856+
certain circumstances that are related to <tt>addPreserved</tt>. In particular,
857+
the <tt>setPreservesAll</tt> method can be called to indicate that the pass does
858+
not modify the LLVM program at all (which is true for analyses), and the
859+
<tt>setPreservesCFG</tt> method can be used by transformations that change
860+
instructions in the program but do not modify the CFG or terminator instructions
861+
(note that this property is implicitly set for <a
862+
href="#BasicBlockPass">BasicBlockPass</a>'s).
863+
</p>
864+
865+
<p>
866+
<tt>addPreserved</tt> is particularly useful for transformations like
867+
<tt>BreakCriticalEdges</tt>. This pass knows how to update a small set of loop
868+
and dominator related analyses if they exist, so it can preserve them, despite
869+
the fact that it hacks on the CFG.
870+
</p>
871+
</div>
872+
873+
<!-- _______________________________________________________________________ -->
874+
<div class="doc_subsubsection">
875+
<a name="AU::examples">Example implementations of <tt>getAnalysisUsage</tt></a>
876+
</div>
848877

849-
<p>Some examples of how to use these methods are:</p>
878+
<div class="doc_text">
850879

851880
<pre>
852881
<i>// This is an example implementation from an analysis, which does not modify
@@ -871,20 +900,22 @@
871900

872901
<!-- _______________________________________________________________________ -->
873902
<div class="doc_subsubsection">
874-
<a name="getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> method</a>
903+
<a name="getAnalysis">The <tt>getAnalysis&lt;&gt;</tt> and <tt>getAnalysisToUpdate&lt;&gt;</tt> methods</a>
875904
</div>
876905

877906
<div class="doc_text">
878907

879-
<p>The <tt>Pass::getAnalysis&lt;&gt;</tt> method is inherited by your class,
880-
providing you with access to the passes that you declared that you required with
881-
the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a> method. It takes
882-
a single template argument that specifies which pass class you want, and returns
883-
a reference to that pass.</p>
908+
<p>The <tt>Pass::getAnalysis&lt;&gt;</tt> method is automatically inherited by
909+
your class, providing you with access to the passes that you declared that you
910+
required with the <a href="#getAnalysisUsage"><tt>getAnalysisUsage</tt></a>
911+
method. It takes a single template argument that specifies which pass class you
912+
want, and returns a reference to that pass. For example:</p>
884913

885914
<pre>
886-
<b>template</b>&lt;<b>typename</b> PassClass&gt;
887-
AnalysisType &amp;getAnalysis();
915+
bool LICM::runOnFunction(Function &F) {
916+
LoopInfo &amp;LI = getAnalysis&lt;LoopInfo&gt;();
917+
...
918+
}
888919
</pre>
889920

890921
<p>This method call returns a reference to the pass desired. You may get a
@@ -894,6 +925,20 @@
894925
method can be called by your <tt>run*</tt> method implementation, or by any
895926
other local method invoked by your <tt>run*</tt> method.</p>
896927

928+
<p>
929+
If your pass is capable of updating analyses if they exist (e.g.,
930+
<tt>BreakCriticalEdges</tt>, as described above), you can use the
931+
<tt>getAnalysisToUpdate</tt> method, which returns a pointer to the analysis if
932+
it is active. For example:</p>
933+
934+
<pre>
935+
...
936+
if (DominatorSet *DS = getAnalysisToUpdate&lt;DominatorSet&gt;()) {
937+
<i>// A DominatorSet is active. This code will update it.</i>
938+
}
939+
...
940+
</pre>
941+
897942
</div>
898943

899944
<!-- *********************************************************************** -->
@@ -924,7 +969,7 @@
924969
</div>
925970

926971
<!-- _______________________________________________________________________ -->
927-
<div class="subsubsection">
972+
<div class="doc_subsubsection">
928973
<a name="agconcepts">Analysis Group Concepts</a>
929974
</div>
930975

@@ -975,7 +1020,7 @@
9751020
</div>
9761021

9771022
<!-- _______________________________________________________________________ -->
978-
<div class="subsubsection">
1023+
<div class="doc_subsubsection">
9791024
<a name="registerag">Using <tt>RegisterAnalysisGroup</tt></a>
9801025
</div>
9811026

@@ -1345,7 +1390,7 @@
13451390
</div>
13461391

13471392
<!-- _______________________________________________________________________ -->
1348-
<div class="subsubsection">
1393+
<div class="doc_subsubsection">
13491394
<a name="SMP">Multithreaded LLVM</a>
13501395
</div>
13511396

@@ -1368,7 +1413,7 @@
13681413
</div>
13691414

13701415
<!-- _______________________________________________________________________ -->
1371-
<div class="subsubsection">
1416+
<div class="doc_subsubsection">
13721417
<a name="ModuleSource">A new <tt>ModuleSource</tt> interface</a>
13731418
</div>
13741419

@@ -1395,7 +1440,7 @@
13951440
</div>
13961441

13971442
<!-- _______________________________________________________________________ -->
1398-
<div class="subsubsection">
1443+
<div class="doc_subsubsection">
13991444
<a name="PassFunctionPass"><tt>Pass</tt>es requiring <tt>FunctionPass</tt>es</a>
14001445
</div>
14011446

0 commit comments

Comments
 (0)