AliasAnalysis.h revision b2143b6247901ae4eca2192ee134564c4f5f7853
1//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the generic AliasAnalysis interface, which is used as the 11// common interface used by all clients of alias analysis information, and 12// implemented by all alias analysis implementations. Mod/Ref information is 13// also captured by this interface. 14// 15// Implementations of this interface must implement the various virtual methods, 16// which automatically provides functionality for the entire suite of client 17// APIs. 18// 19// This API represents memory as a (Pointer, Size) pair. The Pointer component 20// specifies the base memory address of the region, the Size specifies how large 21// of an area is being queried, or UnknownSize if the size is not known. 22// Pointers that point to two completely different objects in memory never 23// alias, regardless of the value of the Size component. 24// 25//===----------------------------------------------------------------------===// 26 27#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H 28#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H 29 30#include "llvm/Support/CallSite.h" 31#include "llvm/System/IncludeFile.h" 32#include <vector> 33 34namespace llvm { 35 36class LoadInst; 37class StoreInst; 38class VAArgInst; 39class TargetData; 40class Pass; 41class AnalysisUsage; 42 43class AliasAnalysis { 44protected: 45 const TargetData *TD; 46 47private: 48 AliasAnalysis *AA; // Previous Alias Analysis to chain to. 49 50protected: 51 /// InitializeAliasAnalysis - Subclasses must call this method to initialize 52 /// the AliasAnalysis interface before any other methods are called. This is 53 /// typically called by the run* methods of these subclasses. This may be 54 /// called multiple times. 55 /// 56 void InitializeAliasAnalysis(Pass *P); 57 58 /// getAnalysisUsage - All alias analysis implementations should invoke this 59 /// directly (using AliasAnalysis::getAnalysisUsage(AU)). 60 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 61 62public: 63 static char ID; // Class identification, replacement for typeinfo 64 AliasAnalysis() : TD(0), AA(0) {} 65 virtual ~AliasAnalysis(); // We want to be subclassed 66 67 /// UnknownSize - This is a special value which can be used with the 68 /// size arguments in alias queries to indicate that the caller does not 69 /// know the sizes of the potential memory references. 70 static unsigned const UnknownSize = ~0u; 71 72 /// getTargetData - Return a pointer to the current TargetData object, or 73 /// null if no TargetData object is available. 74 /// 75 const TargetData *getTargetData() const { return TD; } 76 77 /// getTypeStoreSize - Return the TargetData store size for the given type, 78 /// if known, or a conservative value otherwise. 79 /// 80 unsigned getTypeStoreSize(const Type *Ty); 81 82 //===--------------------------------------------------------------------===// 83 /// Alias Queries... 84 /// 85 86 /// Location - A description of a memory location. 87 struct Location { 88 /// Ptr - The address of the start of the location. 89 const Value *Ptr; 90 /// Size - The size of the location. 91 unsigned Size; 92 /// TBAATag - The metadata node which describes the TBAA type of 93 /// the location, or null if there is no (unique) tag. 94 const MDNode *TBAATag; 95 96 explicit Location(const Value *P = 0, 97 unsigned S = UnknownSize, 98 const MDNode *N = 0) 99 : Ptr(P), Size(S), TBAATag(N) {} 100 }; 101 102 /// Alias analysis result - Either we know for sure that it does not alias, we 103 /// know for sure it must alias, or we don't know anything: The two pointers 104 /// _might_ alias. This enum is designed so you can do things like: 105 /// if (AA.alias(P1, P2)) { ... } 106 /// to check to see if two pointers might alias. 107 /// 108 /// See docs/AliasAnalysis.html for more information on the specific meanings 109 /// of these values. 110 /// 111 enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 }; 112 113 /// alias - The main low level interface to the alias analysis implementation. 114 /// Returns a Result indicating whether the two pointers are aliased to each 115 /// other. This is the interface that must be implemented by specific alias 116 /// analysis implementations. 117 virtual AliasResult alias(const Location &LocA, const Location &LocB); 118 119 /// alias - A convenience wrapper. 120 AliasResult alias(const Value *V1, unsigned V1Size, 121 const Value *V2, unsigned V2Size) { 122 return alias(Location(V1, V1Size), Location(V2, V2Size)); 123 } 124 125 /// alias - A convenience wrapper. 126 AliasResult alias(const Value *V1, const Value *V2) { 127 return alias(V1, UnknownSize, V2, UnknownSize); 128 } 129 130 /// isNoAlias - A trivial helper function to check to see if the specified 131 /// pointers are no-alias. 132 bool isNoAlias(const Location &LocA, const Location &LocB) { 133 return alias(LocA, LocB) == NoAlias; 134 } 135 136 /// isNoAlias - A convenience wrapper. 137 bool isNoAlias(const Value *V1, unsigned V1Size, 138 const Value *V2, unsigned V2Size) { 139 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size)); 140 } 141 142 /// pointsToConstantMemory - If the specified memory location is known to be 143 /// constant, return true. This allows disambiguation of store 144 /// instructions from constant pointers. 145 /// 146 virtual bool pointsToConstantMemory(const Location &Loc); 147 148 /// pointsToConstantMemory - A convenient wrapper. 149 bool pointsToConstantMemory(const Value *P) { 150 return pointsToConstantMemory(Location(P)); 151 } 152 153 //===--------------------------------------------------------------------===// 154 /// Simple mod/ref information... 155 /// 156 157 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 158 /// bits which may be or'd together. 159 /// 160 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 161 162 163 /// ModRefBehavior - Summary of how a function affects memory in the program. 164 /// Loads from constant globals are not considered memory accesses for this 165 /// interface. Also, functions may freely modify stack space local to their 166 /// invocation without having to report it through these interfaces. 167 enum ModRefBehavior { 168 // DoesNotAccessMemory - This function does not perform any non-local loads 169 // or stores to memory. 170 // 171 // This property corresponds to the GCC 'const' attribute. 172 DoesNotAccessMemory, 173 174 // AccessesArguments - This function accesses function arguments in well 175 // known (possibly volatile) ways, but does not access any other memory. 176 AccessesArguments, 177 178 // AccessesArgumentsAndGlobals - This function has accesses function 179 // arguments and global variables well known (possibly volatile) ways, but 180 // does not access any other memory. 181 AccessesArgumentsAndGlobals, 182 183 // OnlyReadsMemory - This function does not perform any non-local stores or 184 // volatile loads, but may read from any memory location. 185 // 186 // This property corresponds to the GCC 'pure' attribute. 187 OnlyReadsMemory, 188 189 // UnknownModRefBehavior - This indicates that the function could not be 190 // classified into one of the behaviors above. 191 UnknownModRefBehavior 192 }; 193 194 /// getModRefBehavior - Return the behavior when calling the given call site. 195 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); 196 197 /// getModRefBehavior - Return the behavior when calling the given function. 198 /// For use when the call site is not known. 199 virtual ModRefBehavior getModRefBehavior(const Function *F); 200 201 /// getIntrinsicModRefBehavior - Return the modref behavior of the intrinsic 202 /// with the given id. Most clients won't need this, because the regular 203 /// getModRefBehavior incorporates this information. 204 static ModRefBehavior getIntrinsicModRefBehavior(unsigned iid); 205 206 /// doesNotAccessMemory - If the specified call is known to never read or 207 /// write memory, return true. If the call only reads from known-constant 208 /// memory, it is also legal to return true. Calls that unwind the stack 209 /// are legal for this predicate. 210 /// 211 /// Many optimizations (such as CSE and LICM) can be performed on such calls 212 /// without worrying about aliasing properties, and many calls have this 213 /// property (e.g. calls to 'sin' and 'cos'). 214 /// 215 /// This property corresponds to the GCC 'const' attribute. 216 /// 217 bool doesNotAccessMemory(ImmutableCallSite CS) { 218 return getModRefBehavior(CS) == DoesNotAccessMemory; 219 } 220 221 /// doesNotAccessMemory - If the specified function is known to never read or 222 /// write memory, return true. For use when the call site is not known. 223 /// 224 bool doesNotAccessMemory(const Function *F) { 225 return getModRefBehavior(F) == DoesNotAccessMemory; 226 } 227 228 /// onlyReadsMemory - If the specified call is known to only read from 229 /// non-volatile memory (or not access memory at all), return true. Calls 230 /// that unwind the stack are legal for this predicate. 231 /// 232 /// This property allows many common optimizations to be performed in the 233 /// absence of interfering store instructions, such as CSE of strlen calls. 234 /// 235 /// This property corresponds to the GCC 'pure' attribute. 236 /// 237 bool onlyReadsMemory(ImmutableCallSite CS) { 238 ModRefBehavior MRB = getModRefBehavior(CS); 239 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 240 } 241 242 /// onlyReadsMemory - If the specified function is known to only read from 243 /// non-volatile memory (or not access memory at all), return true. For use 244 /// when the call site is not known. 245 /// 246 bool onlyReadsMemory(const Function *F) { 247 ModRefBehavior MRB = getModRefBehavior(F); 248 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 249 } 250 251 252 /// getModRefInfo - Return information about whether or not an instruction may 253 /// read or write the specified memory location. An instruction 254 /// that doesn't read or write memory may be trivially LICM'd for example. 255 ModRefResult getModRefInfo(const Instruction *I, 256 const Location &Loc) { 257 switch (I->getOpcode()) { 258 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc); 259 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc); 260 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc); 261 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc); 262 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc); 263 default: return NoModRef; 264 } 265 } 266 267 /// getModRefInfo - A convenience wrapper. 268 ModRefResult getModRefInfo(const Instruction *I, 269 const Value *P, unsigned Size) { 270 return getModRefInfo(I, Location(P, Size)); 271 } 272 273 /// getModRefInfo (for call sites) - Return whether information about whether 274 /// a particular call site modifies or reads the specified memory location. 275 virtual ModRefResult getModRefInfo(ImmutableCallSite CS, 276 const Location &Loc); 277 278 /// getModRefInfo (for call sites) - A convenience wrapper. 279 ModRefResult getModRefInfo(ImmutableCallSite CS, 280 const Value *P, unsigned Size) { 281 return getModRefInfo(CS, Location(P, Size)); 282 } 283 284 /// getModRefInfo (for calls) - Return whether information about whether 285 /// a particular call modifies or reads the specified memory location. 286 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) { 287 return getModRefInfo(ImmutableCallSite(C), Loc); 288 } 289 290 /// getModRefInfo (for calls) - A convenience wrapper. 291 ModRefResult getModRefInfo(const CallInst *C, const Value *P, unsigned Size) { 292 return getModRefInfo(C, Location(P, Size)); 293 } 294 295 /// getModRefInfo (for invokes) - Return whether information about whether 296 /// a particular invoke modifies or reads the specified memory location. 297 ModRefResult getModRefInfo(const InvokeInst *I, 298 const Location &Loc) { 299 return getModRefInfo(ImmutableCallSite(I), Loc); 300 } 301 302 /// getModRefInfo (for invokes) - A convenience wrapper. 303 ModRefResult getModRefInfo(const InvokeInst *I, 304 const Value *P, unsigned Size) { 305 return getModRefInfo(I, Location(P, Size)); 306 } 307 308 /// getModRefInfo (for loads) - Return whether information about whether 309 /// a particular load modifies or reads the specified memory location. 310 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc); 311 312 /// getModRefInfo (for loads) - A convenience wrapper. 313 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) { 314 return getModRefInfo(L, Location(P, Size)); 315 } 316 317 /// getModRefInfo (for stores) - Return whether information about whether 318 /// a particular store modifies or reads the specified memory location. 319 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc); 320 321 /// getModRefInfo (for stores) - A convenience wrapper. 322 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) { 323 return getModRefInfo(S, Location(P, Size)); 324 } 325 326 /// getModRefInfo (for va_args) - Return whether information about whether 327 /// a particular va_arg modifies or reads the specified memory location. 328 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc); 329 330 /// getModRefInfo (for va_args) - A convenience wrapper. 331 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, unsigned Size) { 332 return getModRefInfo(I, Location(P, Size)); 333 } 334 335 /// getModRefInfo - Return information about whether two call sites may refer 336 /// to the same set of memory locations. See 337 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo 338 /// for details. 339 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, 340 ImmutableCallSite CS2); 341 342 //===--------------------------------------------------------------------===// 343 /// Higher level methods for querying mod/ref information. 344 /// 345 346 /// canBasicBlockModify - Return true if it is possible for execution of the 347 /// specified basic block to modify the value pointed to by Ptr. 348 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc); 349 350 /// canBasicBlockModify - A convenience wrapper. 351 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size){ 352 return canBasicBlockModify(BB, Location(P, Size)); 353 } 354 355 /// canInstructionRangeModify - Return true if it is possible for the 356 /// execution of the specified instructions to modify the value pointed to by 357 /// Ptr. The instructions to consider are all of the instructions in the 358 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 359 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 360 const Location &Loc); 361 362 /// canInstructionRangeModify - A convenience wrapper. 363 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 364 const Value *Ptr, unsigned Size) { 365 return canInstructionRangeModify(I1, I2, Location(Ptr, Size)); 366 } 367 368 //===--------------------------------------------------------------------===// 369 /// Methods that clients should call when they transform the program to allow 370 /// alias analyses to update their internal data structures. Note that these 371 /// methods may be called on any instruction, regardless of whether or not 372 /// they have pointer-analysis implications. 373 /// 374 375 /// deleteValue - This method should be called whenever an LLVM Value is 376 /// deleted from the program, for example when an instruction is found to be 377 /// redundant and is eliminated. 378 /// 379 virtual void deleteValue(Value *V); 380 381 /// copyValue - This method should be used whenever a preexisting value in the 382 /// program is copied or cloned, introducing a new value. Note that analysis 383 /// implementations should tolerate clients that use this method to introduce 384 /// the same value multiple times: if the analysis already knows about a 385 /// value, it should ignore the request. 386 /// 387 virtual void copyValue(Value *From, Value *To); 388 389 /// replaceWithNewValue - This method is the obvious combination of the two 390 /// above, and it provided as a helper to simplify client code. 391 /// 392 void replaceWithNewValue(Value *Old, Value *New) { 393 copyValue(Old, New); 394 deleteValue(Old); 395 } 396}; 397 398/// isNoAliasCall - Return true if this pointer is returned by a noalias 399/// function. 400bool isNoAliasCall(const Value *V); 401 402/// isIdentifiedObject - Return true if this pointer refers to a distinct and 403/// identifiable object. This returns true for: 404/// Global Variables and Functions (but not Global Aliases) 405/// Allocas and Mallocs 406/// ByVal and NoAlias Arguments 407/// NoAlias returns 408/// 409bool isIdentifiedObject(const Value *V); 410 411} // End llvm namespace 412 413// Because of the way .a files work, we must force the BasicAA implementation to 414// be pulled in if the AliasAnalysis header is included. Otherwise we run 415// the risk of AliasAnalysis being used, but the default implementation not 416// being linked into the tool that uses it. 417FORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis) 418FORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis) 419 420#endif 421