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