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 bool *New) { 294 AliasSet::PointerRec &Entry = getEntryFor(Pointer); 295 296 if (AliasAnyAS) { 297 // At this point, the AST is saturated, so we only have one active alias 298 // set. That means we already know which alias set we want to return, and 299 // just need to add the pointer to that set to keep the data structure 300 // consistent. 301 // This, of course, means that we will never need a merge here. 302 if (Entry.hasAliasSet()) { 303 Entry.updateSizeAndAAInfo(Size, AAInfo); 304 assert(Entry.getAliasSet(*this) == AliasAnyAS && 305 "Entry in saturated AST must belong to only alias set"); 306 } else { 307 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo); 308 } 309 return *AliasAnyAS; 310 } 311 312 // Check to see if the pointer is already known. 313 if (Entry.hasAliasSet()) { 314 // If the size changed, we may need to merge several alias sets. 315 // Note that we can *not* return the result of mergeAliasSetsForPointer 316 // due to a quirk of alias analysis behavior. Since alias(undef, undef) 317 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the 318 // the right set for undef, even if it exists. 319 if (Entry.updateSizeAndAAInfo(Size, AAInfo)) 320 mergeAliasSetsForPointer(Pointer, Size, AAInfo); 321 // Return the set! 322 return *Entry.getAliasSet(*this)->getForwardedTarget(*this); 323 } 324 325 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) { 326 // Add it to the alias set it aliases. 327 AS->addPointer(*this, Entry, Size, AAInfo); 328 return *AS; 329 } 330 331 if (New) *New = true; 332 // Otherwise create a new alias set to hold the loaded pointer. 333 AliasSets.push_back(new AliasSet()); 334 AliasSets.back().addPointer(*this, Entry, Size, AAInfo); 335 return AliasSets.back(); 336 } 337 338 bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo) { 339 bool NewPtr; 340 addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess, NewPtr); 341 return NewPtr; 342 } 343 344 345 bool AliasSetTracker::add(LoadInst *LI) { 346 if (isStrongerThanMonotonic(LI->getOrdering())) return addUnknown(LI); 347 348 AAMDNodes AAInfo; 349 LI->getAAMetadata(AAInfo); 350 351 AliasSet::AccessLattice Access = AliasSet::RefAccess; 352 bool NewPtr; 353 const DataLayout &DL = LI->getModule()->getDataLayout(); 354 AliasSet &AS = addPointer(LI->getOperand(0), 355 DL.getTypeStoreSize(LI->getType()), 356 AAInfo, Access, NewPtr); 357 if (LI->isVolatile()) AS.setVolatile(); 358 return NewPtr; 359 } 360 361 bool AliasSetTracker::add(StoreInst *SI) { 362 if (isStrongerThanMonotonic(SI->getOrdering())) return addUnknown(SI); 363 364 AAMDNodes AAInfo; 365 SI->getAAMetadata(AAInfo); 366 367 AliasSet::AccessLattice Access = AliasSet::ModAccess; 368 bool NewPtr; 369 const DataLayout &DL = SI->getModule()->getDataLayout(); 370 Value *Val = SI->getOperand(0); 371 AliasSet &AS = addPointer(SI->getOperand(1), 372 DL.getTypeStoreSize(Val->getType()), 373 AAInfo, Access, NewPtr); 374 if (SI->isVolatile()) AS.setVolatile(); 375 return NewPtr; 376 } 377 378 bool AliasSetTracker::add(VAArgInst *VAAI) { 379 AAMDNodes AAInfo; 380 VAAI->getAAMetadata(AAInfo); 381 382 bool NewPtr; 383 addPointer(VAAI->getOperand(0), MemoryLocation::UnknownSize, AAInfo, 384 AliasSet::ModRefAccess, NewPtr); 385 return NewPtr; 386 } 387 388 bool AliasSetTracker::add(MemSetInst *MSI) { 389 AAMDNodes AAInfo; 390 MSI->getAAMetadata(AAInfo); 391 392 bool NewPtr; 393 uint64_t Len; 394 395 if (ConstantInt *C = dyn_cast<ConstantInt>(MSI->getLength())) 396 Len = C->getZExtValue(); 397 else 398 Len = MemoryLocation::UnknownSize; 399 400 AliasSet &AS = 401 addPointer(MSI->getRawDest(), Len, AAInfo, AliasSet::ModAccess, NewPtr); 402 if (MSI->isVolatile()) 403 AS.setVolatile(); 404 return NewPtr; 405 } 406 407 bool AliasSetTracker::addUnknown(Instruction *Inst) { 408 if (isa<DbgInfoIntrinsic>(Inst)) 409 return true; // Ignore DbgInfo Intrinsics. 410 if (!Inst->mayReadOrWriteMemory()) 411 return true; // doesn't alias anything 412 413 AliasSet *AS = findAliasSetForUnknownInst(Inst); 414 if (AS) { 415 AS->addUnknownInst(Inst, AA); 416 return false; 417 } 418 AliasSets.push_back(new AliasSet()); 419 AS = &AliasSets.back(); 420 AS->addUnknownInst(Inst, AA); 421 return true; 422 } 423 424 bool AliasSetTracker::add(Instruction *I) { 425 // Dispatch to one of the other add methods. 426 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 427 return add(LI); 428 if (StoreInst *SI = dyn_cast<StoreInst>(I)) 429 return add(SI); 430 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I)) 431 return add(VAAI); 432 if (MemSetInst *MSI = dyn_cast<MemSetInst>(I)) 433 return add(MSI); 434 return addUnknown(I); 435 // FIXME: add support of memcpy and memmove. 436 } 437 438 void AliasSetTracker::add(BasicBlock &BB) { 439 for (auto &I : BB) 440 add(&I); 441 } 442 443 void AliasSetTracker::add(const AliasSetTracker &AST) { 444 assert(&AA == &AST.AA && 445 "Merging AliasSetTracker objects with different Alias Analyses!"); 446 447 // Loop over all of the alias sets in AST, adding the pointers contained 448 // therein into the current alias sets. This can cause alias sets to be 449 // merged together in the current AST. 450 for (const AliasSet &AS : AST) { 451 if (AS.Forward) 452 continue; // Ignore forwarding alias sets 453 454 // If there are any call sites in the alias set, add them to this AST. 455 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i) 456 add(AS.UnknownInsts[i]); 457 458 // Loop over all of the pointers in this alias set. 459 bool X; 460 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) { 461 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(), 462 ASI.getAAInfo(), 463 (AliasSet::AccessLattice)AS.Access, X); 464 if (AS.isVolatile()) NewAS.setVolatile(); 465 } 466 } 467 } 468 469 // deleteValue method - This method is used to remove a pointer value from the 470 // AliasSetTracker entirely. It should be used when an instruction is deleted 471 // from the program to update the AST. If you don't use this, you would have 472 // dangling pointers to deleted instructions. 473 // 474 void AliasSetTracker::deleteValue(Value *PtrVal) { 475 // If this is a call instruction, remove the callsite from the appropriate 476 // AliasSet (if present). 477 if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) { 478 if (Inst->mayReadOrWriteMemory()) { 479 // Scan all the alias sets to see if this call site is contained. 480 for (iterator I = begin(), E = end(); I != E;) { 481 iterator Cur = I++; 482 if (!Cur->Forward) 483 Cur->removeUnknownInst(*this, Inst); 484 } 485 } 486 } 487 488 // First, look up the PointerRec for this pointer. 489 PointerMapType::iterator I = PointerMap.find_as(PtrVal); 490 if (I == PointerMap.end()) return; // Noop 491 492 // If we found one, remove the pointer from the alias set it is in. 493 AliasSet::PointerRec *PtrValEnt = I->second; 494 AliasSet *AS = PtrValEnt->getAliasSet(*this); 495 496 // Unlink and delete from the list of values. 497 PtrValEnt->eraseFromList(); 498 499 if (AS->Alias == AliasSet::SetMayAlias) { 500 AS->SetSize--; 501 TotalMayAliasSetSize--; 502 } 503 504 // Stop using the alias set. 505 AS->dropRef(*this); 506 507 PointerMap.erase(I); 508 } 509 510 // copyValue - This method should be used whenever a preexisting value in the 511 // program is copied or cloned, introducing a new value. Note that it is ok for 512 // clients that use this method to introduce the same value multiple times: if 513 // the tracker already knows about a value, it will ignore the request. 514 // 515 void AliasSetTracker::copyValue(Value *From, Value *To) { 516 // First, look up the PointerRec for this pointer. 517 PointerMapType::iterator I = PointerMap.find_as(From); 518 if (I == PointerMap.end()) 519 return; // Noop 520 assert(I->second->hasAliasSet() && "Dead entry?"); 521 522 AliasSet::PointerRec &Entry = getEntryFor(To); 523 if (Entry.hasAliasSet()) return; // Already in the tracker! 524 525 // getEntryFor above may invalidate iterator \c I, so reinitialize it. 526 I = PointerMap.find_as(From); 527 // Add it to the alias set it aliases... 528 AliasSet *AS = I->second->getAliasSet(*this); 529 AS->addPointer(*this, Entry, I->second->getSize(), 530 I->second->getAAInfo(), 531 true); 532 } 533 534 AliasSet &AliasSetTracker::mergeAllAliasSets() { 535 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) && 536 "Full merge should happen once, when the saturation threshold is " 537 "reached"); 538 539 // Collect all alias sets, so that we can drop references with impunity 540 // without worrying about iterator invalidation. 541 std::vector<AliasSet *> ASVector; 542 ASVector.reserve(SaturationThreshold); 543 for (iterator I = begin(), E = end(); I != E; I++) 544 ASVector.push_back(&*I); 545 546 // Copy all instructions and pointers into a new set, and forward all other 547 // sets to it. 548 AliasSets.push_back(new AliasSet()); 549 AliasAnyAS = &AliasSets.back(); 550 AliasAnyAS->Alias = AliasSet::SetMayAlias; 551 AliasAnyAS->Access = AliasSet::ModRefAccess; 552 AliasAnyAS->AliasAny = true; 553 554 for (auto Cur : ASVector) { 555 556 // If Cur was already forwarding, just forward to the new AS instead. 557 AliasSet *FwdTo = Cur->Forward; 558 if (FwdTo) { 559 Cur->Forward = AliasAnyAS; 560 AliasAnyAS->addRef(); 561 FwdTo->dropRef(*this); 562 continue; 563 } 564 565 // Otherwise, perform the actual merge. 566 AliasAnyAS->mergeSetIn(*Cur, *this); 567 } 568 569 return *AliasAnyAS; 570 } 571 572 AliasSet &AliasSetTracker::addPointer(Value *P, uint64_t Size, 573 const AAMDNodes &AAInfo, 574 AliasSet::AccessLattice E, bool &NewSet) { 575 576 NewSet = false; 577 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo, &NewSet); 578 AS.Access |= E; 579 580 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) { 581 // The AST is now saturated. From here on, we conservatively consider all 582 // pointers to alias each-other. 583 return mergeAllAliasSets(); 584 } 585 586 return AS; 587 } 588 589 //===----------------------------------------------------------------------===// 590 // AliasSet/AliasSetTracker Printing Support 591 //===----------------------------------------------------------------------===// 592 593 void AliasSet::print(raw_ostream &OS) const { 594 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] "; 595 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, "; 596 switch (Access) { 597 case NoAccess: OS << "No access "; break; 598 case RefAccess: OS << "Ref "; break; 599 case ModAccess: OS << "Mod "; break; 600 case ModRefAccess: OS << "Mod/Ref "; break; 601 default: llvm_unreachable("Bad value for Access!"); 602 } 603 if (isVolatile()) OS << "[volatile] "; 604 if (Forward) 605 OS << " forwarding to " << (void*)Forward; 606 607 608 if (!empty()) { 609 OS << "Pointers: "; 610 for (iterator I = begin(), E = end(); I != E; ++I) { 611 if (I != begin()) OS << ", "; 612 I.getPointer()->printAsOperand(OS << "("); 613 OS << ", " << I.getSize() << ")"; 614 } 615 } 616 if (!UnknownInsts.empty()) { 617 OS << "\n " << UnknownInsts.size() << " Unknown instructions: "; 618 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) { 619 if (i) OS << ", "; 620 UnknownInsts[i]->printAsOperand(OS); 621 } 622 } 623 OS << "\n"; 624 } 625 626 void AliasSetTracker::print(raw_ostream &OS) const { 627 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for " 628 << PointerMap.size() << " pointer values.\n"; 629 for (const AliasSet &AS : *this) 630 AS.print(OS); 631 OS << "\n"; 632 } 633 634 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 635 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); } 636 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); } 637 #endif 638 639 //===----------------------------------------------------------------------===// 640 // ASTCallbackVH Class Implementation 641 //===----------------------------------------------------------------------===// 642 643 void AliasSetTracker::ASTCallbackVH::deleted() { 644 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!"); 645 AST->deleteValue(getValPtr()); 646 // this now dangles! 647 } 648 649 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) { 650 AST->copyValue(getValPtr(), V); 651 } 652 653 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast) 654 : CallbackVH(V), AST(ast) {} 655 656 AliasSetTracker::ASTCallbackVH & 657 AliasSetTracker::ASTCallbackVH::operator=(Value *V) { 658 return *this = ASTCallbackVH(V, AST); 659 } 660 661 //===----------------------------------------------------------------------===// 662 // AliasSetPrinter Pass 663 //===----------------------------------------------------------------------===// 664 665 namespace { 666 class AliasSetPrinter : public FunctionPass { 667 AliasSetTracker *Tracker; 668 public: 669 static char ID; // Pass identification, replacement for typeid 670 AliasSetPrinter() : FunctionPass(ID) { 671 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry()); 672 } 673 674 void getAnalysisUsage(AnalysisUsage &AU) const override { 675 AU.setPreservesAll(); 676 AU.addRequired<AAResultsWrapperPass>(); 677 } 678 679 bool runOnFunction(Function &F) override { 680 auto &AAWP = getAnalysis<AAResultsWrapperPass>(); 681 Tracker = new AliasSetTracker(AAWP.getAAResults()); 682 errs() << "Alias sets for function '" << F.getName() << "':\n"; 683 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 684 Tracker->add(&*I); 685 Tracker->print(errs()); 686 delete Tracker; 687 return false; 688 } 689 }; 690 } 691 692 char AliasSetPrinter::ID = 0; 693 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets", 694 "Alias Set Printer", false, true) 695 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 696 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets", 697 "Alias Set Printer", false, true) 698