diff --git a/docs/AliasAnalysis.html b/docs/AliasAnalysis.html index 7d299c8621c..df85d74371b 100644 --- a/docs/AliasAnalysis.html +++ b/docs/AliasAnalysis.html @@ -2,52 +2,57 @@ "http://www.w3.org/TR/html4/strict.dtd"> - Alias Analysis Infrastructure in LLVM + The LLVM Alias Analysis Infrastructure
- Alias Analysis Infrastructure in LLVM + The LLVM Alias Analysis Infrastructure
  1. Introduction
  2. -
  3. AliasAnalysis Overview +
  4. AliasAnalysis Class Overview
  5. +
  6. Other useful AliasAnalysis methods
  7. + + -
  8. Writing a new AliasAnalysis Implementation +
  9. Writing a new AliasAnalysis Implementation
  10. + + -
  11. Using AliasAnalysis results +
  12. Using alias analysis results
  13. +
  14. Using the AliasAnalysis interface directly
  15. + + -
  16. Helpful alias analysis related tools +
  17. Existing alias analysis implementations and clients
  18. +
  19. Available AliasAnalysis implementations
  20. +
  21. Alias analysis driven transformations
  22. +
  23. Clients for debugging and evaluation of implementations
  24. + +
-
-

Written by Chris Lattner

+
+

Written by Chris Lattner

@@ -58,20 +63,26 @@
-

Alias Analysis (or Pointer Analysis) is a technique which attempts to -determine whether or not two pointers ever can point to the same object in -memory. Traditionally, Alias Analyses respond to a query with either a Must, May, or No alias response, indicating that two -pointers do point to the same object, might point to the same object, or are -known not to point to the same object.

+

Alias Analysis (aka Pointer Analysis) is a class of techniques which attempt +to determine whether or not two pointers ever can point to the same object in +memory. There are many different algorithms for alias analysis and many +different ways of classifying them: flow-sensitive vs flow-insensitive, +context-sensitive vs context-insensitive, field-sensitive vs field-insensitive, +unification-based vs subset-based, etc. Traditionally, alias analyses respond +to a query with a Must, May, or No alias response, +indicating that two pointers always point to the same object, might point to the +same object, or are known to never point to the same object.

-

The AliasAnalysis class is the -centerpiece of the LLVM Alias Analysis related infrastructure. This class is -the common interface between clients of alias analysis information and the -implementations providing it. In addition to simple alias analysis information, -this class exposes Mod/Ref information from those implementations which can -provide it, allowing for powerful analyses and transformations to work well -together.

+

The LLVM AliasAnalysis +class is the primary interface used by clients and implementations of alias +analyses in the LLVM system. This class is the common interface between clients +of alias analysis information and the implementations providing it, and is +designed to support a wide range of implementations and clients (but currently +all clients are assumed to be flow-insensitive). In addition to simple alias +analysis information, this class exposes Mod/Ref information from those +implementations which can provide it, allowing for powerful analyses and +transformations to work well together.

This document contains information necessary to successfully implement this interface, use it, and to test both sides. It also explains some of the finer @@ -83,24 +94,25 @@ know.

-

The AliasAnalysis class -defines the interface that Alias Analysis implementations should support. This -class exports two important enums: AliasResult and -ModRefResult which represent the result of an alias query or a mod/ref -query, respectively.

+

The AliasAnalysis +class defines the interface that the various alias analysis implementations +should support. This class exports two important enums: AliasResult +and ModRefResult which represent the result of an alias query or a +mod/ref query, respectively.

-

The AliasAnalysis interface exposes information about memory, represented in -several different ways. In particular, memory objects are represented as a -starting address and size, and function calls are represented as the actual -call or invoke instructions that performs the call. The -AliasAnalysis interface also exposes some helper methods which allow you to get -mod/ref information for arbitrary instructions.

+

The AliasAnalysis interface exposes information about memory, +represented in several different ways. In particular, memory objects are +represented as a starting address and size, and function calls are represented +as the actual call or invoke instructions that performs the +call. The AliasAnalysis interface also exposes some helper methods +which allow you to get mod/ref information for arbitrary instructions.

@@ -111,13 +123,15 @@ mod/ref information for arbitrary instructions.

-

Most importantly, the AliasAnalysis class provides several methods which are -used to query whether or not pointers alias, whether function calls can modify -or read memory, etc.

+

Most importantly, the AliasAnalysis class provides several methods +which are used to query whether or not two memory objects alias, whether +function calls can modify or read a memory object, etc. For all of these +queries, memory objects are represented as a pair of their starting address (a +symbolic LLVM Value*) and a static size.

