aboutsummaryrefslogtreecommitdiff
path: root/docs/AliasAnalysis.html
blob: dfc6f7b3a52a9aeb1adca7dfc2612a22881a4879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Alias Analysis Infrastructure in LLVM</title>
  <link rel="stylesheet" href="llvm.css" type="text/css">
</head>
<body>

<div class="doc_title">
  Alias Analysis Infrastructure in LLVM
</div>

<ol>
  <li><a href="#introduction">Introduction</a></li>

  <li><a href="#overview">AliasAnalysis Overview</a>
    <ul>
    <li><a href="#pointers">Representation of Pointers</a></li>
    <li><a href="#MustMayNo">Must, May, and No Alias Responses</a></li>
    <li><a href="#ModRefInfo">The <tt>getModRefInfo</tt> methods</a></li>
    </ul></li>

  <li><a href="#writingnew">Writing a new AliasAnalysis Implementation</a>
    <ul>
    <li><a href="#passsubclasses">Different Pass styles</a></li>
    <li><a href="#requiredcalls">Required initialization calls</a></li>
    <li><a href="#interfaces">Interfaces which may be specified</a></li>
    <li><a href="#chaining">The AliasAnalysis chaining behavior</a></li>
    <li><a href="#implefficiency">Efficiency Issues</a></li>
    </ul></li>

  <li><a href="#using">Using AliasAnalysis results</a>
    <ul>
    <li><a href="#loadvn">Using the <tt>-load-vn</tt> Pass</a></li>
    <li><a href="#ast">Using the <tt>AliasSetTracker</tt> class</a></li>
    <li><a href="#direct">Using the AliasAnalysis interface directly</a></li>
    </ul></li>

  <li><a href="#tools">Helpful alias analysis related tools</a>
    <ul>
    <li><a href="#no-aa">The <tt>-no-aa</tt> pass</a></li>
    <li><a href="#print-alias-sets">The <tt>-print-alias-sets</tt> pass</a></li>
    <li><a href="#count-aa">The <tt>-count-aa</tt> pass</a></li>
    <li><a href="#aa-eval">The <tt>-aa-eval</tt> pass</a></li>
    </ul></li>
</ol>

<div class="doc_text">    
  <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></b></p>
</div>

<!-- *********************************************************************** -->
<div class="doc_section">
  <a name="introduction">Introduction</a>
</div>
<!-- *********************************************************************** -->

<div class="doc_text">

<p>Alias Analysis (or Pointer Analysis) is a technique which attempts to
determine whether or not two pointers ever can point to the same object in
memory.  Traditionally, Alias Analyses respond to a query with either a <a
href="#MustNoMay">Must, May, or No</a> alias response, indicating that two
pointers do point to the same object, might point to the same object, or are
known not to point to the same object.</p>

<p>The <a href="/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a> class is the
centerpiece of the LLVM Alias Analysis related infrastructure.  This class is
the common interface between clients of alias analysis information and the
implementations providing it.  In addition to simple alias analysis information,
this class exposes Mod/Ref information from those implementations which can
provide it, allowing for powerful analyses and transformations to work well
together.</p>

<p>This document contains information necessary to successfully implement this
interface, use it, and to test both sides.  It also explains some of the finer
points about what exactly results mean.  If you feel that something is unclear
or should be added, please <a href="mailto:sabre@nondot.org">let me
know</a>.</p>

</div>

<!-- *********************************************************************** -->
<div class="doc_section">
  <a name="overview">AliasAnalysis Overview</a>
</div>
<!-- *********************************************************************** -->

<div class="doc_text">

<p>The <a href="/doxygen/classllvm_1_1AliasAnalysis.html">AliasAnalysis</a> class
defines the interface that Alias Analysis implementations should support.  This
class exports two important enums: <tt>AliasResult</tt> and
<tt>ModRefResult</tt> which represent the result of an alias query or a mod/ref
query, respectively.</p>

<p>The AliasAnalysis interface exposes information about memory, represented in
several different ways.  In particular, memory objects are represented as a
starting address and size, and function calls are represented as the actual
<tt>call</tt> or <tt>invoke</tt> instructions that performs the call.  The
AliasAnalysis interface also exposes some helper methods which allow you to get
mod/ref information for arbitrary instructions.</p>

</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="pointers">Representation of Pointers</a>
</div>

<div class="doc_text">

<p>Most importantly, the AliasAnalysis class provides several methods which are
used to query whether or not pointers alias, whether function calls can modify
or read memory, etc.</p>

<p>Representing memory objects as a starting address and a size is critically
important for precise Alias Analyses.  For example, consider this (silly) C
code:</p>

<pre>
  int i;
  char C[2];
  char A[10]; 
  /* ... */
  for (i = 0; i != 10; ++i) {
    C[0] = A[i];          /* One byte store */
    C[1] = A[9-i];        /* One byte store */
  }
</pre>

