blob: ebf638689824b25d14329364439194cbf497e15f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5 <title>LLVM Alias Analysis Infrastructure</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8<body>
9
10<div class="doc_title">
11 LLVM Alias Analysis Infrastructure
12</div>
13
14<ol>
15 <li><a href="#introduction">Introduction</a></li>
16
17 <li><a href="#overview"><tt>AliasAnalysis</tt> Class Overview</a>
18 <ul>
19 <li><a href="#pointers">Representation of Pointers</a></li>
20 <li><a href="#alias">The <tt>alias</tt> method</a></li>
21 <li><a href="#ModRefInfo">The <tt>getModRefInfo</tt> methods</a></li>
22 <li><a href="#OtherItfs">Other useful <tt>AliasAnalysis</tt> methods</a></li>
23 </ul>
24 </li>
25
26 <li><a href="#writingnew">Writing a new <tt>AliasAnalysis</tt> Implementation</a>
27 <ul>
28 <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>
31 <li><a href="#chaining"><tt>AliasAnalysis</tt> chaining behavior</a></li>
32 <li><a href="#updating">Updating analysis results for transformations</a></li>
33 <li><a href="#implefficiency">Efficiency Issues</a></li>
34 </ul>
35 </li>
36
37 <li><a href="#using">Using alias analysis results</a>
38 <ul>
Chris Lattner72da5762009-04-25 21:11:37 +000039 <li><a href="#memdep">Using the <tt>MemoryDependenceAnalysis</tt> Pass</a></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 <li><a href="#ast">Using the <tt>AliasSetTracker</tt> class</a></li>
41 <li><a href="#direct">Using the <tt>AliasAnalysis</tt> interface directly</a></li>
42 </ul>
43 </li>
44
45 <li><a href="#exist">Existing alias analysis implementations and clients</a>
46 <ul>
47 <li><a href="#impls">Available <tt>AliasAnalysis</tt> implementations</a></li>
48 <li><a href="#aliasanalysis-xforms">Alias analysis driven transformations</a></li>
49 <li><a href="#aliasanalysis-debug">Clients for debugging and evaluation of
50 implementations</a></li>
51 </ul>
52 </li>
Owen Anderson4f10b312007-10-02 00:44:20 +000053 <li><a href="#memdep">Memory Dependence Analysis</a></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054</ol>
55
56<div class="doc_author">
57 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
58</div>
59
60<!-- *********************************************************************** -->
61<div class="doc_section">
62 <a name="introduction">Introduction</a>
63</div>
64<!-- *********************************************************************** -->
65
66<div class="doc_text">
67
68<p>Alias Analysis (aka Pointer Analysis) is a class of techniques which attempt
69to determine whether or not two pointers ever can point to the same object in
70memory. There are many different algorithms for alias analysis and many
71different ways of classifying them: flow-sensitive vs flow-insensitive,
72context-sensitive vs context-insensitive, field-sensitive vs field-insensitive,
73unification-based vs subset-based, etc. Traditionally, alias analyses respond
74to a query with a <a href="#MustMayNo">Must, May, or No</a> alias response,
75indicating that two pointers always point to the same object, might point to the
76same object, or are known to never point to the same object.</p>
77
78<p>The LLVM <a
79href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
80class is the primary interface used by clients and implementations of alias
81analyses in the LLVM system. This class is the common interface between clients
82of alias analysis information and the implementations providing it, and is
83designed to support a wide range of implementations and clients (but currently
84all clients are assumed to be flow-insensitive). In addition to simple alias
85analysis information, this class exposes Mod/Ref information from those
86implementations which can provide it, allowing for powerful analyses and
87transformations to work well together.</p>
88
89<p>This document contains information necessary to successfully implement this
90interface, use it, and to test both sides. It also explains some of the finer
91points about what exactly results mean. If you feel that something is unclear
92or should be added, please <a href="mailto:sabre@nondot.org">let me
93know</a>.</p>
94
95</div>
96
97<!-- *********************************************************************** -->
98<div class="doc_section">
99 <a name="overview"><tt>AliasAnalysis</tt> Class Overview</a>
100</div>
101<!-- *********************************************************************** -->
102
103<div class="doc_text">
104
105<p>The <a
106href="http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
107class defines the interface that the various alias analysis implementations
108should support. This class exports two important enums: <tt>AliasResult</tt>
109and <tt>ModRefResult</tt> which represent the result of an alias query or a
110mod/ref query, respectively.</p>
111
112<p>The <tt>AliasAnalysis</tt> interface exposes information about memory,
113represented in several different ways. In particular, memory objects are
114represented as a starting address and size, and function calls are represented
115as the actual <tt>call</tt> or <tt>invoke</tt> instructions that performs the
116call. The <tt>AliasAnalysis</tt> interface also exposes some helper methods
117which allow you to get mod/ref information for arbitrary instructions.</p>
118
119</div>
120
121<!-- ======================================================================= -->
122<div class="doc_subsection">
123 <a name="pointers">Representation of Pointers</a>
124</div>
125
126<div class="doc_text">
127
128<p>Most importantly, the <tt>AliasAnalysis</tt> class provides several methods
129which are used to query whether or not two memory objects alias, whether
130function calls can modify or read a memory object, etc. For all of these
131queries, memory objects are represented as a pair of their starting address (a
132symbolic LLVM <tt>Value*</tt>) and a static size.</p>
133
134<p>Representing memory objects as a starting address and a size is critically
135important for correct Alias Analyses. For example, consider this (silly, but
136possible) C code:</p>
137
138<div class="doc_code">
139<pre>
140int i;
141char C[2];
142char A[10];
143/* ... */
144for (i = 0; i != 10; ++i) {
145 C[0] = A[i]; /* One byte store */
146 C[1] = A[9-i]; /* One byte store */
147}
148</pre>
149</div>
150
151<p>In this case, the <tt>basicaa</tt> pass will disambiguate the stores to
152<tt>C[0]</tt> and <tt>C[1]</tt> because they are accesses to two distinct
153locations one byte apart, and the accesses are each one byte. In this case, the
154LICM pass can use store motion to remove the stores from the loop. In
155constrast, the following code:</p>
156
157<div class="doc_code">
158<pre>
159int i;
160char C[2];
161char A[10];
162/* ... */
163for (i = 0; i != 10; ++i) {
164 ((short*)C)[0] = A[i]; /* Two byte store! */
165 C[1] = A[9-i]; /* One byte store */
166}
167</pre>
168</div>
169
170<p>In this case, the two stores to C do alias each other, because the access to
171the <tt>&amp;C[0]</tt> element is a two byte access. If size information wasn't
172available in the query, even the first case would have to conservatively assume
173that the accesses alias.</p>
174
175</div>
176
177<!-- ======================================================================= -->
178<div class="doc_subsection">
179 <a name="alias">The <tt>alias</tt> method</a>
180</div>
181
182<div class="doc_text">
183The <tt>alias</tt> method is the primary interface used to determine whether or
184not two memory objects alias each other. It takes two memory objects as input
185and returns MustAlias, MayAlias, or NoAlias as appropriate.
186</div>
187
188<!-- _______________________________________________________________________ -->
189<div class="doc_subsubsection">
190 <a name="MustMayNo">Must, May, and No Alias Responses</a>
191</div>
192
193<div class="doc_text">
Nick Lewycky74c87562008-12-14 21:08:48 +0000194<p>The NoAlias response is used when the two pointers refer to distinct objects,
Nick Lewyckyfeb035b2008-12-18 02:15:05 +0000195regardless of whether the pointers compare equal. For example, freed pointers
196don't alias any pointers that were allocated afterwards. As a degenerate case,
197pointers returned by malloc(0) have no bytes for an object, and are considered
198NoAlias even when malloc returns the same pointer. The same rule applies to
199NULL pointers.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
Nick Lewycky74c87562008-12-14 21:08:48 +0000201<p>The MayAlias response is used whenever the two pointers might refer to the
202same object. If the two memory objects overlap, but do not start at the same
203location, return MayAlias.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204
Nick Lewycky74c87562008-12-14 21:08:48 +0000205<p>The MustAlias response may only be returned if the two memory objects are
206guaranteed to always start at exactly the same location. A MustAlias response
207implies that the pointers compare equal.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208
209</div>
210
211<!-- ======================================================================= -->
212<div class="doc_subsection">
213 <a name="ModRefInfo">The <tt>getModRefInfo</tt> methods</a>
214</div>
215
216<div class="doc_text">
217
218<p>The <tt>getModRefInfo</tt> methods return information about whether the
219execution of an instruction can read or modify a memory location. Mod/Ref
220information is always conservative: if an instruction <b>might</b> read or write
221a location, ModRef is returned.</p>
222
223<p>The <tt>AliasAnalysis</tt> class also provides a <tt>getModRefInfo</tt>
224method for testing dependencies between function calls. This method takes two
225call sites (CS1 &amp; CS2), returns NoModRef if the two calls refer to disjoint
226memory locations, Ref if CS1 reads memory written by CS2, Mod if CS1 writes to
227memory read or written by CS2, or ModRef if CS1 might read or write memory
Chris Lattner3b803752009-11-22 16:01:44 +0000228accessed by CS2. Note that this relation is not commutative.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229
230</div>
231
232
233<!-- ======================================================================= -->
234<div class="doc_subsection">
235 <a name="OtherItfs">Other useful <tt>AliasAnalysis</tt> methods</a>
236</div>
237
238<div class="doc_text">
239
240<p>
241Several other tidbits of information are often collected by various alias
242analysis implementations and can be put to good use by various clients.
243</p>
244
245</div>
246
247<!-- _______________________________________________________________________ -->
248<div class="doc_subsubsection">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 The <tt>pointsToConstantMemory</tt> method
250</div>
251
252<div class="doc_text">
253
254<p>The <tt>pointsToConstantMemory</tt> method returns true if and only if the
255analysis can prove that the pointer only points to unchanging memory locations
256(functions, constant global variables, and the null pointer). This information
257can be used to refine mod/ref information: it is impossible for an unchanging
258memory location to be modified.</p>
259
260</div>
261
262<!-- _______________________________________________________________________ -->
263<div class="doc_subsubsection">
264 <a name="simplemodref">The <tt>doesNotAccessMemory</tt> and
265 <tt>onlyReadsMemory</tt> methods</a>
266</div>
267
268<div class="doc_text">
269
270<p>These methods are used to provide very simple mod/ref information for
271function calls. The <tt>doesNotAccessMemory</tt> method returns true for a
272function if the analysis can prove that the function never reads or writes to
273memory, or if the function only reads from constant memory. Functions with this
274property are side-effect free and only depend on their input arguments, allowing
275them to be eliminated if they form common subexpressions or be hoisted out of
276loops. Many common functions behave this way (e.g., <tt>sin</tt> and
277<tt>cos</tt>) but many others do not (e.g., <tt>acos</tt>, which modifies the
278<tt>errno</tt> variable).</p>
279
280<p>The <tt>onlyReadsMemory</tt> method returns true for a function if analysis
281can prove that (at most) the function only reads from non-volatile memory.
282Functions with this property are side-effect free, only depending on their input
283arguments and the state of memory when they are called. This property allows
284calls to these functions to be eliminated and moved around, as long as there is
285no store instruction that changes the contents of memory. Note that all
286functions that satisfy the <tt>doesNotAccessMemory</tt> method also satisfies
287<tt>onlyReadsMemory</tt>.</p>
288
289</div>
290
291<!-- *********************************************************************** -->
292<div class="doc_section">
293 <a name="writingnew">Writing a new <tt>AliasAnalysis</tt> Implementation</a>
294</div>
295<!-- *********************************************************************** -->
296
297<div class="doc_text">
298
299<p>Writing a new alias analysis implementation for LLVM is quite
300straight-forward. There are already several implementations that you can use
301for examples, and the following information should help fill in any details.
302For a examples, take a look at the <a href="#impls">various alias analysis
303implementations</a> included with LLVM.</p>
304
305</div>
306
307<!-- ======================================================================= -->
308<div class="doc_subsection">
309 <a name="passsubclasses">Different Pass styles</a>
310</div>
311
312<div class="doc_text">
313
314<p>The first step to determining what type of <a
315href="WritingAnLLVMPass.html">LLVM pass</a> you need to use for your Alias
316Analysis. As is the case with most other analyses and transformations, the
317answer should be fairly obvious from what type of problem you are trying to
318solve:</p>
319
320<ol>
321 <li>If you require interprocedural analysis, it should be a
322 <tt>Pass</tt>.</li>
323 <li>If you are a function-local analysis, subclass <tt>FunctionPass</tt>.</li>
324 <li>If you don't need to look at the program at all, subclass
325 <tt>ImmutablePass</tt>.</li>
326</ol>
327
328<p>In addition to the pass that you subclass, you should also inherit from the
329<tt>AliasAnalysis</tt> interface, of course, and use the
330<tt>RegisterAnalysisGroup</tt> template to register as an implementation of
331<tt>AliasAnalysis</tt>.</p>
332
333</div>
334
335<!-- ======================================================================= -->
336<div class="doc_subsection">
337 <a name="requiredcalls">Required initialization calls</a>
338</div>
339
340<div class="doc_text">
341
342<p>Your subclass of <tt>AliasAnalysis</tt> is required to invoke two methods on
343the <tt>AliasAnalysis</tt> base class: <tt>getAnalysisUsage</tt> and
344<tt>InitializeAliasAnalysis</tt>. In particular, your implementation of
345<tt>getAnalysisUsage</tt> should explicitly call into the
346<tt>AliasAnalysis::getAnalysisUsage</tt> method in addition to doing any
347declaring any pass dependencies your pass has. Thus you should have something
348like this:</p>
349
350<div class="doc_code">
351<pre>
352void getAnalysisUsage(AnalysisUsage &amp;AU) const {
353 AliasAnalysis::getAnalysisUsage(AU);
354 <i>// declare your dependencies here.</i>
355}
356</pre>
357</div>
358
359<p>Additionally, your must invoke the <tt>InitializeAliasAnalysis</tt> method
360from your analysis run method (<tt>run</tt> for a <tt>Pass</tt>,
361<tt>runOnFunction</tt> for a <tt>FunctionPass</tt>, or <tt>InitializePass</tt>
362for an <tt>ImmutablePass</tt>). For example (as part of a <tt>Pass</tt>):</p>
363
364<div class="doc_code">
365<pre>
366bool run(Module &amp;M) {
367 InitializeAliasAnalysis(this);
368 <i>// Perform analysis here...</i>
369 return false;
370}
371</pre>
372</div>
373
374</div>
375
376<!-- ======================================================================= -->
377<div class="doc_subsection">
378 <a name="interfaces">Interfaces which may be specified</a>
379</div>
380
381<div class="doc_text">
382
383<p>All of the <a
384href="/doxygen/classllvm_1_1AliasAnalysis.html"><tt>AliasAnalysis</tt></a>
385virtual methods default to providing <a href="#chaining">chaining</a> to another
386alias analysis implementation, which ends up returning conservatively correct
387information (returning "May" Alias and "Mod/Ref" for alias and mod/ref queries
388respectively). Depending on the capabilities of the analysis you are
389implementing, you just override the interfaces you can improve.</p>
390
391</div>
392
393
394
395<!-- ======================================================================= -->
396<div class="doc_subsection">
397 <a name="chaining"><tt>AliasAnalysis</tt> chaining behavior</a>
398</div>
399
400<div class="doc_text">
401
402<p>With only two special exceptions (the <tt><a
403href="#basic-aa">basicaa</a></tt> and <a href="#no-aa"><tt>no-aa</tt></a>
404passes) every alias analysis pass chains to another alias analysis
405implementation (for example, the user can specify "<tt>-basicaa -ds-aa
406-anders-aa -licm</tt>" to get the maximum benefit from the three alias
407analyses). The alias analysis class automatically takes care of most of this
408for methods that you don't override. For methods that you do override, in code
409paths that return a conservative MayAlias or Mod/Ref result, simply return
410whatever the superclass computes. For example:</p>
411
412<div class="doc_code">
413<pre>
414AliasAnalysis::AliasResult alias(const Value *V1, unsigned V1Size,
415 const Value *V2, unsigned V2Size) {
416 if (...)
417 return NoAlias;
418 ...
419
420 <i>// Couldn't determine a must or no-alias result.</i>
421 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
422}
423</pre>
424</div>
425
426<p>In addition to analysis queries, you must make sure to unconditionally pass
427LLVM <a href="#updating">update notification</a> methods to the superclass as
428well if you override them, which allows all alias analyses in a change to be
429updated.</p>
430
431</div>
432
433
434<!-- ======================================================================= -->
435<div class="doc_subsection">
436 <a name="updating">Updating analysis results for transformations</a>
437</div>
438
439<div class="doc_text">
440<p>
441Alias analysis information is initially computed for a static snapshot of the
442program, but clients will use this information to make transformations to the
443code. All but the most trivial forms of alias analysis will need to have their
444analysis results updated to reflect the changes made by these transformations.
445</p>
446
447<p>
448The <tt>AliasAnalysis</tt> interface exposes two methods which are used to
449communicate program changes from the clients to the analysis implementations.
450Various alias analysis implementations should use these methods to ensure that
451their internal data structures are kept up-to-date as the program changes (for
452example, when an instruction is deleted), and clients of alias analysis must be
453sure to call these interfaces appropriately.
454</p>
455</div>
456
457<!-- _______________________________________________________________________ -->
458<div class="doc_subsubsection">The <tt>deleteValue</tt> method</div>
459
460<div class="doc_text">
461The <tt>deleteValue</tt> method is called by transformations when they remove an
462instruction or any other value from the program (including values that do not
463use pointers). Typically alias analyses keep data structures that have entries
464for each value in the program. When this method is called, they should remove
465any entries for the specified value, if they exist.
466</div>
467
468<!-- _______________________________________________________________________ -->
469<div class="doc_subsubsection">The <tt>copyValue</tt> method</div>
470
471<div class="doc_text">
472The <tt>copyValue</tt> method is used when a new value is introduced into the
473program. There is no way to introduce a value into the program that did not
474exist before (this doesn't make sense for a safe compiler transformation), so
475this is the only way to introduce a new value. This method indicates that the
476new value has exactly the same properties as the value being copied.
477</div>
478
479<!-- _______________________________________________________________________ -->
480<div class="doc_subsubsection">The <tt>replaceWithNewValue</tt> method</div>
481
482<div class="doc_text">
483This method is a simple helper method that is provided to make clients easier to
484use. It is implemented by copying the old analysis information to the new
485value, then deleting the old value. This method cannot be overridden by alias
486analysis implementations.
487</div>
488
489<!-- ======================================================================= -->
490<div class="doc_subsection">
491 <a name="implefficiency">Efficiency Issues</a>
492</div>
493
494<div class="doc_text">
495
496<p>From the LLVM perspective, the only thing you need to do to provide an
497efficient alias analysis is to make sure that alias analysis <b>queries</b> are
498serviced quickly. The actual calculation of the alias analysis results (the
499"run" method) is only performed once, but many (perhaps duplicate) queries may
500be performed. Because of this, try to move as much computation to the run
501method as possible (within reason).</p>
502
503</div>
504
505<!-- *********************************************************************** -->
506<div class="doc_section">
507 <a name="using">Using alias analysis results</a>
508</div>
509<!-- *********************************************************************** -->
510
511<div class="doc_text">
512
513<p>There are several different ways to use alias analysis results. In order of
514preference, these are...</p>
515
516</div>
517
518<!-- ======================================================================= -->
519<div class="doc_subsection">
Chris Lattner72da5762009-04-25 21:11:37 +0000520 <a name="memdep">Using the <tt>MemoryDependenceAnalysis</tt> Pass</a>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521</div>
522
523<div class="doc_text">
524
Chris Lattner72da5762009-04-25 21:11:37 +0000525<p>The <tt>memdep</tt> pass uses alias analysis to provide high-level dependence
526information about memory-using instructions. This will tell you which store
527feeds into a load, for example. It uses caching and other techniques to be
528efficient, and is used by Dead Store Elimination, GVN, and memcpy optimizations.
529</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530
531</div>
532
533<!-- ======================================================================= -->
534<div class="doc_subsection">
535 <a name="ast">Using the <tt>AliasSetTracker</tt> class</a>
536</div>
537
538<div class="doc_text">
539
540<p>Many transformations need information about alias <b>sets</b> that are active
541in some scope, rather than information about pairwise aliasing. The <tt><a
542href="/doxygen/classllvm_1_1AliasSetTracker.html">AliasSetTracker</a></tt> class
543is used to efficiently build these Alias Sets from the pairwise alias analysis
544information provided by the <tt>AliasAnalysis</tt> interface.</p>
545
546<p>First you initialize the AliasSetTracker by using the "<tt>add</tt>" methods
547to add information about various potentially aliasing instructions in the scope
548you are interested in. Once all of the alias sets are completed, your pass
549should simply iterate through the constructed alias sets, using the
550<tt>AliasSetTracker</tt> <tt>begin()</tt>/<tt>end()</tt> methods.</p>
551
552<p>The <tt>AliasSet</tt>s formed by the <tt>AliasSetTracker</tt> are guaranteed
553to be disjoint, calculate mod/ref information and volatility for the set, and
554keep track of whether or not all of the pointers in the set are Must aliases.
555The AliasSetTracker also makes sure that sets are properly folded due to call
556instructions, and can provide a list of pointers in each set.</p>
557
558<p>As an example user of this, the <a href="/doxygen/structLICM.html">Loop
559Invariant Code Motion</a> pass uses <tt>AliasSetTracker</tt>s to calculate alias
560sets for each loop nest. If an <tt>AliasSet</tt> in a loop is not modified,
561then all load instructions from that set may be hoisted out of the loop. If any
562alias sets are stored to <b>and</b> are must alias sets, then the stores may be
563sunk to outside of the loop, promoting the memory location to a register for the
564duration of the loop nest. Both of these transformations only apply if the
565pointer argument is loop-invariant.</p>
566
567</div>
568
569<!-- _______________________________________________________________________ -->
570<div class="doc_subsubsection">
571 The AliasSetTracker implementation
572</div>
573
574<div class="doc_text">
575
576<p>The AliasSetTracker class is implemented to be as efficient as possible. It
577uses the union-find algorithm to efficiently merge AliasSets when a pointer is
578inserted into the AliasSetTracker that aliases multiple sets. The primary data
579structure is a hash table mapping pointers to the AliasSet they are in.</p>
580
581<p>The AliasSetTracker class must maintain a list of all of the LLVM Value*'s
582that are in each AliasSet. Since the hash table already has entries for each
583LLVM Value* of interest, the AliasesSets thread the linked list through these
584hash-table nodes to avoid having to allocate memory unnecessarily, and to make
585merging alias sets extremely efficient (the linked list merge is constant time).
586</p>
587
588<p>You shouldn't need to understand these details if you are just a client of
589the AliasSetTracker, but if you look at the code, hopefully this brief
590description will help make sense of why things are designed the way they
591are.</p>
592
593</div>
594
595<!-- ======================================================================= -->
596<div class="doc_subsection">
597 <a name="direct">Using the <tt>AliasAnalysis</tt> interface directly</a>
598</div>
599
600<div class="doc_text">
601
602<p>If neither of these utility class are what your pass needs, you should use
603the interfaces exposed by the <tt>AliasAnalysis</tt> class directly. Try to use
604the higher-level methods when possible (e.g., use mod/ref information instead of
605the <a href="#alias"><tt>alias</tt></a> method directly if possible) to get the
606best precision and efficiency.</p>
607
608</div>
609
610<!-- *********************************************************************** -->
611<div class="doc_section">
612 <a name="exist">Existing alias analysis implementations and clients</a>
613</div>
614<!-- *********************************************************************** -->
615
616<div class="doc_text">
617
618<p>If you're going to be working with the LLVM alias analysis infrastructure,
619you should know what clients and implementations of alias analysis are
620available. In particular, if you are implementing an alias analysis, you should
621be aware of the <a href="#aliasanalysis-debug">the clients</a> that are useful
622for monitoring and evaluating different implementations.</p>
623
624</div>
625
626<!-- ======================================================================= -->
627<div class="doc_subsection">
628 <a name="impls">Available <tt>AliasAnalysis</tt> implementations</a>
629</div>
630
631<div class="doc_text">
632
633<p>This section lists the various implementations of the <tt>AliasAnalysis</tt>
634interface. With the exception of the <a href="#no-aa"><tt>-no-aa</tt></a> and
635<a href="#basic-aa"><tt>-basicaa</tt></a> implementations, all of these <a
636href="#chaining">chain</a> to other alias analysis implementations.</p>
637
638</div>
639
640<!-- _______________________________________________________________________ -->
641<div class="doc_subsubsection">
642 <a name="no-aa">The <tt>-no-aa</tt> pass</a>
643</div>
644
645<div class="doc_text">
646
647<p>The <tt>-no-aa</tt> pass is just like what it sounds: an alias analysis that
648never returns any useful information. This pass can be useful if you think that
649alias analysis is doing something wrong and are trying to narrow down a
650problem.</p>
651
652</div>
653
654<!-- _______________________________________________________________________ -->
655<div class="doc_subsubsection">
656 <a name="basic-aa">The <tt>-basicaa</tt> pass</a>
657</div>
658
659<div class="doc_text">
660
661<p>The <tt>-basicaa</tt> pass is the default LLVM alias analysis. It is an
662aggressive local analysis that "knows" many important facts:</p>
663
664<ul>
665<li>Distinct globals, stack allocations, and heap allocations can never
666 alias.</li>
667<li>Globals, stack allocations, and heap allocations never alias the null
668 pointer.</li>
669<li>Different fields of a structure do not alias.</li>
670<li>Indexes into arrays with statically differing subscripts cannot alias.</li>
671<li>Many common standard C library functions <a
672 href="#simplemodref">never access memory or only read memory</a>.</li>
673<li>Pointers that obviously point to constant globals
674 "<tt>pointToConstantMemory</tt>".</li>
675<li>Function calls can not modify or references stack allocations if they never
676 escape from the function that allocates them (a common case for automatic
677 arrays).</li>
678</ul>
679
680</div>
681
682<!-- _______________________________________________________________________ -->
683<div class="doc_subsubsection">
684 <a name="globalsmodref">The <tt>-globalsmodref-aa</tt> pass</a>
685</div>
686
687<div class="doc_text">
688
689<p>This pass implements a simple context-sensitive mod/ref and alias analysis
690for internal global variables that don't "have their address taken". If a
691global does not have its address taken, the pass knows that no pointers alias
692the global. This pass also keeps track of functions that it knows never access
Chris Lattner72da5762009-04-25 21:11:37 +0000693memory or never read memory. This allows certain optimizations (e.g. GVN) to
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694eliminate call instructions entirely.
695</p>
696
697<p>The real power of this pass is that it provides context-sensitive mod/ref
698information for call instructions. This allows the optimizer to know that
699calls to a function do not clobber or read the value of the global, allowing
700loads and stores to be eliminated.</p>
701
702<p>Note that this pass is somewhat limited in its scope (only support
703non-address taken globals), but is very quick analysis.</p>
704</div>
705
706<!-- _______________________________________________________________________ -->
707<div class="doc_subsubsection">
708 <a name="anders-aa">The <tt>-anders-aa</tt> pass</a>
709</div>
710
711<div class="doc_text">
712
713<p>The <tt>-anders-aa</tt> pass implements the well-known "Andersen's algorithm"
714for interprocedural alias analysis. This algorithm is a subset-based,
715flow-insensitive, context-insensitive, and field-insensitive alias analysis that
716is widely believed to be fairly precise. Unfortunately, this algorithm is also
717O(N<sup>3</sup>). The LLVM implementation currently does not implement any of
718the refinements (such as "online cycle elimination" or "offline variable
719substitution") to improve its efficiency, so it can be quite slow in common
720cases.
721</p>
722
723</div>
724
725<!-- _______________________________________________________________________ -->
726<div class="doc_subsubsection">
727 <a name="steens-aa">The <tt>-steens-aa</tt> pass</a>
728</div>
729
730<div class="doc_text">
731
732<p>The <tt>-steens-aa</tt> pass implements a variation on the well-known
733"Steensgaard's algorithm" for interprocedural alias analysis. Steensgaard's
734algorithm is a unification-based, flow-insensitive, context-insensitive, and
735field-insensitive alias analysis that is also very scalable (effectively linear
736time).</p>
737
738<p>The LLVM <tt>-steens-aa</tt> pass implements a "speculatively
739field-<b>sensitive</b>" version of Steensgaard's algorithm using the Data
740Structure Analysis framework. This gives it substantially more precision than
741the standard algorithm while maintaining excellent analysis scalability.</p>
742
743<p>Note that <tt>-steens-aa</tt> is available in the optional "poolalloc"
744module, it is not part of the LLVM core.</p>
745
746</div>
747
748<!-- _______________________________________________________________________ -->
749<div class="doc_subsubsection">
750 <a name="ds-aa">The <tt>-ds-aa</tt> pass</a>
751</div>
752
753<div class="doc_text">
754
755<p>The <tt>-ds-aa</tt> pass implements the full Data Structure Analysis
756algorithm. Data Structure Analysis is a modular unification-based,
757flow-insensitive, context-<b>sensitive</b>, and speculatively
758field-<b>sensitive</b> alias analysis that is also quite scalable, usually at
759O(n*log(n)).</p>
760
761<p>This algorithm is capable of responding to a full variety of alias analysis
762queries, and can provide context-sensitive mod/ref information as well. The
763only major facility not implemented so far is support for must-alias
764information.</p>
765
766<p>Note that <tt>-ds-aa</tt> is available in the optional "poolalloc"
767module, it is not part of the LLVM core.</p>
768
769</div>
770
771
772<!-- ======================================================================= -->
773<div class="doc_subsection">
774 <a name="aliasanalysis-xforms">Alias analysis driven transformations</a>
775</div>
776
777<div class="doc_text">
778LLVM includes several alias-analysis driven transformations which can be used
779with any of the implementations above.
780</div>
781
782<!-- _______________________________________________________________________ -->
783<div class="doc_subsubsection">
784 <a name="adce">The <tt>-adce</tt> pass</a>
785</div>
786
787<div class="doc_text">
788
789<p>The <tt>-adce</tt> pass, which implements Aggressive Dead Code Elimination
790uses the <tt>AliasAnalysis</tt> interface to delete calls to functions that do
791not have side-effects and are not used.</p>
792
793</div>
794
795
796<!-- _______________________________________________________________________ -->
797<div class="doc_subsubsection">
798 <a name="licm">The <tt>-licm</tt> pass</a>
799</div>
800
801<div class="doc_text">
802
803<p>The <tt>-licm</tt> pass implements various Loop Invariant Code Motion related
804transformations. It uses the <tt>AliasAnalysis</tt> interface for several
805different transformations:</p>
806
807<ul>
808<li>It uses mod/ref information to hoist or sink load instructions out of loops
809if there are no instructions in the loop that modifies the memory loaded.</li>
810
811<li>It uses mod/ref information to hoist function calls out of loops that do not
812write to memory and are loop-invariant.</li>
813
814<li>If uses alias information to promote memory objects that are loaded and
815stored to in loops to live in a register instead. It can do this if there are
816no may aliases to the loaded/stored memory location.</li>
817</ul>
818
819</div>
820
821<!-- _______________________________________________________________________ -->
822<div class="doc_subsubsection">
823 <a name="argpromotion">The <tt>-argpromotion</tt> pass</a>
824</div>
825
826<div class="doc_text">
827<p>
828The <tt>-argpromotion</tt> pass promotes by-reference arguments to be passed in
829by-value instead. In particular, if pointer arguments are only loaded from it
830passes in the value loaded instead of the address to the function. This pass
831uses alias information to make sure that the value loaded from the argument
832pointer is not modified between the entry of the function and any load of the
833pointer.</p>
834</div>
835
836<!-- _______________________________________________________________________ -->
837<div class="doc_subsubsection">
Chris Lattner72da5762009-04-25 21:11:37 +0000838 <a name="gvn">The <tt>-gvn</tt>, <tt>-memcpyopt</tt>, and <tt>-dse</tt>
839 passes</a>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840</div>
841
842<div class="doc_text">
843
Chris Lattner72da5762009-04-25 21:11:37 +0000844<p>These passes use AliasAnalysis information to reason about loads and stores.
845</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846
847</div>
848
849<!-- ======================================================================= -->
850<div class="doc_subsection">
851 <a name="aliasanalysis-debug">Clients for debugging and evaluation of
852 implementations</a>
853</div>
854
855<div class="doc_text">
856
857<p>These passes are useful for evaluating the various alias analysis
858implementations. You can use them with commands like '<tt>opt -anders-aa -ds-aa
859-aa-eval foo.bc -disable-output -stats</tt>'.</p>
860
861</div>
862
863<!-- _______________________________________________________________________ -->
864<div class="doc_subsubsection">
865 <a name="print-alias-sets">The <tt>-print-alias-sets</tt> pass</a>
866</div>
867
868<div class="doc_text">
869
870<p>The <tt>-print-alias-sets</tt> pass is exposed as part of the
871<tt>opt</tt> tool to print out the Alias Sets formed by the <a
872href="#ast"><tt>AliasSetTracker</tt></a> class. This is useful if you're using
873the <tt>AliasSetTracker</tt> class. To use it, use something like:</p>
874
875<div class="doc_code">
876<pre>
877% opt -ds-aa -print-alias-sets -disable-output
878</pre>
879</div>
880
881</div>
882
883
884<!-- _______________________________________________________________________ -->
885<div class="doc_subsubsection">
886 <a name="count-aa">The <tt>-count-aa</tt> pass</a>
887</div>
888
889<div class="doc_text">
890
891<p>The <tt>-count-aa</tt> pass is useful to see how many queries a particular
892pass is making and what responses are returned by the alias analysis. As an
893example,</p>
894
895<div class="doc_code">
896<pre>
897% opt -basicaa -count-aa -ds-aa -count-aa -licm
898</pre>
899</div>
900
901<p>will print out how many queries (and what responses are returned) by the
902<tt>-licm</tt> pass (of the <tt>-ds-aa</tt> pass) and how many queries are made
903of the <tt>-basicaa</tt> pass by the <tt>-ds-aa</tt> pass. This can be useful
904when debugging a transformation or an alias analysis implementation.</p>
905
906</div>
907
908<!-- _______________________________________________________________________ -->
909<div class="doc_subsubsection">
910 <a name="aa-eval">The <tt>-aa-eval</tt> pass</a>
911</div>
912
913<div class="doc_text">
914
915<p>The <tt>-aa-eval</tt> pass simply iterates through all pairs of pointers in a
916function and asks an alias analysis whether or not the pointers alias. This
917gives an indication of the precision of the alias analysis. Statistics are
918printed indicating the percent of no/may/must aliases found (a more precise
919algorithm will have a lower number of may aliases).</p>
920
921</div>
922
923<!-- *********************************************************************** -->
Owen Anderson05e080f2007-10-02 00:43:25 +0000924<div class="doc_section">
925 <a name="memdep">Memory Dependence Analysis</a>
926</div>
927<!-- *********************************************************************** -->
928
929<div class="doc_text">
930
931<p>If you're just looking to be a client of alias analysis information, consider
932using the Memory Dependence Analysis interface instead. MemDep is a lazy,
933caching layer on top of alias analysis that is able to answer the question of
934what preceding memory operations a given instruction depends on, either at an
935intra- or inter-block level. Because of its laziness and caching
936policy, using MemDep can be a significant performance win over accessing alias
937analysis directly.</p>
938
939</div>
940
941<!-- *********************************************************************** -->
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942
943<hr>
944<address>
945 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman947321d2008-12-11 17:34:48 +0000946 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman947321d2008-12-11 17:34:48 +0000948 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949
950 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
951 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
952 Last modified: $Date$
953</address>
954
955</body>
956</html>