Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
12 KiB

  1. //===- llvm/ADT/ValueMap.h - Safe map from Values to data -------*- 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 the ValueMap class. ValueMap maps Value* or any subclass
  11. // to an arbitrary other type. It provides the DenseMap interface but updates
  12. // itself to remain safe when keys are RAUWed or deleted. By default, when a
  13. // key is RAUWed from V1 to V2, the old mapping V1->target is removed, and a new
  14. // mapping V2->target is added. If V2 already existed, its old target is
  15. // overwritten. When a key is deleted, its mapping is removed.
  16. //
  17. // You can override a ValueMap's Config parameter to control exactly what
  18. // happens on RAUW and destruction and to get called back on each event. It's
  19. // legal to call back into the ValueMap from a Config's callbacks. Config
  20. // parameters should inherit from ValueMapConfig<KeyT> to get default
  21. // implementations of all the methods ValueMap uses. See ValueMapConfig for
  22. // documentation of the functions you can override.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_ADT_VALUEMAP_H
  26. #define LLVM_ADT_VALUEMAP_H
  27. #include "llvm/ADT/DenseMap.h"
  28. #include "llvm/Support/Mutex.h"
  29. #include "llvm/Support/ValueHandle.h"
  30. #include "llvm/Support/type_traits.h"
  31. #include <iterator>
  32. namespace llvm {
  33. template<typename KeyT, typename ValueT, typename Config>
  34. class ValueMapCallbackVH;
  35. template<typename DenseMapT, typename KeyT>
  36. class ValueMapIterator;
  37. template<typename DenseMapT, typename KeyT>
  38. class ValueMapConstIterator;
  39. /// This class defines the default behavior for configurable aspects of
  40. /// ValueMap<>. User Configs should inherit from this class to be as compatible
  41. /// as possible with future versions of ValueMap.
  42. template<typename KeyT>
  43. struct ValueMapConfig {
  44. /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's
  45. /// false, the ValueMap will leave the original mapping in place.
  46. enum { FollowRAUW = true };
  47. // All methods will be called with a first argument of type ExtraData. The
  48. // default implementations in this class take a templated first argument so
  49. // that users' subclasses can use any type they want without having to
  50. // override all the defaults.
  51. struct ExtraData {};
  52. template<typename ExtraDataT>
  53. static void onRAUW(const ExtraDataT & /*Data*/, KeyT /*Old*/, KeyT /*New*/) {}
  54. template<typename ExtraDataT>
  55. static void onDelete(const ExtraDataT &/*Data*/, KeyT /*Old*/) {}
  56. /// Returns a mutex that should be acquired around any changes to the map.
  57. /// This is only acquired from the CallbackVH (and held around calls to onRAUW
  58. /// and onDelete) and not inside other ValueMap methods. NULL means that no
  59. /// mutex is necessary.
  60. template<typename ExtraDataT>
  61. static sys::Mutex *getMutex(const ExtraDataT &/*Data*/) { return NULL; }
  62. };
  63. /// See the file comment.
  64. template<typename KeyT, typename ValueT, typename Config =ValueMapConfig<KeyT> >
  65. class ValueMap {
  66. friend class ValueMapCallbackVH<KeyT, ValueT, Config>;
  67. typedef ValueMapCallbackVH<KeyT, ValueT, Config> ValueMapCVH;
  68. typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH> > MapT;
  69. typedef typename Config::ExtraData ExtraData;
  70. MapT Map;
  71. ExtraData Data;
  72. ValueMap(const ValueMap&) LLVM_DELETED_FUNCTION;
  73. ValueMap& operator=(const ValueMap&) LLVM_DELETED_FUNCTION;
  74. public:
  75. typedef KeyT key_type;
  76. typedef ValueT mapped_type;
  77. typedef std::pair<KeyT, ValueT> value_type;
  78. explicit ValueMap(unsigned NumInitBuckets = 64)
  79. : Map(NumInitBuckets), Data() {}
  80. explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64)
  81. : Map(NumInitBuckets), Data(Data) {}
  82. ~ValueMap() {}
  83. typedef ValueMapIterator<MapT, KeyT> iterator;
  84. typedef ValueMapConstIterator<MapT, KeyT> const_iterator;
  85. inline iterator begin() { return iterator(Map.begin()); }
  86. inline iterator end() { return iterator(Map.end()); }
  87. inline const_iterator begin() const { return const_iterator(Map.begin()); }
  88. inline const_iterator end() const { return const_iterator(Map.end()); }
  89. bool empty() const { return Map.empty(); }
  90. unsigned size() const { return Map.size(); }
  91. /// Grow the map so that it has at least Size buckets. Does not shrink
  92. void resize(size_t Size) { Map.resize(Size); }
  93. void clear() { Map.clear(); }
  94. /// count - Return true if the specified key is in the map.
  95. bool count(const KeyT &Val) const {
  96. return Map.find_as(Val) != Map.end();
  97. }
  98. iterator find(const KeyT &Val) {
  99. return iterator(Map.find_as(Val));
  100. }
  101. const_iterator find(const KeyT &Val) const {
  102. return const_iterator(Map.find_as(Val));
  103. }
  104. /// lookup - Return the entry for the specified key, or a default
  105. /// constructed value if no such entry exists.
  106. ValueT lookup(const KeyT &Val) const {
  107. typename MapT::const_iterator I = Map.find_as(Val);
  108. return I != Map.end() ? I->second : ValueT();
  109. }
  110. // Inserts key,value pair into the map if the key isn't already in the map.
  111. // If the key is already in the map, it returns false and doesn't update the
  112. // value.
  113. std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
  114. std::pair<typename MapT::iterator, bool> map_result=
  115. Map.insert(std::make_pair(Wrap(KV.first), KV.second));
  116. return std::make_pair(iterator(map_result.first), map_result.second);
  117. }
  118. /// insert - Range insertion of pairs.
  119. template<typename InputIt>
  120. void insert(InputIt I, InputIt E) {
  121. for (; I != E; ++I)
  122. insert(*I);
  123. }
  124. bool erase(const KeyT &Val) {
  125. typename MapT::iterator I = Map.find_as(Val);
  126. if (I == Map.end())
  127. return false;
  128. Map.erase(I);
  129. return true;
  130. }
  131. void erase(iterator I) {
  132. return Map.erase(I.base());
  133. }
  134. value_type& FindAndConstruct(const KeyT &Key) {
  135. return Map.FindAndConstruct(Wrap(Key));
  136. }
  137. ValueT &operator[](const KeyT &Key) {
  138. return Map[Wrap(Key)];
  139. }
  140. /// isPointerIntoBucketsArray - Return true if the specified pointer points
  141. /// somewhere into the ValueMap's array of buckets (i.e. either to a key or
  142. /// value in the ValueMap).
  143. bool isPointerIntoBucketsArray(const void *Ptr) const {
  144. return Map.isPointerIntoBucketsArray(Ptr);
  145. }
  146. /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
  147. /// array. In conjunction with the previous method, this can be used to
  148. /// determine whether an insertion caused the ValueMap to reallocate.
  149. const void *getPointerIntoBucketsArray() const {
  150. return Map.getPointerIntoBucketsArray();
  151. }
  152. private:
  153. // Takes a key being looked up in the map and wraps it into a
  154. // ValueMapCallbackVH, the actual key type of the map. We use a helper
  155. // function because ValueMapCVH is constructed with a second parameter.
  156. ValueMapCVH Wrap(KeyT key) const {
  157. // The only way the resulting CallbackVH could try to modify *this (making
  158. // the const_cast incorrect) is if it gets inserted into the map. But then
  159. // this function must have been called from a non-const method, making the
  160. // const_cast ok.
  161. return ValueMapCVH(key, const_cast<ValueMap*>(this));
  162. }
  163. };
  164. // This CallbackVH updates its ValueMap when the contained Value changes,
  165. // according to the user's preferences expressed through the Config object.
  166. template<typename KeyT, typename ValueT, typename Config>
  167. class ValueMapCallbackVH : public CallbackVH {
  168. friend class ValueMap<KeyT, ValueT, Config>;
  169. friend struct DenseMapInfo<ValueMapCallbackVH>;
  170. typedef ValueMap<KeyT, ValueT, Config> ValueMapT;
  171. typedef typename llvm::remove_pointer<KeyT>::type KeySansPointerT;
  172. ValueMapT *Map;
  173. ValueMapCallbackVH(KeyT Key, ValueMapT *Map)
  174. : CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))),
  175. Map(Map) {}
  176. public:
  177. KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); }
  178. virtual void deleted() {
  179. // Make a copy that won't get changed even when *this is destroyed.
  180. ValueMapCallbackVH Copy(*this);
  181. sys::Mutex *M = Config::getMutex(Copy.Map->Data);
  182. if (M)
  183. M->acquire();
  184. Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
  185. Copy.Map->Map.erase(Copy); // Definitely destroys *this.
  186. if (M)
  187. M->release();
  188. }
  189. virtual void allUsesReplacedWith(Value *new_key) {
  190. assert(isa<KeySansPointerT>(new_key) &&
  191. "Invalid RAUW on key of ValueMap<>");
  192. // Make a copy that won't get changed even when *this is destroyed.
  193. ValueMapCallbackVH Copy(*this);
  194. sys::Mutex *M = Config::getMutex(Copy.Map->Data);
  195. if (M)
  196. M->acquire();
  197. KeyT typed_new_key = cast<KeySansPointerT>(new_key);
  198. // Can destroy *this:
  199. Config::onRAUW(Copy.Map->Data, Copy.Unwrap(), typed_new_key);
  200. if (Config::FollowRAUW) {
  201. typename ValueMapT::MapT::iterator I = Copy.Map->Map.find(Copy);
  202. // I could == Copy.Map->Map.end() if the onRAUW callback already
  203. // removed the old mapping.
  204. if (I != Copy.Map->Map.end()) {
  205. ValueT Target(I->second);
  206. Copy.Map->Map.erase(I); // Definitely destroys *this.
  207. Copy.Map->insert(std::make_pair(typed_new_key, Target));
  208. }
  209. }
  210. if (M)
  211. M->release();
  212. }
  213. };
  214. template<typename KeyT, typename ValueT, typename Config>
  215. struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config> > {
  216. typedef ValueMapCallbackVH<KeyT, ValueT, Config> VH;
  217. typedef DenseMapInfo<KeyT> PointerInfo;
  218. static inline VH getEmptyKey() {
  219. return VH(PointerInfo::getEmptyKey(), NULL);
  220. }
  221. static inline VH getTombstoneKey() {
  222. return VH(PointerInfo::getTombstoneKey(), NULL);
  223. }
  224. static unsigned getHashValue(const VH &Val) {
  225. return PointerInfo::getHashValue(Val.Unwrap());
  226. }
  227. static unsigned getHashValue(const KeyT &Val) {
  228. return PointerInfo::getHashValue(Val);
  229. }
  230. static bool isEqual(const VH &LHS, const VH &RHS) {
  231. return LHS == RHS;
  232. }
  233. static bool isEqual(const KeyT &LHS, const VH &RHS) {
  234. return LHS == RHS.getValPtr();
  235. }
  236. };
  237. template<typename DenseMapT, typename KeyT>
  238. class ValueMapIterator :
  239. public std::iterator<std::forward_iterator_tag,
  240. std::pair<KeyT, typename DenseMapT::mapped_type>,
  241. ptrdiff_t> {
  242. typedef typename DenseMapT::iterator BaseT;
  243. typedef typename DenseMapT::mapped_type ValueT;
  244. BaseT I;
  245. public:
  246. ValueMapIterator() : I() {}
  247. ValueMapIterator(BaseT I) : I(I) {}
  248. BaseT base() const { return I; }
  249. struct ValueTypeProxy {
  250. const KeyT first;
  251. ValueT& second;
  252. ValueTypeProxy *operator->() { return this; }
  253. operator std::pair<KeyT, ValueT>() const {
  254. return std::make_pair(first, second);
  255. }
  256. };
  257. ValueTypeProxy operator*() const {
  258. ValueTypeProxy Result = {I->first.Unwrap(), I->second};
  259. return Result;
  260. }
  261. ValueTypeProxy operator->() const {
  262. return operator*();
  263. }
  264. bool operator==(const ValueMapIterator &RHS) const {
  265. return I == RHS.I;
  266. }
  267. bool operator!=(const ValueMapIterator &RHS) const {
  268. return I != RHS.I;
  269. }
  270. inline ValueMapIterator& operator++() { // Preincrement
  271. ++I;
  272. return *this;
  273. }
  274. ValueMapIterator operator++(int) { // Postincrement
  275. ValueMapIterator tmp = *this; ++*this; return tmp;
  276. }
  277. };
  278. template<typename DenseMapT, typename KeyT>
  279. class ValueMapConstIterator :
  280. public std::iterator<std::forward_iterator_tag,
  281. std::pair<KeyT, typename DenseMapT::mapped_type>,
  282. ptrdiff_t> {
  283. typedef typename DenseMapT::const_iterator BaseT;
  284. typedef typename DenseMapT::mapped_type ValueT;
  285. BaseT I;
  286. public:
  287. ValueMapConstIterator() : I() {}
  288. ValueMapConstIterator(BaseT I) : I(I) {}
  289. ValueMapConstIterator(ValueMapIterator<DenseMapT, KeyT> Other)
  290. : I(Other.base()) {}
  291. BaseT base() const { return I; }
  292. struct ValueTypeProxy {
  293. const KeyT first;
  294. const ValueT& second;
  295. ValueTypeProxy *operator->() { return this; }
  296. operator std::pair<KeyT, ValueT>() const {
  297. return std::make_pair(first, second);
  298. }
  299. };
  300. ValueTypeProxy operator*() const {
  301. ValueTypeProxy Result = {I->first.Unwrap(), I->second};
  302. return Result;
  303. }
  304. ValueTypeProxy operator->() const {
  305. return operator*();
  306. }
  307. bool operator==(const ValueMapConstIterator &RHS) const {
  308. return I == RHS.I;
  309. }
  310. bool operator!=(const ValueMapConstIterator &RHS) const {
  311. return I != RHS.I;
  312. }
  313. inline ValueMapConstIterator& operator++() { // Preincrement
  314. ++I;
  315. return *this;
  316. }
  317. ValueMapConstIterator operator++(int) { // Postincrement
  318. ValueMapConstIterator tmp = *this; ++*this; return tmp;
  319. }
  320. };
  321. } // end namespace llvm
  322. #endif