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::add(MemTransferInst *MTI) { 393 AAMDNodes AAInfo; 394 MTI->getAAMetadata(AAInfo); 395 396 uint64_t Len; 397 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) 398 Len = C->getZExtValue(); 399 else 400 Len = MemoryLocation::UnknownSize; 401 402 AliasSet &ASSrc = 403 addPointer(MTI->getRawSource(), Len, AAInfo, AliasSet::RefAccess); 404 if (MTI->isVolatile()) 405 ASSrc.setVolatile(); 406 407 AliasSet &ASDst = 408 addPointer(MTI->getRawDest(), Len, AAInfo, AliasSet::ModAccess); 409 if (MTI->isVolatile()) 410 ASDst.setVolatile(); 411 } 412 413 void AliasSetTracker::addUnknown(Instruction *Inst) { 414 if (isa<DbgInfoIntrinsic>(Inst)) 415 return; // Ignore DbgInfo Intrinsics. 416 if (!Inst->mayReadOrWriteMemory()) 417 return; // doesn't alias anything 418 419 AliasSet *AS = findAliasSetForUnknownInst(Inst); 420 if (AS) { 421 AS->addUnknownInst(Inst, AA); 422 return; 423 } 424 AliasSets.push_back(new AliasSet()); 425 AS = &AliasSets.back(); 426 AS->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 (MemSetInst *MSI = dyn_cast<MemSetInst>(I)) 438 return add(MSI); 439 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) 440 return add(MTI); 441 return addUnknown(I); 442 } 443 444 void AliasSetTracker::add(BasicBlock &BB) { 445 for (auto &I : BB) 446 add(&I); 447 } 448 449 void AliasSetTracker::add(const AliasSetTracker &AST) { 450 assert(&AA == &AST.AA && 451 "Merging AliasSetTracker objects with different Alias Analyses!"); 452 453 // Loop over all of the alias sets in AST, adding the pointers contained 454 // therein into the current alias sets. This can cause alias sets to be 455 // merged together in the current AST. 456 for (const AliasSet &AS : AST) { 457 if (AS.Forward) 458 continue; // Ignore forwarding alias sets 459 460 // If there are any call sites in the alias set, add them to this AST. 461 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i) 462 add(AS.UnknownInsts[i]); 463 464 // Loop over all of the pointers in this alias set. 465 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) { 466 AliasSet &NewAS = 467 addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(), 468 (AliasSet::AccessLattice)AS.Access); 469 if (AS.isVolatile()) NewAS.setVolatile(); 470 } 471 } 472 } 473 474 // deleteValue method - This method is used to remove a pointer value from the 475 // AliasSetTracker entirely. It should be used when an instruction is deleted 476 // from the program to update the AST. If you don't use this, you would have 477 // dangling pointers to deleted instructions. 478 // 479 void AliasSetTracker::deleteValue(Value *PtrVal) { 480 // If this is a call instruction, remove the callsite from the appropriate 481 // AliasSet (if present). 482 if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) { 483 if (Inst->mayReadOrWriteMemory()) { 484 // Scan all the alias sets to see if this call site is contained. 485 for (iterator I = begin(), E = end(); I != E;) { 486 iterator Cur = I++; 487 if (!Cur->Forward) 488 Cur->removeUnknownInst(*this, Inst); 489 } 490 } 491 } 492 493 // First, look up the PointerRec for this pointer. 494 PointerMapType::iterator I = PointerMap.find_as(PtrVal); 495 if (I == PointerMap.end()) return; // Noop 496 497 // If we found one, remove the pointer from the alias set it is in. 498 AliasSet::PointerRec *PtrValEnt = I->second; 499 AliasSet *AS = PtrValEnt->getAliasSet(*this); 500 501 // Unlink and delete from the list of values. 502 PtrValEnt->eraseFromList(); 503 504 if (AS->Alias == AliasSet::SetMayAlias) { 505 AS->SetSize--; 506 TotalMayAliasSetSize--; 507 } 508 509 // Stop using the alias set. 510 AS->dropRef(*this); 511 512 PointerMap.erase(I); 513 } 514 515 // copyValue - This method should be used whenever a preexisting value in the 516 // program is copied or cloned, introducing a new value. Note that it is ok for 517 // clients that use this method to introduce the same value multiple times: if 518 // the tracker already knows about a value, it will ignore the request. 519 // 520 void AliasSetTracker::copyValue(Value *From, Value *To) { 521 // First, look up the PointerRec for this pointer. 522 PointerMapType::iterator I = PointerMap.find_as(From); 523 if (I == PointerMap.end()) 524 return; // Noop 525 assert(I->second->hasAliasSet() && "Dead entry?"); 526 527 AliasSet::PointerRec &Entry = getEntryFor(To); 528 if (Entry.hasAliasSet()) return; // Already in the tracker! 529 530 // getEntryFor above may invalidate iterator \c I, so reinitialize it. 531 I = PointerMap.find_as(From); 532 // Add it to the alias set it aliases... 533 AliasSet *AS = I->second->getAliasSet(*this); 534 AS->addPointer(*this, Entry, I->second->getSize(), 535 I->second->getAAInfo(), 536 true); 537 } 538 539 AliasSet &AliasSetTracker::mergeAllAliasSets() { 540 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) && 541 "Full merge should happen once, when the saturation threshold is " 542 "reached"); 543 544 // Collect all alias sets, so that we can drop references with impunity 545 // without worrying about iterator invalidation. 546 std::vector<AliasSet *> ASVector; 547 ASVector.reserve(SaturationThreshold); 548 for (iterator I = begin(), E = end(); I != E; I++) 549 ASVector.push_back(&*I); 550 551 // Copy all instructions and pointers into a new set, and forward all other 552 // sets to it. 553 AliasSets.push_back(new AliasSet()); 554 AliasAnyAS = &AliasSets.back(); 555 AliasAnyAS->Alias = AliasSet::SetMayAlias; 556 AliasAnyAS->Access = AliasSet::ModRefAccess; 557 AliasAnyAS->AliasAny = true; 558 559 for (auto Cur : ASVector) { 560 561 // If Cur was already forwarding, just forward to the new AS instead. 562 AliasSet *FwdTo = Cur->Forward; 563 if (FwdTo) { 564 Cur->Forward = AliasAnyAS; 565 AliasAnyAS->addRef(); 566 FwdTo->dropRef(*this); 567 continue; 568 } 569 570 // Otherwise, perform the actual merge. 571 AliasAnyAS->mergeSetIn(*Cur, *this); 572 } 573 574 return *AliasAnyAS; 575 } 576 577 AliasSet &AliasSetTracker::addPointer(Value *P, uint64_t Size, 578 const AAMDNodes &AAInfo, 579 AliasSet::AccessLattice E) { 580 581 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo); 582 AS.Access |= E; 583 584 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) { 585 // The AST is now saturated. From here on, we conservatively consider all 586 // pointers to alias each-other. 587 return mergeAllAliasSets(); 588 } 589 590 return AS; 591 } 592 593 //===----------------------------------------------------------------------===// 594 // AliasSet/AliasSetTracker Printing Support 595 //===----------------------------------------------------------------------===// 596 597 void AliasSet::print(raw_ostream &OS) const { 598 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] "; 599 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, "; 600 switch (Access) { 601 case NoAccess: OS << "No access "; break; 602 case RefAccess: OS << "Ref "; break; 603 case ModAccess: OS << "Mod "; break; 604 case ModRefAccess: OS << "Mod/Ref "; break; 605 default: llvm_unreachable("Bad value for Access!"); 606 } 607 if (isVolatile()) OS << "[volatile] "; 608 if (Forward) 609 OS << " forwarding to " << (void*)Forward; 610 611 612 if (!empty()) { 613 OS << "Pointers: "; 614 for (iterator I = begin(), E = end(); I != E; ++I) { 615 if (I != begin()) OS << ", "; 616 I.getPointer()->printAsOperand(OS << "("); 617 OS << ", " << I.getSize() << ")"; 618 } 619 } 620 if (!UnknownInsts.empty()) { 621 OS << "\n " << UnknownInsts.size() << " Unknown instructions: "; 622 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) { 623 if (i) OS << ", "; 624 UnknownInsts[i]->printAsOperand(OS); 625 } 626 } 627 OS << "\n"; 628 } 629 630 void AliasSetTracker::print(raw_ostream &OS) const { 631 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for " 632 << PointerMap.size() << " pointer values.\n"; 633 for (const AliasSet &AS : *this) 634 AS.print(OS); 635 OS << "\n"; 636 } 637 638 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 639 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); } 640 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); } 641 #endif 642 643 //===----------------------------------------------------------------------===// 644 // ASTCallbackVH Class Implementation 645 //===----------------------------------------------------------------------===// 646 647 void AliasSetTracker::ASTCallbackVH::deleted() { 648 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!"); 649 AST->deleteValue(getValPtr()); 650 // this now dangles! 651 } 652 653 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) { 654 AST->copyValue(getValPtr(), V); 655 } 656 657 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast) 658 : CallbackVH(V), AST(ast) {} 659 660 AliasSetTracker::ASTCallbackVH & 661 AliasSetTracker::ASTCallbackVH::operator=(Value *V) { 662 return *this = ASTCallbackVH(V, AST); 663 } 664 665 //===----------------------------------------------------------------------===// 666 // AliasSetPrinter Pass 667 //===----------------------------------------------------------------------===// 668 669 namespace { 670 class AliasSetPrinter : public FunctionPass { 671 AliasSetTracker *Tracker; 672 public: 673 static char ID; // Pass identification, replacement for typeid 674 AliasSetPrinter() : FunctionPass(ID) { 675 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry()); 676 } 677 678 void getAnalysisUsage(AnalysisUsage &AU) const override { 679 AU.setPreservesAll(); 680 AU.addRequired<AAResultsWrapperPass>(); 681 } 682 683 bool runOnFunction(Function &F) override { 684 auto &AAWP = getAnalysis<AAResultsWrapperPass>(); 685 Tracker = new AliasSetTracker(AAWP.getAAResults()); 686 errs() << "Alias sets for function '" << F.getName() << "':\n"; 687 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 688 Tracker->add(&*I); 689 Tracker->print(errs()); 690 delete Tracker; 691 return false; 692 } 693 }; 694 } 695 696 char AliasSetPrinter::ID = 0; 697 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets", 698 "Alias Set Printer", false, true) 699 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 700 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets", 701 "Alias Set Printer", false, true) 702