LLVM API Documentation

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