Home | History | Annotate | Download | only in docs
      1 ==================================
      2 LLVM Alias Analysis Infrastructure
      3 ==================================
      4 
      5 .. contents::
      6    :local:
      7 
      8 Introduction
      9 ============
     10 
     11 Alias Analysis (aka Pointer Analysis) is a class of techniques which attempt to
     12 determine whether or not two pointers ever can point to the same object in
     13 memory.  There are many different algorithms for alias analysis and many
     14 different ways of classifying them: flow-sensitive vs. flow-insensitive,
     15 context-sensitive vs. context-insensitive, field-sensitive
     16 vs. field-insensitive, unification-based vs. subset-based, etc.  Traditionally,
     17 alias analyses respond to a query with a `Must, May, or No`_ alias response,
     18 indicating that two pointers always point to the same object, might point to the
     19 same object, or are known to never point to the same object.
     20 
     21 The LLVM `AliasAnalysis
     22 <http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`__ class is the
     23 primary interface used by clients and implementations of alias analyses in the
     24 LLVM system.  This class is the common interface between clients of alias
     25 analysis information and the implementations providing it, and is designed to
     26 support a wide range of implementations and clients (but currently all clients
     27 are assumed to be flow-insensitive).  In addition to simple alias analysis
     28 information, this class exposes Mod/Ref information from those implementations
     29 which can provide it, allowing for powerful analyses and transformations to work
     30 well together.
     31 
     32 This document contains information necessary to successfully implement this
     33 interface, use it, and to test both sides.  It also explains some of the finer
     34 points about what exactly results mean.  
     35 
     36 ``AliasAnalysis`` Class Overview
     37 ================================
     38 
     39 The `AliasAnalysis <http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`__
     40 class defines the interface that the various alias analysis implementations
     41 should support.  This class exports two important enums: ``AliasResult`` and
     42 ``ModRefResult`` which represent the result of an alias query or a mod/ref
     43 query, respectively.
     44 
     45 The ``AliasAnalysis`` interface exposes information about memory, represented in
     46 several different ways.  In particular, memory objects are represented as a
     47 starting address and size, and function calls are represented as the actual
     48 ``call`` or ``invoke`` instructions that performs the call.  The
     49 ``AliasAnalysis`` interface also exposes some helper methods which allow you to
     50 get mod/ref information for arbitrary instructions.
     51 
     52 All ``AliasAnalysis`` interfaces require that in queries involving multiple
     53 values, values which are not :ref:`constants <constants>` are all
     54 defined within the same function.
     55 
     56 Representation of Pointers
     57 --------------------------
     58 
     59 Most importantly, the ``AliasAnalysis`` class provides several methods which are
     60 used to query whether or not two memory objects alias, whether function calls
     61 can modify or read a memory object, etc.  For all of these queries, memory
     62 objects are represented as a pair of their starting address (a symbolic LLVM
     63 ``Value*``) and a static size.
     64 
     65 Representing memory objects as a starting address and a size is critically
     66 important for correct Alias Analyses.  For example, consider this (silly, but
     67 possible) C code:
     68 
     69 .. code-block:: c++
     70 
     71   int i;
     72   char C[2];
     73   char A[10]; 
     74   /* ... */
     75   for (i = 0; i != 10; ++i) {
     76     C[0] = A[i];          /* One byte store */
     77     C[1] = A[9-i];        /* One byte store */
     78   }
     79 
     80 In this case, the ``basicaa`` pass will disambiguate the stores to ``C[0]`` and
     81 ``C[1]`` because they are accesses to two distinct locations one byte apart, and
     82 the accesses are each one byte.  In this case, the Loop Invariant Code Motion
     83 (LICM) pass can use store motion to remove the stores from the loop.  In
     84 constrast, the following code:
     85 
     86 .. code-block:: c++
     87 
     88   int i;
     89   char C[2];
     90   char A[10]; 
     91   /* ... */
     92   for (i = 0; i != 10; ++i) {
     93     ((short*)C)[0] = A[i];  /* Two byte store! */
     94     C[1] = A[9-i];          /* One byte store */
     95   }
     96 
     97 In this case, the two stores to C do alias each other, because the access to the
     98 ``&C[0]`` element is a two byte access.  If size information wasn't available in
     99 the query, even the first case would have to conservatively assume that the
    100 accesses alias.
    101 
    102 .. _alias:
    103 
    104 The ``alias`` method
    105 --------------------
    106   
    107 The ``alias`` method is the primary interface used to determine whether or not
    108 two memory objects alias each other.  It takes two memory objects as input and
    109 returns MustAlias, PartialAlias, MayAlias, or NoAlias as appropriate.
    110 
    111 Like all ``AliasAnalysis`` interfaces, the ``alias`` method requires that either
    112 the two pointer values be defined within the same function, or at least one of
    113 the values is a :ref:`constant <constants>`.
    114 
    115 .. _Must, May, or No:
    116 
    117 Must, May, and No Alias Responses
    118 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    119 
    120 The ``NoAlias`` response may be used when there is never an immediate dependence
    121 between any memory reference *based* on one pointer and any memory reference
    122 *based* the other. The most obvious example is when the two pointers point to
    123 non-overlapping memory ranges. Another is when the two pointers are only ever
    124 used for reading memory. Another is when the memory is freed and reallocated
    125 between accesses through one pointer and accesses through the other --- in this
    126 case, there is a dependence, but it's mediated by the free and reallocation.
    127 
    128 As an exception to this is with the :ref:`noalias <noalias>` keyword;
    129 the "irrelevant" dependencies are ignored.
    130 
    131 The ``MayAlias`` response is used whenever the two pointers might refer to the
    132 same object.
    133 
    134 The ``PartialAlias`` response is used when the two memory objects are known to
    135 be overlapping in some way, but do not start at the same address.
    136 
    137 The ``MustAlias`` response may only be returned if the two memory objects are
    138 guaranteed to always start at exactly the same location. A ``MustAlias``
    139 response implies that the pointers compare equal.
    140 
    141 The ``getModRefInfo`` methods
    142 -----------------------------
    143 
    144 The ``getModRefInfo`` methods return information about whether the execution of
    145 an instruction can read or modify a memory location.  Mod/Ref information is
    146 always conservative: if an instruction **might** read or write a location,
    147 ``ModRef`` is returned.
    148 
    149 The ``AliasAnalysis`` class also provides a ``getModRefInfo`` method for testing
    150 dependencies between function calls.  This method takes two call sites (``CS1``
    151 & ``CS2``), returns ``NoModRef`` if neither call writes to memory read or
    152 written by the other, ``Ref`` if ``CS1`` reads memory written by ``CS2``,
    153 ``Mod`` if ``CS1`` writes to memory read or written by ``CS2``, or ``ModRef`` if
    154 ``CS1`` might read or write memory written to by ``CS2``.  Note that this
    155 relation is not commutative.
    156 
    157 Other useful ``AliasAnalysis`` methods
    158 --------------------------------------
    159 
    160 Several other tidbits of information are often collected by various alias
    161 analysis implementations and can be put to good use by various clients.
    162 
    163 The ``pointsToConstantMemory`` method
    164 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    165 
    166 The ``pointsToConstantMemory`` method returns true if and only if the analysis
    167 can prove that the pointer only points to unchanging memory locations
    168 (functions, constant global variables, and the null pointer).  This information
    169 can be used to refine mod/ref information: it is impossible for an unchanging
    170 memory location to be modified.
    171 
    172 .. _never access memory or only read memory:
    173 
    174 The ``doesNotAccessMemory`` and  ``onlyReadsMemory`` methods
    175 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    176 
    177 These methods are used to provide very simple mod/ref information for function
    178 calls.  The ``doesNotAccessMemory`` method returns true for a function if the
    179 analysis can prove that the function never reads or writes to memory, or if the
    180 function only reads from constant memory.  Functions with this property are
    181 side-effect free and only depend on their input arguments, allowing them to be
    182 eliminated if they form common subexpressions or be hoisted out of loops.  Many
    183 common functions behave this way (e.g., ``sin`` and ``cos``) but many others do
    184 not (e.g., ``acos``, which modifies the ``errno`` variable).
    185 
    186 The ``onlyReadsMemory`` method returns true for a function if analysis can prove
    187 that (at most) the function only reads from non-volatile memory.  Functions with
    188 this property are side-effect free, only depending on their input arguments and
    189 the state of memory when they are called.  This property allows calls to these
    190 functions to be eliminated and moved around, as long as there is no store
    191 instruction that changes the contents of memory.  Note that all functions that
    192 satisfy the ``doesNotAccessMemory`` method also satisfy ``onlyReadsMemory``.
    193 
    194 Writing a new ``AliasAnalysis`` Implementation
    195 ==============================================
    196 
    197 Writing a new alias analysis implementation for LLVM is quite straight-forward.
    198 There are already several implementations that you can use for examples, and the
    199 following information should help fill in any details.  For a examples, take a
    200 look at the `various alias analysis implementations`_ included with LLVM.
    201 
    202 Different Pass styles
    203 ---------------------
    204 
    205 The first step to determining what type of :doc:`LLVM pass <WritingAnLLVMPass>`
    206 you need to use for your Alias Analysis.  As is the case with most other
    207 analyses and transformations, the answer should be fairly obvious from what type
    208 of problem you are trying to solve:
    209 
    210 #. If you require interprocedural analysis, it should be a ``Pass``.
    211 #. If you are a function-local analysis, subclass ``FunctionPass``.
    212 #. If you don't need to look at the program at all, subclass ``ImmutablePass``.
    213 
    214 In addition to the pass that you subclass, you should also inherit from the
    215 ``AliasAnalysis`` interface, of course, and use the ``RegisterAnalysisGroup``
    216 template to register as an implementation of ``AliasAnalysis``.
    217 
    218 Required initialization calls
    219 -----------------------------
    220 
    221 Your subclass of ``AliasAnalysis`` is required to invoke two methods on the
    222 ``AliasAnalysis`` base class: ``getAnalysisUsage`` and
    223 ``InitializeAliasAnalysis``.  In particular, your implementation of
    224 ``getAnalysisUsage`` should explicitly call into the
    225 ``AliasAnalysis::getAnalysisUsage`` method in addition to doing any declaring
    226 any pass dependencies your pass has.  Thus you should have something like this:
    227 
    228 .. code-block:: c++
    229 
    230   void getAnalysisUsage(AnalysisUsage &AU) const {
    231     AliasAnalysis::getAnalysisUsage(AU);
    232     // declare your dependencies here.
    233   }
    234 
    235 Additionally, your must invoke the ``InitializeAliasAnalysis`` method from your
    236 analysis run method (``run`` for a ``Pass``, ``runOnFunction`` for a
    237 ``FunctionPass``, or ``InitializePass`` for an ``ImmutablePass``).  For example
    238 (as part of a ``Pass``):
    239 
    240 .. code-block:: c++
    241 
    242   bool run(Module &M) {
    243     InitializeAliasAnalysis(this);
    244     // Perform analysis here...
    245     return false;
    246   }
    247 
    248 Required methods to override
    249 ----------------------------
    250 
    251 You must override the ``getAdjustedAnalysisPointer`` method on all subclasses
    252 of ``AliasAnalysis``. An example implementation of this method would look like:
    253 
    254 .. code-block:: c++
    255 
    256   void *getAdjustedAnalysisPointer(const void* ID) override {
    257     if (ID == &AliasAnalysis::ID)
    258       return (AliasAnalysis*)this;
    259     return this;
    260   }
    261 
    262 Interfaces which may be specified
    263 ---------------------------------
    264 
    265 All of the `AliasAnalysis
    266 <http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`__ virtual methods
    267 default to providing :ref:`chaining <aliasanalysis-chaining>` to another alias
    268 analysis implementation, which ends up returning conservatively correct
    269 information (returning "May" Alias and "Mod/Ref" for alias and mod/ref queries
    270 respectively).  Depending on the capabilities of the analysis you are
    271 implementing, you just override the interfaces you can improve.
    272 
    273 .. _aliasanalysis-chaining:
    274 
    275 ``AliasAnalysis`` chaining behavior
    276 -----------------------------------
    277 
    278 With only one special exception (the :ref:`-no-aa <aliasanalysis-no-aa>` pass)
    279 every alias analysis pass chains to another alias analysis implementation (for
    280 example, the user can specify "``-basicaa -ds-aa -licm``" to get the maximum
    281 benefit from both alias analyses).  The alias analysis class automatically
    282 takes care of most of this for methods that you don't override.  For methods
    283 that you do override, in code paths that return a conservative MayAlias or
    284 Mod/Ref result, simply return whatever the superclass computes.  For example:
    285 
    286 .. code-block:: c++
    287 
    288   AliasResult alias(const Value *V1, unsigned V1Size,
    289                     const Value *V2, unsigned V2Size) {
    290     if (...)
    291       return NoAlias;
    292     ...
    293 
    294     // Couldn't determine a must or no-alias result.
    295     return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
    296   }
    297 
    298 In addition to analysis queries, you must make sure to unconditionally pass LLVM
    299 `update notification`_ methods to the superclass as well if you override them,
    300 which allows all alias analyses in a change to be updated.
    301 
    302 .. _update notification:
    303 
    304 Updating analysis results for transformations
    305 ---------------------------------------------
    306 
    307 Alias analysis information is initially computed for a static snapshot of the
    308 program, but clients will use this information to make transformations to the
    309 code.  All but the most trivial forms of alias analysis will need to have their
    310 analysis results updated to reflect the changes made by these transformations.
    311 
    312 The ``AliasAnalysis`` interface exposes four methods which are used to
    313 communicate program changes from the clients to the analysis implementations.
    314 Various alias analysis implementations should use these methods to ensure that
    315 their internal data structures are kept up-to-date as the program changes (for
    316 example, when an instruction is deleted), and clients of alias analysis must be
    317 sure to call these interfaces appropriately.
    318 
    319 The ``deleteValue`` method
    320 ^^^^^^^^^^^^^^^^^^^^^^^^^^
    321 
    322 The ``deleteValue`` method is called by transformations when they remove an
    323 instruction or any other value from the program (including values that do not
    324 use pointers).  Typically alias analyses keep data structures that have entries
    325 for each value in the program.  When this method is called, they should remove
    326 any entries for the specified value, if they exist.
    327 
    328 The ``copyValue`` method
    329 ^^^^^^^^^^^^^^^^^^^^^^^^
    330 
    331 The ``copyValue`` method is used when a new value is introduced into the
    332 program.  There is no way to introduce a value into the program that did not
    333 exist before (this doesn't make sense for a safe compiler transformation), so
    334 this is the only way to introduce a new value.  This method indicates that the
    335 new value has exactly the same properties as the value being copied.
    336 
    337 The ``replaceWithNewValue`` method
    338 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    339 
    340 This method is a simple helper method that is provided to make clients easier to
    341 use.  It is implemented by copying the old analysis information to the new
    342 value, then deleting the old value.  This method cannot be overridden by alias
    343 analysis implementations.
    344 
    345 The ``addEscapingUse`` method
    346 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    347 
    348 The ``addEscapingUse`` method is used when the uses of a pointer value have
    349 changed in ways that may invalidate precomputed analysis information.
    350 Implementations may either use this callback to provide conservative responses
    351 for points whose uses have change since analysis time, or may recompute some or
    352 all of their internal state to continue providing accurate responses.
    353 
    354 In general, any new use of a pointer value is considered an escaping use, and
    355 must be reported through this callback, *except* for the uses below:
    356 
    357 * A ``bitcast`` or ``getelementptr`` of the pointer
    358 * A ``store`` through the pointer (but not a ``store`` *of* the pointer)
    359 * A ``load`` through the pointer
    360 
    361 Efficiency Issues
    362 -----------------
    363 
    364 From the LLVM perspective, the only thing you need to do to provide an efficient
    365 alias analysis is to make sure that alias analysis **queries** are serviced
    366 quickly.  The actual calculation of the alias analysis results (the "run"
    367 method) is only performed once, but many (perhaps duplicate) queries may be
    368 performed.  Because of this, try to move as much computation to the run method
    369 as possible (within reason).
    370 
    371 Limitations
    372 -----------
    373 
    374 The AliasAnalysis infrastructure has several limitations which make writing a
    375 new ``AliasAnalysis`` implementation difficult.
    376 
    377 There is no way to override the default alias analysis. It would be very useful
    378 to be able to do something like "``opt -my-aa -O2``" and have it use ``-my-aa``
    379 for all passes which need AliasAnalysis, but there is currently no support for
    380 that, short of changing the source code and recompiling. Similarly, there is
    381 also no way of setting a chain of analyses as the default.
    382 
    383 There is no way for transform passes to declare that they preserve
    384 ``AliasAnalysis`` implementations. The ``AliasAnalysis`` interface includes
    385 ``deleteValue`` and ``copyValue`` methods which are intended to allow a pass to
    386 keep an AliasAnalysis consistent, however there's no way for a pass to declare
    387 in its ``getAnalysisUsage`` that it does so. Some passes attempt to use
    388 ``AU.addPreserved<AliasAnalysis>``, however this doesn't actually have any
    389 effect.
    390 
    391 ``AliasAnalysisCounter`` (``-count-aa``) are implemented as ``ModulePass``
    392 classes, so if your alias analysis uses ``FunctionPass``, it won't be able to
    393 use these utilities. If you try to use them, the pass manager will silently
    394 route alias analysis queries directly to ``BasicAliasAnalysis`` instead.
    395 
    396 Similarly, the ``opt -p`` option introduces ``ModulePass`` passes between each
    397 pass, which prevents the use of ``FunctionPass`` alias analysis passes.
    398 
    399 The ``AliasAnalysis`` API does have functions for notifying implementations when
    400 values are deleted or copied, however these aren't sufficient. There are many
    401 other ways that LLVM IR can be modified which could be relevant to
    402 ``AliasAnalysis`` implementations which can not be expressed.
    403 
    404 The ``AliasAnalysisDebugger`` utility seems to suggest that ``AliasAnalysis``
    405 implementations can expect that they will be informed of any relevant ``Value``
    406 before it appears in an alias query. However, popular clients such as ``GVN``
    407 don't support this, and are known to trigger errors when run with the
    408 ``AliasAnalysisDebugger``.
    409 
    410 Due to several of the above limitations, the most obvious use for the
    411 ``AliasAnalysisCounter`` utility, collecting stats on all alias queries in a
    412 compilation, doesn't work, even if the ``AliasAnalysis`` implementations don't
    413 use ``FunctionPass``.  There's no way to set a default, much less a default
    414 sequence, and there's no way to preserve it.
    415 
    416 The ``AliasSetTracker`` class (which is used by ``LICM``) makes a
    417 non-deterministic number of alias queries. This can cause stats collected by
    418 ``AliasAnalysisCounter`` to have fluctuations among identical runs, for
    419 example. Another consequence is that debugging techniques involving pausing
    420 execution after a predetermined number of queries can be unreliable.
    421 
    422 Many alias queries can be reformulated in terms of other alias queries. When
    423 multiple ``AliasAnalysis`` queries are chained together, it would make sense to
    424 start those queries from the beginning of the chain, with care taken to avoid
    425 infinite looping, however currently an implementation which wants to do this can
    426 only start such queries from itself.
    427 
    428 Using alias analysis results
    429 ============================
    430 
    431 There are several different ways to use alias analysis results.  In order of
    432 preference, these are:
    433 
    434 Using the ``MemoryDependenceAnalysis`` Pass
    435 -------------------------------------------
    436 
    437 The ``memdep`` pass uses alias analysis to provide high-level dependence
    438 information about memory-using instructions.  This will tell you which store
    439 feeds into a load, for example.  It uses caching and other techniques to be
    440 efficient, and is used by Dead Store Elimination, GVN, and memcpy optimizations.
    441 
    442 .. _AliasSetTracker:
    443 
    444 Using the ``AliasSetTracker`` class
    445 -----------------------------------
    446 
    447 Many transformations need information about alias **sets** that are active in
    448 some scope, rather than information about pairwise aliasing.  The
    449 `AliasSetTracker <http://llvm.org/doxygen/classllvm_1_1AliasSetTracker.html>`__
    450 class is used to efficiently build these Alias Sets from the pairwise alias
    451 analysis information provided by the ``AliasAnalysis`` interface.
    452 
    453 First you initialize the AliasSetTracker by using the "``add``" methods to add
    454 information about various potentially aliasing instructions in the scope you are
    455 interested in.  Once all of the alias sets are completed, your pass should
    456 simply iterate through the constructed alias sets, using the ``AliasSetTracker``
    457 ``begin()``/``end()`` methods.
    458 
    459 The ``AliasSet``\s formed by the ``AliasSetTracker`` are guaranteed to be
    460 disjoint, calculate mod/ref information and volatility for the set, and keep
    461 track of whether or not all of the pointers in the set are Must aliases.  The
    462 AliasSetTracker also makes sure that sets are properly folded due to call
    463 instructions, and can provide a list of pointers in each set.
    464 
    465 As an example user of this, the `Loop Invariant Code Motion
    466 <doxygen/structLICM.html>`_ pass uses ``AliasSetTracker``\s to calculate alias
    467 sets for each loop nest.  If an ``AliasSet`` in a loop is not modified, then all
    468 load instructions from that set may be hoisted out of the loop.  If any alias
    469 sets are stored to **and** are must alias sets, then the stores may be sunk
    470 to outside of the loop, promoting the memory location to a register for the
    471 duration of the loop nest.  Both of these transformations only apply if the
    472 pointer argument is loop-invariant.
    473 
    474 The AliasSetTracker implementation
    475 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    476 
    477 The AliasSetTracker class is implemented to be as efficient as possible.  It
    478 uses the union-find algorithm to efficiently merge AliasSets when a pointer is
    479 inserted into the AliasSetTracker that aliases multiple sets.  The primary data
    480 structure is a hash table mapping pointers to the AliasSet they are in.
    481 
    482 The AliasSetTracker class must maintain a list of all of the LLVM ``Value*``\s
    483 that are in each AliasSet.  Since the hash table already has entries for each
    484 LLVM ``Value*`` of interest, the AliasesSets thread the linked list through
    485 these hash-table nodes to avoid having to allocate memory unnecessarily, and to
    486 make merging alias sets extremely efficient (the linked list merge is constant
    487 time).
    488 
    489 You shouldn't need to understand these details if you are just a client of the
    490 AliasSetTracker, but if you look at the code, hopefully this brief description
    491 will help make sense of why things are designed the way they are.
    492 
    493 Using the ``AliasAnalysis`` interface directly
    494 ----------------------------------------------
    495 
    496 If neither of these utility class are what your pass needs, you should use the
    497 interfaces exposed by the ``AliasAnalysis`` class directly.  Try to use the
    498 higher-level methods when possible (e.g., use mod/ref information instead of the
    499 `alias`_ method directly if possible) to get the best precision and efficiency.
    500 
    501 Existing alias analysis implementations and clients
    502 ===================================================
    503 
    504 If you're going to be working with the LLVM alias analysis infrastructure, you
    505 should know what clients and implementations of alias analysis are available.
    506 In particular, if you are implementing an alias analysis, you should be aware of
    507 the `the clients`_ that are useful for monitoring and evaluating different
    508 implementations.
    509 
    510 .. _various alias analysis implementations:
    511 
    512 Available ``AliasAnalysis`` implementations
    513 -------------------------------------------
    514 
    515 This section lists the various implementations of the ``AliasAnalysis``
    516 interface.  With the exception of the :ref:`-no-aa <aliasanalysis-no-aa>`
    517 implementation, all of these :ref:`chain <aliasanalysis-chaining>` to other
    518 alias analysis implementations.
    519 
    520 .. _aliasanalysis-no-aa:
    521 
    522 The ``-no-aa`` pass
    523 ^^^^^^^^^^^^^^^^^^^
    524 
    525 The ``-no-aa`` pass is just like what it sounds: an alias analysis that never
    526 returns any useful information.  This pass can be useful if you think that alias
    527 analysis is doing something wrong and are trying to narrow down a problem.
    528 
    529 The ``-basicaa`` pass
    530 ^^^^^^^^^^^^^^^^^^^^^
    531 
    532 The ``-basicaa`` pass is an aggressive local analysis that *knows* many
    533 important facts:
    534 
    535 * Distinct globals, stack allocations, and heap allocations can never alias.
    536 * Globals, stack allocations, and heap allocations never alias the null pointer.
    537 * Different fields of a structure do not alias.
    538 * Indexes into arrays with statically differing subscripts cannot alias.
    539 * Many common standard C library functions `never access memory or only read
    540   memory`_.
    541 * Pointers that obviously point to constant globals "``pointToConstantMemory``".
    542 * Function calls can not modify or references stack allocations if they never
    543   escape from the function that allocates them (a common case for automatic
    544   arrays).
    545 
    546 The ``-globalsmodref-aa`` pass
    547 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    548 
    549 This pass implements a simple context-sensitive mod/ref and alias analysis for
    550 internal global variables that don't "have their address taken".  If a global
    551 does not have its address taken, the pass knows that no pointers alias the
    552 global.  This pass also keeps track of functions that it knows never access
    553 memory or never read memory.  This allows certain optimizations (e.g. GVN) to
    554 eliminate call instructions entirely.
    555 
    556 The real power of this pass is that it provides context-sensitive mod/ref
    557 information for call instructions.  This allows the optimizer to know that calls
    558 to a function do not clobber or read the value of the global, allowing loads and
    559 stores to be eliminated.
    560 
    561 .. note::
    562 
    563   This pass is somewhat limited in its scope (only support non-address taken
    564   globals), but is very quick analysis.
    565 
    566 The ``-steens-aa`` pass
    567 ^^^^^^^^^^^^^^^^^^^^^^^
    568 
    569 The ``-steens-aa`` pass implements a variation on the well-known "Steensgaard's
    570 algorithm" for interprocedural alias analysis.  Steensgaard's algorithm is a
    571 unification-based, flow-insensitive, context-insensitive, and field-insensitive
    572 alias analysis that is also very scalable (effectively linear time).
    573 
    574 The LLVM ``-steens-aa`` pass implements a "speculatively field-**sensitive**"
    575 version of Steensgaard's algorithm using the Data Structure Analysis framework.
    576 This gives it substantially more precision than the standard algorithm while
    577 maintaining excellent analysis scalability.
    578 
    579 .. note::
    580 
    581   ``-steens-aa`` is available in the optional "poolalloc" module. It is not part
    582   of the LLVM core.
    583 
    584 The ``-ds-aa`` pass
    585 ^^^^^^^^^^^^^^^^^^^
    586 
    587 The ``-ds-aa`` pass implements the full Data Structure Analysis algorithm.  Data
    588 Structure Analysis is a modular unification-based, flow-insensitive,
    589 context-**sensitive**, and speculatively field-**sensitive** alias
    590 analysis that is also quite scalable, usually at ``O(n * log(n))``.
    591 
    592 This algorithm is capable of responding to a full variety of alias analysis
    593 queries, and can provide context-sensitive mod/ref information as well.  The
    594 only major facility not implemented so far is support for must-alias
    595 information.
    596 
    597 .. note::
    598 
    599   ``-ds-aa`` is available in the optional "poolalloc" module. It is not part of
    600   the LLVM core.
    601 
    602 The ``-scev-aa`` pass
    603 ^^^^^^^^^^^^^^^^^^^^^
    604 
    605 The ``-scev-aa`` pass implements AliasAnalysis queries by translating them into
    606 ScalarEvolution queries. This gives it a more complete understanding of
    607 ``getelementptr`` instructions and loop induction variables than other alias
    608 analyses have.
    609 
    610 Alias analysis driven transformations
    611 -------------------------------------
    612 
    613 LLVM includes several alias-analysis driven transformations which can be used
    614 with any of the implementations above.
    615 
    616 The ``-adce`` pass
    617 ^^^^^^^^^^^^^^^^^^
    618 
    619 The ``-adce`` pass, which implements Aggressive Dead Code Elimination uses the
    620 ``AliasAnalysis`` interface to delete calls to functions that do not have
    621 side-effects and are not used.
    622 
    623 The ``-licm`` pass
    624 ^^^^^^^^^^^^^^^^^^
    625 
    626 The ``-licm`` pass implements various Loop Invariant Code Motion related
    627 transformations.  It uses the ``AliasAnalysis`` interface for several different
    628 transformations:
    629 
    630 * It uses mod/ref information to hoist or sink load instructions out of loops if
    631   there are no instructions in the loop that modifies the memory loaded.
    632 
    633 * It uses mod/ref information to hoist function calls out of loops that do not
    634   write to memory and are loop-invariant.
    635 
    636 * It uses alias information to promote memory objects that are loaded and stored
    637   to in loops to live in a register instead.  It can do this if there are no may
    638   aliases to the loaded/stored memory location.
    639 
    640 The ``-argpromotion`` pass
    641 ^^^^^^^^^^^^^^^^^^^^^^^^^^
    642 
    643 The ``-argpromotion`` pass promotes by-reference arguments to be passed in
    644 by-value instead.  In particular, if pointer arguments are only loaded from it
    645 passes in the value loaded instead of the address to the function.  This pass
    646 uses alias information to make sure that the value loaded from the argument
    647 pointer is not modified between the entry of the function and any load of the
    648 pointer.
    649 
    650 The ``-gvn``, ``-memcpyopt``, and ``-dse`` passes
    651 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    652 
    653 These passes use AliasAnalysis information to reason about loads and stores.
    654 
    655 .. _the clients:
    656 
    657 Clients for debugging and evaluation of implementations
    658 -------------------------------------------------------
    659 
    660 These passes are useful for evaluating the various alias analysis
    661 implementations.  You can use them with commands like:
    662 
    663 .. code-block:: bash
    664 
    665   % opt -ds-aa -aa-eval foo.bc -disable-output -stats
    666 
    667 The ``-print-alias-sets`` pass
    668 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    669 
    670 The ``-print-alias-sets`` pass is exposed as part of the ``opt`` tool to print
    671 out the Alias Sets formed by the `AliasSetTracker`_ class.  This is useful if
    672 you're using the ``AliasSetTracker`` class.  To use it, use something like:
    673 
    674 .. code-block:: bash
    675 
    676   % opt -ds-aa -print-alias-sets -disable-output
    677 
    678 The ``-count-aa`` pass
    679 ^^^^^^^^^^^^^^^^^^^^^^
    680 
    681 The ``-count-aa`` pass is useful to see how many queries a particular pass is
    682 making and what responses are returned by the alias analysis.  As an example:
    683 
    684 .. code-block:: bash
    685 
    686   % opt -basicaa -count-aa -ds-aa -count-aa -licm
    687 
    688 will print out how many queries (and what responses are returned) by the
    689 ``-licm`` pass (of the ``-ds-aa`` pass) and how many queries are made of the
    690 ``-basicaa`` pass by the ``-ds-aa`` pass.  This can be useful when debugging a
    691 transformation or an alias analysis implementation.
    692 
    693 The ``-aa-eval`` pass
    694 ^^^^^^^^^^^^^^^^^^^^^
    695 
    696 The ``-aa-eval`` pass simply iterates through all pairs of pointers in a
    697 function and asks an alias analysis whether or not the pointers alias.  This
    698 gives an indication of the precision of the alias analysis.  Statistics are
    699 printed indicating the percent of no/may/must aliases found (a more precise
    700 algorithm will have a lower number of may aliases).
    701 
    702 Memory Dependence Analysis
    703 ==========================
    704 
    705 If you're just looking to be a client of alias analysis information, consider
    706 using the Memory Dependence Analysis interface instead.  MemDep is a lazy,
    707 caching layer on top of alias analysis that is able to answer the question of
    708 what preceding memory operations a given instruction depends on, either at an
    709 intra- or inter-block level.  Because of its laziness and caching policy, using
    710 MemDep can be a significant performance win over accessing alias analysis
    711 directly.
    712