2008-06-09 00:42:10 +00:00
|
|
|
//===- ScopedHashTable.h - A simple scoped hash table ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements an efficient scoped hash table, which is useful for
|
|
|
|
// things like dominator-based optimizations. This allows clients to do things
|
|
|
|
// like this:
|
|
|
|
//
|
|
|
|
// ScopedHashTable<int, int> HT;
|
|
|
|
// {
|
|
|
|
// ScopedHashTableScope<int, int> Scope1(HT);
|
|
|
|
// HT.insert(0, 0);
|
|
|
|
// HT.insert(1, 1);
|
|
|
|
// {
|
|
|
|
// ScopedHashTableScope<int, int> Scope2(HT);
|
|
|
|
// HT.insert(0, 42);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Looking up the value for "0" in the Scope2 block will return 42. Looking
|
|
|
|
// up the value for 0 before 42 is inserted or after Scope2 is popped will
|
|
|
|
// return 0.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_SCOPEDHASHTABLE_H
|
|
|
|
#define LLVM_ADT_SCOPEDHASHTABLE_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2011-01-03 01:29:37 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2008-06-09 00:42:10 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2011-01-03 01:29:37 +00:00
|
|
|
template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
|
|
|
|
typename AllocatorTy = MallocAllocator>
|
2008-06-09 00:42:10 +00:00
|
|
|
class ScopedHashTable;
|
|
|
|
|
2011-01-03 01:38:29 +00:00
|
|
|
template <typename K, typename V>
|
2008-06-09 00:42:10 +00:00
|
|
|
class ScopedHashTableVal {
|
|
|
|
ScopedHashTableVal *NextInScope;
|
|
|
|
ScopedHashTableVal *NextForKey;
|
|
|
|
K Key;
|
|
|
|
V Val;
|
2011-01-03 01:29:37 +00:00
|
|
|
ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
|
2008-06-09 00:42:10 +00:00
|
|
|
public:
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
const K &getKey() const { return Key; }
|
|
|
|
const V &getValue() const { return Val; }
|
|
|
|
V &getValue() { return Val; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
ScopedHashTableVal *getNextForKey() { return NextForKey; }
|
|
|
|
const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
|
|
|
|
ScopedHashTableVal *getNextInScope() { return NextInScope; }
|
2011-01-03 01:29:37 +00:00
|
|
|
|
|
|
|
template <typename AllocatorTy>
|
|
|
|
static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
|
|
|
|
ScopedHashTableVal *nextForKey,
|
|
|
|
const K &key, const V &val,
|
|
|
|
AllocatorTy &Allocator) {
|
|
|
|
ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
|
|
|
|
// Set up the value.
|
|
|
|
new (New) ScopedHashTableVal(key, val);
|
|
|
|
New->NextInScope = nextInScope;
|
|
|
|
New->NextForKey = nextForKey;
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AllocatorTy>
|
|
|
|
void Destroy(AllocatorTy &Allocator) {
|
|
|
|
// Free memory referenced by the item.
|
|
|
|
this->~ScopedHashTableVal();
|
|
|
|
Allocator.Deallocate(this);
|
|
|
|
}
|
2008-06-09 00:42:10 +00:00
|
|
|
};
|
|
|
|
|
2011-01-03 01:38:29 +00:00
|
|
|
template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
|
|
|
|
typename AllocatorTy = MallocAllocator>
|
2008-06-09 00:42:10 +00:00
|
|
|
class ScopedHashTableScope {
|
|
|
|
/// HT - The hashtable that we are active for.
|
2011-01-03 01:38:29 +00:00
|
|
|
ScopedHashTable<K, V, KInfo, AllocatorTy> &HT;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
/// PrevScope - This is the scope that we are shadowing in HT.
|
|
|
|
ScopedHashTableScope *PrevScope;
|
|
|
|
|
|
|
|
/// LastValInScope - This is the last value that was inserted for this scope
|
|
|
|
/// or null if none have been inserted yet.
|
2011-01-03 01:38:29 +00:00
|
|
|
ScopedHashTableVal<K, V> *LastValInScope;
|
2008-06-09 00:42:10 +00:00
|
|
|
void operator=(ScopedHashTableScope&); // DO NOT IMPLEMENT
|
|
|
|
ScopedHashTableScope(ScopedHashTableScope&); // DO NOT IMPLEMENT
|
|
|
|
public:
|
2011-01-03 01:38:29 +00:00
|
|
|
ScopedHashTableScope(ScopedHashTable<K, V, KInfo, AllocatorTy> &HT);
|
2008-06-09 00:42:10 +00:00
|
|
|
~ScopedHashTableScope();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2011-02-27 22:51:57 +00:00
|
|
|
ScopedHashTableScope *getParentScope() { return PrevScope; }
|
|
|
|
const ScopedHashTableScope *getParentScope() const { return PrevScope; }
|
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
private:
|
2011-01-03 01:38:29 +00:00
|
|
|
friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
|
|
|
|
ScopedHashTableVal<K, V> *getLastValInScope() {
|
2010-03-02 22:10:24 +00:00
|
|
|
return LastValInScope;
|
|
|
|
}
|
2011-01-03 01:38:29 +00:00
|
|
|
void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
|
2010-03-02 22:10:24 +00:00
|
|
|
LastValInScope = Val;
|
|
|
|
}
|
2008-06-09 00:42:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-03-02 22:10:24 +00:00
|
|
|
template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
|
2008-06-09 00:42:10 +00:00
|
|
|
class ScopedHashTableIterator {
|
2011-01-03 01:38:29 +00:00
|
|
|
ScopedHashTableVal<K, V> *Node;
|
2008-06-09 00:42:10 +00:00
|
|
|
public:
|
2011-01-03 01:38:29 +00:00
|
|
|
ScopedHashTableIterator(ScopedHashTableVal<K, V> *node) : Node(node) {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
V &operator*() const {
|
|
|
|
assert(Node && "Dereference end()");
|
|
|
|
return Node->getValue();
|
|
|
|
}
|
|
|
|
V *operator->() const {
|
|
|
|
return &Node->getValue();
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
bool operator==(const ScopedHashTableIterator &RHS) const {
|
|
|
|
return Node == RHS.Node;
|
|
|
|
}
|
|
|
|
bool operator!=(const ScopedHashTableIterator &RHS) const {
|
|
|
|
return Node != RHS.Node;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
inline ScopedHashTableIterator& operator++() { // Preincrement
|
|
|
|
assert(Node && "incrementing past end()");
|
|
|
|
Node = Node->getNextForKey();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ScopedHashTableIterator operator++(int) { // Postincrement
|
|
|
|
ScopedHashTableIterator tmp = *this; ++*this; return tmp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-03 01:29:37 +00:00
|
|
|
template <typename K, typename V, typename KInfo, typename AllocatorTy>
|
2008-06-09 00:42:10 +00:00
|
|
|
class ScopedHashTable {
|
2011-02-27 22:51:57 +00:00
|
|
|
public:
|
|
|
|
/// ScopeTy - This is a helpful typedef that allows clients to get easy access
|
|
|
|
/// to the name of the scope for this hash table.
|
|
|
|
typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
|
|
|
|
private:
|
2011-01-03 01:38:29 +00:00
|
|
|
typedef ScopedHashTableVal<K, V> ValTy;
|
2011-01-03 01:29:37 +00:00
|
|
|
DenseMap<K, ValTy*, KInfo> TopLevelMap;
|
2011-02-27 22:51:57 +00:00
|
|
|
ScopeTy *CurScope;
|
2011-01-03 01:29:37 +00:00
|
|
|
|
|
|
|
AllocatorTy Allocator;
|
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
|
|
|
|
void operator=(const ScopedHashTable&); // NOT YET IMPLEMENTED
|
2011-01-03 01:38:29 +00:00
|
|
|
friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
|
2008-06-09 00:42:10 +00:00
|
|
|
public:
|
|
|
|
ScopedHashTable() : CurScope(0) {}
|
2011-01-03 01:29:37 +00:00
|
|
|
ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
|
2008-06-09 00:42:10 +00:00
|
|
|
~ScopedHashTable() {
|
|
|
|
assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
|
|
|
|
}
|
2011-01-03 01:29:37 +00:00
|
|
|
|
2011-01-03 03:16:20 +00:00
|
|
|
|
|
|
|
/// Access to the allocator.
|
2011-01-03 01:29:37 +00:00
|
|
|
typedef typename ReferenceAdder<AllocatorTy>::result AllocatorRefTy;
|
|
|
|
typedef typename ReferenceAdder<const AllocatorTy>::result AllocatorCRefTy;
|
|
|
|
AllocatorRefTy getAllocator() { return Allocator; }
|
|
|
|
AllocatorCRefTy getAllocator() const { return Allocator; }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2010-03-02 02:37:33 +00:00
|
|
|
bool count(const K &Key) const {
|
|
|
|
return TopLevelMap.count(Key);
|
|
|
|
}
|
|
|
|
|
|
|
|
V lookup(const K &Key) {
|
2011-01-03 01:29:37 +00:00
|
|
|
typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
|
2010-07-27 18:59:50 +00:00
|
|
|
if (I != TopLevelMap.end())
|
|
|
|
return I->second->getValue();
|
|
|
|
|
|
|
|
return V();
|
2010-03-02 02:37:33 +00:00
|
|
|
}
|
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
void insert(const K &Key, const V &Val) {
|
2011-02-27 22:51:57 +00:00
|
|
|
insertIntoScope(CurScope, Key, Val);
|
2008-06-09 00:42:10 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2010-03-02 22:10:24 +00:00
|
|
|
typedef ScopedHashTableIterator<K, V, KInfo> iterator;
|
2008-06-09 00:42:10 +00:00
|
|
|
|
|
|
|
iterator end() { return iterator(0); }
|
|
|
|
|
|
|
|
iterator begin(const K &Key) {
|
2011-01-03 01:29:37 +00:00
|
|
|
typename DenseMap<K, ValTy*, KInfo>::iterator I =
|
2008-06-09 00:42:10 +00:00
|
|
|
TopLevelMap.find(Key);
|
|
|
|
if (I == TopLevelMap.end()) return end();
|
|
|
|
return iterator(I->second);
|
|
|
|
}
|
2011-02-27 22:51:57 +00:00
|
|
|
|
|
|
|
ScopeTy *getCurScope() { return CurScope; }
|
|
|
|
const ScopeTy *getCurScope() const { return CurScope; }
|
|
|
|
|
|
|
|
/// insertIntoScope - This inserts the specified key/value at the specified
|
|
|
|
/// (possibly not the current) scope. While it is ok to insert into a scope
|
|
|
|
/// that isn't the current one, it isn't ok to insert *underneath* an existing
|
|
|
|
/// value of the specified key.
|
|
|
|
void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
|
|
|
|
assert(S && "No scope active!");
|
|
|
|
ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
|
|
|
|
KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
|
|
|
|
Allocator);
|
|
|
|
S->setLastValInScope(KeyEntry);
|
|
|
|
}
|
2008-06-09 00:42:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// ScopedHashTableScope ctor - Install this as the current scope for the hash
|
|
|
|
/// table.
|
2011-01-03 01:38:29 +00:00
|
|
|
template <typename K, typename V, typename KInfo, typename Allocator>
|
|
|
|
ScopedHashTableScope<K, V, KInfo, Allocator>::
|
|
|
|
ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
|
2008-06-09 00:42:10 +00:00
|
|
|
PrevScope = HT.CurScope;
|
|
|
|
HT.CurScope = this;
|
|
|
|
LastValInScope = 0;
|
|
|
|
}
|
|
|
|
|
2011-01-03 01:38:29 +00:00
|
|
|
template <typename K, typename V, typename KInfo, typename Allocator>
|
|
|
|
ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
|
2008-06-09 00:42:10 +00:00
|
|
|
assert(HT.CurScope == this && "Scope imbalance!");
|
|
|
|
HT.CurScope = PrevScope;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
// Pop and delete all values corresponding to this scope.
|
2011-01-03 01:38:29 +00:00
|
|
|
while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
|
2008-06-09 00:42:10 +00:00
|
|
|
// Pop this value out of the TopLevelMap.
|
|
|
|
if (ThisEntry->getNextForKey() == 0) {
|
|
|
|
assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
|
|
|
|
"Scope imbalance!");
|
|
|
|
HT.TopLevelMap.erase(ThisEntry->getKey());
|
2009-01-09 19:25:42 +00:00
|
|
|
} else {
|
2011-01-03 01:38:29 +00:00
|
|
|
ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
|
2008-06-09 00:42:10 +00:00
|
|
|
assert(KeyEntry == ThisEntry && "Scope imbalance!");
|
|
|
|
KeyEntry = ThisEntry->getNextForKey();
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
// Pop this value out of the scope.
|
|
|
|
LastValInScope = ThisEntry->getNextInScope();
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-06-09 00:42:10 +00:00
|
|
|
// Delete this entry.
|
2011-01-03 01:29:37 +00:00
|
|
|
ThisEntry->Destroy(HT.getAllocator());
|
2008-06-09 00:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|