My Project
BlockAndValueMapping.h
Go to the documentation of this file.
1 //===- BlockAndValueMapping.h -----------------------------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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:
30  void map(Block *from, Block *to) { valueMap[from] = to; }
31  void map(Value from, Value to) {
32  valueMap[from.getAsOpaquePointer()] = to.getAsOpaquePointer();
33  }
34 
36  void erase(Block *from) { valueMap.erase(from); }
37  void erase(Value from) { valueMap.erase(from.getAsOpaquePointer()); }
38 
40  bool contains(Block *from) const { return valueMap.count(from); }
41  bool contains(Value from) const {
42  return valueMap.count(from.getAsOpaquePointer());
43  }
44 
47  Block *lookupOrNull(Block *from) const {
48  return lookupOrValue(from, (Block *)nullptr);
49  }
50  Value lookupOrNull(Value from) const { return lookupOrValue(from, Value()); }
51 
54  Block *lookupOrDefault(Block *from) const {
55  return lookupOrValue(from, from);
56  }
57  Value lookupOrDefault(Value from) const { return lookupOrValue(from, from); }
58 
61  template <typename T> T lookup(T from) const {
62  auto result = lookupOrNull(from);
63  assert(result && "expected 'from' to be contained within the map");
64  return result;
65  }
66 
68  void clear() { valueMap.clear(); }
69 
70 private:
73  Block *lookupOrValue(Block *from, Block *value) const {
74  auto it = valueMap.find(from);
75  return it != valueMap.end() ? reinterpret_cast<Block *>(it->second) : value;
76  }
77  Value lookupOrValue(Value from, Value value) const {
78  auto it = valueMap.find(from.getAsOpaquePointer());
79  return it != valueMap.end() ? Value::getFromOpaquePointer(it->second)
80  : value;
81  }
82 
83  DenseMap<void *, void *> valueMap;
84 };
85 
86 } // end namespace mlir
87 
88 #endif // MLIR_IR_BLOCKANDVALUEMAPPING_H
Definition: InferTypeOpInterface.cpp:20
Block represents an ordered list of Operations.
Definition: Block.h:21
Definition: LLVM.h:48
void map(Value from, Value to)
Definition: BlockAndValueMapping.h:31
Value lookupOrDefault(Value from) const
Definition: BlockAndValueMapping.h:57
T lookup(T from) const
Definition: BlockAndValueMapping.h:61
void erase(Value from)
Definition: BlockAndValueMapping.h:37
void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: Value.h:184
void map(Block *from, Block *to)
Definition: BlockAndValueMapping.h:30
static Value getFromOpaquePointer(const void *pointer)
Definition: Value.h:185
void clear()
Clears all mappings held by the mapper.
Definition: BlockAndValueMapping.h:68
Block * lookupOrNull(Block *from) const
Definition: BlockAndValueMapping.h:47
void erase(Block *from)
Erases a mapping for &#39;from&#39;.
Definition: BlockAndValueMapping.h:36
Definition: Value.h:38
Definition: BlockAndValueMapping.h:26
bool contains(Block *from) const
Checks to see if a mapping for &#39;from&#39; exists.
Definition: BlockAndValueMapping.h:40
Value lookupOrNull(Value from) const
Definition: BlockAndValueMapping.h:50
Block * lookupOrDefault(Block *from) const
Definition: BlockAndValueMapping.h:54
bool contains(Value from) const
Definition: BlockAndValueMapping.h:41