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