Deleted Added
full compact
ValueMap.h (198396) ValueMap.h (198892)
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//
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.
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.
11//
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//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_VALUEMAP_H
15#define LLVM_ADT_VALUEMAP_H
16
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/Support/ValueHandle.h"
19#include "llvm/Support/type_traits.h"

--- 6 unchanged lines hidden (view full) ---

26template<typename KeyT, typename ValueT, typename Config, typename ValueInfoT>
27class ValueMapCallbackVH;
28
29template<typename DenseMapT, typename KeyT>
30class ValueMapIterator;
31template<typename DenseMapT, typename KeyT>
32class ValueMapConstIterator;
33
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/ValueHandle.h"
31#include "llvm/Support/type_traits.h"

--- 6 unchanged lines hidden (view full) ---

38template<typename KeyT, typename ValueT, typename Config, typename ValueInfoT>
39class ValueMapCallbackVH;
40
41template<typename DenseMapT, typename KeyT>
42class ValueMapIterator;
43template<typename DenseMapT, typename KeyT>
44class ValueMapConstIterator;
45
46/// This class defines the default behavior for configurable aspects of
47/// ValueMap<>. User Configs should inherit from this class to be as compatible
48/// as possible with future versions of ValueMap.
34template<typename KeyT>
35struct ValueMapConfig {
36 /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's
37 /// false, the ValueMap will leave the original mapping in place.
38 enum { FollowRAUW = true };
39
40 // All methods will be called with a first argument of type ExtraData. The
41 // default implementations in this class take a templated first argument so
42 // that users' subclasses can use any type they want without having to
43 // override all the defaults.
44 struct ExtraData {};
45
46 template<typename ExtraDataT>
47 static void onRAUW(const ExtraDataT &Data, KeyT Old, KeyT New) {}
48 template<typename ExtraDataT>
49template<typename KeyT>
50struct ValueMapConfig {
51 /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's
52 /// false, the ValueMap will leave the original mapping in place.
53 enum { FollowRAUW = true };
54
55 // All methods will be called with a first argument of type ExtraData. The
56 // default implementations in this class take a templated first argument so
57 // that users' subclasses can use any type they want without having to
58 // override all the defaults.
59 struct ExtraData {};
60
61 template<typename ExtraDataT>
62 static void onRAUW(const ExtraDataT &Data, KeyT Old, KeyT New) {}
63 template<typename ExtraDataT>
49 static void onDeleted(const ExtraDataT &Data, KeyT Old) {}
64 static void onDelete(const ExtraDataT &Data, KeyT Old) {}
50
51 /// Returns a mutex that should be acquired around any changes to the map.
52 /// This is only acquired from the CallbackVH (and held around calls to onRAUW
65
66 /// Returns a mutex that should be acquired around any changes to the map.
67 /// This is only acquired from the CallbackVH (and held around calls to onRAUW
53 /// and onDeleted) and not inside other ValueMap methods. NULL means that no
68 /// and onDelete) and not inside other ValueMap methods. NULL means that no
54 /// mutex is necessary.
55 template<typename ExtraDataT>
56 static sys::Mutex *getMutex(const ExtraDataT &Data) { return NULL; }
57};
58
69 /// mutex is necessary.
70 template<typename ExtraDataT>
71 static sys::Mutex *getMutex(const ExtraDataT &Data) { return NULL; }
72};
73
59/// ValueMap maps Value* or any subclass to an arbitrary other
60/// type. It provides the DenseMap interface. When the key values are
61/// deleted or RAUWed, ValueMap relies on the Config to decide what to
62/// do. Config parameters should inherit from ValueMapConfig<KeyT> to
63/// get default implementations of all the methods ValueMap uses.
64///
65/// By default, when a key is RAUWed from V1 to V2, the old mapping
66/// V1->target is removed, and a new mapping V2->target is added. If
67/// V2 already existed, its old target is overwritten. When a key is
68/// deleted, its mapping is removed. You can override Config to get
69/// called back on each event.
74/// See the file comment.
70template<typename KeyT, typename ValueT, typename Config = ValueMapConfig<KeyT>,
71 typename ValueInfoT = DenseMapInfo<ValueT> >
72class ValueMap {
73 friend class ValueMapCallbackVH<KeyT, ValueT, Config, ValueInfoT>;
74 typedef ValueMapCallbackVH<KeyT, ValueT, Config, ValueInfoT> ValueMapCVH;
75 typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH>,
76 ValueInfoT> MapT;
77 typedef typename Config::ExtraData ExtraData;

--- 94 unchanged lines hidden (view full) ---

172 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
173 /// array. In conjunction with the previous method, this can be used to
174 /// determine whether an insertion caused the ValueMap to reallocate.
175 const void *getPointerIntoBucketsArray() const {
176 return Map.getPointerIntoBucketsArray();
177 }
178
179private:
75template<typename KeyT, typename ValueT, typename Config = ValueMapConfig<KeyT>,
76 typename ValueInfoT = DenseMapInfo<ValueT> >
77class ValueMap {
78 friend class ValueMapCallbackVH<KeyT, ValueT, Config, ValueInfoT>;
79 typedef ValueMapCallbackVH<KeyT, ValueT, Config, ValueInfoT> ValueMapCVH;
80 typedef DenseMap<ValueMapCVH, ValueT, DenseMapInfo<ValueMapCVH>,
81 ValueInfoT> MapT;
82 typedef typename Config::ExtraData ExtraData;

--- 94 unchanged lines hidden (view full) ---

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.
180 ValueMapCVH Wrap(KeyT key) const {
181 // The only way the resulting CallbackVH could try to modify *this (making
182 // the const_cast incorrect) is if it gets inserted into the map. But then
183 // this function must have been called from a non-const method, making the
184 // const_cast ok.
185 return ValueMapCVH(key, const_cast<ValueMap*>(this));
186 }
187};
188
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.
189template<typename KeyT, typename ValueT, typename Config, typename ValueInfoT>
190class ValueMapCallbackVH : public CallbackVH {
191 friend class ValueMap<KeyT, ValueT, Config, ValueInfoT>;
199template<typename KeyT, typename ValueT, typename Config, typename ValueInfoT>
200class ValueMapCallbackVH : public CallbackVH {
201 friend class ValueMap<KeyT, ValueT, Config, ValueInfoT>;
192 friend class DenseMapInfo<ValueMapCallbackVH>;
202 friend struct DenseMapInfo<ValueMapCallbackVH>;
193 typedef ValueMap<KeyT, ValueT, Config, ValueInfoT> ValueMapT;
194 typedef typename llvm::remove_pointer<KeyT>::type KeySansPointerT;
195
196 ValueMapT *Map;
197
198 ValueMapCallbackVH(KeyT Key, ValueMapT *Map)
199 : CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))),
200 Map(Map) {}
201
202public:
203 KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); }
204
205 virtual void deleted() {
206 // Make a copy that won't get changed even when *this is destroyed.
207 ValueMapCallbackVH Copy(*this);
208 sys::Mutex *M = Config::getMutex(Copy.Map->Data);
209 if (M)
210 M->acquire();
203 typedef ValueMap<KeyT, ValueT, Config, ValueInfoT> ValueMapT;
204 typedef typename llvm::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 virtual void deleted() {
216 // Make a copy that won't get changed even when *this is destroyed.
217 ValueMapCallbackVH Copy(*this);
218 sys::Mutex *M = Config::getMutex(Copy.Map->Data);
219 if (M)
220 M->acquire();
211 Config::onDeleted(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
221 Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
212 Copy.Map->Map.erase(Copy); // Definitely destroys *this.
213 if (M)
214 M->release();
215 }
216 virtual void allUsesReplacedWith(Value *new_key) {
217 assert(isa<KeySansPointerT>(new_key) &&
218 "Invalid RAUW on key of ValueMap<>");
219 // Make a copy that won't get changed even when *this is destroyed.

--- 54 unchanged lines hidden (view full) ---

274
275 ValueMapIterator(BaseT I) : I(I) {}
276
277 BaseT base() const { return I; }
278
279 struct ValueTypeProxy {
280 const KeyT first;
281 ValueT& second;
222 Copy.Map->Map.erase(Copy); // Definitely destroys *this.
223 if (M)
224 M->release();
225 }
226 virtual void allUsesReplacedWith(Value *new_key) {
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.

--- 54 unchanged lines hidden (view full) ---

284
285 ValueMapIterator(BaseT I) : I(I) {}
286
287 BaseT base() const { return I; }
288
289 struct ValueTypeProxy {
290 const KeyT first;
291 ValueT& second;
282 ValueTypeProxy *operator->() { return this; }
292 ValueTypeProxy *operator->() { return this; }
283 operator std::pair<KeyT, ValueT>() const {
284 return std::make_pair(first, second);
285 }
286 };
287
288 ValueTypeProxy operator*() const {
289 ValueTypeProxy Result = {I->first.Unwrap(), I->second};
290 return Result;

--- 5 unchanged lines hidden (view full) ---

296
297 bool operator==(const ValueMapIterator &RHS) const {
298 return I == RHS.I;
299 }
300 bool operator!=(const ValueMapIterator &RHS) const {
301 return I != RHS.I;
302 }
303
293 operator std::pair<KeyT, ValueT>() const {
294 return std::make_pair(first, second);
295 }
296 };
297
298 ValueTypeProxy operator*() const {
299 ValueTypeProxy Result = {I->first.Unwrap(), I->second};
300 return Result;

--- 5 unchanged lines hidden (view full) ---

306
307 bool operator==(const ValueMapIterator &RHS) const {
308 return I == RHS.I;
309 }
310 bool operator!=(const ValueMapIterator &RHS) const {
311 return I != RHS.I;
312 }
313
304 inline ValueMapIterator& operator++() { // Preincrement
314 inline ValueMapIterator& operator++() { // Preincrement
305 ++I;
306 return *this;
307 }
315 ++I;
316 return *this;
317 }
308 ValueMapIterator operator++(int) { // Postincrement
318 ValueMapIterator operator++(int) { // Postincrement
309 ValueMapIterator tmp = *this; ++*this; return tmp;
310 }
311};
312
313template<typename DenseMapT, typename KeyT>
314class ValueMapConstIterator :
315 public std::iterator<std::forward_iterator_tag,
316 std::pair<KeyT, typename DenseMapT::mapped_type>,

--- 7 unchanged lines hidden (view full) ---

324 ValueMapConstIterator(ValueMapIterator<DenseMapT, KeyT> Other)
325 : I(Other.base()) {}
326
327 BaseT base() const { return I; }
328
329 struct ValueTypeProxy {
330 const KeyT first;
331 const ValueT& second;
319 ValueMapIterator tmp = *this; ++*this; return tmp;
320 }
321};
322
323template<typename DenseMapT, typename KeyT>
324class ValueMapConstIterator :
325 public std::iterator<std::forward_iterator_tag,
326 std::pair<KeyT, typename DenseMapT::mapped_type>,

--- 7 unchanged lines hidden (view full) ---

334 ValueMapConstIterator(ValueMapIterator<DenseMapT, KeyT> Other)
335 : I(Other.base()) {}
336
337 BaseT base() const { return I; }
338
339 struct ValueTypeProxy {
340 const KeyT first;
341 const ValueT& second;
332 ValueTypeProxy *operator->() { return this; }
342 ValueTypeProxy *operator->() { return this; }
333 operator std::pair<KeyT, ValueT>() const {
334 return std::make_pair(first, second);
335 }
336 };
337
338 ValueTypeProxy operator*() const {
339 ValueTypeProxy Result = {I->first.Unwrap(), I->second};
340 return Result;

--- 5 unchanged lines hidden (view full) ---

346
347 bool operator==(const ValueMapConstIterator &RHS) const {
348 return I == RHS.I;
349 }
350 bool operator!=(const ValueMapConstIterator &RHS) const {
351 return I != RHS.I;
352 }
353
343 operator std::pair<KeyT, ValueT>() const {
344 return std::make_pair(first, second);
345 }
346 };
347
348 ValueTypeProxy operator*() const {
349 ValueTypeProxy Result = {I->first.Unwrap(), I->second};
350 return Result;

--- 5 unchanged lines hidden (view full) ---

356
357 bool operator==(const ValueMapConstIterator &RHS) const {
358 return I == RHS.I;
359 }
360 bool operator!=(const ValueMapConstIterator &RHS) const {
361 return I != RHS.I;
362 }
363
354 inline ValueMapConstIterator& operator++() { // Preincrement
364 inline ValueMapConstIterator& operator++() { // Preincrement
355 ++I;
356 return *this;
357 }
365 ++I;
366 return *this;
367 }
358 ValueMapConstIterator operator++(int) { // Postincrement
368 ValueMapConstIterator operator++(int) { // Postincrement
359 ValueMapConstIterator tmp = *this; ++*this; return tmp;
360 }
361};
362
363} // end namespace llvm
364
365#endif
369 ValueMapConstIterator tmp = *this; ++*this; return tmp;
370 }
371};
372
373} // end namespace llvm
374
375#endif