1 //===- AliasSetTracker.cpp - Alias Sets Tracker 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 AliasSetTracker and AliasSet classes. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Analysis/AliasSetTracker.h" 14 #include "llvm/Analysis/GuardUtils.h" 15 #include "llvm/Analysis/LoopInfo.h" 16 #include "llvm/Analysis/MemoryLocation.h" 17 #include "llvm/Analysis/MemorySSA.h" 18 #include "llvm/Config/llvm-config.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/DataLayout.h" 21 #include "llvm/IR/Function.h" 22 #include "llvm/IR/InstIterator.h" 23 #include "llvm/IR/Instructions.h" 24 #include "llvm/IR/IntrinsicInst.h" 25 #include "llvm/IR/Module.h" 26 #include "llvm/IR/PassManager.h" 27 #include "llvm/IR/PatternMatch.h" 28 #include "llvm/IR/Value.h" 29 #include "llvm/InitializePasses.h" 30 #include "llvm/Pass.h" 31 #include "llvm/Support/AtomicOrdering.h" 32 #include "llvm/Support/CommandLine.h" 33 #include "llvm/Support/Compiler.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/raw_ostream.h" 37 38 using namespace llvm; 39 40 static cl::opt<unsigned> 41 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden, 42 cl::init(250), 43 cl::desc("The maximum number of pointers may-alias " 44 "sets may contain before degradation")); 45 46 /// mergeSetIn - Merge the specified alias set into this alias set. 47 /// 48 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) { 49 assert(!AS.Forward && "Alias set is already forwarding!"); 50 assert(!Forward && "This set is a forwarding set!!"); 51 52 bool WasMustAlias = (Alias == SetMustAlias); 53 // Update the alias and access types of this set... 54 Access |= AS.Access; 55 Alias |= AS.Alias; 56 57 if (Alias == SetMustAlias) { 58 // Check that these two merged sets really are must aliases. Since both 59 // used to be must-alias sets, we can just check any pointer from each set 60 // for aliasing. 61 AliasAnalysis &AA = AST.getAliasAnalysis(); 62 PointerRec *L = getSomePointer(); 63 PointerRec *R = AS.getSomePointer(); 64 65 // If the pointers are not a must-alias pair, this set becomes a may alias. 66 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()), 67 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) != 68 MustAlias) 69 Alias = SetMayAlias; 70 } 71 72 if (Alias == SetMayAlias) { 73 if (WasMustAlias) 74 AST.TotalMayAliasSetSize += size(); 75 if (AS.Alias == SetMustAlias) 76 AST.TotalMayAliasSetSize += AS.size(); 77 } 78 79 bool ASHadUnknownInsts = !AS.UnknownInsts.empty(); 80 if (UnknownInsts.empty()) { // Merge call sites... 81 if (ASHadUnknownInsts) { 82 std::swap(UnknownInsts, AS.UnknownInsts); 83 addRef(); 84 } 85 } else if (ASHadUnknownInsts) { 86 llvm::append_range(UnknownInsts, AS.UnknownInsts); 87 AS.UnknownInsts.clear(); 88 } 89 90 AS.Forward = this; // Forward across AS now... 91 addRef(); // AS is now pointing to us... 92 93 // Merge the list of constituent pointers... 94 if (AS.PtrList) { 95 SetSize += AS.size(); 96 AS.SetSize = 0; 97 *PtrListEnd = AS.PtrList; 98 AS.PtrList->setPrevInList(PtrListEnd); 99 PtrListEnd = AS.PtrListEnd; 100 101 AS.PtrList = nullptr; 102 AS.PtrListEnd = &AS.PtrList; 103 assert(*AS.PtrListEnd == nullptr && "End of list is not null?"); 104 } 105 if (ASHadUnknownInsts) 106 AS.dropRef(AST); 107 } 108 109 void AliasSetTracker::removeAliasSet(AliasSet *AS) { 110 if (AliasSet *Fwd = AS->Forward) { 111 Fwd->dropRef(*this); 112 AS->Forward = nullptr; 113 } else // Update TotalMayAliasSetSize only if not forwarding. 114 if (AS->Alias == AliasSet::SetMayAlias) 115 TotalMayAliasSetSize -= AS->size(); 116 117 AliasSets.erase(AS); 118 // If we've removed the saturated alias set, set saturated marker back to 119 // nullptr and ensure this tracker is empty. 120 if (AS == AliasAnyAS) { 121 AliasAnyAS = nullptr; 122 assert(AliasSets.empty() && "Tracker not empty"); 123 } 124 } 125 126 void AliasSet::removeFromTracker(AliasSetTracker &AST) { 127 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!"); 128 AST.removeAliasSet(this); 129 } 130 131 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry, 132 LocationSize Size, const AAMDNodes &AAInfo, 133 bool KnownMustAlias, bool SkipSizeUpdate) { 134 assert(!Entry.hasAliasSet() && "Entry already in set!"); 135 136 // Check to see if we have to downgrade to _may_ alias. 137 if (isMustAlias()) 138 if (PointerRec *P = getSomePointer()) { 139 if (!KnownMustAlias) { 140 AliasAnalysis &AA = AST.getAliasAnalysis(); 141 AliasResult Result = AA.alias( 142 MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()), 143 MemoryLocation(Entry.getValue(), Size, AAInfo)); 144 if (Result != MustAlias) { 145 Alias = SetMayAlias; 146 AST.TotalMayAliasSetSize += size(); 147 } 148 assert(Result != NoAlias && "Cannot be part of must set!"); 149 } else if (!SkipSizeUpdate) 150 P->updateSizeAndAAInfo(Size, AAInfo); 151 } 152 153 Entry.setAliasSet(this); 154 Entry.updateSizeAndAAInfo(Size, AAInfo); 155 156 // Add it to the end of the list... 157 ++SetSize; 158 assert(*PtrListEnd == nullptr && "End of list is not null?"); 159 *PtrListEnd = &Entry; 160 PtrListEnd = Entry.setPrevInList(PtrListEnd); 161 assert(*PtrListEnd == nullptr && "End of list is not null?"); 162 // Entry points to alias set. 163 addRef(); 164 165 if (Alias == SetMayAlias) 166 AST.TotalMayAliasSetSize++; 167 } 168 169 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) { 170 if (UnknownInsts.empty()) 171 addRef(); 172 UnknownInsts.emplace_back(I); 173 174 // Guards are marked as modifying memory for control flow modelling purposes, 175 // but don't actually modify any specific memory location. 176 using namespace PatternMatch; 177 bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) && 178 !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>())); 179 if (!MayWriteMemory) { 180 Alias = SetMayAlias; 181 Access |= RefAccess; 182 return; 183 } 184 185 // FIXME: This should use mod/ref information to make this not suck so bad 186 Alias = SetMayAlias; 187 Access = ModRefAccess; 188 } 189 190 /// aliasesPointer - If the specified pointer "may" (or must) alias one of the 191 /// members in the set return the appropriate AliasResult. Otherwise return 192 /// NoAlias. 193 /// 194 AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size, 195 const AAMDNodes &AAInfo, 196 AliasAnalysis &AA) const { 197 if (AliasAny) 198 return MayAlias; 199 200 if (Alias == SetMustAlias) { 201 assert(UnknownInsts.empty() && "Illegal must alias set!"); 202 203 // If this is a set of MustAliases, only check to see if the pointer aliases 204 // SOME value in the set. 205 PointerRec *SomePtr = getSomePointer(); 206 assert(SomePtr && "Empty must-alias set??"); 207 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(), 208 SomePtr->getAAInfo()), 209 MemoryLocation(Ptr, Size, AAInfo)); 210 } 211 212 // If this is a may-alias set, we have to check all of the pointers in the set 213 // to be sure it doesn't alias the set... 214 for (iterator I = begin(), E = end(); I != E; ++I) 215 if (AliasResult AR = AA.alias( 216 MemoryLocation(Ptr, Size, AAInfo), 217 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))) 218 return AR; 219 220 // Check the unknown instructions... 221 if (!UnknownInsts.empty()) { 222 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) 223 if (auto *Inst = getUnknownInst(i)) 224 if (isModOrRefSet( 225 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo)))) 226 return MayAlias; 227 } 228 229 return NoAlias; 230 } 231 232 bool AliasSet::aliasesUnknownInst(const Instruction *Inst, 233 AliasAnalysis &AA) const { 234 235 if (AliasAny) 236 return true; 237 238 assert(Inst->mayReadOrWriteMemory() && 239 "Instruction must either read or write memory."); 240 241 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) { 242 if (auto *UnknownInst = getUnknownInst(i)) { 243 const auto *C1 = dyn_cast<CallBase>(UnknownInst); 244 const auto *C2 = dyn_cast<CallBase>(Inst); 245 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) || 246 isModOrRefSet(AA.getModRefInfo(C2, C1))) 247 return true; 248 } 249 } 250 251 for (iterator I = begin(), E = end(); I != E; ++I) 252 if (isModOrRefSet(AA.getModRefInfo( 253 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))) 254 return true; 255 256 return false; 257 } 258 259 Instruction* AliasSet::getUniqueInstruction() { 260 if (AliasAny) 261 // May have collapses alias set 262 return nullptr; 263 if (begin() != end()) { 264 if (!UnknownInsts.empty()) 265 // Another instruction found 266 return nullptr; 267 if (std::next(begin()) != end()) 268 // Another instruction found 269 return nullptr; 270 Value *Addr = begin()->getValue(); 271 assert(!Addr->user_empty() && 272 "where's the instruction which added this pointer?"); 273 if (std::next(Addr->user_begin()) != Addr->user_end()) 274 // Another instruction found -- this is really restrictive 275 // TODO: generalize! 276 return nullptr; 277 return cast<Instruction>(*(Addr->user_begin())); 278 } 279 if (1 != UnknownInsts.size()) 280 return nullptr; 281 return cast<Instruction>(UnknownInsts[0]); 282 } 283 284 void AliasSetTracker::clear() { 285 // Delete all the PointerRec entries. 286 for (auto &I : PointerMap) 287 I.second->eraseFromList(); 288 289 PointerMap.clear(); 290 291 // The alias sets should all be clear now. 292 AliasSets.clear(); 293 } 294 295 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may 296 /// alias the pointer. Return the unified set, or nullptr if no set that aliases 297 /// the pointer was found. MustAliasAll is updated to true/false if the pointer 298 /// is found to MustAlias all the sets it merged. 299 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr, 300 LocationSize Size, 301 const AAMDNodes &AAInfo, 302 bool &MustAliasAll) { 303 AliasSet *FoundSet = nullptr; 304 AliasResult AllAR = MustAlias; 305 for (AliasSet &AS : llvm::make_early_inc_range(*this)) { 306 if (AS.Forward) 307 continue; 308 309 AliasResult AR = AS.aliasesPointer(Ptr, Size, AAInfo, AA); 310 if (AR == NoAlias) 311 continue; 312 313 AllAR = 314 AliasResult(AllAR & AR); // Possible downgrade to May/Partial, even No 315 316 if (!FoundSet) { 317 // If this is the first alias set ptr can go into, remember it. 318 FoundSet = &AS; 319 } else { 320 // Otherwise, we must merge the sets. 321 FoundSet->mergeSetIn(AS, *this); 322 } 323 } 324 325 MustAliasAll = (AllAR == MustAlias); 326 return FoundSet; 327 } 328 329 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) { 330 AliasSet *FoundSet = nullptr; 331 for (AliasSet &AS : llvm::make_early_inc_range(*this)) { 332 if (AS.Forward || !AS.aliasesUnknownInst(Inst, AA)) 333 continue; 334 if (!FoundSet) { 335 // If this is the first alias set ptr can go into, remember it. 336 FoundSet = &AS; 337 } else { 338 // Otherwise, we must merge the sets. 339 FoundSet->mergeSetIn(AS, *this); 340 } 341 } 342 return FoundSet; 343 } 344 345 AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) { 346 347 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr); 348 const LocationSize Size = MemLoc.Size; 349 const AAMDNodes &AAInfo = MemLoc.AATags; 350 351 AliasSet::PointerRec &Entry = getEntryFor(Pointer); 352 353 if (AliasAnyAS) { 354 // At this point, the AST is saturated, so we only have one active alias 355 // set. That means we already know which alias set we want to return, and 356 // just need to add the pointer to that set to keep the data structure 357 // consistent. 358 // This, of course, means that we will never need a merge here. 359 if (Entry.hasAliasSet()) { 360 Entry.updateSizeAndAAInfo(Size, AAInfo); 361 assert(Entry.getAliasSet(*this) == AliasAnyAS && 362 "Entry in saturated AST must belong to only alias set"); 363 } else { 364 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo); 365 } 366 return *AliasAnyAS; 367 } 368 369 bool MustAliasAll = false; 370 // Check to see if the pointer is already known. 371 if (Entry.hasAliasSet()) { 372 // If the size changed, we may need to merge several alias sets. 373 // Note that we can *not* return the result of mergeAliasSetsForPointer 374 // due to a quirk of alias analysis behavior. Since alias(undef, undef) 375 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the 376 // the right set for undef, even if it exists. 377 if (Entry.updateSizeAndAAInfo(Size, AAInfo)) 378 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll); 379 // Return the set! 380 return *Entry.getAliasSet(*this)->getForwardedTarget(*this); 381 } 382 383 if (AliasSet *AS = 384 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) { 385 // Add it to the alias set it aliases. 386 AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll); 387 return *AS; 388 } 389 390 // Otherwise create a new alias set to hold the loaded pointer. 391 AliasSets.push_back(new AliasSet()); 392 AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true); 393 return AliasSets.back(); 394 } 395 396 void AliasSetTracker::add(Value *Ptr, LocationSize Size, 397 const AAMDNodes &AAInfo) { 398 addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess); 399 } 400 401 void AliasSetTracker::add(LoadInst *LI) { 402 if (isStrongerThanMonotonic(LI->getOrdering())) 403 return addUnknown(LI); 404 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess); 405 } 406 407 void AliasSetTracker::add(StoreInst *SI) { 408 if (isStrongerThanMonotonic(SI->getOrdering())) 409 return addUnknown(SI); 410 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess); 411 } 412 413 void AliasSetTracker::add(VAArgInst *VAAI) { 414 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess); 415 } 416 417 void AliasSetTracker::add(AnyMemSetInst *MSI) { 418 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess); 419 } 420 421 void AliasSetTracker::add(AnyMemTransferInst *MTI) { 422 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess); 423 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess); 424 } 425 426 void AliasSetTracker::addUnknown(Instruction *Inst) { 427 if (isa<DbgInfoIntrinsic>(Inst)) 428 return; // Ignore DbgInfo Intrinsics. 429 430 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) { 431 // These intrinsics will show up as affecting memory, but they are just 432 // markers. 433 switch (II->getIntrinsicID()) { 434 default: 435 break; 436 // FIXME: Add lifetime/invariant intrinsics (See: PR30807). 437 case Intrinsic::assume: 438 case Intrinsic::experimental_noalias_scope_decl: 439 case Intrinsic::sideeffect: 440 case Intrinsic::pseudoprobe: 441 return; 442 } 443 } 444 if (!Inst->mayReadOrWriteMemory()) 445 return; // doesn't alias anything 446 447 if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) { 448 AS->addUnknownInst(Inst, AA); 449 return; 450 } 451 AliasSets.push_back(new AliasSet()); 452 AliasSets.back().addUnknownInst(Inst, AA); 453 } 454 455 void AliasSetTracker::add(Instruction *I) { 456 // Dispatch to one of the other add methods. 457 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 458 return add(LI); 459 if (StoreInst *SI = dyn_cast<StoreInst>(I)) 460 return add(SI); 461 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I)) 462 return add(VAAI); 463 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I)) 464 return add(MSI); 465 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I)) 466 return add(MTI); 467 468 // Handle all calls with known mod/ref sets genericall 469 if (auto *Call = dyn_cast<CallBase>(I)) 470 if (Call->onlyAccessesArgMemory()) { 471 auto getAccessFromModRef = [](ModRefInfo MRI) { 472 if (isRefSet(MRI) && isModSet(MRI)) 473 return AliasSet::ModRefAccess; 474 else if (isModSet(MRI)) 475 return AliasSet::ModAccess; 476 else if (isRefSet(MRI)) 477 return AliasSet::RefAccess; 478 else 479 return AliasSet::NoAccess; 480 }; 481 482 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call)); 483 484 // Some intrinsics are marked as modifying memory for control flow 485 // modelling purposes, but don't actually modify any specific memory 486 // location. 487 using namespace PatternMatch; 488 if (Call->use_empty() && 489 match(Call, m_Intrinsic<Intrinsic::invariant_start>())) 490 CallMask = clearMod(CallMask); 491 492 for (auto IdxArgPair : enumerate(Call->args())) { 493 int ArgIdx = IdxArgPair.index(); 494 const Value *Arg = IdxArgPair.value(); 495 if (!Arg->getType()->isPointerTy()) 496 continue; 497 MemoryLocation ArgLoc = 498 MemoryLocation::getForArgument(Call, ArgIdx, nullptr); 499 ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx); 500 ArgMask = intersectModRef(CallMask, ArgMask); 501 if (!isNoModRef(ArgMask)) 502 addPointer(ArgLoc, getAccessFromModRef(ArgMask)); 503 } 504 return; 505 } 506 507 return addUnknown(I); 508 } 509 510 void AliasSetTracker::add(BasicBlock &BB) { 511 for (auto &I : BB) 512 add(&I); 513 } 514 515 void AliasSetTracker::add(const AliasSetTracker &AST) { 516 assert(&AA == &AST.AA && 517 "Merging AliasSetTracker objects with different Alias Analyses!"); 518 519 // Loop over all of the alias sets in AST, adding the pointers contained 520 // therein into the current alias sets. This can cause alias sets to be 521 // merged together in the current AST. 522 for (const AliasSet &AS : AST) { 523 if (AS.Forward) 524 continue; // Ignore forwarding alias sets 525 526 // If there are any call sites in the alias set, add them to this AST. 527 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i) 528 if (auto *Inst = AS.getUnknownInst(i)) 529 add(Inst); 530 531 // Loop over all of the pointers in this alias set. 532 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) 533 addPointer( 534 MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()), 535 (AliasSet::AccessLattice)AS.Access); 536 } 537 } 538 539 void AliasSetTracker::addAllInstructionsInLoopUsingMSSA() { 540 assert(MSSA && L && "MSSA and L must be available"); 541 for (const BasicBlock *BB : L->blocks()) 542 if (auto *Accesses = MSSA->getBlockAccesses(BB)) 543 for (auto &Access : *Accesses) 544 if (auto *MUD = dyn_cast<MemoryUseOrDef>(&Access)) 545 add(MUD->getMemoryInst()); 546 } 547 548 // deleteValue method - This method is used to remove a pointer value from the 549 // AliasSetTracker entirely. It should be used when an instruction is deleted 550 // from the program to update the AST. If you don't use this, you would have 551 // dangling pointers to deleted instructions. 552 // 553 void AliasSetTracker::deleteValue(Value *PtrVal) { 554 // First, look up the PointerRec for this pointer. 555 PointerMapType::iterator I = PointerMap.find_as(PtrVal); 556 if (I == PointerMap.end()) return; // Noop 557 558 // If we found one, remove the pointer from the alias set it is in. 559 AliasSet::PointerRec *PtrValEnt = I->second; 560 AliasSet *AS = PtrValEnt->getAliasSet(*this); 561 562 // Unlink and delete from the list of values. 563 PtrValEnt->eraseFromList(); 564 565 if (AS->Alias == AliasSet::SetMayAlias) { 566 AS->SetSize--; 567 TotalMayAliasSetSize--; 568 } 569 570 // Stop using the alias set. 571 AS->dropRef(*this); 572 573 PointerMap.erase(I); 574 } 575 576 // copyValue - This method should be used whenever a preexisting value in the 577 // program is copied or cloned, introducing a new value. Note that it is ok for 578 // clients that use this method to introduce the same value multiple times: if 579 // the tracker already knows about a value, it will ignore the request. 580 // 581 void AliasSetTracker::copyValue(Value *From, Value *To) { 582 // First, look up the PointerRec for this pointer. 583 PointerMapType::iterator I = PointerMap.find_as(From); 584 if (I == PointerMap.end()) 585 return; // Noop 586 assert(I->second->hasAliasSet() && "Dead entry?"); 587 588 AliasSet::PointerRec &Entry = getEntryFor(To); 589 if (Entry.hasAliasSet()) return; // Already in the tracker! 590 591 // getEntryFor above may invalidate iterator \c I, so reinitialize it. 592 I = PointerMap.find_as(From); 593 // Add it to the alias set it aliases... 594 AliasSet *AS = I->second->getAliasSet(*this); 595 AS->addPointer(*this, Entry, I->second->getSize(), I->second->getAAInfo(), 596 true, true); 597 } 598 599 AliasSet &AliasSetTracker::mergeAllAliasSets() { 600 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) && 601 "Full merge should happen once, when the saturation threshold is " 602 "reached"); 603 604 // Collect all alias sets, so that we can drop references with impunity 605 // without worrying about iterator invalidation. 606 std::vector<AliasSet *> ASVector; 607 ASVector.reserve(SaturationThreshold); 608 for (AliasSet &AS : *this) 609 ASVector.push_back(&AS); 610 611 // Copy all instructions and pointers into a new set, and forward all other 612 // sets to it. 613 AliasSets.push_back(new AliasSet()); 614 AliasAnyAS = &AliasSets.back(); 615 AliasAnyAS->Alias = AliasSet::SetMayAlias; 616 AliasAnyAS->Access = AliasSet::ModRefAccess; 617 AliasAnyAS->AliasAny = true; 618 619 for (auto Cur : ASVector) { 620 // If Cur was already forwarding, just forward to the new AS instead. 621 AliasSet *FwdTo = Cur->Forward; 622 if (FwdTo) { 623 Cur->Forward = AliasAnyAS; 624 AliasAnyAS->addRef(); 625 FwdTo->dropRef(*this); 626 continue; 627 } 628 629 // Otherwise, perform the actual merge. 630 AliasAnyAS->mergeSetIn(*Cur, *this); 631 } 632 633 return *AliasAnyAS; 634 } 635 636 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc, 637 AliasSet::AccessLattice E) { 638 AliasSet &AS = getAliasSetFor(Loc); 639 AS.Access |= E; 640 641 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) { 642 // The AST is now saturated. From here on, we conservatively consider all 643 // pointers to alias each-other. 644 return mergeAllAliasSets(); 645 } 646 647 return AS; 648 } 649 650 //===----------------------------------------------------------------------===// 651 // AliasSet/AliasSetTracker Printing Support 652 //===----------------------------------------------------------------------===// 653 654 void AliasSet::print(raw_ostream &OS) const { 655 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] "; 656 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, "; 657 switch (Access) { 658 case NoAccess: OS << "No access "; break; 659 case RefAccess: OS << "Ref "; break; 660 case ModAccess: OS << "Mod "; break; 661 case ModRefAccess: OS << "Mod/Ref "; break; 662 default: llvm_unreachable("Bad value for Access!"); 663 } 664 if (Forward) 665 OS << " forwarding to " << (void*)Forward; 666 667 if (!empty()) { 668 OS << "Pointers: "; 669 for (iterator I = begin(), E = end(); I != E; ++I) { 670 if (I != begin()) OS << ", "; 671 I.getPointer()->printAsOperand(OS << "("); 672 if (I.getSize() == LocationSize::afterPointer()) 673 OS << ", unknown after)"; 674 else if (I.getSize() == LocationSize::beforeOrAfterPointer()) 675 OS << ", unknown before-or-after)"; 676 else 677 OS << ", " << I.getSize() << ")"; 678 } 679 } 680 if (!UnknownInsts.empty()) { 681 OS << "\n " << UnknownInsts.size() << " Unknown instructions: "; 682 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) { 683 if (i) OS << ", "; 684 if (auto *I = getUnknownInst(i)) { 685 if (I->hasName()) 686 I->printAsOperand(OS); 687 else 688 I->print(OS); 689 } 690 } 691 } 692 OS << "\n"; 693 } 694 695 void AliasSetTracker::print(raw_ostream &OS) const { 696 OS << "Alias Set Tracker: " << AliasSets.size(); 697 if (AliasAnyAS) 698 OS << " (Saturated)"; 699 OS << " alias sets for " << PointerMap.size() << " pointer values.\n"; 700 for (const AliasSet &AS : *this) 701 AS.print(OS); 702 OS << "\n"; 703 } 704 705 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 706 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); } 707 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); } 708 #endif 709 710 //===----------------------------------------------------------------------===// 711 // ASTCallbackVH Class Implementation 712 //===----------------------------------------------------------------------===// 713 714 void AliasSetTracker::ASTCallbackVH::deleted() { 715 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!"); 716 AST->deleteValue(getValPtr()); 717 // this now dangles! 718 } 719 720 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) { 721 AST->copyValue(getValPtr(), V); 722 } 723 724 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast) 725 : CallbackVH(V), AST(ast) {} 726 727 AliasSetTracker::ASTCallbackVH & 728 AliasSetTracker::ASTCallbackVH::operator=(Value *V) { 729 return *this = ASTCallbackVH(V, AST); 730 } 731 732 //===----------------------------------------------------------------------===// 733 // AliasSetPrinter Pass 734 //===----------------------------------------------------------------------===// 735 736 namespace { 737 738 class AliasSetPrinter : public FunctionPass { 739 public: 740 static char ID; // Pass identification, replacement for typeid 741 742 AliasSetPrinter() : FunctionPass(ID) { 743 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry()); 744 } 745 746 void getAnalysisUsage(AnalysisUsage &AU) const override { 747 AU.setPreservesAll(); 748 AU.addRequired<AAResultsWrapperPass>(); 749 } 750 751 bool runOnFunction(Function &F) override { 752 auto &AAWP = getAnalysis<AAResultsWrapperPass>(); 753 AliasSetTracker Tracker(AAWP.getAAResults()); 754 errs() << "Alias sets for function '" << F.getName() << "':\n"; 755 for (Instruction &I : instructions(F)) 756 Tracker.add(&I); 757 Tracker.print(errs()); 758 return false; 759 } 760 }; 761 762 } // end anonymous namespace 763 764 char AliasSetPrinter::ID = 0; 765 766 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets", 767 "Alias Set Printer", false, true) 768 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 769 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets", 770 "Alias Set Printer", false, true) 771 772 AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {} 773 774 PreservedAnalyses AliasSetsPrinterPass::run(Function &F, 775 FunctionAnalysisManager &AM) { 776 auto &AA = AM.getResult<AAManager>(F); 777 AliasSetTracker Tracker(AA); 778 OS << "Alias sets for function '" << F.getName() << "':\n"; 779 for (Instruction &I : instructions(F)) 780 Tracker.add(&I); 781 Tracker.print(OS); 782 return PreservedAnalyses::all(); 783 } 784