1 //===-- llvm/ADT/FoldingSet.h - Uniquing Hash Set ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a hash set that can be used to remove duplication of nodes
11 // in a graph. This code was originally created by Chris Lattner for use with
12 // SelectionDAGCSEMap, but was isolated to provide use across the llvm code set.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ADT_FOLDINGSET_H
17 #define LLVM_ADT_FOLDINGSET_H
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/ADT/SmallVector.h"
25 /// This folding set used for two purposes:
26 /// 1. Given information about a node we want to create, look up the unique
27 /// instance of the node in the set. If the node already exists, return
28 /// it, otherwise return the bucket it should be inserted into.
29 /// 2. Given a node that has already been created, remove it from the set.
31 /// This class is implemented as a single-link chained hash table, where the
32 /// "buckets" are actually the nodes themselves (the next pointer is in the
33 /// node). The last node points back to the bucket to simplified node removal.
35 /// Any node that is to be included in the folding set must be a subclass of
36 /// FoldingSetNode. The node class must also define a Profile method used to
37 /// establish the unique bits of data for the node. The Profile method is
38 /// passed a FoldingSetNodeID object which is used to gather the bits. Just
39 /// call one of the Add* functions defined in the FoldingSetImpl::NodeID class.
40 /// NOTE: That the folding set does not own the nodes and it is the
41 /// responsibility of the user to dispose of the nodes.
44 /// class MyNode : public FoldingSetNode {
49 /// MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
51 /// void Profile(FoldingSetNodeID &ID) {
52 /// ID.AddString(Name);
53 /// ID.AddInteger(Value);
58 /// To define the folding set itself use the FoldingSet template;
61 /// FoldingSet<MyNode> MyFoldingSet;
63 /// Four public methods are available to manipulate the folding set;
65 /// 1) If you have an existing node that you want add to the set but unsure
66 /// that the node might already exist then call;
68 /// MyNode *M = MyFoldingSet.GetOrInsertNode(N);
70 /// If The result is equal to the input then the node has been inserted.
71 /// Otherwise, the result is the node existing in the folding set, and the
72 /// input can be discarded (use the result instead.)
74 /// 2) If you are ready to construct a node but want to check if it already
75 /// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
78 /// FoldingSetNodeID ID;
79 /// ID.AddString(Name);
80 /// ID.AddInteger(Value);
81 /// void *InsertPoint;
83 /// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
85 /// If found then M with be non-NULL, else InsertPoint will point to where it
86 /// should be inserted using InsertNode.
88 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
89 /// node with FindNodeOrInsertPos;
91 /// InsertNode(N, InsertPoint);
93 /// 4) Finally, if you want to remove a node from the folding set call;
95 /// bool WasRemoved = RemoveNode(N);
97 /// The result indicates whether the node existed in the folding set.
100 //===----------------------------------------------------------------------===//
101 /// FoldingSetImpl - Implements the folding set functionality. The main
102 /// structure is an array of buckets. Each bucket is indexed by the hash of
103 /// the nodes it contains. The bucket itself points to the nodes contained
104 /// in the bucket via a singly linked list. The last node in the list points
105 /// back to the bucket to facilitate node removal.
107 class FoldingSetImpl {
109 /// Buckets - Array of bucket chains.
113 /// NumBuckets - Length of the Buckets array. Always a power of 2.
117 /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
118 /// is greater than twice the number of buckets.
122 explicit FoldingSetImpl(unsigned Log2InitSize = 6);
123 virtual ~FoldingSetImpl();
125 // Forward declaration.
128 //===--------------------------------------------------------------------===//
129 /// NodeID - This class is used to gather all the unique data bits of a
130 /// node. When all the bits are gathered this class is used to produce a
131 /// hash value for the node.
134 /// Bits - Vector of all the data bits that make the node unique.
135 /// Use a SmallVector to avoid a heap allocation in the common case.
136 SmallVector<unsigned, 32> Bits;
141 /// getRawData - Return the ith entry in the Bits data.
143 unsigned getRawData(unsigned i) const {
147 /// Add* - Add various data types to Bit data.
149 void AddPointer(const void *Ptr);
150 void AddInteger(signed I);
151 void AddInteger(unsigned I);
152 void AddInteger(int64_t I);
153 void AddInteger(uint64_t I);
154 void AddFloat(float F);
155 void AddDouble(double D);
156 void AddString(const std::string &String);
158 /// ComputeHash - Compute a strong hash value for this NodeID, used to
159 /// lookup the node in the FoldingSetImpl.
160 unsigned ComputeHash() const;
162 /// operator== - Used to compare two nodes to each other.
164 bool operator==(const NodeID &RHS) const;
167 //===--------------------------------------------------------------------===//
168 /// Node - This class is used to maintain the singly linked bucket list in
173 // NextInFoldingSetBucket - next link in the bucket list.
174 void *NextInFoldingSetBucket;
178 Node() : NextInFoldingSetBucket(0) {}
181 void *getNextInBucket() const { return NextInFoldingSetBucket; }
182 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
185 /// RemoveNode - Remove a node from the folding set, returning true if one
186 /// was removed or false if the node was not in the folding set.
187 bool RemoveNode(Node *N);
189 /// GetOrInsertNode - If there is an existing simple Node exactly
190 /// equal to the specified node, return it. Otherwise, insert 'N' and return
192 Node *GetOrInsertNode(Node *N);
194 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
195 /// return it. If not, return the insertion token that will make insertion
197 Node *FindNodeOrInsertPos(const NodeID &ID, void *&InsertPos);
199 /// InsertNode - Insert the specified node into the folding set, knowing that
200 /// it is not already in the folding set. InsertPos must be obtained from
201 /// FindNodeOrInsertPos.
202 void InsertNode(Node *N, void *InsertPos);
206 /// GrowHashTable - Double the size of the hash table and rehash everything.
208 void GrowHashTable();
212 /// GetNodeProfile - Instantiations of the FoldingSet template implement
213 /// this function to gather data bits for the given node.
214 virtual void GetNodeProfile(NodeID &ID, Node *N) const = 0;
217 // Convenience types to hide the implementation of the folding set.
218 typedef FoldingSetImpl::Node FoldingSetNode;
219 typedef FoldingSetImpl::NodeID FoldingSetNodeID;
221 //===----------------------------------------------------------------------===//
222 /// FoldingSet - This template class is used to instantiate a specialized
223 /// implementation of the folding set to the node class T. T must be a
224 /// subclass of FoldingSetNode and implement a Profile function.
226 template<class T> class FoldingSet : public FoldingSetImpl {
228 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
229 /// way to convert nodes into a unique specifier.
230 virtual void GetNodeProfile(NodeID &ID, Node *N) const {
231 T *TN = static_cast<T *>(N);
236 explicit FoldingSet(unsigned Log2InitSize = 6)
237 : FoldingSetImpl(Log2InitSize)
240 /// GetOrInsertNode - If there is an existing simple Node exactly
241 /// equal to the specified node, return it. Otherwise, insert 'N' and
242 /// return it instead.
243 T *GetOrInsertNode(Node *N) {
244 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
247 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
248 /// return it. If not, return the insertion token that will make insertion
250 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
251 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
255 } // End of namespace llvm.