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