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