Representing memory objects as a starting address and a size is critically -important for precise Alias Analyses. For example, consider this (silly) C -code:

+important for correct Alias Analyses. For example, consider this (silly, but +possible) C code:

   int i;
@@ -156,6 +170,17 @@ that the accesses alias.

+ +
+The alias method is the primary interface used to determine whether or +not two memory objects alias each other. It takes two memory objects as input +and returns MustAlias, MayAlias, or NoAlias as appropriate. +
+ + + @@ -163,13 +188,13 @@ that the accesses alias.

An Alias Analysis implementation can return one of three responses: MustAlias, MayAlias, and NoAlias. The No and May alias results are obvious: if -the two pointers may never equal each other, return NoAlias, if they might, +the two pointers can never equal each other, return NoAlias, if they might, return MayAlias.

-

The Must Alias response is trickier though. In LLVM, the Must Alias response +

The MustAlias response is trickier though. In LLVM, the Must Alias response may only be returned if the two memory objects are guaranteed to always start at exactly the same location. If two memory objects overlap, but do not start at -the same location, MayAlias must be returned.

+the same location, return MayAlias.

@@ -182,14 +207,103 @@ the same location, MayAlias must be returned.

The getModRefInfo methods return information about whether the execution of an instruction can read or modify a memory location. Mod/Ref -information is always conservative: if an action may read a location, Ref -is returned.

+information is always conservative: if an instruction might read or write +a location, ModRef is returned.

+ +

The AliasAnalysis class also provides a getModRefInfo +method for testing dependencies between function calls. This method takes two +call sites (CS1 & CS2), returns NoModRef if the two calls refer to disjoint +memory locations, Ref if CS1 reads memory written by CS2, Mod if CS1 writes to +memory read or written by CS2, or ModRef if CS1 might read or write memory +accessed by CS2. Note that this relation is not commutative. Clients that use +this method should be predicated on the hasNoModRefInfoForCalls() +method, which indicates whether or not an analysis can provide mod/ref +information for function call pairs (most can not). If this predicate is false, +the client shouldn't waste analysis time querying the getModRefInfo +method many times.

+ + +
+ Other useful AliasAnalysis methods +
+ +
+ +

+Several other tidbits of information are often collected by various alias +analysis implementations and can be put to good use by various clients. +

+ +
+ + +
+ The getMustAliases method +
+ +
+ +

The getMustAliases method returns all values that are known to +always must alias a pointer. This information can be provided in some cases for +important objects like the null pointer and global values. Knowing that a +pointer always points to a particular function allows indirect calls to be +turned into direct calls, for example.

+ +
+ + +
+ The pointsToConstantMemory method +
+ +
+ +

The pointsToConstantMemory method returns true if and only if the +analysis can prove that the pointer only points to unchanging memory locations +(functions, constant global variables, and the null pointer). This information +can be used to refine mod/ref information: it is impossible for an unchanging +memory location to be modified.

+ +
+ + + +
+ The doesNotAccessMemory and + onlyReadsMemory methods +
+ +
+ +

These methods are used to provide very simple mod/ref information for +function calls. The doesNotAccessMemory method returns true for a +function if the analysis can prove that the function never reads or writes to +memory, or if the function only reads from constant memory. Functions with this +property are side-effect free and only depend on their input arguments, allowing +them to be eliminated if they form common subexpressions or be hoisted out of +loops. Many common functions behave this way (e.g., sin and +cos) but many others do not (e.g., acos, which modifies the +errno variable).

+ +

The onlyReadsMemory method returns true for a function if analysis +can prove that (at most) the function only reads from non-volatile memory. +Functions with this property are side-effect free, only depending on their input +arguments and the state of memory when they are called. This property allows +calls to these functions to be eliminated and moved around, as long as there is +no store instruction that changes the contents of memory. Note that all +functions that satisfy the doesNotAccessMemory method also satisfies +onlyReadsMemory.

+ +
+ + +
- Writing a new AliasAnalysis Implementation + Writing a new AliasAnalysis Implementation
@@ -198,8 +312,8 @@ is returned.

Writing a new alias analysis implementation for LLVM is quite straight-forward. There are already several implementations that you can use for examples, and the following information should help fill in any details. -For a minimal example, take a look at the no-aa implementation.

+For a examples, take a look at the various alias analysis +implementations included with LLVM.

