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