AliasAnalysis.h revision c8ddbdabb697b20b948c1a56af6062f26691532a
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 /// Alias analysis result - Either we know for sure that it does not alias, we 87 /// know for sure it must alias, or we don't know anything: The two pointers 88 /// _might_ alias. This enum is designed so you can do things like: 89 /// if (AA.alias(P1, P2)) { ... } 90 /// to check to see if two pointers might alias. 91 /// 92 /// See docs/AliasAnalysis.html for more information on the specific meanings 93 /// of these values. 94 /// 95 enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 }; 96 97 /// alias - The main low level interface to the alias analysis implementation. 98 /// Returns a Result indicating whether the two pointers are aliased to each 99 /// other. This is the interface that must be implemented by specific alias 100 /// analysis implementations. 101 /// 102 virtual AliasResult alias(const Value *V1, unsigned V1Size, 103 const Value *V2, unsigned V2Size); 104 105 /// alias - A convenience wrapper for the case where the sizes are unknown. 106 AliasResult alias(const Value *V1, const Value *V2) { 107 return alias(V1, UnknownSize, V2, UnknownSize); 108 } 109 110 /// isNoAlias - A trivial helper function to check to see if the specified 111 /// pointers are no-alias. 112 bool isNoAlias(const Value *V1, unsigned V1Size, 113 const Value *V2, unsigned V2Size) { 114 return alias(V1, V1Size, V2, V2Size) == NoAlias; 115 } 116 117 /// pointsToConstantMemory - If the specified pointer is known to point into 118 /// constant global memory, return true. This allows disambiguation of store 119 /// instructions from constant pointers. 120 /// 121 virtual bool pointsToConstantMemory(const Value *P); 122 123 //===--------------------------------------------------------------------===// 124 /// Simple mod/ref information... 125 /// 126 127 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 128 /// bits which may be or'd together. 129 /// 130 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 131 132 133 /// ModRefBehavior - Summary of how a function affects memory in the program. 134 /// Loads from constant globals are not considered memory accesses for this 135 /// interface. Also, functions may freely modify stack space local to their 136 /// invocation without having to report it through these interfaces. 137 enum ModRefBehavior { 138 // DoesNotAccessMemory - This function does not perform any non-local loads 139 // or stores to memory. 140 // 141 // This property corresponds to the GCC 'const' attribute. 142 DoesNotAccessMemory, 143 144 // AccessesArguments - This function accesses function arguments in well 145 // known (possibly volatile) ways, but does not access any other memory. 146 AccessesArguments, 147 148 // AccessesArgumentsAndGlobals - This function has accesses function 149 // arguments and global variables well known (possibly volatile) ways, but 150 // does not access any other memory. 151 AccessesArgumentsAndGlobals, 152 153 // OnlyReadsMemory - This function does not perform any non-local stores or 154 // volatile loads, but may read from any memory location. 155 // 156 // This property corresponds to the GCC 'pure' attribute. 157 OnlyReadsMemory, 158 159 // UnknownModRefBehavior - This indicates that the function could not be 160 // classified into one of the behaviors above. 161 UnknownModRefBehavior 162 }; 163 164 /// getModRefBehavior - Return the behavior when calling the given call site. 165 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); 166 167 /// getModRefBehavior - Return the behavior when calling the given function. 168 /// For use when the call site is not known. 169 virtual ModRefBehavior getModRefBehavior(const Function *F); 170 171 /// getIntrinsicModRefBehavior - Return the modref behavior of the intrinsic 172 /// with the given id. Most clients won't need this, because the regular 173 /// getModRefBehavior incorporates this information. 174 static ModRefBehavior getIntrinsicModRefBehavior(unsigned iid); 175 176 /// doesNotAccessMemory - If the specified call is known to never read or 177 /// write memory, return true. If the call only reads from known-constant 178 /// memory, it is also legal to return true. Calls that unwind the stack 179 /// are legal for this predicate. 180 /// 181 /// Many optimizations (such as CSE and LICM) can be performed on such calls 182 /// without worrying about aliasing properties, and many calls have this 183 /// property (e.g. calls to 'sin' and 'cos'). 184 /// 185 /// This property corresponds to the GCC 'const' attribute. 186 /// 187 bool doesNotAccessMemory(ImmutableCallSite CS) { 188 return getModRefBehavior(CS) == DoesNotAccessMemory; 189 } 190 191 /// doesNotAccessMemory - If the specified function is known to never read or 192 /// write memory, return true. For use when the call site is not known. 193 /// 194 bool doesNotAccessMemory(const Function *F) { 195 return getModRefBehavior(F) == DoesNotAccessMemory; 196 } 197 198 /// onlyReadsMemory - If the specified call is known to only read from 199 /// non-volatile memory (or not access memory at all), return true. Calls 200 /// that unwind the stack are legal for this predicate. 201 /// 202 /// This property allows many common optimizations to be performed in the 203 /// absence of interfering store instructions, such as CSE of strlen calls. 204 /// 205 /// This property corresponds to the GCC 'pure' attribute. 206 /// 207 bool onlyReadsMemory(ImmutableCallSite CS) { 208 ModRefBehavior MRB = getModRefBehavior(CS); 209 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 210 } 211 212 /// onlyReadsMemory - If the specified function is known to only read from 213 /// non-volatile memory (or not access memory at all), return true. For use 214 /// when the call site is not known. 215 /// 216 bool onlyReadsMemory(const Function *F) { 217 ModRefBehavior MRB = getModRefBehavior(F); 218 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 219 } 220 221 222 /// getModRefInfo - Return information about whether or not an instruction may 223 /// read or write memory specified by the pointer operand. An instruction 224 /// that doesn't read or write memory may be trivially LICM'd for example. 225 ModRefResult getModRefInfo(const Instruction *I, 226 const Value *P, unsigned Size) { 227 switch (I->getOpcode()) { 228 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, P,Size); 229 case Instruction::Load: return getModRefInfo((const LoadInst*)I, P, Size); 230 case Instruction::Store: return getModRefInfo((const StoreInst*)I, P,Size); 231 case Instruction::Call: return getModRefInfo((const CallInst*)I, P, Size); 232 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,P,Size); 233 default: return NoModRef; 234 } 235 } 236 237 /// getModRefInfo (for call sites) - Return whether information about whether 238 /// a particular call site modifies or reads the memory specified by the 239 /// pointer. 240 virtual ModRefResult getModRefInfo(ImmutableCallSite CS, 241 const Value *P, unsigned Size); 242 243 /// getModRefInfo (for calls) - Return whether information about whether 244 /// a particular call modifies or reads the memory specified by the 245 /// pointer. 246 ModRefResult getModRefInfo(const CallInst *C, const Value *P, unsigned Size) { 247 return getModRefInfo(ImmutableCallSite(C), P, Size); 248 } 249 250 /// getModRefInfo (for invokes) - Return whether information about whether 251 /// a particular invoke modifies or reads the memory specified by the 252 /// pointer. 253 ModRefResult getModRefInfo(const InvokeInst *I, 254 const Value *P, unsigned Size) { 255 return getModRefInfo(ImmutableCallSite(I), P, Size); 256 } 257 258 /// getModRefInfo (for loads) - Return whether information about whether 259 /// a particular load modifies or reads the memory specified by the 260 /// pointer. 261 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size); 262 263 /// getModRefInfo (for stores) - Return whether information about whether 264 /// a particular store modifies or reads the memory specified by the 265 /// pointer. 266 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size); 267 268 /// getModRefInfo (for va_args) - Return whether information about whether 269 /// a particular va_arg modifies or reads the memory specified by the 270 /// pointer. 271 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, unsigned Size); 272 273 /// getModRefInfo - Return information about whether two call sites may refer 274 /// to the same set of memory locations. See 275 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo 276 /// for details. 277 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, 278 ImmutableCallSite CS2); 279 280 //===--------------------------------------------------------------------===// 281 /// Dependence queries. 282 /// 283 284 /// DependenceResult - These are the return values for getDependence queries. 285 /// They are defined in terms of "memory", but they are also used to model 286 /// other side effects, such as I/O and volatility. 287 enum DependenceResult { 288 /// ReadThenRead - The instructions are ReadThenReadSome and the second 289 /// instruction reads from exactly the same memory read from by the first. 290 ReadThenRead, 291 292 /// ReadThenReadSome - The instructions are Independent, both are read-only, 293 /// and the second instruction reads from a subset of the memory read from 294 /// by the first. 295 ReadThenReadSome, 296 297 /// Independent - Neither instruction reads from or writes to memory written 298 /// to by the other. All enum values lower than this one are special cases 299 /// of Indepenent. 300 Independent, 301 302 /// WriteThenRead - The instructions are WriteThenReadSome and the second 303 /// instruction reads from exactly the same memory written by the first. 304 WriteThenRead, 305 306 /// WriteThenReadSome - The first instruction is write-only, the second 307 /// instruction is read-only, and the second only reads from memory 308 /// written to by the first. 309 WriteThenReadSome, 310 311 /// ReadThenWrite - The first instruction is read-only, the second 312 /// instruction is write-only, and the second wrotes to exactly the 313 /// same memory read from by the first. 314 ReadThenWrite, 315 316 /// WriteThenWrite - The instructions are WriteThenWriteSome, and the 317 /// second instruction writes to exactly the same memory written to by 318 /// the first. 319 WriteThenWrite, 320 321 /// WriteSomeThenWrite - Both instructions are write-only, and the second 322 /// instruction writes to a superset of the memory written to by the first. 323 WriteSomeThenWrite, 324 325 /// Unknown - The relationship between the instructions cannot be 326 /// determined or does not fit into any of the cases defined here. 327 Unknown 328 }; 329 330 /// DependenceQueryFlags - Flags for refining dependence queries. 331 enum DependenceQueryFlags { 332 Default = 0, 333 IgnoreLoads = 1, 334 IgnoreStores = 2 335 }; 336 337 /// getDependence - Determine the dependence relationship between the 338 /// instructions. This does not include "register" dependencies; it just 339 /// considers memory references and other side effects. 340 /// WARNING: This is an experimental interface. 341 DependenceResult getDependence(const Instruction *First, 342 const Instruction *Second) { 343 return getDependence(First, 0, Default, Second, 0, Default); 344 } 345 346 /// getDependence - Determine the dependence relationship between the 347 /// instructions. This does not include "register" dependencies; it just 348 /// considers memory references and other side effects. This overload 349 /// has additional parameters to allow phi-translated addresses to be 350 /// specified, and additional flags to refine the query. 351 /// WARNING: This is an experimental interface. 352 virtual DependenceResult getDependence(const Instruction *First, 353 const Value *FirstPHITranslatedAddr, 354 DependenceQueryFlags FirstFlags, 355 const Instruction *Second, 356 const Value *SecondPHITranslatedAddr, 357 DependenceQueryFlags SecondFlags); 358 359 //===--------------------------------------------------------------------===// 360 /// Higher level methods for querying mod/ref information. 361 /// 362 363 /// canBasicBlockModify - Return true if it is possible for execution of the 364 /// specified basic block to modify the value pointed to by Ptr. 365 /// 366 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size); 367 368 /// canInstructionRangeModify - Return true if it is possible for the 369 /// execution of the specified instructions to modify the value pointed to by 370 /// Ptr. The instructions to consider are all of the instructions in the 371 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 372 /// 373 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 374 const Value *Ptr, unsigned Size); 375 376 //===--------------------------------------------------------------------===// 377 /// Methods that clients should call when they transform the program to allow 378 /// alias analyses to update their internal data structures. Note that these 379 /// methods may be called on any instruction, regardless of whether or not 380 /// they have pointer-analysis implications. 381 /// 382 383 /// deleteValue - This method should be called whenever an LLVM Value is 384 /// deleted from the program, for example when an instruction is found to be 385 /// redundant and is eliminated. 386 /// 387 virtual void deleteValue(Value *V); 388 389 /// copyValue - This method should be used whenever a preexisting value in the 390 /// program is copied or cloned, introducing a new value. Note that analysis 391 /// implementations should tolerate clients that use this method to introduce 392 /// the same value multiple times: if the analysis already knows about a 393 /// value, it should ignore the request. 394 /// 395 virtual void copyValue(Value *From, Value *To); 396 397 /// replaceWithNewValue - This method is the obvious combination of the two 398 /// above, and it provided as a helper to simplify client code. 399 /// 400 void replaceWithNewValue(Value *Old, Value *New) { 401 copyValue(Old, New); 402 deleteValue(Old); 403 } 404 405protected: 406 /// getDependenceViaModRefInfo - Helper function for implementing getDependence 407 /// in implementations which already have getModRefInfo implementations. 408 DependenceResult getDependenceViaModRefInfo(const Instruction *First, 409 const Value *FirstPHITranslatedAddr, 410 DependenceQueryFlags FirstFlags, 411 const Instruction *Second, 412 const Value *SecondPHITranslatedAddr, 413 DependenceQueryFlags SecondFlags); 414 415}; 416 417/// isNoAliasCall - Return true if this pointer is returned by a noalias 418/// function. 419bool isNoAliasCall(const Value *V); 420 421/// isIdentifiedObject - Return true if this pointer refers to a distinct and 422/// identifiable object. This returns true for: 423/// Global Variables and Functions (but not Global Aliases) 424/// Allocas and Mallocs 425/// ByVal and NoAlias Arguments 426/// NoAlias returns 427/// 428bool isIdentifiedObject(const Value *V); 429 430} // End llvm namespace 431 432// Because of the way .a files work, we must force the BasicAA implementation to 433// be pulled in if the AliasAnalysis header is included. Otherwise we run 434// the risk of AliasAnalysis being used, but the default implementation not 435// being linked into the tool that uses it. 436FORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis) 437FORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis) 438 439#endif 440