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