26 #ifndef LLVM_IR_VALUEMAP_H
27 #define LLVM_IR_VALUEMAP_H
42 #include <type_traits>
47 template<
typename KeyT,
typename ValueT,
typename Config>
50 template<
typename DenseMapT,
typename KeyT>
52 template<
typename DenseMapT,
typename KeyT>
58 template<
typename KeyT,
typename MutexT = sys::Mutex>
72 template<
typename ExtraDataT>
73 static void onRAUW(
const ExtraDataT & , KeyT , KeyT ) {}
74 template<
typename ExtraDataT>
75 static void onDelete(
const ExtraDataT &, KeyT ) {}
81 template<
typename ExtraDataT>
86 template<
typename KeyT,
typename ValueT,
typename Config =ValueMapConfig<KeyT>>
93 typedef typename Config::ExtraData ExtraData;
98 bool MayMapMetadata =
true;
107 : Map(NumInitBuckets), Data() {}
108 explicit ValueMap(
const ExtraData &Data,
unsigned NumInitBuckets = 64)
109 : Map(NumInitBuckets), Data(Data) {}
129 auto Where = MDMap->find(MD);
130 if (Where == MDMap->end())
132 return Where->second.get();
146 void resize(
size_t Size) { Map.resize(Size); }
169 return I != Map.
end() ? I->second :
ValueT();
175 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
176 auto MapResult = Map.
insert(std::make_pair(Wrap(KV.first), KV.second));
177 return std::make_pair(
iterator(MapResult.first), MapResult.second);
180 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
182 Map.
insert(std::make_pair(Wrap(KV.first), std::move(KV.second)));
183 return std::make_pair(
iterator(MapResult.first), MapResult.second);
187 template<
typename InputIt>
210 return Map[Wrap(Key)];
231 ValueMapCVH Wrap(KeyT key)
const {
236 return ValueMapCVH(key, const_cast<ValueMap*>(
this));
242 template <
typename KeyT,
typename ValueT,
typename Config>
243 class ValueMapCallbackVH final :
public CallbackVH {
248 typedef typename std::remove_pointer<KeyT>::type KeySansPointerT;
265 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
269 Config::onDelete(Copy.Map->Data, Copy.Unwrap());
270 Copy.Map->Map.erase(Copy);
274 assert(isa<KeySansPointerT>(new_key) &&
275 "Invalid RAUW on key of ValueMap<>");
278 typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
283 KeyT typed_new_key = cast<KeySansPointerT>(new_key);
285 Config::onRAUW(Copy.Map->Data, Copy.Unwrap(), typed_new_key);
286 if (Config::FollowRAUW) {
290 if (I != Copy.Map->Map.end()) {
292 Copy.Map->Map.erase(I);
293 Copy.Map->insert(std::make_pair(typed_new_key, std::move(Target)));
299 template<
typename KeyT,
typename ValueT,
typename Config>
328 template<
typename DenseMapT,
typename KeyT>
329 class ValueMapIterator :
330 public std::iterator<std::forward_iterator_tag,
331 std::pair<KeyT, typename DenseMapT::mapped_type>,
333 typedef typename DenseMapT::iterator BaseT;
334 typedef typename DenseMapT::mapped_type
ValueT;
342 BaseT
base()
const {
return I; }
348 operator std::pair<KeyT, ValueT>()
const {
378 template<
typename DenseMapT,
typename KeyT>
379 class ValueMapConstIterator :
380 public std::iterator<std::forward_iterator_tag,
381 std::pair<KeyT, typename DenseMapT::mapped_type>,
383 typedef typename DenseMapT::const_iterator BaseT;
384 typedef typename DenseMapT::mapped_type
ValueT;
394 BaseT
base()
const {
return I; }
400 operator std::pair<KeyT, ValueT>()
const {
432 #endif // LLVM_IR_VALUEMAP_H
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.
Optional< Metadata * > getMappedMD(const Metadata *MD) const
Get the mapped metadata, if it's in the map.
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
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
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
Optional< MDMapT > & getMDMap()
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.
void disableMapMetadata()
ValueTypeProxy * operator->()
ValueMapCallbackVH< KeyT, ValueT, Config > VH
ValueMapConstIterator operator++(int)
ValueMapIterator operator++(int)
bool mayMapMetadata() const
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)
static const char * Target
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.
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)
LLVM_NODISCARD bool empty() const
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive, key type.
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the ValueMap's...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
bool operator==(const ValueMapIterator &RHS) const
ValueMap & operator=(const ValueMap &)=delete
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