26 #ifndef LLVM_IR_VALUEMAP_H
27 #define LLVM_IR_VALUEMAP_H
40 template<
typename KeyT,
typename ValueT,
typename Config>
43 template<
typename DenseMapT,
typename KeyT>
45 template<
typename DenseMapT,
typename KeyT>
51 template<
typename KeyT,
typename MutexT = sys::Mutex>
65 template<
typename ExtraDataT>
66 static void onRAUW(
const ExtraDataT & , KeyT , KeyT ) {}
67 template<
typename ExtraDataT>
68 static void onDelete(
const ExtraDataT &, KeyT ) {}
74 template<
typename ExtraDataT>
79 template<
typename KeyT,
typename ValueT,
typename Config =ValueMapConfig<KeyT> >
85 typedef typename Config::ExtraData ExtraData;
87 std::unique_ptr<MDMapT> MDMap;
97 explicit ValueMap(
unsigned NumInitBuckets = 64)
98 : Map(NumInitBuckets), Data() {}
99 explicit ValueMap(
const ExtraData &Data,
unsigned NumInitBuckets = 64)
100 : Map(NumInitBuckets), Data(Data) {}
102 bool hasMD()
const {
return MDMap; }
143 return I != Map.
end() ? I->second : ValueT();
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);
154 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
156 Map.
insert(std::make_pair(Wrap(KV.first), std::move(KV.second)));
157 return std::make_pair(
iterator(MapResult.first), MapResult.second);
161 template<
typename InputIt>
185 return Map[Wrap(Key)];
206 ValueMapCVH Wrap(KeyT key)
const {
211 return ValueMapCVH(key, const_cast<ValueMap*>(
this));
217 template<
typename KeyT,
typename ValueT,
typename Config>
218 class ValueMapCallbackVH :
public CallbackVH {
222 typedef typename std::remove_pointer<KeyT>::type KeySansPointerT;
239 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
243 Config::onDelete(Copy.Map->Data, Copy.Unwrap());
244 Copy.Map->Map.erase(Copy);
247 assert(isa<KeySansPointerT>(new_key) &&
248 "Invalid RAUW on key of ValueMap<>");
251 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
256 KeyT typed_new_key = cast<KeySansPointerT>(new_key);
258 Config::onRAUW(Copy.Map->Data, Copy.Unwrap(), typed_new_key);
259 if (Config::FollowRAUW) {
263 if (I != Copy.Map->Map.end()) {
265 Copy.Map->Map.erase(I);
266 Copy.Map->insert(std::make_pair(typed_new_key, std::move(Target)));
272 template<
typename KeyT,
typename ValueT,
typename Config>
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>,
302 typedef typename DenseMapT::iterator BaseT;
303 typedef typename DenseMapT::mapped_type ValueT;
310 BaseT
base()
const {
return I; }
316 operator std::pair<KeyT, ValueT>()
const {
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>,
351 typedef typename DenseMapT::const_iterator BaseT;
352 typedef typename DenseMapT::mapped_type ValueT;
360 BaseT
base()
const {
return I; }
366 operator std::pair<KeyT, ValueT>()
const {
ValueTypeProxy operator->() const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
const_iterator begin() const
ValueTypeProxy * operator->()
static void onDelete(const ExtraDataT &, KeyT)
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
This class defines the default behavior for configurable aspects of ValueMap<>.
std::pair< KeyT, ValueT > value_type
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
ValueMap(unsigned NumInitBuckets=64)
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
static unsigned getHashValue(const VH &Val)
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
ValueTypeProxy operator->() const
ValueMapConstIterator(ValueMapIterator< DenseMapT, KeyT > Other)
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
iterator find(const KeyT &Val)
value_type & FindAndConstruct(const KeyT &Key)
void allUsesReplacedWith(Value *new_key) override
Callback for Value RAUW.
ValueMapConstIterator< MapT, KeyT > const_iterator
value_type & FindAndConstruct(const KeyT &Key)
ValueMap(const ExtraData &Data, unsigned NumInitBuckets=64)
bool erase(const KeyT &Val)
Value * getValPtr() const
ValueMapIterator & operator++()
bool operator==(const ValueMapConstIterator &RHS) const
static bool isEqual(const VH &LHS, const VH &RHS)
A pared-down imitation of std::unique_lock from C++11.
bool operator!=(const ValueMapIterator &RHS) const
static mutex_type * getMutex(const ExtraDataT &)
Returns a mutex that should be acquired around any changes to the map.
ValueTypeProxy * operator->()
ValueMapCallbackVH< KeyT, ValueT, Config > VH
ValueMapConstIterator operator++(int)
ValueMapIterator operator++(int)
void resize(size_type Size)
Grow the densemap so that it has at least Size buckets. Does not shrink.
const_iterator end() const
void insert(InputIt I, InputIt E)
insert - Range insertion of pairs.
static void onRAUW(const ExtraDataT &, KeyT, KeyT)
ValueMapIterator(BaseT I)
ValueTypeProxy operator*() const
ValueMapConstIterator(BaseT I)
void resize(size_t Size)
Grow the map so that it has at least Size buckets. Does not shrink.
Target - Wrapper for Target specific information.
bool operator!=(const ValueMapConstIterator &RHS) const
const_iterator find(const KeyT &Val) const
static VH getTombstoneKey()
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
ValueMapIterator< MapT, KeyT > iterator
static bool isEqual(const KeyT &LHS, const VH &RHS)
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive, key type.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the ValueMap's...
LLVM Value Representation.
bool operator==(const ValueMapIterator &RHS) const
ValueMapConstIterator & operator++()
Value handle with callbacks on RAUW and destruction.
static unsigned getHashValue(const KeyT &Val)
ValueT & operator[](const KeyT &Key)
void deleted() override
Callback for Value destruction.
bool erase(const KeyT &Val)
ValueTypeProxy operator*() const