MLIR  14.0.0git
BlockAndValueMapping.h
Go to the documentation of this file.
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.
27 public:
28  /// Inserts a new mapping for 'from' to 'to'. If there is an existing mapping,
29  /// it is overwritten.
30  void map(Block *from, Block *to) { blockMap[from] = to; }
31  void map(Value from, Value to) { valueMap[from] = to; }
32 
33  template <
34  typename S, typename T,
37  void map(S &&from, T &&to) {
38  for (auto pair : llvm::zip(from, to))
39  map(std::get<0>(pair), std::get<1>(pair));
40  }
41 
42  /// Erases a mapping for 'from'.
43  void erase(Block *from) { blockMap.erase(from); }
44  void erase(Value from) { valueMap.erase(from); }
45 
46  /// Checks to see if a mapping for 'from' exists.
47  bool contains(Block *from) const { return blockMap.count(from); }
48  bool contains(Value from) const { return valueMap.count(from); }
49 
50  /// Lookup a mapped value within the map. If a mapping for the provided value
51  /// does not exist then return nullptr.
52  Block *lookupOrNull(Block *from) const {
53  return lookupOrValue(from, (Block *)nullptr);
54  }
55  Value lookupOrNull(Value from) const { return lookupOrValue(from, Value()); }
56 
57  /// Lookup a mapped value within the map. If a mapping for the provided value
58  /// does not exist then return the provided value.
59  Block *lookupOrDefault(Block *from) const {
60  return lookupOrValue(from, from);
61  }
62  Value lookupOrDefault(Value from) const { return lookupOrValue(from, from); }
63 
64  /// Lookup a mapped value within the map. This asserts the provided value
65  /// exists within the map.
66  template <typename T> T lookup(T from) const {
67  auto result = lookupOrNull(from);
68  assert(result && "expected 'from' to be contained within the map");
69  return result;
70  }
71 
72  /// Clears all mappings held by the mapper.
73  void clear() { valueMap.clear(); }
74 
75  /// Return the held value mapping.
76  const DenseMap<Value, Value> &getValueMap() const { return valueMap; }
77 
78  /// Return the held block mapping.
79  const DenseMap<Block *, Block *> &getBlockMap() const { return blockMap; }
80 
81 private:
82  /// Utility lookupOrValue that looks up an existing key or returns the
83  /// provided value.
84  Block *lookupOrValue(Block *from, Block *value) const {
85  auto it = blockMap.find(from);
86  return it != blockMap.end() ? it->second : value;
87  }
88  Value lookupOrValue(Value from, Value value) const {
89  auto it = valueMap.find(from);
90  return it != valueMap.end() ? it->second : value;
91  }
92 
93  DenseMap<Value, Value> valueMap;
95 };
96 
97 } // namespace mlir
98 
99 #endif // MLIR_IR_BLOCKANDVALUEMAPPING_H
Include the generated interface declarations.
Block represents an ordered list of Operations.
Definition: Block.h:29
void map(Value from, Value to)
Value lookupOrDefault(Value from) const
T lookup(T from) const
Lookup a mapped value within the map.
static constexpr const bool value
void map(Block *from, Block *to)
Inserts a new mapping for &#39;from&#39; to &#39;to&#39;.
void clear()
Clears all mappings held by the mapper.
Block * lookupOrNull(Block *from) const
Lookup a mapped value within the map.
void map(S &&from, T &&to)
void erase(Block *from)
Erases a mapping for &#39;from&#39;.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:84
const DenseMap< Block *, Block * > & getBlockMap() const
Return the held block mapping.
const DenseMap< Value, Value > & getValueMap() const
Return the held value mapping.
bool contains(Block *from) const
Checks to see if a mapping for &#39;from&#39; exists.
Value lookupOrNull(Value from) const
Block * lookupOrDefault(Block *from) const
Lookup a mapped value within the map.
bool contains(Value from) const