AliasSetTracker.cpp revision 63e9930498a0eefaa6cfcca72213ec527cc72198
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> 24using namespace llvm; 25 26/// mergeSetIn - Merge the specified alias set into this alias set... 27/// 28void 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 AS.Forward = this; // Forward across AS now... 45 addRef(); // AS is now pointing to us... 46 47 // Merge the list of constituent pointers... 48 if (AS.PtrList) { 49 *PtrListEnd = AS.PtrList; 50 AS.PtrList->second.setPrevInList(PtrListEnd); 51 PtrListEnd = AS.PtrListEnd; 52 53 AS.PtrList = 0; 54 AS.PtrListEnd = &AS.PtrList; 55 } 56} 57 58void AliasSetTracker::removeAliasSet(AliasSet *AS) { 59 if (AliasSet *Fwd = AS->Forward) { 60 Fwd->dropRef(*this); 61 AS->Forward = 0; 62 } 63 AliasSets.erase(AS); 64} 65 66void AliasSet::removeFromTracker(AliasSetTracker &AST) { 67 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!"); 68 AST.removeAliasSet(this); 69} 70 71void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry, 72 unsigned Size) { 73 assert(!Entry.second.hasAliasSet() && "Entry already in set!"); 74 75 AliasAnalysis &AA = AST.getAliasAnalysis(); 76 77 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias 78 if (HashNodePair *P = getSomePointer()) { 79 AliasAnalysis::AliasResult Result = 80 AA.alias(P->first, P->second.getSize(), Entry.first, Size); 81 if (Result == AliasAnalysis::MayAlias) 82 AliasTy = MayAlias; 83 else // First entry of must alias must have maximum size! 84 P->second.updateSize(Size); 85 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!"); 86 } 87 88 Entry.second.setAliasSet(this); 89 Entry.second.updateSize(Size); 90 91 // Add it to the end of the list... 92 assert(*PtrListEnd == 0 && "End of list is not null?"); 93 *PtrListEnd = &Entry; 94 PtrListEnd = Entry.second.setPrevInList(PtrListEnd); 95 addRef(); // Entry points to alias set... 96} 97 98void 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/// 119bool 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 151bool 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/// 177AliasSet *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 192AliasSet *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... 211AliasSet &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 233bool AliasSetTracker::add(Value *Ptr, unsigned Size) { 234 bool NewPtr; 235 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr); 236 return NewPtr; 237} 238 239 240bool 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 249bool 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 259bool 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 267bool AliasSetTracker::add(CallSite CS) { 268 bool NewPtr; 269 if (Function *F = CS.getCalledFunction()) 270 if (AA.doesNotAccessMemory(F)) 271 return true; // doesn't alias anything 272 273 AliasSet *AS = findAliasSetForCallSite(CS); 274 if (!AS) { 275 AliasSets.push_back(new AliasSet()); 276 AS = &AliasSets.back(); 277 AS->addCallSite(CS, AA); 278 return true; 279 } else { 280 AS->addCallSite(CS, AA); 281 return false; 282 } 283} 284 285bool AliasSetTracker::add(Instruction *I) { 286 // Dispatch to one of the other add methods... 287 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 288 return add(LI); 289 else if (StoreInst *SI = dyn_cast<StoreInst>(I)) 290 return add(SI); 291 else if (CallInst *CI = dyn_cast<CallInst>(I)) 292 return add(CI); 293 else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) 294 return add(II); 295 else if (FreeInst *FI = dyn_cast<FreeInst>(I)) 296 return add(FI); 297 return true; 298} 299 300void AliasSetTracker::add(BasicBlock &BB) { 301 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) 302 add(I); 303} 304 305void AliasSetTracker::add(const AliasSetTracker &AST) { 306 assert(&AA == &AST.AA && 307 "Merging AliasSetTracker objects with different Alias Analyses!"); 308 309 // Loop over all of the alias sets in AST, adding the pointers contained 310 // therein into the current alias sets. This can cause alias sets to be 311 // merged together in the current AST. 312 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) 313 if (!I->Forward) { // Ignore forwarding alias sets 314 AliasSet &AS = const_cast<AliasSet&>(*I); 315 316 // If there are any call sites in the alias set, add them to this AST. 317 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i) 318 add(AS.CallSites[i]); 319 320 // Loop over all of the pointers in this alias set... 321 AliasSet::iterator I = AS.begin(), E = AS.end(); 322 bool X; 323 for (; I != E; ++I) 324 addPointer(I.getPointer(), I.getSize(), 325 (AliasSet::AccessType)AS.AccessTy, X); 326 } 327} 328 329/// remove - Remove the specified (potentially non-empty) alias set from the 330/// tracker. 331void AliasSetTracker::remove(AliasSet &AS) { 332 bool SetDead; 333 do { 334 AliasSet::iterator I = AS.begin(); 335 Value *Ptr = I.getPointer(); ++I; 336 337 // deleteValue will delete the set automatically when the last pointer 338 // reference is destroyed. "Predict" when this will happen. 339 SetDead = I == AS.end(); 340 deleteValue(Ptr); // Delete all of the pointers from the set 341 } while (!SetDead); 342} 343 344bool AliasSetTracker::remove(Value *Ptr, unsigned Size) { 345 AliasSet *AS = findAliasSetForPointer(Ptr, Size); 346 if (!AS) return false; 347 remove(*AS); 348 return true; 349} 350 351bool AliasSetTracker::remove(LoadInst *LI) { 352 unsigned Size = AA.getTargetData().getTypeSize(LI->getType()); 353 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size); 354 if (!AS) return false; 355 remove(*AS); 356 return true; 357} 358 359bool AliasSetTracker::remove(StoreInst *SI) { 360 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()); 361 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size); 362 if (!AS) return false; 363 remove(*AS); 364 return true; 365} 366 367bool AliasSetTracker::remove(FreeInst *FI) { 368 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0); 369 if (!AS) return false; 370 remove(*AS); 371 return true; 372} 373 374bool AliasSetTracker::remove(CallSite CS) { 375 if (Function *F = CS.getCalledFunction()) 376 if (AA.doesNotAccessMemory(F)) 377 return false; // doesn't alias anything 378 379 AliasSet *AS = findAliasSetForCallSite(CS); 380 if (!AS) return false; 381 remove(*AS); 382 return true; 383} 384 385bool AliasSetTracker::remove(Instruction *I) { 386 // Dispatch to one of the other remove methods... 387 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 388 return remove(LI); 389 else if (StoreInst *SI = dyn_cast<StoreInst>(I)) 390 return remove(SI); 391 else if (CallInst *CI = dyn_cast<CallInst>(I)) 392 return remove(CI); 393 else if (FreeInst *FI = dyn_cast<FreeInst>(I)) 394 return remove(FI); 395 return true; 396} 397 398 399// deleteValue method - This method is used to remove a pointer value from the 400// AliasSetTracker entirely. It should be used when an instruction is deleted 401// from the program to update the AST. If you don't use this, you would have 402// dangling pointers to deleted instructions. 403// 404void AliasSetTracker::deleteValue(Value *PtrVal) { 405 // First, look up the PointerRec for this pointer... 406 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal); 407 if (I == PointerMap.end()) return; // Noop 408 409 // If we found one, remove the pointer from the alias set it is in. 410 AliasSet::HashNodePair &PtrValEnt = *I; 411 AliasSet *AS = PtrValEnt.second.getAliasSet(*this); 412 413 // Unlink from the list of values... 414 PtrValEnt.second.removeFromList(); 415 // Stop using the alias set 416 AS->dropRef(*this); 417 PointerMap.erase(I); 418} 419 420 421//===----------------------------------------------------------------------===// 422// AliasSet/AliasSetTracker Printing Support 423//===----------------------------------------------------------------------===// 424 425void AliasSet::print(std::ostream &OS) const { 426 OS << " AliasSet[" << (void*)this << "," << RefCount << "] "; 427 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, "; 428 switch (AccessTy) { 429 case NoModRef: OS << "No access "; break; 430 case Refs : OS << "Ref "; break; 431 case Mods : OS << "Mod "; break; 432 case ModRef : OS << "Mod/Ref "; break; 433 default: assert(0 && "Bad value for AccessTy!"); 434 } 435 if (isVolatile()) OS << "[volatile] "; 436 if (Forward) 437 OS << " forwarding to " << (void*)Forward; 438 439 440 if (begin() != end()) { 441 OS << "Pointers: "; 442 for (iterator I = begin(), E = end(); I != E; ++I) { 443 if (I != begin()) OS << ", "; 444 WriteAsOperand(OS << "(", I.getPointer()); 445 OS << ", " << I.getSize() << ")"; 446 } 447 } 448 if (!CallSites.empty()) { 449 OS << "\n " << CallSites.size() << " Call Sites: "; 450 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) { 451 if (i) OS << ", "; 452 WriteAsOperand(OS, CallSites[i].getCalledValue()); 453 } 454 } 455 OS << "\n"; 456} 457 458void AliasSetTracker::print(std::ostream &OS) const { 459 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for " 460 << PointerMap.size() << " pointer values.\n"; 461 for (const_iterator I = begin(), E = end(); I != E; ++I) 462 I->print(OS); 463 OS << "\n"; 464} 465 466void AliasSet::dump() const { print (std::cerr); } 467void AliasSetTracker::dump() const { print(std::cerr); } 468 469//===----------------------------------------------------------------------===// 470// AliasSetPrinter Pass 471//===----------------------------------------------------------------------===// 472 473namespace { 474 class AliasSetPrinter : public FunctionPass { 475 AliasSetTracker *Tracker; 476 public: 477 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 478 AU.setPreservesAll(); 479 AU.addRequired<AliasAnalysis>(); 480 } 481 482 virtual bool runOnFunction(Function &F) { 483 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>()); 484 485 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 486 Tracker->add(&*I); 487 return false; 488 } 489 490 /// print - Convert to human readable form 491 virtual void print(std::ostream &OS) const { 492 Tracker->print(OS); 493 } 494 495 virtual void releaseMemory() { 496 delete Tracker; 497 } 498 }; 499 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer", 500 PassInfo::Analysis | PassInfo::Optimization); 501} 502