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/Allocator.h"
00022 #include "llvm/Support/DataTypes.h"
00023 
00024 namespace llvm {
00025   class APFloat;
00026   class APInt;
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(nullptr) {}
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(nullptr), 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   bool operator!=(FoldingSetNodeIDRef RHS) const { return !(*this == RHS); }
00282 
00283   /// Used to compare the "ordering" of two nodes as defined by the
00284   /// profiled bits and their ordering defined by memcmp().
00285   bool operator<(FoldingSetNodeIDRef) const;
00286 
00287   const unsigned *getData() const { return Data; }
00288   size_t getSize() const { return Size; }
00289 };
00290 
00291 //===--------------------------------------------------------------------===//
00292 /// FoldingSetNodeID - This class is used to gather all the unique data bits of
00293 /// a node.  When all the bits are gathered this class is used to produce a
00294 /// hash value for the node.
00295 ///
00296 class FoldingSetNodeID {
00297   /// Bits - Vector of all the data bits that make the node unique.
00298   /// Use a SmallVector to avoid a heap allocation in the common case.
00299   SmallVector<unsigned, 32> Bits;
00300 
00301 public:
00302   FoldingSetNodeID() {}
00303 
00304   FoldingSetNodeID(FoldingSetNodeIDRef Ref)
00305     : Bits(Ref.getData(), Ref.getData() + Ref.getSize()) {}
00306 
00307   /// Add* - Add various data types to Bit data.
00308   ///
00309   void AddPointer(const void *Ptr);
00310   void AddInteger(signed I);
00311   void AddInteger(unsigned I);
00312   void AddInteger(long I);
00313   void AddInteger(unsigned long I);
00314   void AddInteger(long long I);
00315   void AddInteger(unsigned long long I);
00316   void AddBoolean(bool B) { AddInteger(B ? 1U : 0U); }
00317   void AddString(StringRef String);
00318   void AddNodeID(const FoldingSetNodeID &ID);
00319 
00320   template <typename T>
00321   inline void Add(const T &x) { FoldingSetTrait<T>::Profile(x, *this); }
00322 
00323   /// clear - Clear the accumulated profile, allowing this FoldingSetNodeID
00324   /// object to be used to compute a new profile.
00325   inline void clear() { Bits.clear(); }
00326 
00327   /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used
00328   /// to lookup the node in the FoldingSetImpl.
00329   unsigned ComputeHash() const;
00330 
00331   /// operator== - Used to compare two nodes to each other.
00332   ///
00333   bool operator==(const FoldingSetNodeID &RHS) const;
00334   bool operator==(const FoldingSetNodeIDRef RHS) const;
00335 
00336   bool operator!=(const FoldingSetNodeID &RHS) const { return !(*this == RHS); }
00337   bool operator!=(const FoldingSetNodeIDRef RHS) const { return !(*this ==RHS);}
00338 
00339   /// Used to compare the "ordering" of two nodes as defined by the
00340   /// profiled bits and their ordering defined by memcmp().
00341   bool operator<(const FoldingSetNodeID &RHS) const;
00342   bool operator<(const FoldingSetNodeIDRef RHS) const;
00343 
00344   /// Intern - Copy this node's data to a memory region allocated from the
00345   /// given allocator and return a FoldingSetNodeIDRef describing the
00346   /// interned data.
00347   FoldingSetNodeIDRef Intern(BumpPtrAllocator &Allocator) const;
00348 };
00349 
00350 // Convenience type to hide the implementation of the folding set.
00351 typedef FoldingSetImpl::Node FoldingSetNode;
00352 template<class T> class FoldingSetIterator;
00353 template<class T> class FoldingSetBucketIterator;
00354 
00355 // Definitions of FoldingSetTrait and ContextualFoldingSetTrait functions, which
00356 // require the definition of FoldingSetNodeID.
00357 template<typename T>
00358 inline bool
00359 DefaultFoldingSetTrait<T>::Equals(T &X, const FoldingSetNodeID &ID,
00360                                   unsigned /*IDHash*/,
00361                                   FoldingSetNodeID &TempID) {
00362   FoldingSetTrait<T>::Profile(X, TempID);
00363   return TempID == ID;
00364 }
00365 template<typename T>
00366 inline unsigned
00367 DefaultFoldingSetTrait<T>::ComputeHash(T &X, FoldingSetNodeID &TempID) {
00368   FoldingSetTrait<T>::Profile(X, TempID);
00369   return TempID.ComputeHash();
00370 }
00371 template<typename T, typename Ctx>
00372 inline bool
00373 DefaultContextualFoldingSetTrait<T, Ctx>::Equals(T &X,
00374                                                  const FoldingSetNodeID &ID,
00375                                                  unsigned /*IDHash*/,
00376                                                  FoldingSetNodeID &TempID,
00377                                                  Ctx Context) {
00378   ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
00379   return TempID == ID;
00380 }
00381 template<typename T, typename Ctx>
00382 inline unsigned
00383 DefaultContextualFoldingSetTrait<T, Ctx>::ComputeHash(T &X,
00384                                                       FoldingSetNodeID &TempID,
00385                                                       Ctx Context) {
00386   ContextualFoldingSetTrait<T, Ctx>::Profile(X, TempID, Context);
00387   return TempID.ComputeHash();
00388 }
00389 
00390 //===----------------------------------------------------------------------===//
00391 /// FoldingSet - This template class is used to instantiate a specialized
00392 /// implementation of the folding set to the node class T.  T must be a
00393 /// subclass of FoldingSetNode and implement a Profile function.
00394 ///
00395 template<class T> class FoldingSet : public FoldingSetImpl {
00396 private:
00397   /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
00398   /// way to convert nodes into a unique specifier.
00399   void GetNodeProfile(Node *N, FoldingSetNodeID &ID) const override {
00400     T *TN = static_cast<T *>(N);
00401     FoldingSetTrait<T>::Profile(*TN, ID);
00402   }
00403   /// NodeEquals - Instantiations may optionally provide a way to compare a
00404   /// node with a specified ID.
00405   bool NodeEquals(Node *N, const FoldingSetNodeID &ID, unsigned IDHash,
00406                   FoldingSetNodeID &TempID) const override {
00407     T *TN = static_cast<T *>(N);
00408     return FoldingSetTrait<T>::Equals(*TN, ID, IDHash, TempID);
00409   }
00410   /// ComputeNodeHash - Instantiations may optionally provide a way to compute a
00411   /// hash value directly from a node.
00412   unsigned ComputeNodeHash(Node *N, FoldingSetNodeID &TempID) const override {
00413     T *TN = static_cast<T *>(N);
00414     return FoldingSetTrait<T>::ComputeHash(*TN, TempID);
00415   }
00416 
00417 public:
00418   explicit FoldingSet(unsigned Log2InitSize = 6)
00419   : FoldingSetImpl(Log2InitSize)
00420   {}
00421 
00422   typedef FoldingSetIterator<T> iterator;
00423   iterator begin() { return iterator(Buckets); }
00424   iterator end() { return iterator(Buckets+NumBuckets); }
00425 
00426   typedef FoldingSetIterator<const T> const_iterator;
00427   const_iterator begin() const { return const_iterator(Buckets); }
00428   const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
00429 
00430   typedef FoldingSetBucketIterator<T> bucket_iterator;
00431 
00432   bucket_iterator bucket_begin(unsigned hash) {
00433     return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
00434   }
00435 
00436   bucket_iterator bucket_end(unsigned hash) {
00437     return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
00438   }
00439 
00440   /// GetOrInsertNode - If there is an existing simple Node exactly
00441   /// equal to the specified node, return it.  Otherwise, insert 'N' and
00442   /// return it instead.
00443   T *GetOrInsertNode(Node *N) {
00444     return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
00445   }
00446 
00447   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
00448   /// return it.  If not, return the insertion token that will make insertion
00449   /// faster.
00450   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
00451     return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
00452   }
00453 };
00454 
00455 //===----------------------------------------------------------------------===//
00456 /// ContextualFoldingSet - This template class is a further refinement
00457 /// of FoldingSet which provides a context argument when calling
00458 /// Profile on its nodes.  Currently, that argument is fixed at
00459 /// initialization time.
00460 ///
00461 /// T must be a subclass of FoldingSetNode and implement a Profile
00462 /// function with signature
00463 ///   void Profile(llvm::FoldingSetNodeID &, Ctx);
00464 template <class T, class Ctx>
00465 class ContextualFoldingSet : public FoldingSetImpl {
00466   // Unfortunately, this can't derive from FoldingSet<T> because the
00467   // construction vtable for FoldingSet<T> requires
00468   // FoldingSet<T>::GetNodeProfile to be instantiated, which in turn
00469   // requires a single-argument T::Profile().
00470 
00471 private:
00472   Ctx Context;
00473 
00474   /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
00475   /// way to convert nodes into a unique specifier.
00476   void GetNodeProfile(FoldingSetImpl::Node *N,
00477                       FoldingSetNodeID &ID) const override {
00478     T *TN = static_cast<T *>(N);
00479     ContextualFoldingSetTrait<T, Ctx>::Profile(*TN, ID, Context);
00480   }
00481   bool NodeEquals(FoldingSetImpl::Node *N, const FoldingSetNodeID &ID,
00482                   unsigned IDHash, FoldingSetNodeID &TempID) const override {
00483     T *TN = static_cast<T *>(N);
00484     return ContextualFoldingSetTrait<T, Ctx>::Equals(*TN, ID, IDHash, TempID,
00485                                                      Context);
00486   }
00487   unsigned ComputeNodeHash(FoldingSetImpl::Node *N,
00488                            FoldingSetNodeID &TempID) const override {
00489     T *TN = static_cast<T *>(N);
00490     return ContextualFoldingSetTrait<T, Ctx>::ComputeHash(*TN, TempID, Context);
00491   }
00492 
00493 public:
00494   explicit ContextualFoldingSet(Ctx Context, unsigned Log2InitSize = 6)
00495   : FoldingSetImpl(Log2InitSize), Context(Context)
00496   {}
00497 
00498   Ctx getContext() const { return Context; }
00499 
00500 
00501   typedef FoldingSetIterator<T> iterator;
00502   iterator begin() { return iterator(Buckets); }
00503   iterator end() { return iterator(Buckets+NumBuckets); }
00504 
00505   typedef FoldingSetIterator<const T> const_iterator;
00506   const_iterator begin() const { return const_iterator(Buckets); }
00507   const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
00508 
00509   typedef FoldingSetBucketIterator<T> bucket_iterator;
00510 
00511   bucket_iterator bucket_begin(unsigned hash) {
00512     return bucket_iterator(Buckets + (hash & (NumBuckets-1)));
00513   }
00514 
00515   bucket_iterator bucket_end(unsigned hash) {
00516     return bucket_iterator(Buckets + (hash & (NumBuckets-1)), true);
00517   }
00518 
00519   /// GetOrInsertNode - If there is an existing simple Node exactly
00520   /// equal to the specified node, return it.  Otherwise, insert 'N'
00521   /// and return it instead.
00522   T *GetOrInsertNode(Node *N) {
00523     return static_cast<T *>(FoldingSetImpl::GetOrInsertNode(N));
00524   }
00525 
00526   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it
00527   /// exists, return it.  If not, return the insertion token that will
00528   /// make insertion faster.
00529   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
00530     return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
00531   }
00532 };
00533 
00534 //===----------------------------------------------------------------------===//
00535 /// FoldingSetVectorIterator - This implements an iterator for
00536 /// FoldingSetVector. It is only necessary because FoldingSetIterator provides
00537 /// a value_type of T, while the vector in FoldingSetVector exposes
00538 /// a value_type of T*. Fortunately, FoldingSetIterator doesn't expose very
00539 /// much besides operator* and operator->, so we just wrap the inner vector
00540 /// iterator and perform the extra dereference.
00541 template <class T, class VectorIteratorT>
00542 class FoldingSetVectorIterator {
00543   // Provide a typedef to workaround the lack of correct injected class name
00544   // support in older GCCs.
00545   typedef FoldingSetVectorIterator<T, VectorIteratorT> SelfT;
00546 
00547   VectorIteratorT Iterator;
00548 
00549 public:
00550   FoldingSetVectorIterator(VectorIteratorT I) : Iterator(I) {}
00551 
00552   bool operator==(const SelfT &RHS) const {
00553     return Iterator == RHS.Iterator;
00554   }
00555   bool operator!=(const SelfT &RHS) const {
00556     return Iterator != RHS.Iterator;
00557   }
00558 
00559   T &operator*() const { return **Iterator; }
00560 
00561   T *operator->() const { return *Iterator; }
00562 
00563   inline SelfT &operator++() {
00564     ++Iterator;
00565     return *this;
00566   }
00567   SelfT operator++(int) {
00568     SelfT tmp = *this;
00569     ++*this;
00570     return tmp;
00571   }
00572 };
00573 
00574 //===----------------------------------------------------------------------===//
00575 /// FoldingSetVector - This template class combines a FoldingSet and a vector
00576 /// to provide the interface of FoldingSet but with deterministic iteration
00577 /// order based on the insertion order. T must be a subclass of FoldingSetNode
00578 /// and implement a Profile function.
00579 template <class T, class VectorT = SmallVector<T*, 8> >
00580 class FoldingSetVector {
00581   FoldingSet<T> Set;
00582   VectorT Vector;
00583 
00584 public:
00585   explicit FoldingSetVector(unsigned Log2InitSize = 6)
00586       : Set(Log2InitSize) {
00587   }
00588 
00589   typedef FoldingSetVectorIterator<T, typename VectorT::iterator> iterator;
00590   iterator begin() { return Vector.begin(); }
00591   iterator end()   { return Vector.end(); }
00592 
00593   typedef FoldingSetVectorIterator<const T, typename VectorT::const_iterator>
00594     const_iterator;
00595   const_iterator begin() const { return Vector.begin(); }
00596   const_iterator end()   const { return Vector.end(); }
00597 
00598   /// clear - Remove all nodes from the folding set.
00599   void clear() { Set.clear(); Vector.clear(); }
00600 
00601   /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
00602   /// return it.  If not, return the insertion token that will make insertion
00603   /// faster.
00604   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
00605     return Set.FindNodeOrInsertPos(ID, InsertPos);
00606   }
00607 
00608   /// GetOrInsertNode - If there is an existing simple Node exactly
00609   /// equal to the specified node, return it.  Otherwise, insert 'N' and
00610   /// return it instead.
00611   T *GetOrInsertNode(T *N) {
00612     T *Result = Set.GetOrInsertNode(N);
00613     if (Result == N) Vector.push_back(N);
00614     return Result;
00615   }
00616 
00617   /// InsertNode - Insert the specified node into the folding set, knowing that
00618   /// it is not already in the folding set.  InsertPos must be obtained from
00619   /// FindNodeOrInsertPos.
00620   void InsertNode(T *N, void *InsertPos) {
00621     Set.InsertNode(N, InsertPos);
00622     Vector.push_back(N);
00623   }
00624 
00625   /// InsertNode - Insert the specified node into the folding set, knowing that
00626   /// it is not already in the folding set.
00627   void InsertNode(T *N) {
00628     Set.InsertNode(N);
00629     Vector.push_back(N);
00630   }
00631 
00632   /// size - Returns the number of nodes in the folding set.
00633   unsigned size() const { return Set.size(); }
00634 
00635   /// empty - Returns true if there are no nodes in the folding set.
00636   bool empty() const { return Set.empty(); }
00637 };
00638 
00639 //===----------------------------------------------------------------------===//
00640 /// FoldingSetIteratorImpl - This is the common iterator support shared by all
00641 /// folding sets, which knows how to walk the folding set hash table.
00642 class FoldingSetIteratorImpl {
00643 protected:
00644   FoldingSetNode *NodePtr;
00645   FoldingSetIteratorImpl(void **Bucket);
00646   void advance();
00647 
00648 public:
00649   bool operator==(const FoldingSetIteratorImpl &RHS) const {
00650     return NodePtr == RHS.NodePtr;
00651   }
00652   bool operator!=(const FoldingSetIteratorImpl &RHS) const {
00653     return NodePtr != RHS.NodePtr;
00654   }
00655 };
00656 
00657 
00658 template<class T>
00659 class FoldingSetIterator : public FoldingSetIteratorImpl {
00660 public:
00661   explicit FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
00662 
00663   T &operator*() const {
00664     return *static_cast<T*>(NodePtr);
00665   }
00666 
00667   T *operator->() const {
00668     return static_cast<T*>(NodePtr);
00669   }
00670 
00671   inline FoldingSetIterator &operator++() {          // Preincrement
00672     advance();
00673     return *this;
00674   }
00675   FoldingSetIterator operator++(int) {        // Postincrement
00676     FoldingSetIterator tmp = *this; ++*this; return tmp;
00677   }
00678 };
00679 
00680 //===----------------------------------------------------------------------===//
00681 /// FoldingSetBucketIteratorImpl - This is the common bucket iterator support
00682 /// shared by all folding sets, which knows how to walk a particular bucket
00683 /// of a folding set hash table.
00684 
00685 class FoldingSetBucketIteratorImpl {
00686 protected:
00687   void *Ptr;
00688 
00689   explicit FoldingSetBucketIteratorImpl(void **Bucket);
00690 
00691   FoldingSetBucketIteratorImpl(void **Bucket, bool)
00692     : Ptr(Bucket) {}
00693 
00694   void advance() {
00695     void *Probe = static_cast<FoldingSetNode*>(Ptr)->getNextInBucket();
00696     uintptr_t x = reinterpret_cast<uintptr_t>(Probe) & ~0x1;
00697     Ptr = reinterpret_cast<void*>(x);
00698   }
00699 
00700 public:
00701   bool operator==(const FoldingSetBucketIteratorImpl &RHS) const {
00702     return Ptr == RHS.Ptr;
00703   }
00704   bool operator!=(const FoldingSetBucketIteratorImpl &RHS) const {
00705     return Ptr != RHS.Ptr;
00706   }
00707 };
00708 
00709 
00710 template<class T>
00711 class FoldingSetBucketIterator : public FoldingSetBucketIteratorImpl {
00712 public:
00713   explicit FoldingSetBucketIterator(void **Bucket) :
00714     FoldingSetBucketIteratorImpl(Bucket) {}
00715 
00716   FoldingSetBucketIterator(void **Bucket, bool) :
00717     FoldingSetBucketIteratorImpl(Bucket, true) {}
00718 
00719   T &operator*() const { return *static_cast<T*>(Ptr); }
00720   T *operator->() const { return static_cast<T*>(Ptr); }
00721 
00722   inline FoldingSetBucketIterator &operator++() { // Preincrement
00723     advance();
00724     return *this;
00725   }
00726   FoldingSetBucketIterator operator++(int) {      // Postincrement
00727     FoldingSetBucketIterator tmp = *this; ++*this; return tmp;
00728   }
00729 };
00730 
00731 //===----------------------------------------------------------------------===//
00732 /// FoldingSetNodeWrapper - This template class is used to "wrap" arbitrary
00733 /// types in an enclosing object so that they can be inserted into FoldingSets.
00734 template <typename T>
00735 class FoldingSetNodeWrapper : public FoldingSetNode {
00736   T data;
00737 public:
00738   explicit FoldingSetNodeWrapper(const T &x) : data(x) {}
00739   virtual ~FoldingSetNodeWrapper() {}
00740 
00741   template<typename A1>
00742   explicit FoldingSetNodeWrapper(const A1 &a1)
00743     : data(a1) {}
00744 
00745   template <typename A1, typename A2>
00746   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2)
00747     : data(a1,a2) {}
00748 
00749   template <typename A1, typename A2, typename A3>
00750   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3)
00751     : data(a1,a2,a3) {}
00752 
00753   template <typename A1, typename A2, typename A3, typename A4>
00754   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3,
00755                                  const A4 &a4)
00756     : data(a1,a2,a3,a4) {}
00757 
00758   template <typename A1, typename A2, typename A3, typename A4, typename A5>
00759   explicit FoldingSetNodeWrapper(const A1 &a1, const A2 &a2, const A3 &a3,
00760                                  const A4 &a4, const A5 &a5)
00761   : data(a1,a2,a3,a4,a5) {}
00762 
00763 
00764   void Profile(FoldingSetNodeID &ID) { FoldingSetTrait<T>::Profile(data, ID); }
00765 
00766   T &getValue() { return data; }
00767   const T &getValue() const { return data; }
00768 
00769   operator T&() { return data; }
00770   operator const T&() const { return data; }
00771 };
00772 
00773 //===----------------------------------------------------------------------===//
00774 /// FastFoldingSetNode - This is a subclass of FoldingSetNode which stores
00775 /// a FoldingSetNodeID value rather than requiring the node to recompute it
00776 /// each time it is needed. This trades space for speed (which can be
00777 /// significant if the ID is long), and it also permits nodes to drop
00778 /// information that would otherwise only be required for recomputing an ID.
00779 class FastFoldingSetNode : public FoldingSetNode {
00780   FoldingSetNodeID FastID;
00781 protected:
00782   explicit FastFoldingSetNode(const FoldingSetNodeID &ID) : FastID(ID) {}
00783 public:
00784   void Profile(FoldingSetNodeID &ID) const { 
00785     ID.AddNodeID(FastID); 
00786   }
00787 };
00788 
00789 //===----------------------------------------------------------------------===//
00790 // Partial specializations of FoldingSetTrait.
00791 
00792 template<typename T> struct FoldingSetTrait<T*> {
00793   static inline void Profile(T *X, FoldingSetNodeID &ID) {
00794     ID.AddPointer(X);
00795   }
00796 };
00797 template <typename T1, typename T2>
00798 struct FoldingSetTrait<std::pair<T1, T2>> {
00799   static inline void Profile(const std::pair<T1, T2> &P,
00800                              llvm::FoldingSetNodeID &ID) {
00801     ID.Add(P.first);
00802     ID.Add(P.second);
00803   }
00804 };
00805 } // End of namespace llvm.
00806 
00807 #endif