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