blob: 7baa946769797b0b6b3e1c5e81e1c87a6e1b66c3 [file] [log] [blame]
Misha Brukman084a3a072003-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 Brukman283ff692003-10-22 23:27:16 +00003<html>
4<head>
Misha Brukman3eef5d42004-07-01 15:33:24 +00005 <title>LLVM Alias Analysis Infrastructure</title>
Misha Brukmanc6ef5592004-01-15 19:04:12 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
Misha Brukman283ff692003-10-22 23:27:16 +00007</head>
Misha Brukman283ff692003-10-22 23:27:16 +00008<body>
Chris Lattnerb512e9f2003-03-04 19:37:49 +00009
Misha Brukman283ff692003-10-22 23:27:16 +000010<div class="doc_title">
Misha Brukman3eef5d42004-07-01 15:33:24 +000011 LLVM Alias Analysis Infrastructure
Misha Brukman283ff692003-10-22 23:27:16 +000012</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000013
14<ol>
Misha Brukman9782b032003-10-23 02:29:42 +000015 <li><a href="#introduction">Introduction</a></li>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000016
Chris Lattner2a19d832004-05-23 21:04:01 +000017 <li><a href="#overview"><tt>AliasAnalysis</tt> Class Overview</a>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000018 <ul>
Misha Brukman9782b032003-10-23 02:29:42 +000019 <li><a href="#pointers">Representation of Pointers</a></li>
Chris Lattner2a19d832004-05-23 21:04:01 +000020 <li><a href="#alias">The <tt>alias</tt> method</a></li>
Misha Brukman9782b032003-10-23 02:29:42 +000021 <li><a href="#ModRefInfo">The <tt>getModRefInfo</tt> methods</a></li>
Chris Lattner2a19d832004-05-23 21:04:01 +000022 <li><a href="#OtherItfs">Other useful <tt>AliasAnalysis</tt> methods</a></li>
23 </ul>
24 </li>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000025
Chris Lattner2a19d832004-05-23 21:04:01 +000026 <li><a href="#writingnew">Writing a new <tt>AliasAnalysis</tt> Implementation</a>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000027 <ul>
Misha Brukman9782b032003-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 Lattner2a19d832004-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 Brukman9782b032003-10-23 02:29:42 +000033 <li><a href="#implefficiency">Efficiency Issues</a></li>
Dan Gohmane1d8d3a2010-12-15 23:09:41 +000034 <li><a href="#limitations">Limitations</a></li>
Chris Lattner2a19d832004-05-23 21:04:01 +000035 </ul>
36 </li>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000037
Chris Lattner2a19d832004-05-23 21:04:01 +000038 <li><a href="#using">Using alias analysis results</a>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000039 <ul>
Chris Lattner6a6d3cc2009-04-25 21:11:37 +000040 <li><a href="#memdep">Using the <tt>MemoryDependenceAnalysis</tt> Pass</a></li>
Misha Brukman9782b032003-10-23 02:29:42 +000041 <li><a href="#ast">Using the <tt>AliasSetTracker</tt> class</a></li>
Chris Lattner2a19d832004-05-23 21:04:01 +000042 <li><a href="#direct">Using the <tt>AliasAnalysis</tt> interface directly</a></li>
43 </ul>
44 </li>
Misha Brukman084a3a072003-10-24 18:06:11 +000045
Chris Lattner2a19d832004-05-23 21:04:01 +000046 <li><a href="#exist">Existing alias analysis implementations and clients</a>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000047 <ul>
Chris Lattner2a19d832004-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 Brukman3eef5d42004-07-01 15:33:24 +000050 <li><a href="#aliasanalysis-debug">Clients for debugging and evaluation of
51 implementations</a></li>
Chris Lattner2a19d832004-05-23 21:04:01 +000052 </ul>
53 </li>
Owen Anderson88847732007-10-02 00:44:20 +000054 <li><a href="#memdep">Memory Dependence Analysis</a></li>
Misha Brukman9782b032003-10-23 02:29:42 +000055</ol>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000056
Chris Lattner2a19d832004-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 Brukman084a3a072003-10-24 18:06:11 +000059</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000060
Chris Lattnerb512e9f2003-03-04 19:37:49 +000061<!-- *********************************************************************** -->
Misha Brukman283ff692003-10-22 23:27:16 +000062<div class="doc_section">
63 <a name="introduction">Introduction</a>
64</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000065<!-- *********************************************************************** -->
66
Misha Brukman283ff692003-10-22 23:27:16 +000067<div class="doc_text">
Misha Brukmanbc2d3302003-11-21 22:30:25 +000068
Chris Lattner2a19d832004-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 Sands16f122e2007-03-30 12:22:09 +000075to a query with a <a href="#MustMayNo">Must, May, or No</a> alias response,
Chris Lattner2a19d832004-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 Brukmanbc2d3302003-11-21 22:30:25 +000078
Chris Lattner2a19d832004-05-23 21:04:01 +000079<p>The LLVM <a
Reid Spencerca058542006-03-14 05:39:39 +000080href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
Chris Lattner2a19d832004-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 Brukmanbc2d3302003-11-21 22:30:25 +000089
90<p>This document contains information necessary to successfully implement this
Chris Lattnerb512e9f2003-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 Brukman283ff692003-10-22 23:27:16 +000093or should be added, please <a href="mailto:sabre@nondot.org">let me
Misha Brukmanbc2d3302003-11-21 22:30:25 +000094know</a>.</p>
95
Misha Brukman283ff692003-10-22 23:27:16 +000096</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +000097
98<!-- *********************************************************************** -->
Misha Brukman283ff692003-10-22 23:27:16 +000099<div class="doc_section">
Chris Lattner2a19d832004-05-23 21:04:01 +0000100 <a name="overview"><tt>AliasAnalysis</tt> Class Overview</a>
Misha Brukman283ff692003-10-22 23:27:16 +0000101</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000102<!-- *********************************************************************** -->
103
Misha Brukman283ff692003-10-22 23:27:16 +0000104<div class="doc_text">
Misha Brukmanbc2d3302003-11-21 22:30:25 +0000105
Chris Lattner2a19d832004-05-23 21:04:01 +0000106<p>The <a
Reid Spencerca058542006-03-14 05:39:39 +0000107href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
Chris Lattner2a19d832004-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 Brukmanbc2d3302003-11-21 22:30:25 +0000112
Chris Lattner2a19d832004-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 Brukmanbc2d3302003-11-21 22:30:25 +0000119
Dan Gohman00ef9322010-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 Brukman283ff692003-10-22 23:27:16 +0000125</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000126
127<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000128<div class="doc_subsection">
129 <a name="pointers">Representation of Pointers</a>
130</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000131
Misha Brukman283ff692003-10-22 23:27:16 +0000132<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000133
Chris Lattner2a19d832004-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 Brukman084a3a072003-10-24 18:06:11 +0000139
140<p>Representing memory objects as a starting address and a size is critically
Chris Lattner2a19d832004-05-23 21:04:01 +0000141important for correct Alias Analyses. For example, consider this (silly, but
142possible) C code:</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000143
Misha Brukman2361fcf2004-07-28 22:18:33 +0000144<div class="doc_code">
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000145<pre>
Misha Brukman2361fcf2004-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 Lattnerb512e9f2003-03-04 19:37:49 +0000154</pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +0000155</div>
Misha Brukman084a3a072003-10-24 18:06:11 +0000156
157<p>In this case, the <tt>basicaa</tt> pass will disambiguate the stores to
Chris Lattnerb512e9f2003-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 Brukman084a3a072003-10-24 18:06:11 +0000161constrast, the following code:</p>
162
Misha Brukman2361fcf2004-07-28 22:18:33 +0000163<div class="doc_code">
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000164<pre>
Misha Brukman2361fcf2004-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 Lattnerb512e9f2003-03-04 19:37:49 +0000173</pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +0000174</div>
Misha Brukman084a3a072003-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 Lattnerb512e9f2003-03-04 19:37:49 +0000178available in the query, even the first case would have to conservatively assume
Misha Brukman084a3a072003-10-24 18:06:11 +0000179that the accesses alias.</p>
180
Misha Brukman283ff692003-10-22 23:27:16 +0000181</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000182
183<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000184<div class="doc_subsection">
Chris Lattner2a19d832004-05-23 21:04:01 +0000185 <a name="alias">The <tt>alias</tt> method</a>
186</div>
187
188<div class="doc_text">
Dan Gohman00ef9322010-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 Gohman4bd64fa2010-12-10 19:38:58 +0000191input and returns MustAlias, PartialAlias, MayAlias, or NoAlias as
192appropriate.</p>
Dan Gohman00ef9322010-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 Lattner2a19d832004-05-23 21:04:01 +0000197</div>
198
199<!-- _______________________________________________________________________ -->
200<div class="doc_subsubsection">
Misha Brukman283ff692003-10-22 23:27:16 +0000201 <a name="MustMayNo">Must, May, and No Alias Responses</a>
202</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000203
Misha Brukman283ff692003-10-22 23:27:16 +0000204<div class="doc_text">
Dan Gohmandf12d082010-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 Brukman084a3a072003-10-24 18:06:11 +0000213
Dan Gohmande256292010-07-02 23:46:54 +0000214<p>As an exception to this is with the
Dan Gohman00ef9322010-07-07 14:27:09 +0000215<a href="LangRef.html#noalias"><tt>noalias</tt></a> keyword; the "irrelevant"
216dependencies are ignored.</p>
Dan Gohmande256292010-07-02 23:46:54 +0000217
Nick Lewycky4048ca52008-12-14 21:08:48 +0000218<p>The MayAlias response is used whenever the two pointers might refer to the
Dan Gohman4bd64fa2010-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 Brukman084a3a072003-10-24 18:06:11 +0000223
Nick Lewycky4048ca52008-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 Brukman084a3a072003-10-24 18:06:11 +0000227
Misha Brukman283ff692003-10-22 23:27:16 +0000228</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000229
230<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000231<div class="doc_subsection">
232 <a name="ModRefInfo">The <tt>getModRefInfo</tt> methods</a>
233</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000234
Misha Brukman283ff692003-10-22 23:27:16 +0000235<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000236
237<p>The <tt>getModRefInfo</tt> methods return information about whether the
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000238execution of an instruction can read or modify a memory location. Mod/Ref
Chris Lattner2a19d832004-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 Gohmanf2d81712010-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 Brukman084a3a072003-10-24 18:06:11 +0000248
Misha Brukman283ff692003-10-22 23:27:16 +0000249</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000250
Chris Lattner2a19d832004-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 Lattner2a19d832004-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 Lattner2a19d832004-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 Lattnerb512e9f2003-03-04 19:37:49 +0000310<!-- *********************************************************************** -->
Misha Brukman283ff692003-10-22 23:27:16 +0000311<div class="doc_section">
Chris Lattner2a19d832004-05-23 21:04:01 +0000312 <a name="writingnew">Writing a new <tt>AliasAnalysis</tt> Implementation</a>
Misha Brukman283ff692003-10-22 23:27:16 +0000313</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000314<!-- *********************************************************************** -->
315
Misha Brukman283ff692003-10-22 23:27:16 +0000316<div class="doc_text">
Misha Brukman084a3a072003-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 Lattner2a19d832004-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 Brukman084a3a072003-10-24 18:06:11 +0000323
Misha Brukman283ff692003-10-22 23:27:16 +0000324</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000325
326<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000327<div class="doc_subsection">
328 <a name="passsubclasses">Different Pass styles</a>
329</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000330
Misha Brukman283ff692003-10-22 23:27:16 +0000331<div class="doc_text">
Misha Brukman084a3a072003-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 Lattnerb512e9f2003-03-04 19:37:49 +0000339<ol>
Misha Brukman283ff692003-10-22 23:27:16 +0000340 <li>If you require interprocedural analysis, it should be a
341 <tt>Pass</tt>.</li>
Chris Lattner2a19d832004-05-23 21:04:01 +0000342 <li>If you are a function-local analysis, subclass <tt>FunctionPass</tt>.</li>
Misha Brukman283ff692003-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 Brukman084a3a072003-10-24 18:06:11 +0000346
347<p>In addition to the pass that you subclass, you should also inherit from the
Misha Brukmanb4dd5392003-05-07 21:47:16 +0000348<tt>AliasAnalysis</tt> interface, of course, and use the
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000349<tt>RegisterAnalysisGroup</tt> template to register as an implementation of
Misha Brukman084a3a072003-10-24 18:06:11 +0000350<tt>AliasAnalysis</tt>.</p>
351
Misha Brukman283ff692003-10-22 23:27:16 +0000352</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000353
354<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000355<div class="doc_subsection">
356 <a name="requiredcalls">Required initialization calls</a>
357</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000358
Misha Brukman283ff692003-10-22 23:27:16 +0000359<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000360
Chris Lattner2a19d832004-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 Lattnerb512e9f2003-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 Brukman084a3a072003-10-24 18:06:11 +0000367like this:</p>
368
Misha Brukman2361fcf2004-07-28 22:18:33 +0000369<div class="doc_code">
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000370<pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +0000371void getAnalysisUsage(AnalysisUsage &amp;AU) const {
372 AliasAnalysis::getAnalysisUsage(AU);
373 <i>// declare your dependencies here.</i>
374}
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000375</pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +0000376</div>
Misha Brukman084a3a072003-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 Lattner2a19d832004-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 Brukman084a3a072003-10-24 18:06:11 +0000382
Misha Brukman2361fcf2004-07-28 22:18:33 +0000383<div class="doc_code">
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000384<pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +0000385bool run(Module &amp;M) {
386 InitializeAliasAnalysis(this);
387 <i>// Perform analysis here...</i>
388 return false;
389}
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000390</pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +0000391</div>
Misha Brukman084a3a072003-10-24 18:06:11 +0000392
Misha Brukman283ff692003-10-22 23:27:16 +0000393</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000394
395<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000396<div class="doc_subsection">
397 <a name="interfaces">Interfaces which may be specified</a>
398</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000399
Misha Brukman283ff692003-10-22 23:27:16 +0000400<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000401
Chris Lattner2a19d832004-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 Brukman084a3a072003-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 Brukman283ff692003-10-22 23:27:16 +0000410</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000411
Chris Lattner2a19d832004-05-23 21:04:01 +0000412
413
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000414<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000415<div class="doc_subsection">
Chris Lattner2a19d832004-05-23 21:04:01 +0000416 <a name="chaining"><tt>AliasAnalysis</tt> chaining behavior</a>
Misha Brukman283ff692003-10-22 23:27:16 +0000417</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000418
Misha Brukman283ff692003-10-22 23:27:16 +0000419<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000420
Chris Lattner2a19d832004-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 Lattner5ea3e652010-03-01 19:24:17 +0000425-licm</tt>" to get the maximum benefit from both alias
Chris Lattner2a19d832004-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 Brukman084a3a072003-10-24 18:06:11 +0000430
Misha Brukman2361fcf2004-07-28 22:18:33 +0000431<div class="doc_code">
Chris Lattner2a19d832004-05-23 21:04:01 +0000432<pre>
Misha Brukman2361fcf2004-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 Lattner2a19d832004-05-23 21:04:01 +0000438
Misha Brukman2361fcf2004-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 Lattner2a19d832004-05-23 21:04:01 +0000442</pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +0000443</div>
Chris Lattner2a19d832004-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>
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000467The <tt>AliasAnalysis</tt> interface exposes four methods which are used to
Chris Lattner2a19d832004-05-23 21:04:01 +0000468communicate 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 Lattner2a19d832004-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 Lattner2a19d832004-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 Brukman283ff692003-10-22 23:27:16 +0000506</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000507
Owen Andersonb6e4ff02011-01-03 21:38:41 +0000508<!-- _______________________________________________________________________ -->
509<div class="doc_subsubsection">The <tt>addEscapingUse</tt> method</div>
510
511<div class="doc_text">
512<p>The <tt>addEscapingUse</tt> method is used when the uses of a pointer
513value have changed in ways that may invalidate precomputed analysis information.
514Implementations may either use this callback to provide conservative responses
515for points whose uses have change since analysis time, or may recompute some
516or all of their internal state to continue providing accurate responses.</p>
517
518<p>In general, any new use of a pointer value is considered an escaping use,
519and must be reported through this callback, <em>except</em> for the
520uses below:</p>
521
522<ul>
523 <li>A <tt>bitcast</tt> or <tt>getelementptr</tt> of the pointer</li>
524 <li>A <tt>store</tt> through the pointer (but not a <tt>store</tt>
525 <em>of</em> the pointer)</li>
526 <li>A <tt>load</tt> through the pointer</li>
527</ul>
528</div>
529
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000530<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000531<div class="doc_subsection">
532 <a name="implefficiency">Efficiency Issues</a>
533</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000534
Misha Brukman283ff692003-10-22 23:27:16 +0000535<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000536
537<p>From the LLVM perspective, the only thing you need to do to provide an
538efficient alias analysis is to make sure that alias analysis <b>queries</b> are
539serviced quickly. The actual calculation of the alias analysis results (the
540"run" method) is only performed once, but many (perhaps duplicate) queries may
541be performed. Because of this, try to move as much computation to the run
542method as possible (within reason).</p>
543
Misha Brukman283ff692003-10-22 23:27:16 +0000544</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000545
Dan Gohmana3de0182010-06-24 19:34:03 +0000546<!-- ======================================================================= -->
547<div class="doc_subsection">
Dan Gohmanb3f04f42010-12-15 18:45:20 +0000548 <a name="limitations">Limitations</a>
Dan Gohmana3de0182010-06-24 19:34:03 +0000549</div>
550
551<div class="doc_text">
552
Dan Gohmanb3f04f42010-12-15 18:45:20 +0000553<p>The AliasAnalysis infrastructure has several limitations which make
554writing a new <tt>AliasAnalysis</tt> implementation difficult.</p>
Dan Gohmana3de0182010-06-24 19:34:03 +0000555
556<p>There is no way to override the default alias analysis. It would
557be very useful to be able to do something like "opt -my-aa -O2" and
558have it use -my-aa for all passes which need AliasAnalysis, but there
559is currently no support for that, short of changing the source code
560and recompiling. Similarly, there is also no way of setting a chain
561of analyses as the default.</p>
562
563<p>There is no way for transform passes to declare that they preserve
564<tt>AliasAnalysis</tt> implementations. The <tt>AliasAnalysis</tt>
565interface includes <tt>deleteValue</tt> and <tt>copyValue</tt> methods
566which are intended to allow a pass to keep an AliasAnalysis consistent,
567however there's no way for a pass to declare in its
568<tt>getAnalysisUsage</tt> that it does so. Some passes attempt to use
569<tt>AU.addPreserved&lt;AliasAnalysis&gt;</tt>, however this doesn't
570actually have any effect.</tt>
571
572<p><tt>AliasAnalysisCounter</tt> (<tt>-count-aa</tt>) and <tt>AliasDebugger</tt>
573(<tt>-debug-aa</tt>) are implemented as <tt>ModulePass</tt> classes, so if your
574alias analysis uses <tt>FunctionPass</tt>, it won't be able to use
575these utilities. If you try to use them, the pass manager will
576silently route alias analysis queries directly to
577<tt>BasicAliasAnalysis</tt> instead.</p>
578
579<p>Similarly, the <tt>opt -p</tt> option introduces <tt>ModulePass</tt>
580passes between each pass, which prevents the use of <tt>FunctionPass</tt>
581alias analysis passes.</p>
582
Dan Gohmanb3f04f42010-12-15 18:45:20 +0000583<p>The <tt>AliasAnalysis</tt> API does have functions for notifying
584implementations when values are deleted or copied, however these
585aren't sufficient. There are many other ways that LLVM IR can be
586modified which could be relevant to <tt>AliasAnalysis</tt>
587implementations which can not be expressed.</p>
588
589<p>The <tt>AliasAnalysisDebugger</tt> utility seems to suggest that
590<tt>AliasAnalysis</tt> implementations can expect that they will be
591informed of any relevant <tt>Value</tt> before it appears in an
592alias query. However, popular clients such as <tt>GVN</tt> don't
593support this, and are known to trigger errors when run with the
594<tt>AliasAnalysisDebugger</tt>.</p>
595
596<p>Due to several of the above limitations, the most obvious use for
597the <tt>AliasAnalysisCounter</tt> utility, collecting stats on all
598alias queries in a compilation, doesn't work, even if the
599<tt>AliasAnalysis</tt> implementations don't use <tt>FunctionPass</tt>.
600There's no way to set a default, much less a default sequence,
601and there's no way to preserve it.</p>
602
603<p>The <tt>AliasSetTracker</tt> class (which is used by <tt>LICM</tt>
604makes a non-deterministic number of alias queries. This can cause stats
605collected by <tt>AliasAnalysisCounter</tt> to have fluctuations among
606identical runs, for example. Another consequence is that debugging
607techniques involving pausing execution after a predetermined number
608of queries can be unreliable.</p>
609
610<p>Many alias queries can be reformulated in terms of other alias
611queries. When multiple <tt>AliasAnalysis</tt> queries are chained together,
612it would make sense to start those queries from the beginning of the chain,
613with care taken to avoid infinite looping, however currently an
614implementation which wants to do this can only start such queries
615from itself.</p>
616
Dan Gohmana3de0182010-06-24 19:34:03 +0000617</div>
618
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000619<!-- *********************************************************************** -->
Misha Brukman283ff692003-10-22 23:27:16 +0000620<div class="doc_section">
Chris Lattner2a19d832004-05-23 21:04:01 +0000621 <a name="using">Using alias analysis results</a>
Misha Brukman283ff692003-10-22 23:27:16 +0000622</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000623<!-- *********************************************************************** -->
624
Misha Brukman283ff692003-10-22 23:27:16 +0000625<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000626
627<p>There are several different ways to use alias analysis results. In order of
628preference, these are...</p>
629
Misha Brukman283ff692003-10-22 23:27:16 +0000630</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000631
632<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000633<div class="doc_subsection">
Chris Lattner6a6d3cc2009-04-25 21:11:37 +0000634 <a name="memdep">Using the <tt>MemoryDependenceAnalysis</tt> Pass</a>
Misha Brukman283ff692003-10-22 23:27:16 +0000635</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000636
Misha Brukman283ff692003-10-22 23:27:16 +0000637<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000638
Chris Lattner6a6d3cc2009-04-25 21:11:37 +0000639<p>The <tt>memdep</tt> pass uses alias analysis to provide high-level dependence
640information about memory-using instructions. This will tell you which store
641feeds into a load, for example. It uses caching and other techniques to be
642efficient, and is used by Dead Store Elimination, GVN, and memcpy optimizations.
643</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000644
Misha Brukman283ff692003-10-22 23:27:16 +0000645</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000646
647<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000648<div class="doc_subsection">
649 <a name="ast">Using the <tt>AliasSetTracker</tt> class</a>
650</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000651
Misha Brukman283ff692003-10-22 23:27:16 +0000652<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000653
654<p>Many transformations need information about alias <b>sets</b> that are active
655in some scope, rather than information about pairwise aliasing. The <tt><a
Misha Brukman2361fcf2004-07-28 22:18:33 +0000656href="/doxygen/classllvm_1_1AliasSetTracker.html">AliasSetTracker</a></tt> class
657is used to efficiently build these Alias Sets from the pairwise alias analysis
Chris Lattner2a19d832004-05-23 21:04:01 +0000658information provided by the <tt>AliasAnalysis</tt> interface.</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000659
Chris Lattner2a19d832004-05-23 21:04:01 +0000660<p>First you initialize the AliasSetTracker by using the "<tt>add</tt>" methods
661to add information about various potentially aliasing instructions in the scope
662you are interested in. Once all of the alias sets are completed, your pass
663should simply iterate through the constructed alias sets, using the
664<tt>AliasSetTracker</tt> <tt>begin()</tt>/<tt>end()</tt> methods.</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000665
666<p>The <tt>AliasSet</tt>s formed by the <tt>AliasSetTracker</tt> are guaranteed
Chris Lattnerb8b9163a2003-12-19 08:43:07 +0000667to be disjoint, calculate mod/ref information and volatility for the set, and
668keep track of whether or not all of the pointers in the set are Must aliases.
669The AliasSetTracker also makes sure that sets are properly folded due to call
Misha Brukman084a3a072003-10-24 18:06:11 +0000670instructions, and can provide a list of pointers in each set.</p>
671
672<p>As an example user of this, the <a href="/doxygen/structLICM.html">Loop
Chris Lattner2a19d832004-05-23 21:04:01 +0000673Invariant Code Motion</a> pass uses <tt>AliasSetTracker</tt>s to calculate alias
674sets for each loop nest. If an <tt>AliasSet</tt> in a loop is not modified,
675then all load instructions from that set may be hoisted out of the loop. If any
676alias sets are stored to <b>and</b> are must alias sets, then the stores may be
677sunk to outside of the loop, promoting the memory location to a register for the
678duration of the loop nest. Both of these transformations only apply if the
679pointer argument is loop-invariant.</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000680
Misha Brukman283ff692003-10-22 23:27:16 +0000681</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000682
Chris Lattner2a19d832004-05-23 21:04:01 +0000683<!-- _______________________________________________________________________ -->
Chris Lattnerb8b9163a2003-12-19 08:43:07 +0000684<div class="doc_subsubsection">
685 The AliasSetTracker implementation
686</div>
687
688<div class="doc_text">
689
690<p>The AliasSetTracker class is implemented to be as efficient as possible. It
691uses the union-find algorithm to efficiently merge AliasSets when a pointer is
692inserted into the AliasSetTracker that aliases multiple sets. The primary data
693structure is a hash table mapping pointers to the AliasSet they are in.</p>
694
695<p>The AliasSetTracker class must maintain a list of all of the LLVM Value*'s
696that are in each AliasSet. Since the hash table already has entries for each
697LLVM Value* of interest, the AliasesSets thread the linked list through these
698hash-table nodes to avoid having to allocate memory unnecessarily, and to make
699merging alias sets extremely efficient (the linked list merge is constant time).
700</p>
701
702<p>You shouldn't need to understand these details if you are just a client of
703the AliasSetTracker, but if you look at the code, hopefully this brief
704description will help make sense of why things are designed the way they
705are.</p>
706
707</div>
708
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000709<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000710<div class="doc_subsection">
Chris Lattner2a19d832004-05-23 21:04:01 +0000711 <a name="direct">Using the <tt>AliasAnalysis</tt> interface directly</a>
Misha Brukman283ff692003-10-22 23:27:16 +0000712</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000713
Misha Brukman283ff692003-10-22 23:27:16 +0000714<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000715
Chris Lattner2a19d832004-05-23 21:04:01 +0000716<p>If neither of these utility class are what your pass needs, you should use
717the interfaces exposed by the <tt>AliasAnalysis</tt> class directly. Try to use
718the higher-level methods when possible (e.g., use mod/ref information instead of
719the <a href="#alias"><tt>alias</tt></a> method directly if possible) to get the
720best precision and efficiency.</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000721
Misha Brukman283ff692003-10-22 23:27:16 +0000722</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000723
724<!-- *********************************************************************** -->
Misha Brukman283ff692003-10-22 23:27:16 +0000725<div class="doc_section">
Chris Lattner2a19d832004-05-23 21:04:01 +0000726 <a name="exist">Existing alias analysis implementations and clients</a>
Misha Brukman283ff692003-10-22 23:27:16 +0000727</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000728<!-- *********************************************************************** -->
729
Misha Brukman283ff692003-10-22 23:27:16 +0000730<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000731
Chris Lattner2a19d832004-05-23 21:04:01 +0000732<p>If you're going to be working with the LLVM alias analysis infrastructure,
733you should know what clients and implementations of alias analysis are
734available. In particular, if you are implementing an alias analysis, you should
735be aware of the <a href="#aliasanalysis-debug">the clients</a> that are useful
736for monitoring and evaluating different implementations.</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000737
Misha Brukman283ff692003-10-22 23:27:16 +0000738</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000739
740<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000741<div class="doc_subsection">
Chris Lattner2a19d832004-05-23 21:04:01 +0000742 <a name="impls">Available <tt>AliasAnalysis</tt> implementations</a>
743</div>
744
745<div class="doc_text">
746
747<p>This section lists the various implementations of the <tt>AliasAnalysis</tt>
748interface. With the exception of the <a href="#no-aa"><tt>-no-aa</tt></a> and
749<a href="#basic-aa"><tt>-basicaa</tt></a> implementations, all of these <a
Chris Lattner53e5e2a2004-06-21 22:52:48 +0000750href="#chaining">chain</a> to other alias analysis implementations.</p>
Chris Lattner2a19d832004-05-23 21:04:01 +0000751
752</div>
753
754<!-- _______________________________________________________________________ -->
755<div class="doc_subsubsection">
Misha Brukman283ff692003-10-22 23:27:16 +0000756 <a name="no-aa">The <tt>-no-aa</tt> pass</a>
757</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000758
Misha Brukman283ff692003-10-22 23:27:16 +0000759<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000760
Chris Lattner2a19d832004-05-23 21:04:01 +0000761<p>The <tt>-no-aa</tt> pass is just like what it sounds: an alias analysis that
762never returns any useful information. This pass can be useful if you think that
763alias analysis is doing something wrong and are trying to narrow down a
764problem.</p>
765
766</div>
767
Chris Lattner2a19d832004-05-23 21:04:01 +0000768<!-- _______________________________________________________________________ -->
769<div class="doc_subsubsection">
770 <a name="basic-aa">The <tt>-basicaa</tt> pass</a>
771</div>
772
773<div class="doc_text">
774
Dan Gohmanb83aed92010-11-15 18:07:16 +0000775<p>The <tt>-basicaa</tt> pass is an aggressive local analysis that "knows"
776many important facts:</p>
Chris Lattner2a19d832004-05-23 21:04:01 +0000777
778<ul>
779<li>Distinct globals, stack allocations, and heap allocations can never
780 alias.</li>
781<li>Globals, stack allocations, and heap allocations never alias the null
782 pointer.</li>
783<li>Different fields of a structure do not alias.</li>
784<li>Indexes into arrays with statically differing subscripts cannot alias.</li>
785<li>Many common standard C library functions <a
786 href="#simplemodref">never access memory or only read memory</a>.</li>
787<li>Pointers that obviously point to constant globals
788 "<tt>pointToConstantMemory</tt>".</li>
789<li>Function calls can not modify or references stack allocations if they never
790 escape from the function that allocates them (a common case for automatic
791 arrays).</li>
792</ul>
793
794</div>
795
Chris Lattner9475b962004-06-28 19:19:47 +0000796<!-- _______________________________________________________________________ -->
797<div class="doc_subsubsection">
798 <a name="globalsmodref">The <tt>-globalsmodref-aa</tt> pass</a>
799</div>
800
801<div class="doc_text">
802
803<p>This pass implements a simple context-sensitive mod/ref and alias analysis
Chris Lattnerd1384372004-07-27 07:50:07 +0000804for internal global variables that don't "have their address taken". If a
805global does not have its address taken, the pass knows that no pointers alias
806the global. This pass also keeps track of functions that it knows never access
Chris Lattner6a6d3cc2009-04-25 21:11:37 +0000807memory or never read memory. This allows certain optimizations (e.g. GVN) to
Chris Lattnerd1384372004-07-27 07:50:07 +0000808eliminate call instructions entirely.
Chris Lattner9475b962004-06-28 19:19:47 +0000809</p>
810
811<p>The real power of this pass is that it provides context-sensitive mod/ref
812information for call instructions. This allows the optimizer to know that
813calls to a function do not clobber or read the value of the global, allowing
814loads and stores to be eliminated.</p>
815
816<p>Note that this pass is somewhat limited in its scope (only support
817non-address taken globals), but is very quick analysis.</p>
818</div>
Chris Lattner2a19d832004-05-23 21:04:01 +0000819
820<!-- _______________________________________________________________________ -->
821<div class="doc_subsubsection">
Chris Lattner2a19d832004-05-23 21:04:01 +0000822 <a name="steens-aa">The <tt>-steens-aa</tt> pass</a>
823</div>
824
825<div class="doc_text">
826
827<p>The <tt>-steens-aa</tt> pass implements a variation on the well-known
828"Steensgaard's algorithm" for interprocedural alias analysis. Steensgaard's
829algorithm is a unification-based, flow-insensitive, context-insensitive, and
830field-insensitive alias analysis that is also very scalable (effectively linear
831time).</p>
832
833<p>The LLVM <tt>-steens-aa</tt> pass implements a "speculatively
834field-<b>sensitive</b>" version of Steensgaard's algorithm using the Data
835Structure Analysis framework. This gives it substantially more precision than
836the standard algorithm while maintaining excellent analysis scalability.</p>
837
Chris Lattner4b325282007-07-03 04:41:50 +0000838<p>Note that <tt>-steens-aa</tt> is available in the optional "poolalloc"
839module, it is not part of the LLVM core.</p>
840
Chris Lattner2a19d832004-05-23 21:04:01 +0000841</div>
842
843<!-- _______________________________________________________________________ -->
844<div class="doc_subsubsection">
845 <a name="ds-aa">The <tt>-ds-aa</tt> pass</a>
846</div>
847
848<div class="doc_text">
849
850<p>The <tt>-ds-aa</tt> pass implements the full Data Structure Analysis
851algorithm. Data Structure Analysis is a modular unification-based,
852flow-insensitive, context-<b>sensitive</b>, and speculatively
853field-<b>sensitive</b> alias analysis that is also quite scalable, usually at
854O(n*log(n)).</p>
855
856<p>This algorithm is capable of responding to a full variety of alias analysis
857queries, and can provide context-sensitive mod/ref information as well. The
858only major facility not implemented so far is support for must-alias
859information.</p>
Misha Brukman084a3a072003-10-24 18:06:11 +0000860
Chris Lattner4b325282007-07-03 04:41:50 +0000861<p>Note that <tt>-ds-aa</tt> is available in the optional "poolalloc"
862module, it is not part of the LLVM core.</p>
863
Misha Brukman283ff692003-10-22 23:27:16 +0000864</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000865
Dan Gohmanb160ba42010-06-28 22:09:52 +0000866<!-- _______________________________________________________________________ -->
867<div class="doc_subsubsection">
868 <a name="scev-aa">The <tt>-scev-aa</tt> pass</a>
869</div>
870
871<div class="doc_text">
872
873<p>The <tt>-scev-aa</tt> pass implements AliasAnalysis queries by
874translating them into ScalarEvolution queries. This gives it a
875more complete understanding of <tt>getelementptr</tt> instructions
876and loop induction variables than other alias analyses have.</p>
877
878</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000879
880<!-- ======================================================================= -->
Misha Brukman283ff692003-10-22 23:27:16 +0000881<div class="doc_subsection">
Chris Lattner2a19d832004-05-23 21:04:01 +0000882 <a name="aliasanalysis-xforms">Alias analysis driven transformations</a>
883</div>
884
885<div class="doc_text">
886LLVM includes several alias-analysis driven transformations which can be used
887with any of the implementations above.
888</div>
889
890<!-- _______________________________________________________________________ -->
891<div class="doc_subsubsection">
892 <a name="adce">The <tt>-adce</tt> pass</a>
893</div>
894
895<div class="doc_text">
896
897<p>The <tt>-adce</tt> pass, which implements Aggressive Dead Code Elimination
898uses the <tt>AliasAnalysis</tt> interface to delete calls to functions that do
899not have side-effects and are not used.</p>
900
901</div>
902
903
904<!-- _______________________________________________________________________ -->
905<div class="doc_subsubsection">
906 <a name="licm">The <tt>-licm</tt> pass</a>
907</div>
908
909<div class="doc_text">
910
911<p>The <tt>-licm</tt> pass implements various Loop Invariant Code Motion related
912transformations. It uses the <tt>AliasAnalysis</tt> interface for several
913different transformations:</p>
914
915<ul>
916<li>It uses mod/ref information to hoist or sink load instructions out of loops
917if there are no instructions in the loop that modifies the memory loaded.</li>
918
919<li>It uses mod/ref information to hoist function calls out of loops that do not
920write to memory and are loop-invariant.</li>
921
922<li>If uses alias information to promote memory objects that are loaded and
923stored to in loops to live in a register instead. It can do this if there are
924no may aliases to the loaded/stored memory location.</li>
925</ul>
926
927</div>
928
929<!-- _______________________________________________________________________ -->
930<div class="doc_subsubsection">
931 <a name="argpromotion">The <tt>-argpromotion</tt> pass</a>
932</div>
933
934<div class="doc_text">
935<p>
936The <tt>-argpromotion</tt> pass promotes by-reference arguments to be passed in
937by-value instead. In particular, if pointer arguments are only loaded from it
938passes in the value loaded instead of the address to the function. This pass
939uses alias information to make sure that the value loaded from the argument
940pointer is not modified between the entry of the function and any load of the
941pointer.</p>
942</div>
943
944<!-- _______________________________________________________________________ -->
945<div class="doc_subsubsection">
Chris Lattner6a6d3cc2009-04-25 21:11:37 +0000946 <a name="gvn">The <tt>-gvn</tt>, <tt>-memcpyopt</tt>, and <tt>-dse</tt>
947 passes</a>
Chris Lattner2a19d832004-05-23 21:04:01 +0000948</div>
949
950<div class="doc_text">
Misha Brukman2361fcf2004-07-28 22:18:33 +0000951
Chris Lattner6a6d3cc2009-04-25 21:11:37 +0000952<p>These passes use AliasAnalysis information to reason about loads and stores.
953</p>
Chris Lattner2a19d832004-05-23 21:04:01 +0000954
955</div>
956
Chris Lattner2a19d832004-05-23 21:04:01 +0000957<!-- ======================================================================= -->
958<div class="doc_subsection">
Misha Brukman2361fcf2004-07-28 22:18:33 +0000959 <a name="aliasanalysis-debug">Clients for debugging and evaluation of
960 implementations</a>
Chris Lattner2a19d832004-05-23 21:04:01 +0000961</div>
962
Misha Brukman2361fcf2004-07-28 22:18:33 +0000963<div class="doc_text">
Chris Lattner2a19d832004-05-23 21:04:01 +0000964
Misha Brukman2361fcf2004-07-28 22:18:33 +0000965<p>These passes are useful for evaluating the various alias analysis
Chris Lattner5ea3e652010-03-01 19:24:17 +0000966implementations. You can use them with commands like '<tt>opt -ds-aa
Misha Brukman2361fcf2004-07-28 22:18:33 +0000967-aa-eval foo.bc -disable-output -stats</tt>'.</p>
968
969</div>
Chris Lattner2a19d832004-05-23 21:04:01 +0000970
971<!-- _______________________________________________________________________ -->
972<div class="doc_subsubsection">
Misha Brukman283ff692003-10-22 23:27:16 +0000973 <a name="print-alias-sets">The <tt>-print-alias-sets</tt> pass</a>
974</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000975
Misha Brukman283ff692003-10-22 23:27:16 +0000976<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000977
978<p>The <tt>-print-alias-sets</tt> pass is exposed as part of the
Chris Lattner2e6f4482006-01-03 06:04:48 +0000979<tt>opt</tt> tool to print out the Alias Sets formed by the <a
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000980href="#ast"><tt>AliasSetTracker</tt></a> class. This is useful if you're using
Chris Lattner2e6f4482006-01-03 06:04:48 +0000981the <tt>AliasSetTracker</tt> class. To use it, use something like:</p>
982
983<div class="doc_code">
984<pre>
985% opt -ds-aa -print-alias-sets -disable-output
986</pre>
987</div>
Misha Brukman084a3a072003-10-24 18:06:11 +0000988
Misha Brukman283ff692003-10-22 23:27:16 +0000989</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000990
Chris Lattner2a19d832004-05-23 21:04:01 +0000991
992<!-- _______________________________________________________________________ -->
993<div class="doc_subsubsection">
Misha Brukman283ff692003-10-22 23:27:16 +0000994 <a name="count-aa">The <tt>-count-aa</tt> pass</a>
995</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +0000996
Misha Brukman283ff692003-10-22 23:27:16 +0000997<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +0000998
999<p>The <tt>-count-aa</tt> pass is useful to see how many queries a particular
Misha Brukman2361fcf2004-07-28 22:18:33 +00001000pass is making and what responses are returned by the alias analysis. As an
1001example,</p>
Misha Brukman084a3a072003-10-24 18:06:11 +00001002
Misha Brukman2361fcf2004-07-28 22:18:33 +00001003<div class="doc_code">
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001004<pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +00001005% opt -basicaa -count-aa -ds-aa -count-aa -licm
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001006</pre>
Misha Brukman2361fcf2004-07-28 22:18:33 +00001007</div>
Misha Brukman084a3a072003-10-24 18:06:11 +00001008
Misha Brukman2361fcf2004-07-28 22:18:33 +00001009<p>will print out how many queries (and what responses are returned) by the
1010<tt>-licm</tt> pass (of the <tt>-ds-aa</tt> pass) and how many queries are made
1011of the <tt>-basicaa</tt> pass by the <tt>-ds-aa</tt> pass. This can be useful
1012when debugging a transformation or an alias analysis implementation.</p>
Misha Brukman084a3a072003-10-24 18:06:11 +00001013
Misha Brukman283ff692003-10-22 23:27:16 +00001014</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001015
Chris Lattner2a19d832004-05-23 21:04:01 +00001016<!-- _______________________________________________________________________ -->
1017<div class="doc_subsubsection">
Misha Brukman283ff692003-10-22 23:27:16 +00001018 <a name="aa-eval">The <tt>-aa-eval</tt> pass</a>
1019</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001020
Misha Brukman283ff692003-10-22 23:27:16 +00001021<div class="doc_text">
Misha Brukman084a3a072003-10-24 18:06:11 +00001022
1023<p>The <tt>-aa-eval</tt> pass simply iterates through all pairs of pointers in a
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001024function and asks an alias analysis whether or not the pointers alias. This
1025gives an indication of the precision of the alias analysis. Statistics are
Chris Lattner2a19d832004-05-23 21:04:01 +00001026printed indicating the percent of no/may/must aliases found (a more precise
1027algorithm will have a lower number of may aliases).</p>
Misha Brukman084a3a072003-10-24 18:06:11 +00001028
Misha Brukman283ff692003-10-22 23:27:16 +00001029</div>
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001030
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001031<!-- *********************************************************************** -->
Owen Andersond263cfa2007-10-02 00:43:25 +00001032<div class="doc_section">
1033 <a name="memdep">Memory Dependence Analysis</a>
1034</div>
1035<!-- *********************************************************************** -->
1036
1037<div class="doc_text">
1038
1039<p>If you're just looking to be a client of alias analysis information, consider
1040using the Memory Dependence Analysis interface instead. MemDep is a lazy,
1041caching layer on top of alias analysis that is able to answer the question of
1042what preceding memory operations a given instruction depends on, either at an
1043intra- or inter-block level. Because of its laziness and caching
1044policy, using MemDep can be a significant performance win over accessing alias
1045analysis directly.</p>
1046
1047</div>
1048
1049<!-- *********************************************************************** -->
Chris Lattnerb512e9f2003-03-04 19:37:49 +00001050
Misha Brukman084a3a072003-10-24 18:06:11 +00001051<hr>
Misha Brukmane8e932a2003-11-22 01:26:21 +00001052<address>
Misha Brukman8834af92003-12-21 22:53:21 +00001053 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +00001054 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukmane8e932a2003-11-22 01:26:21 +00001055 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +00001056 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukmane8e932a2003-11-22 01:26:21 +00001057
1058 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencerca058542006-03-14 05:39:39 +00001059 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
Misha Brukmane8e932a2003-11-22 01:26:21 +00001060 Last modified: $Date$
1061</address>
Misha Brukman283ff692003-10-22 23:27:16 +00001062
1063</body>
1064</html>