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