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