AliasAnalysis.h revision 192913e281a0e9b97275fa1b84da96b02397323e
1b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===// 2b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 3b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// The LLVM Compiler Infrastructure 4b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 5b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// This file was developed by the LLVM research group and is distributed under 6b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// the University of Illinois Open Source License. See LICENSE.TXT for details. 7b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 8b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru//===----------------------------------------------------------------------===// 9b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 10b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// This file defines the generic AliasAnalysis interface, which is used as the 11b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// common interface used by all clients of alias analysis information, and 12b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// implemented by all alias analysis implementations. Mod/Ref information is 13b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// also captured by this interface. 14b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 15b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// Implementations of this interface must implement the various virtual methods, 16b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// which automatically provides functionality for the entire suite of client 17b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// APIs. 18b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 19b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// This API represents memory as a (Pointer, Size) pair. The Pointer component 20b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// specifies the base memory address of the region, the Size specifies how large 21b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// of an area is being queried. If Size is 0, two pointers only alias if they 22b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// are exactly equal. If size is greater than zero, but small, the two pointers 23b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// alias if the areas pointed to overlap. If the size is very large (ie, ~0U), 24b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// then the two pointers alias if they may be pointing to components of the same 25b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// memory object. Pointers that point to two completely different objects in 26b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// memory never alias, regardless of the value of the Size component. 27b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 28b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru//===----------------------------------------------------------------------===// 29b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 30b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H 31b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H 32b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 33b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "llvm/Support/CallSite.h" 34b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "llvm/Pass.h" // Need this for IncludeFile 35b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 36b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querunamespace llvm { 37b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru 38b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruclass LoadInst; 39b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruclass StoreInst; 40b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruclass VAArgInst; 41b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruclass TargetData; 42b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 43b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruclass AliasAnalysis { 44b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruprotected: 45b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru const TargetData *TD; 46b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru AliasAnalysis *AA; // Previous Alias Analysis to chain to. 47b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 48b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// InitializeAliasAnalysis - Subclasses must call this method to initialize 49b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// the AliasAnalysis interface before any other methods are called. This is 50b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// typically called by the run* methods of these subclasses. This may be 51b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// called multiple times. 52b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 53b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru void InitializeAliasAnalysis(Pass *P); 54b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 55b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // getAnalysisUsage - All alias analysis implementations should invoke this 56b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that 57b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // TargetData is required by the pass. 58b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual void getAnalysisUsage(AnalysisUsage &AU) const; 59b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 60b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querupublic: 61b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru AliasAnalysis() : TD(0), AA(0) {} 62b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual ~AliasAnalysis(); // We want to be subclassed 63b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 64b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// getTargetData - Every alias analysis implementation depends on the size of 65b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// data items in the current Target. This provides a uniform way to handle 66b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// it. 67b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 68b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru const TargetData &getTargetData() const { return *TD; } 69b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 70b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru //===--------------------------------------------------------------------===// 71b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Alias Queries... 72b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 73b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 74b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Alias analysis result - Either we know for sure that it does not alias, we 75b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// know for sure it must alias, or we don't know anything: The two pointers 76b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// _might_ alias. This enum is designed so you can do things like: 77b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// if (AA.alias(P1, P2)) { ... } 78b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// to check to see if two pointers might alias. 79b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 80b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 }; 81b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 82b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// alias - The main low level interface to the alias analysis implementation. 83b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Returns a Result indicating whether the two pointers are aliased to each 84b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// other. This is the interface that must be implemented by specific alias 85b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// analysis implementations. 86b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 87b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual AliasResult alias(const Value *V1, unsigned V1Size, 88b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru const Value *V2, unsigned V2Size); 89c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru 90b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// getMustAliases - If there are any pointers known that must alias this 91b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// pointer, return them now. This allows alias-set based alias analyses to 92b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// perform a form a value numbering (which is exposed by load-vn). If an 93b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// alias analysis supports this, it should ADD any must aliased pointers to 94b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// the specified vector. 95b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 96c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals); 97b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 98b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// pointsToConstantMemory - If the specified pointer is known to point into 99b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// constant global memory, return true. This allows disambiguation of store 100c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru /// instructions from constant pointers. 101b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 102b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru virtual bool pointsToConstantMemory(const Value *P); 103b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 104b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru //===--------------------------------------------------------------------===// 105b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Simple mod/ref information... 106b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 107b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 108b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 109b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru /// bits which may be or'd together. 110b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru /// 111b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 112b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 113b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 114b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// ModRefBehavior - Summary of how a function affects memory in the program. 115b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Loads from constant globals are not considered memory accesses for this 116b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// interface. Also, functions may freely modify stack space local to their 117b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// invocation without having to report it through these interfaces. 118b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru enum ModRefBehavior { 119b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // DoesNotAccessMemory - This function does not perform any non-local loads 120b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // or stores to memory. 121b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // 122b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // This property corresponds to the GCC 'const' attribute. 123b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru DoesNotAccessMemory, 124b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 125b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // AccessesArguments - This function accesses function arguments in 126b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru // non-volatile and well known ways, but does not access any other memory. 127b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // 128b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // Clients may call getArgumentAccesses to get specific information about 129b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru // how pointer arguments are used. 130b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru AccessesArguments, 131b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 132b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // AccessesArgumentsAndGlobals - This function has accesses function 133b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // arguments and global variables in non-volatile and well-known ways, but 134b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // does not access any other memory. 135b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // 136b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // Clients may call getArgumentAccesses to get specific information about 137b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // how pointer arguments and globals are used. 138b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru AccessesArgumentsAndGlobals, 139b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 140b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // OnlyReadsMemory - This function does not perform any non-local stores or 141b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // volatile loads, but may read from any memory location. 142b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // 143b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru // This property corresponds to the GCC 'pure' attribute. 144b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru OnlyReadsMemory, 145b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 146b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // UnknownModRefBehavior - This indicates that the function could not be 147b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru // classified into one of the behaviors above. 148b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru UnknownModRefBehavior 149b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru }; 150b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru 151b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru /// PointerAccessInfo - This struct is used to return results for pointers, 152b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// globals, and the return value of a function. 153b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru struct PointerAccessInfo { 154b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// V - The value this record corresponds to. This may be an Argument for 155b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// the function, a GlobalVariable, or null, corresponding to the return 156b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// value for the function. 157b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru Value *V; 158b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 159b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// ModRefInfo - Whether the pointer is loaded or stored to/from. 160b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 161b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefResult ModRefInfo; 162b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 163b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// AccessType - Specific fine-grained access information for the argument. 164b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// If none of these classifications is general enough, the 165b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// getModRefBehavior method should not return AccessesArguments*. If a 166b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// record is not returned for a particular argument, the argument is never 167b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// dead and never dereferenced. 168b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru enum AccessType { 169b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// ScalarAccess - The pointer is dereferenced. 170b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 171b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ScalarAccess, 172b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 173b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// ArrayAccess - The pointer is indexed through as an array of elements. 174b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 175b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ArrayAccess, 176b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 177b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// ElementAccess ?? P->F only? 178b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 179b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// CallsThrough - Indirect calls are made through the specified function 180b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// pointer. 181b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru CallsThrough 182b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru }; 183b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru }; 184b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 185b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// getModRefBehavior - Return the behavior of the specified function if 186b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// called from the specified call site. The call site may be null in which 187b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// case the most generic behavior of this function should be returned. 188b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS, 189b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru std::vector<PointerAccessInfo> *Info = 0); 190b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 191b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// doesNotAccessMemory - If the specified function is known to never read or 192b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// write memory, return true. If the function only reads from known-constant 193b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// memory, it is also legal to return true. Functions that unwind the stack 194b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// are not legal for this predicate. 195b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 196b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Many optimizations (such as CSE and LICM) can be performed on calls to it, 197b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// without worrying about aliasing properties, and many functions have this 198b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// property (e.g. 'sin' and 'cos'). 199b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 200b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// This property corresponds to the GCC 'const' attribute. 201b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 202b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru bool doesNotAccessMemory(Function *F) { 203b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru return getModRefBehavior(F, CallSite()) == DoesNotAccessMemory; 204b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 205b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 206b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// onlyReadsMemory - If the specified function is known to only read from 207b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// non-volatile memory (or not access memory at all), return true. Functions 208b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// that unwind the stack are not legal for this predicate. 209b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 210b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// This property allows many common optimizations to be performed in the 211b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// absence of interfering store instructions, such as CSE of strlen calls. 212b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 213b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// This property corresponds to the GCC 'pure' attribute. 214b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 215b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru bool onlyReadsMemory(Function *F) { 216b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// FIXME: If the analysis returns more precise info, we can reduce it to 217b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// this. 218b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefBehavior MRB = getModRefBehavior(F, CallSite()); 219b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 220b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 221b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 222b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 223b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// getModRefInfo - Return information about whether or not an instruction may 224b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// read or write memory specified by the pointer operand. An instruction 225b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// that doesn't read or write memory may be trivially LICM'd for example. 226b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 227b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// getModRefInfo (for call sites) - Return whether information about whether 228b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// a particular call site modifies or reads the memory specified by the 229b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// pointer. 230b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 231b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); 232b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 233b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// getModRefInfo - Return information about whether two call sites may refer 234b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// to the same set of memory locations. This function returns NoModRef if 235c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory 236c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or 237b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru /// ModRef if CS1 might read or write memory accessed by CS2. 238b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru /// 239b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2); 240b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 241b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// hasNoModRefInfoForCalls - Return true if the analysis has no mod/ref 242b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// information for pairs of function calls (other than "pure" and "const" 243b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// functions). This can be used by clients to avoid many pointless queries. 244b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Remember that if you override this and chain to another analysis, you must 245b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// make sure that it doesn't have mod/ref info either. 246b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 247b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual bool hasNoModRefInfoForCalls() const; 248b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 249b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Convenience functions... 250b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size); 251b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size); 252b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) { 253b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru return getModRefInfo(CallSite(C), P, Size); 254b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 255b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) { 256b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru return getModRefInfo(CallSite(I), P, Size); 257b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 258b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefResult getModRefInfo(VAArgInst* I, Value* P, unsigned Size) { 259b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru return AliasAnalysis::Mod; 260b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 261b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) { 262b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru switch (I->getOpcode()) { 263b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru case Instruction::VAArg: return getModRefInfo((VAArgInst*)I, P, Size); 264b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru case Instruction::Load: return getModRefInfo((LoadInst*)I, P, Size); 265b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru case Instruction::Store: return getModRefInfo((StoreInst*)I, P, Size); 266b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru case Instruction::Call: return getModRefInfo((CallInst*)I, P, Size); 267b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size); 268b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru default: return NoModRef; 269b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 270b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 271b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 272b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru //===--------------------------------------------------------------------===// 273b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Higher level methods for querying mod/ref information. 274b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 275b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 276b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// canBasicBlockModify - Return true if it is possible for execution of the 277b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// specified basic block to modify the value pointed to by Ptr. 278b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 279b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size); 280b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 281b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// canInstructionRangeModify - Return true if it is possible for the 282b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// execution of the specified instructions to modify the value pointed to by 283b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Ptr. The instructions to consider are all of the instructions in the 284b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 285b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 286b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 287b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru const Value *Ptr, unsigned Size); 288b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 289b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru //===--------------------------------------------------------------------===// 290b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// Methods that clients should call when they transform the program to allow 291b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// alias analyses to update their internal data structures. Note that these 292b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// methods may be called on any instruction, regardless of whether or not 293b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// they have pointer-analysis implications. 294b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 295b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 296b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// deleteValue - This method should be called whenever an LLVM Value is 297b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// deleted from the program, for example when an instruction is found to be 298b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// redundant and is eliminated. 299b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 300b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual void deleteValue(Value *V); 301b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 302b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// copyValue - This method should be used whenever a preexisting value in the 303b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// program is copied or cloned, introducing a new value. Note that analysis 304b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// implementations should tolerate clients that use this method to introduce 305b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// the same value multiple times: if the analysis already knows about a 306b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// value, it should ignore the request. 307b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 308b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru virtual void copyValue(Value *From, Value *To); 309b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 310b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// replaceWithNewValue - This method is the obvious combination of the two 311b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// above, and it provided as a helper to simplify client code. 312b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru /// 313b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru void replaceWithNewValue(Value *Old, Value *New) { 314b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru copyValue(Old, New); 315b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru deleteValue(Old); 316b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru } 317b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}; 318b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 319b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// Because of the way .a files work, we must force the BasicAA implementation to 320b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// be pulled in if the AliasAnalysis header is included. Otherwise we run 321b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// the risk of AliasAnalysis being used, but the default implementation not 322b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// being linked into the tool that uses it. 323b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru// 324b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruextern int BasicAAStub; 325b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic IncludeFile HDR_INCLUDE_BASICAA_CPP(&BasicAAStub); 326b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 327b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru} // End llvm namespace 328b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru 329b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif 330b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru