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