Home | History | Annotate | Download | only in IR
      1 //===-- llvm/Use.h - Definition of the Use class ----------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This defines the Use class.  The Use class represents the operand of an
     11 // instruction or some other User instance which refers to a Value.  The Use
     12 // class keeps the "use list" of the referenced value up to date.
     13 //
     14 // Pointer tagging is used to efficiently find the User corresponding
     15 // to a Use without having to store a User pointer in every Use. A
     16 // User is preceded in memory by all the Uses corresponding to its
     17 // operands, and the low bits of one of the fields (Prev) of the Use
     18 // class are used to encode offsets to be able to find that User given
     19 // a pointer to any Use. For details, see:
     20 //
     21 //   http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
     22 //
     23 //===----------------------------------------------------------------------===//
     24 
     25 #ifndef LLVM_IR_USE_H
     26 #define LLVM_IR_USE_H
     27 
     28 #include "llvm/ADT/PointerIntPair.h"
     29 #include "llvm/Support/CBindingWrapping.h"
     30 #include "llvm/Support/Compiler.h"
     31 #include "llvm-c/Core.h"
     32 #include <cstddef>
     33 #include <iterator>
     34 
     35 namespace llvm {
     36 
     37 class Value;
     38 class User;
     39 class Use;
     40 template<typename>
     41 struct simplify_type;
     42 
     43 // Use** is only 4-byte aligned.
     44 template<>
     45 class PointerLikeTypeTraits<Use**> {
     46 public:
     47   static inline void *getAsVoidPointer(Use** P) { return P; }
     48   static inline Use **getFromVoidPointer(void *P) {
     49     return static_cast<Use**>(P);
     50   }
     51   enum { NumLowBitsAvailable = 2 };
     52 };
     53 
     54 //===----------------------------------------------------------------------===//
     55 //                                  Use Class
     56 //===----------------------------------------------------------------------===//
     57 
     58 /// Use is here to make keeping the "use" list of a Value up-to-date really
     59 /// easy.
     60 class Use {
     61 public:
     62   /// swap - provide a fast substitute to std::swap<Use>
     63   /// that also works with less standard-compliant compilers
     64   void swap(Use &RHS);
     65 
     66   // A type for the word following an array of hung-off Uses in memory, which is
     67   // a pointer back to their User with the bottom bit set.
     68   typedef PointerIntPair<User*, 1, unsigned> UserRef;
     69 
     70 private:
     71   Use(const Use &U) LLVM_DELETED_FUNCTION;
     72 
     73   /// Destructor - Only for zap()
     74   ~Use() {
     75     if (Val) removeFromList();
     76   }
     77 
     78   enum PrevPtrTag { zeroDigitTag
     79                   , oneDigitTag
     80                   , stopTag
     81                   , fullStopTag };
     82 
     83   /// Constructor
     84   Use(PrevPtrTag tag) : Val(0) {
     85     Prev.setInt(tag);
     86   }
     87 
     88 public:
     89   /// Normally Use will just implicitly convert to a Value* that it holds.
     90   operator Value*() const { return Val; }
     91 
     92   /// If implicit conversion to Value* doesn't work, the get() method returns
     93   /// the Value*.
     94   Value *get() const { return Val; }
     95 
     96   /// getUser - This returns the User that contains this Use.  For an
     97   /// instruction operand, for example, this will return the instruction.
     98   User *getUser() const;
     99 
    100   inline void set(Value *Val);
    101 
    102   Value *operator=(Value *RHS) {
    103     set(RHS);
    104     return RHS;
    105   }
    106   const Use &operator=(const Use &RHS) {
    107     set(RHS.Val);
    108     return *this;
    109   }
    110 
    111         Value *operator->()       { return Val; }
    112   const Value *operator->() const { return Val; }
    113 
    114   Use *getNext() const { return Next; }
    115 
    116 
    117   /// initTags - initialize the waymarking tags on an array of Uses, so that
    118   /// getUser() can find the User from any of those Uses.
    119   static Use *initTags(Use *Start, Use *Stop);
    120 
    121   /// zap - This is used to destroy Use operands when the number of operands of
    122   /// a User changes.
    123   static void zap(Use *Start, const Use *Stop, bool del = false);
    124 
    125 private:
    126   const Use* getImpliedUser() const;
    127 
    128   Value *Val;
    129   Use *Next;
    130   PointerIntPair<Use**, 2, PrevPtrTag> Prev;
    131 
    132   void setPrev(Use **NewPrev) {
    133     Prev.setPointer(NewPrev);
    134   }
    135   void addToList(Use **List) {
    136     Next = *List;
    137     if (Next) Next->setPrev(&Next);
    138     setPrev(List);
    139     *List = this;
    140   }
    141   void removeFromList() {
    142     Use **StrippedPrev = Prev.getPointer();
    143     *StrippedPrev = Next;
    144     if (Next) Next->setPrev(StrippedPrev);
    145   }
    146 
    147   friend class Value;
    148 };
    149 
    150 // simplify_type - Allow clients to treat uses just like values when using
    151 // casting operators.
    152 template<> struct simplify_type<Use> {
    153   typedef Value* SimpleType;
    154   static SimpleType getSimplifiedValue(Use &Val) {
    155     return Val.get();
    156   }
    157 };
    158 template<> struct simplify_type<const Use> {
    159   typedef /*const*/ Value* SimpleType;
    160   static SimpleType getSimplifiedValue(const Use &Val) {
    161     return Val.get();
    162   }
    163 };
    164 
    165 
    166 
    167 template<typename UserTy>  // UserTy == 'User' or 'const User'
    168 class value_use_iterator : public std::iterator<std::forward_iterator_tag,
    169                                                 UserTy*, ptrdiff_t> {
    170   typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
    171   typedef value_use_iterator<UserTy> _Self;
    172 
    173   Use *U;
    174   explicit value_use_iterator(Use *u) : U(u) {}
    175   friend class Value;
    176 public:
    177   typedef typename super::reference reference;
    178   typedef typename super::pointer pointer;
    179 
    180   value_use_iterator() {}
    181 
    182   bool operator==(const _Self &x) const {
    183     return U == x.U;
    184   }
    185   bool operator!=(const _Self &x) const {
    186     return !operator==(x);
    187   }
    188 
    189   /// atEnd - return true if this iterator is equal to use_end() on the value.
    190   bool atEnd() const { return U == 0; }
    191 
    192   // Iterator traversal: forward iteration only
    193   _Self &operator++() {          // Preincrement
    194     assert(U && "Cannot increment end iterator!");
    195     U = U->getNext();
    196     return *this;
    197   }
    198   _Self operator++(int) {        // Postincrement
    199     _Self tmp = *this; ++*this; return tmp;
    200   }
    201 
    202   // Retrieve a pointer to the current User.
    203   UserTy *operator*() const {
    204     assert(U && "Cannot dereference end iterator!");
    205     return U->getUser();
    206   }
    207 
    208   UserTy *operator->() const { return operator*(); }
    209 
    210   Use &getUse() const { return *U; }
    211 
    212   /// getOperandNo - Return the operand # of this use in its User.  Defined in
    213   /// User.h
    214   ///
    215   unsigned getOperandNo() const;
    216 };
    217 
    218 // Create wrappers for C Binding types (see CBindingWrapping.h).
    219 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
    220 
    221 } // End llvm namespace
    222 
    223 #endif
    224