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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
==================================
LLVM Alias Analysis Infrastructure
==================================
.. contents::
:local:
Introduction
============
Alias Analysis (aka Pointer Analysis) is a class of techniques which attempt to
determine whether or not two pointers ever can point to the same object in
memory. There are many different algorithms for alias analysis and many
different ways of classifying them: flow-sensitive vs. flow-insensitive,
context-sensitive vs. context-insensitive, field-sensitive
vs. field-insensitive, unification-based vs. subset-based, etc. Traditionally,
alias analyses respond to a query with a `Must, May, or No`_ alias response,
indicating that two pointers always point to the same object, might point to the
same object, or are known to never point to the same object.
The LLVM `AliasAnalysis
<http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`__ class is the
primary interface used by clients and implementations of alias analyses in the
LLVM system. This class is the common interface between clients of alias
analysis information and the implementations providing it, and is designed to
support a wide range of implementations and clients (but currently all clients
are assumed to be flow-insensitive). 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.
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 `let me know <mailto:sabre@nondot.org>`_.
``AliasAnalysis`` Class Overview
================================
The `AliasAnalysis <http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`__
class defines the interface that the various alias analysis implementations
should support. This class exports two important enums: ``AliasResult`` and
``ModRefResult`` which represent the result of an alias query or a mod/ref
query, respectively.
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
``call`` or ``invoke`` instructions that performs the call. The
``AliasAnalysis`` interface also exposes some helper methods which allow you to
get mod/ref information for arbitrary instructions.
All ``AliasAnalysis`` interfaces require that in queries involving multiple
values, values which are not `constants <LangRef.html#constants>`_ are all
defined within the same function.
Representation of Pointers
--------------------------
Most importantly, the ``AliasAnalysis`` class provides several methods which are
used to query whether or not two memory objects alias, whether function calls
can modify or read a memory object, etc. For all of these queries, memory
objects are represented as a pair of their starting address (a symbolic LLVM
``Value*``) and a static size.
Representing memory objects as a starting address and a size is critically
important for correct Alias Analyses. For example, consider this (silly, but
possible) C code:
.. code-block:: c++
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 */
}
In this case, the ``basicaa`` pass will disambiguate the stores to ``C[0]`` and
``C[1]`` because they are accesses to two distinct locations one byte apart, and
the accesses are each one byte. In this case, the Loop Invariant Code Motion
(LICM) pass can use store motion to remove the stores from the loop. In
constrast, the following code:
.. code-block:: c++
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 */
}
In this case, the two stores to C do alias each other, because the access to the
``&C[0]`` 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.
.. _alias:
The ``alias`` method
--------------------
The ``alias`` method is the primary interface used to determine whether or not
two memory objects alias each other. It takes two memory objects as input and
returns MustAlias, PartialAlias, MayAlias, or NoAlias as appropriate.
Like all ``AliasAnalysis`` interfaces, the ``alias`` method requires that either
the two pointer values be defined within the same function, or at least one of
the values is a `constant <LangRef.html#constants>`_.
.. _Must, May, or No:
Must, May, and No Alias Responses
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The ``NoAlias`` response may be used when there is never an immediate dependence
between any memory reference *based* on one pointer and any memory reference
*based* the other. The most obvious example is when the two pointers point to
non-overlapping memory ranges. Another is when the two pointers are only ever
used for reading memory. Another is when the memory is freed and reallocated
between accesses through one pointer and accesses through the other --- in this
case, there is a dependence, but it's mediated by the free and reallocation.
As an exception to this is with the `noalias <LangRef.html#noalias>`_ keyword;
the "irrelevant" dependencies are ignored.
The ``MayAlias`` response is used whenever the two pointers might refer to the
same object.
The ``PartialAlias`` response is used when the two memory objects are known to
be overlapping in some way, but do not start at the same address.
The ``MustAlias`` response may only be returned if the two memory objects are
guaranteed to always start at exactly the same location. A ``MustAlias``
response implies that the pointers compare equal.
The ``getModRefInfo`` methods
-----------------------------
The ``getModRefInfo`` 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 instruction **might** read or write a location,
``ModRef`` is returned.
The ``AliasAnalysis`` class also provides a ``getModRefInfo`` method for testing
dependencies between function calls. This method takes two call sites (``CS1``
& ``CS2``), returns ``NoModRef`` if neither call writes to memory read or
written by the other, ``Ref`` if ``CS1`` reads memory written by ``CS2``,
``Mod`` if ``CS1`` writes to memory read or written by ``CS2``, or ``ModRef`` if
``CS1`` might read or write memory written to by ``CS2``. Note that this
relation is not commutative.
Other useful ``AliasAnalysis`` methods
--------------------------------------
Several other tidbits of information are often collected by various alias
analysis implementations and can be put to good use by various clients.
The ``pointsToConstantMemory`` method
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The ``pointsToConstantMemory`` method returns true if and only if the analysis
can prove that the pointer only points to unchanging memory locations
(functions, constant global variables, and the null pointer). This information
can be used to refine mod/ref information: it is impossible for an unchanging
memory location to be modified.
.. _never access memory or only read memory:
The ``doesNotAccessMemory`` and ``onlyReadsMemory`` methods
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
These methods are used to provide very simple mod/ref information for function
calls. The ``doesNotAccessMemory`` method returns true for a function if the
analysis can prove that the function never reads or writes to memory, or if the
function only reads from constant memory. Functions with this property are
side-effect free and only depend on their input arguments, allowing them to be
eliminated if they form common subexpressions or be hoisted out of loops. Many
common functions behave this way (e.g., ``sin`` and ``cos``) but many others do
not (e.g., ``acos``, which modifies the ``errno`` variable).
The ``onlyReadsMemory`` method returns true for a function if analysis can prove
that (at most) the function only reads from non-volatile memory. Functions with
this property are side-effect free, only depending on their input arguments and
the state of memory when they are called. This property allows calls to these
functions to be eliminated and moved around, as long as there is no store
instruction that changes the contents of memory. Note that all functions that
satisfy the ``doesNotAccessMemory`` method also satisfies ``onlyReadsMemory``.
Writing a new ``AliasAnalysis`` Implementation
==============================================
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 examples, take a
look at the `various alias analysis implementations`_ included with LLVM.
Different Pass styles
---------------------
The first step to determining what type of :doc:`LLVM pass <WritingAnLLVMPass>`
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:
#. If you require interprocedural analysis, it should be a ``Pass``.
#. If you are a function-local analysis, subclass ``FunctionPass``.
#. If you don't need to look at the program at all, subclass ``ImmutablePass``.
In addition to the pass that you subclass, you should also inherit from the
``AliasAnalysis`` interface, of course, and use the ``RegisterAnalysisGroup``
template to register as an implementation of ``AliasAnalysis``.
Required initialization calls
-----------------------------
Your subclass of ``AliasAnalysis`` is required to invoke two methods on the
``AliasAnalysis`` base class: ``getAnalysisUsage`` and
``InitializeAliasAnalysis``. In particular, your implementation of
``getAnalysisUsage`` should explicitly call into the
``AliasAnalysis::getAnalysisUsage`` method in addition to doing any declaring
any pass dependencies your pass has. Thus you should have something like this:
.. code-block:: c++
void getAnalysisUsage(AnalysisUsage &AU) const {
AliasAnalysis::getAnalysisUsage(AU);
// declare your dependencies here.
}
Additionally, your must invoke the ``InitializeAliasAnalysis`` method from your
analysis run method (``run`` for a ``Pass``, ``runOnFunction`` for a
``FunctionPass``, or ``InitializePass`` for an ``ImmutablePass``). For example
(as part of a ``Pass``):
.. code-block:: c++
bool run(Module &M) {
InitializeAliasAnalysis(this);
// Perform analysis here...
return false;
}
Interfaces which may be specified
---------------------------------
All of the `AliasAnalysis
<http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`__ virtual methods
default to providing :ref:`chaining <aliasanalysis-chaining>` to another alias
analysis implementation, which ends up returning conservatively correct
information (returning "May" Alias and "Mod/Ref" for alias and mod/ref queries
respectively). Depending on the capabilities of the analysis you are
implementing, you just override the interfaces you can improve.
.. _aliasanalysis-chaining:
``AliasAnalysis`` chaining behavior
-----------------------------------
With only one special exception (the :ref:`-no-aa <aliasanalysis-no-aa>` pass)
every alias analysis pass chains to another alias analysis implementation (for
example, the user can specify "``-basicaa -ds-aa -licm``" to get the maximum
benefit from both alias analyses). The alias analysis class automatically
takes care of most of this for methods that you don't override. For methods
that you do override, in code paths that return a conservative MayAlias or
Mod/Ref result, simply return whatever the superclass computes. For example:
.. code-block:: c++
AliasAnalysis::AliasResult alias(const Value *V1, unsigned V1Size,
const Value *V2, unsigned V2Size) {
if (...)
return NoAlias;
...
// Couldn't determine a must or no-alias result.
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
}
In addition to analysis queries, you must make sure to unconditionally pass LLVM
`update notification`_ methods to the superclass as well if you override them,
which allows all alias analyses in a change to be updated.
.. _update notification:
Updating analysis results for transformations
---------------------------------------------
Alias analysis information is initially computed for a static snapshot of the
program, but clients will use this information to make transformations to the
code. All but the most trivial forms of alias analysis will need to have their
analysis results updated to reflect the changes made by these transformations.
The ``AliasAnalysis`` interface exposes four methods which are used to
communicate program changes from the clients to the analysis implementations.
Various alias analysis implementations should use these methods to ensure that
their internal data structures are kept up-to-date as the program changes (for
example, when an instruction is deleted), and clients of alias analysis must be
sure to call these interfaces appropriately.
The ``deleteValue`` method
^^^^^^^^^^^^^^^^^^^^^^^^^^
The ``deleteValue`` method is called by transformations when they remove an
instruction or any other value from the program (including values that do not
use pointers). Typically alias analyses keep data structures that have entries
for each value in the program. When this method is called, they should remove
any entries for the specified value, if they exist.
The ``copyValue`` method
^^^^^^^^^^^^^^^^^^^^^^^^
The ``copyValue`` method is used when a new value is introduced into the
program. There is no way to introduce a value into the program that did not
exist before (this doesn't make sense for a safe compiler transformation), so
this is the only way to introduce a new value. This method indicates that the
new value has exactly the same properties as the value being copied.
The ``replaceWithNewValue`` method
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This method is a simple helper method that is provided to make clients easier to
use. It is implemented by copying the old analysis information to the new
value, then deleting the old value. This method cannot be overridden by alias
analysis implementations.
The ``addEscapingUse`` method
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The ``addEscapingUse`` method is used when the uses of a pointer value have
changed in ways that may invalidate precomputed analysis information.
Implementations may either use this callback to provide conservative responses
for points whose uses have change since analysis time, or may recompute some or
all of their internal state to continue providing accurate responses.
In general, any new use of a pointer value is considered an escaping use, and
must be reported through this callback, *except* for the uses below:
* A ``bitcast`` or ``getelementptr`` of the pointer
* A ``store`` through the pointer (but not a ``store`` *of* the pointer)
* A ``load`` through the pointer
Efficiency Issues
-----------------
From the LLVM perspective, the only thing you need to do to provide an efficient
alias analysis is to make sure that alias analysis **queries** are serviced
quickly. The actual calculation of the alias analysis results (the "run"
method) is only performed once, but many (perhaps duplicate) queries may be
performed. Because of this, try to move as much computation to the run method
as possible (within reason).
Limitations
-----------
The AliasAnalysis infrastructure has several limitations which make writing a
new ``AliasAnalysis`` implementation difficult.
There is no way to override the default alias analysis. It would be very useful
to be able to do something like "``opt -my-aa -O2``" and have it use ``-my-aa``
for all passes which need AliasAnalysis, but there is currently no support for
that, short of changing the source code and recompiling. Similarly, there is
also no way of setting a chain of analyses as the default.
There is no way for transform passes to declare that they preserve
``AliasAnalysis`` implementations. The ``AliasAnalysis`` interface includes
``deleteValue`` and ``copyValue`` methods which are intended to allow a pass to
keep an AliasAnalysis consistent, however there's no way for a pass to declare
in its ``getAnalysisUsage`` that it does so. Some passes attempt to use
``AU.addPreserved<AliasAnalysis>``, however this doesn't actually have any
effect.
``AliasAnalysisCounter`` (``-count-aa``) and ``AliasDebugger`` (``-debug-aa``)
are implemented as ``ModulePass`` classes, so if your alias analysis uses
``FunctionPass``, it won't be able to use these utilities. If you try to use
them, the pass manager will silently route alias analysis queries directly to
``BasicAliasAnalysis`` instead.
Similarly, the ``opt -p`` option introduces ``ModulePass`` passes between each
pass, which prevents the use of ``FunctionPass`` alias analysis passes.
The ``AliasAnalysis`` API does have functions for notifying implementations when
values are deleted or copied, however these aren't sufficient. There are many
other ways that LLVM IR can be modified which could be relevant to
``AliasAnalysis`` implementations which can not be expressed.
The ``AliasAnalysisDebugger`` utility seems to suggest that ``AliasAnalysis``
implementations can expect that they will be informed of any relevant ``Value``
before it appears in an alias query. However, popular clients such as ``GVN``
don't support this, and are known to trigger errors when run with the
``AliasAnalysisDebugger``.
Due to several of the above limitations, the most obvious use for the
``AliasAnalysisCounter`` utility, collecting stats on all alias queries in a
compilation, doesn't work, even if the ``AliasAnalysis`` implementations don't
use ``FunctionPass``. There's no way to set a default, much less a default
sequence, and there's no way to preserve it.
The ``AliasSetTracker`` class (which is used by ``LICM``) makes a
non-deterministic number of alias queries. This can cause stats collected by
``AliasAnalysisCounter`` to have fluctuations among identical runs, for
example. Another consequence is that debugging techniques involving pausing
execution after a predetermined number of queries can be unreliable.
Many alias queries can be reformulated in terms of other alias queries. When
multiple ``AliasAnalysis`` queries are chained together, it would make sense to
start those queries from the beginning of the chain, with care taken to avoid
infinite looping, however currently an implementation which wants to do this can
only start such queries from itself.
Using alias analysis results
============================
There are several different ways to use alias analysis results. In order of
preference, these are:
Using the ``MemoryDependenceAnalysis`` Pass
-------------------------------------------
The ``memdep`` pass uses alias analysis to provide high-level dependence
information about memory-using instructions. This will tell you which store
feeds into a load, for example. It uses caching and other techniques to be
efficient, and is used by Dead Store Elimination, GVN, and memcpy optimizations.
.. _AliasSetTracker:
Using the ``AliasSetTracker`` class
-----------------------------------
Many transformations need information about alias **sets** that are active in
some scope, rather than information about pairwise aliasing. The
`AliasSetTracker <http://llvm.org/doxygen/classllvm_1_1AliasSetTracker.html>`__
class is used to efficiently build these Alias Sets from the pairwise alias
analysis information provided by the ``AliasAnalysis`` interface.
First you initialize the AliasSetTracker by using the "``add``" methods to add
information about various potentially aliasing instructions in the scope you are
interested in. Once all of the alias sets are completed, your pass should
simply iterate through the constructed alias sets, using the ``AliasSetTracker``
``begin()``/``end()`` methods.
The ``AliasSet``\s formed by the ``AliasSetTracker`` are guaranteed to be
disjoint, calculate mod/ref information and volatility for the set, and keep
track of whether or not all of the pointers in the set are Must aliases. The
AliasSetTracker also makes sure that sets are properly folded due to call
instructions, and can provide a list of pointers in each set.
As an example user of this, the `Loop Invariant Code Motion
<doxygen/structLICM.html>`_ pass uses ``AliasSetTracker``\s to calculate alias
sets for each loop nest. If an ``AliasSet`` in a loop is not modified, then all
load instructions from that set may be hoisted out of the loop. If any alias
sets are stored to **and** are must alias sets, then the stores may be sunk
to outside of the loop, promoting the memory location to a register for the
duration of the loop nest. Both of these transformations only apply if the
pointer argument is loop-invariant.
The AliasSetTracker implementation
^^^^^^
|