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