1 //===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the generic AliasAnalysis interface, which is used as the 10 // common interface used by all clients of alias analysis information, and 11 // implemented by all alias analysis implementations. Mod/Ref information is 12 // also captured by this interface. 13 // 14 // Implementations of this interface must implement the various virtual methods, 15 // which automatically provides functionality for the entire suite of client 16 // APIs. 17 // 18 // This API identifies memory regions with the MemoryLocation class. The pointer 19 // component specifies the base memory address of the region. The Size specifies 20 // the maximum size (in address units) of the memory region, or 21 // MemoryLocation::UnknownSize if the size is not known. The TBAA tag 22 // identifies the "type" of the memory reference; see the 23 // TypeBasedAliasAnalysis class for details. 24 // 25 // Some non-obvious details include: 26 // - Pointers that point to two completely different objects in memory never 27 // alias, regardless of the value of the Size component. 28 // - NoAlias doesn't imply inequal pointers. The most obvious example of this 29 // is two pointers to constant memory. Even if they are equal, constant 30 // memory is never stored to, so there will never be any dependencies. 31 // In this and other situations, the pointers may be both NoAlias and 32 // MustAlias at the same time. The current API can only return one result, 33 // though this is rarely a problem in practice. 34 // 35 //===----------------------------------------------------------------------===// 36 37 #ifndef LLVM_ANALYSIS_ALIASANALYSIS_H 38 #define LLVM_ANALYSIS_ALIASANALYSIS_H 39 40 #include "llvm/ADT/DenseMap.h" 41 #include "llvm/ADT/None.h" 42 #include "llvm/ADT/Optional.h" 43 #include "llvm/ADT/SmallVector.h" 44 #include "llvm/Analysis/MemoryLocation.h" 45 #include "llvm/IR/PassManager.h" 46 #include "llvm/Pass.h" 47 #include <cstdint> 48 #include <functional> 49 #include <memory> 50 #include <vector> 51 52 namespace llvm { 53 54 class AnalysisUsage; 55 class AtomicCmpXchgInst; 56 class BasicAAResult; 57 class BasicBlock; 58 class CatchPadInst; 59 class CatchReturnInst; 60 class DominatorTree; 61 class FenceInst; 62 class Function; 63 class InvokeInst; 64 class PreservedAnalyses; 65 class TargetLibraryInfo; 66 class Value; 67 68 /// The possible results of an alias query. 69 /// 70 /// These results are always computed between two MemoryLocation objects as 71 /// a query to some alias analysis. 72 /// 73 /// Note that these are unscoped enumerations because we would like to support 74 /// implicitly testing a result for the existence of any possible aliasing with 75 /// a conversion to bool, but an "enum class" doesn't support this. The 76 /// canonical names from the literature are suffixed and unique anyways, and so 77 /// they serve as global constants in LLVM for these results. 78 /// 79 /// See docs/AliasAnalysis.html for more information on the specific meanings 80 /// of these values. 81 enum AliasResult : uint8_t { 82 /// The two locations do not alias at all. 83 /// 84 /// This value is arranged to convert to false, while all other values 85 /// convert to true. This allows a boolean context to convert the result to 86 /// a binary flag indicating whether there is the possibility of aliasing. 87 NoAlias = 0, 88 /// The two locations may or may not alias. This is the least precise result. 89 MayAlias, 90 /// The two locations alias, but only due to a partial overlap. 91 PartialAlias, 92 /// The two locations precisely alias each other. 93 MustAlias, 94 }; 95 96 /// << operator for AliasResult. 97 raw_ostream &operator<<(raw_ostream &OS, AliasResult AR); 98 99 /// Flags indicating whether a memory access modifies or references memory. 100 /// 101 /// This is no access at all, a modification, a reference, or both 102 /// a modification and a reference. These are specifically structured such that 103 /// they form a three bit matrix and bit-tests for 'mod' or 'ref' or 'must' 104 /// work with any of the possible values. 105 enum class ModRefInfo : uint8_t { 106 /// Must is provided for completeness, but no routines will return only 107 /// Must today. See definition of Must below. 108 Must = 0, 109 /// The access may reference the value stored in memory, 110 /// a mustAlias relation was found, and no mayAlias or partialAlias found. 111 MustRef = 1, 112 /// The access may modify the value stored in memory, 113 /// a mustAlias relation was found, and no mayAlias or partialAlias found. 114 MustMod = 2, 115 /// The access may reference, modify or both the value stored in memory, 116 /// a mustAlias relation was found, and no mayAlias or partialAlias found. 117 MustModRef = MustRef | MustMod, 118 /// The access neither references nor modifies the value stored in memory. 119 NoModRef = 4, 120 /// The access may reference the value stored in memory. 121 Ref = NoModRef | MustRef, 122 /// The access may modify the value stored in memory. 123 Mod = NoModRef | MustMod, 124 /// The access may reference and may modify the value stored in memory. 125 ModRef = Ref | Mod, 126 127 /// About Must: 128 /// Must is set in a best effort manner. 129 /// We usually do not try our best to infer Must, instead it is merely 130 /// another piece of "free" information that is presented when available. 131 /// Must set means there was certainly a MustAlias found. For calls, 132 /// where multiple arguments are checked (argmemonly), this translates to 133 /// only MustAlias or NoAlias was found. 134 /// Must is not set for RAR accesses, even if the two locations must 135 /// alias. The reason is that two read accesses translate to an early return 136 /// of NoModRef. An additional alias check to set Must may be 137 /// expensive. Other cases may also not set Must(e.g. callCapturesBefore). 138 /// We refer to Must being *set* when the most significant bit is *cleared*. 139 /// Conversely we *clear* Must information by *setting* the Must bit to 1. 140 }; 141 142 LLVM_NODISCARD inline bool isNoModRef(const ModRefInfo MRI) { 143 return (static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef)) == 144 static_cast<int>(ModRefInfo::Must); 145 } 146 LLVM_NODISCARD inline bool isModOrRefSet(const ModRefInfo MRI) { 147 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef); 148 } 149 LLVM_NODISCARD inline bool isModAndRefSet(const ModRefInfo MRI) { 150 return (static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustModRef)) == 151 static_cast<int>(ModRefInfo::MustModRef); 152 } 153 LLVM_NODISCARD inline bool isModSet(const ModRefInfo MRI) { 154 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustMod); 155 } 156 LLVM_NODISCARD inline bool isRefSet(const ModRefInfo MRI) { 157 return static_cast<int>(MRI) & static_cast<int>(ModRefInfo::MustRef); 158 } 159 LLVM_NODISCARD inline bool isMustSet(const ModRefInfo MRI) { 160 return !(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::NoModRef)); 161 } 162 163 LLVM_NODISCARD inline ModRefInfo setMod(const ModRefInfo MRI) { 164 return ModRefInfo(static_cast<int>(MRI) | 165 static_cast<int>(ModRefInfo::MustMod)); 166 } 167 LLVM_NODISCARD inline ModRefInfo setRef(const ModRefInfo MRI) { 168 return ModRefInfo(static_cast<int>(MRI) | 169 static_cast<int>(ModRefInfo::MustRef)); 170 } 171 LLVM_NODISCARD inline ModRefInfo setMust(const ModRefInfo MRI) { 172 return ModRefInfo(static_cast<int>(MRI) & 173 static_cast<int>(ModRefInfo::MustModRef)); 174 } 175 LLVM_NODISCARD inline ModRefInfo setModAndRef(const ModRefInfo MRI) { 176 return ModRefInfo(static_cast<int>(MRI) | 177 static_cast<int>(ModRefInfo::MustModRef)); 178 } 179 LLVM_NODISCARD inline ModRefInfo clearMod(const ModRefInfo MRI) { 180 return ModRefInfo(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Ref)); 181 } 182 LLVM_NODISCARD inline ModRefInfo clearRef(const ModRefInfo MRI) { 183 return ModRefInfo(static_cast<int>(MRI) & static_cast<int>(ModRefInfo::Mod)); 184 } 185 LLVM_NODISCARD inline ModRefInfo clearMust(const ModRefInfo MRI) { 186 return ModRefInfo(static_cast<int>(MRI) | 187 static_cast<int>(ModRefInfo::NoModRef)); 188 } 189 LLVM_NODISCARD inline ModRefInfo unionModRef(const ModRefInfo MRI1, 190 const ModRefInfo MRI2) { 191 return ModRefInfo(static_cast<int>(MRI1) | static_cast<int>(MRI2)); 192 } 193 LLVM_NODISCARD inline ModRefInfo intersectModRef(const ModRefInfo MRI1, 194 const ModRefInfo MRI2) { 195 return ModRefInfo(static_cast<int>(MRI1) & static_cast<int>(MRI2)); 196 } 197 198 /// The locations at which a function might access memory. 199 /// 200 /// These are primarily used in conjunction with the \c AccessKind bits to 201 /// describe both the nature of access and the locations of access for a 202 /// function call. 203 enum FunctionModRefLocation { 204 /// Base case is no access to memory. 205 FMRL_Nowhere = 0, 206 /// Access to memory via argument pointers. 207 FMRL_ArgumentPointees = 8, 208 /// Memory that is inaccessible via LLVM IR. 209 FMRL_InaccessibleMem = 16, 210 /// Access to any memory. 211 FMRL_Anywhere = 32 | FMRL_InaccessibleMem | FMRL_ArgumentPointees 212 }; 213 214 /// Summary of how a function affects memory in the program. 215 /// 216 /// Loads from constant globals are not considered memory accesses for this 217 /// interface. Also, functions may freely modify stack space local to their 218 /// invocation without having to report it through these interfaces. 219 enum FunctionModRefBehavior { 220 /// This function does not perform any non-local loads or stores to memory. 221 /// 222 /// This property corresponds to the GCC 'const' attribute. 223 /// This property corresponds to the LLVM IR 'readnone' attribute. 224 /// This property corresponds to the IntrNoMem LLVM intrinsic flag. 225 FMRB_DoesNotAccessMemory = 226 FMRL_Nowhere | static_cast<int>(ModRefInfo::NoModRef), 227 228 /// The only memory references in this function (if it has any) are 229 /// non-volatile loads from objects pointed to by its pointer-typed 230 /// arguments, with arbitrary offsets. 231 /// 232 /// This property corresponds to the combination of the IntrReadMem 233 /// and IntrArgMemOnly LLVM intrinsic flags. 234 FMRB_OnlyReadsArgumentPointees = 235 FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::Ref), 236 237 /// The only memory references in this function (if it has any) are 238 /// non-volatile stores from objects pointed to by its pointer-typed 239 /// arguments, with arbitrary offsets. 240 /// 241 /// This property corresponds to the combination of the IntrWriteMem 242 /// and IntrArgMemOnly LLVM intrinsic flags. 243 FMRB_OnlyWritesArgumentPointees = 244 FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::Mod), 245 246 /// The only memory references in this function (if it has any) are 247 /// non-volatile loads and stores from objects pointed to by its 248 /// pointer-typed arguments, with arbitrary offsets. 249 /// 250 /// This property corresponds to the IntrArgMemOnly LLVM intrinsic flag. 251 FMRB_OnlyAccessesArgumentPointees = 252 FMRL_ArgumentPointees | static_cast<int>(ModRefInfo::ModRef), 253 254 /// The only memory references in this function (if it has any) are 255 /// reads of memory that is otherwise inaccessible via LLVM IR. 256 /// 257 /// This property corresponds to the LLVM IR inaccessiblememonly attribute. 258 FMRB_OnlyReadsInaccessibleMem = 259 FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::Ref), 260 261 /// The only memory references in this function (if it has any) are 262 /// writes to memory that is otherwise inaccessible via LLVM IR. 263 /// 264 /// This property corresponds to the LLVM IR inaccessiblememonly attribute. 265 FMRB_OnlyWritesInaccessibleMem = 266 FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::Mod), 267 268 /// The only memory references in this function (if it has any) are 269 /// references of memory that is otherwise inaccessible via LLVM IR. 270 /// 271 /// This property corresponds to the LLVM IR inaccessiblememonly attribute. 272 FMRB_OnlyAccessesInaccessibleMem = 273 FMRL_InaccessibleMem | static_cast<int>(ModRefInfo::ModRef), 274 275 /// The function may perform non-volatile loads from objects pointed 276 /// to by its pointer-typed arguments, with arbitrary offsets, and 277 /// it may also perform loads of memory that is otherwise 278 /// inaccessible via LLVM IR. 279 /// 280 /// This property corresponds to the LLVM IR 281 /// inaccessiblemem_or_argmemonly attribute. 282 FMRB_OnlyReadsInaccessibleOrArgMem = FMRL_InaccessibleMem | 283 FMRL_ArgumentPointees | 284 static_cast<int>(ModRefInfo::Ref), 285 286 /// The function may perform non-volatile stores to objects pointed 287 /// to by its pointer-typed arguments, with arbitrary offsets, and 288 /// it may also perform stores of memory that is otherwise 289 /// inaccessible via LLVM IR. 290 /// 291 /// This property corresponds to the LLVM IR 292 /// inaccessiblemem_or_argmemonly attribute. 293 FMRB_OnlyWritesInaccessibleOrArgMem = FMRL_InaccessibleMem | 294 FMRL_ArgumentPointees | 295 static_cast<int>(ModRefInfo::Mod), 296 297 /// The function may perform non-volatile loads and stores of objects 298 /// pointed to by its pointer-typed arguments, with arbitrary offsets, and 299 /// it may also perform loads and stores of memory that is otherwise 300 /// inaccessible via LLVM IR. 301 /// 302 /// This property corresponds to the LLVM IR 303 /// inaccessiblemem_or_argmemonly attribute. 304 FMRB_OnlyAccessesInaccessibleOrArgMem = FMRL_InaccessibleMem | 305 FMRL_ArgumentPointees | 306 static_cast<int>(ModRefInfo::ModRef), 307 308 /// This function does not perform any non-local stores or volatile loads, 309 /// but may read from any memory location. 310 /// 311 /// This property corresponds to the GCC 'pure' attribute. 312 /// This property corresponds to the LLVM IR 'readonly' attribute. 313 /// This property corresponds to the IntrReadMem LLVM intrinsic flag. 314 FMRB_OnlyReadsMemory = FMRL_Anywhere | static_cast<int>(ModRefInfo::Ref), 315 316 // This function does not read from memory anywhere, but may write to any 317 // memory location. 318 // 319 // This property corresponds to the LLVM IR 'writeonly' attribute. 320 // This property corresponds to the IntrWriteMem LLVM intrinsic flag. 321 FMRB_OnlyWritesMemory = FMRL_Anywhere | static_cast<int>(ModRefInfo::Mod), 322 323 /// This indicates that the function could not be classified into one of the 324 /// behaviors above. 325 FMRB_UnknownModRefBehavior = 326 FMRL_Anywhere | static_cast<int>(ModRefInfo::ModRef) 327 }; 328 329 // Wrapper method strips bits significant only in FunctionModRefBehavior, 330 // to obtain a valid ModRefInfo. The benefit of using the wrapper is that if 331 // ModRefInfo enum changes, the wrapper can be updated to & with the new enum 332 // entry with all bits set to 1. 333 LLVM_NODISCARD inline ModRefInfo 334 createModRefInfo(const FunctionModRefBehavior FMRB) { 335 return ModRefInfo(FMRB & static_cast<int>(ModRefInfo::ModRef)); 336 } 337 338 /// This class stores info we want to provide to or retain within an alias 339 /// query. By default, the root query is stateless and starts with a freshly 340 /// constructed info object. Specific alias analyses can use this query info to 341 /// store per-query state that is important for recursive or nested queries to 342 /// avoid recomputing. To enable preserving this state across multiple queries 343 /// where safe (due to the IR not changing), use a `BatchAAResults` wrapper. 344 /// The information stored in an `AAQueryInfo` is currently limitted to the 345 /// caches used by BasicAA, but can further be extended to fit other AA needs. 346 class AAQueryInfo { 347 public: 348 using LocPair = std::pair<MemoryLocation, MemoryLocation>; 349 struct CacheEntry { 350 AliasResult Result; 351 /// Number of times a NoAlias assumption has been used. 352 /// 0 for assumptions that have not been used, -1 for definitive results. 353 int NumAssumptionUses; 354 /// Whether this is a definitive (non-assumption) result. 355 bool isDefinitive() const { return NumAssumptionUses < 0; } 356 }; 357 using AliasCacheT = SmallDenseMap<LocPair, CacheEntry, 8>; 358 AliasCacheT AliasCache; 359 360 using IsCapturedCacheT = SmallDenseMap<const Value *, bool, 8>; 361 IsCapturedCacheT IsCapturedCache; 362 363 /// How many active NoAlias assumption uses there are. 364 int NumAssumptionUses = 0; 365 366 /// Location pairs for which an assumption based result is currently stored. 367 /// Used to remove all potentially incorrect results from the cache if an 368 /// assumption is disproven. 369 SmallVector<AAQueryInfo::LocPair, 4> AssumptionBasedResults; 370 371 AAQueryInfo() : AliasCache(), IsCapturedCache() {} 372 }; 373 374 class BatchAAResults; 375 376 class AAResults { 377 public: 378 // Make these results default constructable and movable. We have to spell 379 // these out because MSVC won't synthesize them. 380 AAResults(const TargetLibraryInfo &TLI) : TLI(TLI) {} 381 AAResults(AAResults &&Arg); 382 ~AAResults(); 383 384 /// Register a specific AA result. 385 template <typename AAResultT> void addAAResult(AAResultT &AAResult) { 386 // FIXME: We should use a much lighter weight system than the usual 387 // polymorphic pattern because we don't own AAResult. It should 388 // ideally involve two pointers and no separate allocation. 389 AAs.emplace_back(new Model<AAResultT>(AAResult, *this)); 390 } 391 392 /// Register a function analysis ID that the results aggregation depends on. 393 /// 394 /// This is used in the new pass manager to implement the invalidation logic 395 /// where we must invalidate the results aggregation if any of our component 396 /// analyses become invalid. 397 void addAADependencyID(AnalysisKey *ID) { AADeps.push_back(ID); } 398 399 /// Handle invalidation events in the new pass manager. 400 /// 401 /// The aggregation is invalidated if any of the underlying analyses is 402 /// invalidated. 403 bool invalidate(Function &F, const PreservedAnalyses &PA, 404 FunctionAnalysisManager::Invalidator &Inv); 405 406 //===--------------------------------------------------------------------===// 407 /// \name Alias Queries 408 /// @{ 409 410 /// The main low level interface to the alias analysis implementation. 411 /// Returns an AliasResult indicating whether the two pointers are aliased to 412 /// each other. This is the interface that must be implemented by specific 413 /// alias analysis implementations. 414 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB); 415 416 /// A convenience wrapper around the primary \c alias interface. 417 AliasResult alias(const Value *V1, LocationSize V1Size, const Value *V2, 418 LocationSize V2Size) { 419 return alias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size)); 420 } 421 422 /// A convenience wrapper around the primary \c alias interface. 423 AliasResult alias(const Value *V1, const Value *V2) { 424 return alias(MemoryLocation::getBeforeOrAfter(V1), 425 MemoryLocation::getBeforeOrAfter(V2)); 426 } 427 428 /// A trivial helper function to check to see if the specified pointers are 429 /// no-alias. 430 bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) { 431 return alias(LocA, LocB) == NoAlias; 432 } 433 434 /// A convenience wrapper around the \c isNoAlias helper interface. 435 bool isNoAlias(const Value *V1, LocationSize V1Size, const Value *V2, 436 LocationSize V2Size) { 437 return isNoAlias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size)); 438 } 439 440 /// A convenience wrapper around the \c isNoAlias helper interface. 441 bool isNoAlias(const Value *V1, const Value *V2) { 442 return isNoAlias(MemoryLocation::getBeforeOrAfter(V1), 443 MemoryLocation::getBeforeOrAfter(V2)); 444 } 445 446 /// A trivial helper function to check to see if the specified pointers are 447 /// must-alias. 448 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) { 449 return alias(LocA, LocB) == MustAlias; 450 } 451 452 /// A convenience wrapper around the \c isMustAlias helper interface. 453 bool isMustAlias(const Value *V1, const Value *V2) { 454 return alias(V1, LocationSize::precise(1), V2, LocationSize::precise(1)) == 455 MustAlias; 456 } 457 458 /// Checks whether the given location points to constant memory, or if 459 /// \p OrLocal is true whether it points to a local alloca. 460 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal = false); 461 462 /// A convenience wrapper around the primary \c pointsToConstantMemory 463 /// interface. 464 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) { 465 return pointsToConstantMemory(MemoryLocation::getBeforeOrAfter(P), OrLocal); 466 } 467 468 /// @} 469 //===--------------------------------------------------------------------===// 470 /// \name Simple mod/ref information 471 /// @{ 472 473 /// Get the ModRef info associated with a pointer argument of a call. The 474 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note 475 /// that these bits do not necessarily account for the overall behavior of 476 /// the function, but rather only provide additional per-argument 477 /// information. This never sets ModRefInfo::Must. 478 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx); 479 480 /// Return the behavior of the given call site. 481 FunctionModRefBehavior getModRefBehavior(const CallBase *Call); 482 483 /// Return the behavior when calling the given function. 484 FunctionModRefBehavior getModRefBehavior(const Function *F); 485 486 /// Checks if the specified call is known to never read or write memory. 487 /// 488 /// Note that if the call only reads from known-constant memory, it is also 489 /// legal to return true. Also, calls that unwind the stack are legal for 490 /// this predicate. 491 /// 492 /// Many optimizations (such as CSE and LICM) can be performed on such calls 493 /// without worrying about aliasing properties, and many calls have this 494 /// property (e.g. calls to 'sin' and 'cos'). 495 /// 496 /// This property corresponds to the GCC 'const' attribute. 497 bool doesNotAccessMemory(const CallBase *Call) { 498 return getModRefBehavior(Call) == FMRB_DoesNotAccessMemory; 499 } 500 501 /// Checks if the specified function is known to never read or write memory. 502 /// 503 /// Note that if the function only reads from known-constant memory, it is 504 /// also legal to return true. Also, function that unwind the stack are legal 505 /// for this predicate. 506 /// 507 /// Many optimizations (such as CSE and LICM) can be performed on such calls 508 /// to such functions without worrying about aliasing properties, and many 509 /// functions have this property (e.g. 'sin' and 'cos'). 510 /// 511 /// This property corresponds to the GCC 'const' attribute. 512 bool doesNotAccessMemory(const Function *F) { 513 return getModRefBehavior(F) == FMRB_DoesNotAccessMemory; 514 } 515 516 /// Checks if the specified call is known to only read from non-volatile 517 /// memory (or not access memory at all). 518 /// 519 /// Calls that unwind the stack are legal for this predicate. 520 /// 521 /// This property allows many common optimizations to be performed in the 522 /// absence of interfering store instructions, such as CSE of strlen calls. 523 /// 524 /// This property corresponds to the GCC 'pure' attribute. 525 bool onlyReadsMemory(const CallBase *Call) { 526 return onlyReadsMemory(getModRefBehavior(Call)); 527 } 528 529 /// Checks if the specified function is known to only read from non-volatile 530 /// memory (or not access memory at all). 531 /// 532 /// Functions that unwind the stack are legal for this predicate. 533 /// 534 /// This property allows many common optimizations to be performed in the 535 /// absence of interfering store instructions, such as CSE of strlen calls. 536 /// 537 /// This property corresponds to the GCC 'pure' attribute. 538 bool onlyReadsMemory(const Function *F) { 539 return onlyReadsMemory(getModRefBehavior(F)); 540 } 541 542 /// Checks if functions with the specified behavior are known to only read 543 /// from non-volatile memory (or not access memory at all). 544 static bool onlyReadsMemory(FunctionModRefBehavior MRB) { 545 return !isModSet(createModRefInfo(MRB)); 546 } 547 548 /// Checks if functions with the specified behavior are known to only write 549 /// memory (or not access memory at all). 550 static bool doesNotReadMemory(FunctionModRefBehavior MRB) { 551 return !isRefSet(createModRefInfo(MRB)); 552 } 553 554 /// Checks if functions with the specified behavior are known to read and 555 /// write at most from objects pointed to by their pointer-typed arguments 556 /// (with arbitrary offsets). 557 static bool onlyAccessesArgPointees(FunctionModRefBehavior MRB) { 558 return !((unsigned)MRB & FMRL_Anywhere & ~FMRL_ArgumentPointees); 559 } 560 561 /// Checks if functions with the specified behavior are known to potentially 562 /// read or write from objects pointed to be their pointer-typed arguments 563 /// (with arbitrary offsets). 564 static bool doesAccessArgPointees(FunctionModRefBehavior MRB) { 565 return isModOrRefSet(createModRefInfo(MRB)) && 566 ((unsigned)MRB & FMRL_ArgumentPointees); 567 } 568 569 /// Checks if functions with the specified behavior are known to read and 570 /// write at most from memory that is inaccessible from LLVM IR. 571 static bool onlyAccessesInaccessibleMem(FunctionModRefBehavior MRB) { 572 return !((unsigned)MRB & FMRL_Anywhere & ~FMRL_InaccessibleMem); 573 } 574 575 /// Checks if functions with the specified behavior are known to potentially 576 /// read or write from memory that is inaccessible from LLVM IR. 577 static bool doesAccessInaccessibleMem(FunctionModRefBehavior MRB) { 578 return isModOrRefSet(createModRefInfo(MRB)) && 579 ((unsigned)MRB & FMRL_InaccessibleMem); 580 } 581 582 /// Checks if functions with the specified behavior are known to read and 583 /// write at most from memory that is inaccessible from LLVM IR or objects 584 /// pointed to by their pointer-typed arguments (with arbitrary offsets). 585 static bool onlyAccessesInaccessibleOrArgMem(FunctionModRefBehavior MRB) { 586 return !((unsigned)MRB & FMRL_Anywhere & 587 ~(FMRL_InaccessibleMem | FMRL_ArgumentPointees)); 588 } 589 590 /// getModRefInfo (for call sites) - Return information about whether 591 /// a particular call site modifies or reads the specified memory location. 592 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc); 593 594 /// getModRefInfo (for call sites) - A convenience wrapper. 595 ModRefInfo getModRefInfo(const CallBase *Call, const Value *P, 596 LocationSize Size) { 597 return getModRefInfo(Call, MemoryLocation(P, Size)); 598 } 599 600 /// getModRefInfo (for loads) - Return information about whether 601 /// a particular load modifies or reads the specified memory location. 602 ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc); 603 604 /// getModRefInfo (for loads) - A convenience wrapper. 605 ModRefInfo getModRefInfo(const LoadInst *L, const Value *P, 606 LocationSize Size) { 607 return getModRefInfo(L, MemoryLocation(P, Size)); 608 } 609 610 /// getModRefInfo (for stores) - Return information about whether 611 /// a particular store modifies or reads the specified memory location. 612 ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc); 613 614 /// getModRefInfo (for stores) - A convenience wrapper. 615 ModRefInfo getModRefInfo(const StoreInst *S, const Value *P, 616 LocationSize Size) { 617 return getModRefInfo(S, MemoryLocation(P, Size)); 618 } 619 620 /// getModRefInfo (for fences) - Return information about whether 621 /// a particular store modifies or reads the specified memory location. 622 ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc); 623 624 /// getModRefInfo (for fences) - A convenience wrapper. 625 ModRefInfo getModRefInfo(const FenceInst *S, const Value *P, 626 LocationSize Size) { 627 return getModRefInfo(S, MemoryLocation(P, Size)); 628 } 629 630 /// getModRefInfo (for cmpxchges) - Return information about whether 631 /// a particular cmpxchg modifies or reads the specified memory location. 632 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, 633 const MemoryLocation &Loc); 634 635 /// getModRefInfo (for cmpxchges) - A convenience wrapper. 636 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, const Value *P, 637 LocationSize Size) { 638 return getModRefInfo(CX, MemoryLocation(P, Size)); 639 } 640 641 /// getModRefInfo (for atomicrmws) - Return information about whether 642 /// a particular atomicrmw modifies or reads the specified memory location. 643 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc); 644 645 /// getModRefInfo (for atomicrmws) - A convenience wrapper. 646 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const Value *P, 647 LocationSize Size) { 648 return getModRefInfo(RMW, MemoryLocation(P, Size)); 649 } 650 651 /// getModRefInfo (for va_args) - Return information about whether 652 /// a particular va_arg modifies or reads the specified memory location. 653 ModRefInfo getModRefInfo(const VAArgInst *I, const MemoryLocation &Loc); 654 655 /// getModRefInfo (for va_args) - A convenience wrapper. 656 ModRefInfo getModRefInfo(const VAArgInst *I, const Value *P, 657 LocationSize Size) { 658 return getModRefInfo(I, MemoryLocation(P, Size)); 659 } 660 661 /// getModRefInfo (for catchpads) - Return information about whether 662 /// a particular catchpad modifies or reads the specified memory location. 663 ModRefInfo getModRefInfo(const CatchPadInst *I, const MemoryLocation &Loc); 664 665 /// getModRefInfo (for catchpads) - A convenience wrapper. 666 ModRefInfo getModRefInfo(const CatchPadInst *I, const Value *P, 667 LocationSize Size) { 668 return getModRefInfo(I, MemoryLocation(P, Size)); 669 } 670 671 /// getModRefInfo (for catchrets) - Return information about whether 672 /// a particular catchret modifies or reads the specified memory location. 673 ModRefInfo getModRefInfo(const CatchReturnInst *I, const MemoryLocation &Loc); 674 675 /// getModRefInfo (for catchrets) - A convenience wrapper. 676 ModRefInfo getModRefInfo(const CatchReturnInst *I, const Value *P, 677 LocationSize Size) { 678 return getModRefInfo(I, MemoryLocation(P, Size)); 679 } 680 681 /// Check whether or not an instruction may read or write the optionally 682 /// specified memory location. 683 /// 684 /// 685 /// An instruction that doesn't read or write memory may be trivially LICM'd 686 /// for example. 687 /// 688 /// For function calls, this delegates to the alias-analysis specific 689 /// call-site mod-ref behavior queries. Otherwise it delegates to the specific 690 /// helpers above. 691 ModRefInfo getModRefInfo(const Instruction *I, 692 const Optional<MemoryLocation> &OptLoc) { 693 AAQueryInfo AAQIP; 694 return getModRefInfo(I, OptLoc, AAQIP); 695 } 696 697 /// A convenience wrapper for constructing the memory location. 698 ModRefInfo getModRefInfo(const Instruction *I, const Value *P, 699 LocationSize Size) { 700 return getModRefInfo(I, MemoryLocation(P, Size)); 701 } 702 703 /// Return information about whether a call and an instruction may refer to 704 /// the same memory locations. 705 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call); 706 707 /// Return information about whether two call sites may refer to the same set 708 /// of memory locations. See the AA documentation for details: 709 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo 710 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2); 711 712 /// Return information about whether a particular call site modifies 713 /// or reads the specified memory location \p MemLoc before instruction \p I 714 /// in a BasicBlock. 715 /// Early exits in callCapturesBefore may lead to ModRefInfo::Must not being 716 /// set. 717 ModRefInfo callCapturesBefore(const Instruction *I, 718 const MemoryLocation &MemLoc, DominatorTree *DT); 719 720 /// A convenience wrapper to synthesize a memory location. 721 ModRefInfo callCapturesBefore(const Instruction *I, const Value *P, 722 LocationSize Size, DominatorTree *DT) { 723 return callCapturesBefore(I, MemoryLocation(P, Size), DT); 724 } 725 726 /// @} 727 //===--------------------------------------------------------------------===// 728 /// \name Higher level methods for querying mod/ref information. 729 /// @{ 730 731 /// Check if it is possible for execution of the specified basic block to 732 /// modify the location Loc. 733 bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc); 734 735 /// A convenience wrapper synthesizing a memory location. 736 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, 737 LocationSize Size) { 738 return canBasicBlockModify(BB, MemoryLocation(P, Size)); 739 } 740 741 /// Check if it is possible for the execution of the specified instructions 742 /// to mod\ref (according to the mode) the location Loc. 743 /// 744 /// The instructions to consider are all of the instructions in the range of 745 /// [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 746 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2, 747 const MemoryLocation &Loc, 748 const ModRefInfo Mode); 749 750 /// A convenience wrapper synthesizing a memory location. 751 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2, 752 const Value *Ptr, LocationSize Size, 753 const ModRefInfo Mode) { 754 return canInstructionRangeModRef(I1, I2, MemoryLocation(Ptr, Size), Mode); 755 } 756 757 private: 758 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, 759 AAQueryInfo &AAQI); 760 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, 761 bool OrLocal = false); 762 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2, 763 AAQueryInfo &AAQIP); 764 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, 765 AAQueryInfo &AAQI); 766 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, 767 AAQueryInfo &AAQI); 768 ModRefInfo getModRefInfo(const VAArgInst *V, const MemoryLocation &Loc, 769 AAQueryInfo &AAQI); 770 ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc, 771 AAQueryInfo &AAQI); 772 ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc, 773 AAQueryInfo &AAQI); 774 ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc, 775 AAQueryInfo &AAQI); 776 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, 777 const MemoryLocation &Loc, AAQueryInfo &AAQI); 778 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc, 779 AAQueryInfo &AAQI); 780 ModRefInfo getModRefInfo(const CatchPadInst *I, const MemoryLocation &Loc, 781 AAQueryInfo &AAQI); 782 ModRefInfo getModRefInfo(const CatchReturnInst *I, const MemoryLocation &Loc, 783 AAQueryInfo &AAQI); 784 ModRefInfo getModRefInfo(const Instruction *I, 785 const Optional<MemoryLocation> &OptLoc, 786 AAQueryInfo &AAQIP); 787 788 class Concept; 789 790 template <typename T> class Model; 791 792 template <typename T> friend class AAResultBase; 793 794 const TargetLibraryInfo &TLI; 795 796 std::vector<std::unique_ptr<Concept>> AAs; 797 798 std::vector<AnalysisKey *> AADeps; 799 800 /// Query depth used to distinguish recursive queries. 801 unsigned Depth = 0; 802 803 friend class BatchAAResults; 804 }; 805 806 /// This class is a wrapper over an AAResults, and it is intended to be used 807 /// only when there are no IR changes inbetween queries. BatchAAResults is 808 /// reusing the same `AAQueryInfo` to preserve the state across queries, 809 /// esentially making AA work in "batch mode". The internal state cannot be 810 /// cleared, so to go "out-of-batch-mode", the user must either use AAResults, 811 /// or create a new BatchAAResults. 812 class BatchAAResults { 813 AAResults &AA; 814 AAQueryInfo AAQI; 815 816 public: 817 BatchAAResults(AAResults &AAR) : AA(AAR), AAQI() {} 818 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB) { 819 return AA.alias(LocA, LocB, AAQI); 820 } 821 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal = false) { 822 return AA.pointsToConstantMemory(Loc, AAQI, OrLocal); 823 } 824 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc) { 825 return AA.getModRefInfo(Call, Loc, AAQI); 826 } 827 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2) { 828 return AA.getModRefInfo(Call1, Call2, AAQI); 829 } 830 ModRefInfo getModRefInfo(const Instruction *I, 831 const Optional<MemoryLocation> &OptLoc) { 832 return AA.getModRefInfo(I, OptLoc, AAQI); 833 } 834 ModRefInfo getModRefInfo(Instruction *I, const CallBase *Call2) { 835 return AA.getModRefInfo(I, Call2, AAQI); 836 } 837 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { 838 return AA.getArgModRefInfo(Call, ArgIdx); 839 } 840 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) { 841 return AA.getModRefBehavior(Call); 842 } 843 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) { 844 return alias(LocA, LocB) == MustAlias; 845 } 846 bool isMustAlias(const Value *V1, const Value *V2) { 847 return alias(MemoryLocation(V1, LocationSize::precise(1)), 848 MemoryLocation(V2, LocationSize::precise(1))) == MustAlias; 849 } 850 }; 851 852 /// Temporary typedef for legacy code that uses a generic \c AliasAnalysis 853 /// pointer or reference. 854 using AliasAnalysis = AAResults; 855 856 /// A private abstract base class describing the concept of an individual alias 857 /// analysis implementation. 858 /// 859 /// This interface is implemented by any \c Model instantiation. It is also the 860 /// interface which a type used to instantiate the model must provide. 861 /// 862 /// All of these methods model methods by the same name in the \c 863 /// AAResults class. Only differences and specifics to how the 864 /// implementations are called are documented here. 865 class AAResults::Concept { 866 public: 867 virtual ~Concept() = 0; 868 869 /// An update API used internally by the AAResults to provide 870 /// a handle back to the top level aggregation. 871 virtual void setAAResults(AAResults *NewAAR) = 0; 872 873 //===--------------------------------------------------------------------===// 874 /// \name Alias Queries 875 /// @{ 876 877 /// The main low level interface to the alias analysis implementation. 878 /// Returns an AliasResult indicating whether the two pointers are aliased to 879 /// each other. This is the interface that must be implemented by specific 880 /// alias analysis implementations. 881 virtual AliasResult alias(const MemoryLocation &LocA, 882 const MemoryLocation &LocB, AAQueryInfo &AAQI) = 0; 883 884 /// Checks whether the given location points to constant memory, or if 885 /// \p OrLocal is true whether it points to a local alloca. 886 virtual bool pointsToConstantMemory(const MemoryLocation &Loc, 887 AAQueryInfo &AAQI, bool OrLocal) = 0; 888 889 /// @} 890 //===--------------------------------------------------------------------===// 891 /// \name Simple mod/ref information 892 /// @{ 893 894 /// Get the ModRef info associated with a pointer argument of a callsite. The 895 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note 896 /// that these bits do not necessarily account for the overall behavior of 897 /// the function, but rather only provide additional per-argument 898 /// information. 899 virtual ModRefInfo getArgModRefInfo(const CallBase *Call, 900 unsigned ArgIdx) = 0; 901 902 /// Return the behavior of the given call site. 903 virtual FunctionModRefBehavior getModRefBehavior(const CallBase *Call) = 0; 904 905 /// Return the behavior when calling the given function. 906 virtual FunctionModRefBehavior getModRefBehavior(const Function *F) = 0; 907 908 /// getModRefInfo (for call sites) - Return information about whether 909 /// a particular call site modifies or reads the specified memory location. 910 virtual ModRefInfo getModRefInfo(const CallBase *Call, 911 const MemoryLocation &Loc, 912 AAQueryInfo &AAQI) = 0; 913 914 /// Return information about whether two call sites may refer to the same set 915 /// of memory locations. See the AA documentation for details: 916 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo 917 virtual ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, 918 AAQueryInfo &AAQI) = 0; 919 920 /// @} 921 }; 922 923 /// A private class template which derives from \c Concept and wraps some other 924 /// type. 925 /// 926 /// This models the concept by directly forwarding each interface point to the 927 /// wrapped type which must implement a compatible interface. This provides 928 /// a type erased binding. 929 template <typename AAResultT> class AAResults::Model final : public Concept { 930 AAResultT &Result; 931 932 public: 933 explicit Model(AAResultT &Result, AAResults &AAR) : Result(Result) { 934 Result.setAAResults(&AAR); 935 } 936 ~Model() override = default; 937 938 void setAAResults(AAResults *NewAAR) override { Result.setAAResults(NewAAR); } 939 940 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, 941 AAQueryInfo &AAQI) override { 942 return Result.alias(LocA, LocB, AAQI); 943 } 944 945 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, 946 bool OrLocal) override { 947 return Result.pointsToConstantMemory(Loc, AAQI, OrLocal); 948 } 949 950 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) override { 951 return Result.getArgModRefInfo(Call, ArgIdx); 952 } 953 954 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) override { 955 return Result.getModRefBehavior(Call); 956 } 957 958 FunctionModRefBehavior getModRefBehavior(const Function *F) override { 959 return Result.getModRefBehavior(F); 960 } 961 962 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, 963 AAQueryInfo &AAQI) override { 964 return Result.getModRefInfo(Call, Loc, AAQI); 965 } 966 967 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, 968 AAQueryInfo &AAQI) override { 969 return Result.getModRefInfo(Call1, Call2, AAQI); 970 } 971 }; 972 973 /// A CRTP-driven "mixin" base class to help implement the function alias 974 /// analysis results concept. 975 /// 976 /// Because of the nature of many alias analysis implementations, they often 977 /// only implement a subset of the interface. This base class will attempt to 978 /// implement the remaining portions of the interface in terms of simpler forms 979 /// of the interface where possible, and otherwise provide conservatively 980 /// correct fallback implementations. 981 /// 982 /// Implementors of an alias analysis should derive from this CRTP, and then 983 /// override specific methods that they wish to customize. There is no need to 984 /// use virtual anywhere, the CRTP base class does static dispatch to the 985 /// derived type passed into it. 986 template <typename DerivedT> class AAResultBase { 987 // Expose some parts of the interface only to the AAResults::Model 988 // for wrapping. Specifically, this allows the model to call our 989 // setAAResults method without exposing it as a fully public API. 990 friend class AAResults::Model<DerivedT>; 991 992 /// A pointer to the AAResults object that this AAResult is 993 /// aggregated within. May be null if not aggregated. 994 AAResults *AAR = nullptr; 995 996 /// Helper to dispatch calls back through the derived type. 997 DerivedT &derived() { return static_cast<DerivedT &>(*this); } 998 999 /// A setter for the AAResults pointer, which is used to satisfy the 1000 /// AAResults::Model contract. 1001 void setAAResults(AAResults *NewAAR) { AAR = NewAAR; } 1002 1003 protected: 1004 /// This proxy class models a common pattern where we delegate to either the 1005 /// top-level \c AAResults aggregation if one is registered, or to the 1006 /// current result if none are registered. 1007 class AAResultsProxy { 1008 AAResults *AAR; 1009 DerivedT &CurrentResult; 1010 1011 public: 1012 AAResultsProxy(AAResults *AAR, DerivedT &CurrentResult) 1013 : AAR(AAR), CurrentResult(CurrentResult) {} 1014 1015 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, 1016 AAQueryInfo &AAQI) { 1017 return AAR ? AAR->alias(LocA, LocB, AAQI) 1018 : CurrentResult.alias(LocA, LocB, AAQI); 1019 } 1020 1021 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, 1022 bool OrLocal) { 1023 return AAR ? AAR->pointsToConstantMemory(Loc, AAQI, OrLocal) 1024 : CurrentResult.pointsToConstantMemory(Loc, AAQI, OrLocal); 1025 } 1026 1027 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { 1028 return AAR ? AAR->getArgModRefInfo(Call, ArgIdx) 1029 : CurrentResult.getArgModRefInfo(Call, ArgIdx); 1030 } 1031 1032 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) { 1033 return AAR ? AAR->getModRefBehavior(Call) 1034 : CurrentResult.getModRefBehavior(Call); 1035 } 1036 1037 FunctionModRefBehavior getModRefBehavior(const Function *F) { 1038 return AAR ? AAR->getModRefBehavior(F) : CurrentResult.getModRefBehavior(F); 1039 } 1040 1041 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, 1042 AAQueryInfo &AAQI) { 1043 return AAR ? AAR->getModRefInfo(Call, Loc, AAQI) 1044 : CurrentResult.getModRefInfo(Call, Loc, AAQI); 1045 } 1046 1047 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, 1048 AAQueryInfo &AAQI) { 1049 return AAR ? AAR->getModRefInfo(Call1, Call2, AAQI) 1050 : CurrentResult.getModRefInfo(Call1, Call2, AAQI); 1051 } 1052 }; 1053 1054 explicit AAResultBase() = default; 1055 1056 // Provide all the copy and move constructors so that derived types aren't 1057 // constrained. 1058 AAResultBase(const AAResultBase &Arg) {} 1059 AAResultBase(AAResultBase &&Arg) {} 1060 1061 /// Get a proxy for the best AA result set to query at this time. 1062 /// 1063 /// When this result is part of a larger aggregation, this will proxy to that 1064 /// aggregation. When this result is used in isolation, it will just delegate 1065 /// back to the derived class's implementation. 1066 /// 1067 /// Note that callers of this need to take considerable care to not cause 1068 /// performance problems when they use this routine, in the case of a large 1069 /// number of alias analyses being aggregated, it can be expensive to walk 1070 /// back across the chain. 1071 AAResultsProxy getBestAAResults() { return AAResultsProxy(AAR, derived()); } 1072 1073 public: 1074 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, 1075 AAQueryInfo &AAQI) { 1076 return MayAlias; 1077 } 1078 1079 bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI, 1080 bool OrLocal) { 1081 return false; 1082 } 1083 1084 ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { 1085 return ModRefInfo::ModRef; 1086 } 1087 1088 FunctionModRefBehavior getModRefBehavior(const CallBase *Call) { 1089 return FMRB_UnknownModRefBehavior; 1090 } 1091 1092 FunctionModRefBehavior getModRefBehavior(const Function *F) { 1093 return FMRB_UnknownModRefBehavior; 1094 } 1095 1096 ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, 1097 AAQueryInfo &AAQI) { 1098 return ModRefInfo::ModRef; 1099 } 1100 1101 ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2, 1102 AAQueryInfo &AAQI) { 1103 return ModRefInfo::ModRef; 1104 } 1105 }; 1106 1107 /// Return true if this pointer is returned by a noalias function. 1108 bool isNoAliasCall(const Value *V); 1109 1110 /// Return true if this pointer refers to a distinct and identifiable object. 1111 /// This returns true for: 1112 /// Global Variables and Functions (but not Global Aliases) 1113 /// Allocas 1114 /// ByVal and NoAlias Arguments 1115 /// NoAlias returns (e.g. calls to malloc) 1116 /// 1117 bool isIdentifiedObject(const Value *V); 1118 1119 /// Return true if V is umabigously identified at the function-level. 1120 /// Different IdentifiedFunctionLocals can't alias. 1121 /// Further, an IdentifiedFunctionLocal can not alias with any function 1122 /// arguments other than itself, which is not necessarily true for 1123 /// IdentifiedObjects. 1124 bool isIdentifiedFunctionLocal(const Value *V); 1125 1126 /// A manager for alias analyses. 1127 /// 1128 /// This class can have analyses registered with it and when run, it will run 1129 /// all of them and aggregate their results into single AA results interface 1130 /// that dispatches across all of the alias analysis results available. 1131 /// 1132 /// Note that the order in which analyses are registered is very significant. 1133 /// That is the order in which the results will be aggregated and queried. 1134 /// 1135 /// This manager effectively wraps the AnalysisManager for registering alias 1136 /// analyses. When you register your alias analysis with this manager, it will 1137 /// ensure the analysis itself is registered with its AnalysisManager. 1138 /// 1139 /// The result of this analysis is only invalidated if one of the particular 1140 /// aggregated AA results end up being invalidated. This removes the need to 1141 /// explicitly preserve the results of `AAManager`. Note that analyses should no 1142 /// longer be registered once the `AAManager` is run. 1143 class AAManager : public AnalysisInfoMixin<AAManager> { 1144 public: 1145 using Result = AAResults; 1146 1147 /// Register a specific AA result. 1148 template <typename AnalysisT> void registerFunctionAnalysis() { 1149 ResultGetters.push_back(&getFunctionAAResultImpl<AnalysisT>); 1150 } 1151 1152 /// Register a specific AA result. 1153 template <typename AnalysisT> void registerModuleAnalysis() { 1154 ResultGetters.push_back(&getModuleAAResultImpl<AnalysisT>); 1155 } 1156 1157 Result run(Function &F, FunctionAnalysisManager &AM); 1158 1159 private: 1160 friend AnalysisInfoMixin<AAManager>; 1161 1162 static AnalysisKey Key; 1163 1164 SmallVector<void (*)(Function &F, FunctionAnalysisManager &AM, 1165 AAResults &AAResults), 1166 4> ResultGetters; 1167 1168 template <typename AnalysisT> 1169 static void getFunctionAAResultImpl(Function &F, 1170 FunctionAnalysisManager &AM, 1171 AAResults &AAResults) { 1172 AAResults.addAAResult(AM.template getResult<AnalysisT>(F)); 1173 AAResults.addAADependencyID(AnalysisT::ID()); 1174 } 1175 1176 template <typename AnalysisT> 1177 static void getModuleAAResultImpl(Function &F, FunctionAnalysisManager &AM, 1178 AAResults &AAResults) { 1179 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F); 1180 if (auto *R = 1181 MAMProxy.template getCachedResult<AnalysisT>(*F.getParent())) { 1182 AAResults.addAAResult(*R); 1183 MAMProxy 1184 .template registerOuterAnalysisInvalidation<AnalysisT, AAManager>(); 1185 } 1186 } 1187 }; 1188 1189 /// A wrapper pass to provide the legacy pass manager access to a suitably 1190 /// prepared AAResults object. 1191 class AAResultsWrapperPass : public FunctionPass { 1192 std::unique_ptr<AAResults> AAR; 1193 1194 public: 1195 static char ID; 1196 1197 AAResultsWrapperPass(); 1198 1199 AAResults &getAAResults() { return *AAR; } 1200 const AAResults &getAAResults() const { return *AAR; } 1201 1202 bool runOnFunction(Function &F) override; 1203 1204 void getAnalysisUsage(AnalysisUsage &AU) const override; 1205 }; 1206 1207 /// A wrapper pass for external alias analyses. This just squirrels away the 1208 /// callback used to run any analyses and register their results. 1209 struct ExternalAAWrapperPass : ImmutablePass { 1210 using CallbackT = std::function<void(Pass &, Function &, AAResults &)>; 1211 1212 CallbackT CB; 1213 1214 static char ID; 1215 1216 ExternalAAWrapperPass(); 1217 1218 explicit ExternalAAWrapperPass(CallbackT CB); 1219 1220 void getAnalysisUsage(AnalysisUsage &AU) const override { 1221 AU.setPreservesAll(); 1222 } 1223 }; 1224 1225 FunctionPass *createAAResultsWrapperPass(); 1226 1227 /// A wrapper pass around a callback which can be used to populate the 1228 /// AAResults in the AAResultsWrapperPass from an external AA. 1229 /// 1230 /// The callback provided here will be used each time we prepare an AAResults 1231 /// object, and will receive a reference to the function wrapper pass, the 1232 /// function, and the AAResults object to populate. This should be used when 1233 /// setting up a custom pass pipeline to inject a hook into the AA results. 1234 ImmutablePass *createExternalAAWrapperPass( 1235 std::function<void(Pass &, Function &, AAResults &)> Callback); 1236 1237 /// A helper for the legacy pass manager to create a \c AAResults 1238 /// object populated to the best of our ability for a particular function when 1239 /// inside of a \c ModulePass or a \c CallGraphSCCPass. 1240 /// 1241 /// If a \c ModulePass or a \c CallGraphSCCPass calls \p 1242 /// createLegacyPMAAResults, it also needs to call \p addUsedAAAnalyses in \p 1243 /// getAnalysisUsage. 1244 AAResults createLegacyPMAAResults(Pass &P, Function &F, BasicAAResult &BAR); 1245 1246 /// A helper for the legacy pass manager to populate \p AU to add uses to make 1247 /// sure the analyses required by \p createLegacyPMAAResults are available. 1248 void getAAResultsAnalysisUsage(AnalysisUsage &AU); 1249 1250 } // end namespace llvm 1251 1252 #endif // LLVM_ANALYSIS_ALIASANALYSIS_H 1253