@@ -219,8 +333,7 @@ solve:

  1. If you require interprocedural analysis, it should be a Pass.
  2. -
  3. If you are a global analysis, subclass FunctionPass.
  4. -
  5. If you are a local pass, subclass BasicBlockPass.
  6. +
  7. If you are a function-local analysis, subclass FunctionPass.
  8. If you don't need to look at the program at all, subclass ImmutablePass.
@@ -239,8 +352,8 @@ solve:

-

Your subclass of AliasAnalysis is required to invoke two methods on the -AliasAnalysis base class: getAnalysisUsage and +

Your subclass of AliasAnalysis is required to invoke two methods on +the AliasAnalysis base class: getAnalysisUsage and InitializeAliasAnalysis. In particular, your implementation of getAnalysisUsage should explicitly call into the AliasAnalysis::getAnalysisUsage method in addition to doing any @@ -256,9 +369,8 @@ like this:

Additionally, your must invoke the InitializeAliasAnalysis method from your analysis run method (run for a Pass, -runOnFunction for a FunctionPass, runOnBasicBlock for -a BasicBlockPass, or InitializeAliasAnalysis for an -ImmutablePass). For example (as part of a Pass):

+runOnFunction for a FunctionPass, or InitializePass +for an ImmutablePass). For example (as part of a Pass):

     bool run(Module &M) {
@@ -277,29 +389,110 @@ a BasicBlockPass, or InitializeAliasAnalysis for an
 
 
-

All of the AliasAnalysis -virtual methods default to providing conservatively correct information -(returning "May" Alias and "Mod/Ref" for alias and mod/ref queries +

All of the AliasAnalysis +virtual methods default to providing chaining to another +alias analysis implementation, which ends up returning conservatively correct +information (returning "May" Alias and "Mod/Ref" for alias and mod/ref queries respectively). Depending on the capabilities of the analysis you are implementing, you just override the interfaces you can improve.

+ +
-

With only two special exceptions (the basicaa and no-aa passes) every alias analysis pass should chain -to another alias analysis implementation (for example, you could specify -"-basic-aa -ds-aa -andersens-aa -licm" to get the maximum benefit from -the three alias analyses). To do this, simply "Require" AliasAnalysis in your -getAnalysisUsage method, and if you need to return a conservative -MayAlias or Mod/Ref result, simply chain to a lower analysis.

+

With only two special exceptions (the basicaa and no-aa +passes) every alias analysis pass chains to another alias analysis +implementation (for example, the user can specify "-basicaa -ds-aa +-anders-aa -licm" to get the maximum benefit from the three alias +analyses). The alias analysis class automatically takes care of most of this +for methods that you don't override. For methods that you do override, in code +paths that return a conservative MayAlias or Mod/Ref result, simply return +whatever the superclass computes. For example:

+
+  AliasAnalysis::AliasResult alias(const Value *V1, unsigned V1Size,
+                                   const Value *V2, unsigned V2Size) {
+    if (...)
+      return NoAlias;
+    ...
+
+    // Couldn't determine a must or no-alias result.
+    return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
+  }
+
+ +

In addition to analysis queries, you must make sure to unconditionally pass +LLVM update notification methods to the superclass as +well if you override them, which allows all alias analyses in a change to be +updated.

+ +
+ + + + + +
+

+Alias analysis information is initially computed for a static snapshot of the +program, but clients will use this information to make transformations to the +code. All but the most trivial forms of alias analysis will need to have their +analysis results updated to reflect the changes made by these transformations. +

+ +

+The AliasAnalysis interface exposes two methods which are used to +communicate program changes from the clients to the analysis implementations. +Various alias analysis implementations should use these methods to ensure that +their internal data structures are kept up-to-date as the program changes (for +example, when an instruction is deleted), and clients of alias analysis must be +sure to call these interfaces appropriately. +

+
+ + +
The deleteValue method
+ +
+The deleteValue method is called by transformations when they remove an +instruction or any other value from the program (including values that do not +use pointers). Typically alias analyses keep data structures that have entries +for each value in the program. When this method is called, they should remove +any entries for the specified value, if they exist. +
+ + + +
The copyValue method
+ +
+The copyValue method is used when a new value is introduced into the +program. There is no way to introduce a value into the program that did not +exist before (this doesn't make sense for a safe compiler transformation), so +this is the only way to introduce a new value. This method indicates that the +new value has exactly the same properties as the value being copied. +
+ + + +
The replaceWithNewValue method
+ +
+This method is a simple helper method that is provided to make clients easier to +use. It is implemented by copying the old analysis information to the new +value, then deleting the old value. This method cannot be overridden by alias +analysis implementations.
@@ -320,7 +513,7 @@ method as possible (within reason).

@@ -339,10 +532,10 @@ preference, these are...

The load-vn pass uses alias analysis to provide value numbering -information for load instructions. If your analysis or transformation -can be modelled in a form that uses value numbering information, you don't have -to do anything special to handle load instructions: just use the -load-vn pass, which uses alias analysis.

+information for load instructions and pointer values. If your analysis +or transformation can be modeled in a form that uses value numbering +information, you don't have to do anything special to handle load instructions: +just use the load-vn pass, which uses alias analysis.

@@ -357,13 +550,13 @@ to do anything special to handle load instructions: just use the in some scope, rather than information about pairwise aliasing. The AliasSetTracker class is used to efficiently build these Alias Sets from the pairwise alias analysis -information provided by the AliasAnalysis interface.

+information provided by the AliasAnalysis interface.

-

First you initialize the AliasSetTracker by use the "add" methods to -add information about various potentially aliasing instructions in the scope you -are interested in. Once all of the alias sets are completed, your pass should -simply iterate through the constructed alias sets, using the AliasSetTracker -begin()/end() methods.

+

First you initialize the AliasSetTracker by using the "add" methods +to add information about various potentially aliasing instructions in the scope +you are interested in. Once all of the alias sets are completed, your pass +should simply iterate through the constructed alias sets, using the +AliasSetTracker begin()/end() methods.

The AliasSets formed by the AliasSetTracker are guaranteed to be disjoint, calculate mod/ref information and volatility for the set, and @@ -372,16 +565,17 @@ The AliasSetTracker also makes sure that sets are properly folded due to call instructions, and can provide a list of pointers in each set.

As an example user of this, the Loop -Invariant Code Motion pass uses AliasSetTrackers to build alias information -about each loop nest. If an AliasSet in a loop is not modified, then all load -instructions from that set may be hoisted out of the loop. If any alias sets -are stored and are must alias sets, then the stores may be sunk to -outside of the loop, promoting the memory location to a register for the -duration of the loop nest. Both of these transformations obviously only apply -if the pointer argument is loop-invariant.

+Invariant Code Motion pass uses AliasSetTrackers to calculate alias +sets for each loop nest. If an AliasSet in a loop is not modified, +then all load instructions from that set may be hoisted out of the loop. If any +alias sets are stored to and are must alias sets, then the stores may be +sunk to outside of the loop, promoting the memory location to a register for the +duration of the loop nest. Both of these transformations only apply if the +pointer argument is loop-invariant.

+
The AliasSetTracker implementation
@@ -410,50 +604,257 @@ are.

- Using the AliasAnalysis interface directly + Using the AliasAnalysis interface directly
-

As a last resort, your pass could use the AliasAnalysis interface directly to -service your pass. If you find the need to do this, please let me know so I can see if something new -needs to be added to LLVM.

+

If neither of these utility class are what your pass needs, you should use +the interfaces exposed by the AliasAnalysis class directly. Try to use +the higher-level methods when possible (e.g., use mod/ref information instead of +the alias method directly if possible) to get the +best precision and efficiency.

- Helpful alias-analysis-related tools + Existing alias analysis implementations and clients
-

If you're going to be working with the AliasAnalysis infrastructure, there -are several nice tools that may be useful for you and are worth knowing -about...

+

If you're going to be working with the LLVM alias analysis infrastructure, +you should know what clients and implementations of alias analysis are +available. In particular, if you are implementing an alias analysis, you should +be aware of the the clients that are useful +for monitoring and evaluating different implementations.

+ Available AliasAnalysis implementations +
+ +
+ +

This section lists the various implementations of the AliasAnalysis +interface. With the exception of the -no-aa and +-basicaa implementations, all of these chain to other alias analysis implementations.

+ +
+ + +
The -no-aa pass
-

The -no-aa analysis is just like what it sounds: an alias analysis -that never returns any useful information. This pass can be useful if you think -that alias analysis is doing something wrong and are trying to narrow down a -problem. If you don't specify an alias analysis, the default will be to use the -basicaa pass which does quite a bit of disambiguation on its own.

+

The -no-aa pass is just like what it sounds: an alias analysis that +never returns any useful information. This pass can be useful if you think that +alias analysis is doing something wrong and are trying to narrow down a +problem.

+ +
+ + + +
+ The -basicaa pass +
+ +
+ +

The -basicaa pass is the default LLVM alias analysis. It is an +aggressive local analysis that "knows" many important facts:

+ + + +
+ + + +
+ The -anders-aa pass +
+ +
+ +

The -anders-aa pass implements the well-known "Andersen's algorithm" +for interprocedural alias analysis. This algorithm is a subset-based, +flow-insensitive, context-insensitive, and field-insensitive alias analysis that +is widely believed to be fairly precise. Unfortunately, this algorithm is also +O(N3). The LLVM implementation currently does not implement any of +the refinements (such as "online cycle elimination" or "offline variable +substitution") to improve its efficiency, so it can be quite slow in common +cases. +

+ +
+ + +
+ The -steens-aa pass +
+ +
+ +

The -steens-aa pass implements a variation on the well-known +"Steensgaard's algorithm" for interprocedural alias analysis. Steensgaard's +algorithm is a unification-based, flow-insensitive, context-insensitive, and +field-insensitive alias analysis that is also very scalable (effectively linear +time).

+ +

The LLVM -steens-aa pass implements a "speculatively +field-sensitive" version of Steensgaard's algorithm using the Data +Structure Analysis framework. This gives it substantially more precision than +the standard algorithm while maintaining excellent analysis scalability.

+ +
+ + +
+ The -ds-aa pass +
+ +
+ +

The -ds-aa pass implements the full Data Structure Analysis +algorithm. Data Structure Analysis is a modular unification-based, +flow-insensitive, context-sensitive, and speculatively +field-sensitive alias analysis that is also quite scalable, usually at +O(n*log(n)).

+ +

This algorithm is capable of responding to a full variety of alias analysis +queries, and can provide context-sensitive mod/ref information as well. The +only major facility not implemented so far is support for must-alias +information.

+ Alias analysis driven transformations +
+ +
+LLVM includes several alias-analysis driven transformations which can be used +with any of the implementations above. +
+ + +
+ The -adce pass +
+ +
+ +

The -adce pass, which implements Aggressive Dead Code Elimination +uses the AliasAnalysis interface to delete calls to functions that do +not have side-effects and are not used.

+ +
+ + + +
+ The -licm pass +
+ +
+ +

The -licm pass implements various Loop Invariant Code Motion related +transformations. It uses the AliasAnalysis interface for several +different transformations:

+ + + +
+ + +
+ The -argpromotion pass +
+ +
+

+The -argpromotion pass promotes by-reference arguments to be passed in +by-value instead. In particular, if pointer arguments are only loaded from it +passes in the value loaded instead of the address to the function. This pass +uses alias information to make sure that the value loaded from the argument +pointer is not modified between the entry of the function and any load of the +pointer.

+
+ + +
+ The -load-vn & -gcse passes +
+ +
+

+The -load-vn pass uses alias analysis to "value +number" loads and pointers values, which is used by the GCSE pass to +eliminate instructions. The -load-vn pass relies on alias information +and must-alias information. This combination of passes can make the following +transformations:

+ + + +
+ + + + + +
+ Clients for debugging and evaluation of implementations +
+ +These passes are useful for evaluating the various alias analysis +implementations. You can use them with commands like 'opt -anders-aa -ds-aa +-aa-eval foo.bc -disable-output -stats'. + + + +
The -print-alias-sets pass
@@ -462,20 +863,21 @@ problem. If you don't specify an alias analysis, the default will be to use the

The -print-alias-sets pass is exposed as part of the analyze tool to print out the Alias Sets formed by the AliasSetTracker class. This is useful if you're using -the AliasSetTracker.

+the AliasSetTracker class.

- -
+ + +
The -count-aa pass

The -count-aa pass is useful to see how many queries a particular -pass is making and what kinds of responses are returned by the alias analysis. -An example usage is:

+pass is making and what responses are returned by the alias analysis. An +example usage is:

   $ opt -basicaa -count-aa -ds-aa -count-aa -licm
@@ -484,12 +886,12 @@ An example usage is:

Which will print out how many queries (and what responses are returned) by the -licm pass (of the -ds-aa pass) and how many queries are made of the -basicaa pass by the -ds-aa pass. This can be -useful when evaluating an alias analysis for precision.

+useful when debugging a transformation or an alias analysis implementation.

- -
+ + @@ -498,7 +900,8 @@ useful when evaluating an alias analysis for precision.

The -aa-eval pass simply iterates through all pairs of pointers in a function and asks an alias analysis whether or not the pointers alias. This gives an indication of the precision of the alias analysis. Statistics are -printed.

+printed indicating the percent of no/may/must aliases found (a more precise +algorithm will have a lower number of may aliases).