]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/Use.h
Merge llvm, clang, lld and lldb trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / Use.h
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   /// Pointer traits for the UserRef PointerIntPair. This ensures we always
65   /// use the LSB regardless of pointer alignment on different targets.
66   struct UserRefPointerTraits {
67     static inline void *getAsVoidPointer(User *P) { return P; }
68     static inline User *getFromVoidPointer(void *P) {
69       return (User *)P;
70     }
71     enum { NumLowBitsAvailable = 1 };
72   };
73
74   // A type for the word following an array of hung-off Uses in memory, which is
75   // a pointer back to their User with the bottom bit set.
76   typedef PointerIntPair<User *, 1, unsigned, UserRefPointerTraits> UserRef;
77
78   /// Pointer traits for the Prev PointerIntPair. This ensures we always use
79   /// the two LSBs regardless of pointer alignment on different targets.
80   struct PrevPointerTraits {
81     static inline void *getAsVoidPointer(Use **P) { return P; }
82     static inline Use **getFromVoidPointer(void *P) {
83       return (Use **)P;
84     }
85     enum { NumLowBitsAvailable = 2 };
86   };
87
88 private:
89   /// Destructor - Only for zap()
90   ~Use() {
91     if (Val)
92       removeFromList();
93   }
94
95   enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag };
96
97   /// Constructor
98   Use(PrevPtrTag tag) : Val(nullptr) { Prev.setInt(tag); }
99
100 public:
101   operator Value *() const { return Val; }
102   Value *get() const { return Val; }
103
104   /// \brief Returns the User that contains this Use.
105   ///
106   /// For an instruction operand, for example, this will return the
107   /// instruction.
108   User *getUser() const LLVM_READONLY;
109
110   inline void set(Value *Val);
111
112   inline Value *operator=(Value *RHS);
113   inline const Use &operator=(const Use &RHS);
114
115   Value *operator->() { return Val; }
116   const Value *operator->() const { return Val; }
117
118   Use *getNext() const { return Next; }
119
120   /// \brief Return the operand # of this use in its User.
121   unsigned getOperandNo() const;
122
123   /// \brief Initializes the waymarking tags on an array of Uses.
124   ///
125   /// This sets up the array of Uses such that getUser() can find the User from
126   /// any of those Uses.
127   static Use *initTags(Use *Start, Use *Stop);
128
129   /// \brief Destroys Use operands when the number of operands of
130   /// a User changes.
131   static void zap(Use *Start, const Use *Stop, bool del = false);
132
133 private:
134   const Use *getImpliedUser() const LLVM_READONLY;
135
136   Value *Val;
137   Use *Next;
138   PointerIntPair<Use **, 2, PrevPtrTag, PrevPointerTraits> Prev;
139
140   void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); }
141
142   void addToList(Use **List) {
143     Next = *List;
144     if (Next)
145       Next->setPrev(&Next);
146     setPrev(List);
147     *List = this;
148   }
149
150   void removeFromList() {
151     Use **StrippedPrev = Prev.getPointer();
152     *StrippedPrev = Next;
153     if (Next)
154       Next->setPrev(StrippedPrev);
155   }
156
157   friend class Value;
158 };
159
160 /// \brief Allow clients to treat uses just like values when using
161 /// casting operators.
162 template <> struct simplify_type<Use> {
163   typedef Value *SimpleType;
164   static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
165 };
166 template <> struct simplify_type<const Use> {
167   typedef /*const*/ Value *SimpleType;
168   static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
169 };
170
171 // Create wrappers for C Binding types (see CBindingWrapping.h).
172 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
173
174 } // end namespace llvm
175
176 #endif // LLVM_IR_USE_H