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