id_tag.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_BASE_ID_TAG_H_
  5. #define CARBON_TOOLCHAIN_BASE_ID_TAG_H_
  6. #include <stdint.h>
  7. #include <limits>
  8. #include "common/check.h"
  9. #include "common/ostream.h"
  10. #include "llvm/Support/MathExtras.h"
  11. namespace Carbon {
  12. // A sentinel type to construct an IdTag without tagging.
  13. struct Untagged : Printable<Untagged> {
  14. auto Print(llvm::raw_ostream& out) const -> void { out << "<untagged>"; }
  15. };
  16. // A wrapper type used as the template argument to IdTag, in order to mark the
  17. // tag type as such.
  18. template <typename TagIdT>
  19. struct Tag {};
  20. template <typename T>
  21. struct GetTagIdType {
  22. static_assert(false, "IdTag with TagT that is neither Untagged nor Tag");
  23. };
  24. template <>
  25. struct GetTagIdType<Untagged> {
  26. using TagIdType = Untagged;
  27. };
  28. template <typename TagIdT>
  29. struct GetTagIdType<Tag<TagIdT>> {
  30. using TagIdType = TagIdT;
  31. };
  32. // Tests if an `IdTag` type is untagged.
  33. template <typename IdTagT>
  34. concept IdTagIsUntagged = std::same_as<typename IdTagT::TagIdType, Untagged>;
  35. // A tagged Id. It is used to add a tag into the unused bits of the id, in order
  36. // to verify ids are used in the correct context. The tag type must be `Tag` or
  37. // `Untagged`.
  38. template <typename IdT, typename TagT>
  39. struct IdTag {
  40. using IdType = IdT;
  41. using TagIdType = GetTagIdType<TagT>::TagIdType;
  42. IdTag()
  43. requires(IdTagIsUntagged<IdTag>)
  44. = default;
  45. IdTag(TagIdType tag, int32_t initial_reserved_ids)
  46. requires(!IdTagIsUntagged<IdTag>)
  47. : // Shift down by 1 to get out of the high bit to avoid using any
  48. // negative ids, since they have special uses. Shift down by another 1
  49. // to free up the second highest bit for a marker to indicate whether
  50. // the index is tagged (& needs to be untagged) or not. Add one to the
  51. // index so it's not zero-based, to make it a bit less likely this
  52. // doesn't collide with anything else (though with the
  53. // second-highest-bit-tagging this might not be needed).
  54. tag_(llvm::reverseBits((((tag.index + 1) << 1) | 1) << 1)),
  55. initial_reserved_ids_(initial_reserved_ids) {}
  56. auto Apply(int32_t index) const -> IdT {
  57. CARBON_DCHECK(index >= 0, "{0}", index);
  58. if (index < initial_reserved_ids_) {
  59. return IdT(index);
  60. }
  61. // TODO: Assert that tag_ doesn't have the second highest bit set.
  62. auto tagged_index = index ^ tag_;
  63. CARBON_DCHECK(tagged_index >= 0, "{0}", tagged_index);
  64. return IdT(tagged_index);
  65. }
  66. auto Remove(IdT id) const -> int32_t {
  67. CARBON_DCHECK(id.index >= 0, "{0}", id);
  68. if (!HasTag(id.index)) {
  69. CARBON_DCHECK(id.index < initial_reserved_ids_,
  70. "This untagged index is outside the initial reserved ids "
  71. "and should have been tagged.");
  72. return id.index;
  73. }
  74. auto untagged_index = id.index ^ tag_;
  75. CARBON_DCHECK(untagged_index >= initial_reserved_ids_,
  76. "When removing tagging bits, found an index that "
  77. "shouldn't've been tagged in the first place.");
  78. return untagged_index;
  79. }
  80. // Gets the value unique to this IdTag instance that is added to indices in
  81. // Apply, and removed in Remove.
  82. auto GetContainerTag() const -> TagIdType {
  83. if constexpr (IdTagIsUntagged<IdTag>) {
  84. return TagIdType();
  85. } else {
  86. return TagIdType((llvm::reverseBits(tag_) >> 2) - 1);
  87. }
  88. }
  89. // Returns whether `tagged_index` has an IdTag applied to it, from this IdTag
  90. // instance or any other one.
  91. static auto HasTag(int32_t tagged_index) -> bool {
  92. return (llvm::reverseBits(2) & tagged_index) != 0;
  93. }
  94. struct TagAndIndex {
  95. TagIdType tag;
  96. int32_t index;
  97. };
  98. static auto DecomposeWithBestEffort(IdT id) -> TagAndIndex {
  99. if constexpr (IdTagIsUntagged<IdTag>) {
  100. return {TagIdType(), id.index};
  101. } else {
  102. if (!id.has_value()) {
  103. return {TagIdType::None, id.index};
  104. }
  105. if (!HasTag(id.index)) {
  106. return {TagIdType::None, id.index};
  107. }
  108. int length = 0;
  109. int location = 0;
  110. for (int i = 0; i != 32; ++i) {
  111. int current_run = 0;
  112. int location_of_current_run = i;
  113. while (i != 32 && (id.index & (1 << i)) == 0) {
  114. ++current_run;
  115. ++i;
  116. }
  117. if (current_run != 0) {
  118. --i;
  119. }
  120. if (current_run > length) {
  121. length = current_run;
  122. location = location_of_current_run;
  123. }
  124. }
  125. if (length < 8) {
  126. return {TagIdType::None, id.index};
  127. }
  128. auto index_mask = llvm::maskTrailingOnes<uint32_t>(location);
  129. auto tag = (llvm::reverseBits(id.index & ~index_mask) >> 2) - 1;
  130. auto index = id.index & index_mask;
  131. return {.tag = TagIdType(static_cast<int32_t>(tag)),
  132. .index = static_cast<int32_t>(index)};
  133. }
  134. }
  135. // Converts an IdTag to be used for a different ID type. This is only valid
  136. // when the id indices are interchangeable, as they will have the same tag and
  137. // the same reserved ids.
  138. template <typename OtherIdT>
  139. requires(!IdTagIsUntagged<IdTag>)
  140. auto ToEquivalentIdType() -> IdTag<OtherIdT, Tag<TagIdType>> {
  141. return {GetContainerTag(), initial_reserved_ids_};
  142. }
  143. private:
  144. int32_t tag_ = 0;
  145. int32_t initial_reserved_ids_ = std::numeric_limits<int32_t>::max();
  146. };
  147. } // namespace Carbon
  148. #endif // CARBON_TOOLCHAIN_BASE_ID_TAG_H_