FoldingSet.h revision 32f3bd43dbf4082ed05478c6ef69727925ab1646
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/Support/DataTypes.h" 20#include "llvm/ADT/SmallVector.h" 21#include <string> 22 23namespace llvm { 24 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. 30/// 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. 34/// 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. 42/// 43/// Eg. 44/// class MyNode : public FoldingSetNode { 45/// private: 46/// std::string Name; 47/// unsigned Value; 48/// public: 49/// MyNode(const char *N, unsigned V) : Name(N), Value(V) {} 50/// ... 51/// void Profile(FoldingSetNodeID &ID) { 52/// ID.AddString(Name); 53/// ID.AddInteger(Value); 54/// } 55/// ... 56/// }; 57/// 58/// To define the folding set itself use the FoldingSet template; 59/// 60/// Eg. 61/// FoldingSet<MyNode> MyFoldingSet; 62/// 63/// Four public methods are available to manipulate the folding set; 64/// 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; 67/// 68/// MyNode *M = MyFoldingSet.GetOrInsertNode(N); 69/// 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.) 73/// 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 76/// check; 77/// 78/// FoldingSetNodeID ID; 79/// ID.AddString(Name); 80/// ID.AddInteger(Value); 81/// void *InsertPoint; 82/// 83/// MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint); 84/// 85/// If found then M with be non-NULL, else InsertPoint will point to where it 86/// should be inserted using InsertNode. 87/// 88/// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new 89/// node with FindNodeOrInsertPos; 90/// 91/// InsertNode(N, InsertPoint); 92/// 93/// 4) Finally, if you want to remove a node from the folding set call; 94/// 95/// bool WasRemoved = RemoveNode(N); 96/// 97/// The result indicates whether the node existed in the folding set. 98 99 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. 106/// 107class FoldingSetImpl { 108private: 109 /// Buckets - Array of bucket chains. 110 /// 111 void **Buckets; 112 113 /// NumBuckets - Length of the Buckets array. Always a power of 2. 114 /// 115 unsigned NumBuckets; 116 117 /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes 118 /// is greater than twice the number of buckets. 119 unsigned NumNodes; 120 121public: 122 FoldingSetImpl(unsigned Log2InitSize = 6); 123 virtual ~FoldingSetImpl(); 124 125 // Forward declaration. 126 class Node; 127 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. 132 /// 133 class NodeID { 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; 137 138 public: 139 NodeID() {} 140 141 /// getRawData - Return the ith entry in the Bits data. 142 /// 143 unsigned getRawData(unsigned i) const { 144 return Bits[i]; 145 } 146 147 /// Add* - Add various data types to Bit data. 148 /// 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); 156 157 /// ComputeHash - Compute a strong hash value for this NodeID, used to 158 /// lookup the node in the FoldingSetImpl. 159 unsigned ComputeHash() const; 160 161 /// operator== - Used to compare two nodes to each other. 162 /// 163 bool operator==(const NodeID &RHS) const; 164 }; 165 166 //===--------------------------------------------------------------------===// 167 /// Node - This class is used to maintain the singly linked bucket list in 168 /// a folding set. 169 /// 170 class Node { 171 private: 172 // NextInFoldingSetBucket - next link in the bucket list. 173 void *NextInFoldingSetBucket; 174 175 public: 176 177 Node() : NextInFoldingSetBucket(0) {} 178 179 // Accessors 180 void *getNextInBucket() const { return NextInFoldingSetBucket; } 181 void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; } 182 }; 183 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); 187 188 /// GetOrInsertNode - If there is an existing simple Node exactly 189 /// equal to the specified node, return it. Otherwise, insert 'N' and return 190 /// it instead. 191 Node *GetOrInsertNode(Node *N); 192 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 195 /// faster. 196 Node *FindNodeOrInsertPos(const NodeID &ID, void *&InsertPos); 197 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); 202 203private: 204 205 /// GrowHashTable - Double the size of the hash table and rehash everything. 206 /// 207 void GrowHashTable(); 208 209protected: 210 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; 214}; 215 216// Convenience types to hide the implementation of the folding set. 217typedef FoldingSetImpl::Node FoldingSetNode; 218typedef FoldingSetImpl::NodeID FoldingSetNodeID; 219 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. 224/// 225template<class T> class FoldingSet : public FoldingSetImpl { 226private: 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); 231 TN->Profile(ID); 232 } 233 234public: 235 FoldingSet(unsigned Log2InitSize = 6) 236 : FoldingSetImpl(Log2InitSize) 237 {} 238 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)); 244 } 245 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 248 /// faster. 249 T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) { 250 return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos)); 251 } 252}; 253 254} // End of namespace llvm. 255 256 257#endif 258 259