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