LLVM API Documentation
00001 //===- llvm/ADT/ValueMap.h - Safe map from Values to data -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the ValueMap class. ValueMap maps Value* or any subclass 00011 // to an arbitrary other type. It provides the DenseMap interface but updates 00012 // itself to remain safe when keys are RAUWed or deleted. By default, when a 00013 // key is RAUWed from V1 to V2, the old mapping V1->target is removed, and a new 00014 // mapping V2->target is added. If V2 already existed, its old target is 00015 // overwritten. When a key is deleted, its mapping is removed. 00016 // 00017 // You can override a ValueMap's Config parameter to control exactly what 00018 // happens on RAUW and destruction and to get called back on each event. It's 00019 // legal to call back into the ValueMap from a Config's callbacks. Config 00020 // parameters should inherit from ValueMapConfig<KeyT> to get default 00021 // implementations of all the methods ValueMap uses. See ValueMapConfig for 00022 // documentation of the functions you can override. 00023 // 00024 //===----------------------------------------------------------------------===// 00025 00026 #ifndef LLVM_ADT_VALUEMAP_H 00027 #define LLVM_ADT_VALUEMAP_H 00028 00029 #include "llvm/ADT/DenseMap.h" 00030 #include "llvm/Support/Mutex.h" 00031 #include "llvm/Support/ValueHandle.h" 00032 #include "llvm/Support/type_traits.h" 00033 #include <iterator> 00034 00035 namespace llvm { 00036 00037 template<typename KeyT, typename ValueT, typename Config> 00038 class ValueMapCallbackVH; 00039 00040 template<typename DenseMapT, typename KeyT> 00041 class ValueMapIterator; 00042 template<typename DenseMapT, typename KeyT> 00043 class ValueMapConstIterator; 00044 00045 /// This class defines the default behavior for configurable aspects of 00046 /// ValueMap<>. User Configs should inherit from this class to be as compatible 00047 /// as possible with future versions of ValueMap. 00048 template<typename KeyT> 00049 struct ValueMapConfig { 00050 /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's 00051 /// false, the ValueMap will leave the original mapping in place. 00052 enum { FollowRAUW = true }; 00053 00054 // All methods will be called with a first argument of type ExtraData. The 00055 // default implementations in this class take a templated first argument so 00056 // that users' subclasses can use any type they want without having to 00057 // override all the defaults. 00058 struct ExtraData {}; 00059 00060 template<typename ExtraDataT> 00061 static void onRAUW(const ExtraDataT & /*Data*/, KeyT /*Old*/, KeyT /*New*/) {} 00062 template<typename ExtraDataT> 00063 static void onDelete(const ExtraDataT &/*Data*/, KeyT /*Old*/) {} 00064 00065 /// Returns a mutex that should be acquired around any changes to the map. 00066 /// This is only acquired from the CallbackVH (and held around calls to onRAUW 00067 /// and onDelete) and not inside other ValueMap methods. NULL means that no 00068 /// mutex is necessary. 00069 template<typename ExtraDataT> 00070 static sys::Mutex *getMutex(const ExtraDataT &/*Data*/) { return NULL; } 00071 }; 00072 00073 /// See the file comment. 00074 template<typename KeyT, typename ValueT, typename Config =ValueMapConfig<KeyT> > 00075 class ValueMap { 00076 friend class ValueMapCallbackVH<KeyT, ValueT, Config>; 00077 typedef ValueMapCallbackVH<KeyT, ValueT, Config> ValueMapCVH; 00078 typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH> > MapT; 00079 typedef typename Config::ExtraData ExtraData; 00080 MapT Map; 00081 ExtraData Data; 00082 ValueMap(const ValueMap&) LLVM_DELETED_FUNCTION; 00083 ValueMap& operator=(const ValueMap&) LLVM_DELETED_FUNCTION; 00084 public: 00085 typedef KeyT key_type; 00086 typedef ValueT mapped_type; 00087 typedef std::pair<KeyT, ValueT> value_type; 00088 00089 explicit ValueMap(unsigned NumInitBuckets = 64) 00090 : Map(NumInitBuckets), Data() {} 00091 explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64) 00092 : Map(NumInitBuckets), Data(Data) {} 00093 00094 ~ValueMap() {} 00095 00096 typedef ValueMapIterator<MapT, KeyT> iterator; 00097 typedef ValueMapConstIterator<MapT, KeyT> const_iterator; 00098 inline iterator begin() { return iterator(Map.begin()); } 00099 inline iterator end() { return iterator(Map.end()); } 00100 inline const_iterator begin() const { return const_iterator(Map.begin()); } 00101 inline const_iterator end() const { return const_iterator(Map.end()); } 00102 00103 bool empty() const { return Map.empty(); } 00104 unsigned size() const { return Map.size(); } 00105 00106 /// Grow the map so that it has at least Size buckets. Does not shrink 00107 void resize(size_t Size) { Map.resize(Size); } 00108 00109 void clear() { Map.clear(); } 00110 00111 /// count - Return true if the specified key is in the map. 00112 bool count(const KeyT &Val) const { 00113 return Map.find_as(Val) != Map.end(); 00114 } 00115 00116 iterator find(const KeyT &Val) { 00117 return iterator(Map.find_as(Val)); 00118 } 00119 const_iterator find(const KeyT &Val) const { 00120 return const_iterator(Map.find_as(Val)); 00121 } 00122 00123 /// lookup - Return the entry for the specified key, or a default 00124 /// constructed value if no such entry exists. 00125 ValueT lookup(const KeyT &Val) const { 00126 typename MapT::const_iterator I = Map.find_as(Val); 00127 return I != Map.end() ? I->second : ValueT(); 00128 } 00129 00130 // Inserts key,value pair into the map if the key isn't already in the map. 00131 // If the key is already in the map, it returns false and doesn't update the 00132 // value. 00133 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) { 00134 std::pair<typename MapT::iterator, bool> map_result= 00135 Map.insert(std::make_pair(Wrap(KV.first), KV.second)); 00136 return std::make_pair(iterator(map_result.first), map_result.second); 00137 } 00138 00139 /// insert - Range insertion of pairs. 00140 template<typename InputIt> 00141 void insert(InputIt I, InputIt E) { 00142 for (; I != E; ++I) 00143 insert(*I); 00144 } 00145 00146 00147 bool erase(const KeyT &Val) { 00148 typename MapT::iterator I = Map.find_as(Val); 00149 if (I == Map.end()) 00150 return false; 00151 00152 Map.erase(I); 00153 return true; 00154 } 00155 void erase(iterator I) { 00156 return Map.erase(I.base()); 00157 } 00158 00159 value_type& FindAndConstruct(const KeyT &Key) { 00160 return Map.FindAndConstruct(Wrap(Key)); 00161 } 00162 00163 ValueT &operator[](const KeyT &Key) { 00164 return Map[Wrap(Key)]; 00165 } 00166 00167 /// isPointerIntoBucketsArray - Return true if the specified pointer points 00168 /// somewhere into the ValueMap's array of buckets (i.e. either to a key or 00169 /// value in the ValueMap). 00170 bool isPointerIntoBucketsArray(const void *Ptr) const { 00171 return Map.isPointerIntoBucketsArray(Ptr); 00172 } 00173 00174 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets 00175 /// array. In conjunction with the previous method, this can be used to 00176 /// determine whether an insertion caused the ValueMap to reallocate. 00177 const void *getPointerIntoBucketsArray() const { 00178 return Map.getPointerIntoBucketsArray(); 00179 } 00180 00181 private: 00182 // Takes a key being looked up in the map and wraps it into a 00183 // ValueMapCallbackVH, the actual key type of the map. We use a helper 00184 // function because ValueMapCVH is constructed with a second parameter. 00185 ValueMapCVH Wrap(KeyT key) const { 00186 // The only way the resulting CallbackVH could try to modify *this (making 00187 // the const_cast incorrect) is if it gets inserted into the map. But then 00188 // this function must have been called from a non-const method, making the 00189 // const_cast ok. 00190 return ValueMapCVH(key, const_cast<ValueMap*>(this)); 00191 } 00192 }; 00193 00194 // This CallbackVH updates its ValueMap when the contained Value changes, 00195 // according to the user's preferences expressed through the Config object. 00196 template<typename KeyT, typename ValueT, typename Config> 00197 class ValueMapCallbackVH : public CallbackVH { 00198 friend class ValueMap<KeyT, ValueT, Config>; 00199 friend struct DenseMapInfo<ValueMapCallbackVH>; 00200 typedef ValueMap<KeyT, ValueT, Config> ValueMapT; 00201 typedef typename llvm::remove_pointer<KeyT>::type KeySansPointerT; 00202 00203 ValueMapT *Map; 00204 00205 ValueMapCallbackVH(KeyT Key, ValueMapT *Map) 00206 : CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))), 00207 Map(Map) {} 00208 00209 public: 00210 KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); } 00211 00212 virtual void deleted() { 00213 // Make a copy that won't get changed even when *this is destroyed. 00214 ValueMapCallbackVH Copy(*this); 00215 sys::Mutex *M = Config::getMutex(Copy.Map->Data); 00216 if (M) 00217 M->acquire(); 00218 Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. 00219 Copy.Map->Map.erase(Copy); // Definitely destroys *this. 00220 if (M) 00221 M->release(); 00222 } 00223 virtual void allUsesReplacedWith(Value *new_key) { 00224 assert(isa<KeySansPointerT>(new_key) && 00225 "Invalid RAUW on key of ValueMap<>"); 00226 // Make a copy that won't get changed even when *this is destroyed. 00227 ValueMapCallbackVH Copy(*this); 00228 sys::Mutex *M = Config::getMutex(Copy.Map->Data); 00229 if (M) 00230 M->acquire(); 00231 00232 KeyT typed_new_key = cast<KeySansPointerT>(new_key); 00233 // Can destroy *this: 00234 Config::onRAUW(Copy.Map->Data, Copy.Unwrap(), typed_new_key); 00235 if (Config::FollowRAUW) { 00236 typename ValueMapT::MapT::iterator I = Copy.Map->Map.find(Copy); 00237 // I could == Copy.Map->Map.end() if the onRAUW callback already 00238 // removed the old mapping. 00239 if (I != Copy.Map->Map.end()) { 00240 ValueT Target(I->second); 00241 Copy.Map->Map.erase(I); // Definitely destroys *this. 00242 Copy.Map->insert(std::make_pair(typed_new_key, Target)); 00243 } 00244 } 00245 if (M) 00246 M->release(); 00247 } 00248 }; 00249 00250 template<typename KeyT, typename ValueT, typename Config> 00251 struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config> > { 00252 typedef ValueMapCallbackVH<KeyT, ValueT, Config> VH; 00253 typedef DenseMapInfo<KeyT> PointerInfo; 00254 00255 static inline VH getEmptyKey() { 00256 return VH(PointerInfo::getEmptyKey(), NULL); 00257 } 00258 static inline VH getTombstoneKey() { 00259 return VH(PointerInfo::getTombstoneKey(), NULL); 00260 } 00261 static unsigned getHashValue(const VH &Val) { 00262 return PointerInfo::getHashValue(Val.Unwrap()); 00263 } 00264 static unsigned getHashValue(const KeyT &Val) { 00265 return PointerInfo::getHashValue(Val); 00266 } 00267 static bool isEqual(const VH &LHS, const VH &RHS) { 00268 return LHS == RHS; 00269 } 00270 static bool isEqual(const KeyT &LHS, const VH &RHS) { 00271 return LHS == RHS.getValPtr(); 00272 } 00273 }; 00274 00275 00276 template<typename DenseMapT, typename KeyT> 00277 class ValueMapIterator : 00278 public std::iterator<std::forward_iterator_tag, 00279 std::pair<KeyT, typename DenseMapT::mapped_type>, 00280 ptrdiff_t> { 00281 typedef typename DenseMapT::iterator BaseT; 00282 typedef typename DenseMapT::mapped_type ValueT; 00283 BaseT I; 00284 public: 00285 ValueMapIterator() : I() {} 00286 00287 ValueMapIterator(BaseT I) : I(I) {} 00288 00289 BaseT base() const { return I; } 00290 00291 struct ValueTypeProxy { 00292 const KeyT first; 00293 ValueT& second; 00294 ValueTypeProxy *operator->() { return this; } 00295 operator std::pair<KeyT, ValueT>() const { 00296 return std::make_pair(first, second); 00297 } 00298 }; 00299 00300 ValueTypeProxy operator*() const { 00301 ValueTypeProxy Result = {I->first.Unwrap(), I->second}; 00302 return Result; 00303 } 00304 00305 ValueTypeProxy operator->() const { 00306 return operator*(); 00307 } 00308 00309 bool operator==(const ValueMapIterator &RHS) const { 00310 return I == RHS.I; 00311 } 00312 bool operator!=(const ValueMapIterator &RHS) const { 00313 return I != RHS.I; 00314 } 00315 00316 inline ValueMapIterator& operator++() { // Preincrement 00317 ++I; 00318 return *this; 00319 } 00320 ValueMapIterator operator++(int) { // Postincrement 00321 ValueMapIterator tmp = *this; ++*this; return tmp; 00322 } 00323 }; 00324 00325 template<typename DenseMapT, typename KeyT> 00326 class ValueMapConstIterator : 00327 public std::iterator<std::forward_iterator_tag, 00328 std::pair<KeyT, typename DenseMapT::mapped_type>, 00329 ptrdiff_t> { 00330 typedef typename DenseMapT::const_iterator BaseT; 00331 typedef typename DenseMapT::mapped_type ValueT; 00332 BaseT I; 00333 public: 00334 ValueMapConstIterator() : I() {} 00335 ValueMapConstIterator(BaseT I) : I(I) {} 00336 ValueMapConstIterator(ValueMapIterator<DenseMapT, KeyT> Other) 00337 : I(Other.base()) {} 00338 00339 BaseT base() const { return I; } 00340 00341 struct ValueTypeProxy { 00342 const KeyT first; 00343 const ValueT& second; 00344 ValueTypeProxy *operator->() { return this; } 00345 operator std::pair<KeyT, ValueT>() const { 00346 return std::make_pair(first, second); 00347 } 00348 }; 00349 00350 ValueTypeProxy operator*() const { 00351 ValueTypeProxy Result = {I->first.Unwrap(), I->second}; 00352 return Result; 00353 } 00354 00355 ValueTypeProxy operator->() const { 00356 return operator*(); 00357 } 00358 00359 bool operator==(const ValueMapConstIterator &RHS) const { 00360 return I == RHS.I; 00361 } 00362 bool operator!=(const ValueMapConstIterator &RHS) const { 00363 return I != RHS.I; 00364 } 00365 00366 inline ValueMapConstIterator& operator++() { // Preincrement 00367 ++I; 00368 return *this; 00369 } 00370 ValueMapConstIterator operator++(int) { // Postincrement 00371 ValueMapConstIterator tmp = *this; ++*this; return tmp; 00372 } 00373 }; 00374 00375 } // end namespace llvm 00376 00377 #endif