FoldingSet.h revision 0e5af195f6c54dbf5a24a1ec12ed2d0bd02f5b7f
1//===-- llvm/ADT/FoldingSet.h - Uniquing Hash Set ---------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 7// 8//===----------------------------------------------------------------------===// 9// 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. 13// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_ADT_FOLDINGSET_H 17#define LLVM_ADT_FOLDINGSET_H 18 19#include "llvm/ADT/SmallVector.h" 20 21namespace llvm { 22 23/// This folding set used for two purposes: 24/// 1. Given information about a node we want to create, look up the unique 25/// instance of the node in the set. If the node already exists, return 26/// it, otherwise return the bucket it should be inserted into. 27/// 2. Given a node that has already been created, remove it from the set. 28/// 29/// This class is implemented as a single-link chained hash table, where the 30/// "buckets" are actually the nodes themselves (the next pointer is in the 31/// node). The last node points back to the bucket to simplified node removal. 32/// 33/// Any node that is to be included in the folding set must be a subclass of 34/// FoldingSetNode. The node class must also define a Profile method used to 35/// establish the unique bits of data for the node. The Profile method is 36/// passed a FoldingSetNodeID object which is used to gather the bits. Just 37/// call one of the Add* functions defined in the FoldingSetImpl::NodeID class. 38/// 39/// Eg. 40/// class MyNode : public FoldingSetNode { 41/// private: 42/// std::string Name; 43/// unsigned Value; 44/// public: 45/// MyNode(const char *N, unsigned V) : Name(N), Value(V) {} 46/// ... 47/// void Profile(FoldingSetNodeID &ID) { 48/// ID.AddString(Name); 49/// ID.AddInteger(Value); 50/// } 51/// ... 52/// }; 53/// 54/// To define the folding set itself use the FoldingSet template; 55/// 56/// Eg. 57/// FoldingSet<MyNode> MyFoldingSet; 58/// 59/// Four public methods are available to manipulate the folding set; 60/// 61/// 1) If you have an existing node that you want add to the set but unsure 62/// that the node might already exist then call; 63/// 64/// MyNode *M = MyFoldingSet.GetOrInsertNode(N); 65/// 66/// If The result is equal to the input then the node has been inserted. 67/// Otherwise, the result is the node existing in the folding set, and the 68/// input can be discarded (use the result instead.) 69/// 70/// 2) If you are ready to construct a node but want to check if it already 71/// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to 72/// check; 73/// 74/// FoldingSetNodeID ID; 75/// ID.AddString(Name); 76/// ID.AddInteger(Value); 77/// void *InsertPoint; 78/// 79/// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint); 80/// 81/// If found then M with be non-NULL, else InsertPoint will point to where it 82/// should be inserted using InsertNode. 83/// 84/// 3) If you get a NULL result from FindNodeOrInsertPos then you can ass a new 85/// node with FindNodeOrInsertPos; 86/// 87/// InsertNode(N, InsertPoint); 88/// 89/// 4) Finally, if you want to remove a node from the folding set call; 90/// 91/// bool WasRemoved = RemoveNode(N); 92/// 93/// The result indicates whether the node did exist in the folding set. 94 95 96//===----------------------------------------------------------------------===// 97/// FoldingSetImpl - Implements the folding set functionality. The main 98/// structure is an array of buckets. Each bucket is indexed by the hash of 99/// the nodes it contains. The bucket itself points to the nodes contained 100/// in the bucket via a singly linked list. The last node in the list points 101/// back to the bucket to facilitate node removal. 102/// 103class FoldingSetImpl { 104private: 105 // Buckets - Array of bucket chains. 106 void **Buckets; 107 108 // NumBuckets - Length of the Buckets array. Always a power of 2. 109 unsigned NumBuckets; 110 111 // NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes 112 // is greater than twice teh number of buckets. 113 unsigned NumNodes; 114 115public: 116 FoldingSetImpl(); 117 ~FoldingSetImpl(); 118 119 // Forward declaration. 120 class Node; 121 122 //===--------------------------------------------------------------------===// 123 /// NodeID - This class is used to gather all the unique data bits of a 124 /// node. When all the bits are gathered this class is used to produce a 125 /// hash value for the node. 126 /// 127 class NodeID { 128 /// Bits - Vector of all the data bits that make the node unique. 129 /// Use a SmallVector to avoid a heap allocation in the common case. 130 SmallVector<unsigned, 32> Bits; 131 132 public: 133 NodeID() {} 134 135 /// getRawData - Return the ith entry in the Bits data. 136 /// 137 unsigned getRawData(unsigned i) const { 138 return Bits[i]; 139 } 140 141 /// Add* - Add various data types to Bit data. 142 /// 143 void AddPointer(const void *Ptr); 144 void AddInteger(signed I); 145 void AddInteger(unsigned I); 146 void AddInteger(uint64_t I); 147 void AddFloat(float F); 148 void AddDouble(double D); 149 void AddString(const std::string &String); 150 151 /// ComputeHash - Compute a strong hash value for this NodeID, used to 152 /// lookup the node in the FoldingSetImpl. 153 unsigned ComputeHash() const; 154 155 /// operator== - Used to compare two nodes to each other. 156 /// 157 bool operator==(const NodeID &RHS) const; 158 }; 159 160 //===--------------------------------------------------------------------===// 161 /// Node - This class is used to maintain the singly linked bucket list in 162 /// a folding set. 163 /// 164 class Node { 165 private: 166 // nextInBucket - next linek in the bucket list. 167 void *nextInBucket; 168 169 public: 170 171 Node() : nextInBucket(0) {} 172 173 // Accessors 174 void *getNextInBucket() const { return nextInBucket; } 175 void SetNextInBucket(void *N) { nextInBucket = N; } 176 }; 177 178 /// RemoveNode - Remove a node from the folding set, returning true if one 179 /// was removed or false if the node was not in the folding set. 180 bool RemoveNode(Node *N); 181 182 /// GetOrInsertNode - If there is an existing simple Node exactly 183 /// equal to the specified node, return it. Otherwise, insert 'N' and return 184 /// it instead. 185 Node *GetOrInsertNode(Node *N); 186 187 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists, 188 /// return it. If not, return the insertion token that will make insertion 189 /// faster. 190 Node *FindNodeOrInsertPos(const NodeID &ID, void *&InsertPos); 191 192 /// InsertNode - Insert the specified node into the folding set, knowing that 193 /// it is not already in the folding set. InsertPos must be obtained from 194 /// FindNodeOrInsertPos. 195 void InsertNode(Node *N, void *InsertPos); 196 197 private: 198 /// GetNextPtr - In order to save space, each bucket is a 199 /// singly-linked-list. In order to make deletion more efficient, we make 200 /// the list circular, so we can delete a node without computing its hash. 201 /// The problem with this is that the start of the hash buckets are not 202 /// Nodes. If NextInBucketPtr is a bucket pointer, this method returns null 203 /// : use GetBucketPtr when this happens. 204 Node *GetNextPtr(void *NextInBucketPtr); 205 206 /// GetNextPtr - This is just like the previous GetNextPtr implementation, 207 /// but allows a bucket array to be specified. 208 Node *GetNextPtr(void *NextInBucketPtr, void **Buckets, unsigned NumBuck); 209 210 /// GetBucketPtr - Provides a casting of a bucket pointer for isNode 211 /// testing. 212 void **GetBucketPtr(void *NextInBucketPtr); 213 214 /// GetBucketFor - Hash the specified node ID and return the hash bucket for 215 /// the specified ID. 216 void **GetBucketFor(const NodeID &ID) const; 217 218 /// GrowHashTable - Double the size of the hash table and rehash everything. 219 /// 220 void GrowHashTable(); 221 222 protected: 223 224 /// GetNodeProfile - Instantiations of the FoldingSet template implement 225 /// this function to gather data bits for teh given node. 226 virtual void GetNodeProfile(NodeID &ID, Node *N) = 0; 227 }; 228 229 // Convenence types to hide the implementation of the folding set. 230 typedef FoldingSetImpl::Node FoldingSetNode; 231 typedef FoldingSetImpl::NodeID FoldingSetNodeID; 232 233 //===--------------------------------------------------------------------===// 234 /// FoldingSet - This template class is used to instantiate a specialized 235 /// implementation of the folding set to the node class T. T must be a 236 /// subclass of FoldingSetNode and implement a Profile function. 237 /// 238 template<class T> class FoldingSet : public FoldingSetImpl { 239 private: 240 /// GetNodeProfile - Each instantiatation of the FoldingSet 241 virtual void GetNodeProfile(NodeID &ID, Node *N) { 242 T *TN = static_cast<T *>(N); 243 TN->Profile(ID); 244 } 245 246 public: 247 /// RemoveNode - Remove a node from the folding set, returning true if one 248 /// was removed or false if the node was not in the folding set. 249 bool RemoveNode(T *N) { 250 return FoldingSetImpl::RemoveNode(static_cast<FoldingSetNode *>(N)); 251 } 252 253 /// GetOrInsertNode - If there is an existing simple Node exactly 254 /// equal to the specified node, return it. Otherwise, insert 'N' and 255 /// return it instead. 256 T *GetOrInsertNode(Node *N) { 257 return static_cast<T *>(FoldingSetImpl::GetOrInsertNode( 258 static_cast<FoldingSetNode *>(N))); 259 } 260 261 /// FindNodeOrInsertPos - Look up the node specified by ID. If it exists, 262 /// return it. If not, return the insertion token that will make insertion 263 /// faster. 264 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) { 265 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, 266 InsertPos)); 267 } 268 269 /// InsertNode - Insert the specified node into the folding set, knowing 270 /// that it is not already in the folding set. InsertPos must be obtained 271 /// from FindNodeOrInsertPos. 272 void InsertNode(T *N, void *InsertPos) { 273 FoldingSetImpl::InsertNode(static_cast<FoldingSetNode *>(N), InsertPos); 274 } 275 }; 276 277}; // End of namespace llvm. 278 279 280#endif 281 282