1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5 <link rel="stylesheet" href="llvm.css" type="text/css" media="screen" />
6 <title>Alias Analysis Infrastructure in LLVM</title>
11 <div class="doc_title">
12 Alias Analysis Infrastructure in LLVM
16 <li><a href="#introduction">Introduction</a>
18 <li><a href="#overview">AliasAnalysis Overview</a>
20 <li><a href="#pointers">Representation of Pointers</a>
21 <li><a href="#MustMayNo">Must, May, and No Alias Responses</a>
22 <li><a href="#ModRefInfo">The <tt>getModRefInfo</tt> methods</a>
25 <li><a href="#writingnew">Writing a new AliasAnalysis Implementation</a>
27 <li><a href="#passsubclasses">Different Pass styles</a>
28 <li><a href="#requiredcalls">Required initialization calls</a>
29 <li><a href="#interfaces">Interfaces which may be specified</a>
30 <li><a href="#chaining">The AliasAnalysis chaining behavior</a>
31 <li><a href="#implefficiency">Efficiency Issues</a>
34 <li><a href="#using">Using AliasAnalysis results</a>
36 <li><a href="#loadvn">Using the <tt>-load-vn</tt> Pass</a>
37 <li><a href="#ast">Using the <tt>AliasSetTracker</tt> class</a>
38 <li><a href="#direct">Using the AliasAnalysis interface directly</a>
40 <li><a href="#tools">Helpful alias analysis related tools</a>
42 <li><a href="#no-aa">The <tt>-no-aa</tt> pass</a>
43 <li><a href="#print-alias-sets">The <tt>-print-alias-sets</tt> pass</a>
44 <li><a href="#count-aa">The <tt>-count-aa</tt> pass</a>
45 <li><a href="#aa-eval">The <tt>-aa-eval</tt> pass</a>
49 <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b><p>
53 <!-- *********************************************************************** -->
54 <div class="doc_section">
55 <a name="introduction">Introduction</a>
57 <!-- *********************************************************************** -->
59 <div class="doc_text">
61 Alias Analysis (or Pointer Analysis) is a technique which attempts to determine
62 whether or not two pointers ever can point to the same object in memory.
63 Traditionally, Alias Analyses respond to a query with either a <a
64 href="#MustNoMay">Must, May, or No</a> alias response, indicating that two
65 pointers do point to the same object, might point to the same object, or are
66 known not to point to the same object.
69 The <a href="/doxygen/classAliasAnalysis.html">AliasAnalysis</a> class is the
70 centerpiece of the LLVM Alias Analysis related infrastructure. This class is
71 the common interface between clients of alias analysis information and the
72 implementations providing it. In addition to simple alias analysis information,
73 this class exposes Mod/Ref information from those implementations which can
74 provide it, allowing for powerful analyses and transformations to work well
78 This document contains information necessary to successfully implement this
79 interface, use it, and to test both sides. It also explains some of the finer
80 points about what exactly results mean. If you feel that something is unclear
81 or should be added, please <a href="mailto:sabre@nondot.org">let me
86 <!-- *********************************************************************** -->
87 <div class="doc_section">
88 <a name="overview">AliasAnalysis Overview</a>
90 <!-- *********************************************************************** -->
92 <div class="doc_text">
94 The <a href="/doxygen/classAliasAnalysis.html">AliasAnalysis</a> class defines
95 the interface that Alias Analysis implementations should support. This class
96 exports two important enums: <tt>AliasResult</tt> and <tt>ModRefResult</tt>
97 which represent the result of an alias query or a mod/ref query,
101 The AliasAnalysis interface exposes information about memory, represented in
102 several different ways. In particular, memory objects are represented as a
103 starting address and size, and function calls are represented as the actual
104 <tt>call</tt> or <tt>invoke</tt> instructions that performs the call. The
105 AliasAnalysis interface also exposes some helper methods which allow you to get
106 mod/ref information for arbitrary instructions.
110 <!-- ======================================================================= -->
111 <div class="doc_subsection">
112 <a name="pointers">Representation of Pointers</a>
115 <div class="doc_text">
117 Most importantly, the AliasAnalysis class provides several methods which are
118 used to query whether or not pointers alias, whether function calls can modify
122 Representing memory objects as a starting address and a size is critically
123 important for precise Alias Analyses. For example, consider this (silly) C
132 for (i = 0; i != 10; ++i) {
133 C[0] = A[i]; /* One byte store */
134 C[1] = A[9-i]; /* One byte store */
139 In this case, the <tt>basicaa</tt> pass will disambiguate the stores to
140 <tt>C[0]</tt> and <tt>C[1]</tt> because they are accesses to two distinct
141 locations one byte apart, and the accesses are each one byte. In this case, the
142 LICM pass can use store motion to remove the stores from the loop. In
143 constrast, the following code:
151 for (i = 0; i != 10; ++i) {
152 ((short*)C)[0] = A[i]; /* Two byte store! */
153 C[1] = A[9-i]; /* One byte store */
158 In this case, the two stores to C do alias each other, because the access to the
159 <tt>&C[0]</tt> element is a two byte access. If size information wasn't
160 available in the query, even the first case would have to conservatively assume
161 that the accesses alias.
165 <!-- ======================================================================= -->
166 <div class="doc_subsection">
167 <a name="MustMayNo">Must, May, and No Alias Responses</a>
170 <div class="doc_text">
172 An Alias Analysis implementation can return one of three responses: MustAlias,
173 MayAlias, and NoAlias. The No and May alias results are obvious: if the two
174 pointers may never equal each other, return NoAlias, if they might, return
178 The Must Alias response is trickier though. In LLVM, the Must Alias response
179 may only be returned if the two memory objects are guaranteed to always start at
180 exactly the same location. If two memory objects overlap, but do not start at
181 the same location, MayAlias must be returned.
185 <!-- ======================================================================= -->
186 <div class="doc_subsection">
187 <a name="ModRefInfo">The <tt>getModRefInfo</tt> methods</a>
190 <div class="doc_text">
192 The <tt>getModRefInfo</tt> methods return information about whether the
193 execution of an instruction can read or modify a memory location. Mod/Ref
194 information is always conservative: if an action <b>may</b> read a location, Ref
199 <!-- *********************************************************************** -->
200 <div class="doc_section">
201 <a name="writingnew">Writing a new AliasAnalysis Implementation</a>
203 <!-- *********************************************************************** -->
205 <div class="doc_text">
207 Writing a new alias analysis implementation for LLVM is quite straight-forward.
208 There are already several implementations that you can use for examples, and the
209 following information should help fill in any details. For a minimal example,
210 take a look at the <a href="/doxygen/structNoAA.html"><tt>no-aa</tt></a>
215 <!-- ======================================================================= -->
216 <div class="doc_subsection">
217 <a name="passsubclasses">Different Pass styles</a>
220 <div class="doc_text">
222 The first step to determining what type of <a href="WritingAnLLVMPass.html">LLVM
223 pass</a> you need to use for your Alias Analysis. As is the case with most
224 other analyses and transformations, the answer should be fairly obvious from
225 what type of problem you are trying to solve:
229 <li>If you require interprocedural analysis, it should be a
231 <li>If you are a global analysis, subclass <tt>FunctionPass</tt>.</li>
232 <li>If you are a local pass, subclass <tt>BasicBlockPass</tt>.</li>
233 <li>If you don't need to look at the program at all, subclass
234 <tt>ImmutablePass</tt>.</li>
238 In addition to the pass that you subclass, you should also inherit from the
239 <tt>AliasAnalysis</tt> interface, of course, and use the
240 <tt>RegisterAnalysisGroup</tt> template to register as an implementation of
241 <tt>AliasAnalysis</tt>.
245 <!-- ======================================================================= -->
246 <div class="doc_subsection">
247 <a name="requiredcalls">Required initialization calls</a>
250 <div class="doc_text">
252 Your subclass of AliasAnalysis is required to invoke two methods on the
253 AliasAnalysis base class: <tt>getAnalysisUsage</tt> and
254 <tt>InitializeAliasAnalysis</tt>. In particular, your implementation of
255 <tt>getAnalysisUsage</tt> should explicitly call into the
256 <tt>AliasAnalysis::getAnalysisUsage</tt> method in addition to doing any
257 declaring any pass dependencies your pass has. Thus you should have something
262 void getAnalysisUsage(AnalysisUsage &AU) const {
263 AliasAnalysis::getAnalysisUsage(AU);
264 <i>// declare your dependencies here.</i>
269 Additionally, your must invoke the <tt>InitializeAliasAnalysis</tt> method from
270 your analysis run method (<tt>run</tt> for a <tt>Pass</tt>,
271 <tt>runOnFunction</tt> for a <tt>FunctionPass</tt>, <tt>runOnBasicBlock</tt> for
272 a <tt>BasicBlockPass</tt>, or <tt>InitializeAliasAnalysis</tt> for an
273 <tt>ImmutablePass</tt>). For example (as part of a <tt>Pass</tt>):
277 bool run(Module &M) {
278 InitializeAliasAnalysis(this);
279 <i>// Perform analysis here...</i>
286 <!-- ======================================================================= -->
287 <div class="doc_subsection">
288 <a name="interfaces">Interfaces which may be specified</a>
291 <div class="doc_text">
293 All of the <a href="/doxygen/classAliasAnalysis.html">AliasAnalysis</a> virtual
294 methods default to providing conservatively correct information (returning "May"
295 Alias and "Mod/Ref" for alias and mod/ref queries respectively). Depending on
296 the capabilities of the analysis you are implementing, you just override the
297 interfaces you can improve.
301 <!-- ======================================================================= -->
302 <div class="doc_subsection">
303 <a name="chaining">The AliasAnalysis chaining behavior</a>
306 <div class="doc_text">
308 With only two special exceptions (the <tt>basicaa</tt> and <a
309 href="#no-aa"><tt>no-aa</tt></a> passes) every alias analysis pass should chain
310 to another alias analysis implementation (for example, you could specify
311 "<tt>-basic-aa -ds-aa -andersens-aa -licm</tt>" to get the maximum benefit from
312 the three alias analyses). To do this, simply "Require" AliasAnalysis in your
313 <tt>getAnalysisUsage</tt> method, and if you need to return a conservative
314 MayAlias or Mod/Ref result, simply chain to a lower analysis.
318 <!-- ======================================================================= -->
319 <div class="doc_subsection">
320 <a name="implefficiency">Efficiency Issues</a>
323 <div class="doc_text">
325 From the LLVM perspective, the only thing you need to do to provide an efficient
326 alias analysis is to make sure that alias analysis <b>queries</b> are serviced
327 quickly. The actual calculation of the alias analysis results (the "run"
328 method) is only performed once, but many (perhaps duplicate) queries may be
329 performed. Because of this, try to move as much computation to the run method
330 as possible (within reason).
334 <!-- *********************************************************************** -->
335 <div class="doc_section">
336 <a name="using">Using AliasAnalysis results</a>
338 <!-- *********************************************************************** -->
340 <div class="doc_text">
342 There are several different ways to use alias analysis results. In order of
343 preference, these are...
347 <!-- ======================================================================= -->
348 <div class="doc_subsection">
349 <a name="loadvn">Using the <tt>-load-vn</tt> Pass</a>
352 <div class="doc_text">
354 The <tt>load-vn</tt> pass uses alias analysis to provide value numbering
355 information for <tt>load</tt> instructions. If your analysis or transformation
356 can be modelled in a form that uses value numbering information, you don't have
357 to do anything special to handle load instructions: just use the
358 <tt>load-vn</tt> pass, which uses alias analysis.
362 <!-- ======================================================================= -->
363 <div class="doc_subsection">
364 <a name="ast">Using the <tt>AliasSetTracker</tt> class</a>
367 <div class="doc_text">
369 Many transformations need information about alias <b>sets</b> that are active in
370 some scope, rather than information about pairwise aliasing. The <tt><a
371 href="/doxygen/classAliasSetTracker.html">AliasSetTracker</a></tt> class is used
372 to efficiently build these Alias Sets from the pairwise alias analysis
373 information provided by the AliasAnalysis interface.
376 First you initialize the AliasSetTracker by use the "<tt>add</tt>" methods to
377 add information about various potentially aliasing instructions in the scope you
378 are interested in. Once all of the alias sets are completed, your pass should
379 simply iterate through the constructed alias sets, using the AliasSetTracker
380 <tt>begin()</tt>/<tt>end()</tt> methods.
383 The <tt>AliasSet</tt>s formed by the <tt>AliasSetTracker</tt> are guaranteed to
384 be disjoint, calculate mod/ref information for the set, and keep track of
385 whether or not all of the pointers in the set are Must aliases. The
386 AliasSetTracker also makes sure that sets are properly folded due to call
387 instructions, and can provide a list of pointers in each set.
390 As an example user of this, the <a href="/doxygen/structLICM.html">Loop
391 Invariant Code Motion</a> pass uses AliasSetTrackers to build alias information
392 about each loop nest. If an AliasSet in a loop is not modified, then all load
393 instructions from that set may be hoisted out of the loop. If any alias sets
394 are stored <b>and</b> are must alias sets, then the stores may be sunk to
395 outside of the loop. Both of these transformations obviously only apply if the
396 pointer argument is loop-invariant.
400 <!-- ======================================================================= -->
401 <div class="doc_subsection">
402 <a name="direct">Using the AliasAnalysis interface directly</a>
405 <div class="doc_text">
407 As a last resort, your pass could use the AliasAnalysis interface directly to
408 service your pass. If you find the need to do this, please <a
409 href="mailto:sabre@nondot.org">let me know</a> so I can see if something new
410 needs to be added to LLVM.
414 <!-- *********************************************************************** -->
415 <div class="doc_section">
416 <a name="tools">Helpful alias-analysis-related tools</a>
418 <!-- *********************************************************************** -->
420 <div class="doc_text">
422 If you're going to be working with the AliasAnalysis infrastructure, there are
423 several nice tools that may be useful for you and are worth knowing about...
427 <!-- ======================================================================= -->
428 <div class="doc_subsection">
429 <a name="no-aa">The <tt>-no-aa</tt> pass</a>
432 <div class="doc_text">
434 The <tt>-no-aa</tt> analysis is just like what it sounds: an alias analysis that
435 never returns any useful information. This pass can be useful if you think that
436 alias analysis is doing something wrong and are trying to narrow down a problem.
437 If you don't specify an alias analysis, the default will be to use the
438 <tt>basicaa</tt> pass which does quite a bit of disambiguation on its own.
443 <!-- ======================================================================= -->
444 <div class="doc_subsection">
445 <a name="print-alias-sets">The <tt>-print-alias-sets</tt> pass</a>
448 <div class="doc_text">
450 The <tt>-print-alias-sets</tt> pass is exposed as part of the <tt>analyze</tt>
451 tool to print out the Alias Sets formed by the <a
452 href="#ast"><tt>AliasSetTracker</tt></a> class. This is useful if you're using
453 the <tt>AliasSetTracker</tt>.
457 <!-- ======================================================================= -->
458 <div class="doc_subsection">
459 <a name="count-aa">The <tt>-count-aa</tt> pass</a>
462 <div class="doc_text">
464 The <tt>-count-aa</tt> pass is useful to see how many queries a particular pass
465 is making and what kinds of responses are returned by the alias analysis. An
470 $ opt -basicaa -count-aa -ds-aa -count-aa -licm
474 Which will print out how many queries (and what responses are returned) by the
475 <tt>-licm</tt> pass (of the <tt>-ds-aa</tt> pass) and how many queries are made
476 of the <tt>-basicaa</tt> pass by the <tt>-ds-aa</tt> pass. This can be useful
477 when evaluating an alias analysis for precision.
481 <!-- ======================================================================= -->
482 <div class="doc_subsection">
483 <a name="aa-eval">The <tt>-aa-eval</tt> pass</a>
486 <div class="doc_text">
488 The <tt>-aa-eval</tt> pass simply iterates through all pairs of pointers in a
489 function and asks an alias analysis whether or not the pointers alias. This
490 gives an indication of the precision of the alias analysis. Statistics are
495 <!-- *********************************************************************** -->
498 <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
499 <!-- Created: Wed Feb 26 10:40:50 CST 2003 -->
501 Last modified: $Date$