FoldingSet.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 FoldingSetBase.
  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. static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long),
  51. "unexpected pointer size");
  52. AddInteger(reinterpret_cast<uintptr_t>(Ptr));
  53. }
  54. void FoldingSetNodeID::AddInteger(signed I) {
  55. Bits.push_back(I);
  56. }
  57. void FoldingSetNodeID::AddInteger(unsigned I) {
  58. Bits.push_back(I);
  59. }
  60. void FoldingSetNodeID::AddInteger(long I) {
  61. AddInteger((unsigned long)I);
  62. }
  63. void FoldingSetNodeID::AddInteger(unsigned long I) {
  64. if (sizeof(long) == sizeof(int))
  65. AddInteger(unsigned(I));
  66. else if (sizeof(long) == sizeof(long long)) {
  67. AddInteger((unsigned long long)I);
  68. } else {
  69. llvm_unreachable("unexpected sizeof(long)");
  70. }
  71. }
  72. void FoldingSetNodeID::AddInteger(long long I) {
  73. AddInteger((unsigned long long)I);
  74. }
  75. void FoldingSetNodeID::AddInteger(unsigned long long I) {
  76. AddInteger(unsigned(I));
  77. AddInteger(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]; LLVM_FALLTHROUGH;
  120. case 2: V = (V << 8) | (unsigned char)String[Size - 2]; LLVM_FALLTHROUGH;
  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 FoldingSetBase.
  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 FoldingSetBase.
  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 FoldingSetBase::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<FoldingSetBase::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)
  193. report_bad_alloc_error("Allocation of Buckets failed.");
  194. // Set the very last bucket to be a non-null "pointer".
  195. Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
  196. return Buckets;
  197. }
  198. //===----------------------------------------------------------------------===//
  199. // FoldingSetBase Implementation
  200. void FoldingSetBase::anchor() {}
  201. FoldingSetBase::FoldingSetBase(unsigned Log2InitSize) {
  202. assert(5 < Log2InitSize && Log2InitSize < 32 &&
  203. "Initial hash table size out of range");
  204. NumBuckets = 1 << Log2InitSize;
  205. Buckets = AllocateBuckets(NumBuckets);
  206. NumNodes = 0;
  207. }
  208. FoldingSetBase::FoldingSetBase(FoldingSetBase &&Arg)
  209. : Buckets(Arg.Buckets), NumBuckets(Arg.NumBuckets), NumNodes(Arg.NumNodes) {
  210. Arg.Buckets = nullptr;
  211. Arg.NumBuckets = 0;
  212. Arg.NumNodes = 0;
  213. }
  214. FoldingSetBase &FoldingSetBase::operator=(FoldingSetBase &&RHS) {
  215. free(Buckets); // This may be null if the set is in a moved-from state.
  216. Buckets = RHS.Buckets;
  217. NumBuckets = RHS.NumBuckets;
  218. NumNodes = RHS.NumNodes;
  219. RHS.Buckets = nullptr;
  220. RHS.NumBuckets = 0;
  221. RHS.NumNodes = 0;
  222. return *this;
  223. }
  224. FoldingSetBase::~FoldingSetBase() {
  225. free(Buckets);
  226. }
  227. void FoldingSetBase::clear() {
  228. // Set all but the last bucket to null pointers.
  229. memset(Buckets, 0, NumBuckets*sizeof(void*));
  230. // Set the very last bucket to be a non-null "pointer".
  231. Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
  232. // Reset the node count to zero.
  233. NumNodes = 0;
  234. }
  235. void FoldingSetBase::GrowBucketCount(unsigned NewBucketCount) {
  236. assert((NewBucketCount > NumBuckets) && "Can't shrink a folding set with GrowBucketCount");
  237. assert(isPowerOf2_32(NewBucketCount) && "Bad bucket count!");
  238. void **OldBuckets = Buckets;
  239. unsigned OldNumBuckets = NumBuckets;
  240. // Clear out new buckets.
  241. Buckets = AllocateBuckets(NewBucketCount);
  242. // Set NumBuckets only if allocation of new buckets was succesful
  243. NumBuckets = NewBucketCount;
  244. NumNodes = 0;
  245. // Walk the old buckets, rehashing nodes into their new place.
  246. FoldingSetNodeID TempID;
  247. for (unsigned i = 0; i != OldNumBuckets; ++i) {
  248. void *Probe = OldBuckets[i];
  249. if (!Probe) continue;
  250. while (Node *NodeInBucket = GetNextPtr(Probe)) {
  251. // Figure out the next link, remove NodeInBucket from the old link.
  252. Probe = NodeInBucket->getNextInBucket();
  253. NodeInBucket->SetNextInBucket(nullptr);
  254. // Insert the node into the new bucket, after recomputing the hash.
  255. InsertNode(NodeInBucket,
  256. GetBucketFor(ComputeNodeHash(NodeInBucket, TempID),
  257. Buckets, NumBuckets));
  258. TempID.clear();
  259. }
  260. }
  261. free(OldBuckets);
  262. }
  263. /// GrowHashTable - Double the size of the hash table and rehash everything.
  264. ///
  265. void FoldingSetBase::GrowHashTable() {
  266. GrowBucketCount(NumBuckets * 2);
  267. }
  268. void FoldingSetBase::reserve(unsigned EltCount) {
  269. // This will give us somewhere between EltCount / 2 and
  270. // EltCount buckets. This puts us in the load factor
  271. // range of 1.0 - 2.0.
  272. if(EltCount < capacity())
  273. return;
  274. GrowBucketCount(PowerOf2Floor(EltCount));
  275. }
  276. /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
  277. /// return it. If not, return the insertion token that will make insertion
  278. /// faster.
  279. FoldingSetBase::Node *
  280. FoldingSetBase::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
  281. void *&InsertPos) {
  282. unsigned IDHash = ID.ComputeHash();
  283. void **Bucket = GetBucketFor(IDHash, Buckets, NumBuckets);
  284. void *Probe = *Bucket;
  285. InsertPos = nullptr;
  286. FoldingSetNodeID TempID;
  287. while (Node *NodeInBucket = GetNextPtr(Probe)) {
  288. if (NodeEquals(NodeInBucket, ID, IDHash, TempID))
  289. return NodeInBucket;
  290. TempID.clear();
  291. Probe = NodeInBucket->getNextInBucket();
  292. }
  293. // Didn't find the node, return null with the bucket as the InsertPos.
  294. InsertPos = Bucket;
  295. return nullptr;
  296. }
  297. /// InsertNode - Insert the specified node into the folding set, knowing that it
  298. /// is not already in the map. InsertPos must be obtained from
  299. /// FindNodeOrInsertPos.
  300. void FoldingSetBase::InsertNode(Node *N, void *InsertPos) {
  301. assert(!N->getNextInBucket());
  302. // Do we need to grow the hashtable?
  303. if (NumNodes+1 > capacity()) {
  304. GrowHashTable();
  305. FoldingSetNodeID TempID;
  306. InsertPos = GetBucketFor(ComputeNodeHash(N, TempID), Buckets, NumBuckets);
  307. }
  308. ++NumNodes;
  309. /// The insert position is actually a bucket pointer.
  310. void **Bucket = static_cast<void**>(InsertPos);
  311. void *Next = *Bucket;
  312. // If this is the first insertion into this bucket, its next pointer will be
  313. // null. Pretend as if it pointed to itself, setting the low bit to indicate
  314. // that it is a pointer to the bucket.
  315. if (!Next)
  316. Next = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(Bucket)|1);
  317. // Set the node's next pointer, and make the bucket point to the node.
  318. N->SetNextInBucket(Next);
  319. *Bucket = N;
  320. }
  321. /// RemoveNode - Remove a node from the folding set, returning true if one was
  322. /// removed or false if the node was not in the folding set.
  323. bool FoldingSetBase::RemoveNode(Node *N) {
  324. // Because each bucket is a circular list, we don't need to compute N's hash
  325. // to remove it.
  326. void *Ptr = N->getNextInBucket();
  327. if (!Ptr) return false; // Not in folding set.
  328. --NumNodes;
  329. N->SetNextInBucket(nullptr);
  330. // Remember what N originally pointed to, either a bucket or another node.
  331. void *NodeNextPtr = Ptr;
  332. // Chase around the list until we find the node (or bucket) which points to N.
  333. while (true) {
  334. if (Node *NodeInBucket = GetNextPtr(Ptr)) {
  335. // Advance pointer.
  336. Ptr = NodeInBucket->getNextInBucket();
  337. // We found a node that points to N, change it to point to N's next node,
  338. // removing N from the list.
  339. if (Ptr == N) {
  340. NodeInBucket->SetNextInBucket(NodeNextPtr);
  341. return true;
  342. }
  343. } else {
  344. void **Bucket = GetBucketPtr(Ptr);
  345. Ptr = *Bucket;
  346. // If we found that the bucket points to N, update the bucket to point to
  347. // whatever is next.
  348. if (Ptr == N) {
  349. *Bucket = NodeNextPtr;
  350. return true;
  351. }
  352. }
  353. }
  354. }
  355. /// GetOrInsertNode - If there is an existing simple Node exactly
  356. /// equal to the specified node, return it. Otherwise, insert 'N' and it
  357. /// instead.
  358. FoldingSetBase::Node *FoldingSetBase::GetOrInsertNode(FoldingSetBase::Node *N) {
  359. FoldingSetNodeID ID;
  360. GetNodeProfile(N, ID);
  361. void *IP;
  362. if (Node *E = FindNodeOrInsertPos(ID, IP))
  363. return E;
  364. InsertNode(N, IP);
  365. return N;
  366. }
  367. //===----------------------------------------------------------------------===//
  368. // FoldingSetIteratorImpl Implementation
  369. FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
  370. // Skip to the first non-null non-self-cycle bucket.
  371. while (*Bucket != reinterpret_cast<void*>(-1) &&
  372. (!*Bucket || !GetNextPtr(*Bucket)))
  373. ++Bucket;
  374. NodePtr = static_cast<FoldingSetNode*>(*Bucket);
  375. }
  376. void FoldingSetIteratorImpl::advance() {
  377. // If there is another link within this bucket, go to it.
  378. void *Probe = NodePtr->getNextInBucket();
  379. if (FoldingSetNode *NextNodeInBucket = GetNextPtr(Probe))
  380. NodePtr = NextNodeInBucket;
  381. else {
  382. // Otherwise, this is the last link in this bucket.
  383. void **Bucket = GetBucketPtr(Probe);
  384. // Skip to the next non-null non-self-cycle bucket.
  385. do {
  386. ++Bucket;
  387. } while (*Bucket != reinterpret_cast<void*>(-1) &&
  388. (!*Bucket || !GetNextPtr(*Bucket)));
  389. NodePtr = static_cast<FoldingSetNode*>(*Bucket);
  390. }
  391. }
  392. //===----------------------------------------------------------------------===//
  393. // FoldingSetBucketIteratorImpl Implementation
  394. FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
  395. Ptr = (!*Bucket || !GetNextPtr(*Bucket)) ? (void*) Bucket : *Bucket;
  396. }