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