18
18
19
19
#include < cstdint>
20
20
21
- namespace llvm ::sandboxir {
21
+ namespace llvm {
22
+ namespace sandboxir {
22
23
23
24
class Argument ;
24
25
class BBIterator ;
@@ -37,10 +38,28 @@ class Context {
37
38
using MoveInstrCallback =
38
39
std::function<void (Instruction *, const BBIterator &)>;
39
40
40
- // / An ID for a registered callback. Used for deregistration. Using a 64-bit
41
- // / integer so we don't have to worry about the unlikely case of overflowing
42
- // / a 32-bit counter.
43
- using CallbackID = uint64_t ;
41
+ // / An ID for a registered callback. Used for deregistration. A dedicated type
42
+ // / is employed so as to keep IDs opaque to the end user; only Context should
43
+ // / deal with its underlying representation.
44
+ class CallbackID {
45
+ public:
46
+ // Uses a 64-bit integer so we don't have to worry about the unlikely case
47
+ // of overflowing a 32-bit counter.
48
+ using ValTy = uint64_t ;
49
+ static constexpr const ValTy InvalidVal = 0 ;
50
+
51
+ private:
52
+ // Default initialization results in an invalid ID.
53
+ ValTy Val = InvalidVal;
54
+ explicit CallbackID (ValTy Val) : Val{Val} {
55
+ assert (Val != InvalidVal && " newly-created ID is invalid!" );
56
+ }
57
+
58
+ public:
59
+ CallbackID () = default ;
60
+ friend class Context ;
61
+ friend struct DenseMapInfo <CallbackID>;
62
+ };
44
63
45
64
protected:
46
65
LLVMContext &LLVMCtx;
@@ -83,7 +102,7 @@ class Context {
83
102
// / A counter used for assigning callback IDs during registration. The same
84
103
// / counter is used for all kinds of callbacks so we can detect mismatched
85
104
// / registration/deregistration.
86
- CallbackID NextCallbackID = 0 ;
105
+ CallbackID::ValTy NextCallbackID = 1 ;
87
106
88
107
// / Remove \p V from the maps and returns the unique_ptr.
89
108
std::unique_ptr<Value> detachLLVMValue (llvm::Value *V);
@@ -263,6 +282,27 @@ class Context {
263
282
// TODO: Add callbacks for instructions inserted/removed if needed.
264
283
};
265
284
266
- } // namespace llvm::sandboxir
285
+ } // namespace sandboxir
286
+
287
+ // DenseMap info for CallbackIDs
288
+ template <> struct DenseMapInfo <sandboxir::Context::CallbackID> {
289
+ using CallbackID = sandboxir::Context::CallbackID;
290
+ using ReprInfo = DenseMapInfo<CallbackID::ValTy>;
291
+
292
+ static CallbackID getEmptyKey () {
293
+ return CallbackID{ReprInfo::getEmptyKey ()};
294
+ }
295
+ static CallbackID getTombstoneKey () {
296
+ return CallbackID{ReprInfo::getTombstoneKey ()};
297
+ }
298
+ static unsigned getHashValue (const CallbackID &ID) {
299
+ return ReprInfo::getHashValue (ID.Val );
300
+ }
301
+ static bool isEqual (const CallbackID &LHS, const CallbackID &RHS) {
302
+ return ReprInfo::isEqual (LHS.Val , RHS.Val );
303
+ }
304
+ };
305
+
306
+ } // namespace llvm
267
307
268
308
#endif // LLVM_SANDBOXIR_CONTEXT_H
0 commit comments