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" |
David Greene | e7ef6a9 | 2009-12-23 19:27:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.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()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 157 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 158 | if (AA.getModRefInfo(CallSites[i], Ptr, Size) != AliasAnalysis::NoModRef) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const { |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 166 | if (AA.doesNotAccessMemory(CS)) |
| 167 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 168 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) |
| 170 | if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef || |
| 171 | AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef) |
| 172 | return true; |
| 173 | |
| 174 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 175 | if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) != |
| 176 | AliasAnalysis::NoModRef) |
| 177 | return true; |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 182 | void AliasSetTracker::clear() { |
| 183 | // Delete all the PointerRec entries. |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 184 | for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end(); |
| 185 | I != E; ++I) |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 186 | I->second->eraseFromList(); |
| 187 | |
| 188 | PointerMap.clear(); |
| 189 | |
| 190 | // The alias sets should all be clear now. |
| 191 | AliasSets.clear(); |
| 192 | } |
| 193 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 194 | |
| 195 | /// findAliasSetForPointer - Given a pointer, find the one alias set to put the |
| 196 | /// instruction referring to the pointer into. If there are multiple alias sets |
| 197 | /// that may alias the pointer, merge them together and return the unified set. |
| 198 | /// |
| 199 | AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr, |
| 200 | unsigned Size) { |
| 201 | AliasSet *FoundSet = 0; |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 202 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 203 | if (I->Forward || !I->aliasesPointer(Ptr, Size, AA)) continue; |
| 204 | |
| 205 | if (FoundSet == 0) { // If this is the first alias set ptr can go into. |
| 206 | FoundSet = I; // Remember it. |
| 207 | } else { // Otherwise, we must merge the sets. |
| 208 | FoundSet->mergeSetIn(*I, *this); // Merge in contents. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 209 | } |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 210 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 211 | |
| 212 | return FoundSet; |
| 213 | } |
| 214 | |
| 215 | /// containsPointer - Return true if the specified location is represented by |
| 216 | /// this alias set, false otherwise. This does not modify the AST object or |
| 217 | /// alias sets. |
| 218 | bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const { |
| 219 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 220 | if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) |
| 221 | return true; |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) { |
| 228 | AliasSet *FoundSet = 0; |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 229 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 230 | if (I->Forward || !I->aliasesCallSite(CS, AA)) |
| 231 | continue; |
| 232 | |
| 233 | if (FoundSet == 0) { // If this is the first alias set ptr can go into. |
| 234 | FoundSet = I; // Remember it. |
| 235 | } else if (!I->Forward) { // Otherwise, we must merge the sets. |
| 236 | FoundSet->mergeSetIn(*I, *this); // Merge in contents. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 238 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 239 | return FoundSet; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | /// getAliasSetForPointer - Return the alias set that the specified pointer |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 246 | /// lives in. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 247 | AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size, |
| 248 | bool *New) { |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 249 | AliasSet::PointerRec &Entry = getEntryFor(Pointer); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 250 | |
| 251 | // Check to see if the pointer is already known... |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 252 | if (Entry.hasAliasSet()) { |
| 253 | Entry.updateSize(Size); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 254 | // Return the set! |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 255 | return *Entry.getAliasSet(*this)->getForwardedTarget(*this); |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 256 | } |
| 257 | |
| 258 | if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 259 | // Add it to the alias set it aliases... |
| 260 | AS->addPointer(*this, Entry, Size); |
| 261 | return *AS; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 263 | |
| 264 | if (New) *New = true; |
| 265 | // Otherwise create a new alias set to hold the loaded pointer... |
| 266 | AliasSets.push_back(new AliasSet()); |
| 267 | AliasSets.back().addPointer(*this, Entry, Size); |
| 268 | return AliasSets.back(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | bool AliasSetTracker::add(Value *Ptr, unsigned Size) { |
| 272 | bool NewPtr; |
| 273 | addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr); |
| 274 | return NewPtr; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | bool AliasSetTracker::add(LoadInst *LI) { |
| 279 | bool NewPtr; |
| 280 | AliasSet &AS = addPointer(LI->getOperand(0), |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 281 | AA.getTypeStoreSize(LI->getType()), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | AliasSet::Refs, NewPtr); |
| 283 | if (LI->isVolatile()) AS.setVolatile(); |
| 284 | return NewPtr; |
| 285 | } |
| 286 | |
| 287 | bool AliasSetTracker::add(StoreInst *SI) { |
| 288 | bool NewPtr; |
| 289 | Value *Val = SI->getOperand(0); |
| 290 | AliasSet &AS = addPointer(SI->getOperand(1), |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 291 | AA.getTypeStoreSize(Val->getType()), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 292 | AliasSet::Mods, NewPtr); |
| 293 | if (SI->isVolatile()) AS.setVolatile(); |
| 294 | return NewPtr; |
| 295 | } |
| 296 | |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 297 | bool AliasSetTracker::add(VAArgInst *VAAI) { |
| 298 | bool NewPtr; |
Chris Lattner | 81f40a9 | 2008-05-22 03:23:06 +0000 | [diff] [blame] | 299 | addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr); |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 300 | return NewPtr; |
| 301 | } |
| 302 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 303 | |
| 304 | bool AliasSetTracker::add(CallSite CS) { |
Zhou Sheng | f74f7a8 | 2009-03-03 06:02:04 +0000 | [diff] [blame] | 305 | if (isa<DbgInfoIntrinsic>(CS.getInstruction())) |
| 306 | return true; // Ignore DbgInfo Intrinsics. |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 307 | if (AA.doesNotAccessMemory(CS)) |
| 308 | return true; // doesn't alias anything |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 309 | |
| 310 | AliasSet *AS = findAliasSetForCallSite(CS); |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 311 | if (AS) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 312 | AS->addCallSite(CS, AA); |
| 313 | return false; |
| 314 | } |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 315 | AliasSets.push_back(new AliasSet()); |
| 316 | AS = &AliasSets.back(); |
| 317 | AS->addCallSite(CS, AA); |
| 318 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | bool AliasSetTracker::add(Instruction *I) { |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 322 | // Dispatch to one of the other add methods. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 323 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 324 | return add(LI); |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 325 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 326 | return add(SI); |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 327 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 328 | return add(CI); |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 329 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 330 | return add(II); |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 331 | if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I)) |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 332 | return add(VAAI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | |
| 336 | void AliasSetTracker::add(BasicBlock &BB) { |
| 337 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
| 338 | add(I); |
| 339 | } |
| 340 | |
| 341 | void AliasSetTracker::add(const AliasSetTracker &AST) { |
| 342 | assert(&AA == &AST.AA && |
| 343 | "Merging AliasSetTracker objects with different Alias Analyses!"); |
| 344 | |
| 345 | // Loop over all of the alias sets in AST, adding the pointers contained |
| 346 | // therein into the current alias sets. This can cause alias sets to be |
| 347 | // merged together in the current AST. |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 348 | for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) { |
| 349 | if (I->Forward) continue; // Ignore forwarding alias sets |
| 350 | |
| 351 | AliasSet &AS = const_cast<AliasSet&>(*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 352 | |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 353 | // If there are any call sites in the alias set, add them to this AST. |
| 354 | for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i) |
| 355 | add(AS.CallSites[i]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 356 | |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 357 | // Loop over all of the pointers in this alias set. |
| 358 | bool X; |
| 359 | for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) { |
| 360 | AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(), |
| 361 | (AliasSet::AccessType)AS.AccessTy, X); |
| 362 | if (AS.isVolatile()) NewAS.setVolatile(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 363 | } |
Chris Lattner | d0c8b89 | 2010-08-29 04:06:55 +0000 | [diff] [blame^] | 364 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /// remove - Remove the specified (potentially non-empty) alias set from the |
| 368 | /// tracker. |
| 369 | void AliasSetTracker::remove(AliasSet &AS) { |
| 370 | // Drop all call sites. |
| 371 | AS.CallSites.clear(); |
| 372 | |
| 373 | // Clear the alias set. |
| 374 | unsigned NumRefs = 0; |
| 375 | while (!AS.empty()) { |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 376 | AliasSet::PointerRec *P = AS.PtrList; |
| 377 | |
| 378 | Value *ValToRemove = P->getValue(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 380 | // Unlink and delete entry from the list of values. |
| 381 | P->eraseFromList(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 382 | |
| 383 | // Remember how many references need to be dropped. |
| 384 | ++NumRefs; |
| 385 | |
| 386 | // Finally, remove the entry. |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 387 | PointerMap.erase(ValToRemove); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Stop using the alias set, removing it. |
| 391 | AS.RefCount -= NumRefs; |
| 392 | if (AS.RefCount == 0) |
| 393 | AS.removeFromTracker(*this); |
| 394 | } |
| 395 | |
| 396 | bool AliasSetTracker::remove(Value *Ptr, unsigned Size) { |
| 397 | AliasSet *AS = findAliasSetForPointer(Ptr, Size); |
| 398 | if (!AS) return false; |
| 399 | remove(*AS); |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | bool AliasSetTracker::remove(LoadInst *LI) { |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 404 | unsigned Size = AA.getTypeStoreSize(LI->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 405 | AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size); |
| 406 | if (!AS) return false; |
| 407 | remove(*AS); |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | bool AliasSetTracker::remove(StoreInst *SI) { |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 412 | unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 413 | AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size); |
| 414 | if (!AS) return false; |
| 415 | remove(*AS); |
| 416 | return true; |
| 417 | } |
| 418 | |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 419 | bool AliasSetTracker::remove(VAArgInst *VAAI) { |
| 420 | AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0); |
| 421 | if (!AS) return false; |
| 422 | remove(*AS); |
| 423 | return true; |
| 424 | } |
| 425 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 426 | bool AliasSetTracker::remove(CallSite CS) { |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 427 | if (AA.doesNotAccessMemory(CS)) |
| 428 | return false; // doesn't alias anything |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 429 | |
| 430 | AliasSet *AS = findAliasSetForCallSite(CS); |
| 431 | if (!AS) return false; |
| 432 | remove(*AS); |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | bool AliasSetTracker::remove(Instruction *I) { |
| 437 | // Dispatch to one of the other remove methods... |
| 438 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 439 | return remove(LI); |
| 440 | else if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 441 | return remove(SI); |
| 442 | else if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 443 | return remove(CI); |
Dan Gohman | ddcc856 | 2008-04-14 18:34:50 +0000 | [diff] [blame] | 444 | else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I)) |
| 445 | return remove(VAAI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 446 | return true; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | // deleteValue method - This method is used to remove a pointer value from the |
| 451 | // AliasSetTracker entirely. It should be used when an instruction is deleted |
| 452 | // from the program to update the AST. If you don't use this, you would have |
| 453 | // dangling pointers to deleted instructions. |
| 454 | // |
| 455 | void AliasSetTracker::deleteValue(Value *PtrVal) { |
| 456 | // Notify the alias analysis implementation that this value is gone. |
| 457 | AA.deleteValue(PtrVal); |
| 458 | |
| 459 | // If this is a call instruction, remove the callsite from the appropriate |
| 460 | // AliasSet. |
Gabor Greif | 617e3d6 | 2010-07-28 10:46:09 +0000 | [diff] [blame] | 461 | if (CallSite CS = PtrVal) |
Duncan Sands | 00b24b5 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 462 | if (!AA.doesNotAccessMemory(CS)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 463 | if (AliasSet *AS = findAliasSetForCallSite(CS)) |
| 464 | AS->removeCallSite(CS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 465 | |
| 466 | // First, look up the PointerRec for this pointer. |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 467 | PointerMapType::iterator I = PointerMap.find(PtrVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 468 | if (I == PointerMap.end()) return; // Noop |
| 469 | |
| 470 | // 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] | 471 | AliasSet::PointerRec *PtrValEnt = I->second; |
| 472 | AliasSet *AS = PtrValEnt->getAliasSet(*this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 474 | // Unlink and delete from the list of values. |
| 475 | PtrValEnt->eraseFromList(); |
| 476 | |
| 477 | // Stop using the alias set. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 478 | AS->dropRef(*this); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 479 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 480 | PointerMap.erase(I); |
| 481 | } |
| 482 | |
| 483 | // copyValue - This method should be used whenever a preexisting value in the |
| 484 | // program is copied or cloned, introducing a new value. Note that it is ok for |
| 485 | // clients that use this method to introduce the same value multiple times: if |
| 486 | // the tracker already knows about a value, it will ignore the request. |
| 487 | // |
| 488 | void AliasSetTracker::copyValue(Value *From, Value *To) { |
| 489 | // Notify the alias analysis implementation that this value is copied. |
| 490 | AA.copyValue(From, To); |
| 491 | |
| 492 | // First, look up the PointerRec for this pointer. |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 493 | PointerMapType::iterator I = PointerMap.find(From); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 494 | if (I == PointerMap.end()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 495 | return; // Noop |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 496 | assert(I->second->hasAliasSet() && "Dead entry?"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 498 | AliasSet::PointerRec &Entry = getEntryFor(To); |
| 499 | if (Entry.hasAliasSet()) return; // Already in the tracker! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 500 | |
| 501 | // Add it to the alias set it aliases... |
Devang Patel | 5d11d43 | 2009-03-30 18:34:47 +0000 | [diff] [blame] | 502 | I = PointerMap.find(From); |
Chris Lattner | 47b3b8c | 2009-03-09 05:11:09 +0000 | [diff] [blame] | 503 | AliasSet *AS = I->second->getAliasSet(*this); |
| 504 | AS->addPointer(*this, Entry, I->second->getSize(), true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
| 508 | |
| 509 | //===----------------------------------------------------------------------===// |
| 510 | // AliasSet/AliasSetTracker Printing Support |
| 511 | //===----------------------------------------------------------------------===// |
| 512 | |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 513 | void AliasSet::print(raw_ostream &OS) const { |
| 514 | OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] "; |
Dan Gohman | cbec076 | 2008-04-21 19:48:48 +0000 | [diff] [blame] | 515 | OS << (AliasTy == MustAlias ? "must" : "may") << " alias, "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 516 | switch (AccessTy) { |
| 517 | case NoModRef: OS << "No access "; break; |
| 518 | case Refs : OS << "Ref "; break; |
| 519 | case Mods : OS << "Mod "; break; |
| 520 | case ModRef : OS << "Mod/Ref "; break; |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 521 | default: llvm_unreachable("Bad value for AccessTy!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 522 | } |
| 523 | if (isVolatile()) OS << "[volatile] "; |
| 524 | if (Forward) |
| 525 | OS << " forwarding to " << (void*)Forward; |
| 526 | |
| 527 | |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 528 | if (!empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 529 | OS << "Pointers: "; |
| 530 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 531 | if (I != begin()) OS << ", "; |
| 532 | WriteAsOperand(OS << "(", I.getPointer()); |
| 533 | OS << ", " << I.getSize() << ")"; |
| 534 | } |
| 535 | } |
| 536 | if (!CallSites.empty()) { |
| 537 | OS << "\n " << CallSites.size() << " Call Sites: "; |
| 538 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) { |
| 539 | if (i) OS << ", "; |
| 540 | WriteAsOperand(OS, CallSites[i].getCalledValue()); |
| 541 | } |
| 542 | } |
| 543 | OS << "\n"; |
| 544 | } |
| 545 | |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 546 | void AliasSetTracker::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 547 | OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for " |
| 548 | << PointerMap.size() << " pointer values.\n"; |
| 549 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 550 | I->print(OS); |
| 551 | OS << "\n"; |
| 552 | } |
| 553 | |
David Greene | e7ef6a9 | 2009-12-23 19:27:59 +0000 | [diff] [blame] | 554 | void AliasSet::dump() const { print(dbgs()); } |
| 555 | void AliasSetTracker::dump() const { print(dbgs()); } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 556 | |
| 557 | //===----------------------------------------------------------------------===// |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 558 | // ASTCallbackVH Class Implementation |
| 559 | //===----------------------------------------------------------------------===// |
| 560 | |
| 561 | void AliasSetTracker::ASTCallbackVH::deleted() { |
| 562 | assert(AST && "ASTCallbackVH called with a null AliasSetTracker!"); |
| 563 | AST->deleteValue(getValPtr()); |
| 564 | // this now dangles! |
| 565 | } |
| 566 | |
| 567 | AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast) |
Dan Gohman | 1e5054e | 2009-07-31 18:21:48 +0000 | [diff] [blame] | 568 | : CallbackVH(V), AST(ast) {} |
| 569 | |
| 570 | AliasSetTracker::ASTCallbackVH & |
| 571 | AliasSetTracker::ASTCallbackVH::operator=(Value *V) { |
| 572 | return *this = ASTCallbackVH(V, AST); |
| 573 | } |
Dan Gohman | 63b64a7 | 2009-07-30 20:21:41 +0000 | [diff] [blame] | 574 | |
| 575 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 576 | // AliasSetPrinter Pass |
| 577 | //===----------------------------------------------------------------------===// |
| 578 | |
| 579 | namespace { |
Nick Lewycky | 492d06e | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 580 | class AliasSetPrinter : public FunctionPass { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 581 | AliasSetTracker *Tracker; |
| 582 | public: |
| 583 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 7569322 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 584 | AliasSetPrinter() : FunctionPass(ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 585 | |
| 586 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 587 | AU.setPreservesAll(); |
| 588 | AU.addRequired<AliasAnalysis>(); |
| 589 | } |
| 590 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 591 | virtual bool runOnFunction(Function &F) { |
| 592 | Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>()); |
| 593 | |
| 594 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
| 595 | Tracker->add(&*I); |
David Greene | 092316b | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 596 | Tracker->print(errs()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 597 | delete Tracker; |
| 598 | return false; |
| 599 | } |
| 600 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 601 | } |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 602 | |
| 603 | char AliasSetPrinter::ID = 0; |
Owen Anderson | 6374c3d | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 604 | INITIALIZE_PASS(AliasSetPrinter, "print-alias-sets", |
| 605 | "Alias Set Printer", false, true); |