@@ -20,24 +20,65 @@ namespace pir {
2020class Block ;
2121class Operation ;
2222
23+ namespace detail {
24+ template <typename T, typename ... OthersT>
25+ struct ExactlyOneIrType {
26+ using type = void ;
27+ };
28+ template <typename T, typename FirstT, typename ... OthersT>
29+ struct ExactlyOneIrType <T, FirstT, OthersT...> {
30+ using type =
31+ std::conditional_t <std::is_convertible<T, FirstT>::value,
32+ FirstT,
33+ typename ExactlyOneIrType<T, OthersT...>::type>;
34+ };
35+ } // namespace detail
2336class IrMapping {
2437 public:
2538 template <typename T>
26- void Add (T from, T to) {
39+ using IrType =
40+ typename detail::ExactlyOneIrType<T, Value, Block*, Operation*>::type;
41+ template <typename T>
42+ std::unordered_map<T, T>& GetMap () {
43+ if constexpr (std::is_same<T, Value>::value) {
44+ return value_map_;
45+ } else if constexpr (std::is_same<T, Block*>::value) {
46+ return block_map_;
47+ } else if constexpr (std::is_same<T, Operation*>::value) {
48+ return operation_map_;
49+ } else {
50+ IR_THROW (" Not support type in IRMapping." );
51+ }
52+ }
53+ template <typename T>
54+ const std::unordered_map<T, T>& GetMap () const {
55+ if constexpr (std::is_same<T, Value>::value) {
56+ return value_map_;
57+ } else if constexpr (std::is_same<T, Block*>::value) {
58+ return block_map_;
59+ } else if constexpr (std::is_same<T, Operation*>::value) {
60+ return operation_map_;
61+ } else {
62+ IR_THROW (" Not support type in IRMapping." );
63+ }
64+ }
65+ template <typename T, typename S>
66+ void Add (T from, S to) {
2767 if (!from) return ;
28- MutableMap<T >()[from] = to;
68+ GetMap<IrType<T> >()[from] = to;
2969 }
3070
3171 template <typename T>
3272 T Lookup (T from) const {
3373 if (!from) return static_cast <T>(nullptr );
34- IR_ENFORCE (Map<T>().count (from) > 0 , " Not found key in IRMapping." );
35- return Map<T>().at (from);
74+ IR_ENFORCE (GetMap<IrType<T>>().count (from) > 0 ,
75+ " Not found key in IRMapping." );
76+ return GetMap<IrType<T>>().at (from);
3677 }
3778
3879 template <typename T>
3980 void Earse (T from) {
40- MutableMap<T >().erase (from);
81+ GetMap<IrType<T> >().erase (from);
4182 }
4283
4384 void Clear () {
@@ -46,37 +87,10 @@ class IrMapping {
4687 operation_map_.clear ();
4788 }
4889
49- template <typename T>
50- using MapType = std::unordered_map<T, T>;
51-
52- template <typename T>
53- const MapType<T> &Map () const {
54- if constexpr (std::is_convertible<T, Value>::value)
55- return value_map_;
56- else if constexpr (std::is_convertible<T, Block *>::value)
57- return block_map_;
58- else if constexpr (std::is_convertible<T, Operation *>::value)
59- return operation_map_;
60- else
61- IR_THROW (" Not support type in IRMapping." );
62- }
63-
64- template <typename T>
65- MapType<T> &MutableMap () {
66- if constexpr (std::is_convertible<T, Value>::value)
67- return value_map_;
68- else if constexpr (std::is_convertible<T, Block *>::value)
69- return block_map_;
70- else if constexpr (std::is_convertible<T, Operation *>::value)
71- return operation_map_;
72- else
73- IR_THROW (" Not support type in IRMapping." );
74- }
75-
7690 private:
77- MapType< Value> value_map_;
78- MapType <Block *> block_map_;
79- MapType <Operation *> operation_map_;
91+ std::unordered_map<Value, Value> value_map_;
92+ std::unordered_map <Block*, Block *> block_map_;
93+ std::unordered_map <Operation*, Operation *> operation_map_;
8094};
8195
8296} // namespace pir
0 commit comments