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 /// \file
     10 ///
     11 /// This defines the Use class.  The Use class represents the operand of an
     12 /// instruction or some other User instance which refers to a Value.  The Use
     13 /// class keeps the "use list" of the referenced value up to date.
     14 ///
     15 /// Pointer tagging is used to efficiently find the User corresponding to a Use
     16 /// without having to store a User pointer in every Use. A User is preceded in
     17 /// memory by all the Uses corresponding to its operands, and the low bits of
     18 /// one of the fields (Prev) of the Use class are used to encode offsets to be
     19 /// able to find that User given 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 <cstddef>
     31 
     32 namespace llvm {
     33 
     34 class Value;
     35 class User;
     36 class Use;
     37 template <typename> struct simplify_type;
     38 
     39 // Use** is only 4-byte aligned.
     40 template <> class PointerLikeTypeTraits<Use **> {
     41 public:
     42   static inline void *getAsVoidPointer(Use **P) { return P; }
     43   static inline Use **getFromVoidPointer(void *P) {
     44     return static_cast<Use **>(P);
     45   }
     46   enum { NumLowBitsAvailable = 2 };
     47 };
     48 
     49 /// \brief A Use represents the edge between a Value definition and its users.
     50 ///
     51 /// This is notionally a two-dimensional linked list. It supports traversing
     52 /// all of the uses for a particular value definition. It also supports jumping
     53 /// directly to the used value when we arrive from the User's operands, and
     54 /// jumping directly to the User when we arrive from the Value's uses.
     55 ///
     56 /// The pointer to the used Value is explicit, and the pointer to the User is
     57 /// implicit. The implicit pointer is found via a waymarking algorithm
     58 /// described in the programmer's manual:
     59 ///
     60 ///   http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
     61 ///
     62 /// This is essentially the single most memory intensive object in LLVM because
     63 /// of the number of uses in the system. At the same time, the constant time
     64 /// operations it allows are essential to many optimizations having reasonable
     65 /// time complexity.
     66 class Use {
     67 public:
     68   /// \brief Provide a fast substitute to std::swap<Use>
     69   /// that also works with less standard-compliant compilers
     70   void swap(Use &RHS);
     71 
     72   // A type for the word following an array of hung-off Uses in memory, which is
     73   // a pointer back to their User with the bottom bit set.
     74   typedef PointerIntPair<User *, 1, unsigned> UserRef;
     75 
     76 private:
     77   Use(const Use &U) = delete;
     78 
     79   /// Destructor - Only for zap()
     80   ~Use() {
     81     if (Val)
     82       removeFromList();
     83   }
     84 
     85   enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag };
     86 
     87   /// Constructor
     88   Use(PrevPtrTag tag) : Val(nullptr) { Prev.setInt(tag); }
     89 
     90 public:
     91   operator Value *() const { return Val; }
     92   Value *get() const { return Val; }
     93 
     94   /// \brief Returns the User that contains this Use.
     95   ///
     96   /// For an instruction operand, for example, this will return the
     97   /// instruction.
     98   User *getUser() const;
     99 
    100   inline void set(Value *Val);
    101 
    102   inline Value *operator=(Value *RHS);
    103   inline const Use &operator=(const Use &RHS);
    104 
    105   Value *operator->() { return Val; }
    106   const Value *operator->() const { return Val; }
    107 
    108   Use *getNext() const { return Next; }
    109 
    110   /// \brief Return the operand # of this use in its User.
    111   unsigned getOperandNo() const;
    112 
    113   /// \brief Initializes the waymarking tags on an array of Uses.
    114   ///
    115   /// This sets up the array of Uses such that getUser() can find the User from
    116   /// any of those Uses.
    117   static Use *initTags(Use *Start, Use *Stop);
    118 
    119   /// \brief Destroys Use operands when the number of operands of
    120   /// a User changes.
    121   static void zap(Use *Start, const Use *Stop, bool del = false);
    122 
    123 private:
    124   const Use *getImpliedUser() const;
    125 
    126   Value *Val;
    127   Use *Next;
    128   PointerIntPair<Use **, 2, PrevPtrTag> Prev;
    129 
    130   void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); }
    131   void addToList(Use **List) {
    132     Next = *List;
    133     if (Next)
    134       Next->setPrev(&Next);
    135     setPrev(List);
    136     *List = this;
    137   }
    138   void removeFromList() {
    139     Use **StrippedPrev = Prev.getPointer();
    140     *StrippedPrev = Next;
    141     if (Next)
    142       Next->setPrev(StrippedPrev);
    143   }
    144 
    145   friend class Value;
    146 };
    147 
    148 /// \brief Allow clients to treat uses just like values when using
    149 /// casting operators.
    150 template <> struct simplify_type<Use> {
    151   typedef Value *SimpleType;
    152   static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
    153 };
    154 template <> struct simplify_type<const Use> {
    155   typedef /*const*/ Value *SimpleType;
    156   static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
    157 };
    158 
    159 // Create wrappers for C Binding types (see CBindingWrapping.h).
    160 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
    161 
    162 }
    163 
    164 #endif
    165