blob: 6962a3f27d6a8d8b9fe6cce1568d9fde0df71b2d [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 Gohman01e59882010-12-15 23:09:41 +000034 <li><a href="#limitations">Limitations</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">
Dan Gohman49e41c52010-12-15 18:45:20 +0000526 <a name="limitations">Limitations</a>
Dan Gohman3f43dc32010-06-24 19:34:03 +0000527</div>
528
529<div class="doc_text">
530
Dan Gohman49e41c52010-12-15 18:45:20 +0000531<p>The AliasAnalysis infrastructure has several limitations which make
532writing a new <tt>AliasAnalysis</tt> implementation difficult.</p>
Dan Gohman3f43dc32010-06-24 19:34:03 +0000533
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
Dan Gohman49e41c52010-12-15 18:45:20 +0000561<p>The <tt>AliasAnalysis</tt> API does have functions for notifying
562implementations when values are deleted or copied, however these
563aren't sufficient. There are many other ways that LLVM IR can be
564modified which could be relevant to <tt>AliasAnalysis</tt>
565implementations which can not be expressed.</p>
566
567<p>The <tt>AliasAnalysisDebugger</tt> utility seems to suggest that
568<tt>AliasAnalysis</tt> implementations can expect that they will be
569informed of any relevant <tt>Value</tt> before it appears in an
570alias query. However, popular clients such as <tt>GVN</tt> don't
571support this, and are known to trigger errors when run with the
572<tt>AliasAnalysisDebugger</tt>.</p>
573
574<p>Due to several of the above limitations, the most obvious use for
575the <tt>AliasAnalysisCounter</tt> utility, collecting stats on all
576alias queries in a compilation, doesn't work, even if the
577<tt>AliasAnalysis</tt> implementations don't use <tt>FunctionPass</tt>.
578There's no way to set a default, much less a default sequence,
579and there's no way to preserve it.</p>
580
581<p>The <tt>AliasSetTracker</tt> class (which is used by <tt>LICM</tt>
582makes a non-deterministic number of alias queries. This can cause stats
583collected by <tt>AliasAnalysisCounter</tt> to have fluctuations among
584identical runs, for example. Another consequence is that debugging
585techniques involving pausing execution after a predetermined number
586of queries can be unreliable.</p>
587
588<p>Many alias queries can be reformulated in terms of other alias
589queries. When multiple <tt>AliasAnalysis</tt> queries are chained together,
590it would make sense to start those queries from the beginning of the chain,
591with care taken to avoid infinite looping, however currently an
592implementation which wants to do this can only start such queries
593from itself.</p>
594
Dan Gohman3f43dc32010-06-24 19:34:03 +0000595</div>
596
Chris Lattner9f648752003-03-04 19:37:49 +0000597<!-- *********************************************************************** -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000598<div class="doc_section">
Chris Lattnerccc80652004-05-23 21:04:01 +0000599 <a name="using">Using alias analysis results</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000600</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000601<!-- *********************************************************************** -->
602
Misha Brukmancd11e452003-10-22 23:27:16 +0000603<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000604
605<p>There are several different ways to use alias analysis results. In order of
606preference, these are...</p>
607
Misha Brukmancd11e452003-10-22 23:27:16 +0000608</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000609
610<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000611<div class="doc_subsection">
Chris Lattner4f4365e2009-04-25 21:11:37 +0000612 <a name="memdep">Using the <tt>MemoryDependenceAnalysis</tt> Pass</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000613</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000614
Misha Brukmancd11e452003-10-22 23:27:16 +0000615<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000616
Chris Lattner4f4365e2009-04-25 21:11:37 +0000617<p>The <tt>memdep</tt> pass uses alias analysis to provide high-level dependence
618information about memory-using instructions. This will tell you which store
619feeds into a load, for example. It uses caching and other techniques to be
620efficient, and is used by Dead Store Elimination, GVN, and memcpy optimizations.
621</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000622
Misha Brukmancd11e452003-10-22 23:27:16 +0000623</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000624
625<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000626<div class="doc_subsection">
627 <a name="ast">Using the <tt>AliasSetTracker</tt> class</a>
628</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000629
Misha Brukmancd11e452003-10-22 23:27:16 +0000630<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000631
632<p>Many transformations need information about alias <b>sets</b> that are active
633in some scope, rather than information about pairwise aliasing. The <tt><a
Misha Brukman294611a2004-07-28 22:18:33 +0000634href="/doxygen/classllvm_1_1AliasSetTracker.html">AliasSetTracker</a></tt> class
635is used to efficiently build these Alias Sets from the pairwise alias analysis
Chris Lattnerccc80652004-05-23 21:04:01 +0000636information provided by the <tt>AliasAnalysis</tt> interface.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000637
Chris Lattnerccc80652004-05-23 21:04:01 +0000638<p>First you initialize the AliasSetTracker by using the "<tt>add</tt>" methods
639to add information about various potentially aliasing instructions in the scope
640you are interested in. Once all of the alias sets are completed, your pass
641should simply iterate through the constructed alias sets, using the
642<tt>AliasSetTracker</tt> <tt>begin()</tt>/<tt>end()</tt> methods.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000643
644<p>The <tt>AliasSet</tt>s formed by the <tt>AliasSetTracker</tt> are guaranteed
Chris Lattner539ca702003-12-19 08:43:07 +0000645to be disjoint, calculate mod/ref information and volatility for the set, and
646keep track of whether or not all of the pointers in the set are Must aliases.
647The AliasSetTracker also makes sure that sets are properly folded due to call
Misha Brukman3896be22003-10-24 18:06:11 +0000648instructions, and can provide a list of pointers in each set.</p>
649
650<p>As an example user of this, the <a href="/doxygen/structLICM.html">Loop
Chris Lattnerccc80652004-05-23 21:04:01 +0000651Invariant Code Motion</a> pass uses <tt>AliasSetTracker</tt>s to calculate alias
652sets for each loop nest. If an <tt>AliasSet</tt> in a loop is not modified,
653then all load instructions from that set may be hoisted out of the loop. If any
654alias sets are stored to <b>and</b> are must alias sets, then the stores may be
655sunk to outside of the loop, promoting the memory location to a register for the
656duration of the loop nest. Both of these transformations only apply if the
657pointer argument is loop-invariant.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000658
Misha Brukmancd11e452003-10-22 23:27:16 +0000659</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000660
Chris Lattnerccc80652004-05-23 21:04:01 +0000661<!-- _______________________________________________________________________ -->
Chris Lattner539ca702003-12-19 08:43:07 +0000662<div class="doc_subsubsection">
663 The AliasSetTracker implementation
664</div>
665
666<div class="doc_text">
667
668<p>The AliasSetTracker class is implemented to be as efficient as possible. It
669uses the union-find algorithm to efficiently merge AliasSets when a pointer is
670inserted into the AliasSetTracker that aliases multiple sets. The primary data
671structure is a hash table mapping pointers to the AliasSet they are in.</p>
672
673<p>The AliasSetTracker class must maintain a list of all of the LLVM Value*'s
674that are in each AliasSet. Since the hash table already has entries for each
675LLVM Value* of interest, the AliasesSets thread the linked list through these
676hash-table nodes to avoid having to allocate memory unnecessarily, and to make
677merging alias sets extremely efficient (the linked list merge is constant time).
678</p>
679
680<p>You shouldn't need to understand these details if you are just a client of
681the AliasSetTracker, but if you look at the code, hopefully this brief
682description will help make sense of why things are designed the way they
683are.</p>
684
685</div>
686
Chris Lattner9f648752003-03-04 19:37:49 +0000687<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000688<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000689 <a name="direct">Using the <tt>AliasAnalysis</tt> interface directly</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000690</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000691
Misha Brukmancd11e452003-10-22 23:27:16 +0000692<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000693
Chris Lattnerccc80652004-05-23 21:04:01 +0000694<p>If neither of these utility class are what your pass needs, you should use
695the interfaces exposed by the <tt>AliasAnalysis</tt> class directly. Try to use
696the higher-level methods when possible (e.g., use mod/ref information instead of
697the <a href="#alias"><tt>alias</tt></a> method directly if possible) to get the
698best precision and efficiency.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000699
Misha Brukmancd11e452003-10-22 23:27:16 +0000700</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000701
702<!-- *********************************************************************** -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000703<div class="doc_section">
Chris Lattnerccc80652004-05-23 21:04:01 +0000704 <a name="exist">Existing alias analysis implementations and clients</a>
Misha Brukmancd11e452003-10-22 23:27:16 +0000705</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000706<!-- *********************************************************************** -->
707
Misha Brukmancd11e452003-10-22 23:27:16 +0000708<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000709
Chris Lattnerccc80652004-05-23 21:04:01 +0000710<p>If you're going to be working with the LLVM alias analysis infrastructure,
711you should know what clients and implementations of alias analysis are
712available. In particular, if you are implementing an alias analysis, you should
713be aware of the <a href="#aliasanalysis-debug">the clients</a> that are useful
714for monitoring and evaluating different implementations.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000715
Misha Brukmancd11e452003-10-22 23:27:16 +0000716</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000717
718<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000719<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000720 <a name="impls">Available <tt>AliasAnalysis</tt> implementations</a>
721</div>
722
723<div class="doc_text">
724
725<p>This section lists the various implementations of the <tt>AliasAnalysis</tt>
726interface. With the exception of the <a href="#no-aa"><tt>-no-aa</tt></a> and
727<a href="#basic-aa"><tt>-basicaa</tt></a> implementations, all of these <a
Chris Lattnerfcd37252004-06-21 22:52:48 +0000728href="#chaining">chain</a> to other alias analysis implementations.</p>
Chris Lattnerccc80652004-05-23 21:04:01 +0000729
730</div>
731
732<!-- _______________________________________________________________________ -->
733<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000734 <a name="no-aa">The <tt>-no-aa</tt> pass</a>
735</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000736
Misha Brukmancd11e452003-10-22 23:27:16 +0000737<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000738
Chris Lattnerccc80652004-05-23 21:04:01 +0000739<p>The <tt>-no-aa</tt> pass is just like what it sounds: an alias analysis that
740never returns any useful information. This pass can be useful if you think that
741alias analysis is doing something wrong and are trying to narrow down a
742problem.</p>
743
744</div>
745
Chris Lattnerccc80652004-05-23 21:04:01 +0000746<!-- _______________________________________________________________________ -->
747<div class="doc_subsubsection">
748 <a name="basic-aa">The <tt>-basicaa</tt> pass</a>
749</div>
750
751<div class="doc_text">
752
Dan Gohmanc66712f2010-11-15 18:07:16 +0000753<p>The <tt>-basicaa</tt> pass is an aggressive local analysis that "knows"
754many important facts:</p>
Chris Lattnerccc80652004-05-23 21:04:01 +0000755
756<ul>
757<li>Distinct globals, stack allocations, and heap allocations can never
758 alias.</li>
759<li>Globals, stack allocations, and heap allocations never alias the null
760 pointer.</li>
761<li>Different fields of a structure do not alias.</li>
762<li>Indexes into arrays with statically differing subscripts cannot alias.</li>
763<li>Many common standard C library functions <a
764 href="#simplemodref">never access memory or only read memory</a>.</li>
765<li>Pointers that obviously point to constant globals
766 "<tt>pointToConstantMemory</tt>".</li>
767<li>Function calls can not modify or references stack allocations if they never
768 escape from the function that allocates them (a common case for automatic
769 arrays).</li>
770</ul>
771
772</div>
773
Chris Lattner100a4f82004-06-28 19:19:47 +0000774<!-- _______________________________________________________________________ -->
775<div class="doc_subsubsection">
776 <a name="globalsmodref">The <tt>-globalsmodref-aa</tt> pass</a>
777</div>
778
779<div class="doc_text">
780
781<p>This pass implements a simple context-sensitive mod/ref and alias analysis
Chris Lattnerccb354b2004-07-27 07:50:07 +0000782for internal global variables that don't "have their address taken". If a
783global does not have its address taken, the pass knows that no pointers alias
784the global. This pass also keeps track of functions that it knows never access
Chris Lattner4f4365e2009-04-25 21:11:37 +0000785memory or never read memory. This allows certain optimizations (e.g. GVN) to
Chris Lattnerccb354b2004-07-27 07:50:07 +0000786eliminate call instructions entirely.
Chris Lattner100a4f82004-06-28 19:19:47 +0000787</p>
788
789<p>The real power of this pass is that it provides context-sensitive mod/ref
790information for call instructions. This allows the optimizer to know that
791calls to a function do not clobber or read the value of the global, allowing
792loads and stores to be eliminated.</p>
793
794<p>Note that this pass is somewhat limited in its scope (only support
795non-address taken globals), but is very quick analysis.</p>
796</div>
Chris Lattnerccc80652004-05-23 21:04:01 +0000797
798<!-- _______________________________________________________________________ -->
799<div class="doc_subsubsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000800 <a name="steens-aa">The <tt>-steens-aa</tt> pass</a>
801</div>
802
803<div class="doc_text">
804
805<p>The <tt>-steens-aa</tt> pass implements a variation on the well-known
806"Steensgaard's algorithm" for interprocedural alias analysis. Steensgaard's
807algorithm is a unification-based, flow-insensitive, context-insensitive, and
808field-insensitive alias analysis that is also very scalable (effectively linear
809time).</p>
810
811<p>The LLVM <tt>-steens-aa</tt> pass implements a "speculatively
812field-<b>sensitive</b>" version of Steensgaard's algorithm using the Data
813Structure Analysis framework. This gives it substantially more precision than
814the standard algorithm while maintaining excellent analysis scalability.</p>
815
Chris Lattnerd6e0dd12007-07-03 04:41:50 +0000816<p>Note that <tt>-steens-aa</tt> is available in the optional "poolalloc"
817module, it is not part of the LLVM core.</p>
818
Chris Lattnerccc80652004-05-23 21:04:01 +0000819</div>
820
821<!-- _______________________________________________________________________ -->
822<div class="doc_subsubsection">
823 <a name="ds-aa">The <tt>-ds-aa</tt> pass</a>
824</div>
825
826<div class="doc_text">
827
828<p>The <tt>-ds-aa</tt> pass implements the full Data Structure Analysis
829algorithm. Data Structure Analysis is a modular unification-based,
830flow-insensitive, context-<b>sensitive</b>, and speculatively
831field-<b>sensitive</b> alias analysis that is also quite scalable, usually at
832O(n*log(n)).</p>
833
834<p>This algorithm is capable of responding to a full variety of alias analysis
835queries, and can provide context-sensitive mod/ref information as well. The
836only major facility not implemented so far is support for must-alias
837information.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000838
Chris Lattnerd6e0dd12007-07-03 04:41:50 +0000839<p>Note that <tt>-ds-aa</tt> is available in the optional "poolalloc"
840module, it is not part of the LLVM core.</p>
841
Misha Brukmancd11e452003-10-22 23:27:16 +0000842</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000843
Dan Gohman85dfca62010-06-28 22:09:52 +0000844<!-- _______________________________________________________________________ -->
845<div class="doc_subsubsection">
846 <a name="scev-aa">The <tt>-scev-aa</tt> pass</a>
847</div>
848
849<div class="doc_text">
850
851<p>The <tt>-scev-aa</tt> pass implements AliasAnalysis queries by
852translating them into ScalarEvolution queries. This gives it a
853more complete understanding of <tt>getelementptr</tt> instructions
854and loop induction variables than other alias analyses have.</p>
855
856</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000857
858<!-- ======================================================================= -->
Misha Brukmancd11e452003-10-22 23:27:16 +0000859<div class="doc_subsection">
Chris Lattnerccc80652004-05-23 21:04:01 +0000860 <a name="aliasanalysis-xforms">Alias analysis driven transformations</a>
861</div>
862
863<div class="doc_text">
864LLVM includes several alias-analysis driven transformations which can be used
865with any of the implementations above.
866</div>
867
868<!-- _______________________________________________________________________ -->
869<div class="doc_subsubsection">
870 <a name="adce">The <tt>-adce</tt> pass</a>
871</div>
872
873<div class="doc_text">
874
875<p>The <tt>-adce</tt> pass, which implements Aggressive Dead Code Elimination
876uses the <tt>AliasAnalysis</tt> interface to delete calls to functions that do
877not have side-effects and are not used.</p>
878
879</div>
880
881
882<!-- _______________________________________________________________________ -->
883<div class="doc_subsubsection">
884 <a name="licm">The <tt>-licm</tt> pass</a>
885</div>
886
887<div class="doc_text">
888
889<p>The <tt>-licm</tt> pass implements various Loop Invariant Code Motion related
890transformations. It uses the <tt>AliasAnalysis</tt> interface for several
891different transformations:</p>
892
893<ul>
894<li>It uses mod/ref information to hoist or sink load instructions out of loops
895if there are no instructions in the loop that modifies the memory loaded.</li>
896
897<li>It uses mod/ref information to hoist function calls out of loops that do not
898write to memory and are loop-invariant.</li>
899
900<li>If uses alias information to promote memory objects that are loaded and
901stored to in loops to live in a register instead. It can do this if there are
902no may aliases to the loaded/stored memory location.</li>
903</ul>
904
905</div>
906
907<!-- _______________________________________________________________________ -->
908<div class="doc_subsubsection">
909 <a name="argpromotion">The <tt>-argpromotion</tt> pass</a>
910</div>
911
912<div class="doc_text">
913<p>
914The <tt>-argpromotion</tt> pass promotes by-reference arguments to be passed in
915by-value instead. In particular, if pointer arguments are only loaded from it
916passes in the value loaded instead of the address to the function. This pass
917uses alias information to make sure that the value loaded from the argument
918pointer is not modified between the entry of the function and any load of the
919pointer.</p>
920</div>
921
922<!-- _______________________________________________________________________ -->
923<div class="doc_subsubsection">
Chris Lattner4f4365e2009-04-25 21:11:37 +0000924 <a name="gvn">The <tt>-gvn</tt>, <tt>-memcpyopt</tt>, and <tt>-dse</tt>
925 passes</a>
Chris Lattnerccc80652004-05-23 21:04:01 +0000926</div>
927
928<div class="doc_text">
Misha Brukman294611a2004-07-28 22:18:33 +0000929
Chris Lattner4f4365e2009-04-25 21:11:37 +0000930<p>These passes use AliasAnalysis information to reason about loads and stores.
931</p>
Chris Lattnerccc80652004-05-23 21:04:01 +0000932
933</div>
934
Chris Lattnerccc80652004-05-23 21:04:01 +0000935<!-- ======================================================================= -->
936<div class="doc_subsection">
Misha Brukman294611a2004-07-28 22:18:33 +0000937 <a name="aliasanalysis-debug">Clients for debugging and evaluation of
938 implementations</a>
Chris Lattnerccc80652004-05-23 21:04:01 +0000939</div>
940
Misha Brukman294611a2004-07-28 22:18:33 +0000941<div class="doc_text">
Chris Lattnerccc80652004-05-23 21:04:01 +0000942
Misha Brukman294611a2004-07-28 22:18:33 +0000943<p>These passes are useful for evaluating the various alias analysis
Chris Lattnera9cf1962010-03-01 19:24:17 +0000944implementations. You can use them with commands like '<tt>opt -ds-aa
Misha Brukman294611a2004-07-28 22:18:33 +0000945-aa-eval foo.bc -disable-output -stats</tt>'.</p>
946
947</div>
Chris Lattnerccc80652004-05-23 21:04:01 +0000948
949<!-- _______________________________________________________________________ -->
950<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000951 <a name="print-alias-sets">The <tt>-print-alias-sets</tt> pass</a>
952</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000953
Misha Brukmancd11e452003-10-22 23:27:16 +0000954<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000955
956<p>The <tt>-print-alias-sets</tt> pass is exposed as part of the
Chris Lattner05d5c9d2006-01-03 06:04:48 +0000957<tt>opt</tt> tool to print out the Alias Sets formed by the <a
Chris Lattner9f648752003-03-04 19:37:49 +0000958href="#ast"><tt>AliasSetTracker</tt></a> class. This is useful if you're using
Chris Lattner05d5c9d2006-01-03 06:04:48 +0000959the <tt>AliasSetTracker</tt> class. To use it, use something like:</p>
960
961<div class="doc_code">
962<pre>
963% opt -ds-aa -print-alias-sets -disable-output
964</pre>
965</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000966
Misha Brukmancd11e452003-10-22 23:27:16 +0000967</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000968
Chris Lattnerccc80652004-05-23 21:04:01 +0000969
970<!-- _______________________________________________________________________ -->
971<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000972 <a name="count-aa">The <tt>-count-aa</tt> pass</a>
973</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000974
Misha Brukmancd11e452003-10-22 23:27:16 +0000975<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +0000976
977<p>The <tt>-count-aa</tt> pass is useful to see how many queries a particular
Misha Brukman294611a2004-07-28 22:18:33 +0000978pass is making and what responses are returned by the alias analysis. As an
979example,</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000980
Misha Brukman294611a2004-07-28 22:18:33 +0000981<div class="doc_code">
Chris Lattner9f648752003-03-04 19:37:49 +0000982<pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000983% opt -basicaa -count-aa -ds-aa -count-aa -licm
Chris Lattner9f648752003-03-04 19:37:49 +0000984</pre>
Misha Brukman294611a2004-07-28 22:18:33 +0000985</div>
Misha Brukman3896be22003-10-24 18:06:11 +0000986
Misha Brukman294611a2004-07-28 22:18:33 +0000987<p>will print out how many queries (and what responses are returned) by the
988<tt>-licm</tt> pass (of the <tt>-ds-aa</tt> pass) and how many queries are made
989of the <tt>-basicaa</tt> pass by the <tt>-ds-aa</tt> pass. This can be useful
990when debugging a transformation or an alias analysis implementation.</p>
Misha Brukman3896be22003-10-24 18:06:11 +0000991
Misha Brukmancd11e452003-10-22 23:27:16 +0000992</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000993
Chris Lattnerccc80652004-05-23 21:04:01 +0000994<!-- _______________________________________________________________________ -->
995<div class="doc_subsubsection">
Misha Brukmancd11e452003-10-22 23:27:16 +0000996 <a name="aa-eval">The <tt>-aa-eval</tt> pass</a>
997</div>
Chris Lattner9f648752003-03-04 19:37:49 +0000998
Misha Brukmancd11e452003-10-22 23:27:16 +0000999<div class="doc_text">
Misha Brukman3896be22003-10-24 18:06:11 +00001000
1001<p>The <tt>-aa-eval</tt> pass simply iterates through all pairs of pointers in a
Chris Lattner9f648752003-03-04 19:37:49 +00001002function and asks an alias analysis whether or not the pointers alias. This
1003gives an indication of the precision of the alias analysis. Statistics are
Chris Lattnerccc80652004-05-23 21:04:01 +00001004printed indicating the percent of no/may/must aliases found (a more precise
1005algorithm will have a lower number of may aliases).</p>
Misha Brukman3896be22003-10-24 18:06:11 +00001006
Misha Brukmancd11e452003-10-22 23:27:16 +00001007</div>
Chris Lattner9f648752003-03-04 19:37:49 +00001008
Chris Lattner9f648752003-03-04 19:37:49 +00001009<!-- *********************************************************************** -->
Owen Anderson5a726b82007-10-02 00:43:25 +00001010<div class="doc_section">
1011 <a name="memdep">Memory Dependence Analysis</a>
1012</div>
1013<!-- *********************************************************************** -->
1014
1015<div class="doc_text">
1016
1017<p>If you're just looking to be a client of alias analysis information, consider
1018using the Memory Dependence Analysis interface instead. MemDep is a lazy,
1019caching layer on top of alias analysis that is able to answer the question of
1020what preceding memory operations a given instruction depends on, either at an
1021intra- or inter-block level. Because of its laziness and caching
1022policy, using MemDep can be a significant performance win over accessing alias
1023analysis directly.</p>
1024
1025</div>
1026
1027<!-- *********************************************************************** -->
Chris Lattner9f648752003-03-04 19:37:49 +00001028
Misha Brukman3896be22003-10-24 18:06:11 +00001029<hr>
Misha Brukman915cab22003-11-22 01:26:21 +00001030<address>
Misha Brukman38847d52003-12-21 22:53:21 +00001031 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +00001032 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukman915cab22003-11-22 01:26:21 +00001033 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +00001034 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukman915cab22003-11-22 01:26:21 +00001035
1036 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer05fe4b02006-03-14 05:39:39 +00001037 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
Misha Brukman915cab22003-11-22 01:26:21 +00001038 Last modified: $Date$
1039</address>
Misha Brukmancd11e452003-10-22 23:27:16 +00001040
1041</body>
1042</html>