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