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