1 //===- ValueMap.h - Safe map from Values to data ----------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ValueMap class. ValueMap maps Value* or any subclass 10 // to an arbitrary other type. It provides the DenseMap interface but updates 11 // itself to remain safe when keys are RAUWed or deleted. By default, when a 12 // key is RAUWed from V1 to V2, the old mapping V1->target is removed, and a new 13 // mapping V2->target is added. If V2 already existed, its old target is 14 // overwritten. When a key is deleted, its mapping is removed. 15 // 16 // You can override a ValueMap's Config parameter to control exactly what 17 // happens on RAUW and destruction and to get called back on each event. It's 18 // legal to call back into the ValueMap from a Config's callbacks. Config 19 // parameters should inherit from ValueMapConfig<KeyT> to get default 20 // implementations of all the methods ValueMap uses. See ValueMapConfig for 21 // documentation of the functions you can override. 22 // 23 //===----------------------------------------------------------------------===// 24 25 #ifndef LLVM_IR_VALUEMAP_H 26 #define LLVM_IR_VALUEMAP_H 27 28 #include "llvm/ADT/DenseMap.h" 29 #include "llvm/ADT/DenseMapInfo.h" 30 #include "llvm/IR/TrackingMDRef.h" 31 #include "llvm/IR/ValueHandle.h" 32 #include "llvm/Support/Casting.h" 33 #include "llvm/Support/Mutex.h" 34 #include <algorithm> 35 #include <cassert> 36 #include <cstddef> 37 #include <iterator> 38 #include <mutex> 39 #include <optional> 40 #include <type_traits> 41 #include <utility> 42 43 namespace llvm { 44 45 template<typename KeyT, typename ValueT, typename Config> 46 class ValueMapCallbackVH; 47 template<typename DenseMapT, typename KeyT> 48 class ValueMapIterator; 49 template<typename DenseMapT, typename KeyT> 50 class ValueMapConstIterator; 51 52 /// This class defines the default behavior for configurable aspects of 53 /// ValueMap<>. User Configs should inherit from this class to be as compatible 54 /// as possible with future versions of ValueMap. 55 template<typename KeyT, typename MutexT = sys::Mutex> 56 struct ValueMapConfig { 57 using mutex_type = MutexT; 58 59 /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's 60 /// false, the ValueMap will leave the original mapping in place. 61 enum { FollowRAUW = true }; 62 63 // All methods will be called with a first argument of type ExtraData. The 64 // default implementations in this class take a templated first argument so 65 // that users' subclasses can use any type they want without having to 66 // override all the defaults. 67 struct ExtraData {}; 68 69 template<typename ExtraDataT> onRAUWValueMapConfig70 static void onRAUW(const ExtraDataT & /*Data*/, KeyT /*Old*/, KeyT /*New*/) {} 71 template<typename ExtraDataT> onDeleteValueMapConfig72 static void onDelete(const ExtraDataT &/*Data*/, KeyT /*Old*/) {} 73 74 /// Returns a mutex that should be acquired around any changes to the map. 75 /// This is only acquired from the CallbackVH (and held around calls to onRAUW 76 /// and onDelete) and not inside other ValueMap methods. NULL means that no 77 /// mutex is necessary. 78 template<typename ExtraDataT> getMutexValueMapConfig79 static mutex_type *getMutex(const ExtraDataT &/*Data*/) { return nullptr; } 80 }; 81 82 /// See the file comment. 83 template<typename KeyT, typename ValueT, typename Config =ValueMapConfig<KeyT>> 84 class ValueMap { 85 friend class ValueMapCallbackVH<KeyT, ValueT, Config>; 86 87 using ValueMapCVH = ValueMapCallbackVH<KeyT, ValueT, Config>; 88 using MapT = DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH>>; 89 using MDMapT = DenseMap<const Metadata *, TrackingMDRef>; 90 /// Map {(InlinedAt, old atom number) -> new atom number}. 91 using DMAtomT = SmallDenseMap<std::pair<Metadata *, uint64_t>, uint64_t>; 92 using ExtraData = typename Config::ExtraData; 93 94 MapT Map; 95 std::optional<MDMapT> MDMap; 96 ExtraData Data; 97 98 public: 99 using key_type = KeyT; 100 using mapped_type = ValueT; 101 using value_type = std::pair<KeyT, ValueT>; 102 using size_type = unsigned; 103 104 explicit ValueMap(unsigned NumInitBuckets = 64) Map(NumInitBuckets)105 : Map(NumInitBuckets), Data() {} 106 explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64) Map(NumInitBuckets)107 : Map(NumInitBuckets), Data(Data) {} 108 // ValueMap can't be copied nor moved, because the callbacks store pointer to 109 // it. 110 ValueMap(const ValueMap &) = delete; 111 ValueMap(ValueMap &&) = delete; 112 ValueMap &operator=(const ValueMap &) = delete; 113 ValueMap &operator=(ValueMap &&) = delete; 114 hasMD()115 bool hasMD() const { return bool(MDMap); } MD()116 MDMapT &MD() { 117 if (!MDMap) 118 MDMap.emplace(); 119 return *MDMap; 120 } getMDMap()121 std::optional<MDMapT> &getMDMap() { return MDMap; } 122 /// Map {(InlinedAt, old atom number) -> new atom number}. 123 DMAtomT AtomMap; 124 125 /// Get the mapped metadata, if it's in the map. getMappedMD(const Metadata * MD)126 std::optional<Metadata *> getMappedMD(const Metadata *MD) const { 127 if (!MDMap) 128 return std::nullopt; 129 auto Where = MDMap->find(MD); 130 if (Where == MDMap->end()) 131 return std::nullopt; 132 return Where->second.get(); 133 } 134 135 using iterator = ValueMapIterator<MapT, KeyT>; 136 using const_iterator = ValueMapConstIterator<MapT, KeyT>; 137 begin()138 inline iterator begin() { return iterator(Map.begin()); } end()139 inline iterator end() { return iterator(Map.end()); } begin()140 inline const_iterator begin() const { return const_iterator(Map.begin()); } end()141 inline const_iterator end() const { return const_iterator(Map.end()); } 142 empty()143 bool empty() const { return Map.empty(); } size()144 size_type size() const { return Map.size(); } 145 146 /// Grow the map so that it has at least Size buckets. Does not shrink reserve(size_t Size)147 void reserve(size_t Size) { Map.reserve(Size); } 148 clear()149 void clear() { 150 Map.clear(); 151 MDMap.reset(); 152 AtomMap.clear(); 153 } 154 155 /// Return 1 if the specified key is in the map, 0 otherwise. count(const KeyT & Val)156 size_type count(const KeyT &Val) const { 157 return Map.find_as(Val) == Map.end() ? 0 : 1; 158 } 159 find(const KeyT & Val)160 iterator find(const KeyT &Val) { 161 return iterator(Map.find_as(Val)); 162 } find(const KeyT & Val)163 const_iterator find(const KeyT &Val) const { 164 return const_iterator(Map.find_as(Val)); 165 } 166 167 /// lookup - Return the entry for the specified key, or a default 168 /// constructed value if no such entry exists. lookup(const KeyT & Val)169 ValueT lookup(const KeyT &Val) const { 170 typename MapT::const_iterator I = Map.find_as(Val); 171 return I != Map.end() ? I->second : ValueT(); 172 } 173 174 // Inserts key,value pair into the map if the key isn't already in the map. 175 // If the key is already in the map, it returns false and doesn't update the 176 // value. insert(const std::pair<KeyT,ValueT> & KV)177 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) { 178 auto MapResult = Map.insert(std::make_pair(Wrap(KV.first), KV.second)); 179 return std::make_pair(iterator(MapResult.first), MapResult.second); 180 } 181 insert(std::pair<KeyT,ValueT> && KV)182 std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) { 183 auto MapResult = 184 Map.insert(std::make_pair(Wrap(KV.first), std::move(KV.second))); 185 return std::make_pair(iterator(MapResult.first), MapResult.second); 186 } 187 188 /// insert - Range insertion of pairs. 189 template<typename InputIt> insert(InputIt I,InputIt E)190 void insert(InputIt I, InputIt E) { 191 for (; I != E; ++I) 192 insert(*I); 193 } 194 erase(const KeyT & Val)195 bool erase(const KeyT &Val) { 196 typename MapT::iterator I = Map.find_as(Val); 197 if (I == Map.end()) 198 return false; 199 200 Map.erase(I); 201 return true; 202 } erase(iterator I)203 void erase(iterator I) { 204 return Map.erase(I.base()); 205 } 206 FindAndConstruct(const KeyT & Key)207 value_type& FindAndConstruct(const KeyT &Key) { 208 return Map.FindAndConstruct(Wrap(Key)); 209 } 210 211 ValueT &operator[](const KeyT &Key) { 212 return Map[Wrap(Key)]; 213 } 214 215 /// isPointerIntoBucketsArray - Return true if the specified pointer points 216 /// somewhere into the ValueMap's array of buckets (i.e. either to a key or 217 /// value in the ValueMap). isPointerIntoBucketsArray(const void * Ptr)218 bool isPointerIntoBucketsArray(const void *Ptr) const { 219 return Map.isPointerIntoBucketsArray(Ptr); 220 } 221 222 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets 223 /// array. In conjunction with the previous method, this can be used to 224 /// determine whether an insertion caused the ValueMap to reallocate. getPointerIntoBucketsArray()225 const void *getPointerIntoBucketsArray() const { 226 return Map.getPointerIntoBucketsArray(); 227 } 228 229 private: 230 // Takes a key being looked up in the map and wraps it into a 231 // ValueMapCallbackVH, the actual key type of the map. We use a helper 232 // function because ValueMapCVH is constructed with a second parameter. Wrap(KeyT key)233 ValueMapCVH Wrap(KeyT key) const { 234 // The only way the resulting CallbackVH could try to modify *this (making 235 // the const_cast incorrect) is if it gets inserted into the map. But then 236 // this function must have been called from a non-const method, making the 237 // const_cast ok. 238 return ValueMapCVH(key, const_cast<ValueMap*>(this)); 239 } 240 }; 241 242 // This CallbackVH updates its ValueMap when the contained Value changes, 243 // according to the user's preferences expressed through the Config object. 244 template <typename KeyT, typename ValueT, typename Config> 245 class ValueMapCallbackVH final : public CallbackVH { 246 friend class ValueMap<KeyT, ValueT, Config>; 247 friend struct DenseMapInfo<ValueMapCallbackVH>; 248 249 using ValueMapT = ValueMap<KeyT, ValueT, Config>; 250 using KeySansPointerT = std::remove_pointer_t<KeyT>; 251 252 ValueMapT *Map; 253 254 ValueMapCallbackVH(KeyT Key, ValueMapT *Map) 255 : CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))), 256 Map(Map) {} 257 258 // Private constructor used to create empty/tombstone DenseMap keys. 259 ValueMapCallbackVH(Value *V) : CallbackVH(V), Map(nullptr) {} 260 261 public: 262 KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); } 263 264 void deleted() override { 265 // Make a copy that won't get changed even when *this is destroyed. 266 ValueMapCallbackVH Copy(*this); 267 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data); 268 std::unique_lock<typename Config::mutex_type> Guard; 269 if (M) 270 Guard = std::unique_lock<typename Config::mutex_type>(*M); 271 Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. 272 Copy.Map->Map.erase(Copy); // Definitely destroys *this. 273 } 274 275 void allUsesReplacedWith(Value *new_key) override { 276 assert(isa<KeySansPointerT>(new_key) && 277 "Invalid RAUW on key of ValueMap<>"); 278 // Make a copy that won't get changed even when *this is destroyed. 279 ValueMapCallbackVH Copy(*this); 280 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data); 281 std::unique_lock<typename Config::mutex_type> Guard; 282 if (M) 283 Guard = std::unique_lock<typename Config::mutex_type>(*M); 284 285 KeyT typed_new_key = cast<KeySansPointerT>(new_key); 286 // Can destroy *this: 287 Config::onRAUW(Copy.Map->Data, Copy.Unwrap(), typed_new_key); 288 if (Config::FollowRAUW) { 289 typename ValueMapT::MapT::iterator I = Copy.Map->Map.find(Copy); 290 // I could == Copy.Map->Map.end() if the onRAUW callback already 291 // removed the old mapping. 292 if (I != Copy.Map->Map.end()) { 293 ValueT Target(std::move(I->second)); 294 Copy.Map->Map.erase(I); // Definitely destroys *this. 295 Copy.Map->insert(std::make_pair(typed_new_key, std::move(Target))); 296 } 297 } 298 } 299 }; 300 301 template<typename KeyT, typename ValueT, typename Config> 302 struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config>> { 303 using VH = ValueMapCallbackVH<KeyT, ValueT, Config>; 304 305 static inline VH getEmptyKey() { 306 return VH(DenseMapInfo<Value *>::getEmptyKey()); 307 } 308 309 static inline VH getTombstoneKey() { 310 return VH(DenseMapInfo<Value *>::getTombstoneKey()); 311 } 312 313 static unsigned getHashValue(const VH &Val) { 314 return DenseMapInfo<KeyT>::getHashValue(Val.Unwrap()); 315 } 316 317 static unsigned getHashValue(const KeyT &Val) { 318 return DenseMapInfo<KeyT>::getHashValue(Val); 319 } 320 321 static bool isEqual(const VH &LHS, const VH &RHS) { 322 return LHS == RHS; 323 } 324 325 static bool isEqual(const KeyT &LHS, const VH &RHS) { 326 return LHS == RHS.getValPtr(); 327 } 328 }; 329 330 template <typename DenseMapT, typename KeyT> class ValueMapIterator { 331 using BaseT = typename DenseMapT::iterator; 332 using ValueT = typename DenseMapT::mapped_type; 333 334 BaseT I; 335 336 public: 337 using iterator_category = std::forward_iterator_tag; 338 using value_type = std::pair<KeyT, typename DenseMapT::mapped_type>; 339 using difference_type = std::ptrdiff_t; 340 using pointer = value_type *; 341 using reference = value_type &; 342 343 ValueMapIterator() : I() {} 344 ValueMapIterator(BaseT I) : I(I) {} 345 346 BaseT base() const { return I; } 347 348 struct ValueTypeProxy { 349 const KeyT first; 350 ValueT& second; 351 352 ValueTypeProxy *operator->() { return this; } 353 354 operator std::pair<KeyT, ValueT>() const { 355 return std::make_pair(first, second); 356 } 357 }; 358 359 ValueTypeProxy operator*() const { 360 ValueTypeProxy Result = {I->first.Unwrap(), I->second}; 361 return Result; 362 } 363 364 ValueTypeProxy operator->() const { 365 return operator*(); 366 } 367 368 bool operator==(const ValueMapIterator &RHS) const { 369 return I == RHS.I; 370 } 371 bool operator!=(const ValueMapIterator &RHS) const { 372 return I != RHS.I; 373 } 374 375 inline ValueMapIterator& operator++() { // Preincrement 376 ++I; 377 return *this; 378 } 379 ValueMapIterator operator++(int) { // Postincrement 380 ValueMapIterator tmp = *this; ++*this; return tmp; 381 } 382 }; 383 384 template <typename DenseMapT, typename KeyT> class ValueMapConstIterator { 385 using BaseT = typename DenseMapT::const_iterator; 386 using ValueT = typename DenseMapT::mapped_type; 387 388 BaseT I; 389 390 public: 391 using iterator_category = std::forward_iterator_tag; 392 using value_type = std::pair<KeyT, typename DenseMapT::mapped_type>; 393 using difference_type = std::ptrdiff_t; 394 using pointer = value_type *; 395 using reference = value_type &; 396 397 ValueMapConstIterator() : I() {} 398 ValueMapConstIterator(BaseT I) : I(I) {} 399 ValueMapConstIterator(ValueMapIterator<DenseMapT, KeyT> Other) 400 : I(Other.base()) {} 401 402 BaseT base() const { return I; } 403 404 struct ValueTypeProxy { 405 const KeyT first; 406 const ValueT& second; 407 ValueTypeProxy *operator->() { return this; } 408 operator std::pair<KeyT, ValueT>() const { 409 return std::make_pair(first, second); 410 } 411 }; 412 413 ValueTypeProxy operator*() const { 414 ValueTypeProxy Result = {I->first.Unwrap(), I->second}; 415 return Result; 416 } 417 418 ValueTypeProxy operator->() const { 419 return operator*(); 420 } 421 422 bool operator==(const ValueMapConstIterator &RHS) const { 423 return I == RHS.I; 424 } 425 bool operator!=(const ValueMapConstIterator &RHS) const { 426 return I != RHS.I; 427 } 428 429 inline ValueMapConstIterator& operator++() { // Preincrement 430 ++I; 431 return *this; 432 } 433 ValueMapConstIterator operator++(int) { // Postincrement 434 ValueMapConstIterator tmp = *this; ++*this; return tmp; 435 } 436 }; 437 438 } // end namespace llvm 439 440 #endif // LLVM_IR_VALUEMAP_H 441