1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source 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/Instructions.h" 17 #include "llvm/Pass.h" 18 #include "llvm/Target/TargetData.h" 19 #include "llvm/Assembly/Writer.h" 20 #include "llvm/Support/InstIterator.h" 21 #include <iostream> 22 using namespace llvm; 23 24 /// mergeSetIn - Merge the specified alias set into this alias set... 25 /// 26 void AliasSet::mergeSetIn(AliasSet &AS) { 27 assert(!AS.Forward && "Alias set is already forwarding!"); 28 assert(!Forward && "This set is a forwarding set!!"); 29 30 // Update the alias and access types of this set... 31 AccessTy |= AS.AccessTy; 32 AliasTy |= AS.AliasTy; 33 34 if (CallSites.empty()) { // Merge call sites... 35 if (!AS.CallSites.empty()) 36 std::swap(CallSites, AS.CallSites); 37 } else if (!AS.CallSites.empty()) { 38 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end()); 39 AS.CallSites.clear(); 40 } 41 42 AS.Forward = this; // Forward across AS now... 43 addRef(); // AS is now pointing to us... 44 45 // Merge the list of constituent pointers... 46 if (AS.PtrList) { 47 *PtrListEnd = AS.PtrList; 48 AS.PtrList->second.setPrevInList(PtrListEnd); 49 PtrListEnd = AS.PtrListEnd; 50 51 AS.PtrList = 0; 52 AS.PtrListEnd = &AS.PtrList; 53 assert(*AS.PtrListEnd == 0 && "End of list is not null?"); 54 } 55 } 56 57 void AliasSetTracker::removeAliasSet(AliasSet *AS) { 58 if (AliasSet *Fwd = AS->Forward) { 59 Fwd->dropRef(*this); 60 AS->Forward = 0; 61 } 62 AliasSets.erase(AS); 63 } 64 65 void AliasSet::removeFromTracker(AliasSetTracker &AST) { 66 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!"); 67 AST.removeAliasSet(this); 68 } 69 70 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry, 71 unsigned Size, bool KnownMustAlias) { 72 assert(!Entry.second.hasAliasSet() && "Entry already in set!"); 73 74 // Check to see if we have to downgrade to _may_ alias. 75 if (isMustAlias() && !KnownMustAlias) 76 if (HashNodePair *P = getSomePointer()) { 77 AliasAnalysis &AA = AST.getAliasAnalysis(); 78 AliasAnalysis::AliasResult Result = 79 AA.alias(P->first, P->second.getSize(), Entry.first, Size); 80 if (Result == AliasAnalysis::MayAlias) 81 AliasTy = MayAlias; 82 else // First entry of must alias must have maximum size! 83 P->second.updateSize(Size); 84 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!"); 85 } 86 87 Entry.second.setAliasSet(this); 88 Entry.second.updateSize(Size); 89 90 // Add it to the end of the list... 91 assert(*PtrListEnd == 0 && "End of list is not null?"); 92 *PtrListEnd = &Entry; 93 PtrListEnd = Entry.second.setPrevInList(PtrListEnd); 94 assert(*PtrListEnd == 0 && "End of list is not null?"); 95 addRef(); // Entry points to alias set... 96 } 97 98 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) { 99 CallSites.push_back(CS); 100 101 if (Function *F = CS.getCalledFunction()) { 102 if (AA.doesNotAccessMemory(F)) 103 return; 104 else if (AA.onlyReadsMemory(F)) { 105 AliasTy = MayAlias; 106 AccessTy |= Refs; 107 return; 108 } 109 } 110 111 // FIXME: This should use mod/ref information to make this not suck so bad 112 AliasTy = MayAlias; 113 AccessTy = ModRef; 114 } 115 116 /// aliasesPointer - Return true if the specified pointer "may" (or must) 117 /// alias one of the members in the set. 118 /// 119 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size, 120 AliasAnalysis &AA) const { 121 if (AliasTy == MustAlias) { 122 assert(CallSites.empty() && "Illegal must alias set!"); 123 124 // If this is a set of MustAliases, only check to see if the pointer aliases 125 // SOME value in the set... 126 HashNodePair *SomePtr = getSomePointer(); 127 assert(SomePtr && "Empty must-alias set??"); 128 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size); 129 } 130 131 // If this is a may-alias set, we have to check all of the pointers in the set 132 // to be sure it doesn't alias the set... 133 for (iterator I = begin(), E = end(); I != E; ++I) 134 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize())) 135 return true; 136 137 // Check the call sites list and invoke list... 138 if (!CallSites.empty()) { 139 if (AA.hasNoModRefInfoForCalls()) 140 return true; 141 142 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) 143 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size) 144 != AliasAnalysis::NoModRef) 145 return true; 146 } 147 148 return false; 149 } 150 151 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const { 152 if (Function *F = CS.getCalledFunction()) 153 if (AA.doesNotAccessMemory(F)) 154 return false; 155 156 if (AA.hasNoModRefInfoForCalls()) 157 return true; 158 159 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) 160 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef || 161 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef) 162 return true; 163 164 for (iterator I = begin(), E = end(); I != E; ++I) 165 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) != 166 AliasAnalysis::NoModRef) 167 return true; 168 169 return false; 170 } 171 172 173 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the 174 /// instruction referring to the pointer into. If there are multiple alias sets 175 /// that may alias the pointer, merge them together and return the unified set. 176 /// 177 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr, 178 unsigned Size) { 179 AliasSet *FoundSet = 0; 180 for (iterator I = begin(), E = end(); I != E; ++I) 181 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) { 182 if (FoundSet == 0) { // If this is the first alias set ptr can go into... 183 FoundSet = I; // Remember it. 184 } else { // Otherwise, we must merge the sets... 185 FoundSet->mergeSetIn(*I); // Merge in contents... 186 } 187 } 188 189 return FoundSet; 190 } 191 192 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) { 193 AliasSet *FoundSet = 0; 194 for (iterator I = begin(), E = end(); I != E; ++I) 195 if (!I->Forward && I->aliasesCallSite(CS, AA)) { 196 if (FoundSet == 0) { // If this is the first alias set ptr can go into... 197 FoundSet = I; // Remember it. 198 } else if (!I->Forward) { // Otherwise, we must merge the sets... 199 FoundSet->mergeSetIn(*I); // Merge in contents... 200 } 201 } 202 203 return FoundSet; 204 } 205 206 207 208 209 /// getAliasSetForPointer - Return the alias set that the specified pointer 210 /// lives in... 211 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size, 212 bool *New) { 213 AliasSet::HashNodePair &Entry = getEntryFor(Pointer); 214 215 // Check to see if the pointer is already known... 216 if (Entry.second.hasAliasSet()) { 217 Entry.second.updateSize(Size); 218 // Return the set! 219 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this); 220 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) { 221 // Add it to the alias set it aliases... 222 AS->addPointer(*this, Entry, Size); 223 return *AS; 224 } else { 225 if (New) *New = true; 226 // Otherwise create a new alias set to hold the loaded pointer... 227 AliasSets.push_back(new AliasSet()); 228 AliasSets.back().addPointer(*this, Entry, Size); 229 return AliasSets.back(); 230 } 231 } 232 233 bool AliasSetTracker::add(Value *Ptr, unsigned Size) { 234 bool NewPtr; 235 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr); 236 return NewPtr; 237 } 238 239 240 bool AliasSetTracker::add(LoadInst *LI) { 241 bool NewPtr; 242 AliasSet &AS = addPointer(LI->getOperand(0), 243 AA.getTargetData().getTypeSize(LI->getType()), 244 AliasSet::Refs, NewPtr); 245 if (LI->isVolatile()) AS.setVolatile(); 246 return NewPtr; 247 } 248 249 bool AliasSetTracker::add(StoreInst *SI) { 250 bool NewPtr; 251 Value *Val = SI->getOperand(0); 252 AliasSet &AS = addPointer(SI->getOperand(1), 253 AA.getTargetData().getTypeSize(Val->getType()), 254 AliasSet::Mods, NewPtr); 255 if (SI->isVolatile()) AS.setVolatile(); 256 return NewPtr; 257 } 258 259 bool AliasSetTracker::add(FreeInst *FI) { 260 bool NewPtr; 261 AliasSet &AS = addPointer(FI->getOperand(0), ~0, 262 AliasSet::Mods, NewPtr); 263 return NewPtr; 264 } 265 266 267 bool AliasSetTracker::add(CallSite CS) { 268 if (Function *F = CS.getCalledFunction()) 269 if (AA.doesNotAccessMemory(F)) 270 return true; // doesn't alias anything 271 272 AliasSet *AS = findAliasSetForCallSite(CS); 273 if (!AS) { 274 AliasSets.push_back(new AliasSet()); 275 AS = &AliasSets.back(); 276 AS->addCallSite(CS, AA); 277 return true; 278 } else { 279 AS->addCallSite(CS, AA); 280 return false; 281 } 282 } 283 284 bool AliasSetTracker::add(Instruction *I) { 285 // Dispatch to one of the other add methods... 286 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 287 return add(LI); 288 else if (StoreInst *SI = dyn_cast<StoreInst>(I)) 289 return add(SI); 290 else if (CallInst *CI = dyn_cast<CallInst>(I)) 291 return add(CI); 292 else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) 293 return add(II); 294 else if (FreeInst *FI = dyn_cast<FreeInst>(I)) 295 return add(FI); 296 return true; 297 } 298 299 void AliasSetTracker::add(BasicBlock &BB) { 300 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) 301 add(I); 302 } 303 304 void AliasSetTracker::add(const AliasSetTracker &AST) { 305 assert(&AA == &AST.AA && 306 "Merging AliasSetTracker objects with different Alias Analyses!"); 307 308 // Loop over all of the alias sets in AST, adding the pointers contained 309 // therein into the current alias sets. This can cause alias sets to be 310 // merged together in the current AST. 311 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) 312 if (!I->Forward) { // Ignore forwarding alias sets 313 AliasSet &AS = const_cast<AliasSet&>(*I); 314 315 // If there are any call sites in the alias set, add them to this AST. 316 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i) 317 add(AS.CallSites[i]); 318 319 // Loop over all of the pointers in this alias set... 320 AliasSet::iterator I = AS.begin(), E = AS.end(); 321 bool X; 322 for (; I != E; ++I) 323 addPointer(I.getPointer(), I.getSize(), 324 (AliasSet::AccessType)AS.AccessTy, X); 325 } 326 } 327 328 /// remove - Remove the specified (potentially non-empty) alias set from the 329 /// tracker. 330 void AliasSetTracker::remove(AliasSet &AS) { 331 bool SetDead; 332 do { 333 AliasSet::iterator I = AS.begin(); 334 Value *Ptr = I.getPointer(); ++I; 335 336 // deleteValue will delete the set automatically when the last pointer 337 // reference is destroyed. "Predict" when this will happen. 338 SetDead = I == AS.end(); 339 deleteValue(Ptr); // Delete all of the pointers from the set 340 } while (!SetDead); 341 } 342 343 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) { 344 AliasSet *AS = findAliasSetForPointer(Ptr, Size); 345 if (!AS) return false; 346 remove(*AS); 347 return true; 348 } 349 350 bool AliasSetTracker::remove(LoadInst *LI) { 351 unsigned Size = AA.getTargetData().getTypeSize(LI->getType()); 352 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size); 353 if (!AS) return false; 354 remove(*AS); 355 return true; 356 } 357 358 bool AliasSetTracker::remove(StoreInst *SI) { 359 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()); 360 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size); 361 if (!AS) return false; 362 remove(*AS); 363 return true; 364 } 365 366 bool AliasSetTracker::remove(FreeInst *FI) { 367 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0); 368 if (!AS) return false; 369 remove(*AS); 370 return true; 371 } 372 373 bool AliasSetTracker::remove(CallSite CS) { 374 if (Function *F = CS.getCalledFunction()) 375 if (AA.doesNotAccessMemory(F)) 376 return false; // doesn't alias anything 377 378 AliasSet *AS = findAliasSetForCallSite(CS); 379 if (!AS) return false; 380 remove(*AS); 381 return true; 382 } 383 384 bool AliasSetTracker::remove(Instruction *I) { 385 // Dispatch to one of the other remove methods... 386 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 387 return remove(LI); 388 else if (StoreInst *SI = dyn_cast<StoreInst>(I)) 389 return remove(SI); 390 else if (CallInst *CI = dyn_cast<CallInst>(I)) 391 return remove(CI); 392 else if (FreeInst *FI = dyn_cast<FreeInst>(I)) 393 return remove(FI); 394 return true; 395 } 396 397 398 // deleteValue method - This method is used to remove a pointer value from the 399 // AliasSetTracker entirely. It should be used when an instruction is deleted 400 // from the program to update the AST. If you don't use this, you would have 401 // dangling pointers to deleted instructions. 402 // 403 void AliasSetTracker::deleteValue(Value *PtrVal) { 404 // Notify the alias analysis implementation that this value is gone. 405 AA.deleteValue(PtrVal); 406 407 // First, look up the PointerRec for this pointer. 408 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal); 409 if (I == PointerMap.end()) return; // Noop 410 411 // If we found one, remove the pointer from the alias set it is in. 412 AliasSet::HashNodePair &PtrValEnt = *I; 413 AliasSet *AS = PtrValEnt.second.getAliasSet(*this); 414 415 // Unlink from the list of values... 416 PtrValEnt.second.removeFromList(); 417 // Stop using the alias set 418 AS->dropRef(*this); 419 PointerMap.erase(I); 420 } 421 422 // copyValue - This method should be used whenever a preexisting value in the 423 // program is copied or cloned, introducing a new value. Note that it is ok for 424 // clients that use this method to introduce the same value multiple times: if 425 // the tracker already knows about a value, it will ignore the request. 426 // 427 void AliasSetTracker::copyValue(Value *From, Value *To) { 428 // Notify the alias analysis implementation that this value is copied. 429 AA.copyValue(From, To); 430 431 // First, look up the PointerRec for this pointer. 432 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From); 433 if (I == PointerMap.end() || !I->second.hasAliasSet()) 434 return; // Noop 435 436 AliasSet::HashNodePair &Entry = getEntryFor(To); 437 if (Entry.second.hasAliasSet()) return; // Already in the tracker! 438 439 // Add it to the alias set it aliases... 440 AliasSet *AS = I->second.getAliasSet(*this); 441 AS->addPointer(*this, Entry, I->second.getSize(), true); 442 } 443 444 445 446 //===----------------------------------------------------------------------===// 447 // AliasSet/AliasSetTracker Printing Support 448 //===----------------------------------------------------------------------===// 449 450 void AliasSet::print(std::ostream &OS) const { 451 OS << " AliasSet[" << (void*)this << "," << RefCount << "] "; 452 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, "; 453 switch (AccessTy) { 454 case NoModRef: OS << "No access "; break; 455 case Refs : OS << "Ref "; break; 456 case Mods : OS << "Mod "; break; 457 case ModRef : OS << "Mod/Ref "; break; 458 default: assert(0 && "Bad value for AccessTy!"); 459 } 460 if (isVolatile()) OS << "[volatile] "; 461 if (Forward) 462 OS << " forwarding to " << (void*)Forward; 463 464 465 if (begin() != end()) { 466 OS << "Pointers: "; 467 for (iterator I = begin(), E = end(); I != E; ++I) { 468 if (I != begin()) OS << ", "; 469 WriteAsOperand(OS << "(", I.getPointer()); 470 OS << ", " << I.getSize() << ")"; 471 } 472 } 473 if (!CallSites.empty()) { 474 OS << "\n " << CallSites.size() << " Call Sites: "; 475 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) { 476 if (i) OS << ", "; 477 WriteAsOperand(OS, CallSites[i].getCalledValue()); 478 } 479 } 480 OS << "\n"; 481 } 482 483 void AliasSetTracker::print(std::ostream &OS) const { 484 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for " 485 << PointerMap.size() << " pointer values.\n"; 486 for (const_iterator I = begin(), E = end(); I != E; ++I) 487 I->print(OS); 488 OS << "\n"; 489 } 490 491 void AliasSet::dump() const { print (std::cerr); } 492 void AliasSetTracker::dump() const { print(std::cerr); } 493 494 //===----------------------------------------------------------------------===// 495 // AliasSetPrinter Pass 496 //===----------------------------------------------------------------------===// 497 498 namespace { 499 class AliasSetPrinter : public FunctionPass { 500 AliasSetTracker *Tracker; 501 public: 502 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 503 AU.setPreservesAll(); 504 AU.addRequired<AliasAnalysis>(); 505 } 506 507 virtual bool runOnFunction(Function &F) { 508 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>()); 509 510 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 511 Tracker->add(&*I); 512 return false; 513 } 514 515 /// print - Convert to human readable form 516 virtual void print(std::ostream &OS) const { 517 Tracker->print(OS); 518 } 519 520 virtual void releaseMemory() { 521 delete Tracker; 522 } 523 }; 524 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer", 525 PassInfo::Analysis | PassInfo::Optimization); 526 } 527