AliasSetTracker.h revision fb8096dee5df60f156e770b9f96f8417e9dbd4c9
1//===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines two classes: AliasSetTracker and AliasSet. These interface 11// are used to classify a collection of pointer references into a maximal number 12// of disjoint sets. Each AliasSet object constructed by the AliasSetTracker 13// object refers to memory disjoint from the other sets. 14// 15//===----------------------------------------------------------------------===// 16 17#ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H 18#define LLVM_ANALYSIS_ALIASSETTRACKER_H 19 20#include "llvm/Support/CallSite.h" 21#include "llvm/Support/ValueHandle.h" 22#include "llvm/ADT/DenseMap.h" 23#include "llvm/ADT/ilist.h" 24#include "llvm/ADT/ilist_node.h" 25#include <vector> 26 27namespace llvm { 28 29class AliasAnalysis; 30class LoadInst; 31class StoreInst; 32class VAArgInst; 33class AliasSetTracker; 34class AliasSet; 35 36class AliasSet : public ilist_node<AliasSet> { 37 friend class AliasSetTracker; 38 39 class PointerRec { 40 Value *Val; // The pointer this record corresponds to. 41 PointerRec **PrevInList, *NextInList; 42 AliasSet *AS; 43 unsigned Size; 44 const MDNode *TBAAInfo; 45 public: 46 PointerRec(Value *V) 47 : Val(V), PrevInList(0), NextInList(0), AS(0), Size(0), TBAAInfo(0) {} 48 49 Value *getValue() const { return Val; } 50 51 PointerRec *getNext() const { return NextInList; } 52 bool hasAliasSet() const { return AS != 0; } 53 54 PointerRec** setPrevInList(PointerRec **PIL) { 55 PrevInList = PIL; 56 return &NextInList; 57 } 58 59 void updateSizeAndTBAAInfo(unsigned NewSize, const MDNode *NewTBAAInfo) { 60 if (NewSize > Size) Size = NewSize; 61 62 if (!TBAAInfo) 63 TBAAInfo = NewTBAAInfo; 64 else if (TBAAInfo != NewTBAAInfo) 65 TBAAInfo = reinterpret_cast<const MDNode *>(-1); 66 } 67 68 unsigned getSize() const { return Size; } 69 70 /// getRawTBAAInfo - Return the raw TBAAInfo member. In addition to 71 /// being null or a pointer to an MDNode, this could be -1, meaning 72 /// there was conflicting information. 73 const MDNode *getRawTBAAInfo() const { 74 return TBAAInfo; 75 } 76 77 /// getTBAAInfo - Return the TBAAInfo, or null if there is no 78 /// information or conflicting information. 79 const MDNode *getTBAAInfo() const { 80 // If we have conflicting TBAAInfo, return null. 81 if (TBAAInfo == reinterpret_cast<const MDNode *>(-1)) 82 return 0; 83 return TBAAInfo; 84 } 85 86 AliasSet *getAliasSet(AliasSetTracker &AST) { 87 assert(AS && "No AliasSet yet!"); 88 if (AS->Forward) { 89 AliasSet *OldAS = AS; 90 AS = OldAS->getForwardedTarget(AST); 91 AS->addRef(); 92 OldAS->dropRef(AST); 93 } 94 return AS; 95 } 96 97 void setAliasSet(AliasSet *as) { 98 assert(AS == 0 && "Already have an alias set!"); 99 AS = as; 100 } 101 102 void eraseFromList() { 103 if (NextInList) NextInList->PrevInList = PrevInList; 104 *PrevInList = NextInList; 105 if (AS->PtrListEnd == &NextInList) { 106 AS->PtrListEnd = PrevInList; 107 assert(*AS->PtrListEnd == 0 && "List not terminated right!"); 108 } 109 delete this; 110 } 111 }; 112 113 PointerRec *PtrList, **PtrListEnd; // Doubly linked list of nodes. 114 AliasSet *Forward; // Forwarding pointer. 115 AliasSet *Next, *Prev; // Doubly linked list of AliasSets. 116 117 // All calls & invokes in this alias set. 118 std::vector<AssertingVH<Instruction> > CallSites; 119 120 // RefCount - Number of nodes pointing to this AliasSet plus the number of 121 // AliasSets forwarding to it. 122 unsigned RefCount : 28; 123 124 /// AccessType - Keep track of whether this alias set merely refers to the 125 /// locations of memory, whether it modifies the memory, or whether it does 126 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to 127 /// ModRef as necessary. 128 /// 129 enum AccessType { 130 NoModRef = 0, Refs = 1, // Ref = bit 1 131 Mods = 2, ModRef = 3 // Mod = bit 2 132 }; 133 unsigned AccessTy : 2; 134 135 /// AliasType - Keep track the relationships between the pointers in the set. 136 /// Lattice goes from MustAlias to MayAlias. 137 /// 138 enum AliasType { 139 MustAlias = 0, MayAlias = 1 140 }; 141 unsigned AliasTy : 1; 142 143 // Volatile - True if this alias set contains volatile loads or stores. 144 bool Volatile : 1; 145 146 void addRef() { ++RefCount; } 147 void dropRef(AliasSetTracker &AST) { 148 assert(RefCount >= 1 && "Invalid reference count detected!"); 149 if (--RefCount == 0) 150 removeFromTracker(AST); 151 } 152 153 CallSite getCallSite(unsigned i) const { 154 assert(i < CallSites.size()); 155 return CallSite(CallSites[i]); 156 } 157 158public: 159 /// Accessors... 160 bool isRef() const { return AccessTy & Refs; } 161 bool isMod() const { return AccessTy & Mods; } 162 bool isMustAlias() const { return AliasTy == MustAlias; } 163 bool isMayAlias() const { return AliasTy == MayAlias; } 164 165 // isVolatile - Return true if this alias set contains volatile loads or 166 // stores. 167 bool isVolatile() const { return Volatile; } 168 169 /// isForwardingAliasSet - Return true if this alias set should be ignored as 170 /// part of the AliasSetTracker object. 171 bool isForwardingAliasSet() const { return Forward; } 172 173 /// mergeSetIn - Merge the specified alias set into this alias set... 174 /// 175 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST); 176 177 // Alias Set iteration - Allow access to all of the pointer which are part of 178 // this alias set... 179 class iterator; 180 iterator begin() const { return iterator(PtrList); } 181 iterator end() const { return iterator(); } 182 bool empty() const { return PtrList == 0; } 183 184 void print(raw_ostream &OS) const; 185 void dump() const; 186 187 /// Define an iterator for alias sets... this is just a forward iterator. 188 class iterator : public std::iterator<std::forward_iterator_tag, 189 PointerRec, ptrdiff_t> { 190 PointerRec *CurNode; 191 public: 192 explicit iterator(PointerRec *CN = 0) : CurNode(CN) {} 193 194 bool operator==(const iterator& x) const { 195 return CurNode == x.CurNode; 196 } 197 bool operator!=(const iterator& x) const { return !operator==(x); } 198 199 const iterator &operator=(const iterator &I) { 200 CurNode = I.CurNode; 201 return *this; 202 } 203 204 value_type &operator*() const { 205 assert(CurNode && "Dereferencing AliasSet.end()!"); 206 return *CurNode; 207 } 208 value_type *operator->() const { return &operator*(); } 209 210 Value *getPointer() const { return CurNode->getValue(); } 211 unsigned getSize() const { return CurNode->getSize(); } 212 const MDNode *getRawTBAAInfo() const { return CurNode->getRawTBAAInfo(); } 213 const MDNode *getTBAAInfo() const { return CurNode->getTBAAInfo(); } 214 215 iterator& operator++() { // Preincrement 216 assert(CurNode && "Advancing past AliasSet.end()!"); 217 CurNode = CurNode->getNext(); 218 return *this; 219 } 220 iterator operator++(int) { // Postincrement 221 iterator tmp = *this; ++*this; return tmp; 222 } 223 }; 224 225private: 226 // Can only be created by AliasSetTracker. Also, ilist creates one 227 // to serve as a sentinel. 228 friend struct ilist_sentinel_traits<AliasSet>; 229 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0), 230 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) { 231 } 232 233 AliasSet(const AliasSet &AS); // do not implement 234 void operator=(const AliasSet &AS); // do not implement 235 236 PointerRec *getSomePointer() const { 237 return PtrList; 238 } 239 240 /// getForwardedTarget - Return the real alias set this represents. If this 241 /// has been merged with another set and is forwarding, return the ultimate 242 /// destination set. This also implements the union-find collapsing as well. 243 AliasSet *getForwardedTarget(AliasSetTracker &AST) { 244 if (!Forward) return this; 245 246 AliasSet *Dest = Forward->getForwardedTarget(AST); 247 if (Dest != Forward) { 248 Dest->addRef(); 249 Forward->dropRef(AST); 250 Forward = Dest; 251 } 252 return Dest; 253 } 254 255 void removeFromTracker(AliasSetTracker &AST); 256 257 void addPointer(AliasSetTracker &AST, PointerRec &Entry, unsigned Size, 258 const MDNode *TBAAInfo, 259 bool KnownMustAlias = false); 260 void addCallSite(CallSite CS, AliasAnalysis &AA); 261 void removeCallSite(CallSite CS) { 262 for (size_t i = 0, e = CallSites.size(); i != e; ++i) 263 if (CallSites[i] == CS.getInstruction()) { 264 CallSites[i] = CallSites.back(); 265 CallSites.pop_back(); 266 } 267 } 268 void setVolatile() { Volatile = true; } 269 270 /// aliasesPointer - Return true if the specified pointer "may" (or must) 271 /// alias one of the members in the set. 272 /// 273 bool aliasesPointer(const Value *Ptr, unsigned Size, const MDNode *TBAAInfo, 274 AliasAnalysis &AA) const; 275 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const; 276}; 277 278inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) { 279 AS.print(OS); 280 return OS; 281} 282 283 284class AliasSetTracker { 285 /// CallbackVH - A CallbackVH to arrange for AliasSetTracker to be 286 /// notified whenever a Value is deleted. 287 class ASTCallbackVH : public CallbackVH { 288 AliasSetTracker *AST; 289 virtual void deleted(); 290 public: 291 ASTCallbackVH(Value *V, AliasSetTracker *AST = 0); 292 ASTCallbackVH &operator=(Value *V); 293 }; 294 /// ASTCallbackVHDenseMapInfo - Traits to tell DenseMap that tell us how to 295 /// compare and hash the value handle. 296 struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {}; 297 298 AliasAnalysis &AA; 299 ilist<AliasSet> AliasSets; 300 301 typedef DenseMap<ASTCallbackVH, AliasSet::PointerRec*, 302 ASTCallbackVHDenseMapInfo> 303 PointerMapType; 304 305 // Map from pointers to their node 306 PointerMapType PointerMap; 307 308public: 309 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use 310 /// the specified alias analysis object to disambiguate load and store 311 /// addresses. 312 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {} 313 ~AliasSetTracker() { clear(); } 314 315 /// add methods - These methods are used to add different types of 316 /// instructions to the alias sets. Adding a new instruction can result in 317 /// one of three actions happening: 318 /// 319 /// 1. If the instruction doesn't alias any other sets, create a new set. 320 /// 2. If the instruction aliases exactly one set, add it to the set 321 /// 3. If the instruction aliases multiple sets, merge the sets, and add 322 /// the instruction to the result. 323 /// 324 /// These methods return true if inserting the instruction resulted in the 325 /// addition of a new alias set (i.e., the pointer did not alias anything). 326 /// 327 bool add(Value *Ptr, unsigned Size, const MDNode *TBAAInfo); // Add a location 328 bool add(LoadInst *LI); 329 bool add(StoreInst *SI); 330 bool add(VAArgInst *VAAI); 331 bool add(CallSite CS); // Call/Invoke instructions 332 bool add(CallInst *CI) { return add(CallSite(CI)); } 333 bool add(InvokeInst *II) { return add(CallSite(II)); } 334 bool add(Instruction *I); // Dispatch to one of the other add methods... 335 void add(BasicBlock &BB); // Add all instructions in basic block 336 void add(const AliasSetTracker &AST); // Add alias relations from another AST 337 338 /// remove methods - These methods are used to remove all entries that might 339 /// be aliased by the specified instruction. These methods return true if any 340 /// alias sets were eliminated. 341 // Remove a location 342 bool remove(Value *Ptr, unsigned Size, const MDNode *TBAAInfo); 343 bool remove(LoadInst *LI); 344 bool remove(StoreInst *SI); 345 bool remove(VAArgInst *VAAI); 346 bool remove(CallSite CS); 347 bool remove(CallInst *CI) { return remove(CallSite(CI)); } 348 bool remove(InvokeInst *II) { return remove(CallSite(II)); } 349 bool remove(Instruction *I); 350 void remove(AliasSet &AS); 351 352 void clear(); 353 354 /// getAliasSets - Return the alias sets that are active. 355 /// 356 const ilist<AliasSet> &getAliasSets() const { return AliasSets; } 357 358 /// getAliasSetForPointer - Return the alias set that the specified pointer 359 /// lives in. If the New argument is non-null, this method sets the value to 360 /// true if a new alias set is created to contain the pointer (because the 361 /// pointer didn't alias anything). 362 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, 363 const MDNode *TBAAInfo, 364 bool *New = 0); 365 366 /// getAliasSetForPointerIfExists - Return the alias set containing the 367 /// location specified if one exists, otherwise return null. 368 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size, 369 const MDNode *TBAAInfo) { 370 return findAliasSetForPointer(P, Size, TBAAInfo); 371 } 372 373 /// containsPointer - Return true if the specified location is represented by 374 /// this alias set, false otherwise. This does not modify the AST object or 375 /// alias sets. 376 bool containsPointer(Value *P, unsigned Size, const MDNode *TBAAInfo) const; 377 378 /// getAliasAnalysis - Return the underlying alias analysis object used by 379 /// this tracker. 380 AliasAnalysis &getAliasAnalysis() const { return AA; } 381 382 /// deleteValue method - This method is used to remove a pointer value from 383 /// the AliasSetTracker entirely. It should be used when an instruction is 384 /// deleted from the program to update the AST. If you don't use this, you 385 /// would have dangling pointers to deleted instructions. 386 /// 387 void deleteValue(Value *PtrVal); 388 389 /// copyValue - This method should be used whenever a preexisting value in the 390 /// program is copied or cloned, introducing a new value. Note that it is ok 391 /// for clients that use this method to introduce the same value multiple 392 /// times: if the tracker already knows about a value, it will ignore the 393 /// request. 394 /// 395 void copyValue(Value *From, Value *To); 396 397 398 typedef ilist<AliasSet>::iterator iterator; 399 typedef ilist<AliasSet>::const_iterator const_iterator; 400 401 const_iterator begin() const { return AliasSets.begin(); } 402 const_iterator end() const { return AliasSets.end(); } 403 404 iterator begin() { return AliasSets.begin(); } 405 iterator end() { return AliasSets.end(); } 406 407 void print(raw_ostream &OS) const; 408 void dump() const; 409 410private: 411 friend class AliasSet; 412 void removeAliasSet(AliasSet *AS); 413 414 // getEntryFor - Just like operator[] on the map, except that it creates an 415 // entry for the pointer if it doesn't already exist. 416 AliasSet::PointerRec &getEntryFor(Value *V) { 417 AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)]; 418 if (Entry == 0) 419 Entry = new AliasSet::PointerRec(V); 420 return *Entry; 421 } 422 423 AliasSet &addPointer(Value *P, unsigned Size, const MDNode *TBAAInfo, 424 AliasSet::AccessType E, 425 bool &NewSet) { 426 NewSet = false; 427 AliasSet &AS = getAliasSetForPointer(P, Size, TBAAInfo, &NewSet); 428 AS.AccessTy |= E; 429 return AS; 430 } 431 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size, 432 const MDNode *TBAAInfo); 433 434 AliasSet *findAliasSetForCallSite(CallSite CS); 435}; 436 437inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) { 438 AST.print(OS); 439 return OS; 440} 441 442} // End llvm namespace 443 444#endif 445