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