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/PatternMatch.h" 27 #include "llvm/IR/Value.h" 28 #include "llvm/InitializePasses.h" 29 #include "llvm/Pass.h" 30 #include "llvm/Support/AtomicOrdering.h" 31 #include "llvm/Support/CommandLine.h" 32 #include "llvm/Support/Compiler.h" 33 #include "llvm/Support/Debug.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/raw_ostream.h" 36 37 using namespace llvm; 38 39 static cl::opt<unsigned> 40 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden, 41 cl::init(250), 42 cl::desc("The maximum number of pointers may-alias " 43 "sets may contain before degradation")); 44 45 /// mergeSetIn - Merge the specified alias set into this alias set. 46 /// 47 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) { 48 assert(!AS.Forward && "Alias set is already forwarding!"); 49 assert(!Forward && "This set is a forwarding set!!"); 50 51 bool WasMustAlias = (Alias == SetMustAlias); 52 // Update the alias and access types of this set... 53 Access |= AS.Access; 54 Alias |= AS.Alias; 55 56 if (Alias == SetMustAlias) { 57 // Check that these two merged sets really are must aliases. Since both 58 // used to be must-alias sets, we can just check any pointer from each set 59 // for aliasing. 60 AliasAnalysis &AA = AST.getAliasAnalysis(); 61 PointerRec *L = getSomePointer(); 62 PointerRec *R = AS.getSomePointer(); 63 64 // If the pointers are not a must-alias pair, this set becomes a may alias. 65 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()), 66 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) != 67 MustAlias) 68 Alias = SetMayAlias; 69 } 70 71 if (Alias == SetMayAlias) { 72 if (WasMustAlias) 73 AST.TotalMayAliasSetSize += size(); 74 if (AS.Alias == SetMustAlias) 75 AST.TotalMayAliasSetSize += AS.size(); 76 } 77 78 bool ASHadUnknownInsts = !AS.UnknownInsts.empty(); 79 if (UnknownInsts.empty()) { // Merge call sites... 80 if (ASHadUnknownInsts) { 81 std::swap(UnknownInsts, AS.UnknownInsts); 82 addRef(); 83 } 84 } else if (ASHadUnknownInsts) { 85 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end()); 86 AS.UnknownInsts.clear(); 87 } 88 89 AS.Forward = this; // Forward across AS now... 90 addRef(); // AS is now pointing to us... 91 92 // Merge the list of constituent pointers... 93 if (AS.PtrList) { 94 SetSize += AS.size(); 95 AS.SetSize = 0; 96 *PtrListEnd = AS.PtrList; 97 AS.PtrList->setPrevInList(PtrListEnd); 98 PtrListEnd = AS.PtrListEnd; 99 100 AS.PtrList = nullptr; 101 AS.PtrListEnd = &AS.PtrList; 102 assert(*AS.PtrListEnd == nullptr && "End of list is not null?"); 103 } 104 if (ASHadUnknownInsts) 105 AS.dropRef(AST); 106 } 107 108 void AliasSetTracker::removeAliasSet(AliasSet *AS) { 109 if (AliasSet *Fwd = AS->Forward) { 110 Fwd->dropRef(*this); 111 AS->Forward = nullptr; 112 } else // Update TotalMayAliasSetSize only if not forwarding. 113 if (AS->Alias == AliasSet::SetMayAlias) 114 TotalMayAliasSetSize -= AS->size(); 115 116 AliasSets.erase(AS); 117 // If we've removed the saturated alias set, set saturated marker back to 118 // nullptr and ensure this tracker is empty. 119 if (AS == AliasAnyAS) { 120 AliasAnyAS = nullptr; 121 assert(AliasSets.empty() && "Tracker not empty"); 122 } 123 } 124 125 void AliasSet::removeFromTracker(AliasSetTracker &AST) { 126 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!"); 127 AST.removeAliasSet(this); 128 } 129 130 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry, 131 LocationSize Size, const AAMDNodes &AAInfo, 132 bool KnownMustAlias, bool SkipSizeUpdate) { 133 assert(!Entry.hasAliasSet() && "Entry already in set!"); 134 135 // Check to see if we have to downgrade to _may_ alias. 136 if (isMustAlias()) 137 if (PointerRec *P = getSomePointer()) { 138 if (!KnownMustAlias) { 139 AliasAnalysis &AA = AST.getAliasAnalysis(); 140 AliasResult Result = AA.alias( 141 MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()), 142 MemoryLocation(Entry.getValue(), Size, AAInfo)); 143 if (Result != MustAlias) { 144 Alias = SetMayAlias; 145 AST.TotalMayAliasSetSize += size(); 146 } 147 assert(Result != NoAlias && "Cannot be part of must set!"); 148 } else if (!SkipSizeUpdate) 149 P->updateSizeAndAAInfo(Size, AAInfo); 150 } 151 152 Entry.setAliasSet(this); 153 Entry.updateSizeAndAAInfo(Size, AAInfo); 154 155 // Add it to the end of the list... 156 ++SetSize; 157 assert(*PtrListEnd == nullptr && "End of list is not null?"); 158 *PtrListEnd = &Entry; 159 PtrListEnd = Entry.setPrevInList(PtrListEnd); 160 assert(*PtrListEnd == nullptr && "End of list is not null?"); 161 // Entry points to alias set. 162 addRef(); 163 164 if (Alias == SetMayAlias) 165 AST.TotalMayAliasSetSize++; 166 } 167 168 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) { 169 if (UnknownInsts.empty()) 170 addRef(); 171 UnknownInsts.emplace_back(I); 172 173 // Guards are marked as modifying memory for control flow modelling purposes, 174 // but don't actually modify any specific memory location. 175 using namespace PatternMatch; 176 bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) && 177 !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>())); 178 if (!MayWriteMemory) { 179 Alias = SetMayAlias; 180 Access |= RefAccess; 181 return; 182 } 183 184 // FIXME: This should use mod/ref information to make this not suck so bad 185 Alias = SetMayAlias; 186 Access = ModRefAccess; 187 } 188 189 /// aliasesPointer - If the specified pointer "may" (or must) alias one of the 190 /// members in the set return the appropriate AliasResult. Otherwise return 191 /// NoAlias. 192 /// 193 AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size, 194 const AAMDNodes &AAInfo, 195 AliasAnalysis &AA) const { 196 if (AliasAny) 197 return MayAlias; 198 199 if (Alias == SetMustAlias) { 200 assert(UnknownInsts.empty() && "Illegal must alias set!"); 201 202 // If this is a set of MustAliases, only check to see if the pointer aliases 203 // SOME value in the set. 204 PointerRec *SomePtr = getSomePointer(); 205 assert(SomePtr && "Empty must-alias set??"); 206 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(), 207 SomePtr->getAAInfo()), 208 MemoryLocation(Ptr, Size, AAInfo)); 209 } 210 211 // If this is a may-alias set, we have to check all of the pointers in the set 212 // to be sure it doesn't alias the set... 213 for (iterator I = begin(), E = end(); I != E; ++I) 214 if (AliasResult AR = AA.alias( 215 MemoryLocation(Ptr, Size, AAInfo), 216 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))) 217 return AR; 218 219 // Check the unknown instructions... 220 if (!UnknownInsts.empty()) { 221 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) 222 if (auto *Inst = getUnknownInst(i)) 223 if (isModOrRefSet( 224 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo)))) 225 return MayAlias; 226 } 227 228 return NoAlias; 229 } 230 231 bool AliasSet::aliasesUnknownInst(const Instruction *Inst, 232 AliasAnalysis &AA) const { 233 234 if (AliasAny) 235 return true; 236 237 assert(Inst->mayReadOrWriteMemory() && 238 "Instruction must either read or write memory."); 239 240 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) { 241 if (auto *UnknownInst = getUnknownInst(i)) { 242 const auto *C1 = dyn_cast<CallBase>(UnknownInst); 243 const auto *C2 = dyn_cast<CallBase>(Inst); 244 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) || 245 isModOrRefSet(AA.getModRefInfo(C2, C1))) 246 return true; 247 } 248 } 249 250 for (iterator I = begin(), E = end(); I != E; ++I) 251 if (isModOrRefSet(AA.getModRefInfo( 252 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))) 253 return true; 254 255 return false; 256 } 257 258 Instruction* AliasSet::getUniqueInstruction() { 259 if (AliasAny) 260 // May have collapses alias set 261 return nullptr; 262 if (begin() != end()) { 263 if (!UnknownInsts.empty()) 264 // Another instruction found 265 return nullptr; 266 if (std::next(begin()) != end()) 267 // Another instruction found 268 return nullptr; 269 Value *Addr = begin()->getValue(); 270 assert(!Addr->user_empty() && 271 "where's the instruction which added this pointer?"); 272 if (std::next(Addr->user_begin()) != Addr->user_end()) 273 // Another instruction found -- this is really restrictive 274 // TODO: generalize! 275 return nullptr; 276 return cast<Instruction>(*(Addr->user_begin())); 277 } 278 if (1 != UnknownInsts.size()) 279 return nullptr; 280 return cast<Instruction>(UnknownInsts[0]); 281 } 282 283 void AliasSetTracker::clear() { 284 // Delete all the PointerRec entries. 285 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end(); 286 I != E; ++I) 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 (iterator I = begin(), E = end(); I != E;) { 306 iterator Cur = I++; 307 if (Cur->Forward) 308 continue; 309 310 AliasResult AR = Cur->aliasesPointer(Ptr, Size, AAInfo, AA); 311 if (AR == NoAlias) 312 continue; 313 314 AllAR = 315 AliasResult(AllAR & AR); // Possible downgrade to May/Partial, even No 316 317 if (!FoundSet) { 318 // If this is the first alias set ptr can go into, remember it. 319 FoundSet = &*Cur; 320 } else { 321 // Otherwise, we must merge the sets. 322 FoundSet->mergeSetIn(*Cur, *this); 323 } 324 } 325 326 MustAliasAll = (AllAR == MustAlias); 327 return FoundSet; 328 } 329 330 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) { 331 AliasSet *FoundSet = nullptr; 332 for (iterator I = begin(), E = end(); I != E;) { 333 iterator Cur = I++; 334 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA)) 335 continue; 336 if (!FoundSet) { 337 // If this is the first alias set ptr can go into, remember it. 338 FoundSet = &*Cur; 339 } else { 340 // Otherwise, we must merge the sets. 341 FoundSet->mergeSetIn(*Cur, *this); 342 } 343 } 344 return FoundSet; 345 } 346 347 AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) { 348 349 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr); 350 const LocationSize Size = MemLoc.Size; 351 const AAMDNodes &AAInfo = MemLoc.AATags; 352 353 AliasSet::PointerRec &Entry = getEntryFor(Pointer); 354 355 if (AliasAnyAS) { 356 // At this point, the AST is saturated, so we only have one active alias 357 // set. That means we already know which alias set we want to return, and 358 // just need to add the pointer to that set to keep the data structure 359 // consistent. 360 // This, of course, means that we will never need a merge here. 361 if (Entry.hasAliasSet()) { 362 Entry.updateSizeAndAAInfo(Size, AAInfo); 363 assert(Entry.getAliasSet(*this) == AliasAnyAS && 364 "Entry in saturated AST must belong to only alias set"); 365 } else { 366 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo); 367 } 368 return *AliasAnyAS; 369 } 370 371 bool MustAliasAll = false; 372 // Check to see if the pointer is already known. 373 if (Entry.hasAliasSet()) { 374 // If the size changed, we may need to merge several alias sets. 375 // Note that we can *not* return the result of mergeAliasSetsForPointer 376 // due to a quirk of alias analysis behavior. Since alias(undef, undef) 377 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the 378 // the right set for undef, even if it exists. 379 if (Entry.updateSizeAndAAInfo(Size, AAInfo)) 380 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll); 381 // Return the set! 382 return *Entry.getAliasSet(*this)->getForwardedTarget(*this); 383 } 384 385 if (AliasSet *AS = 386 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) { 387 // Add it to the alias set it aliases. 388 AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll); 389 return *AS; 390 } 391 392 // Otherwise create a new alias set to hold the loaded pointer. 393 AliasSets.push_back(new AliasSet()); 394 AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true); 395 return AliasSets.back(); 396 } 397 398 void AliasSetTracker::add(Value *Ptr, LocationSize Size, 399 const AAMDNodes &AAInfo) { 400 addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess); 401 } 402 403 void AliasSetTracker::add(LoadInst *LI) { 404 if (isStrongerThanMonotonic(LI->getOrdering())) 405 return addUnknown(LI); 406 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess); 407 } 408 409 void AliasSetTracker::add(StoreInst *SI) { 410 if (isStrongerThanMonotonic(SI->getOrdering())) 411 return addUnknown(SI); 412 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess); 413 } 414 415 void AliasSetTracker::add(VAArgInst *VAAI) { 416 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess); 417 } 418 419 void AliasSetTracker::add(AnyMemSetInst *MSI) { 420 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess); 421 } 422 423 void AliasSetTracker::add(AnyMemTransferInst *MTI) { 424 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess); 425 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess); 426 } 427 428 void AliasSetTracker::addUnknown(Instruction *Inst) { 429 if (isa<DbgInfoIntrinsic>(Inst)) 430 return; // Ignore DbgInfo Intrinsics. 431 432 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) { 433 // These intrinsics will show up as affecting memory, but they are just 434 // markers. 435 switch (II->getIntrinsicID()) { 436 default: 437 break; 438 // FIXME: Add lifetime/invariant intrinsics (See: PR30807). 439 case Intrinsic::assume: 440 case Intrinsic::sideeffect: 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 (iterator I = begin(), E = end(); I != E; I++) 609 ASVector.push_back(&*I); 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::unknown()) 673 OS << ", unknown)"; 674 else 675 OS << ", " << I.getSize() << ")"; 676 } 677 } 678 if (!UnknownInsts.empty()) { 679 OS << "\n " << UnknownInsts.size() << " Unknown instructions: "; 680 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) { 681 if (i) OS << ", "; 682 if (auto *I = getUnknownInst(i)) { 683 if (I->hasName()) 684 I->printAsOperand(OS); 685 else 686 I->print(OS); 687 } 688 } 689 } 690 OS << "\n"; 691 } 692 693 void AliasSetTracker::print(raw_ostream &OS) const { 694 OS << "Alias Set Tracker: " << AliasSets.size(); 695 if (AliasAnyAS) 696 OS << " (Saturated)"; 697 OS << " alias sets for " << PointerMap.size() << " pointer values.\n"; 698 for (const AliasSet &AS : *this) 699 AS.print(OS); 700 OS << "\n"; 701 } 702 703 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 704 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); } 705 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); } 706 #endif 707 708 //===----------------------------------------------------------------------===// 709 // ASTCallbackVH Class Implementation 710 //===----------------------------------------------------------------------===// 711 712 void AliasSetTracker::ASTCallbackVH::deleted() { 713 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!"); 714 AST->deleteValue(getValPtr()); 715 // this now dangles! 716 } 717 718 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) { 719 AST->copyValue(getValPtr(), V); 720 } 721 722 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast) 723 : CallbackVH(V), AST(ast) {} 724 725 AliasSetTracker::ASTCallbackVH & 726 AliasSetTracker::ASTCallbackVH::operator=(Value *V) { 727 return *this = ASTCallbackVH(V, AST); 728 } 729 730 //===----------------------------------------------------------------------===// 731 // AliasSetPrinter Pass 732 //===----------------------------------------------------------------------===// 733 734 namespace { 735 736 class AliasSetPrinter : public FunctionPass { 737 AliasSetTracker *Tracker; 738 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 Tracker = new AliasSetTracker(AAWP.getAAResults()); 754 errs() << "Alias sets for function '" << F.getName() << "':\n"; 755 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 756 Tracker->add(&*I); 757 Tracker->print(errs()); 758 delete Tracker; 759 return false; 760 } 761 }; 762 763 } // end anonymous namespace 764 765 char AliasSetPrinter::ID = 0; 766 767 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets", 768 "Alias Set Printer", false, true) 769 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 770 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets", 771 "Alias Set Printer", false, true) 772