@@ -33,36 +33,60 @@ class ValueStoreNotPrintable {};
33
33
} // namespace Internal
34
34
35
35
struct IdTag {
36
- IdTag ()
37
- : id_tag_(0 ),
38
- initial_reserved_ids_ (std::numeric_limits<int32_t >::max()) {}
36
+ IdTag () = default ;
37
+
39
38
explicit IdTag (int32_t id_index, int32_t initial_reserved_ids)
40
39
: initial_reserved_ids_(initial_reserved_ids) {
41
40
// Shift down by 1 to get out of the high bit to avoid using any negative
42
- // ids, since they have special uses.
43
- // Shift down by another 1 to free up the second highest bit for a marker to
44
- // indicate whether the index is tagged (& needs to be untagged) or not.
45
- // Add one to the index so it's not zero-based, to make it a bit less likely
46
- // this doesn't collide with anything else (though with the
47
- // second-highest-bit-tagging this might not be needed).
41
+ // ids, since they have special uses. Shift down by another 1 to free up the
42
+ // second highest bit for a marker to indicate whether the index is tagged
43
+ // (& needs to be untagged) or not. Add one to the index so it's not
44
+ // zero-based, to make it a bit less likely this doesn't collide with
45
+ // anything else (though with the second-highest-bit-tagging this might not
46
+ // be needed).
48
47
id_tag_ = llvm::reverseBits ((((id_index + 1 ) << 1 ) | 1 ) << 1 );
49
48
}
49
+
50
50
auto GetCheckIRId () const -> int32_t {
51
51
return (llvm::reverseBits (id_tag_) >> 2 ) - 1 ;
52
52
}
53
+
53
54
auto Apply (int32_t index) const -> int32_t {
54
55
if (index < initial_reserved_ids_) {
55
56
return index;
56
57
}
57
- // assert that id_tag_ doesn't have the second highest bit set
58
+ // TODO: Assert that id_tag_ doesn't have the second highest bit set.
58
59
return index ^ id_tag_;
59
60
}
60
- static auto DecomposeWithBestEffort (int32_t tagged_index)
61
- -> std::pair<int32_t, int32_t> {
61
+
62
+ auto Remove (int32_t tagged_index) const -> int32_t {
63
+ if (!HasTag (tagged_index)) {
64
+ CARBON_DCHECK (tagged_index < initial_reserved_ids_,
65
+ " This untagged index is outside the initial reserved ids "
66
+ " and should have been tagged." );
67
+ return tagged_index;
68
+ }
69
+ auto index = tagged_index ^ id_tag_;
70
+ CARBON_DCHECK (index >= initial_reserved_ids_,
71
+ " When removing tagging bits, found an index that "
72
+ " shouldn't've been tagged in the first place." );
73
+ return index;
74
+ }
75
+
76
+ static auto HasTag (int32_t tagged_index) -> bool {
77
+ return (llvm::reverseBits (2 ) & tagged_index) != 0 ;
78
+ }
79
+
80
+ struct IrAndIndex {
81
+ int32_t check_ir_id;
82
+ int32_t index;
83
+ };
84
+
85
+ static auto DecomposeWithBestEffort (int32_t tagged_index) -> IrAndIndex {
62
86
if (tagged_index < 0 ) {
63
87
return {-1 , tagged_index};
64
88
}
65
- if (( llvm::reverseBits ( 2 ) & tagged_index) == 0 ) {
89
+ if (! HasTag ( tagged_index)) {
66
90
return {-1 , tagged_index};
67
91
}
68
92
int length = 0 ;
@@ -86,25 +110,15 @@ struct IdTag {
86
110
return {-1 , tagged_index};
87
111
}
88
112
auto index_mask = llvm::maskTrailingOnes<uint32_t >(location);
89
- auto ir_id = (llvm::reverseBits (tagged_index & ~index_mask) >> 2 ) - 1 ;
113
+ auto check_ir_id = (llvm::reverseBits (tagged_index & ~index_mask) >> 2 ) - 1 ;
90
114
auto index = tagged_index & index_mask;
91
- return {ir_id, index};
92
- }
93
- auto Remove (int32_t tagged_index) const -> int32_t {
94
- if ((llvm::reverseBits (2 ) & tagged_index) == 0 ) {
95
- CARBON_DCHECK (tagged_index < initial_reserved_ids_,
96
- " This untagged index is outside the initial reserved ids "
97
- " and should have been tagged." );
98
- return tagged_index;
99
- }
100
- auto index = tagged_index ^ id_tag_;
101
- CARBON_DCHECK (index >= initial_reserved_ids_,
102
- " When removing tagging bits, found an index that "
103
- " shouldn't've been tagged in the first place." );
104
- return index;
115
+ return {.check_ir_id = static_cast <int32_t >(check_ir_id),
116
+ .index = static_cast <int32_t >(index)};
105
117
}
106
- int32_t id_tag_;
107
- int32_t initial_reserved_ids_;
118
+
119
+ private:
120
+ int32_t id_tag_ = 0 ;
121
+ int32_t initial_reserved_ids_ = std::numeric_limits<int32_t >::max();
108
122
};
109
123
110
124
// A simple wrapper for accumulating values, providing IDs to later retrieve the
0 commit comments