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