LLVM API Documentation
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 /// \file 00010 /// 00011 /// This defines the Use class. The Use class represents the operand of an 00012 /// instruction or some other User instance which refers to a Value. The Use 00013 /// class keeps the "use list" of the referenced value up to date. 00014 /// 00015 /// Pointer tagging is used to efficiently find the User corresponding to a Use 00016 /// without having to store a User pointer in every Use. A User is preceded in 00017 /// memory by all the Uses corresponding to its operands, and the low bits of 00018 /// one of the fields (Prev) of the Use class are used to encode offsets to be 00019 /// able to find that User given 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-c/Core.h" 00029 #include "llvm/ADT/PointerIntPair.h" 00030 #include "llvm/Support/CBindingWrapping.h" 00031 #include "llvm/Support/Compiler.h" 00032 #include <cstddef> 00033 #include <iterator> 00034 00035 namespace llvm { 00036 00037 class Value; 00038 class User; 00039 class Use; 00040 template <typename> struct simplify_type; 00041 00042 // Use** is only 4-byte aligned. 00043 template <> class PointerLikeTypeTraits<Use **> { 00044 public: 00045 static inline void *getAsVoidPointer(Use **P) { return P; } 00046 static inline Use **getFromVoidPointer(void *P) { 00047 return static_cast<Use **>(P); 00048 } 00049 enum { NumLowBitsAvailable = 2 }; 00050 }; 00051 00052 /// \brief A Use represents the edge between a Value definition and its users. 00053 /// 00054 /// This is notionally a two-dimensional linked list. It supports traversing 00055 /// all of the uses for a particular value definition. It also supports jumping 00056 /// directly to the used value when we arrive from the User's operands, and 00057 /// jumping directly to the User when we arrive from the Value's uses. 00058 /// 00059 /// The pointer to the used Value is explicit, and the pointer to the User is 00060 /// implicit. The implicit pointer is found via a waymarking algorithm 00061 /// described in the programmer's manual: 00062 /// 00063 /// http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm 00064 /// 00065 /// This is essentially the single most memory intensive object in LLVM because 00066 /// of the number of uses in the system. At the same time, the constant time 00067 /// operations it allows are essential to many optimizations having reasonable 00068 /// time complexity. 00069 class Use { 00070 public: 00071 /// \brief Provide a fast substitute to std::swap<Use> 00072 /// that also works with less standard-compliant compilers 00073 void swap(Use &RHS); 00074 00075 // A type for the word following an array of hung-off Uses in memory, which is 00076 // a pointer back to their User with the bottom bit set. 00077 typedef PointerIntPair<User *, 1, unsigned> UserRef; 00078 00079 private: 00080 Use(const Use &U) LLVM_DELETED_FUNCTION; 00081 00082 /// Destructor - Only for zap() 00083 ~Use() { 00084 if (Val) 00085 removeFromList(); 00086 } 00087 00088 enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag }; 00089 00090 /// Constructor 00091 Use(PrevPtrTag tag) : Val(nullptr) { Prev.setInt(tag); } 00092 00093 public: 00094 operator Value *() const { return Val; } 00095 Value *get() const { return Val; } 00096 00097 /// \brief Returns the User that contains this Use. 00098 /// 00099 /// For an instruction operand, for example, this will return the 00100 /// instruction. 00101 User *getUser() const; 00102 00103 inline void set(Value *Val); 00104 00105 Value *operator=(Value *RHS) { 00106 set(RHS); 00107 return RHS; 00108 } 00109 const Use &operator=(const Use &RHS) { 00110 set(RHS.Val); 00111 return *this; 00112 } 00113 00114 Value *operator->() { return Val; } 00115 const Value *operator->() const { return Val; } 00116 00117 Use *getNext() const { return Next; } 00118 00119 /// \brief Return the operand # of this use in its User. 00120 unsigned getOperandNo() const; 00121 00122 /// \brief Initializes the waymarking tags on an array of Uses. 00123 /// 00124 /// This sets up the array of Uses such that getUser() can find the User from 00125 /// any of those Uses. 00126 static Use *initTags(Use *Start, Use *Stop); 00127 00128 /// \brief Destroys Use operands when the number of operands of 00129 /// a User changes. 00130 static void zap(Use *Start, const Use *Stop, bool del = false); 00131 00132 private: 00133 const Use *getImpliedUser() const; 00134 00135 Value *Val; 00136 Use *Next; 00137 PointerIntPair<Use **, 2, PrevPtrTag> Prev; 00138 00139 void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); } 00140 void addToList(Use **List) { 00141 Next = *List; 00142 if (Next) 00143 Next->setPrev(&Next); 00144 setPrev(List); 00145 *List = this; 00146 } 00147 void removeFromList() { 00148 Use **StrippedPrev = Prev.getPointer(); 00149 *StrippedPrev = Next; 00150 if (Next) 00151 Next->setPrev(StrippedPrev); 00152 } 00153 00154 friend class Value; 00155 }; 00156 00157 /// \brief Allow clients to treat uses just like values when using 00158 /// casting operators. 00159 template <> struct simplify_type<Use> { 00160 typedef Value *SimpleType; 00161 static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); } 00162 }; 00163 template <> struct simplify_type<const Use> { 00164 typedef /*const*/ Value *SimpleType; 00165 static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); } 00166 }; 00167 00168 // Create wrappers for C Binding types (see CBindingWrapping.h). 00169 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef) 00170 00171 } 00172 00173 #endif