1198396Srdivacky//===- llvm/ADT/ValueMap.h - Safe map from Values to data -------*- C++ -*-===// 2198396Srdivacky// 3198396Srdivacky// The LLVM Compiler Infrastructure 4198396Srdivacky// 5198396Srdivacky// This file is distributed under the University of Illinois Open Source 6198396Srdivacky// License. See LICENSE.TXT for details. 7198396Srdivacky// 8198396Srdivacky//===----------------------------------------------------------------------===// 9198396Srdivacky// 10198892Srdivacky// This file defines the ValueMap class. ValueMap maps Value* or any subclass 11198892Srdivacky// to an arbitrary other type. It provides the DenseMap interface but updates 12198892Srdivacky// itself to remain safe when keys are RAUWed or deleted. By default, when a 13198892Srdivacky// key is RAUWed from V1 to V2, the old mapping V1->target is removed, and a new 14198892Srdivacky// mapping V2->target is added. If V2 already existed, its old target is 15198892Srdivacky// overwritten. When a key is deleted, its mapping is removed. 16198396Srdivacky// 17198892Srdivacky// You can override a ValueMap's Config parameter to control exactly what 18198892Srdivacky// happens on RAUW and destruction and to get called back on each event. It's 19198892Srdivacky// legal to call back into the ValueMap from a Config's callbacks. Config 20198892Srdivacky// parameters should inherit from ValueMapConfig<KeyT> to get default 21198892Srdivacky// implementations of all the methods ValueMap uses. See ValueMapConfig for 22198892Srdivacky// documentation of the functions you can override. 23198892Srdivacky// 24198396Srdivacky//===----------------------------------------------------------------------===// 25198396Srdivacky 26198396Srdivacky#ifndef LLVM_ADT_VALUEMAP_H 27198396Srdivacky#define LLVM_ADT_VALUEMAP_H 28198396Srdivacky 29198396Srdivacky#include "llvm/ADT/DenseMap.h" 30249423Sdim#include "llvm/Support/Mutex.h" 31198396Srdivacky#include "llvm/Support/ValueHandle.h" 32198396Srdivacky#include "llvm/Support/type_traits.h" 33198396Srdivacky#include <iterator> 34198396Srdivacky 35198396Srdivackynamespace llvm { 36198396Srdivacky 37234353Sdimtemplate<typename KeyT, typename ValueT, typename Config> 38198396Srdivackyclass ValueMapCallbackVH; 39198396Srdivacky 40198396Srdivackytemplate<typename DenseMapT, typename KeyT> 41198396Srdivackyclass ValueMapIterator; 42198396Srdivackytemplate<typename DenseMapT, typename KeyT> 43198396Srdivackyclass ValueMapConstIterator; 44198396Srdivacky 45198892Srdivacky/// This class defines the default behavior for configurable aspects of 46198892Srdivacky/// ValueMap<>. User Configs should inherit from this class to be as compatible 47198892Srdivacky/// as possible with future versions of ValueMap. 48198396Srdivackytemplate<typename KeyT> 49198396Srdivackystruct ValueMapConfig { 50198396Srdivacky /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's 51198396Srdivacky /// false, the ValueMap will leave the original mapping in place. 52198396Srdivacky enum { FollowRAUW = true }; 53198396Srdivacky 54198396Srdivacky // All methods will be called with a first argument of type ExtraData. The 55198396Srdivacky // default implementations in this class take a templated first argument so 56198396Srdivacky // that users' subclasses can use any type they want without having to 57198396Srdivacky // override all the defaults. 58198396Srdivacky struct ExtraData {}; 59198396Srdivacky 60198396Srdivacky template<typename ExtraDataT> 61210299Sed static void onRAUW(const ExtraDataT & /*Data*/, KeyT /*Old*/, KeyT /*New*/) {} 62198396Srdivacky template<typename ExtraDataT> 63210299Sed static void onDelete(const ExtraDataT &/*Data*/, KeyT /*Old*/) {} 64198396Srdivacky 65198396Srdivacky /// Returns a mutex that should be acquired around any changes to the map. 66198396Srdivacky /// This is only acquired from the CallbackVH (and held around calls to onRAUW 67198892Srdivacky /// and onDelete) and not inside other ValueMap methods. NULL means that no 68198396Srdivacky /// mutex is necessary. 69198396Srdivacky template<typename ExtraDataT> 70210299Sed static sys::Mutex *getMutex(const ExtraDataT &/*Data*/) { return NULL; } 71198396Srdivacky}; 72198396Srdivacky 73198892Srdivacky/// See the file comment. 74234353Sdimtemplate<typename KeyT, typename ValueT, typename Config =ValueMapConfig<KeyT> > 75198396Srdivackyclass ValueMap { 76234353Sdim friend class ValueMapCallbackVH<KeyT, ValueT, Config>; 77234353Sdim typedef ValueMapCallbackVH<KeyT, ValueT, Config> ValueMapCVH; 78234353Sdim typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH> > MapT; 79198396Srdivacky typedef typename Config::ExtraData ExtraData; 80198396Srdivacky MapT Map; 81198396Srdivacky ExtraData Data; 82243830Sdim ValueMap(const ValueMap&) LLVM_DELETED_FUNCTION; 83243830Sdim ValueMap& operator=(const ValueMap&) LLVM_DELETED_FUNCTION; 84198396Srdivackypublic: 85198396Srdivacky typedef KeyT key_type; 86198396Srdivacky typedef ValueT mapped_type; 87198396Srdivacky typedef std::pair<KeyT, ValueT> value_type; 88198396Srdivacky 89198396Srdivacky explicit ValueMap(unsigned NumInitBuckets = 64) 90198396Srdivacky : Map(NumInitBuckets), Data() {} 91198396Srdivacky explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64) 92198396Srdivacky : Map(NumInitBuckets), Data(Data) {} 93198396Srdivacky 94198396Srdivacky ~ValueMap() {} 95198396Srdivacky 96198396Srdivacky typedef ValueMapIterator<MapT, KeyT> iterator; 97198396Srdivacky typedef ValueMapConstIterator<MapT, KeyT> const_iterator; 98198396Srdivacky inline iterator begin() { return iterator(Map.begin()); } 99198396Srdivacky inline iterator end() { return iterator(Map.end()); } 100198396Srdivacky inline const_iterator begin() const { return const_iterator(Map.begin()); } 101198396Srdivacky inline const_iterator end() const { return const_iterator(Map.end()); } 102198396Srdivacky 103198396Srdivacky bool empty() const { return Map.empty(); } 104198396Srdivacky unsigned size() const { return Map.size(); } 105198396Srdivacky 106198396Srdivacky /// Grow the map so that it has at least Size buckets. Does not shrink 107198396Srdivacky void resize(size_t Size) { Map.resize(Size); } 108198396Srdivacky 109198396Srdivacky void clear() { Map.clear(); } 110198396Srdivacky 111198396Srdivacky /// count - Return true if the specified key is in the map. 112198396Srdivacky bool count(const KeyT &Val) const { 113239462Sdim return Map.find_as(Val) != Map.end(); 114198396Srdivacky } 115198396Srdivacky 116198396Srdivacky iterator find(const KeyT &Val) { 117239462Sdim return iterator(Map.find_as(Val)); 118198396Srdivacky } 119198396Srdivacky const_iterator find(const KeyT &Val) const { 120239462Sdim return const_iterator(Map.find_as(Val)); 121198396Srdivacky } 122198396Srdivacky 123198396Srdivacky /// lookup - Return the entry for the specified key, or a default 124198396Srdivacky /// constructed value if no such entry exists. 125198396Srdivacky ValueT lookup(const KeyT &Val) const { 126239462Sdim typename MapT::const_iterator I = Map.find_as(Val); 127239462Sdim return I != Map.end() ? I->second : ValueT(); 128198396Srdivacky } 129198396Srdivacky 130198396Srdivacky // Inserts key,value pair into the map if the key isn't already in the map. 131198396Srdivacky // If the key is already in the map, it returns false and doesn't update the 132198396Srdivacky // value. 133198396Srdivacky std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) { 134198396Srdivacky std::pair<typename MapT::iterator, bool> map_result= 135198396Srdivacky Map.insert(std::make_pair(Wrap(KV.first), KV.second)); 136198396Srdivacky return std::make_pair(iterator(map_result.first), map_result.second); 137198396Srdivacky } 138198396Srdivacky 139198396Srdivacky /// insert - Range insertion of pairs. 140198396Srdivacky template<typename InputIt> 141198396Srdivacky void insert(InputIt I, InputIt E) { 142198396Srdivacky for (; I != E; ++I) 143198396Srdivacky insert(*I); 144198396Srdivacky } 145198396Srdivacky 146198396Srdivacky 147198396Srdivacky bool erase(const KeyT &Val) { 148239462Sdim typename MapT::iterator I = Map.find_as(Val); 149239462Sdim if (I == Map.end()) 150239462Sdim return false; 151239462Sdim 152239462Sdim Map.erase(I); 153239462Sdim return true; 154198396Srdivacky } 155212904Sdim void erase(iterator I) { 156198396Srdivacky return Map.erase(I.base()); 157198396Srdivacky } 158198396Srdivacky 159198396Srdivacky value_type& FindAndConstruct(const KeyT &Key) { 160198396Srdivacky return Map.FindAndConstruct(Wrap(Key)); 161198396Srdivacky } 162198396Srdivacky 163198396Srdivacky ValueT &operator[](const KeyT &Key) { 164198396Srdivacky return Map[Wrap(Key)]; 165198396Srdivacky } 166198396Srdivacky 167198396Srdivacky /// isPointerIntoBucketsArray - Return true if the specified pointer points 168198396Srdivacky /// somewhere into the ValueMap's array of buckets (i.e. either to a key or 169198396Srdivacky /// value in the ValueMap). 170198396Srdivacky bool isPointerIntoBucketsArray(const void *Ptr) const { 171198396Srdivacky return Map.isPointerIntoBucketsArray(Ptr); 172198396Srdivacky } 173198396Srdivacky 174198396Srdivacky /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets 175198396Srdivacky /// array. In conjunction with the previous method, this can be used to 176198396Srdivacky /// determine whether an insertion caused the ValueMap to reallocate. 177198396Srdivacky const void *getPointerIntoBucketsArray() const { 178198396Srdivacky return Map.getPointerIntoBucketsArray(); 179198396Srdivacky } 180198396Srdivacky 181198396Srdivackyprivate: 182198892Srdivacky // Takes a key being looked up in the map and wraps it into a 183198892Srdivacky // ValueMapCallbackVH, the actual key type of the map. We use a helper 184198892Srdivacky // function because ValueMapCVH is constructed with a second parameter. 185198396Srdivacky ValueMapCVH Wrap(KeyT key) const { 186198396Srdivacky // The only way the resulting CallbackVH could try to modify *this (making 187198396Srdivacky // the const_cast incorrect) is if it gets inserted into the map. But then 188198396Srdivacky // this function must have been called from a non-const method, making the 189198396Srdivacky // const_cast ok. 190198396Srdivacky return ValueMapCVH(key, const_cast<ValueMap*>(this)); 191198396Srdivacky } 192198396Srdivacky}; 193198396Srdivacky 194198892Srdivacky// This CallbackVH updates its ValueMap when the contained Value changes, 195198892Srdivacky// according to the user's preferences expressed through the Config object. 196234353Sdimtemplate<typename KeyT, typename ValueT, typename Config> 197198396Srdivackyclass ValueMapCallbackVH : public CallbackVH { 198234353Sdim friend class ValueMap<KeyT, ValueT, Config>; 199198892Srdivacky friend struct DenseMapInfo<ValueMapCallbackVH>; 200234353Sdim typedef ValueMap<KeyT, ValueT, Config> ValueMapT; 201198396Srdivacky typedef typename llvm::remove_pointer<KeyT>::type KeySansPointerT; 202198396Srdivacky 203198396Srdivacky ValueMapT *Map; 204198396Srdivacky 205198396Srdivacky ValueMapCallbackVH(KeyT Key, ValueMapT *Map) 206198396Srdivacky : CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))), 207198396Srdivacky Map(Map) {} 208198396Srdivacky 209198396Srdivackypublic: 210198396Srdivacky KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); } 211198396Srdivacky 212198396Srdivacky virtual void deleted() { 213198396Srdivacky // Make a copy that won't get changed even when *this is destroyed. 214198396Srdivacky ValueMapCallbackVH Copy(*this); 215198396Srdivacky sys::Mutex *M = Config::getMutex(Copy.Map->Data); 216198396Srdivacky if (M) 217198396Srdivacky M->acquire(); 218198892Srdivacky Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. 219198396Srdivacky Copy.Map->Map.erase(Copy); // Definitely destroys *this. 220198396Srdivacky if (M) 221198396Srdivacky M->release(); 222198396Srdivacky } 223198396Srdivacky virtual void allUsesReplacedWith(Value *new_key) { 224198396Srdivacky assert(isa<KeySansPointerT>(new_key) && 225198396Srdivacky "Invalid RAUW on key of ValueMap<>"); 226198396Srdivacky // Make a copy that won't get changed even when *this is destroyed. 227198396Srdivacky ValueMapCallbackVH Copy(*this); 228198396Srdivacky sys::Mutex *M = Config::getMutex(Copy.Map->Data); 229198396Srdivacky if (M) 230198396Srdivacky M->acquire(); 231198396Srdivacky 232198396Srdivacky KeyT typed_new_key = cast<KeySansPointerT>(new_key); 233198396Srdivacky // Can destroy *this: 234198396Srdivacky Config::onRAUW(Copy.Map->Data, Copy.Unwrap(), typed_new_key); 235198396Srdivacky if (Config::FollowRAUW) { 236198396Srdivacky typename ValueMapT::MapT::iterator I = Copy.Map->Map.find(Copy); 237198396Srdivacky // I could == Copy.Map->Map.end() if the onRAUW callback already 238198396Srdivacky // removed the old mapping. 239198396Srdivacky if (I != Copy.Map->Map.end()) { 240198396Srdivacky ValueT Target(I->second); 241198396Srdivacky Copy.Map->Map.erase(I); // Definitely destroys *this. 242198396Srdivacky Copy.Map->insert(std::make_pair(typed_new_key, Target)); 243198396Srdivacky } 244198396Srdivacky } 245198396Srdivacky if (M) 246198396Srdivacky M->release(); 247198396Srdivacky } 248198396Srdivacky}; 249198396Srdivacky 250234353Sdimtemplate<typename KeyT, typename ValueT, typename Config> 251234353Sdimstruct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config> > { 252234353Sdim typedef ValueMapCallbackVH<KeyT, ValueT, Config> VH; 253198396Srdivacky typedef DenseMapInfo<KeyT> PointerInfo; 254198396Srdivacky 255198396Srdivacky static inline VH getEmptyKey() { 256198396Srdivacky return VH(PointerInfo::getEmptyKey(), NULL); 257198396Srdivacky } 258198396Srdivacky static inline VH getTombstoneKey() { 259198396Srdivacky return VH(PointerInfo::getTombstoneKey(), NULL); 260198396Srdivacky } 261198396Srdivacky static unsigned getHashValue(const VH &Val) { 262198396Srdivacky return PointerInfo::getHashValue(Val.Unwrap()); 263198396Srdivacky } 264239462Sdim static unsigned getHashValue(const KeyT &Val) { 265239462Sdim return PointerInfo::getHashValue(Val); 266239462Sdim } 267198396Srdivacky static bool isEqual(const VH &LHS, const VH &RHS) { 268198396Srdivacky return LHS == RHS; 269198396Srdivacky } 270239462Sdim static bool isEqual(const KeyT &LHS, const VH &RHS) { 271239462Sdim return LHS == RHS.getValPtr(); 272239462Sdim } 273198396Srdivacky}; 274198396Srdivacky 275198396Srdivacky 276198396Srdivackytemplate<typename DenseMapT, typename KeyT> 277198396Srdivackyclass ValueMapIterator : 278198396Srdivacky public std::iterator<std::forward_iterator_tag, 279198396Srdivacky std::pair<KeyT, typename DenseMapT::mapped_type>, 280198396Srdivacky ptrdiff_t> { 281198396Srdivacky typedef typename DenseMapT::iterator BaseT; 282198396Srdivacky typedef typename DenseMapT::mapped_type ValueT; 283198396Srdivacky BaseT I; 284198396Srdivackypublic: 285198396Srdivacky ValueMapIterator() : I() {} 286198396Srdivacky 287198396Srdivacky ValueMapIterator(BaseT I) : I(I) {} 288198396Srdivacky 289198396Srdivacky BaseT base() const { return I; } 290198396Srdivacky 291198396Srdivacky struct ValueTypeProxy { 292198396Srdivacky const KeyT first; 293198396Srdivacky ValueT& second; 294198892Srdivacky ValueTypeProxy *operator->() { return this; } 295198396Srdivacky operator std::pair<KeyT, ValueT>() const { 296198396Srdivacky return std::make_pair(first, second); 297198396Srdivacky } 298198396Srdivacky }; 299198396Srdivacky 300198396Srdivacky ValueTypeProxy operator*() const { 301198396Srdivacky ValueTypeProxy Result = {I->first.Unwrap(), I->second}; 302198396Srdivacky return Result; 303198396Srdivacky } 304198396Srdivacky 305198396Srdivacky ValueTypeProxy operator->() const { 306198396Srdivacky return operator*(); 307198396Srdivacky } 308198396Srdivacky 309198396Srdivacky bool operator==(const ValueMapIterator &RHS) const { 310198396Srdivacky return I == RHS.I; 311198396Srdivacky } 312198396Srdivacky bool operator!=(const ValueMapIterator &RHS) const { 313198396Srdivacky return I != RHS.I; 314198396Srdivacky } 315198396Srdivacky 316198892Srdivacky inline ValueMapIterator& operator++() { // Preincrement 317198396Srdivacky ++I; 318198396Srdivacky return *this; 319198396Srdivacky } 320198892Srdivacky ValueMapIterator operator++(int) { // Postincrement 321198396Srdivacky ValueMapIterator tmp = *this; ++*this; return tmp; 322198396Srdivacky } 323198396Srdivacky}; 324198396Srdivacky 325198396Srdivackytemplate<typename DenseMapT, typename KeyT> 326198396Srdivackyclass ValueMapConstIterator : 327198396Srdivacky public std::iterator<std::forward_iterator_tag, 328198396Srdivacky std::pair<KeyT, typename DenseMapT::mapped_type>, 329198396Srdivacky ptrdiff_t> { 330198396Srdivacky typedef typename DenseMapT::const_iterator BaseT; 331198396Srdivacky typedef typename DenseMapT::mapped_type ValueT; 332198396Srdivacky BaseT I; 333198396Srdivackypublic: 334198396Srdivacky ValueMapConstIterator() : I() {} 335198396Srdivacky ValueMapConstIterator(BaseT I) : I(I) {} 336198396Srdivacky ValueMapConstIterator(ValueMapIterator<DenseMapT, KeyT> Other) 337198396Srdivacky : I(Other.base()) {} 338198396Srdivacky 339198396Srdivacky BaseT base() const { return I; } 340198396Srdivacky 341198396Srdivacky struct ValueTypeProxy { 342198396Srdivacky const KeyT first; 343198396Srdivacky const ValueT& second; 344198892Srdivacky ValueTypeProxy *operator->() { return this; } 345198396Srdivacky operator std::pair<KeyT, ValueT>() const { 346198396Srdivacky return std::make_pair(first, second); 347198396Srdivacky } 348198396Srdivacky }; 349198396Srdivacky 350198396Srdivacky ValueTypeProxy operator*() const { 351198396Srdivacky ValueTypeProxy Result = {I->first.Unwrap(), I->second}; 352198396Srdivacky return Result; 353198396Srdivacky } 354198396Srdivacky 355198396Srdivacky ValueTypeProxy operator->() const { 356198396Srdivacky return operator*(); 357198396Srdivacky } 358198396Srdivacky 359198396Srdivacky bool operator==(const ValueMapConstIterator &RHS) const { 360198396Srdivacky return I == RHS.I; 361198396Srdivacky } 362198396Srdivacky bool operator!=(const ValueMapConstIterator &RHS) const { 363198396Srdivacky return I != RHS.I; 364198396Srdivacky } 365198396Srdivacky 366198892Srdivacky inline ValueMapConstIterator& operator++() { // Preincrement 367198396Srdivacky ++I; 368198396Srdivacky return *this; 369198396Srdivacky } 370198892Srdivacky ValueMapConstIterator operator++(int) { // Postincrement 371198396Srdivacky ValueMapConstIterator tmp = *this; ++*this; return tmp; 372198396Srdivacky } 373198396Srdivacky}; 374198396Srdivacky 375198396Srdivacky} // end namespace llvm 376198396Srdivacky 377198396Srdivacky#endif 378