<p>In this case, the <tt>basicaa</tt> pass will disambiguate the stores to
<tt>C[0]</tt> and <tt>C[1]</tt> because they are accesses to two distinct
locations one byte apart, and the accesses are each one byte.  In this case, the
LICM pass can use store motion to remove the stores from the loop.  In
constrast, the following code:</p>

<pre>
  int i;
  char C[2];
  char A[10]; 
  /* ... */
  for (i = 0; i != 10; ++i) {
    ((short*)C)[0] = A[i];  /* Two byte store! */
    C[1] = A[9-i];          /* One byte store */
  }
</pre>

<p>In this case, the two stores to C do alias each other, because the access to
the <tt>&amp;C[0]</tt> element is a two byte access.  If size information wasn't
available in the query, even the first case would have to conservatively assume
that the accesses alias.</p>

</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="MustMayNo">Must, May, and No Alias Responses</a>
</div>

<div class="doc_text">

<p>An Alias Analysis implementation can return one of three responses:
MustAlias, MayAlias, and NoAlias.  The No and May alias results are obvious: if
the two pointers may never equal each other, return NoAlias, if they might,
return MayAlias.</p>

<p>The Must Alias response is trickier though.  In LLVM, the Must Alias response
may only be returned if the two memory objects are guaranteed to always start at
exactly the same location.  If two memory objects overlap, but do not start at
the same location, MayAlias must be returned.</p>

</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="ModRefInfo">The <tt>getModRefInfo</tt> methods</a>
</div>

<div class="doc_text">

<p>The <tt>getModRefInfo</tt> methods return information about whether the
execution of an instruction can read or modify a memory location.  Mod/Ref
information is always conservative: if an action <b>may</b> read a location, Ref
is returned.</p>

</div>

<!-- *********************************************************************** -->
<div class="doc_section">
  <a name="writingnew">Writing a new AliasAnalysis Implementation</a>
</div>
<!-- *********************************************************************** -->

<div class="doc_text">

<p>Writing a new alias analysis implementation for LLVM is quite
straight-forward.  There are already several implementations that you can use
for examples, and the following information should help fill in any details.
For a minimal example, take a look at the <a
href="/doxygen/structllvm_1_1NoAA.html"><tt>no-aa</tt></a> implementation.</p>

</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="passsubclasses">Different Pass styles</a>
</div>

<div class="doc_text">

<p>The first step to determining what type of <a
href="WritingAnLLVMPass.html">LLVM pass</a> you need to use for your Alias
Analysis.  As is the case with most other analyses and transformations, the
answer should be fairly obvious from what type of problem you are trying to
solve:</p>

<ol>
  <li>If you require interprocedural analysis, it should be a
      <tt>Pass</tt>.</li>
  <li>If you are a global analysis, subclass <tt>FunctionPass</tt>.</li>
  <li>If you are a local pass, subclass <tt>BasicBlockPass</tt>.</li>
  <li>If you don't need to look at the program at all, subclass 
      <tt>ImmutablePass</tt>.</li>
</ol>

<p>In addition to the pass that you subclass, you should also inherit from the
<tt>AliasAnalysis</tt> interface, of course, and use the
<tt>RegisterAnalysisGroup</tt> template to register as an implementation of
<tt>AliasAnalysis</tt>.</p>

</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="requiredcalls">Required initialization calls</a>
</div>

<div class="doc_text">

<p>Your subclass of AliasAnalysis is required to invoke two methods on the
AliasAnalysis base class: <tt>getAnalysisUsage</tt> and
<tt>InitializeAliasAnalysis</tt>.  In particular, your implementation of
<tt>getAnalysisUsage</tt> should explicitly call into the
<tt>AliasAnalysis::getAnalysisUsage</tt> method in addition to doing any
declaring any pass dependencies your pass has.  Thus you should have something
like this:</p>

<pre>
    void getAnalysisUsage(AnalysisUsage &amp;AU) const {
      AliasAnalysis::getAnalysisUsage(AU);
      <i>// declare your dependencies here.</i>
    }
</pre>

<p>Additionally, your must invoke the <tt>InitializeAliasAnalysis</tt> method
from your analysis run method (<tt>run</tt> for a <tt>Pass</tt>,
<tt>runOnFunction</tt> for a <tt>FunctionPass</tt>, <tt>runOnBasicBlock</tt> for
a <tt>BasicBlockPass</tt>, or <tt>InitializeAliasAnalysis</tt> for an
<tt>ImmutablePass</tt>).  For example (as part of a <tt>Pass</tt>):</p>

<pre>
    bool run(Module &amp;M) {
      InitializeAliasAnalysis(this);
      <i>// Perform analysis here...</i>
      return false;
    }
</pre>

</div>

<!-- ======================================================================= -->
<div class="doc_subsection">
  <a name="interfaces">Interfaces which may be specified</a>
</div>

<div class="doc_text">

<p>All of the <a href=&qu