diff options
Diffstat (limited to 'src/cfg/Relooper.h')
-rw-r--r-- | src/cfg/Relooper.h | 114 |
1 files changed, 1 insertions, 113 deletions
diff --git a/src/cfg/Relooper.h b/src/cfg/Relooper.h index 1821336ba..173f180c7 100644 --- a/src/cfg/Relooper.h +++ b/src/cfg/Relooper.h @@ -37,6 +37,7 @@ http://doi.acm.org/10.1145/2048147.2048224 #include <memory> #include <set> +#include "support/insert_ordered.h" #include "wasm-builder.h" #include "wasm.h" @@ -123,119 +124,6 @@ struct Branch { Render(RelooperBuilder& Builder, Block* Target, bool SetLabel); }; -// like std::set, except that begin() -> end() iterates in the -// order that elements were added to the set (not in the order -// of operator<(T, T)) -template<typename T> struct InsertOrderedSet { - std::map<T, typename std::list<T>::iterator> Map; - std::list<T> List; - - typedef typename std::list<T>::iterator iterator; - iterator begin() { return List.begin(); } - iterator end() { return List.end(); } - - void erase(const T& val) { - auto it = Map.find(val); - if (it != Map.end()) { - List.erase(it->second); - Map.erase(it); - } - } - - void erase(iterator position) { - Map.erase(*position); - List.erase(position); - } - - // cheating a bit, not returning the iterator - void insert(const T& val) { - auto it = Map.find(val); - if (it == Map.end()) { - List.push_back(val); - Map.insert(std::make_pair(val, --List.end())); - } - } - - size_t size() const { return Map.size(); } - bool empty() const { return Map.empty(); } - - void clear() { - Map.clear(); - List.clear(); - } - - size_t count(const T& val) const { return Map.count(val); } - - InsertOrderedSet() = default; - InsertOrderedSet(const InsertOrderedSet& other) { *this = other; } - InsertOrderedSet& operator=(const InsertOrderedSet& other) { - clear(); - for (auto i : other.List) { - insert(i); // inserting manually creates proper iterators - } - return *this; - } -}; - -// like std::map, except that begin() -> end() iterates in the -// order that elements were added to the map (not in the order -// of operator<(Key, Key)) -template<typename Key, typename T> struct InsertOrderedMap { - std::map<Key, typename std::list<std::pair<Key, T>>::iterator> Map; - std::list<std::pair<Key, T>> List; - - T& operator[](const Key& k) { - auto it = Map.find(k); - if (it == Map.end()) { - List.push_back(std::make_pair(k, T())); - auto e = --List.end(); - Map.insert(std::make_pair(k, e)); - return e->second; - } - return it->second->second; - } - - typedef typename std::list<std::pair<Key, T>>::iterator iterator; - iterator begin() { return List.begin(); } - iterator end() { return List.end(); } - - void erase(const Key& k) { - auto it = Map.find(k); - if (it != Map.end()) { - List.erase(it->second); - Map.erase(it); - } - } - - void erase(iterator position) { erase(position->first); } - - void clear() { - Map.clear(); - List.clear(); - } - - void swap(InsertOrderedMap<Key, T>& Other) { - Map.swap(Other.Map); - List.swap(Other.List); - } - - size_t size() const { return Map.size(); } - bool empty() const { return Map.empty(); } - size_t count(const Key& k) const { return Map.count(k); } - - InsertOrderedMap() = default; - InsertOrderedMap(InsertOrderedMap& other) { - abort(); // TODO, watch out for iterators - } - InsertOrderedMap& operator=(const InsertOrderedMap& other) { - abort(); // TODO, watch out for iterators - } - bool operator==(const InsertOrderedMap& other) { - return Map == other.Map && List == other.List; - } - bool operator!=(const InsertOrderedMap& other) { return !(*this == other); } -}; - typedef InsertOrderedSet<Block*> BlockSet; typedef InsertOrderedMap<Block*, Branch*> BlockBranchMap; |