26 #ifndef LLVM_IR_VALUEMAP_H
27 #define LLVM_IR_VALUEMAP_H
42 #include <type_traits>
47 template<
typename KeyT,
typename ValueT,
typename Config>
49 template<
typename DenseMapT,
typename KeyT>
51 template<
typename DenseMapT,
typename KeyT>
57 template<
typename KeyT,
typename MutexT = sys::Mutex>
71 template<
typename ExtraDataT>
72 static void onRAUW(
const ExtraDataT & , KeyT , KeyT ) {}
73 template<
typename ExtraDataT>
74 static void onDelete(
const ExtraDataT &, KeyT ) {}
80 template<
typename ExtraDataT>
85 template<
typename KeyT,
typename ValueT,
typename Config =ValueMapConfig<KeyT>>
92 using ExtraData =
typename Config::ExtraData;
97 bool MayMapMetadata =
true;
106 : Map(NumInitBuckets),
Data() {}
108 : Map(NumInitBuckets), Data(Data) {}
128 auto Where = MDMap->find(MD);
129 if (Where == MDMap->end())
131 return Where->second.get();
142 bool empty()
const {
return Map.empty(); }
155 return Map.find_as(Val) == Map.end() ? 0 : 1;
168 typename MapT::const_iterator
I = Map.find_as(Val);
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>
194 typename MapT::iterator
I = Map.find_as(Val);
202 return Map.erase(I.
base());
206 return Map.FindAndConstruct(Wrap(Key));
210 return Map[Wrap(Key)];
217 return Map.isPointerIntoBucketsArray(Ptr);
224 return Map.getPointerIntoBucketsArray();
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 using KeySansPointerT =
typename std::remove_pointer<KeyT>::type;
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 using BaseT =
typename DenseMapT::iterator;
334 using ValueT =
typename DenseMapT::mapped_type;
342 BaseT
base()
const {
return I; }
350 operator std::pair<KeyT, ValueT>()
const {
380 template<
typename DenseMapT,
typename KeyT>
381 class ValueMapConstIterator :
382 public std::iterator<std::forward_iterator_tag,
383 std::pair<KeyT, typename DenseMapT::mapped_type>,
385 using BaseT =
typename DenseMapT::const_iterator;
386 using ValueT =
typename DenseMapT::mapped_type;
396 BaseT
base()
const {
return I; }
402 operator std::pair<KeyT, ValueT>()
const {
434 #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<>.
ValueMap(unsigned NumInitBuckets=64)
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
static unsigned getHashValue(const VH &Val)
ValueTypeProxy operator->() const
ValueMapConstIterator(ValueMapIterator< DenseMapT, KeyT > Other)
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
ValueMapConstIterator< MapT, KeyT > const_iterator
iterator find(const KeyT &Val)
value_type & FindAndConstruct(const KeyT &Key)
void allUsesReplacedWith(Value *new_key) override
Callback for Value RAUW.
ValueMap(const ExtraData &Data, unsigned NumInitBuckets=64)
Value * getValPtr() const
ValueMapIterator & operator++()
std::pair< GlobalValue *, uint64_t > value_type
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->()
ValueMapConstIterator operator++(int)
ValueMapIterator operator++(int)
bool mayMapMetadata() const
ValueMapIterator< MapT, KeyT > iterator
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()
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static bool isEqual(const KeyT &LHS, const VH &RHS)
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