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