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/ADT/StringExtras.h" 15 #include "llvm/Analysis/AliasAnalysis.h" 16 #include "llvm/Analysis/GuardUtils.h" 17 #include "llvm/Analysis/MemoryLocation.h" 18 #include "llvm/Config/llvm-config.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/IR/InstIterator.h" 21 #include "llvm/IR/Instructions.h" 22 #include "llvm/IR/IntrinsicInst.h" 23 #include "llvm/IR/PassManager.h" 24 #include "llvm/IR/PatternMatch.h" 25 #include "llvm/IR/Value.h" 26 #include "llvm/InitializePasses.h" 27 #include "llvm/Pass.h" 28 #include "llvm/Support/AtomicOrdering.h" 29 #include "llvm/Support/CommandLine.h" 30 #include "llvm/Support/Compiler.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/raw_ostream.h" 34 35 using namespace llvm; 36 37 static cl::opt<unsigned> 38 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden, 39 cl::init(250), 40 cl::desc("The maximum number of pointers may-alias " 41 "sets may contain before degradation")); 42 43 /// mergeSetIn - Merge the specified alias set into this alias set. 44 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST, 45 BatchAAResults &BatchAA) { 46 assert(!AS.Forward && "Alias set is already forwarding!"); 47 assert(!Forward && "This set is a forwarding set!!"); 48 49 bool WasMustAlias = (Alias == SetMustAlias); 50 // Update the alias and access types of this set... 51 Access |= AS.Access; 52 Alias |= AS.Alias; 53 54 if (Alias == SetMustAlias) { 55 // Check that these two merged sets really are must aliases. Since both 56 // used to be must-alias sets, we can just check any pointer from each set 57 // for aliasing. 58 PointerRec *L = getSomePointer(); 59 PointerRec *R = AS.getSomePointer(); 60 61 // If the pointers are not a must-alias pair, this set becomes a may alias. 62 if (!BatchAA.isMustAlias( 63 MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()), 64 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo()))) 65 Alias = SetMayAlias; 66 } 67 68 if (Alias == SetMayAlias) { 69 if (WasMustAlias) 70 AST.TotalMayAliasSetSize += size(); 71 if (AS.Alias == SetMustAlias) 72 AST.TotalMayAliasSetSize += AS.size(); 73 } 74 75 bool ASHadUnknownInsts = !AS.UnknownInsts.empty(); 76 if (UnknownInsts.empty()) { // Merge call sites... 77 if (ASHadUnknownInsts) { 78 std::swap(UnknownInsts, AS.UnknownInsts); 79 addRef(); 80 } 81 } else if (ASHadUnknownInsts) { 82 llvm::append_range(UnknownInsts, AS.UnknownInsts); 83 AS.UnknownInsts.clear(); 84 } 85 86 AS.Forward = this; // Forward across AS now... 87 addRef(); // AS is now pointing to us... 88 89 // Merge the list of constituent pointers... 90 if (AS.PtrList) { 91 SetSize += AS.size(); 92 AS.SetSize = 0; 93 *PtrListEnd = AS.PtrList; 94 AS.PtrList->setPrevInList(PtrListEnd); 95 PtrListEnd = AS.PtrListEnd; 96 97 AS.PtrList = nullptr; 98 AS.PtrListEnd = &AS.PtrList; 99 assert(*AS.PtrListEnd == nullptr && "End of list is not null?"); 100 } 101 if (ASHadUnknownInsts) 102 AS.dropRef(AST); 103 } 104 105 void AliasSetTracker::removeAliasSet(AliasSet *AS) { 106 if (AliasSet *Fwd = AS->Forward) { 107 Fwd->dropRef(*this); 108 AS->Forward = nullptr; 109 } else // Update TotalMayAliasSetSize only if not forwarding. 110 if (AS->Alias == AliasSet::SetMayAlias) 111 TotalMayAliasSetSize -= AS->size(); 112 113 AliasSets.erase(AS); 114 // If we've removed the saturated alias set, set saturated marker back to 115 // nullptr and ensure this tracker is empty. 116 if (AS == AliasAnyAS) { 117 AliasAnyAS = nullptr; 118 assert(AliasSets.empty() && "Tracker not empty"); 119 } 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, bool SkipSizeUpdate) { 130 assert(!Entry.hasAliasSet() && "Entry already in set!"); 131 132 // Check to see if we have to downgrade to _may_ alias. 133 if (isMustAlias()) 134 if (PointerRec *P = getSomePointer()) { 135 if (!KnownMustAlias) { 136 BatchAAResults &AA = AST.getAliasAnalysis(); 137 AliasResult Result = AA.alias( 138 MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()), 139 MemoryLocation(Entry.getValue(), Size, AAInfo)); 140 if (Result != AliasResult::MustAlias) { 141 Alias = SetMayAlias; 142 AST.TotalMayAliasSetSize += size(); 143 } 144 assert(Result != AliasResult::NoAlias && "Cannot be part of must set!"); 145 } else if (!SkipSizeUpdate) 146 P->updateSizeAndAAInfo(Size, AAInfo); 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, BatchAAResults &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 BatchAAResults &AA) const { 193 if (AliasAny) 194 return AliasResult::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 AliasResult AR = 212 AA.alias(MemoryLocation(Ptr, Size, AAInfo), 213 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())); 214 if (AR != AliasResult::NoAlias) 215 return AR; 216 } 217 218 // Check the unknown instructions... 219 if (!UnknownInsts.empty()) { 220 for (Instruction *Inst : UnknownInsts) 221 if (isModOrRefSet( 222 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo)))) 223 return AliasResult::MayAlias; 224 } 225 226 return AliasResult::NoAlias; 227 } 228 229 ModRefInfo AliasSet::aliasesUnknownInst(const Instruction *Inst, 230 BatchAAResults &AA) const { 231 232 if (AliasAny) 233 return ModRefInfo::ModRef; 234 235 if (!Inst->mayReadOrWriteMemory()) 236 return ModRefInfo::NoModRef; 237 238 for (Instruction *UnknownInst : UnknownInsts) { 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 // TODO: Could be more precise, but not really useful right now. 244 return ModRefInfo::ModRef; 245 } 246 } 247 248 ModRefInfo MR = ModRefInfo::NoModRef; 249 for (iterator I = begin(), E = end(); I != E; ++I) { 250 MR |= AA.getModRefInfo( 251 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())); 252 if (isModAndRefSet(MR)) 253 return MR; 254 } 255 256 return MR; 257 } 258 259 void AliasSetTracker::clear() { 260 // Delete all the PointerRec entries. 261 for (auto &I : PointerMap) 262 I.second->eraseFromList(); 263 264 PointerMap.clear(); 265 266 // The alias sets should all be clear now. 267 AliasSets.clear(); 268 } 269 270 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may 271 /// alias the pointer. Return the unified set, or nullptr if no set that aliases 272 /// the pointer was found. MustAliasAll is updated to true/false if the pointer 273 /// is found to MustAlias all the sets it merged. 274 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr, 275 LocationSize Size, 276 const AAMDNodes &AAInfo, 277 bool &MustAliasAll) { 278 AliasSet *FoundSet = nullptr; 279 MustAliasAll = true; 280 for (AliasSet &AS : llvm::make_early_inc_range(*this)) { 281 if (AS.Forward) 282 continue; 283 284 AliasResult AR = AS.aliasesPointer(Ptr, Size, AAInfo, AA); 285 if (AR == AliasResult::NoAlias) 286 continue; 287 288 if (AR != AliasResult::MustAlias) 289 MustAliasAll = false; 290 291 if (!FoundSet) { 292 // If this is the first alias set ptr can go into, remember it. 293 FoundSet = &AS; 294 } else { 295 // Otherwise, we must merge the sets. 296 FoundSet->mergeSetIn(AS, *this, AA); 297 } 298 } 299 300 return FoundSet; 301 } 302 303 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) { 304 AliasSet *FoundSet = nullptr; 305 for (AliasSet &AS : llvm::make_early_inc_range(*this)) { 306 if (AS.Forward || !isModOrRefSet(AS.aliasesUnknownInst(Inst, AA))) 307 continue; 308 if (!FoundSet) { 309 // If this is the first alias set ptr can go into, remember it. 310 FoundSet = &AS; 311 } else { 312 // Otherwise, we must merge the sets. 313 FoundSet->mergeSetIn(AS, *this, AA); 314 } 315 } 316 return FoundSet; 317 } 318 319 AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) { 320 321 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr); 322 const LocationSize Size = MemLoc.Size; 323 const AAMDNodes &AAInfo = MemLoc.AATags; 324 325 AliasSet::PointerRec &Entry = getEntryFor(Pointer); 326 327 if (AliasAnyAS) { 328 // At this point, the AST is saturated, so we only have one active alias 329 // set. That means we already know which alias set we want to return, and 330 // just need to add the pointer to that set to keep the data structure 331 // consistent. 332 // This, of course, means that we will never need a merge here. 333 if (Entry.hasAliasSet()) { 334 Entry.updateSizeAndAAInfo(Size, AAInfo); 335 assert(Entry.getAliasSet(*this) == AliasAnyAS && 336 "Entry in saturated AST must belong to only alias set"); 337 } else { 338 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo); 339 } 340 return *AliasAnyAS; 341 } 342 343 bool MustAliasAll = false; 344 // Check to see if the pointer is already known. 345 if (Entry.hasAliasSet()) { 346 // If the size changed, we may need to merge several alias sets. 347 // Note that we can *not* return the result of mergeAliasSetsForPointer 348 // due to a quirk of alias analysis behavior. Since alias(undef, undef) 349 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the 350 // the right set for undef, even if it exists. 351 if (Entry.updateSizeAndAAInfo(Size, AAInfo)) 352 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll); 353 // Return the set! 354 return *Entry.getAliasSet(*this)->getForwardedTarget(*this); 355 } 356 357 if (AliasSet *AS = 358 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) { 359 // Add it to the alias set it aliases. 360 AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll); 361 return *AS; 362 } 363 364 // Otherwise create a new alias set to hold the loaded pointer. 365 AliasSets.push_back(new AliasSet()); 366 AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true); 367 return AliasSets.back(); 368 } 369 370 void AliasSetTracker::add(Value *Ptr, LocationSize Size, 371 const AAMDNodes &AAInfo) { 372 addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess); 373 } 374 375 void AliasSetTracker::add(LoadInst *LI) { 376 if (isStrongerThanMonotonic(LI->getOrdering())) 377 return addUnknown(LI); 378 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess); 379 } 380 381 void AliasSetTracker::add(StoreInst *SI) { 382 if (isStrongerThanMonotonic(SI->getOrdering())) 383 return addUnknown(SI); 384 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess); 385 } 386 387 void AliasSetTracker::add(VAArgInst *VAAI) { 388 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess); 389 } 390 391 void AliasSetTracker::add(AnyMemSetInst *MSI) { 392 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess); 393 } 394 395 void AliasSetTracker::add(AnyMemTransferInst *MTI) { 396 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess); 397 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess); 398 } 399 400 void AliasSetTracker::addUnknown(Instruction *Inst) { 401 if (isa<DbgInfoIntrinsic>(Inst)) 402 return; // Ignore DbgInfo Intrinsics. 403 404 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) { 405 // These intrinsics will show up as affecting memory, but they are just 406 // markers. 407 switch (II->getIntrinsicID()) { 408 default: 409 break; 410 // FIXME: Add lifetime/invariant intrinsics (See: PR30807). 411 case Intrinsic::assume: 412 case Intrinsic::experimental_noalias_scope_decl: 413 case Intrinsic::sideeffect: 414 case Intrinsic::pseudoprobe: 415 return; 416 } 417 } 418 if (!Inst->mayReadOrWriteMemory()) 419 return; // doesn't alias anything 420 421 if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) { 422 AS->addUnknownInst(Inst, AA); 423 return; 424 } 425 AliasSets.push_back(new AliasSet()); 426 AliasSets.back().addUnknownInst(Inst, AA); 427 } 428 429 void AliasSetTracker::add(Instruction *I) { 430 // Dispatch to one of the other add methods. 431 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 432 return add(LI); 433 if (StoreInst *SI = dyn_cast<StoreInst>(I)) 434 return add(SI); 435 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I)) 436 return add(VAAI); 437 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I)) 438 return add(MSI); 439 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I)) 440 return add(MTI); 441 442 // Handle all calls with known mod/ref sets genericall 443 if (auto *Call = dyn_cast<CallBase>(I)) 444 if (Call->onlyAccessesArgMemory()) { 445 auto getAccessFromModRef = [](ModRefInfo MRI) { 446 if (isRefSet(MRI) && isModSet(MRI)) 447 return AliasSet::ModRefAccess; 448 else if (isModSet(MRI)) 449 return AliasSet::ModAccess; 450 else if (isRefSet(MRI)) 451 return AliasSet::RefAccess; 452 else 453 return AliasSet::NoAccess; 454 }; 455 456 ModRefInfo CallMask = AA.getMemoryEffects(Call).getModRef(); 457 458 // Some intrinsics are marked as modifying memory for control flow 459 // modelling purposes, but don't actually modify any specific memory 460 // location. 461 using namespace PatternMatch; 462 if (Call->use_empty() && 463 match(Call, m_Intrinsic<Intrinsic::invariant_start>())) 464 CallMask &= ModRefInfo::Ref; 465 466 for (auto IdxArgPair : enumerate(Call->args())) { 467 int ArgIdx = IdxArgPair.index(); 468 const Value *Arg = IdxArgPair.value(); 469 if (!Arg->getType()->isPointerTy()) 470 continue; 471 MemoryLocation ArgLoc = 472 MemoryLocation::getForArgument(Call, ArgIdx, nullptr); 473 ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx); 474 ArgMask &= CallMask; 475 if (!isNoModRef(ArgMask)) 476 addPointer(ArgLoc, getAccessFromModRef(ArgMask)); 477 } 478 return; 479 } 480 481 return addUnknown(I); 482 } 483 484 void AliasSetTracker::add(BasicBlock &BB) { 485 for (auto &I : BB) 486 add(&I); 487 } 488 489 void AliasSetTracker::add(const AliasSetTracker &AST) { 490 assert(&AA == &AST.AA && 491 "Merging AliasSetTracker objects with different Alias Analyses!"); 492 493 // Loop over all of the alias sets in AST, adding the pointers contained 494 // therein into the current alias sets. This can cause alias sets to be 495 // merged together in the current AST. 496 for (const AliasSet &AS : AST) { 497 if (AS.Forward) 498 continue; // Ignore forwarding alias sets 499 500 // If there are any call sites in the alias set, add them to this AST. 501 for (Instruction *Inst : AS.UnknownInsts) 502 add(Inst); 503 504 // Loop over all of the pointers in this alias set. 505 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) 506 addPointer( 507 MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()), 508 (AliasSet::AccessLattice)AS.Access); 509 } 510 } 511 512 AliasSet &AliasSetTracker::mergeAllAliasSets() { 513 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) && 514 "Full merge should happen once, when the saturation threshold is " 515 "reached"); 516 517 // Collect all alias sets, so that we can drop references with impunity 518 // without worrying about iterator invalidation. 519 std::vector<AliasSet *> ASVector; 520 ASVector.reserve(SaturationThreshold); 521 for (AliasSet &AS : *this) 522 ASVector.push_back(&AS); 523 524 // Copy all instructions and pointers into a new set, and forward all other 525 // sets to it. 526 AliasSets.push_back(new AliasSet()); 527 AliasAnyAS = &AliasSets.back(); 528 AliasAnyAS->Alias = AliasSet::SetMayAlias; 529 AliasAnyAS->Access = AliasSet::ModRefAccess; 530 AliasAnyAS->AliasAny = true; 531 532 for (auto *Cur : ASVector) { 533 // If Cur was already forwarding, just forward to the new AS instead. 534 AliasSet *FwdTo = Cur->Forward; 535 if (FwdTo) { 536 Cur->Forward = AliasAnyAS; 537 AliasAnyAS->addRef(); 538 FwdTo->dropRef(*this); 539 continue; 540 } 541 542 // Otherwise, perform the actual merge. 543 AliasAnyAS->mergeSetIn(*Cur, *this, AA); 544 } 545 546 return *AliasAnyAS; 547 } 548 549 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc, 550 AliasSet::AccessLattice E) { 551 AliasSet &AS = getAliasSetFor(Loc); 552 AS.Access |= E; 553 554 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) { 555 // The AST is now saturated. From here on, we conservatively consider all 556 // pointers to alias each-other. 557 return mergeAllAliasSets(); 558 } 559 560 return AS; 561 } 562 563 //===----------------------------------------------------------------------===// 564 // AliasSet/AliasSetTracker Printing Support 565 //===----------------------------------------------------------------------===// 566 567 void AliasSet::print(raw_ostream &OS) const { 568 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] "; 569 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, "; 570 switch (Access) { 571 case NoAccess: OS << "No access "; break; 572 case RefAccess: OS << "Ref "; break; 573 case ModAccess: OS << "Mod "; break; 574 case ModRefAccess: OS << "Mod/Ref "; break; 575 default: llvm_unreachable("Bad value for Access!"); 576 } 577 if (Forward) 578 OS << " forwarding to " << (void*)Forward; 579 580 if (!empty()) { 581 OS << "Pointers: "; 582 for (iterator I = begin(), E = end(); I != E; ++I) { 583 if (I != begin()) OS << ", "; 584 I.getPointer()->printAsOperand(OS << "("); 585 if (I.getSize() == LocationSize::afterPointer()) 586 OS << ", unknown after)"; 587 else if (I.getSize() == LocationSize::beforeOrAfterPointer()) 588 OS << ", unknown before-or-after)"; 589 else 590 OS << ", " << I.getSize() << ")"; 591 } 592 } 593 if (!UnknownInsts.empty()) { 594 ListSeparator LS; 595 OS << "\n " << UnknownInsts.size() << " Unknown instructions: "; 596 for (Instruction *I : UnknownInsts) { 597 OS << LS; 598 if (I->hasName()) 599 I->printAsOperand(OS); 600 else 601 I->print(OS); 602 } 603 } 604 OS << "\n"; 605 } 606 607 void AliasSetTracker::print(raw_ostream &OS) const { 608 OS << "Alias Set Tracker: " << AliasSets.size(); 609 if (AliasAnyAS) 610 OS << " (Saturated)"; 611 OS << " alias sets for " << PointerMap.size() << " pointer values.\n"; 612 for (const AliasSet &AS : *this) 613 AS.print(OS); 614 OS << "\n"; 615 } 616 617 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 618 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); } 619 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); } 620 #endif 621 622 //===----------------------------------------------------------------------===// 623 // AliasSetPrinter Pass 624 //===----------------------------------------------------------------------===// 625 626 AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {} 627 628 PreservedAnalyses AliasSetsPrinterPass::run(Function &F, 629 FunctionAnalysisManager &AM) { 630 auto &AA = AM.getResult<AAManager>(F); 631 BatchAAResults BatchAA(AA); 632 AliasSetTracker Tracker(BatchAA); 633 OS << "Alias sets for function '" << F.getName() << "':\n"; 634 for (Instruction &I : instructions(F)) 635 Tracker.add(&I); 636 Tracker.print(OS); 637 return PreservedAnalyses::all(); 638 } 639