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_IR_USE_H
00026 #define LLVM_IR_USE_H
00027 
00028 #include "llvm/ADT/PointerIntPair.h"
00029 #include "llvm/Support/CBindingWrapping.h"
00030 #include "llvm/Support/Compiler.h"
00031 #include "llvm-c/Core.h"
00032 #include <cstddef>
00033 #include <iterator>
00034 
00035 namespace llvm {
00036 
00037 class Value;
00038 class User;
00039 class Use;
00040 template<typename>
00041 struct simplify_type;
00042 
00043 // Use** is only 4-byte aligned.
00044 template<>
00045 class PointerLikeTypeTraits<Use**> {
00046 public:
00047   static inline void *getAsVoidPointer(Use** P) { return P; }
00048   static inline Use **getFromVoidPointer(void *P) {
00049     return static_cast<Use**>(P);
00050   }
00051   enum { NumLowBitsAvailable = 2 };
00052 };
00053 
00054 //===----------------------------------------------------------------------===//
00055 //                                  Use Class
00056 //===----------------------------------------------------------------------===//
00057 
00058 /// Use is here to make keeping the "use" list of a Value up-to-date really
00059 /// easy.
00060 class Use {
00061 public:
00062   /// swap - provide a fast substitute to std::swap<Use>
00063   /// that also works with less standard-compliant compilers
00064   void swap(Use &RHS);
00065 
00066   // A type for the word following an array of hung-off Uses in memory, which is
00067   // a pointer back to their User with the bottom bit set.
00068   typedef PointerIntPair<User*, 1, unsigned> UserRef;
00069 
00070 private:
00071   Use(const Use &U) LLVM_DELETED_FUNCTION;
00072 
00073   /// Destructor - Only for zap()
00074   ~Use() {
00075     if (Val) removeFromList();
00076   }
00077 
00078   enum PrevPtrTag { zeroDigitTag
00079                   , oneDigitTag
00080                   , stopTag
00081                   , fullStopTag };
00082 
00083   /// Constructor
00084   Use(PrevPtrTag tag) : Val(0) {
00085     Prev.setInt(tag);
00086   }
00087 
00088 public:
00089   /// Normally Use will just implicitly convert to a Value* that it holds.
00090   operator Value*() const { return Val; }
00091   
00092   /// If implicit conversion to Value* doesn't work, the get() method returns
00093   /// the Value*.
00094   Value *get() const { return Val; }
00095   
00096   /// getUser - This returns the User that contains this Use.  For an
00097   /// instruction operand, for example, this will return the instruction.
00098   User *getUser() const;
00099 
00100   inline void set(Value *Val);
00101 
00102   Value *operator=(Value *RHS) {
00103     set(RHS);
00104     return RHS;
00105   }
00106   const Use &operator=(const Use &RHS) {
00107     set(RHS.Val);
00108     return *this;
00109   }
00110 
00111         Value *operator->()       { return Val; }
00112   const Value *operator->() const { return Val; }
00113 
00114   Use *getNext() const { return Next; }
00115 
00116   
00117   /// initTags - initialize the waymarking tags on an array of Uses, so that
00118   /// getUser() can find the User from any of those Uses.
00119   static Use *initTags(Use *Start, Use *Stop);
00120 
00121   /// zap - This is used to destroy Use operands when the number of operands of
00122   /// a User changes.
00123   static void zap(Use *Start, const Use *Stop, bool del = false);
00124 
00125 private:
00126   const Use* getImpliedUser() const;
00127   
00128   Value *Val;
00129   Use *Next;
00130   PointerIntPair<Use**, 2, PrevPtrTag> Prev;
00131 
00132   void setPrev(Use **NewPrev) {
00133     Prev.setPointer(NewPrev);
00134   }
00135   void addToList(Use **List) {
00136     Next = *List;
00137     if (Next) Next->setPrev(&Next);
00138     setPrev(List);
00139     *List = this;
00140   }
00141   void removeFromList() {
00142     Use **StrippedPrev = Prev.getPointer();
00143     *StrippedPrev = Next;
00144     if (Next) Next->setPrev(StrippedPrev);
00145   }
00146 
00147   friend class Value;
00148 };
00149 
00150 // simplify_type - Allow clients to treat uses just like values when using
00151 // casting operators.
00152 template<> struct simplify_type<Use> {
00153   typedef Value* SimpleType;
00154   static SimpleType getSimplifiedValue(Use &Val) {
00155     return Val.get();
00156   }
00157 };
00158 template<> struct simplify_type<const Use> {
00159   typedef /*const*/ Value* SimpleType;
00160   static SimpleType getSimplifiedValue(const Use &Val) {
00161     return Val.get();
00162   }
00163 };
00164 
00165 
00166 
00167 template<typename UserTy>  // UserTy == 'User' or 'const User'
00168 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
00169                                                 UserTy*, ptrdiff_t> {
00170   typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
00171   typedef value_use_iterator<UserTy> _Self;
00172 
00173   Use *U;
00174   explicit value_use_iterator(Use *u) : U(u) {}
00175   friend class Value;
00176 public:
00177   typedef typename super::reference reference;
00178   typedef typename super::pointer pointer;
00179 
00180   value_use_iterator() {}
00181 
00182   bool operator==(const _Self &x) const {
00183     return U == x.U;
00184   }
00185   bool operator!=(const _Self &x) const {
00186     return !operator==(x);
00187   }
00188 
00189   /// atEnd - return true if this iterator is equal to use_end() on the value.
00190   bool atEnd() const { return U == 0; }
00191 
00192   // Iterator traversal: forward iteration only
00193   _Self &operator++() {          // Preincrement
00194     assert(U && "Cannot increment end iterator!");
00195     U = U->getNext();
00196     return *this;
00197   }
00198   _Self operator++(int) {        // Postincrement
00199     _Self tmp = *this; ++*this; return tmp;
00200   }
00201 
00202   // Retrieve a pointer to the current User.
00203   UserTy *operator*() const {
00204     assert(U && "Cannot dereference end iterator!");
00205     return U->getUser();
00206   }
00207 
00208   UserTy *operator->() const { return operator*(); }
00209 
00210   Use &getUse() const { return *U; }
00211   
00212   /// getOperandNo - Return the operand # of this use in its User.  Defined in
00213   /// User.h
00214   ///
00215   unsigned getOperandNo() const;
00216 };
00217 
00218 // Create wrappers for C Binding types (see CBindingWrapping.h).
00219 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
00220 
00221 } // End llvm namespace
00222 
00223 #endif