LLVM API Documentation

Use.h

Go to the documentation of this file.
00001 //===-- llvm/Use.h - Definition of the Use class ----------------*- 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 defines the Use class.  The Use class represents the operand of an
00011 // instruction or some other User instance which refers to a Value.  The Use
00012 // class keeps the "use list" of the referenced value up to date.
00013 //
00014 // Pointer tagging is used to efficiently find the User corresponding
00015 // to a Use without having to store a User pointer in every Use. A
00016 // User is preceded in memory by all the Uses corresponding to its
00017 // operands, and the low bits of one of the fields (Prev) of the Use
00018 // class are used to encode offsets to be able to find that User given
00019 // a pointer to any Use. For details, see:
00020 //
00021 //   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
00022 //
00023 //===----------------------------------------------------------------------===//
00024 
00025 #ifndef LLVM_USE_H
00026 #define LLVM_USE_H
00027 
00028 #include "llvm/Support/Casting.h"
00029 #include "llvm/ADT/iterator.h"
00030 #include "llvm/ADT/PointerIntPair.h"
00031 
00032 namespace llvm {
00033 
00034 class Value;
00035 class User;
00036 class Use;
00037 
00038 /// Tag - generic tag type for (at least 32 bit) pointers
00039 enum Tag { noTag, tagOne, tagTwo, tagThree };
00040 
00041 // Use** is only 4-byte aligned.
00042 template<>
00043 class PointerLikeTypeTraits<Use**> {
00044 public:
00045   static inline void *getAsVoidPointer(Use** P) { return P; }
00046   static inline Use **getFromVoidPointer(void *P) {
00047     return static_cast<Use**>(P);
00048   }
00049   enum { NumLowBitsAvailable = 2 };
00050 };
00051 
00052 //===----------------------------------------------------------------------===//
00053 //                                  Use Class
00054 //===----------------------------------------------------------------------===//
00055 
00056 /// Use is here to make keeping the "use" list of a Value up-to-date really
00057 /// easy.
00058 class Use {
00059 public:
00060   /// swap - provide a fast substitute to std::swap<Use>
00061   /// that also works with less standard-compliant compilers
00062   void swap(Use &RHS);
00063 
00064 private:
00065   /// Copy ctor - do not implement
00066   Use(const Use &U);
00067 
00068   /// Destructor - Only for zap()
00069   inline ~Use() {
00070     if (Val) removeFromList();
00071   }
00072 
00073   /// Default ctor - This leaves the Use completely uninitialized.  The only
00074   /// thing that is valid to do with this use is to call the "init" method.
00075   inline Use() {}
00076   enum PrevPtrTag { zeroDigitTag = noTag
00077                   , oneDigitTag = tagOne
00078                   , stopTag = tagTwo
00079                   , fullStopTag = tagThree };
00080 
00081 public:
00082   /// Normally Use will just implicitly convert to a Value* that it holds.
00083   operator Value*() const { return Val; }
00084   
00085   /// If implicit conversion to Value* doesn't work, the get() method returns
00086   /// the Value*.
00087   Value *get() const { return Val; }
00088   
00089   /// getUser - This returns the User that contains this Use.  For an
00090   /// instruction operand, for example, this will return the instruction.
00091   User *getUser() const;
00092 
00093   inline void set(Value *Val);
00094 
00095   Value *operator=(Value *RHS) {
00096     set(RHS);
00097     return RHS;
00098   }
00099   const Use &operator=(const Use &RHS) {
00100     set(RHS.Val);
00101     return *this;
00102   }
00103 
00104         Value *operator->()       { return Val; }
00105   const Value *operator->() const { return Val; }
00106 
00107   Use *getNext() const { return Next; }
00108 
00109   
00110   /// zap - This is used to destroy Use operands when the number of operands of
00111   /// a User changes.
00112   static void zap(Use *Start, const Use *Stop, bool del = false);
00113 
00114   /// getPrefix - Return deletable pointer if appropriate
00115   Use *getPrefix();
00116 private:
00117   const Use* getImpliedUser() const;
00118   static Use *initTags(Use *Start, Use *Stop, ptrdiff_t Done = 0);
00119   
00120   Value *Val;
00121   Use *Next;
00122   PointerIntPair<Use**, 2, PrevPtrTag> Prev;
00123 
00124   void setPrev(Use **NewPrev) {
00125     Prev.setPointer(NewPrev);
00126   }
00127   void addToList(Use **List) {
00128     Next = *List;
00129     if (Next) Next->setPrev(&Next);
00130     setPrev(List);
00131     *List = this;
00132   }
00133   void removeFromList() {
00134     Use **StrippedPrev = Prev.getPointer();
00135     *StrippedPrev = Next;
00136     if (Next) Next->setPrev(StrippedPrev);
00137   }
00138 
00139   friend class Value;
00140   friend class User;
00141 };
00142 
00143 // simplify_type - Allow clients to treat uses just like values when using
00144 // casting operators.
00145 template<> struct simplify_type<Use> {
00146   typedef Value* SimpleType;
00147   static SimpleType getSimplifiedValue(const Use &Val) {
00148     return static_cast<SimpleType>(Val.get());
00149   }
00150 };
00151 template<> struct simplify_type<const Use> {
00152   typedef Value* SimpleType;
00153   static SimpleType getSimplifiedValue(const Use &Val) {
00154     return static_cast<SimpleType>(Val.get());
00155   }
00156 };
00157 
00158 
00159 
00160 template<typename UserTy>  // UserTy == 'User' or 'const User'
00161 class value_use_iterator : public forward_iterator<UserTy*, ptrdiff_t> {
00162   typedef forward_iterator<UserTy*, ptrdiff_t> super;
00163   typedef value_use_iterator<UserTy> _Self;
00164 
00165   Use *U;
00166   explicit value_use_iterator(Use *u) : U(u) {}
00167   friend class Value;
00168 public:
00169   typedef typename super::reference reference;
00170   typedef typename super::pointer pointer;
00171 
00172   value_use_iterator(const _Self &I) : U(I.U) {}
00173   value_use_iterator() {}
00174 
00175   bool operator==(const _Self &x) const {
00176     return U == x.U;
00177   }
00178   bool operator!=(const _Self &x) const {
00179     return !operator==(x);
00180   }
00181 
00182   /// atEnd - return true if this iterator is equal to use_end() on the value.
00183   bool atEnd() const { return U == 0; }
00184 
00185   // Iterator traversal: forward iteration only
00186   _Self &operator++() {          // Preincrement
00187     assert(U && "Cannot increment end iterator!");
00188     U = U->getNext();
00189     return *this;
00190   }
00191   _Self operator++(int) {        // Postincrement
00192     _Self tmp = *this; ++*this; return tmp;
00193   }
00194 
00195   // Retrieve a pointer to the current User.
00196   UserTy *operator*() const {
00197     assert(U && "Cannot dereference end iterator!");
00198     return U->getUser();
00199   }
00200 
00201   UserTy *operator->() const { return operator*(); }
00202 
00203   Use &getUse() const { return *U; }
00204   
00205   /// getOperandNo - Return the operand # of this use in its User.  Defined in
00206   /// User.h
00207   ///
00208   unsigned getOperandNo() const;
00209 };
00210 
00211 
00212 template<> struct simplify_type<value_use_iterator<User> > {
00213   typedef User* SimpleType;
00214   
00215   static SimpleType getSimplifiedValue(const value_use_iterator<User> &Val) {
00216     return *Val;
00217   }
00218 };
00219 
00220 template<> struct simplify_type<const value_use_iterator<User> >
00221  : public simplify_type<value_use_iterator<User> > {};
00222 
00223 template<> struct simplify_type<value_use_iterator<const User> > {
00224   typedef const User* SimpleType;
00225   
00226   static SimpleType getSimplifiedValue(const 
00227                                        value_use_iterator<const User> &Val) {
00228     return *Val;
00229   }
00230 };
00231 
00232 template<> struct simplify_type<const value_use_iterator<const User> >
00233   : public simplify_type<value_use_iterator<const User> > {};
00234  
00235 } // End llvm namespace
00236 
00237 #endif