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(uint64_t I);
153 void AddFloat(float F);
154 void AddDouble(double D);
155 void AddString(const std::string &String);
157 /// ComputeHash - Compute a strong hash value for this NodeID, used to
158 /// lookup the node in the FoldingSetImpl.
159 unsigned ComputeHash() const;
161 /// operator== - Used to compare two nodes to each other.
163 bool operator==(const NodeID &RHS) const;
166 //===--------------------------------------------------------------------===//
167 /// Node - This class is used to maintain the singly linked bucket list in
172 // NextInFoldingSetBucket - next link in the bucket list.
173 void *NextInFoldingSetBucket;
177 Node() : NextInFoldingSetBucket(0) {}
180 void *getNextInBucket() const { return NextInFoldingSetBucket; }
181 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
184 /// RemoveNode - Remove a node from the folding set, returning true if one
185 /// was removed or false if the node was not in the folding set.
186 bool RemoveNode(Node *N);
188 /// GetOrInsertNode - If there is an existing simple Node exactly
189 /// equal to the specified node, return it. Otherwise, insert 'N' and return
191 Node *GetOrInsertNode(Node *N);
193 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
194 /// return it. If not, return the insertion token that will make insertion
196 Node *FindNodeOrInsertPos(const NodeID &ID, void *&InsertPos);
198 /// InsertNode - Insert the specified node into the folding set, knowing that
199 /// it is not already in the folding set. InsertPos must be obtained from
200 /// FindNodeOrInsertPos.
201 void InsertNode(Node *N, void *InsertPos);
205 /// GrowHashTable - Double the size of the hash table and rehash everything.
207 void GrowHashTable();
211 /// GetNodeProfile - Instantiations of the FoldingSet template implement
212 /// this function to gather data bits for the given node.
213 virtual void GetNodeProfile(NodeID &ID, Node *N) const = 0;
216 // Convenience types to hide the implementation of the folding set.
217 typedef FoldingSetImpl::Node FoldingSetNode;
218 typedef FoldingSetImpl::NodeID FoldingSetNodeID;
220 //===----------------------------------------------------------------------===//
221 /// FoldingSet - This template class is used to instantiate a specialized
222 /// implementation of the folding set to the node class T. T must be a
223 /// subclass of FoldingSetNode and implement a Profile function.
225 template<class T> class FoldingSet : public FoldingSetImpl {
227 /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
228 /// way to convert nodes into a unique specifier.
229 virtual void GetNodeProfile(NodeID &ID, Node *N) const {
230 T *TN = static_cast<T *>(N);
235 explicit FoldingSet(unsigned Log2InitSize = 6)
236 : FoldingSetImpl(Log2InitSize)
239 /// GetOrInsertNode - If there is an existing simple Node exactly
240 /// equal to the specified node, return it. Otherwise, insert 'N' and
241 /// return it instead.
242 T *GetOrInsertNode(Node *N) {
243 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
246 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists,
247 /// return it. If not, return the insertion token that will make insertion
249 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
250 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
254 } // End of namespace llvm.