/home/arjun/llvm-project/mlir/include/mlir/IR/BlockAndValueMapping.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- BlockAndValueMapping.h -----------------------------------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines a utility class for maintaining a mapping for multiple |
10 | | // value types. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #ifndef MLIR_IR_BLOCKANDVALUEMAPPING_H |
15 | | #define MLIR_IR_BLOCKANDVALUEMAPPING_H |
16 | | |
17 | | #include "mlir/IR/Block.h" |
18 | | |
19 | | namespace mlir { |
20 | | // This is a utility class for mapping one set of values to another. New |
21 | | // mappings can be inserted via 'map'. Existing mappings can be |
22 | | // found via the 'lookup*' functions. There are two variants that differ only in |
23 | | // return value when an existing is not found for the provided key. |
24 | | // 'lookupOrNull' returns nullptr where as 'lookupOrDefault' will return the |
25 | | // lookup key. |
26 | | class BlockAndValueMapping { |
27 | | public: |
28 | | /// Inserts a new mapping for 'from' to 'to'. If there is an existing mapping, |
29 | | /// it is overwritten. |
30 | 0 | void map(Block *from, Block *to) { valueMap[from] = to; } |
31 | 0 | void map(Value from, Value to) { |
32 | 0 | valueMap[from.getAsOpaquePointer()] = to.getAsOpaquePointer(); |
33 | 0 | } |
34 | | |
35 | | template < |
36 | | typename S, typename T, |
37 | | std::enable_if_t<!std::is_assignable<Value, S>::value && |
38 | | !std::is_assignable<Block *, S>::value> * = nullptr> |
39 | | void map(S &&from, T &&to) { |
40 | | for (auto pair : llvm::zip(from, to)) |
41 | | map(std::get<0>(pair), std::get<1>(pair)); |
42 | | } |
43 | | |
44 | | /// Erases a mapping for 'from'. |
45 | 0 | void erase(Block *from) { valueMap.erase(from); } |
46 | 0 | void erase(Value from) { valueMap.erase(from.getAsOpaquePointer()); } |
47 | | |
48 | | /// Checks to see if a mapping for 'from' exists. |
49 | 0 | bool contains(Block *from) const { return valueMap.count(from); } |
50 | 0 | bool contains(Value from) const { |
51 | 0 | return valueMap.count(from.getAsOpaquePointer()); |
52 | 0 | } |
53 | | |
54 | | /// Lookup a mapped value within the map. If a mapping for the provided value |
55 | | /// does not exist then return nullptr. |
56 | 0 | Block *lookupOrNull(Block *from) const { |
57 | 0 | return lookupOrValue(from, (Block *)nullptr); |
58 | 0 | } |
59 | 0 | Value lookupOrNull(Value from) const { return lookupOrValue(from, Value()); } |
60 | | |
61 | | /// Lookup a mapped value within the map. If a mapping for the provided value |
62 | | /// does not exist then return the provided value. |
63 | 0 | Block *lookupOrDefault(Block *from) const { |
64 | 0 | return lookupOrValue(from, from); |
65 | 0 | } |
66 | 0 | Value lookupOrDefault(Value from) const { return lookupOrValue(from, from); } |
67 | | |
68 | | /// Lookup a mapped value within the map. This asserts the provided value |
69 | | /// exists within the map. |
70 | 0 | template <typename T> T lookup(T from) const { |
71 | 0 | auto result = lookupOrNull(from); |
72 | 0 | assert(result && "expected 'from' to be contained within the map"); |
73 | 0 | return result; |
74 | 0 | } |
75 | | |
76 | | /// Clears all mappings held by the mapper. |
77 | 0 | void clear() { valueMap.clear(); } |
78 | | |
79 | | private: |
80 | | /// Utility lookupOrValue that looks up an existing key or returns the |
81 | | /// provided value. |
82 | 0 | Block *lookupOrValue(Block *from, Block *value) const { |
83 | 0 | auto it = valueMap.find(from); |
84 | 0 | return it != valueMap.end() ? reinterpret_cast<Block *>(it->second) : value; |
85 | 0 | } |
86 | 0 | Value lookupOrValue(Value from, Value value) const { |
87 | 0 | auto it = valueMap.find(from.getAsOpaquePointer()); |
88 | 0 | return it != valueMap.end() ? Value::getFromOpaquePointer(it->second) |
89 | 0 | : value; |
90 | 0 | } |
91 | | |
92 | | DenseMap<void *, void *> valueMap; |
93 | | }; |
94 | | |
95 | | } // end namespace mlir |
96 | | |
97 | | #endif // MLIR_IR_BLOCKANDVALUEMAPPING_H |