Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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" |
Zhou Sheng | f74f7a8 | 2009-03-03 06:02:04 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/Target/TargetData.h" |
| 21 | #include "llvm/Assembly/Writer.h" |
| 22 | #include "llvm/Support/Compiler.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/Format.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | AccessTy |= AS.AccessTy; |
| 37 | AliasTy |= AS.AliasTy; |
| 38 | |
| 39 | if (AliasTy == MustAlias) { |
| 40 | // Check that these two merged sets really are must aliases. Since both |
| 41 | // used to be must-alias sets, we can just check any pointer from each set |
| 42 | // for aliasing. |
| 43 | AliasAnalysis &AA = AST.getAliasAnalysis(); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 44 | PointerRec *L = getSomePointer(); |
| 45 | PointerRec *R = AS.getSomePointer(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | |
| 47 | // If the pointers are not a must-alias pair, this set becomes a may alias. |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 48 | if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | != AliasAnalysis::MustAlias) |
| 50 | AliasTy = MayAlias; |
| 51 | } |
| 52 | |
| 53 | if (CallSites.empty()) { // Merge call sites... |
| 54 | if (!AS.CallSites.empty()) |
| 55 | std::swap(CallSites, AS.CallSites); |
| 56 | } else if (!AS.CallSites.empty()) { |
| 57 | CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end()); |
| 58 | AS.CallSites.clear(); |
| 59 | } |
| 60 | |
| 61 | AS.Forward = this; // Forward across AS now... |
| 62 | addRef(); // AS is now pointing to us... |
| 63 | |
| 64 | // Merge the list of constituent pointers... |
| 65 | if (AS.PtrList) { |
| 66 | *PtrListEnd = AS.PtrList; |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 67 | AS.PtrList->setPrevInList(PtrListEnd); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 68 | PtrListEnd = AS.PtrListEnd; |
| 69 | |
| 70 | AS.PtrList = 0; |
| 71 | AS.PtrListEnd = &AS.PtrList; |
| 72 | assert(*AS.PtrListEnd == 0 && "End of list is not null?"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void AliasSetTracker::removeAliasSet(AliasSet *AS) { |
| 77 | if (AliasSet *Fwd = AS->Forward) { |
| 78 | Fwd->dropRef(*this); |
| 79 | AS->Forward = 0; |
| 80 | } |
| 81 | AliasSets.erase(AS); |
| 82 | } |
| 83 | |
| 84 | void AliasSet::removeFromTracker(AliasSetTracker &AST) { |
| 85 | assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!"); |
| 86 | AST.removeAliasSet(this); |
| 87 | } |
| 88 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 89 | void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | unsigned Size, bool KnownMustAlias) { |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 91 | assert(!Entry.hasAliasSet() && "Entry already in set!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 92 | |
| 93 | // Check to see if we have to downgrade to _may_ alias. |
| 94 | if (isMustAlias() && !KnownMustAlias) |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 95 | if (PointerRec *P = getSomePointer()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | AliasAnalysis &AA = AST.getAliasAnalysis(); |
| 97 | AliasAnalysis::AliasResult Result = |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 98 | AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | if (Result == AliasAnalysis::MayAlias) |
| 100 | AliasTy = MayAlias; |
| 101 | else // First entry of must alias must have maximum size! |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 102 | P->updateSize(Size); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 103 | assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!"); |
| 104 | } |
| 105 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 106 | Entry.setAliasSet(this); |
| 107 | Entry.updateSize(Size); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 108 | |
| 109 | // Add it to the end of the list... |
| 110 | assert(*PtrListEnd == 0 && "End of list is not null?"); |
| 111 | *PtrListEnd = &Entry; |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 112 | PtrListEnd = Entry.setPrevInList(PtrListEnd); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | assert(*PtrListEnd == 0 && "End of list is not null?"); |
| 114 | addRef(); // Entry points to alias set... |
| 115 | } |
| 116 | |
| 117 | void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) { |
| 118 | CallSites.push_back(CS); |
| 119 | |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 120 | AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS); |
| 121 | if (Behavior == AliasAnalysis::DoesNotAccessMemory) |
| 122 | return; |
| 123 | else if (Behavior == AliasAnalysis::OnlyReadsMemory) { |
| 124 | AliasTy = MayAlias; |
| 125 | AccessTy |= Refs; |
| 126 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // FIXME: This should use mod/ref information to make this not suck so bad |
| 130 | AliasTy = MayAlias; |
| 131 | AccessTy = ModRef; |
| 132 | } |
| 133 | |
| 134 | /// aliasesPointer - Return true if the specified pointer "may" (or must) |
| 135 | /// alias one of the members in the set. |
| 136 | /// |
| 137 | bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size, |
| 138 | AliasAnalysis &AA) const { |
| 139 | if (AliasTy == MustAlias) { |
| 140 | assert(CallSites.empty() && "Illegal must alias set!"); |
| 141 | |
| 142 | // If this is a set of MustAliases, only check to see if the pointer aliases |
| 143 | // SOME value in the set... |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 144 | PointerRec *SomePtr = getSomePointer(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 145 | assert(SomePtr && "Empty must-alias set??"); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 146 | return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // If this is a may-alias set, we have to check all of the pointers in the set |
| 150 | // to be sure it doesn't alias the set... |
| 151 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 152 | if (AA.alias(Ptr, Size, I.getPointer(), I.getSize())) |
| 153 | return true; |
| 154 | |
| 155 | // Check the call sites list and invoke list... |
| 156 | if (!CallSites.empty()) { |
| 157 | if (AA.hasNoModRefInfoForCalls()) |
| 158 | return true; |
| 159 | |
| 160 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) |
| 161 | if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size) |
| 162 | != AliasAnalysis::NoModRef) |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const { |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 170 | if (AA.doesNotAccessMemory(CS)) |
| 171 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 172 | |
| 173 | if (AA.hasNoModRefInfoForCalls()) |
| 174 | return true; |
| 175 | |
| 176 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) |
| 177 | if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef || |
| 178 | AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef) |
| 179 | return true; |
| 180 | |
| 181 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 182 | if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) != |
| 183 | AliasAnalysis::NoModRef) |
| 184 | return true; |
| 185 | |
| 186 | return false; |
| 187 | } |
| 188 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 189 | void AliasSetTracker::clear() { |
| 190 | // Delete all the PointerRec entries. |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 191 | for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end(); |
| 192 | I != E; ++I) |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 193 | I->second->eraseFromList(); |
| 194 | |
| 195 | PointerMap.clear(); |
| 196 | |
| 197 | // The alias sets should all be clear now. |
| 198 | AliasSets.clear(); |
| 199 | } |
| 200 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 201 | |
| 202 | /// findAliasSetForPointer - Given a pointer, find the one alias set to put the |
| 203 | /// instruction referring to the pointer into. If there are multiple alias sets |
| 204 | /// that may alias the pointer, merge them together and return the unified set. |
| 205 | /// |
| 206 | AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr, |
| 207 | unsigned Size) { |
| 208 | AliasSet *FoundSet = 0; |
| 209 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 210 | if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) { |
| 211 | if (FoundSet == 0) { // If this is the first alias set ptr can go into. |
| 212 | FoundSet = I; // Remember it. |
| 213 | } else { // Otherwise, we must merge the sets. |
| 214 | FoundSet->mergeSetIn(*I, *this); // Merge in contents. |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return FoundSet; |
| 219 | } |
| 220 | |
| 221 | /// containsPointer - Return true if the specified location is represented by |
| 222 | /// this alias set, false otherwise. This does not modify the AST object or |
| 223 | /// alias sets. |
| 224 | bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const { |
| 225 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 226 | if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) |
| 227 | return true; |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | |
| 233 | AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) { |
| 234 | AliasSet *FoundSet = 0; |
| 235 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 236 | if (!I->Forward && I->aliasesCallSite(CS, AA)) { |
| 237 | if (FoundSet == 0) { // If this is the first alias set ptr can go into. |
| 238 | FoundSet = I; // Remember it. |
| 239 | } else if (!I->Forward) { // Otherwise, we must merge the sets. |
| 240 | FoundSet->mergeSetIn(*I, *this); // Merge in contents. |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return FoundSet; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | /// getAliasSetForPointer - Return the alias set that the specified pointer |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 251 | /// lives in. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 252 | AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size, |
| 253 | bool *New) { |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 254 | AliasSet::PointerRec &Entry = getEntryFor(Pointer); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 255 | |
| 256 | // Check to see if the pointer is already known... |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 257 | if (Entry.hasAliasSet()) { |
| 258 | Entry.updateSize(Size); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 259 | // Return the set! |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 260 | return *Entry.getAliasSet(*this)->getForwardedTarget(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 261 | } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) { |
| 262 | // Add it to the alias set it aliases... |
| 263 | AS->addPointer(*this, Entry, Size); |
| 264 | return *AS; |
| 265 | } else { |
| 266 | if (New) *New = true; |
| 267 | // Otherwise create a new alias set to hold the loaded pointer... |
| 268 | AliasSets.push_back(new AliasSet()); |
| 269 | AliasSets.back().addPointer(*this, Entry, Size); |
| 270 | return AliasSets.back(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | bool AliasSetTracker::add(Value *Ptr, unsigned Size) { |
| 275 | bool NewPtr; |
| 276 | addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr); |
| 277 | return NewPtr; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | bool AliasSetTracker::add(LoadInst *LI) { |
| 282 | bool NewPtr; |
| 283 | AliasSet &AS = addPointer(LI->getOperand(0), |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 284 | AA.getTypeStoreSize(LI->getType()), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 285 | AliasSet::Refs, NewPtr); |
| 286 | if (LI->isVolatile()) AS.setVolatile(); |
| 287 | return NewPtr; |
| 288 | } |
| 289 | |
| 290 | bool AliasSetTracker::add(StoreInst *SI) { |
| 291 | bool NewPtr; |
| 292 | Value *Val = SI->getOperand(0); |
| 293 | AliasSet &AS = addPointer(SI->getOperand(1), |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 294 | AA.getTypeStoreSize(Val->getType()), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 295 | AliasSet::Mods, NewPtr); |
| 296 | if (SI->isVolatile()) AS.setVolatile(); |
| 297 | return NewPtr; |
| 298 | } |
| 299 | |
| 300 | bool AliasSetTracker::add(FreeInst *FI) { |
| 301 | bool NewPtr; |
Chris Lattner | 81f40a9 | 2008-05-22 03:23:06 +0000 | [diff] [blame] | 302 | addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 303 | return NewPtr; |
| 304 | } |
| 305 | |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 306 | bool AliasSetTracker::add(VAArgInst *VAAI) { |
| 307 | bool NewPtr; |
Chris Lattner | 81f40a9 | 2008-05-22 03:23:06 +0000 | [diff] [blame] | 308 | addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr); |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 309 | return NewPtr; |
| 310 | } |
| 311 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 312 | |
| 313 | bool AliasSetTracker::add(CallSite CS) { |
Zhou Sheng | f74f7a8 | 2009-03-03 06:02:04 +0000 | [diff] [blame] | 314 | if (isa<DbgInfoIntrinsic>(CS.getInstruction())) |
| 315 | return true; // Ignore DbgInfo Intrinsics. |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 316 | if (AA.doesNotAccessMemory(CS)) |
| 317 | return true; // doesn't alias anything |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 318 | |
| 319 | AliasSet *AS = findAliasSetForCallSite(CS); |
| 320 | if (!AS) { |
| 321 | AliasSets.push_back(new AliasSet()); |
| 322 | AS = &AliasSets.back(); |
| 323 | AS->addCallSite(CS, AA); |
| 324 | return true; |
| 325 | } else { |
| 326 | AS->addCallSite(CS, AA); |
| 327 | return false; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | bool AliasSetTracker::add(Instruction *I) { |
| 332 | // Dispatch to one of the other add methods... |
| 333 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 334 | return add(LI); |
| 335 | else if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 336 | return add(SI); |
| 337 | else if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 338 | return add(CI); |
| 339 | else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 340 | return add(II); |
| 341 | else if (FreeInst *FI = dyn_cast<FreeInst>(I)) |
| 342 | return add(FI); |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 343 | else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I)) |
| 344 | return add(VAAI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
| 348 | void AliasSetTracker::add(BasicBlock &BB) { |
| 349 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
| 350 | add(I); |
| 351 | } |
| 352 | |
| 353 | void AliasSetTracker::add(const AliasSetTracker &AST) { |
| 354 | assert(&AA == &AST.AA && |
| 355 | "Merging AliasSetTracker objects with different Alias Analyses!"); |
| 356 | |
| 357 | // Loop over all of the alias sets in AST, adding the pointers contained |
| 358 | // therein into the current alias sets. This can cause alias sets to be |
| 359 | // merged together in the current AST. |
| 360 | for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) |
| 361 | if (!I->Forward) { // Ignore forwarding alias sets |
| 362 | AliasSet &AS = const_cast<AliasSet&>(*I); |
| 363 | |
| 364 | // If there are any call sites in the alias set, add them to this AST. |
| 365 | for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i) |
| 366 | add(AS.CallSites[i]); |
| 367 | |
| 368 | // Loop over all of the pointers in this alias set... |
| 369 | AliasSet::iterator I = AS.begin(), E = AS.end(); |
| 370 | bool X; |
| 371 | for (; I != E; ++I) { |
| 372 | AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(), |
| 373 | (AliasSet::AccessType)AS.AccessTy, X); |
| 374 | if (AS.isVolatile()) NewAS.setVolatile(); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /// remove - Remove the specified (potentially non-empty) alias set from the |
| 380 | /// tracker. |
| 381 | void AliasSetTracker::remove(AliasSet &AS) { |
| 382 | // Drop all call sites. |
| 383 | AS.CallSites.clear(); |
| 384 | |
| 385 | // Clear the alias set. |
| 386 | unsigned NumRefs = 0; |
| 387 | while (!AS.empty()) { |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 388 | AliasSet::PointerRec *P = AS.PtrList; |
| 389 | |
| 390 | Value *ValToRemove = P->getValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 392 | // Unlink and delete entry from the list of values. |
| 393 | P->eraseFromList(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 394 | |
| 395 | // Remember how many references need to be dropped. |
| 396 | ++NumRefs; |
| 397 | |
| 398 | // Finally, remove the entry. |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 399 | PointerMap.erase(ValToRemove); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Stop using the alias set, removing it. |
| 403 | AS.RefCount -= NumRefs; |
| 404 | if (AS.RefCount == 0) |
| 405 | AS.removeFromTracker(*this); |
| 406 | } |
| 407 | |
| 408 | bool AliasSetTracker::remove(Value *Ptr, unsigned Size) { |
| 409 | AliasSet *AS = findAliasSetForPointer(Ptr, Size); |
| 410 | if (!AS) return false; |
| 411 | remove(*AS); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | bool AliasSetTracker::remove(LoadInst *LI) { |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 416 | unsigned Size = AA.getTypeStoreSize(LI->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 417 | AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size); |
| 418 | if (!AS) return false; |
| 419 | remove(*AS); |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | bool AliasSetTracker::remove(StoreInst *SI) { |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 424 | unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 425 | AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size); |
| 426 | if (!AS) return false; |
| 427 | remove(*AS); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | bool AliasSetTracker::remove(FreeInst *FI) { |
| 432 | AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0); |
| 433 | if (!AS) return false; |
| 434 | remove(*AS); |
| 435 | return true; |
| 436 | } |
| 437 | |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 438 | bool AliasSetTracker::remove(VAArgInst *VAAI) { |
| 439 | AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0); |
| 440 | if (!AS) return false; |
| 441 | remove(*AS); |
| 442 | return true; |
| 443 | } |
| 444 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 445 | bool AliasSetTracker::remove(CallSite CS) { |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 446 | if (AA.doesNotAccessMemory(CS)) |
| 447 | return false; // doesn't alias anything |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 448 | |
| 449 | AliasSet *AS = findAliasSetForCallSite(CS); |
| 450 | if (!AS) return false; |
| 451 | remove(*AS); |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | bool AliasSetTracker::remove(Instruction *I) { |
| 456 | // Dispatch to one of the other remove methods... |
| 457 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 458 | return remove(LI); |
| 459 | else if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 460 | return remove(SI); |
| 461 | else if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 462 | return remove(CI); |
| 463 | else if (FreeInst *FI = dyn_cast<FreeInst>(I)) |
| 464 | return remove(FI); |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 465 | else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I)) |
| 466 | return remove(VAAI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | // deleteValue method - This method is used to remove a pointer value from the |
| 472 | // AliasSetTracker entirely. It should be used when an instruction is deleted |
| 473 | // from the program to update the AST. If you don't use this, you would have |
| 474 | // dangling pointers to deleted instructions. |
| 475 | // |
| 476 | void AliasSetTracker::deleteValue(Value *PtrVal) { |
| 477 | // Notify the alias analysis implementation that this value is gone. |
| 478 | AA.deleteValue(PtrVal); |
| 479 | |
| 480 | // If this is a call instruction, remove the callsite from the appropriate |
| 481 | // AliasSet. |
| 482 | CallSite CS = CallSite::get(PtrVal); |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 483 | if (CS.getInstruction()) |
| 484 | if (!AA.doesNotAccessMemory(CS)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 485 | if (AliasSet *AS = findAliasSetForCallSite(CS)) |
| 486 | AS->removeCallSite(CS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 487 | |
| 488 | // First, look up the PointerRec for this pointer. |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 489 | PointerMapType::iterator I = PointerMap.find(PtrVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 490 | if (I == PointerMap.end()) return; // Noop |
| 491 | |
| 492 | // If we found one, remove the pointer from the alias set it is in. |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 493 | AliasSet::PointerRec *PtrValEnt = I->second; |
| 494 | AliasSet *AS = PtrValEnt->getAliasSet(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 496 | // Unlink and delete from the list of values. |
| 497 | PtrValEnt->eraseFromList(); |
| 498 | |
| 499 | // Stop using the alias set. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 500 | AS->dropRef(*this); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 501 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 502 | PointerMap.erase(I); |
| 503 | } |
| 504 | |
| 505 | // copyValue - This method should be used whenever a preexisting value in the |
| 506 | // program is copied or cloned, introducing a new value. Note that it is ok for |
| 507 | // clients that use this method to introduce the same value multiple times: if |
| 508 | // the tracker already knows about a value, it will ignore the request. |
| 509 | // |
| 510 | void AliasSetTracker::copyValue(Value *From, Value *To) { |
| 511 | // Notify the alias analysis implementation that this value is copied. |
| 512 | AA.copyValue(From, To); |
| 513 | |
| 514 | // First, look up the PointerRec for this pointer. |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 515 | PointerMapType::iterator I = PointerMap.find(From); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 516 | if (I == PointerMap.end()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 517 | return; // Noop |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 518 | assert(I->second->hasAliasSet() && "Dead entry?"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 520 | AliasSet::PointerRec &Entry = getEntryFor(To); |
| 521 | if (Entry.hasAliasSet()) return; // Already in the tracker! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 522 | |
| 523 | // Add it to the alias set it aliases... |
Devang Patel | 5d11d43 | 2009-03-30 18:34:47 +0000 | [diff] [blame] | 524 | I = PointerMap.find(From); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 525 | AliasSet *AS = I->second->getAliasSet(*this); |
| 526 | AS->addPointer(*this, Entry, I->second->getSize(), true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
| 530 | |
| 531 | //===----------------------------------------------------------------------===// |
| 532 | // AliasSet/AliasSetTracker Printing Support |
| 533 | //===----------------------------------------------------------------------===// |
| 534 | |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame^] | 535 | void AliasSet::print(raw_ostream &OS) const { |
| 536 | OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] "; |
Dan Gohman | cbec076 | 2008-04-21 19:48:48 +0000 | [diff] [blame] | 537 | OS << (AliasTy == MustAlias ? "must" : "may") << " alias, "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 538 | switch (AccessTy) { |
| 539 | case NoModRef: OS << "No access "; break; |
| 540 | case Refs : OS << "Ref "; break; |
| 541 | case Mods : OS << "Mod "; break; |
| 542 | case ModRef : OS << "Mod/Ref "; break; |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 543 | default: llvm_unreachable("Bad value for AccessTy!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 544 | } |
| 545 | if (isVolatile()) OS << "[volatile] "; |
| 546 | if (Forward) |
| 547 | OS << " forwarding to " << (void*)Forward; |
| 548 | |
| 549 | |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 550 | if (!empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 551 | OS << "Pointers: "; |
| 552 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 553 | if (I != begin()) OS << ", "; |
| 554 | WriteAsOperand(OS << "(", I.getPointer()); |
| 555 | OS << ", " << I.getSize() << ")"; |
| 556 | } |
| 557 | } |
| 558 | if (!CallSites.empty()) { |
| 559 | OS << "\n " << CallSites.size() << " Call Sites: "; |
| 560 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) { |
| 561 | if (i) OS << ", "; |
| 562 | WriteAsOperand(OS, CallSites[i].getCalledValue()); |
| 563 | } |
| 564 | } |
| 565 | OS << "\n"; |
| 566 | } |
| 567 | |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame^] | 568 | void AliasSetTracker::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 569 | OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for " |
| 570 | << PointerMap.size() << " pointer values.\n"; |
| 571 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 572 | I->print(OS); |
| 573 | OS << "\n"; |
| 574 | } |
| 575 | |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame^] | 576 | void AliasSet::dump() const { print(errs()); } |
| 577 | void AliasSetTracker::dump() const { print(errs()); } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 578 | |
| 579 | //===----------------------------------------------------------------------===// |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 580 | // ASTCallbackVH Class Implementation |
| 581 | //===----------------------------------------------------------------------===// |
| 582 | |
| 583 | void AliasSetTracker::ASTCallbackVH::deleted() { |
| 584 | assert(AST && "ASTCallbackVH called with a null AliasSetTracker!"); |
| 585 | AST->deleteValue(getValPtr()); |
| 586 | // this now dangles! |
| 587 | } |
| 588 | |
| 589 | AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast) |
Dan Gohman | 1e5054e | 2009-07-31 18:21:48 +0000 | [diff] [blame] | 590 | : CallbackVH(V), AST(ast) {} |
| 591 | |
| 592 | AliasSetTracker::ASTCallbackVH & |
| 593 | AliasSetTracker::ASTCallbackVH::operator=(Value *V) { |
| 594 | return *this = ASTCallbackVH(V, AST); |
| 595 | } |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 596 | |
| 597 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 598 | // AliasSetPrinter Pass |
| 599 | //===----------------------------------------------------------------------===// |
| 600 | |
| 601 | namespace { |
| 602 | class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass { |
| 603 | AliasSetTracker *Tracker; |
| 604 | public: |
| 605 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 606 | AliasSetPrinter() : FunctionPass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 607 | |
| 608 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 609 | AU.setPreservesAll(); |
| 610 | AU.addRequired<AliasAnalysis>(); |
| 611 | } |
| 612 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 613 | virtual bool runOnFunction(Function &F) { |
| 614 | Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>()); |
| 615 | |
| 616 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 617 | Tracker->add(&*I); |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame^] | 618 | Tracker->print(errs()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 619 | delete Tracker; |
| 620 | return false; |
| 621 | } |
| 622 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 623 | } |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 624 | |
| 625 | char AliasSetPrinter::ID = 0; |
| 626 | static RegisterPass<AliasSetPrinter> |
| 627 | X("print-alias-sets", "Alias Set Printer", false, true); |