FoldingSet.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //===-- Support/FoldingSet.cpp - Uniquing Hash Set --------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a hash set that can be used to remove duplication of
  11. // nodes in a graph.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/FoldingSet.h"
  15. #include "llvm/ADT/Hashing.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/Host.h"
  19. #include "llvm/Support/MathExtras.h"
  20. #include <cassert>
  21. #include <cstring>
  22. using namespace llvm;
  23. //===----------------------------------------------------------------------===//
  24. // FoldingSetNodeIDRef Implementation
  25. /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
  26. /// used to lookup the node in the FoldingSetImpl.
  27. unsigned FoldingSetNodeIDRef::ComputeHash() const {
  28. return static_cast<unsigned>(hash_combine_range(Data, Data+Size));
  29. }
  30. bool FoldingSetNodeIDRef::operator==(FoldingSetNodeIDRef RHS) const {
  31. if (Size != RHS.Size) return false;
  32. return memcmp(Data, RHS.Data, Size*sizeof(*Data)) == 0;
  33. }
  34. /// Used to compare the "ordering" of two nodes as defined by the
  35. /// profiled bits and their ordering defined by memcmp().
  36. bool FoldingSetNodeIDRef::operator<(FoldingSetNodeIDRef RHS) const {
  37. if (Size != RHS.Size)
  38. return Size < RHS.Size;
  39. return memcmp(Data, RHS.Data, Size*sizeof(*Data)) < 0;
  40. }
  41. //===----------------------------------------------------------------------===//
  42. // FoldingSetNodeID Implementation
  43. /// Add* - Add various data types to Bit data.
  44. ///
  45. void FoldingSetNodeID::AddPointer(const void *Ptr) {
  46. // Note: this adds pointers to the hash using sizes and endianness that
  47. // depend on the host. It doesn't matter, however, because hashing on
  48. // pointer values is inherently unstable. Nothing should depend on the
  49. // ordering of nodes in the folding set.
  50. Bits.append(reinterpret_cast<unsigned *>(&Ptr),
  51. reinterpret_cast<unsigned *>(&Ptr+1));
  52. }
  53. void FoldingSetNodeID::AddInteger(signed I) {
  54. Bits.push_back(I);
  55. }
  56. void FoldingSetNodeID::AddInteger(unsigned I) {
  57. Bits.push_back(I);
  58. }
  59. void FoldingSetNodeID::AddInteger(long I) {
  60. AddInteger((unsigned long)I);
  61. }
  62. void FoldingSetNodeID::AddInteger(unsigned long I) {
  63. if (sizeof(long) == sizeof(int))
  64. AddInteger(unsigned(I));
  65. else if (sizeof(long) == sizeof(long long)) {
  66. AddInteger((unsigned long long)I);
  67. } else {
  68. llvm_unreachable("unexpected sizeof(long)");
  69. }
  70. }
  71. void FoldingSetNodeID::AddInteger(long long I) {
  72. AddInteger((unsigned long long)I);
  73. }
  74. void FoldingSetNodeID::AddInteger(unsigned long long I) {
  75. AddInteger(unsigned(I));
  76. if ((uint64_t)(unsigned)I != I)
  77. Bits.push_back(unsigned(I >> 32));
  78. }
  79. void FoldingSetNodeID::AddString(StringRef String) {
  80. unsigned Size = String.size();
  81. Bits.push_back(Size);
  82. if (!Size) return;
  83. unsigned Units = Size / 4;
  84. unsigned Pos = 0;
  85. const unsigned *Base = (const unsigned*) String.data();
  86. // If the string is aligned do a bulk transfer.
  87. if (!((intptr_t)Base & 3)) {
  88. Bits.append(Base, Base + Units);
  89. Pos = (Units + 1) * 4;
  90. } else {
  91. // Otherwise do it the hard way.
  92. // To be compatible with above bulk transfer, we need to take endianness
  93. // into account.
  94. static_assert(sys::IsBigEndianHost || sys::IsLittleEndianHost,
  95. "Unexpected host endianness");
  96. if (sys::IsBigEndianHost) {
  97. for (Pos += 4; Pos <= Size; Pos += 4) {
  98. unsigned V = ((unsigned char)String[Pos - 4] << 24) |
  99. ((unsigned char)String[Pos - 3] << 16) |
  100. ((unsigned char)String[Pos - 2] << 8) |
  101. (unsigned char)String[Pos - 1];
  102. Bits.push_back(V);
  103. }
  104. } else { // Little-endian host
  105. for (Pos += 4; Pos <= Size; Pos += 4) {
  106. unsigned V = ((unsigned char)String[Pos - 1] << 24) |
  107. ((unsigned char)String[Pos - 2] << 16) |
  108. ((unsigned char)String[Pos - 3] << 8) |
  109. (unsigned char)String[Pos - 4];
  110. Bits.push_back(V);
  111. }
  112. }
  113. }
  114. // With the leftover bits.
  115. unsigned V = 0;
  116. // Pos will have overshot size by 4 - #bytes left over.
  117. // No need to take endianness into account here - this is always executed.
  118. switch (Pos - Size) {
  119. case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
  120. case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
  121. case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
  122. default: return; // Nothing left.
  123. }
  124. Bits.push_back(V);
  125. }
  126. // AddNodeID - Adds the Bit data of another ID to *this.
  127. void FoldingSetNodeID::AddNodeID(const FoldingSetNodeID &ID) {
  128. Bits.append(ID.Bits.begin(), ID.Bits.end());
  129. }
  130. /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to
  131. /// lookup the node in the FoldingSetImpl.
  132. unsigned FoldingSetNodeID::ComputeHash() const {
  133. return FoldingSetNodeIDRef(Bits.data(), Bits.size()).ComputeHash();
  134. }
  135. /// operator== - Used to compare two nodes to each other.
  136. ///
  137. bool FoldingSetNodeID::operator==(const FoldingSetNodeID &RHS) const {
  138. return *this == FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
  139. }
  140. /// operator== - Used to compare two nodes to each other.
  141. ///
  142. bool FoldingSetNodeID::operator==(FoldingSetNodeIDRef RHS) const {
  143. return FoldingSetNodeIDRef(Bits.data(), Bits.size()) == RHS;
  144. }
  145. /// Used to compare the "ordering" of two nodes as defined by the
  146. /// profiled bits and their ordering defined by memcmp().
  147. bool FoldingSetNodeID::operator<(const FoldingSetNodeID &RHS) const {
  148. return *this < FoldingSetNodeIDRef(RHS.Bits.data(), RHS.Bits.size());
  149. }
  150. bool FoldingSetNodeID::operator<(FoldingSetNodeIDRef RHS) const {
  151. return FoldingSetNodeIDRef(Bits.data(), Bits.size()) < RHS;
  152. }
  153. /// Intern - Copy this node's data to a memory region allocated from the
  154. /// given allocator and return a FoldingSetNodeIDRef describing the
  155. /// interned data.
  156. FoldingSetNodeIDRef
  157. FoldingSetNodeID::Intern(BumpPtrAllocator &Allocator) const {
  158. unsigned *New = Allocator.Allocate<unsigned>(Bits.size());
  159. std::uninitialized_copy(Bits.begin(), Bits.end(), New);
  160. return FoldingSetNodeIDRef(New, Bits.size());
  161. }
  162. //===----------------------------------------------------------------------===//
  163. /// Helper functions for FoldingSetImpl.
  164. /// GetNextPtr - In order to save space, each bucket is a
  165. /// singly-linked-list. In order to make deletion more efficient, we make
  166. /// the list circular, so we can delete a node without computing its hash.
  167. /// The problem with this is that the start of the hash buckets are not
  168. /// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null:
  169. /// use GetBucketPtr when this happens.
  170. static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) {
  171. // The low bit is set if this is the pointer back to the bucket.
  172. if (reinterpret_cast<intptr_t>(NextInBucketPtr) & 1)
  173. return nullptr;
  174. return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr);
  175. }
  176. /// testing.
  177. static void **GetBucketPtr(void *NextInBucketPtr) {
  178. intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
  179. assert((Ptr & 1) && "Not a bucket pointer");
  180. return reinterpret_cast<void**>(Ptr & ~intptr_t(1));
  181. }
  182. /// GetBucketFor - Hash the specified node ID and return the hash bucket for
  183. /// the specified ID.
  184. static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
  185. // NumBuckets is always a power of 2.
  186. unsigned BucketNum = Hash & (NumBuckets-1);
  187. return Buckets + BucketNum;
  188. }
  189. /// AllocateBuckets - Allocated initialized bucket memory.
  190. static void **AllocateBuckets(unsigned NumBuckets) {
  191. void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
  192. if (Buckets == nullptr) throw std::bad_alloc(); // HLSL Change
  193. // Set the very last bucket to be a non-null "pointer".
  194. Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
  195. return Buckets;
  196. }
  197. //===----------------------------------------------------------------------===//
  198. // FoldingSetImpl Implementation
  199. void FoldingSetImpl::anchor() {}
  200. FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) {
  201. assert(5 < Log2InitSize && Log2InitSize < 32 &&
  202. "Initial hash table size out of range");
  203. NumBuckets = 1 << Log2InitSize;
  204. Buckets = AllocateBuckets(NumBuckets);
  205. NumNodes = 0;
  206. }
  207. FoldingSetImpl::~FoldingSetImpl() {
  208. free(Buckets);
  209. }
  210. void FoldingSetImpl::clear() {
  211. // Set all but the last bucket to null pointers.
  212. memset(Buckets, 0, NumBuckets*sizeof(void*));
  213. // Set the very last bucket to be a non-null "pointer".
  214. Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
  215. // Reset the node count to zero.
  216. NumNodes = 0;
  217. }
  218. /// GrowHashTable - Double the size of the hash table and rehash everything.
  219. ///
  220. void FoldingSetImpl::GrowHashTable() {
  221. void **OldBuckets = Buckets;
  222. unsigned OldNumBuckets = NumBuckets;
  223. NumBuckets <<= 1;
  224. // Clear out new buckets.
  225. Buckets = AllocateBuckets(NumBuckets);
  226. NumNodes = 0;
  227. // Walk the old buckets, rehashing nodes into their new place.
  228. FoldingSetNodeID TempID;
  229. for (unsigned i = 0; i != OldNumBuckets; ++i) {
  230. void *Probe = OldBuckets[i];
  231. if (!Probe) continue;
  232. while (Node *NodeInBucket = GetNextPtr(Probe)) {
  233. // Figure out the next link, remove NodeInBucket from the old link.
  234. Probe = NodeInBucket->getNextInBucket();
  235. NodeInBucket->SetNextInBucket(nullptr);
  236. // Insert the node into the new bucket, after recomputing the hash.
  237. InsertNode(NodeInBucket,
  238. GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
  239. Buckets, NumBuckets));
  240. TempID.clear();
  241. }
  242. }
  243. free(OldBuckets);
  244. }
  245. /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
  246. /// return it. If not, return the insertion token that will make insertion
  247. /// faster.
  248. FoldingSetImpl::Node
  249. *FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
  250. void *&InsertPos) {
  251. unsigned IDHash = ID.ComputeHash();
  252. void **Bucket = GetBucketFor(IDHash, Buckets, NumBuckets);
  253. void *Probe = *Bucket;
  254. InsertPos = nullptr;
  255. FoldingSetNodeID TempID;
  256. while (Node *NodeInBucket = GetNextPtr(Probe)) {
  257. if (NodeEquals(NodeInBucket, ID, IDHash, TempID))
  258. return NodeInBucket;
  259. TempID.clear();
  260. Probe = NodeInBucket->getNextInBucket();
  261. }
  262. // Didn't find the node, return null with the bucket as the InsertPos.
  263. InsertPos = Bucket;
  264. return nullptr;
  265. }
  266. /// InsertNode - Insert the specified node into the folding set, knowing that it
  267. /// is not already in the map. InsertPos must be obtained from
  268. /// FindNodeOrInsertPos.
  269. void FoldingSetImpl::InsertNode(Node *N, void *InsertPos) {
  270. assert(!N->getNextInBucket());
  271. // Do we need to grow the hashtable?
  272. if (NumNodes+1 > NumBuckets*2) {
  273. GrowHashTable();
  274. FoldingSetNodeID TempID;
  275. InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
  276. }
  277. ++NumNodes;
  278. /// The insert position is actually a bucket pointer.
  279. void **Bucket = static_cast<void**>(InsertPos);
  280. void *Next = *Bucket;
  281. // If this is the first insertion into this bucket, its next pointer will be
  282. // null. Pretend as if it pointed to itself, setting the low bit to indicate
  283. // that it is a pointer to the bucket.
  284. if (!Next)
  285. Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
  286. // Set the node's next pointer, and make the bucket point to the node.
  287. N->SetNextInBucket(Next);
  288. *Bucket = N;
  289. }
  290. /// RemoveNode - Remove a node from the folding set, returning true if one was
  291. /// removed or false if the node was not in the folding set.
  292. bool FoldingSetImpl::RemoveNode(Node *N) {
  293. // Because each bucket is a circular list, we don't need to compute N's hash
  294. // to remove it.
  295. void *Ptr = N->getNextInBucket();
  296. if (!Ptr) return false; // Not in folding set.
  297. --NumNodes;
  298. N->SetNextInBucket(nullptr);
  299. // Remember what N originally pointed to, either a bucket or another node.
  300. void *NodeNextPtr = Ptr;
  301. // Chase around the list until we find the node (or bucket) which points to N.
  302. while (true) {
  303. if (Node *NodeInBucket = GetNextPtr(Ptr)) {
  304. // Advance pointer.
  305. Ptr = NodeInBucket->getNextInBucket();
  306. // We found a node that points to N, change it to point to N's next node,
  307. // removing N from the list.
  308. if (Ptr == N) {
  309. NodeInBucket->SetNextInBucket(NodeNextPtr);
  310. return true;
  311. }
  312. } else {
  313. void **Bucket = GetBucketPtr(Ptr);
  314. Ptr = *Bucket;
  315. // If we found that the bucket points to N, update the bucket to point to
  316. // whatever is next.
  317. if (Ptr == N) {
  318. *Bucket = NodeNextPtr;
  319. return true;
  320. }
  321. }
  322. }
  323. }
  324. /// GetOrInsertNode - If there is an existing simple Node exactly
  325. /// equal to the specified node, return it. Otherwise, insert 'N' and it
  326. /// instead.
  327. FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
  328. FoldingSetNodeID ID;
  329. GetNodeProfile(N, ID);
  330. void *IP;
  331. if (Node *E = FindNodeOrInsertPos(ID, IP))
  332. return E;
  333. InsertNode(N, IP);
  334. return N;
  335. }
  336. //===----------------------------------------------------------------------===//
  337. // FoldingSetIteratorImpl Implementation
  338. FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
  339. // Skip to the first non-null non-self-cycle bucket.
  340. while (*Bucket != reinterpret_cast<void*>(-1) &&
  341. (!*Bucket || !GetNextPtr(*Bucket)))
  342. ++Bucket;
  343. NodePtr = static_cast<FoldingSetNode*>(*Bucket);
  344. }
  345. void FoldingSetIteratorImpl::advance() {
  346. // If there is another link within this bucket, go to it.
  347. void *Probe = NodePtr->getNextInBucket();
  348. if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
  349. NodePtr = NextNodeInBucket;
  350. else {
  351. // Otherwise, this is the last link in this bucket.
  352. void **Bucket = GetBucketPtr(Probe);
  353. // Skip to the next non-null non-self-cycle bucket.
  354. do {
  355. ++Bucket;
  356. } while (*Bucket != reinterpret_cast<void*>(-1) &&
  357. (!*Bucket || !GetNextPtr(*Bucket)));
  358. NodePtr = static_cast<FoldingSetNode*>(*Bucket);
  359. }
  360. }
  361. //===----------------------------------------------------------------------===//
  362. // FoldingSetBucketIteratorImpl Implementation
  363. FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
  364. Ptr = (!*Bucket || !GetNextPtr(*Bucket)) ? (void*) Bucket : *Bucket;
  365. }