LLVM API Documentation

FoldingSet.h
Go to the documentation of this file.
00001 //===-- llvm/ADT/FoldingSet.h - Uniquing Hash Set ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a hash set that can be used to remove duplication of nodes
00011 // in a graph.  This code was originally created by Chris Lattner for use with
00012 // SelectionDAGCSEMap, but was isolated to provide use across the llvm code set.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_ADT_FOLDINGSET_H
00017 #define LLVM_ADT_FOLDINGSET_H
00018 
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/ADT/StringRef.h"
00021 #include "llvm/Support/DataTypes.h"
00022 
00023 namespace llvm {
00024   class APFloat;
00025   class APInt;
00026   class BumpPtrAllocator;
00027 
00028 /// This folding set used for two purposes:
00029 ///   1. Given information about a node we want to create, look up the unique
00030 ///      instance of the node in the set.  If the node already exists, return
00031 ///      it, otherwise return the bucket it should be inserted into.
00032 ///   2. Given a node that has already been created, remove it from the set.
00033 ///
00034 /// This class is implemented as a single-link chained hash table, where the
00035 /// "buckets" are actually the nodes themselves (the next pointer is in the
00036 /// node).  The last node points back to the bucket to simplify node removal.
00037 ///
00038 /// Any node that is to be included in the folding set must be a subclass of
00039 /// FoldingSetNode.  The node class must also define a Profile method used to
00040 /// establish the unique bits of data for the node.  The Profile method is
00041 /// passed a FoldingSetNodeID object which is used to gather the bits.  Just
00042 /// call one of the Add* functions defined in the FoldingSetImpl::NodeID class.
00043 /// NOTE: That the folding set does not own the nodes and it is the
00044 /// responsibility of the user to dispose of the nodes.
00045 ///
00046 /// Eg.
00047 ///    class MyNode : public FoldingSetNode {
00048 ///    private:
00049 ///      std::string Name;
00050 ///      unsigned Value;
00051 ///    public:
00052 ///      MyNode(const char *N, unsigned V) : Name(N), Value(V) {}
00053 ///       ...
00054 ///      void Profile(FoldingSetNodeID &ID) const {
00055 ///        ID.AddString(Name);
00056 ///        ID.AddInteger(Value);
00057 ///      }
00058 ///      ...
00059 ///    };
00060 ///
00061 /// To define the folding set itself use the FoldingSet template;
00062 ///
00063 /// Eg.
00064 ///    FoldingSet<MyNode> MyFoldingSet;
00065 ///
00066 /// Four public methods are available to manipulate the folding set;
00067 ///
00068 /// 1) If you have an existing node that you want add to the set but unsure
00069 /// that the node might already exist then call;
00070 ///
00071 ///    MyNode *M = MyFoldingSet.GetOrInsertNode(N);
00072 ///
00073 /// If The result is equal to the input then the node has been inserted.
00074 /// Otherwise, the result is the node existing in the folding set, and the
00075 /// input can be discarded (use the result instead.)
00076 ///
00077 /// 2) If you are ready to construct a node but want to check if it already
00078 /// exists, then call FindNodeOrInsertPos with a FoldingSetNodeID of the bits to
00079 /// check;
00080 ///
00081 ///   FoldingSetNodeID ID;
00082 ///   ID.AddString(Name);
00083 ///   ID.AddInteger(Value);
00084 ///   void *InsertPoint;
00085 ///
00086 ///    MyNode *M = MyFoldingSet.FindNodeOrInsertPos(ID, InsertPoint);
00087 ///
00088 /// If found then M with be non-NULL, else InsertPoint will point to where it
00089 /// should be inserted using InsertNode.
00090 ///
00091 /// 3) If you get a NULL result from FindNodeOrInsertPos then you can as a new
00092 /// node with FindNodeOrInsertPos;
00093 ///
00094 ///    InsertNode(N, InsertPoint);
00095 ///
00096 /// 4) Finally, if you want to remove a node from the folding set call;
00097 ///
00098 ///    bool WasRemoved = RemoveNode(N);
00099 ///
00100 /// The result indicates whether the node existed in the folding set.
00101 
00102 class FoldingSetNodeID;
00103 
00104 //===----------------------------------------------------------------------===//
00105 /// FoldingSetImpl - Implements the folding set functionality.  The main
00106 /// structure is an array of buckets.  Each bucket is indexed by the hash of
00107 /// the nodes it contains.  The bucket itself points to the nodes contained
00108 /// in the bucket via a singly linked list.  The last node in the list points
00109 /// back to the bucket to facilitate node removal.
00110 ///
00111 class FoldingSetImpl {
00112 protected:
00113   /// Buckets - Array of bucket chains.
00114   ///
00115   void **Buckets;
00116 
00117   /// NumBuckets - Length of the Buckets array.  Always a power of 2.
00118   ///
00119   unsigned NumBuckets;
00120 
00121   /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
00122   /// is greater than twice the number of buckets.
00123   unsigned NumNodes;
00124 
00125 public:
00126   explicit FoldingSetImpl(unsigned Log2InitSize = 6);
00127   virtual ~FoldingSetImpl();
00128 
00129   //===--------------------------------------------------------------------===//
00130   /// Node - This class is used to maintain the singly linked bucket list in
00131   /// a folding set.
00132   ///
00133   class Node {
00134   private:
00135     // NextInFoldingSetBucket - next link in the bucket list.
00136     void *NextInFoldingSetBucket;
00137 
00138   public:
00139 
00140     Node() : NextInFoldingSetBucket(0) {}
00141 
00142     // Accessors
00143     void *getNextInBucket() const { return NextInFoldingSetBucket; }
00144     void SetNextInBucket(void *N) { NextInFoldingSetBucket = N; }
00145   };
00146 
00147   /// clear - Remove all nodes from the folding set.
00148   void clear();
00149 
00150   /// RemoveNode - Remove a node from the folding set, returning true if one
00151   /// was removed or false if the node was not in the folding set.
00152   bool RemoveNode(Node *N);
00153 
00154   /// GetOrInsertNode - If there is an existing simple Node exactly
00155   /// equal to the specified node, return it.  Otherwise, insert 'N' and return
00156   /// it instead.
00157   Node *GetOrInsertNode(Node *N);
00158 
00159   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
00160   /// return it.  If not, return the insertion token that will make insertion
00161   /// faster.
00162   Node *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos);
00163 
00164   /// InsertNode - Insert the specified node into the folding set, knowing that
00165   /// it is not already in the folding set.  InsertPos must be obtained from
00166   /// FindNodeOrInsertPos.
00167   void InsertNode(Node *N, void *InsertPos);
00168 
00169   /// InsertNode - Insert the specified node into the folding set, knowing that
00170   /// it is not already in the folding set.
00171   void InsertNode(Node *N) {
00172     Node *Inserted = GetOrInsertNode(N);
00173     (void)Inserted;
00174     assert(Inserted == N && "Node already inserted!");
00175   }
00176 
00177   /// size - Returns the number of nodes in the folding set.
00178   unsigned size() const { return NumNodes; }
00179 
00180   /// empty - Returns true if there are no nodes in the folding set.
00181   bool empty() const { return NumNodes == 0; }
00182 
00183 private:
00184 
00185   /// GrowHashTable - Double the size of the hash table and rehash everything.
00186   ///
00187   void GrowHashTable();
00188 
00189 protected:
00190 
00191   /// GetNodeProfile - Instantiations of the FoldingSet template implement
00192   /// this function to gather data bits for the given node.
00193   virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const = 0;
00194   /// NodeEquals - Instantiations of the FoldingSet template implement
00195   /// this function to compare the given node with the given ID.
00196   virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
00197                           FoldingSetNodeID &TempID) const=0;
00198   /// ComputeNodeHash - Instantiations of the FoldingSet template implement
00199   /// this function to compute a hash value for the given node.
00200   virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const = 0;
00201 };
00202 
00203 //===----------------------------------------------------------------------===//
00204 
00205 template<typename T> struct FoldingSetTrait;
00206 
00207 /// DefaultFoldingSetTrait - This class provides default implementations
00208 /// for FoldingSetTrait implementations.
00209 ///
00210 template<typename T> struct DefaultFoldingSetTrait {
00211   static void Profile(const T &X, FoldingSetNodeID &ID) {
00212     X.Profile(ID);
00213   }
00214   static void Profile(T &X, FoldingSetNodeID &ID) {
00215     X.Profile(ID);
00216   }
00217 
00218   // Equals - Test if the profile for X would match ID, using TempID
00219   // to compute a temporary ID if necessary. The default implementation
00220   // just calls Profile and does a regular comparison. Implementations
00221   // can override this to provide more efficient implementations.
00222   static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
00223                             FoldingSetNodeID &TempID);
00224 
00225   // ComputeHash - Compute a hash value for X, using TempID to
00226   // compute a temporary ID if necessary. The default implementation
00227   // just calls Profile and does a regular hash computation.
00228   // Implementations can override this to provide more efficient
00229   // implementations.
00230   static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID);
00231 };
00232 
00233 /// FoldingSetTrait - This trait class is used to define behavior of how
00234 /// to "profile" (in the FoldingSet parlance) an object of a given type.
00235 /// The default behavior is to invoke a 'Profile' method on an object, but
00236 /// through template specialization the behavior can be tailored for specific
00237 /// types.  Combined with the FoldingSetNodeWrapper class, one can add objects
00238 /// to FoldingSets that were not originally designed to have that behavior.
00239 template<typename T> struct FoldingSetTrait
00240   : public DefaultFoldingSetTrait<T> {};
00241 
00242 template<typename T, typename Ctx> struct ContextualFoldingSetTrait;
00243 
00244 /// DefaultContextualFoldingSetTrait - Like DefaultFoldingSetTrait, but
00245 /// for ContextualFoldingSets.
00246 template<typename T, typename Ctx>
00247 struct DefaultContextualFoldingSetTrait {
00248   static void Profile(T &X, FoldingSetNodeID &ID, Ctx Context) {
00249     X.Profile(ID, Context);
00250   }
00251   static inline bool Equals(T &X, const FoldingSetNodeID &ID, unsigned IDHash,
00252                             FoldingSetNodeID &TempID, Ctx Context);
00253   static inline unsigned ComputeHash(T &X, FoldingSetNodeID &TempID,
00254                                      Ctx Context);
00255 };
00256 
00257 /// ContextualFoldingSetTrait - Like FoldingSetTrait, but for
00258 /// ContextualFoldingSets.
00259 template<typename T, typename Ctx> struct ContextualFoldingSetTrait
00260   : public DefaultContextualFoldingSetTrait<T, Ctx> {};
00261 
00262 //===--------------------------------------------------------------------===//
00263 /// FoldingSetNodeIDRef - This class describes a reference to an interned
00264 /// FoldingSetNodeID, which can be a useful to store node id data rather
00265 /// than using plain FoldingSetNodeIDs, since the 32-element SmallVector
00266 /// is often much larger than necessary, and the possibility of heap
00267 /// allocation means it requires a non-trivial destructor call.
00268 class FoldingSetNodeIDRef {
00269   const unsigned *Data;
00270   size_t Size;
00271 public:
00272   FoldingSetNodeIDRef() : Data(0), Size(0) {}
00273   FoldingSetNodeIDRef(const unsigned *D, size_t S) : Data(D), Size(S) {}
00274 
00275   /// ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef,
00276   /// used to lookup the node in the FoldingSetImpl.
00277   unsigned ComputeHash() const;
00278 
00279   bool operator==(FoldingSetNodeIDRef) const;
00280 
00281   /// Used to compare the "ordering" of two nodes as defined by the
00282   /// profiled bits and their ordering defined by memcmp().
00283   bool operator<(FoldingSetNodeIDRef) const;
00284 
00285   const unsigned *getData() const { return Data; }
00286   size_t getSize() const { return Size; }
00287 };
00288 
00289 //===--------------------------------------------------------------------===//
00290 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
00291 /// a node.  When all the bits are gathered this class is used to produce a
00292 /// hash value for the node.
00293 ///
00294 class FoldingSetNodeID {
00295   /// Bits - Vector of all the data bits that make the node unique.
00296   /// Use a SmallVector to avoid a heap allocation in the common case.
00297   SmallVector<unsigned, 32> Bits;
00298 
00299 public:
00300   FoldingSetNodeID() {}
00301 
00302   FoldingSetNodeID(FoldingSetNodeIDRef Ref)
00303     : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
00304 
00305   /// Add* - Add various data types to Bit data.
00306   ///
00307   void AddPointer(const void *Ptr);
00308   void AddInteger(signed I);
00309   void AddInteger(unsigned I);
00310   void AddInteger(long I);
00311   void AddInteger(unsigned long I);
00312   void AddInteger(long long I);
00313   void AddInteger(unsigned long long I);
00314   void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
00315   void AddString(StringRef String);
00316   void AddNodeID(const FoldingSetNodeID &ID);
00317 
00318   template <typename T>
00319   inline void Add(const T &x) { FoldingSetTrait<T>::Profile(x, *this); }
00320 
00321   /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
00322   /// object to be used to compute a new profile.
00323   inline void clear() { Bits.clear(); }
00324 
00325   /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
00326   /// to lookup the node in the FoldingSetImpl.
00327   unsigned ComputeHash() const;
00328 
00329   /// operator== - Used to compare two nodes to each other.
00330   ///
00331   bool operator==(const FoldingSetNodeID &RHS) const;
00332   bool operator==(const FoldingSetNodeIDRef RHS) const;
00333 
00334   /// Used to compare the "ordering" of two nodes as defined by the
00335   /// profiled bits and their ordering defined by memcmp().
00336   bool operator<(const FoldingSetNodeID &RHS) const;
00337   bool operator<(const FoldingSetNodeIDRef RHS) const;
00338 
00339   /// Intern - Copy this node's data to a memory region allocated from the
00340   /// given allocator and return a FoldingSetNodeIDRef describing the
00341   /// interned data.
00342   FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
00343 };
00344 
00345 // Convenience type to hide the implementation of the folding set.
00346 typedef FoldingSetImpl::Node FoldingSetNode;
00347 template<class T> class FoldingSetIterator;
00348 template<class T> class FoldingSetBucketIterator;
00349 
00350 // Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
00351 // require the definition of FoldingSetNodeID.
00352 template<typename T>
00353 inline bool
00354 DefaultFoldingSetTrait<T>::Equals(T &X, const FoldingSetNodeID &ID,
00355                                   unsigned /*IDHash*/,
00356                                   FoldingSetNodeID &TempID) {
00357   FoldingSetTrait<T>::Profile(X, TempID);
00358   return TempID == ID;
00359 }
00360 template<typename T>
00361 inline unsigned
00362 DefaultFoldingSetTrait<T>::ComputeHash(T &X, FoldingSetNodeID &TempID) {
00363   FoldingSetTrait<T>::Profile(X, TempID);
00364   return TempID.ComputeHash();
00365 }
00366 template<typename T, typename Ctx>
00367 inline bool
00368 DefaultContextualFoldingSetTrait<T, Ctx>::Equals(T &X,
00369                                                  const FoldingSetNodeID &ID,
00370                                                  unsigned /*IDHash*/,
00371                                                  FoldingSetNodeID &TempID,
00372                                                  Ctx Context) {
00373   ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
00374   return TempID == ID;
00375 }
00376 template<typename T, typename Ctx>
00377 inline unsigned
00378 DefaultContextualFoldingSetTrait<T, Ctx>::ComputeHash(T &X,
00379                                                       FoldingSetNodeID &TempID,
00380                                                       Ctx Context) {
00381   ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
00382   return TempID.ComputeHash();
00383 }
00384 
00385 //===----------------------------------------------------------------------===//
00386 /// FoldingSet - This template class is used to instantiate a specialized
00387 /// implementation of the folding set to the node class T.  T must be a
00388 /// subclass of FoldingSetNode and implement a Profile function.
00389 ///
00390 template<class T> class FoldingSet : public FoldingSetImpl {
00391 private:
00392   /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
00393   /// way to convert nodes into a unique specifier.
00394   virtual void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const {
00395     T *TN = static_cast<T *>(N);
00396     FoldingSetTrait<T>::Profile(*TN, ID);
00397   }
00398   /// NodeEquals - Instantiations may optionally provide a way to compare a
00399   /// node with a specified ID.
00400   virtual bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
00401                           FoldingSetNodeID &TempID) const {
00402     T *TN = static_cast<T *>(N);
00403     return FoldingSetTrait<T>::Equals(*TN, ID, IDHash, TempID);
00404   }
00405   /// ComputeNodeHash - Instantiations may optionally provide a way to compute a
00406   /// hash value directly from a node.
00407   virtual unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const {
00408     T *TN = static_cast<T *>(N);
00409     return FoldingSetTrait<T>::ComputeHash(*TN, TempID);
00410   }
00411 
00412 public:
00413   explicit FoldingSet(unsigned Log2InitSize = 6)
00414   : FoldingSetImpl(Log2InitSize)
00415   {}
00416 
00417   typedef FoldingSetIterator<T> iterator;
00418   iterator begin() { return iterator(Buckets); }
00419   iterator end() { return iterator(Buckets+NumBuckets); }
00420 
00421   typedef FoldingSetIterator<const T> const_iterator;
00422   const_iterator begin() const { return const_iterator(Buckets); }
00423   const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
00424 
00425   typedef FoldingSetBucketIterator<T> bucket_iterator;
00426 
00427   bucket_iterator bucket_begin(unsigned hash) {
00428     return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
00429   }
00430 
00431   bucket_iterator bucket_end(unsigned hash) {
00432     return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
00433   }
00434 
00435   /// GetOrInsertNode - If there is an existing simple Node exactly
00436   /// equal to the specified node, return it.  Otherwise, insert 'N' and
00437   /// return it instead.
00438   T *GetOrInsertNode(Node *N) {
00439     return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
00440   }
00441 
00442   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
00443   /// return it.  If not, return the insertion token that will make insertion
00444   /// faster.
00445   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
00446     return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
00447   }
00448 };
00449 
00450 //===----------------------------------------------------------------------===//
00451 /// ContextualFoldingSet - This template class is a further refinement
00452 /// of FoldingSet which provides a context argument when calling
00453 /// Profile on its nodes.  Currently, that argument is fixed at
00454 /// initialization time.
00455 ///
00456 /// T must be a subclass of FoldingSetNode and implement a Profile
00457 /// function with signature
00458 ///   void Profile(llvm::FoldingSetNodeID &, Ctx);
00459 template <class T, class Ctx>
00460 class ContextualFoldingSet : public FoldingSetImpl {
00461   // Unfortunately, this can't derive from FoldingSet<T> because the
00462   // construction vtable for FoldingSet<T> requires
00463   // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
00464   // requires a single-argument T::Profile().
00465 
00466 private:
00467   Ctx Context;
00468 
00469   /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
00470   /// way to convert nodes into a unique specifier.
00471   virtual void GetNodeProfile(FoldingSetImpl::Node *N,
00472                               FoldingSetNodeID &ID) const {
00473     T *TN = static_cast<T *>(N);
00474     ContextualFoldingSetTrait<T, Ctx>::Profile(*TN, ID, Context);
00475   }
00476   virtual bool NodeEquals(FoldingSetImpl::Node *N,
00477                           const FoldingSetNodeID &ID, unsigned IDHash,
00478                           FoldingSetNodeID &TempID) const {
00479     T *TN = static_cast<T *>(N);
00480     return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
00481                                                      Context);
00482   }
00483   virtual unsigned ComputeNodeHash(FoldingSetImpl::Node *N,
00484                                    FoldingSetNodeID &TempID) const {
00485     T *TN = static_cast<T *>(N);
00486     return ContextualFoldingSetTrait<T, Ctx>::ComputeHash(*TN, TempID, Context);
00487   }
00488 
00489 public:
00490   explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
00491   : FoldingSetImpl(Log2InitSize), Context(Context)
00492   {}
00493 
00494   Ctx getContext() const { return Context; }
00495 
00496 
00497   typedef FoldingSetIterator<T> iterator;
00498   iterator begin() { return iterator(Buckets); }
00499   iterator end() { return iterator(Buckets+NumBuckets); }
00500 
00501   typedef FoldingSetIterator<const T> const_iterator;
00502   const_iterator begin() const { return const_iterator(Buckets); }
00503   const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
00504 
00505   typedef FoldingSetBucketIterator<T> bucket_iterator;
00506 
00507   bucket_iterator bucket_begin(unsigned hash) {
00508     return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
00509   }
00510 
00511   bucket_iterator bucket_end(unsigned hash) {
00512     return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
00513   }
00514 
00515   /// GetOrInsertNode - If there is an existing simple Node exactly
00516   /// equal to the specified node, return it.  Otherwise, insert 'N'
00517   /// and return it instead.
00518   T *GetOrInsertNode(Node *N) {
00519     return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
00520   }
00521 
00522   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it
00523   /// exists, return it.  If not, return the insertion token that will
00524   /// make insertion faster.
00525   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
00526     return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
00527   }
00528 };
00529 
00530 //===----------------------------------------------------------------------===//
00531 /// FoldingSetVectorIterator - This implements an iterator for
00532 /// FoldingSetVector. It is only necessary because FoldingSetIterator provides
00533 /// a value_type of T, while the vector in FoldingSetVector exposes
00534 /// a value_type of T*. Fortunately, FoldingSetIterator doesn't expose very
00535 /// much besides operator* and operator->, so we just wrap the inner vector
00536 /// iterator and perform the extra dereference.
00537 template <class T, class VectorIteratorT>
00538 class FoldingSetVectorIterator {
00539   // Provide a typedef to workaround the lack of correct injected class name
00540   // support in older GCCs.
00541   typedef FoldingSetVectorIterator<T, VectorIteratorT> SelfT;
00542 
00543   VectorIteratorT Iterator;
00544 
00545 public:
00546   FoldingSetVectorIterator(VectorIteratorT I) : Iterator(I) {}
00547 
00548   bool operator==(const SelfT &RHS) const {
00549     return Iterator == RHS.Iterator;
00550   }
00551   bool operator!=(const SelfT &RHS) const {
00552     return Iterator != RHS.Iterator;
00553   }
00554 
00555   T &operator*() const { return **Iterator; }
00556 
00557   T *operator->() const { return *Iterator; }
00558 
00559   inline SelfT &operator++() {
00560     ++Iterator;
00561     return *this;
00562   }
00563   SelfT operator++(int) {
00564     SelfT tmp = *this;
00565     ++*this;
00566     return tmp;
00567   }
00568 };
00569 
00570 //===----------------------------------------------------------------------===//
00571 /// FoldingSetVector - This template class combines a FoldingSet and a vector
00572 /// to provide the interface of FoldingSet but with deterministic iteration
00573 /// order based on the insertion order. T must be a subclass of FoldingSetNode
00574 /// and implement a Profile function.
00575 template <class T, class VectorT = SmallVector<T*, 8> >
00576 class FoldingSetVector {
00577   FoldingSet<T> Set;
00578   VectorT Vector;
00579 
00580 public:
00581   explicit FoldingSetVector(unsigned Log2InitSize = 6)
00582       : Set(Log2InitSize) {
00583   }
00584 
00585   typedef FoldingSetVectorIterator<T, typename VectorT::iterator> iterator;
00586   iterator begin() { return Vector.begin(); }
00587   iterator end()   { return Vector.end(); }
00588 
00589   typedef FoldingSetVectorIterator<const T, typename VectorT::const_iterator>
00590     const_iterator;
00591   const_iterator begin() const { return Vector.begin(); }
00592   const_iterator end()   const { return Vector.end(); }
00593 
00594   /// clear - Remove all nodes from the folding set.
00595   void clear() { Set.clear(); Vector.clear(); }
00596 
00597   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
00598   /// return it.  If not, return the insertion token that will make insertion
00599   /// faster.
00600   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
00601     return Set.FindNodeOrInsertPos(ID, InsertPos);
00602   }
00603 
00604   /// GetOrInsertNode - If there is an existing simple Node exactly
00605   /// equal to the specified node, return it.  Otherwise, insert 'N' and
00606   /// return it instead.
00607   T *GetOrInsertNode(T *N) {
00608     T *Result = Set.GetOrInsertNode(N);
00609     if (Result == N) Vector.push_back(N);
00610     return Result;
00611   }
00612 
00613   /// InsertNode - Insert the specified node into the folding set, knowing that
00614   /// it is not already in the folding set.  InsertPos must be obtained from
00615   /// FindNodeOrInsertPos.
00616   void InsertNode(T *N, void *InsertPos) {
00617     Set.InsertNode(N, InsertPos);
00618     Vector.push_back(N);
00619   }
00620 
00621   /// InsertNode - Insert the specified node into the folding set, knowing that
00622   /// it is not already in the folding set.
00623   void InsertNode(T *N) {
00624     Set.InsertNode(N);
00625     Vector.push_back(N);
00626   }
00627 
00628   /// size - Returns the number of nodes in the folding set.
00629   unsigned size() const { return Set.size(); }
00630 
00631   /// empty - Returns true if there are no nodes in the folding set.
00632   bool empty() const { return Set.empty(); }
00633 };
00634 
00635 //===----------------------------------------------------------------------===//
00636 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
00637 /// folding sets, which knows how to walk the folding set hash table.
00638 class FoldingSetIteratorImpl {
00639 protected:
00640   FoldingSetNode *NodePtr;
00641   FoldingSetIteratorImpl(void **Bucket);
00642   void advance();
00643 
00644 public:
00645   bool operator==(const FoldingSetIteratorImpl &RHS) const {
00646     return NodePtr == RHS.NodePtr;
00647   }
00648   bool operator!=(const FoldingSetIteratorImpl &RHS) const {
00649     return NodePtr != RHS.NodePtr;
00650   }
00651 };
00652 
00653 
00654 template<class T>
00655 class FoldingSetIterator : public FoldingSetIteratorImpl {
00656 public:
00657   explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
00658 
00659   T &operator*() const {
00660     return *static_cast<T*>(NodePtr);
00661   }
00662 
00663   T *operator->() const {
00664     return static_cast<T*>(NodePtr);
00665   }
00666 
00667   inline FoldingSetIterator &operator++() {          // Preincrement
00668     advance();
00669     return *this;
00670   }
00671   FoldingSetIterator operator++(int) {        // Postincrement
00672     FoldingSetIterator tmp = *this; ++*this; return tmp;
00673   }
00674 };
00675 
00676 //===----------------------------------------------------------------------===//
00677 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
00678 /// shared by all folding sets, which knows how to walk a particular bucket
00679 /// of a folding set hash table.
00680 
00681 class FoldingSetBucketIteratorImpl {
00682 protected:
00683   void *Ptr;
00684 
00685   explicit FoldingSetBucketIteratorImpl(void **Bucket);
00686 
00687   FoldingSetBucketIteratorImpl(void **Bucket, bool)
00688     : Ptr(Bucket) {}
00689 
00690   void advance() {
00691     void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
00692     uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
00693     Ptr = reinterpret_cast<void*>(x);
00694   }
00695 
00696 public:
00697   bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
00698     return Ptr == RHS.Ptr;
00699   }
00700   bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
00701     return Ptr != RHS.Ptr;
00702   }
00703 };
00704 
00705 
00706 template<class T>
00707 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
00708 public:
00709   explicit FoldingSetBucketIterator(void **Bucket) :
00710     FoldingSetBucketIteratorImpl(Bucket) {}
00711 
00712   FoldingSetBucketIterator(void **Bucket, bool) :
00713     FoldingSetBucketIteratorImpl(Bucket, true) {}
00714 
00715   T &operator*() const { return *static_cast<T*>(Ptr); }
00716   T *operator->() const { return static_cast<T*>(Ptr); }
00717 
00718   inline FoldingSetBucketIterator &operator++() { // Preincrement
00719     advance();
00720     return *this;
00721   }
00722   FoldingSetBucketIterator operator++(int) {      // Postincrement
00723     FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
00724   }
00725 };
00726 
00727 //===----------------------------------------------------------------------===//
00728 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
00729 /// types in an enclosing object so that they can be inserted into FoldingSets.
00730 template <typename T>
00731 class FoldingSetNodeWrapper : public FoldingSetNode {
00732   T data;
00733 public:
00734   explicit FoldingSetNodeWrapper(const T &x) : data(x) {}
00735   virtual ~FoldingSetNodeWrapper() {}
00736 
00737   template<typename A1>
00738   explicit FoldingSetNodeWrapper(const A1 &a1)
00739     : data(a1) {}
00740 
00741   template <typename A1, typename A2>
00742   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2)
00743     : data(a1,a2) {}
00744 
00745   template <typename A1, typename A2, typename A3>
00746   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3)
00747     : data(a1,a2,a3) {}
00748 
00749   template <typename A1, typename A2, typename A3, typename A4>
00750   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3,
00751                                  const A4 &a4)
00752     : data(a1,a2,a3,a4) {}
00753 
00754   template <typename A1, typename A2, typename A3, typename A4, typename A5>
00755   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3,
00756                                  const A4 &a4, const A5 &a5)
00757   : data(a1,a2,a3,a4,a5) {}
00758 
00759 
00760   void Profile(FoldingSetNodeID &ID) { FoldingSetTrait<T>::Profile(data, ID); }
00761 
00762   T &getValue() { return data; }
00763   const T &getValue() const { return data; }
00764 
00765   operator T&() { return data; }
00766   operator const T&() const { return data; }
00767 };
00768 
00769 //===----------------------------------------------------------------------===//
00770 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
00771 /// a FoldingSetNodeID value rather than requiring the node to recompute it
00772 /// each time it is needed. This trades space for speed (which can be
00773 /// significant if the ID is long), and it also permits nodes to drop
00774 /// information that would otherwise only be required for recomputing an ID.
00775 class FastFoldingSetNode : public FoldingSetNode {
00776   FoldingSetNodeID FastID;
00777 protected:
00778   explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
00779 public:
00780   void Profile(FoldingSetNodeID &ID) const { 
00781     ID.AddNodeID(FastID); 
00782   }
00783 };
00784 
00785 //===----------------------------------------------------------------------===//
00786 // Partial specializations of FoldingSetTrait.
00787 
00788 template<typename T> struct FoldingSetTrait<T*> {
00789   static inline void Profile(T *X, FoldingSetNodeID &ID) {
00790     ID.AddPointer(X);
00791   }
00792 };
00793 } // End of namespace llvm.
00794 
00795 #endif