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