Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.1 KiB

  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. #ifndef LLVM_IR_USE_H
  25. #define LLVM_IR_USE_H
  26. #include "llvm/ADT/PointerIntPair.h"
  27. #include "llvm/Support/Compiler.h"
  28. #include <cstddef>
  29. #include <iterator>
  30. namespace llvm {
  31. class Value;
  32. class User;
  33. class Use;
  34. template<typename>
  35. struct simplify_type;
  36. // Use** is only 4-byte aligned.
  37. template<>
  38. class PointerLikeTypeTraits<Use**> {
  39. public:
  40. static inline void *getAsVoidPointer(Use** P) { return P; }
  41. static inline Use **getFromVoidPointer(void *P) {
  42. return static_cast<Use**>(P);
  43. }
  44. enum { NumLowBitsAvailable = 2 };
  45. };
  46. //===----------------------------------------------------------------------===//
  47. // Use Class
  48. //===----------------------------------------------------------------------===//
  49. /// Use is here to make keeping the "use" list of a Value up-to-date really
  50. /// easy.
  51. class Use {
  52. public:
  53. /// swap - provide a fast substitute to std::swap<Use>
  54. /// that also works with less standard-compliant compilers
  55. void swap(Use &RHS);
  56. // A type for the word following an array of hung-off Uses in memory, which is
  57. // a pointer back to their User with the bottom bit set.
  58. typedef PointerIntPair<User*, 1, unsigned> UserRef;
  59. private:
  60. Use(const Use &U) LLVM_DELETED_FUNCTION;
  61. /// Destructor - Only for zap()
  62. ~Use() {
  63. if (Val) removeFromList();
  64. }
  65. enum PrevPtrTag { zeroDigitTag
  66. , oneDigitTag
  67. , stopTag
  68. , fullStopTag };
  69. /// Constructor
  70. Use(PrevPtrTag tag) : Val(0) {
  71. Prev.setInt(tag);
  72. }
  73. public:
  74. /// Normally Use will just implicitly convert to a Value* that it holds.
  75. operator Value*() const { return Val; }
  76. /// If implicit conversion to Value* doesn't work, the get() method returns
  77. /// the Value*.
  78. Value *get() const { return Val; }
  79. /// getUser - This returns the User that contains this Use. For an
  80. /// instruction operand, for example, this will return the instruction.
  81. User *getUser() const;
  82. inline void set(Value *Val);
  83. Value *operator=(Value *RHS) {
  84. set(RHS);
  85. return RHS;
  86. }
  87. const Use &operator=(const Use &RHS) {
  88. set(RHS.Val);
  89. return *this;
  90. }
  91. Value *operator->() { return Val; }
  92. const Value *operator->() const { return Val; }
  93. Use *getNext() const { return Next; }
  94. /// initTags - initialize the waymarking tags on an array of Uses, so that
  95. /// getUser() can find the User from any of those Uses.
  96. static Use *initTags(Use *Start, Use *Stop);
  97. /// zap - This is used to destroy Use operands when the number of operands of
  98. /// a User changes.
  99. static void zap(Use *Start, const Use *Stop, bool del = false);
  100. private:
  101. const Use* getImpliedUser() const;
  102. Value *Val;
  103. Use *Next;
  104. PointerIntPair<Use**, 2, PrevPtrTag> Prev;
  105. void setPrev(Use **NewPrev) {
  106. Prev.setPointer(NewPrev);
  107. }
  108. void addToList(Use **List) {
  109. Next = *List;
  110. if (Next) Next->setPrev(&Next);
  111. setPrev(List);
  112. *List = this;
  113. }
  114. void removeFromList() {
  115. Use **StrippedPrev = Prev.getPointer();
  116. *StrippedPrev = Next;
  117. if (Next) Next->setPrev(StrippedPrev);
  118. }
  119. friend class Value;
  120. };
  121. // simplify_type - Allow clients to treat uses just like values when using
  122. // casting operators.
  123. template<> struct simplify_type<Use> {
  124. typedef Value* SimpleType;
  125. static SimpleType getSimplifiedValue(Use &Val) {
  126. return Val.get();
  127. }
  128. };
  129. template<> struct simplify_type<const Use> {
  130. typedef /*const*/ Value* SimpleType;
  131. static SimpleType getSimplifiedValue(const Use &Val) {
  132. return Val.get();
  133. }
  134. };
  135. template<typename UserTy> // UserTy == 'User' or 'const User'
  136. class value_use_iterator : public std::iterator<std::forward_iterator_tag,
  137. UserTy*, ptrdiff_t> {
  138. typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
  139. typedef value_use_iterator<UserTy> _Self;
  140. Use *U;
  141. explicit value_use_iterator(Use *u) : U(u) {}
  142. friend class Value;
  143. public:
  144. typedef typename super::reference reference;
  145. typedef typename super::pointer pointer;
  146. value_use_iterator(const _Self &I) : U(I.U) {}
  147. value_use_iterator() {}
  148. bool operator==(const _Self &x) const {
  149. return U == x.U;
  150. }
  151. bool operator!=(const _Self &x) const {
  152. return !operator==(x);
  153. }
  154. /// atEnd - return true if this iterator is equal to use_end() on the value.
  155. bool atEnd() const { return U == 0; }
  156. // Iterator traversal: forward iteration only
  157. _Self &operator++() { // Preincrement
  158. assert(U && "Cannot increment end iterator!");
  159. U = U->getNext();
  160. return *this;
  161. }
  162. _Self operator++(int) { // Postincrement
  163. _Self tmp = *this; ++*this; return tmp;
  164. }
  165. // Retrieve a pointer to the current User.
  166. UserTy *operator*() const {
  167. assert(U && "Cannot dereference end iterator!");
  168. return U->getUser();
  169. }
  170. UserTy *operator->() const { return operator*(); }
  171. Use &getUse() const { return *U; }
  172. /// getOperandNo - Return the operand # of this use in its User. Defined in
  173. /// User.h
  174. ///
  175. unsigned getOperandNo() const;
  176. };
  177. } // End llvm namespace
  178. #endif