blob: 8fbb29f818a36061eaa945bf597af5639dd7d909 [file] [log] [blame]
Misha Brukman3896be22003-10-24 18:06:11 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
Misha Brukmancd11e452003-10-22 23:27:16 +00003<html>
4<head>
Misha Brukmana97e6cb2004-07-01 15:33:24 +00005 <title>LLVM Alias Analysis Infrastructure</title>
Misha Brukman6e4afd02004-01-15 19:04:12 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
Misha Brukmancd11e452003-10-22 23:27:16 +00007</head>
Misha Brukmancd11e452003-10-22 23:27:16 +00008<body>
Chris Lattner9f648752003-03-04 19:37:49 +00009
Misha Brukmancd11e452003-10-22 23:27:16 +000010<div class="doc_title">
Misha Brukmana97e6cb2004-07-01 15:33:24 +000011 LLVM Alias Analysis Infrastructure
Misha Brukmancd11e452003-10-22 23:27:16 +000012</div>
Chris Lattner9f648752003-03-04 19:37:49 +000013
14<ol>
Misha Brukmanb2154252003-10-23 02:29:42 +000015 <li><a href="#introduction">Introduction</a></li>
Chris Lattner9f648752003-03-04 19:37:49 +000016
Chris Lattnerccc80652004-05-23 21:04:01 +000017 <li><a href="#overview"><tt>AliasAnalysis</tt> Class Overview</a>
Chris Lattner9f648752003-03-04 19:37:49 +000018 <ul>
Misha Brukmanb2154252003-10-23 02:29:42 +000019 <li><a href="#pointers">Representation of Pointers</a></li>
Chris Lattnerccc80652004-05-23 21:04:01 +000020 <li><a href="#alias">The <tt>alias</tt> method</a></li>
Misha Brukmanb2154252003-10-23 02:29:42 +000021 <li><a href="#ModRefInfo">The <tt>getModRefInfo</tt> methods</a></li>
Chris Lattnerccc80652004-05-23 21:04:01 +000022 <li><a href="#OtherItfs">Other useful <tt>AliasAnalysis</tt> methods</a></li>
23 </ul>
24 </li>
Chris Lattner9f648752003-03-04 19:37:49 +000025
Chris Lattnerccc80652004-05-23 21:04:01 +000026 <li><a href="#writingnew">Writing a new <tt>AliasAnalysis</tt> Implementation</a>
Chris Lattner9f648752003-03-04 19:37:49 +000027 <ul>
Misha Brukmanb2154252003-10-23 02:29:42 +000028 <li><a href="#passsubclasses">Different Pass styles</a></li>
29 <li><a href="#requiredcalls">Required initialization calls</a></li>
30 <li><a href="#interfaces">Interfaces which may be specified</a></li>
Chris Lattnerccc80652004-05-23 21:04:01 +000031 <li><a href="#chaining"><tt>AliasAnalysis</tt> chaining behavior</a></li>
32 <li><a href="#updating">Updating analysis results for transformations</a></li>
Misha Brukmanb2154252003-10-23 02:29:42 +000033 <li><a href="#implefficiency">Efficiency Issues</a></li>
Dan Gohman3f43dc32010-06-24 19:34:03 +000034 <li><a href="#passmanager">Pass Manager Issues</a></li>
Chris Lattnerccc80652004-05-23 21:04:01 +000035 </ul>
36 </li>
Chris Lattner9f648752003-03-04 19:37:49 +000037
Chris Lattnerccc80652004-05-23 21:04:01 +000038 <li><a href="#using">Using alias analysis results</a>
Chris Lattner9f648752003-03-04 19:37:49 +000039 <ul>
Chris Lattner4f4365e2009-04-25 21:11:37 +000040 <li><a href="#memdep">Using the <tt>MemoryDependenceAnalysis</tt> Pass</a></li>
Misha Brukmanb2154252003-10-23 02:29:42 +000041 <li><a href="#ast">Using the <tt>AliasSetTracker</tt> class</a></li>
Chris Lattnerccc80652004-05-23 21:04:01 +000042 <li><a href="#direct">Using the <tt>AliasAnalysis</tt> interface directly</a></li>
43 </ul>
44 </li>
Misha Brukman3896be22003-10-24 18:06:11 +000045
Chris Lattnerccc80652004-05-23 21:04:01 +000046 <li><a href="#exist">Existing alias analysis implementations and clients</a>
Chris Lattner9f648752003-03-04 19:37:49 +000047 <ul>
Chris Lattnerccc80652004-05-23 21:04:01 +000048 <li><a href="#impls">Available <tt>AliasAnalysis</tt> implementations</a></li>
49 <li><a href="#aliasanalysis-xforms">Alias analysis driven transformations</a></li>
Misha Brukmana97e6cb2004-07-01 15:33:24 +000050 <li><a href="#aliasanalysis-debug">Clients for debugging and evaluation of
51 implementations</a></li>
Chris Lattnerccc80652004-05-23 21:04:01 +000052 </ul>
53 </li>
Owen Andersond8b47282007-10-02 00:44:20 +000054 <li><a href="#memdep">Memory Dependence Analysis</a></li>
Misha Brukmanb2154252003-10-23 02:29:42 +000055</ol>
Chris Lattner9f648752003-03-04 19:37:49 +000056
Chris Lattnerccc80652004-05-23 21:04:01 +000057<div class="doc_author">
58 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
Misha Brukman3896be22003-10-24 18:06:11 +000059</div>
Chris Lattner9f648752003-03-04 19:37:49 +000060
Chris Lattner9f648752003-03-04 19:37:49 +000061<!-- *********************************************************************** -->
Misha Brukmancd11e452003-10-22 23:27:16 +000062<div class="doc_section">
63 <a name="introduction">Introduction</a>
64</div>
Chris Lattner9f648752003-03-04 19:37:49 +000065<!-- *********************************************************************** -->
66
Misha Brukmancd11e452003-10-22 23:27:16 +000067<div class="doc_text">
Misha Brukman1f254d52003-11-21 22:30:25 +000068
Chris Lattnerccc80652004-05-23 21:04:01 +000069<p>Alias Analysis (aka Pointer Analysis) is a class of techniques which attempt
70to determine whether or not two pointers ever can point to the same object in
71memory. There are many different algorithms for alias analysis and many
72different ways of classifying them: flow-sensitive vs flow-insensitive,
73context-sensitive vs context-insensitive, field-sensitive vs field-insensitive,
74unification-based vs subset-based, etc. Traditionally, alias analyses respond
Duncan Sands8036ca42007-03-30 12:22:09 +000075to a query with a <a href="#MustMayNo">Must, May, or No</a> alias response,
Chris Lattnerccc80652004-05-23 21:04:01 +000076indicating that two pointers always point to the same object, might point to the
77same object, or are known to never point to the same object.</p>
Misha Brukman1f254d52003-11-21 22:30:25 +000078
Chris Lattnerccc80652004-05-23 21:04:01 +000079<p>The LLVM <a
Reid Spencer05fe4b02006-03-14 05:39:39 +000080href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
Chris Lattnerccc80652004-05-23 21:04:01 +000081class is the primary interface used by clients and implementations of alias
82analyses in the LLVM system. This class is the common interface between clients
83of alias analysis information and the implementations providing it, and is
84designed to support a wide range of implementations and clients (but currently
85all clients are assumed to be flow-insensitive). In addition to simple alias
86analysis information, this class exposes Mod/Ref information from those
87implementations which can provide it, allowing for powerful analyses and
88transformations to work well together.</p>
Misha Brukman1f254d52003-11-21 22:30:25 +000089
90<p>This document contains information necessary to successfully implement this
Chris Lattner9f648752003-03-04 19:37:49 +000091interface, use it, and to test both sides. It also explains some of the finer
92points about what exactly results mean. If you feel that something is unclear
Misha Brukmancd11e452003-10-22 23:27:16 +000093or should be added, please <a href="mailto:sabre@nondot.org">let me
Misha Brukman1f254d52003-11-21 22:30:25 +000094know</a>.</p>
95
Misha Brukmancd11e452003-10-22 23:27:16 +000096</div>
Chris Lattner9f648752003-03-04 19:37:49 +000097
98<!-- *********************************************************************** -->
Misha Brukmancd11e452003-10-22 23:27:16 +000099<div class="doc_section">
Chris Lattnerccc80652004-05-23 21:04:01 +0000100 <a name="overview"><tt>AliasAnalysis</tt> Class Overview</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000101</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000102<!-- *********************************************************************** -->
103
Misha Brukmancd11e452003-10-22 23:27:16 +0000104<div class="doc_text">
Misha Brukman1f254d52003-11-21 22:30:25 +0000105
Chris Lattnerccc80652004-05-23 21:04:01 +0000106<p>The <a
Reid Spencer05fe4b02006-03-14 05:39:39 +0000107href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
Chris Lattnerccc80652004-05-23 21:04:01 +0000108class defines the interface that the various alias analysis implementations
109should support. This class exports two important enums: <tt>AliasResult</tt>
110and <tt>ModRefResult</tt> which represent the result of an alias query or a
111mod/ref query, respectively.</p>
Misha Brukman1f254d52003-11-21 22:30:25 +0000112
Chris Lattnerccc80652004-05-23 21:04:01 +0000113<p>The <tt>AliasAnalysis</tt> interface exposes information about memory,
114represented in several different ways. In particular, memory objects are
115represented as a starting address and size, and function calls are represented
116as the actual <tt>call</tt> or <tt>invoke</tt> instructions that performs the
117call. The <tt>AliasAnalysis</tt> interface also exposes some helper methods
118which allow you to get mod/ref information for arbitrary instructions.</p>
Misha Brukman1f254d52003-11-21 22:30:25 +0000119
Dan Gohman9e86f432010-07-07 14:27:09 +0000120<p>All <tt>AliasAnalysis</tt> interfaces require that in queries involving
121multiple values, values which are not
122<a href="LangRef.html#constants">constants</a> are all defined within the
123same function.</p>
124
Misha Brukmancd11e452003-10-22 23:27:16 +0000125</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000126
127<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000128<div class="doc_subsection">
129 <a name="pointers">Representation of Pointers</a>
130</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000131
Misha Brukmancd11e452003-10-22 23:27:16 +0000132<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000133
Chris Lattnerccc80652004-05-23 21:04:01 +0000134<p>Most importantly, the <tt>AliasAnalysis</tt> class provides several methods
135which are used to query whether or not two memory objects alias, whether
136function calls can modify or read a memory object, etc. For all of these
137queries, memory objects are represented as a pair of their starting address (a
138symbolic LLVM <tt>Value*</tt>) and a static size.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000139
140<p>Representing memory objects as a starting address and a size is critically
Chris Lattnerccc80652004-05-23 21:04:01 +0000141important for correct Alias Analyses. For example, consider this (silly, but
142possible) C code:</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000143
Misha Brukman294611a2004-07-28 22:18:33 +0000144<div class="doc_code">
Chris Lattner9f648752003-03-04 19:37:49 +0000145<pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000146int i;
147char C[2];
148char A[10];
149/* ... */
150for (i = 0; i != 10; ++i) {
151 C[0] = A[i]; /* One byte store */
152 C[1] = A[9-i]; /* One byte store */
153}
Chris Lattner9f648752003-03-04 19:37:49 +0000154</pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000155</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000156
157<p>In this case, the <tt>basicaa</tt> pass will disambiguate the stores to
Chris Lattner9f648752003-03-04 19:37:49 +0000158<tt>C[0]</tt> and <tt>C[1]</tt> because they are accesses to two distinct
159locations one byte apart, and the accesses are each one byte. In this case, the
160LICM pass can use store motion to remove the stores from the loop. In
Misha Brukman3896be22003-10-24 18:06:11 +0000161constrast, the following code:</p>
162
Misha Brukman294611a2004-07-28 22:18:33 +0000163<div class="doc_code">
Chris Lattner9f648752003-03-04 19:37:49 +0000164<pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000165int i;
166char C[2];
167char A[10];
168/* ... */
169for (i = 0; i != 10; ++i) {
170 ((short*)C)[0] = A[i]; /* Two byte store! */
171 C[1] = A[9-i]; /* One byte store */
172}
Chris Lattner9f648752003-03-04 19:37:49 +0000173</pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000174</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000175
176<p>In this case, the two stores to C do alias each other, because the access to
177the <tt>&amp;C[0]</tt> element is a two byte access. If size information wasn't
Chris Lattner9f648752003-03-04 19:37:49 +0000178available in the query, even the first case would have to conservatively assume
Misha Brukman3896be22003-10-24 18:06:11 +0000179that the accesses alias.</p>
180
Misha Brukmancd11e452003-10-22 23:27:16 +0000181</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000182
183<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000184<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000185 <a name="alias">The <tt>alias</tt> method</a>
186</div>
187
188<div class="doc_text">
Dan Gohman9e86f432010-07-07 14:27:09 +0000189<p>The <tt>alias</tt> method is the primary interface used to determine whether
190or not two memory objects alias each other. It takes two memory objects as
Dan Gohman4a34cbd2010-12-10 19:38:58 +0000191input and returns MustAlias, PartialAlias, MayAlias, or NoAlias as
192appropriate.</p>
Dan Gohman9e86f432010-07-07 14:27:09 +0000193
194<p>Like all <tt>AliasAnalysis</tt> interfaces, the <tt>alias</tt> method requires
195that either the two pointer values be defined within the same function, or at
196least one of the values is a <a href="LangRef.html#constants">constant</a>.</p>
Chris Lattnerccc80652004-05-23 21:04:01 +0000197</div>
198
199<!-- _______________________________________________________________________ -->
200<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000201 <a name="MustMayNo">Must, May, and No Alias Responses</a>
202</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000203
Misha Brukmancd11e452003-10-22 23:27:16 +0000204<div class="doc_text">
Dan Gohman1e109622010-07-02 18:41:32 +0000205<p>The NoAlias response may be used when there is never an immediate dependence
206between any memory reference <i>based</i> on one pointer and any memory
207reference <i>based</i> the other. The most obvious example is when the two
208pointers point to non-overlapping memory ranges. Another is when the two
209pointers are only ever used for reading memory. Another is when the memory is
210freed and reallocated between accesses through one pointer and accesses through
211the other -- in this case, there is a dependence, but it's mediated by the free
212and reallocation.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000213
Dan Gohmanefca7f92010-07-02 23:46:54 +0000214<p>As an exception to this is with the
Dan Gohman9e86f432010-07-07 14:27:09 +0000215<a href="LangRef.html#noalias"><tt>noalias</tt></a> keyword; the "irrelevant"
216dependencies are ignored.</p>
Dan Gohmanefca7f92010-07-02 23:46:54 +0000217
Nick Lewycky01557ce2008-12-14 21:08:48 +0000218<p>The MayAlias response is used whenever the two pointers might refer to the
Dan Gohman4a34cbd2010-12-10 19:38:58 +0000219same object.</p>
220
221<p>The PartialAlias response is used when the two memory objects are known
222to be overlapping in some way, but do not start at the same address.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000223
Nick Lewycky01557ce2008-12-14 21:08:48 +0000224<p>The MustAlias response may only be returned if the two memory objects are
225guaranteed to always start at exactly the same location. A MustAlias response
226implies that the pointers compare equal.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000227
Misha Brukmancd11e452003-10-22 23:27:16 +0000228</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000229
230<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000231<div class="doc_subsection">
232 <a name="ModRefInfo">The <tt>getModRefInfo</tt> methods</a>
233</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000234
Misha Brukmancd11e452003-10-22 23:27:16 +0000235<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000236
237<p>The <tt>getModRefInfo</tt> methods return information about whether the
Chris Lattner9f648752003-03-04 19:37:49 +0000238execution of an instruction can read or modify a memory location. Mod/Ref
Chris Lattnerccc80652004-05-23 21:04:01 +0000239information is always conservative: if an instruction <b>might</b> read or write
240a location, ModRef is returned.</p>
241
242<p>The <tt>AliasAnalysis</tt> class also provides a <tt>getModRefInfo</tt>
243method for testing dependencies between function calls. This method takes two
Dan Gohman39429e22010-08-30 23:47:24 +0000244call sites (CS1 &amp; CS2), returns NoModRef if neither call writes to memory
245read or written by the other, Ref if CS1 reads memory written by CS2, Mod if CS1
246writes to memory read or written by CS2, or ModRef if CS1 might read or write
247memory written to by CS2. Note that this relation is not commutative.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000248
Misha Brukmancd11e452003-10-22 23:27:16 +0000249</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000250
Chris Lattnerccc80652004-05-23 21:04:01 +0000251
252<!-- ======================================================================= -->
253<div class="doc_subsection">
254 <a name="OtherItfs">Other useful <tt>AliasAnalysis</tt> methods</a>
255</div>
256
257<div class="doc_text">
258
259<p>
260Several other tidbits of information are often collected by various alias
261analysis implementations and can be put to good use by various clients.
262</p>
263
264</div>
265
266<!-- _______________________________________________________________________ -->
267<div class="doc_subsubsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000268 The <tt>pointsToConstantMemory</tt> method
269</div>
270
271<div class="doc_text">
272
273<p>The <tt>pointsToConstantMemory</tt> method returns true if and only if the
274analysis can prove that the pointer only points to unchanging memory locations
275(functions, constant global variables, and the null pointer). This information
276can be used to refine mod/ref information: it is impossible for an unchanging
277memory location to be modified.</p>
278
279</div>
280
Chris Lattnerccc80652004-05-23 21:04:01 +0000281<!-- _______________________________________________________________________ -->
282<div class="doc_subsubsection">
283 <a name="simplemodref">The <tt>doesNotAccessMemory</tt> and
284 <tt>onlyReadsMemory</tt> methods</a>
285</div>
286
287<div class="doc_text">
288
289<p>These methods are used to provide very simple mod/ref information for
290function calls. The <tt>doesNotAccessMemory</tt> method returns true for a
291function if the analysis can prove that the function never reads or writes to
292memory, or if the function only reads from constant memory. Functions with this
293property are side-effect free and only depend on their input arguments, allowing
294them to be eliminated if they form common subexpressions or be hoisted out of
295loops. Many common functions behave this way (e.g., <tt>sin</tt> and
296<tt>cos</tt>) but many others do not (e.g., <tt>acos</tt>, which modifies the
297<tt>errno</tt> variable).</p>
298
299<p>The <tt>onlyReadsMemory</tt> method returns true for a function if analysis
300can prove that (at most) the function only reads from non-volatile memory.
301Functions with this property are side-effect free, only depending on their input
302arguments and the state of memory when they are called. This property allows
303calls to these functions to be eliminated and moved around, as long as there is
304no store instruction that changes the contents of memory. Note that all
305functions that satisfy the <tt>doesNotAccessMemory</tt> method also satisfies
306<tt>onlyReadsMemory</tt>.</p>
307
308</div>
309
Chris Lattner9f648752003-03-04 19:37:49 +0000310<!-- *********************************************************************** -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000311<div class="doc_section">
Chris Lattnerccc80652004-05-23 21:04:01 +0000312 <a name="writingnew">Writing a new <tt>AliasAnalysis</tt> Implementation</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000313</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000314<!-- *********************************************************************** -->
315
Misha Brukmancd11e452003-10-22 23:27:16 +0000316<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000317
318<p>Writing a new alias analysis implementation for LLVM is quite
319straight-forward. There are already several implementations that you can use
320for examples, and the following information should help fill in any details.
Chris Lattnerccc80652004-05-23 21:04:01 +0000321For a examples, take a look at the <a href="#impls">various alias analysis
322implementations</a> included with LLVM.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000323
Misha Brukmancd11e452003-10-22 23:27:16 +0000324</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000325
326<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000327<div class="doc_subsection">
328 <a name="passsubclasses">Different Pass styles</a>
329</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000330
Misha Brukmancd11e452003-10-22 23:27:16 +0000331<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000332
333<p>The first step to determining what type of <a
334href="WritingAnLLVMPass.html">LLVM pass</a> you need to use for your Alias
335Analysis. As is the case with most other analyses and transformations, the
336answer should be fairly obvious from what type of problem you are trying to
337solve:</p>
338
Chris Lattner9f648752003-03-04 19:37:49 +0000339<ol>
Misha Brukmancd11e452003-10-22 23:27:16 +0000340 <li>If you require interprocedural analysis, it should be a
341 <tt>Pass</tt>.</li>
Chris Lattnerccc80652004-05-23 21:04:01 +0000342 <li>If you are a function-local analysis, subclass <tt>FunctionPass</tt>.</li>
Misha Brukmancd11e452003-10-22 23:27:16 +0000343 <li>If you don't need to look at the program at all, subclass
344 <tt>ImmutablePass</tt>.</li>
345</ol>
Misha Brukman3896be22003-10-24 18:06:11 +0000346
347<p>In addition to the pass that you subclass, you should also inherit from the
Misha Brukman700fd492003-05-07 21:47:16 +0000348<tt>AliasAnalysis</tt> interface, of course, and use the
Chris Lattner9f648752003-03-04 19:37:49 +0000349<tt>RegisterAnalysisGroup</tt> template to register as an implementation of
Misha Brukman3896be22003-10-24 18:06:11 +0000350<tt>AliasAnalysis</tt>.</p>
351
Misha Brukmancd11e452003-10-22 23:27:16 +0000352</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000353
354<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000355<div class="doc_subsection">
356 <a name="requiredcalls">Required initialization calls</a>
357</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000358
Misha Brukmancd11e452003-10-22 23:27:16 +0000359<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000360
Chris Lattnerccc80652004-05-23 21:04:01 +0000361<p>Your subclass of <tt>AliasAnalysis</tt> is required to invoke two methods on
362the <tt>AliasAnalysis</tt> base class: <tt>getAnalysisUsage</tt> and
Chris Lattner9f648752003-03-04 19:37:49 +0000363<tt>InitializeAliasAnalysis</tt>. In particular, your implementation of
364<tt>getAnalysisUsage</tt> should explicitly call into the
365<tt>AliasAnalysis::getAnalysisUsage</tt> method in addition to doing any
366declaring any pass dependencies your pass has. Thus you should have something
Misha Brukman3896be22003-10-24 18:06:11 +0000367like this:</p>
368
Misha Brukman294611a2004-07-28 22:18:33 +0000369<div class="doc_code">
Chris Lattner9f648752003-03-04 19:37:49 +0000370<pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000371void getAnalysisUsage(AnalysisUsage &amp;AU) const {
372 AliasAnalysis::getAnalysisUsage(AU);
373 <i>// declare your dependencies here.</i>
374}
Chris Lattner9f648752003-03-04 19:37:49 +0000375</pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000376</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000377
378<p>Additionally, your must invoke the <tt>InitializeAliasAnalysis</tt> method
379from your analysis run method (<tt>run</tt> for a <tt>Pass</tt>,
Chris Lattnerccc80652004-05-23 21:04:01 +0000380<tt>runOnFunction</tt> for a <tt>FunctionPass</tt>, or <tt>InitializePass</tt>
381for an <tt>ImmutablePass</tt>). For example (as part of a <tt>Pass</tt>):</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000382
Misha Brukman294611a2004-07-28 22:18:33 +0000383<div class="doc_code">
Chris Lattner9f648752003-03-04 19:37:49 +0000384<pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000385bool run(Module &amp;M) {
386 InitializeAliasAnalysis(this);
387 <i>// Perform analysis here...</i>
388 return false;
389}
Chris Lattner9f648752003-03-04 19:37:49 +0000390</pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000391</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000392
Misha Brukmancd11e452003-10-22 23:27:16 +0000393</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000394
395<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000396<div class="doc_subsection">
397 <a name="interfaces">Interfaces which may be specified</a>
398</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000399
Misha Brukmancd11e452003-10-22 23:27:16 +0000400<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000401
Chris Lattnerccc80652004-05-23 21:04:01 +0000402<p>All of the <a
403href="/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
404virtual methods default to providing <a href="#chaining">chaining</a> to another
405alias analysis implementation, which ends up returning conservatively correct
406information (returning "May" Alias and "Mod/Ref" for alias and mod/ref queries
Misha Brukman3896be22003-10-24 18:06:11 +0000407respectively). Depending on the capabilities of the analysis you are
408implementing, you just override the interfaces you can improve.</p>
409
Misha Brukmancd11e452003-10-22 23:27:16 +0000410</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000411
Chris Lattnerccc80652004-05-23 21:04:01 +0000412
413
Chris Lattner9f648752003-03-04 19:37:49 +0000414<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000415<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000416 <a name="chaining"><tt>AliasAnalysis</tt> chaining behavior</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000417</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000418
Misha Brukmancd11e452003-10-22 23:27:16 +0000419<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000420
Chris Lattnerccc80652004-05-23 21:04:01 +0000421<p>With only two special exceptions (the <tt><a
422href="#basic-aa">basicaa</a></tt> and <a href="#no-aa"><tt>no-aa</tt></a>
423passes) every alias analysis pass chains to another alias analysis
424implementation (for example, the user can specify "<tt>-basicaa -ds-aa
Chris Lattnera9cf1962010-03-01 19:24:17 +0000425-licm</tt>" to get the maximum benefit from both alias
Chris Lattnerccc80652004-05-23 21:04:01 +0000426analyses). The alias analysis class automatically takes care of most of this
427for methods that you don't override. For methods that you do override, in code
428paths that return a conservative MayAlias or Mod/Ref result, simply return
429whatever the superclass computes. For example:</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000430
Misha Brukman294611a2004-07-28 22:18:33 +0000431<div class="doc_code">
Chris Lattnerccc80652004-05-23 21:04:01 +0000432<pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000433AliasAnalysis::AliasResult alias(const Value *V1, unsigned V1Size,
434 const Value *V2, unsigned V2Size) {
435 if (...)
436 return NoAlias;
437 ...
Chris Lattnerccc80652004-05-23 21:04:01 +0000438
Misha Brukman294611a2004-07-28 22:18:33 +0000439 <i>// Couldn't determine a must or no-alias result.</i>
440 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
441}
Chris Lattnerccc80652004-05-23 21:04:01 +0000442</pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000443</div>
Chris Lattnerccc80652004-05-23 21:04:01 +0000444
445<p>In addition to analysis queries, you must make sure to unconditionally pass
446LLVM <a href="#updating">update notification</a> methods to the superclass as
447well if you override them, which allows all alias analyses in a change to be
448updated.</p>
449
450</div>
451
452
453<!-- ======================================================================= -->
454<div class="doc_subsection">
455 <a name="updating">Updating analysis results for transformations</a>
456</div>
457
458<div class="doc_text">
459<p>
460Alias analysis information is initially computed for a static snapshot of the
461program, but clients will use this information to make transformations to the
462code. All but the most trivial forms of alias analysis will need to have their
463analysis results updated to reflect the changes made by these transformations.
464</p>
465
466<p>
467The <tt>AliasAnalysis</tt> interface exposes two methods which are used to
468communicate program changes from the clients to the analysis implementations.
469Various alias analysis implementations should use these methods to ensure that
470their internal data structures are kept up-to-date as the program changes (for
471example, when an instruction is deleted), and clients of alias analysis must be
472sure to call these interfaces appropriately.
473</p>
474</div>
475
476<!-- _______________________________________________________________________ -->
477<div class="doc_subsubsection">The <tt>deleteValue</tt> method</div>
478
479<div class="doc_text">
480The <tt>deleteValue</tt> method is called by transformations when they remove an
481instruction or any other value from the program (including values that do not
482use pointers). Typically alias analyses keep data structures that have entries
483for each value in the program. When this method is called, they should remove
484any entries for the specified value, if they exist.
485</div>
486
Chris Lattnerccc80652004-05-23 21:04:01 +0000487<!-- _______________________________________________________________________ -->
488<div class="doc_subsubsection">The <tt>copyValue</tt> method</div>
489
490<div class="doc_text">
491The <tt>copyValue</tt> method is used when a new value is introduced into the
492program. There is no way to introduce a value into the program that did not
493exist before (this doesn't make sense for a safe compiler transformation), so
494this is the only way to introduce a new value. This method indicates that the
495new value has exactly the same properties as the value being copied.
496</div>
497
Chris Lattnerccc80652004-05-23 21:04:01 +0000498<!-- _______________________________________________________________________ -->
499<div class="doc_subsubsection">The <tt>replaceWithNewValue</tt> method</div>
500
501<div class="doc_text">
502This method is a simple helper method that is provided to make clients easier to
503use. It is implemented by copying the old analysis information to the new
504value, then deleting the old value. This method cannot be overridden by alias
505analysis implementations.
Misha Brukmancd11e452003-10-22 23:27:16 +0000506</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000507
508<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000509<div class="doc_subsection">
510 <a name="implefficiency">Efficiency Issues</a>
511</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000512
Misha Brukmancd11e452003-10-22 23:27:16 +0000513<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000514
515<p>From the LLVM perspective, the only thing you need to do to provide an
516efficient alias analysis is to make sure that alias analysis <b>queries</b> are
517serviced quickly. The actual calculation of the alias analysis results (the
518"run" method) is only performed once, but many (perhaps duplicate) queries may
519be performed. Because of this, try to move as much computation to the run
520method as possible (within reason).</p>
521
Misha Brukmancd11e452003-10-22 23:27:16 +0000522</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000523
Dan Gohman3f43dc32010-06-24 19:34:03 +0000524<!-- ======================================================================= -->
525<div class="doc_subsection">
526 <a name="passmanager">Pass Manager Issues</a>
527</div>
528
529<div class="doc_text">
530
531<p>PassManager support for alternative AliasAnalysis implementation
532has some issues.</p>
533
534<p>There is no way to override the default alias analysis. It would
535be very useful to be able to do something like "opt -my-aa -O2" and
536have it use -my-aa for all passes which need AliasAnalysis, but there
537is currently no support for that, short of changing the source code
538and recompiling. Similarly, there is also no way of setting a chain
539of analyses as the default.</p>
540
541<p>There is no way for transform passes to declare that they preserve
542<tt>AliasAnalysis</tt> implementations. The <tt>AliasAnalysis</tt>
543interface includes <tt>deleteValue</tt> and <tt>copyValue</tt> methods
544which are intended to allow a pass to keep an AliasAnalysis consistent,
545however there's no way for a pass to declare in its
546<tt>getAnalysisUsage</tt> that it does so. Some passes attempt to use
547<tt>AU.addPreserved&lt;AliasAnalysis&gt;</tt>, however this doesn't
548actually have any effect.</tt>
549
550<p><tt>AliasAnalysisCounter</tt> (<tt>-count-aa</tt>) and <tt>AliasDebugger</tt>
551(<tt>-debug-aa</tt>) are implemented as <tt>ModulePass</tt> classes, so if your
552alias analysis uses <tt>FunctionPass</tt>, it won't be able to use
553these utilities. If you try to use them, the pass manager will
554silently route alias analysis queries directly to
555<tt>BasicAliasAnalysis</tt> instead.</p>
556
557<p>Similarly, the <tt>opt -p</tt> option introduces <tt>ModulePass</tt>
558passes between each pass, which prevents the use of <tt>FunctionPass</tt>
559alias analysis passes.</p>
560
561</div>
562
Chris Lattner9f648752003-03-04 19:37:49 +0000563<!-- *********************************************************************** -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000564<div class="doc_section">
Chris Lattnerccc80652004-05-23 21:04:01 +0000565 <a name="using">Using alias analysis results</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000566</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000567<!-- *********************************************************************** -->
568
Misha Brukmancd11e452003-10-22 23:27:16 +0000569<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000570
571<p>There are several different ways to use alias analysis results. In order of
572preference, these are...</p>
573
Misha Brukmancd11e452003-10-22 23:27:16 +0000574</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000575
576<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000577<div class="doc_subsection">
Chris Lattner4f4365e2009-04-25 21:11:37 +0000578 <a name="memdep">Using the <tt>MemoryDependenceAnalysis</tt> Pass</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000579</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000580
Misha Brukmancd11e452003-10-22 23:27:16 +0000581<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000582
Chris Lattner4f4365e2009-04-25 21:11:37 +0000583<p>The <tt>memdep</tt> pass uses alias analysis to provide high-level dependence
584information about memory-using instructions. This will tell you which store
585feeds into a load, for example. It uses caching and other techniques to be
586efficient, and is used by Dead Store Elimination, GVN, and memcpy optimizations.
587</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000588
Misha Brukmancd11e452003-10-22 23:27:16 +0000589</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000590
591<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000592<div class="doc_subsection">
593 <a name="ast">Using the <tt>AliasSetTracker</tt> class</a>
594</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000595
Misha Brukmancd11e452003-10-22 23:27:16 +0000596<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000597
598<p>Many transformations need information about alias <b>sets</b> that are active
599in some scope, rather than information about pairwise aliasing. The <tt><a
Misha Brukman294611a2004-07-28 22:18:33 +0000600href="/doxygen/classllvm_1_1AliasSetTracker.html">AliasSetTracker</a></tt> class
601is used to efficiently build these Alias Sets from the pairwise alias analysis
Chris Lattnerccc80652004-05-23 21:04:01 +0000602information provided by the <tt>AliasAnalysis</tt> interface.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000603
Chris Lattnerccc80652004-05-23 21:04:01 +0000604<p>First you initialize the AliasSetTracker by using the "<tt>add</tt>" methods
605to add information about various potentially aliasing instructions in the scope
606you are interested in. Once all of the alias sets are completed, your pass
607should simply iterate through the constructed alias sets, using the
608<tt>AliasSetTracker</tt> <tt>begin()</tt>/<tt>end()</tt> methods.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000609
610<p>The <tt>AliasSet</tt>s formed by the <tt>AliasSetTracker</tt> are guaranteed
Chris Lattner539ca702003-12-19 08:43:07 +0000611to be disjoint, calculate mod/ref information and volatility for the set, and
612keep track of whether or not all of the pointers in the set are Must aliases.
613The AliasSetTracker also makes sure that sets are properly folded due to call
Misha Brukman3896be22003-10-24 18:06:11 +0000614instructions, and can provide a list of pointers in each set.</p>
615
616<p>As an example user of this, the <a href="/doxygen/structLICM.html">Loop
Chris Lattnerccc80652004-05-23 21:04:01 +0000617Invariant Code Motion</a> pass uses <tt>AliasSetTracker</tt>s to calculate alias
618sets for each loop nest. If an <tt>AliasSet</tt> in a loop is not modified,
619then all load instructions from that set may be hoisted out of the loop. If any
620alias sets are stored to <b>and</b> are must alias sets, then the stores may be
621sunk to outside of the loop, promoting the memory location to a register for the
622duration of the loop nest. Both of these transformations only apply if the
623pointer argument is loop-invariant.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000624
Misha Brukmancd11e452003-10-22 23:27:16 +0000625</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000626
Chris Lattnerccc80652004-05-23 21:04:01 +0000627<!-- _______________________________________________________________________ -->
Chris Lattner539ca702003-12-19 08:43:07 +0000628<div class="doc_subsubsection">
629 The AliasSetTracker implementation
630</div>
631
632<div class="doc_text">
633
634<p>The AliasSetTracker class is implemented to be as efficient as possible. It
635uses the union-find algorithm to efficiently merge AliasSets when a pointer is
636inserted into the AliasSetTracker that aliases multiple sets. The primary data
637structure is a hash table mapping pointers to the AliasSet they are in.</p>
638
639<p>The AliasSetTracker class must maintain a list of all of the LLVM Value*'s
640that are in each AliasSet. Since the hash table already has entries for each
641LLVM Value* of interest, the AliasesSets thread the linked list through these
642hash-table nodes to avoid having to allocate memory unnecessarily, and to make
643merging alias sets extremely efficient (the linked list merge is constant time).
644</p>
645
646<p>You shouldn't need to understand these details if you are just a client of
647the AliasSetTracker, but if you look at the code, hopefully this brief
648description will help make sense of why things are designed the way they
649are.</p>
650
651</div>
652
Chris Lattner9f648752003-03-04 19:37:49 +0000653<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000654<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000655 <a name="direct">Using the <tt>AliasAnalysis</tt> interface directly</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000656</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000657
Misha Brukmancd11e452003-10-22 23:27:16 +0000658<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000659
Chris Lattnerccc80652004-05-23 21:04:01 +0000660<p>If neither of these utility class are what your pass needs, you should use
661the interfaces exposed by the <tt>AliasAnalysis</tt> class directly. Try to use
662the higher-level methods when possible (e.g., use mod/ref information instead of
663the <a href="#alias"><tt>alias</tt></a> method directly if possible) to get the
664best precision and efficiency.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000665
Misha Brukmancd11e452003-10-22 23:27:16 +0000666</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000667
668<!-- *********************************************************************** -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000669<div class="doc_section">
Chris Lattnerccc80652004-05-23 21:04:01 +0000670 <a name="exist">Existing alias analysis implementations and clients</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000671</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000672<!-- *********************************************************************** -->
673
Misha Brukmancd11e452003-10-22 23:27:16 +0000674<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000675
Chris Lattnerccc80652004-05-23 21:04:01 +0000676<p>If you're going to be working with the LLVM alias analysis infrastructure,
677you should know what clients and implementations of alias analysis are
678available. In particular, if you are implementing an alias analysis, you should
679be aware of the <a href="#aliasanalysis-debug">the clients</a> that are useful
680for monitoring and evaluating different implementations.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000681
Misha Brukmancd11e452003-10-22 23:27:16 +0000682</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000683
684<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000685<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000686 <a name="impls">Available <tt>AliasAnalysis</tt> implementations</a>
687</div>
688
689<div class="doc_text">
690
691<p>This section lists the various implementations of the <tt>AliasAnalysis</tt>
692interface. With the exception of the <a href="#no-aa"><tt>-no-aa</tt></a> and
693<a href="#basic-aa"><tt>-basicaa</tt></a> implementations, all of these <a
Chris Lattnerfcd37252004-06-21 22:52:48 +0000694href="#chaining">chain</a> to other alias analysis implementations.</p>
Chris Lattnerccc80652004-05-23 21:04:01 +0000695
696</div>
697
698<!-- _______________________________________________________________________ -->
699<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000700 <a name="no-aa">The <tt>-no-aa</tt> pass</a>
701</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000702
Misha Brukmancd11e452003-10-22 23:27:16 +0000703<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000704
Chris Lattnerccc80652004-05-23 21:04:01 +0000705<p>The <tt>-no-aa</tt> pass is just like what it sounds: an alias analysis that
706never returns any useful information. This pass can be useful if you think that
707alias analysis is doing something wrong and are trying to narrow down a
708problem.</p>
709
710</div>
711
Chris Lattnerccc80652004-05-23 21:04:01 +0000712<!-- _______________________________________________________________________ -->
713<div class="doc_subsubsection">
714 <a name="basic-aa">The <tt>-basicaa</tt> pass</a>
715</div>
716
717<div class="doc_text">
718
Dan Gohmanc66712f2010-11-15 18:07:16 +0000719<p>The <tt>-basicaa</tt> pass is an aggressive local analysis that "knows"
720many important facts:</p>
Chris Lattnerccc80652004-05-23 21:04:01 +0000721
722<ul>
723<li>Distinct globals, stack allocations, and heap allocations can never
724 alias.</li>
725<li>Globals, stack allocations, and heap allocations never alias the null
726 pointer.</li>
727<li>Different fields of a structure do not alias.</li>
728<li>Indexes into arrays with statically differing subscripts cannot alias.</li>
729<li>Many common standard C library functions <a
730 href="#simplemodref">never access memory or only read memory</a>.</li>
731<li>Pointers that obviously point to constant globals
732 "<tt>pointToConstantMemory</tt>".</li>
733<li>Function calls can not modify or references stack allocations if they never
734 escape from the function that allocates them (a common case for automatic
735 arrays).</li>
736</ul>
737
738</div>
739
Chris Lattner100a4f82004-06-28 19:19:47 +0000740<!-- _______________________________________________________________________ -->
741<div class="doc_subsubsection">
742 <a name="globalsmodref">The <tt>-globalsmodref-aa</tt> pass</a>
743</div>
744
745<div class="doc_text">
746
747<p>This pass implements a simple context-sensitive mod/ref and alias analysis
Chris Lattnerccb354b2004-07-27 07:50:07 +0000748for internal global variables that don't "have their address taken". If a
749global does not have its address taken, the pass knows that no pointers alias
750the global. This pass also keeps track of functions that it knows never access
Chris Lattner4f4365e2009-04-25 21:11:37 +0000751memory or never read memory. This allows certain optimizations (e.g. GVN) to
Chris Lattnerccb354b2004-07-27 07:50:07 +0000752eliminate call instructions entirely.
Chris Lattner100a4f82004-06-28 19:19:47 +0000753</p>
754
755<p>The real power of this pass is that it provides context-sensitive mod/ref
756information for call instructions. This allows the optimizer to know that
757calls to a function do not clobber or read the value of the global, allowing
758loads and stores to be eliminated.</p>
759
760<p>Note that this pass is somewhat limited in its scope (only support
761non-address taken globals), but is very quick analysis.</p>
762</div>
Chris Lattnerccc80652004-05-23 21:04:01 +0000763
764<!-- _______________________________________________________________________ -->
765<div class="doc_subsubsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000766 <a name="steens-aa">The <tt>-steens-aa</tt> pass</a>
767</div>
768
769<div class="doc_text">
770
771<p>The <tt>-steens-aa</tt> pass implements a variation on the well-known
772"Steensgaard's algorithm" for interprocedural alias analysis. Steensgaard's
773algorithm is a unification-based, flow-insensitive, context-insensitive, and
774field-insensitive alias analysis that is also very scalable (effectively linear
775time).</p>
776
777<p>The LLVM <tt>-steens-aa</tt> pass implements a "speculatively
778field-<b>sensitive</b>" version of Steensgaard's algorithm using the Data
779Structure Analysis framework. This gives it substantially more precision than
780the standard algorithm while maintaining excellent analysis scalability.</p>
781
Chris Lattnerd6e0dd12007-07-03 04:41:50 +0000782<p>Note that <tt>-steens-aa</tt> is available in the optional "poolalloc"
783module, it is not part of the LLVM core.</p>
784
Chris Lattnerccc80652004-05-23 21:04:01 +0000785</div>
786
787<!-- _______________________________________________________________________ -->
788<div class="doc_subsubsection">
789 <a name="ds-aa">The <tt>-ds-aa</tt> pass</a>
790</div>
791
792<div class="doc_text">
793
794<p>The <tt>-ds-aa</tt> pass implements the full Data Structure Analysis
795algorithm. Data Structure Analysis is a modular unification-based,
796flow-insensitive, context-<b>sensitive</b>, and speculatively
797field-<b>sensitive</b> alias analysis that is also quite scalable, usually at
798O(n*log(n)).</p>
799
800<p>This algorithm is capable of responding to a full variety of alias analysis
801queries, and can provide context-sensitive mod/ref information as well. The
802only major facility not implemented so far is support for must-alias
803information.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000804
Chris Lattnerd6e0dd12007-07-03 04:41:50 +0000805<p>Note that <tt>-ds-aa</tt> is available in the optional "poolalloc"
806module, it is not part of the LLVM core.</p>
807
Misha Brukmancd11e452003-10-22 23:27:16 +0000808</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000809
Dan Gohman85dfca62010-06-28 22:09:52 +0000810<!-- _______________________________________________________________________ -->
811<div class="doc_subsubsection">
812 <a name="scev-aa">The <tt>-scev-aa</tt> pass</a>
813</div>
814
815<div class="doc_text">
816
817<p>The <tt>-scev-aa</tt> pass implements AliasAnalysis queries by
818translating them into ScalarEvolution queries. This gives it a
819more complete understanding of <tt>getelementptr</tt> instructions
820and loop induction variables than other alias analyses have.</p>
821
822</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000823
824<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000825<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000826 <a name="aliasanalysis-xforms">Alias analysis driven transformations</a>
827</div>
828
829<div class="doc_text">
830LLVM includes several alias-analysis driven transformations which can be used
831with any of the implementations above.
832</div>
833
834<!-- _______________________________________________________________________ -->
835<div class="doc_subsubsection">
836 <a name="adce">The <tt>-adce</tt> pass</a>
837</div>
838
839<div class="doc_text">
840
841<p>The <tt>-adce</tt> pass, which implements Aggressive Dead Code Elimination
842uses the <tt>AliasAnalysis</tt> interface to delete calls to functions that do
843not have side-effects and are not used.</p>
844
845</div>
846
847
848<!-- _______________________________________________________________________ -->
849<div class="doc_subsubsection">
850 <a name="licm">The <tt>-licm</tt> pass</a>
851</div>
852
853<div class="doc_text">
854
855<p>The <tt>-licm</tt> pass implements various Loop Invariant Code Motion related
856transformations. It uses the <tt>AliasAnalysis</tt> interface for several
857different transformations:</p>
858
859<ul>
860<li>It uses mod/ref information to hoist or sink load instructions out of loops
861if there are no instructions in the loop that modifies the memory loaded.</li>
862
863<li>It uses mod/ref information to hoist function calls out of loops that do not
864write to memory and are loop-invariant.</li>
865
866<li>If uses alias information to promote memory objects that are loaded and
867stored to in loops to live in a register instead. It can do this if there are
868no may aliases to the loaded/stored memory location.</li>
869</ul>
870
871</div>
872
873<!-- _______________________________________________________________________ -->
874<div class="doc_subsubsection">
875 <a name="argpromotion">The <tt>-argpromotion</tt> pass</a>
876</div>
877
878<div class="doc_text">
879<p>
880The <tt>-argpromotion</tt> pass promotes by-reference arguments to be passed in
881by-value instead. In particular, if pointer arguments are only loaded from it
882passes in the value loaded instead of the address to the function. This pass
883uses alias information to make sure that the value loaded from the argument
884pointer is not modified between the entry of the function and any load of the
885pointer.</p>
886</div>
887
888<!-- _______________________________________________________________________ -->
889<div class="doc_subsubsection">
Chris Lattner4f4365e2009-04-25 21:11:37 +0000890 <a name="gvn">The <tt>-gvn</tt>, <tt>-memcpyopt</tt>, and <tt>-dse</tt>
891 passes</a>
Chris Lattnerccc80652004-05-23 21:04:01 +0000892</div>
893
894<div class="doc_text">
Misha Brukman294611a2004-07-28 22:18:33 +0000895
Chris Lattner4f4365e2009-04-25 21:11:37 +0000896<p>These passes use AliasAnalysis information to reason about loads and stores.
897</p>
Chris Lattnerccc80652004-05-23 21:04:01 +0000898
899</div>
900
Chris Lattnerccc80652004-05-23 21:04:01 +0000901<!-- ======================================================================= -->
902<div class="doc_subsection">
Misha Brukman294611a2004-07-28 22:18:33 +0000903 <a name="aliasanalysis-debug">Clients for debugging and evaluation of
904 implementations</a>
Chris Lattnerccc80652004-05-23 21:04:01 +0000905</div>
906
Misha Brukman294611a2004-07-28 22:18:33 +0000907<div class="doc_text">
Chris Lattnerccc80652004-05-23 21:04:01 +0000908
Misha Brukman294611a2004-07-28 22:18:33 +0000909<p>These passes are useful for evaluating the various alias analysis
Chris Lattnera9cf1962010-03-01 19:24:17 +0000910implementations. You can use them with commands like '<tt>opt -ds-aa
Misha Brukman294611a2004-07-28 22:18:33 +0000911-aa-eval foo.bc -disable-output -stats</tt>'.</p>
912
913</div>
Chris Lattnerccc80652004-05-23 21:04:01 +0000914
915<!-- _______________________________________________________________________ -->
916<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000917 <a name="print-alias-sets">The <tt>-print-alias-sets</tt> pass</a>
918</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000919
Misha Brukmancd11e452003-10-22 23:27:16 +0000920<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000921
922<p>The <tt>-print-alias-sets</tt> pass is exposed as part of the
Chris Lattner05d5c9d2006-01-03 06:04:48 +0000923<tt>opt</tt> tool to print out the Alias Sets formed by the <a
Chris Lattner9f648752003-03-04 19:37:49 +0000924href="#ast"><tt>AliasSetTracker</tt></a> class. This is useful if you're using
Chris Lattner05d5c9d2006-01-03 06:04:48 +0000925the <tt>AliasSetTracker</tt> class. To use it, use something like:</p>
926
927<div class="doc_code">
928<pre>
929% opt -ds-aa -print-alias-sets -disable-output
930</pre>
931</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000932
Misha Brukmancd11e452003-10-22 23:27:16 +0000933</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000934
Chris Lattnerccc80652004-05-23 21:04:01 +0000935
936<!-- _______________________________________________________________________ -->
937<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000938 <a name="count-aa">The <tt>-count-aa</tt> pass</a>
939</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000940
Misha Brukmancd11e452003-10-22 23:27:16 +0000941<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000942
943<p>The <tt>-count-aa</tt> pass is useful to see how many queries a particular
Misha Brukman294611a2004-07-28 22:18:33 +0000944pass is making and what responses are returned by the alias analysis. As an
945example,</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000946
Misha Brukman294611a2004-07-28 22:18:33 +0000947<div class="doc_code">
Chris Lattner9f648752003-03-04 19:37:49 +0000948<pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000949% opt -basicaa -count-aa -ds-aa -count-aa -licm
Chris Lattner9f648752003-03-04 19:37:49 +0000950</pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000951</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000952
Misha Brukman294611a2004-07-28 22:18:33 +0000953<p>will print out how many queries (and what responses are returned) by the
954<tt>-licm</tt> pass (of the <tt>-ds-aa</tt> pass) and how many queries are made
955of the <tt>-basicaa</tt> pass by the <tt>-ds-aa</tt> pass. This can be useful
956when debugging a transformation or an alias analysis implementation.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000957
Misha Brukmancd11e452003-10-22 23:27:16 +0000958</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000959
Chris Lattnerccc80652004-05-23 21:04:01 +0000960<!-- _______________________________________________________________________ -->
961<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000962 <a name="aa-eval">The <tt>-aa-eval</tt> pass</a>
963</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000964
Misha Brukmancd11e452003-10-22 23:27:16 +0000965<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000966
967<p>The <tt>-aa-eval</tt> pass simply iterates through all pairs of pointers in a
Chris Lattner9f648752003-03-04 19:37:49 +0000968function and asks an alias analysis whether or not the pointers alias. This
969gives an indication of the precision of the alias analysis. Statistics are
Chris Lattnerccc80652004-05-23 21:04:01 +0000970printed indicating the percent of no/may/must aliases found (a more precise
971algorithm will have a lower number of may aliases).</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000972
Misha Brukmancd11e452003-10-22 23:27:16 +0000973</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000974
Chris Lattner9f648752003-03-04 19:37:49 +0000975<!-- *********************************************************************** -->
Owen Anderson5a726b82007-10-02 00:43:25 +0000976<div class="doc_section">
977 <a name="memdep">Memory Dependence Analysis</a>
978</div>
979<!-- *********************************************************************** -->
980
981<div class="doc_text">
982
983<p>If you're just looking to be a client of alias analysis information, consider
984using the Memory Dependence Analysis interface instead. MemDep is a lazy,
985caching layer on top of alias analysis that is able to answer the question of
986what preceding memory operations a given instruction depends on, either at an
987intra- or inter-block level. Because of its laziness and caching
988policy, using MemDep can be a significant performance win over accessing alias
989analysis directly.</p>
990
991</div>
992
993<!-- *********************************************************************** -->
Chris Lattner9f648752003-03-04 19:37:49 +0000994
Misha Brukman3896be22003-10-24 18:06:11 +0000995<hr>
Misha Brukman915cab22003-11-22 01:26:21 +0000996<address>
Misha Brukman38847d52003-12-21 22:53:21 +0000997 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000998 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukman915cab22003-11-22 01:26:21 +0000999 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +00001000 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukman915cab22003-11-22 01:26:21 +00001001
1002 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer05fe4b02006-03-14 05:39:39 +00001003 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
Misha Brukman915cab22003-11-22 01:26:21 +00001004 Last modified: $Date$
1005</address>
Misha Brukmancd11e452003-10-22 23:27:16 +00001006
1007</body>
1008</html>