1 //==- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation --==// 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 implements the generic AliasAnalysis interface which is used as the 10 // common interface used by all clients and implementations of alias analysis. 11 // 12 // This file also implements the default version of the AliasAnalysis interface 13 // that is to be used when no other implementation is specified. This does some 14 // simple tests that detect obvious cases: two different global pointers cannot 15 // alias, a global cannot alias a malloc, two different mallocs cannot alias, 16 // etc. 17 // 18 // This alias analysis implementation really isn't very good for anything, but 19 // it is very fast, and makes a nice clean default implementation. Because it 20 // handles lots of little corner cases, other, more complex, alias analysis 21 // implementations may choose to rely on this pass to resolve these simple and 22 // easy cases. 23 // 24 //===----------------------------------------------------------------------===// 25 26 #include "llvm/Analysis/AliasAnalysis.h" 27 #include "llvm/Analysis/BasicAliasAnalysis.h" 28 #include "llvm/Analysis/CFLAndersAliasAnalysis.h" 29 #include "llvm/Analysis/CFLSteensAliasAnalysis.h" 30 #include "llvm/Analysis/CaptureTracking.h" 31 #include "llvm/Analysis/GlobalsModRef.h" 32 #include "llvm/Analysis/MemoryLocation.h" 33 #include "llvm/Analysis/ObjCARCAliasAnalysis.h" 34 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 35 #include "llvm/Analysis/ScopedNoAliasAA.h" 36 #include "llvm/Analysis/TargetLibraryInfo.h" 37 #include "llvm/Analysis/TypeBasedAliasAnalysis.h" 38 #include "llvm/Analysis/ValueTracking.h" 39 #include "llvm/IR/Argument.h" 40 #include "llvm/IR/Attributes.h" 41 #include "llvm/IR/BasicBlock.h" 42 #include "llvm/IR/Instruction.h" 43 #include "llvm/IR/Instructions.h" 44 #include "llvm/IR/Module.h" 45 #include "llvm/IR/Type.h" 46 #include "llvm/IR/Value.h" 47 #include "llvm/Pass.h" 48 #include "llvm/Support/AtomicOrdering.h" 49 #include "llvm/Support/Casting.h" 50 #include "llvm/Support/CommandLine.h" 51 #include <algorithm> 52 #include <cassert> 53 #include <functional> 54 #include <iterator> 55 56 using namespace llvm; 57 58 /// Allow disabling BasicAA from the AA results. This is particularly useful 59 /// when testing to isolate a single AA implementation. 60 static cl::opt<bool> DisableBasicAA("disable-basicaa", cl::Hidden, 61 cl::init(false)); 62 63 AAResults::AAResults(AAResults &&Arg) 64 : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) { 65 for (auto &AA : AAs) 66 AA->setAAResults(this); 67 } 68 69 AAResults::~AAResults() { 70 // FIXME; It would be nice to at least clear out the pointers back to this 71 // aggregation here, but we end up with non-nesting lifetimes in the legacy 72 // pass manager that prevent this from working. In the legacy pass manager 73 // we'll end up with dangling references here in some cases. 74 #if 0 75 for (auto &AA : AAs) 76 AA->setAAResults(nullptr); 77 #endif 78 } 79 80 bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA, 81 FunctionAnalysisManager::Invalidator &Inv) { 82 // AAResults preserves the AAManager by default, due to the stateless nature 83 // of AliasAnalysis. There is no need to check whether it has been preserved 84 // explicitly. However, we still need to check if any of the dependencies end 85 // up being invalidated, and invalidate ourselves in that case. 86 for (AnalysisKey *ID : AADeps) 87 if (Inv.invalidate(ID, F, PA)) 88 return true; 89 90 // Everything we depend on is still fine, so are we. Nothing to invalidate. 91 return false; 92 } 93 94 //===----------------------------------------------------------------------===// 95 // Default chaining methods 96 //===----------------------------------------------------------------------===// 97 98 AliasResult AAResults::alias(const MemoryLocation &LocA, 99 const MemoryLocation &LocB) { 100 AAQueryInfo AAQIP; 101 return alias(LocA, LocB, AAQIP); 102 } 103 104 AliasResult AAResults::alias(const MemoryLocation &LocA, 105 const MemoryLocation &LocB, AAQueryInfo &AAQI) { 106 for (const auto &AA : AAs) { 107 auto Result = AA->alias(LocA, LocB, AAQI); 108 if (Result != MayAlias) 109 return Result; 110 } 111 return MayAlias; 112 } 113 114 bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc, 115 bool OrLocal) { 116 AAQueryInfo AAQIP; 117 return pointsToConstantMemory(Loc, AAQIP, OrLocal); 118 } 119 120 bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc, 121 AAQueryInfo &AAQI, bool OrLocal) { 122 for (const auto &AA : AAs) 123 if (AA->pointsToConstantMemory(Loc, AAQI, OrLocal)) 124 return true; 125 126 return false; 127 } 128 129 ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) { 130 ModRefInfo Result = ModRefInfo::ModRef; 131 132 for (const auto &AA : AAs) { 133 Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx)); 134 135 // Early-exit the moment we reach the bottom of the lattice. 136 if (isNoModRef(Result)) 137 return ModRefInfo::NoModRef; 138 } 139 140 return Result; 141 } 142 143 ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) { 144 AAQueryInfo AAQIP; 145 return getModRefInfo(I, Call2, AAQIP); 146 } 147 148 ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2, 149 AAQueryInfo &AAQI) { 150 // We may have two calls. 151 if (const auto *Call1 = dyn_cast<CallBase>(I)) { 152 // Check if the two calls modify the same memory. 153 return getModRefInfo(Call1, Call2, AAQI); 154 } else if (I->isFenceLike()) { 155 // If this is a fence, just return ModRef. 156 return ModRefInfo::ModRef; 157 } else { 158 // Otherwise, check if the call modifies or references the 159 // location this memory access defines. The best we can say 160 // is that if the call references what this instruction 161 // defines, it must be clobbered by this location. 162 const MemoryLocation DefLoc = MemoryLocation::get(I); 163 ModRefInfo MR = getModRefInfo(Call2, DefLoc, AAQI); 164 if (isModOrRefSet(MR)) 165 return setModAndRef(MR); 166 } 167 return ModRefInfo::NoModRef; 168 } 169 170 ModRefInfo AAResults::getModRefInfo(const CallBase *Call, 171 const MemoryLocation &Loc) { 172 AAQueryInfo AAQIP; 173 return getModRefInfo(Call, Loc, AAQIP); 174 } 175 176 ModRefInfo AAResults::getModRefInfo(const CallBase *Call, 177 const MemoryLocation &Loc, 178 AAQueryInfo &AAQI) { 179 ModRefInfo Result = ModRefInfo::ModRef; 180 181 for (const auto &AA : AAs) { 182 Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc, AAQI)); 183 184 // Early-exit the moment we reach the bottom of the lattice. 185 if (isNoModRef(Result)) 186 return ModRefInfo::NoModRef; 187 } 188 189 // Try to refine the mod-ref info further using other API entry points to the 190 // aggregate set of AA results. 191 auto MRB = getModRefBehavior(Call); 192 if (MRB == FMRB_DoesNotAccessMemory || 193 MRB == FMRB_OnlyAccessesInaccessibleMem) 194 return ModRefInfo::NoModRef; 195 196 if (onlyReadsMemory(MRB)) 197 Result = clearMod(Result); 198 else if (doesNotReadMemory(MRB)) 199 Result = clearRef(Result); 200 201 if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) { 202 bool IsMustAlias = true; 203 ModRefInfo AllArgsMask = ModRefInfo::NoModRef; 204 if (doesAccessArgPointees(MRB)) { 205 for (auto AI = Call->arg_begin(), AE = Call->arg_end(); AI != AE; ++AI) { 206 const Value *Arg = *AI; 207 if (!Arg->getType()->isPointerTy()) 208 continue; 209 unsigned ArgIdx = std::distance(Call->arg_begin(), AI); 210 MemoryLocation ArgLoc = 211 MemoryLocation::getForArgument(Call, ArgIdx, TLI); 212 AliasResult ArgAlias = alias(ArgLoc, Loc); 213 if (ArgAlias != NoAlias) { 214 ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx); 215 AllArgsMask = unionModRef(AllArgsMask, ArgMask); 216 } 217 // Conservatively clear IsMustAlias unless only MustAlias is found. 218 IsMustAlias &= (ArgAlias == MustAlias); 219 } 220 } 221 // Return NoModRef if no alias found with any argument. 222 if (isNoModRef(AllArgsMask)) 223 return ModRefInfo::NoModRef; 224 // Logical & between other AA analyses and argument analysis. 225 Result = intersectModRef(Result, AllArgsMask); 226 // If only MustAlias found above, set Must bit. 227 Result = IsMustAlias ? setMust(Result) : clearMust(Result); 228 } 229 230 // If Loc is a constant memory location, the call definitely could not 231 // modify the memory location. 232 if (isModSet(Result) && pointsToConstantMemory(Loc, /*OrLocal*/ false)) 233 Result = clearMod(Result); 234 235 return Result; 236 } 237 238 ModRefInfo AAResults::getModRefInfo(const CallBase *Call1, 239 const CallBase *Call2) { 240 AAQueryInfo AAQIP; 241 return getModRefInfo(Call1, Call2, AAQIP); 242 } 243 244 ModRefInfo AAResults::getModRefInfo(const CallBase *Call1, 245 const CallBase *Call2, AAQueryInfo &AAQI) { 246 ModRefInfo Result = ModRefInfo::ModRef; 247 248 for (const auto &AA : AAs) { 249 Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2, AAQI)); 250 251 // Early-exit the moment we reach the bottom of the lattice. 252 if (isNoModRef(Result)) 253 return ModRefInfo::NoModRef; 254 } 255 256 // Try to refine the mod-ref info further using other API entry points to the 257 // aggregate set of AA results. 258 259 // If Call1 or Call2 are readnone, they don't interact. 260 auto Call1B = getModRefBehavior(Call1); 261 if (Call1B == FMRB_DoesNotAccessMemory) 262 return ModRefInfo::NoModRef; 263 264 auto Call2B = getModRefBehavior(Call2); 265 if (Call2B == FMRB_DoesNotAccessMemory) 266 return ModRefInfo::NoModRef; 267 268 // If they both only read from memory, there is no dependence. 269 if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B)) 270 return ModRefInfo::NoModRef; 271 272 // If Call1 only reads memory, the only dependence on Call2 can be 273 // from Call1 reading memory written by Call2. 274 if (onlyReadsMemory(Call1B)) 275 Result = clearMod(Result); 276 else if (doesNotReadMemory(Call1B)) 277 Result = clearRef(Result); 278 279 // If Call2 only access memory through arguments, accumulate the mod/ref 280 // information from Call1's references to the memory referenced by 281 // Call2's arguments. 282 if (onlyAccessesArgPointees(Call2B)) { 283 if (!doesAccessArgPointees(Call2B)) 284 return ModRefInfo::NoModRef; 285 ModRefInfo R = ModRefInfo::NoModRef; 286 bool IsMustAlias = true; 287 for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) { 288 const Value *Arg = *I; 289 if (!Arg->getType()->isPointerTy()) 290 continue; 291 unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I); 292 auto Call2ArgLoc = 293 MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI); 294 295 // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the 296 // dependence of Call1 on that location is the inverse: 297 // - If Call2 modifies location, dependence exists if Call1 reads or 298 // writes. 299 // - If Call2 only reads location, dependence exists if Call1 writes. 300 ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx); 301 ModRefInfo ArgMask = ModRefInfo::NoModRef; 302 if (isModSet(ArgModRefC2)) 303 ArgMask = ModRefInfo::ModRef; 304 else if (isRefSet(ArgModRefC2)) 305 ArgMask = ModRefInfo::Mod; 306 307 // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use 308 // above ArgMask to update dependence info. 309 ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc); 310 ArgMask = intersectModRef(ArgMask, ModRefC1); 311 312 // Conservatively clear IsMustAlias unless only MustAlias is found. 313 IsMustAlias &= isMustSet(ModRefC1); 314 315 R = intersectModRef(unionModRef(R, ArgMask), Result); 316 if (R == Result) { 317 // On early exit, not all args were checked, cannot set Must. 318 if (I + 1 != E) 319 IsMustAlias = false; 320 break; 321 } 322 } 323 324 if (isNoModRef(R)) 325 return ModRefInfo::NoModRef; 326 327 // If MustAlias found above, set Must bit. 328 return IsMustAlias ? setMust(R) : clearMust(R); 329 } 330 331 // If Call1 only accesses memory through arguments, check if Call2 references 332 // any of the memory referenced by Call1's arguments. If not, return NoModRef. 333 if (onlyAccessesArgPointees(Call1B)) { 334 if (!doesAccessArgPointees(Call1B)) 335 return ModRefInfo::NoModRef; 336 ModRefInfo R = ModRefInfo::NoModRef; 337 bool IsMustAlias = true; 338 for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) { 339 const Value *Arg = *I; 340 if (!Arg->getType()->isPointerTy()) 341 continue; 342 unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I); 343 auto Call1ArgLoc = 344 MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI); 345 346 // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1 347 // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by 348 // Call2. If Call1 might Ref, then we care only about a Mod by Call2. 349 ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx); 350 ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc); 351 if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) || 352 (isRefSet(ArgModRefC1) && isModSet(ModRefC2))) 353 R = intersectModRef(unionModRef(R, ArgModRefC1), Result); 354 355 // Conservatively clear IsMustAlias unless only MustAlias is found. 356 IsMustAlias &= isMustSet(ModRefC2); 357 358 if (R == Result) { 359 // On early exit, not all args were checked, cannot set Must. 360 if (I + 1 != E) 361 IsMustAlias = false; 362 break; 363 } 364 } 365 366 if (isNoModRef(R)) 367 return ModRefInfo::NoModRef; 368 369 // If MustAlias found above, set Must bit. 370 return IsMustAlias ? setMust(R) : clearMust(R); 371 } 372 373 return Result; 374 } 375 376 FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) { 377 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; 378 379 for (const auto &AA : AAs) { 380 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call)); 381 382 // Early-exit the moment we reach the bottom of the lattice. 383 if (Result == FMRB_DoesNotAccessMemory) 384 return Result; 385 } 386 387 return Result; 388 } 389 390 FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) { 391 FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior; 392 393 for (const auto &AA : AAs) { 394 Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F)); 395 396 // Early-exit the moment we reach the bottom of the lattice. 397 if (Result == FMRB_DoesNotAccessMemory) 398 return Result; 399 } 400 401 return Result; 402 } 403 404 raw_ostream &llvm::operator<<(raw_ostream &OS, AliasResult AR) { 405 switch (AR) { 406 case NoAlias: 407 OS << "NoAlias"; 408 break; 409 case MustAlias: 410 OS << "MustAlias"; 411 break; 412 case MayAlias: 413 OS << "MayAlias"; 414 break; 415 case PartialAlias: 416 OS << "PartialAlias"; 417 break; 418 } 419 return OS; 420 } 421 422 //===----------------------------------------------------------------------===// 423 // Helper method implementation 424 //===----------------------------------------------------------------------===// 425 426 ModRefInfo AAResults::getModRefInfo(const LoadInst *L, 427 const MemoryLocation &Loc) { 428 AAQueryInfo AAQIP; 429 return getModRefInfo(L, Loc, AAQIP); 430 } 431 ModRefInfo AAResults::getModRefInfo(const LoadInst *L, 432 const MemoryLocation &Loc, 433 AAQueryInfo &AAQI) { 434 // Be conservative in the face of atomic. 435 if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered)) 436 return ModRefInfo::ModRef; 437 438 // If the load address doesn't alias the given address, it doesn't read 439 // or write the specified memory. 440 if (Loc.Ptr) { 441 AliasResult AR = alias(MemoryLocation::get(L), Loc, AAQI); 442 if (AR == NoAlias) 443 return ModRefInfo::NoModRef; 444 if (AR == MustAlias) 445 return ModRefInfo::MustRef; 446 } 447 // Otherwise, a load just reads. 448 return ModRefInfo::Ref; 449 } 450 451 ModRefInfo AAResults::getModRefInfo(const StoreInst *S, 452 const MemoryLocation &Loc) { 453 AAQueryInfo AAQIP; 454 return getModRefInfo(S, Loc, AAQIP); 455 } 456 ModRefInfo AAResults::getModRefInfo(const StoreInst *S, 457 const MemoryLocation &Loc, 458 AAQueryInfo &AAQI) { 459 // Be conservative in the face of atomic. 460 if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered)) 461 return ModRefInfo::ModRef; 462 463 if (Loc.Ptr) { 464 AliasResult AR = alias(MemoryLocation::get(S), Loc, AAQI); 465 // If the store address cannot alias the pointer in question, then the 466 // specified memory cannot be modified by the store. 467 if (AR == NoAlias) 468 return ModRefInfo::NoModRef; 469 470 // If the pointer is a pointer to constant memory, then it could not have 471 // been modified by this store. 472 if (pointsToConstantMemory(Loc, AAQI)) 473 return ModRefInfo::NoModRef; 474 475 // If the store address aliases the pointer as must alias, set Must. 476 if (AR == MustAlias) 477 return ModRefInfo::MustMod; 478 } 479 480 // Otherwise, a store just writes. 481 return ModRefInfo::Mod; 482 } 483 484 ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) { 485 AAQueryInfo AAQIP; 486 return getModRefInfo(S, Loc, AAQIP); 487 } 488 489 ModRefInfo AAResults::getModRefInfo(const FenceInst *S, 490 const MemoryLocation &Loc, 491 AAQueryInfo &AAQI) { 492 // If we know that the location is a constant memory location, the fence 493 // cannot modify this location. 494 if (Loc.Ptr && pointsToConstantMemory(Loc, AAQI)) 495 return ModRefInfo::Ref; 496 return ModRefInfo::ModRef; 497 } 498 499 ModRefInfo AAResults::getModRefInfo(const VAArgInst *V, 500 const MemoryLocation &Loc) { 501 AAQueryInfo AAQIP; 502 return getModRefInfo(V, Loc, AAQIP); 503 } 504 505 ModRefInfo AAResults::getModRefInfo(const VAArgInst *V, 506 const MemoryLocation &Loc, 507 AAQueryInfo &AAQI) { 508 if (Loc.Ptr) { 509 AliasResult AR = alias(MemoryLocation::get(V), Loc, AAQI); 510 // If the va_arg address cannot alias the pointer in question, then the 511 // specified memory cannot be accessed by the va_arg. 512 if (AR == NoAlias) 513 return ModRefInfo::NoModRef; 514 515 // If the pointer is a pointer to constant memory, then it could not have 516 // been modified by this va_arg. 517 if (pointsToConstantMemory(Loc, AAQI)) 518 return ModRefInfo::NoModRef; 519 520 // If the va_arg aliases the pointer as must alias, set Must. 521 if (AR == MustAlias) 522 return ModRefInfo::MustModRef; 523 } 524 525 // Otherwise, a va_arg reads and writes. 526 return ModRefInfo::ModRef; 527 } 528 529 ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad, 530 const MemoryLocation &Loc) { 531 AAQueryInfo AAQIP; 532 return getModRefInfo(CatchPad, Loc, AAQIP); 533 } 534 535 ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad, 536 const MemoryLocation &Loc, 537 AAQueryInfo &AAQI) { 538 if (Loc.Ptr) { 539 // If the pointer is a pointer to constant memory, 540 // then it could not have been modified by this catchpad. 541 if (pointsToConstantMemory(Loc, AAQI)) 542 return ModRefInfo::NoModRef; 543 } 544 545 // Otherwise, a catchpad reads and writes. 546 return ModRefInfo::ModRef; 547 } 548 549 ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet, 550 const MemoryLocation &Loc) { 551 AAQueryInfo AAQIP; 552 return getModRefInfo(CatchRet, Loc, AAQIP); 553 } 554 555 ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet, 556 const MemoryLocation &Loc, 557 AAQueryInfo &AAQI) { 558 if (Loc.Ptr) { 559 // If the pointer is a pointer to constant memory, 560 // then it could not have been modified by this catchpad. 561 if (pointsToConstantMemory(Loc, AAQI)) 562 return ModRefInfo::NoModRef; 563 } 564 565 // Otherwise, a catchret reads and writes. 566 return ModRefInfo::ModRef; 567 } 568 569 ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX, 570 const MemoryLocation &Loc) { 571 AAQueryInfo AAQIP; 572 return getModRefInfo(CX, Loc, AAQIP); 573 } 574 575 ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX, 576 const MemoryLocation &Loc, 577 AAQueryInfo &AAQI) { 578 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. 579 if (isStrongerThanMonotonic(CX->getSuccessOrdering())) 580 return ModRefInfo::ModRef; 581 582 if (Loc.Ptr) { 583 AliasResult AR = alias(MemoryLocation::get(CX), Loc, AAQI); 584 // If the cmpxchg address does not alias the location, it does not access 585 // it. 586 if (AR == NoAlias) 587 return ModRefInfo::NoModRef; 588 589 // If the cmpxchg address aliases the pointer as must alias, set Must. 590 if (AR == MustAlias) 591 return ModRefInfo::MustModRef; 592 } 593 594 return ModRefInfo::ModRef; 595 } 596 597 ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW, 598 const MemoryLocation &Loc) { 599 AAQueryInfo AAQIP; 600 return getModRefInfo(RMW, Loc, AAQIP); 601 } 602 603 ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW, 604 const MemoryLocation &Loc, 605 AAQueryInfo &AAQI) { 606 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. 607 if (isStrongerThanMonotonic(RMW->getOrdering())) 608 return ModRefInfo::ModRef; 609 610 if (Loc.Ptr) { 611 AliasResult AR = alias(MemoryLocation::get(RMW), Loc, AAQI); 612 // If the atomicrmw address does not alias the location, it does not access 613 // it. 614 if (AR == NoAlias) 615 return ModRefInfo::NoModRef; 616 617 // If the atomicrmw address aliases the pointer as must alias, set Must. 618 if (AR == MustAlias) 619 return ModRefInfo::MustModRef; 620 } 621 622 return ModRefInfo::ModRef; 623 } 624 625 /// Return information about whether a particular call site modifies 626 /// or reads the specified memory location \p MemLoc before instruction \p I 627 /// in a BasicBlock. An ordered basic block \p OBB can be used to speed up 628 /// instruction-ordering queries inside the BasicBlock containing \p I. 629 /// FIXME: this is really just shoring-up a deficiency in alias analysis. 630 /// BasicAA isn't willing to spend linear time determining whether an alloca 631 /// was captured before or after this particular call, while we are. However, 632 /// with a smarter AA in place, this test is just wasting compile time. 633 ModRefInfo AAResults::callCapturesBefore(const Instruction *I, 634 const MemoryLocation &MemLoc, 635 DominatorTree *DT, 636 OrderedBasicBlock *OBB) { 637 if (!DT) 638 return ModRefInfo::ModRef; 639 640 const Value *Object = 641 GetUnderlyingObject(MemLoc.Ptr, I->getModule()->getDataLayout()); 642 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || 643 isa<Constant>(Object)) 644 return ModRefInfo::ModRef; 645 646 const auto *Call = dyn_cast<CallBase>(I); 647 if (!Call || Call == Object) 648 return ModRefInfo::ModRef; 649 650 if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true, 651 /* StoreCaptures */ true, I, DT, 652 /* include Object */ true, 653 /* OrderedBasicBlock */ OBB)) 654 return ModRefInfo::ModRef; 655 656 unsigned ArgNo = 0; 657 ModRefInfo R = ModRefInfo::NoModRef; 658 bool IsMustAlias = true; 659 // Set flag only if no May found and all operands processed. 660 for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end(); 661 CI != CE; ++CI, ++ArgNo) { 662 // Only look at the no-capture or byval pointer arguments. If this 663 // pointer were passed to arguments that were neither of these, then it 664 // couldn't be no-capture. 665 if (!(*CI)->getType()->isPointerTy() || 666 (!Call->doesNotCapture(ArgNo) && ArgNo < Call->getNumArgOperands() && 667 !Call->isByValArgument(ArgNo))) 668 continue; 669 670 AliasResult AR = alias(MemoryLocation(*CI), MemoryLocation(Object)); 671 // If this is a no-capture pointer argument, see if we can tell that it 672 // is impossible to alias the pointer we're checking. If not, we have to 673 // assume that the call could touch the pointer, even though it doesn't 674 // escape. 675 if (AR != MustAlias) 676 IsMustAlias = false; 677 if (AR == NoAlias) 678 continue; 679 if (Call->doesNotAccessMemory(ArgNo)) 680 continue; 681 if (Call->onlyReadsMemory(ArgNo)) { 682 R = ModRefInfo::Ref; 683 continue; 684 } 685 // Not returning MustModRef since we have not seen all the arguments. 686 return ModRefInfo::ModRef; 687 } 688 return IsMustAlias ? setMust(R) : clearMust(R); 689 } 690 691 /// canBasicBlockModify - Return true if it is possible for execution of the 692 /// specified basic block to modify the location Loc. 693 /// 694 bool AAResults::canBasicBlockModify(const BasicBlock &BB, 695 const MemoryLocation &Loc) { 696 return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod); 697 } 698 699 /// canInstructionRangeModRef - Return true if it is possible for the 700 /// execution of the specified instructions to mod\ref (according to the 701 /// mode) the location Loc. The instructions to consider are all 702 /// of the instructions in the range of [I1,I2] INCLUSIVE. 703 /// I1 and I2 must be in the same basic block. 704 bool AAResults::canInstructionRangeModRef(const Instruction &I1, 705 const Instruction &I2, 706 const MemoryLocation &Loc, 707 const ModRefInfo Mode) { 708 assert(I1.getParent() == I2.getParent() && 709 "Instructions not in same basic block!"); 710 BasicBlock::const_iterator I = I1.getIterator(); 711 BasicBlock::const_iterator E = I2.getIterator(); 712 ++E; // Convert from inclusive to exclusive range. 713 714 for (; I != E; ++I) // Check every instruction in range 715 if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode))) 716 return true; 717 return false; 718 } 719 720 // Provide a definition for the root virtual destructor. 721 AAResults::Concept::~Concept() = default; 722 723 // Provide a definition for the static object used to identify passes. 724 AnalysisKey AAManager::Key; 725 726 namespace { 727 728 729 } // end anonymous namespace 730 731 char ExternalAAWrapperPass::ID = 0; 732 733 INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis", 734 false, true) 735 736 ImmutablePass * 737 llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) { 738 return new ExternalAAWrapperPass(std::move(Callback)); 739 } 740 741 AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) { 742 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry()); 743 } 744 745 char AAResultsWrapperPass::ID = 0; 746 747 INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa", 748 "Function Alias Analysis Results", false, true) 749 INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) 750 INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass) 751 INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass) 752 INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass) 753 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) 754 INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass) 755 INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) 756 INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass) 757 INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass) 758 INITIALIZE_PASS_END(AAResultsWrapperPass, "aa", 759 "Function Alias Analysis Results", false, true) 760 761 FunctionPass *llvm::createAAResultsWrapperPass() { 762 return new AAResultsWrapperPass(); 763 } 764 765 /// Run the wrapper pass to rebuild an aggregation over known AA passes. 766 /// 767 /// This is the legacy pass manager's interface to the new-style AA results 768 /// aggregation object. Because this is somewhat shoe-horned into the legacy 769 /// pass manager, we hard code all the specific alias analyses available into 770 /// it. While the particular set enabled is configured via commandline flags, 771 /// adding a new alias analysis to LLVM will require adding support for it to 772 /// this list. 773 bool AAResultsWrapperPass::runOnFunction(Function &F) { 774 // NB! This *must* be reset before adding new AA results to the new 775 // AAResults object because in the legacy pass manager, each instance 776 // of these will refer to the *same* immutable analyses, registering and 777 // unregistering themselves with them. We need to carefully tear down the 778 // previous object first, in this case replacing it with an empty one, before 779 // registering new results. 780 AAR.reset( 781 new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI())); 782 783 // BasicAA is always available for function analyses. Also, we add it first 784 // so that it can trump TBAA results when it proves MustAlias. 785 // FIXME: TBAA should have an explicit mode to support this and then we 786 // should reconsider the ordering here. 787 if (!DisableBasicAA) 788 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult()); 789 790 // Populate the results with the currently available AAs. 791 if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) 792 AAR->addAAResult(WrapperPass->getResult()); 793 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) 794 AAR->addAAResult(WrapperPass->getResult()); 795 if (auto *WrapperPass = 796 getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>()) 797 AAR->addAAResult(WrapperPass->getResult()); 798 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>()) 799 AAR->addAAResult(WrapperPass->getResult()); 800 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>()) 801 AAR->addAAResult(WrapperPass->getResult()); 802 if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) 803 AAR->addAAResult(WrapperPass->getResult()); 804 if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) 805 AAR->addAAResult(WrapperPass->getResult()); 806 807 // If available, run an external AA providing callback over the results as 808 // well. 809 if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>()) 810 if (WrapperPass->CB) 811 WrapperPass->CB(*this, F, *AAR); 812 813 // Analyses don't mutate the IR, so return false. 814 return false; 815 } 816 817 void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 818 AU.setPreservesAll(); 819 AU.addRequired<BasicAAWrapperPass>(); 820 AU.addRequired<TargetLibraryInfoWrapperPass>(); 821 822 // We also need to mark all the alias analysis passes we will potentially 823 // probe in runOnFunction as used here to ensure the legacy pass manager 824 // preserves them. This hard coding of lists of alias analyses is specific to 825 // the legacy pass manager. 826 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>(); 827 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>(); 828 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>(); 829 AU.addUsedIfAvailable<GlobalsAAWrapperPass>(); 830 AU.addUsedIfAvailable<SCEVAAWrapperPass>(); 831 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); 832 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); 833 } 834 835 AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F, 836 BasicAAResult &BAR) { 837 AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()); 838 839 // Add in our explicitly constructed BasicAA results. 840 if (!DisableBasicAA) 841 AAR.addAAResult(BAR); 842 843 // Populate the results with the other currently available AAs. 844 if (auto *WrapperPass = 845 P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>()) 846 AAR.addAAResult(WrapperPass->getResult()); 847 if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) 848 AAR.addAAResult(WrapperPass->getResult()); 849 if (auto *WrapperPass = 850 P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>()) 851 AAR.addAAResult(WrapperPass->getResult()); 852 if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>()) 853 AAR.addAAResult(WrapperPass->getResult()); 854 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>()) 855 AAR.addAAResult(WrapperPass->getResult()); 856 if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>()) 857 AAR.addAAResult(WrapperPass->getResult()); 858 859 return AAR; 860 } 861 862 bool llvm::isNoAliasCall(const Value *V) { 863 if (const auto *Call = dyn_cast<CallBase>(V)) 864 return Call->hasRetAttr(Attribute::NoAlias); 865 return false; 866 } 867 868 bool llvm::isNoAliasArgument(const Value *V) { 869 if (const Argument *A = dyn_cast<Argument>(V)) 870 return A->hasNoAliasAttr(); 871 return false; 872 } 873 874 bool llvm::isIdentifiedObject(const Value *V) { 875 if (isa<AllocaInst>(V)) 876 return true; 877 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) 878 return true; 879 if (isNoAliasCall(V)) 880 return true; 881 if (const Argument *A = dyn_cast<Argument>(V)) 882 return A->hasNoAliasAttr() || A->hasByValAttr(); 883 return false; 884 } 885 886 bool llvm::isIdentifiedFunctionLocal(const Value *V) { 887 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); 888 } 889 890 void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) { 891 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if 892 // more alias analyses are added to llvm::createLegacyPMAAResults, they need 893 // to be added here also. 894 AU.addRequired<TargetLibraryInfoWrapperPass>(); 895 AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>(); 896 AU.addUsedIfAvailable<TypeBasedAAWrapperPass>(); 897 AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>(); 898 AU.addUsedIfAvailable<GlobalsAAWrapperPass>(); 899 AU.addUsedIfAvailable<CFLAndersAAWrapperPass>(); 900 AU.addUsedIfAvailable<CFLSteensAAWrapperPass>(); 901 } 902