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.

220 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_USE_H
  25. #define LLVM_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. /// Copy ctor - do not implement
  61. Use(const Use &U) LLVM_DELETED_FUNCTION;
  62. /// Destructor - Only for zap()
  63. ~Use() {
  64. if (Val) removeFromList();
  65. }
  66. enum PrevPtrTag { zeroDigitTag
  67. , oneDigitTag
  68. , stopTag
  69. , fullStopTag };
  70. /// Constructor
  71. Use(PrevPtrTag tag) : Val(0) {
  72. Prev.setInt(tag);
  73. }
  74. public:
  75. /// Normally Use will just implicitly convert to a Value* that it holds.
  76. operator Value*() const { return Val; }
  77. /// If implicit conversion to Value* doesn't work, the get() method returns
  78. /// the Value*.
  79. Value *get() const { return Val; }
  80. /// getUser - This returns the User that contains this Use. For an
  81. /// instruction operand, for example, this will return the instruction.
  82. User *getUser() const;
  83. inline void set(Value *Val);
  84. Value *operator=(Value *RHS) {
  85. set(RHS);
  86. return RHS;
  87. }
  88. const Use &operator=(const Use &RHS) {
  89. set(RHS.Val);
  90. return *this;
  91. }
  92. Value *operator->() { return Val; }
  93. const Value *operator->() const { return Val; }
  94. Use *getNext() const { return Next; }
  95. /// initTags - initialize the waymarking tags on an array of Uses, so that
  96. /// getUser() can find the User from any of those Uses.
  97. static Use *initTags(Use *Start, Use *Stop);
  98. /// zap - This is used to destroy Use operands when the number of operands of
  99. /// a User changes.
  100. static void zap(Use *Start, const Use *Stop, bool del = false);
  101. private:
  102. const Use* getImpliedUser() const;
  103. Value *Val;
  104. Use *Next;
  105. PointerIntPair<Use**, 2, PrevPtrTag> Prev;
  106. void setPrev(Use **NewPrev) {
  107. Prev.setPointer(NewPrev);
  108. }
  109. void addToList(Use **List) {
  110. Next = *List;
  111. if (Next) Next->setPrev(&Next);
  112. setPrev(List);
  113. *List = this;
  114. }
  115. void removeFromList() {
  116. Use **StrippedPrev = Prev.getPointer();
  117. *StrippedPrev = Next;
  118. if (Next) Next->setPrev(StrippedPrev);
  119. }
  120. friend class Value;
  121. };
  122. // simplify_type - Allow clients to treat uses just like values when using
  123. // casting operators.
  124. template<> struct simplify_type<Use> {
  125. typedef Value* SimpleType;
  126. static SimpleType getSimplifiedValue(const Use &Val) {
  127. return static_cast<SimpleType>(Val.get());
  128. }
  129. };
  130. template<> struct simplify_type<const Use> {
  131. typedef Value* SimpleType;
  132. static SimpleType getSimplifiedValue(const Use &Val) {
  133. return static_cast<SimpleType>(Val.get());
  134. }
  135. };
  136. template<typename UserTy> // UserTy == 'User' or 'const User'
  137. class value_use_iterator : public std::iterator<std::forward_iterator_tag,
  138. UserTy*, ptrdiff_t> {
  139. typedef std::iterator<std::forward_iterator_tag, UserTy*, ptrdiff_t> super;
  140. typedef value_use_iterator<UserTy> _Self;
  141. Use *U;
  142. explicit value_use_iterator(Use *u) : U(u) {}
  143. friend class Value;
  144. public:
  145. typedef typename super::reference reference;
  146. typedef typename super::pointer pointer;
  147. value_use_iterator(const _Self &I) : U(I.U) {}
  148. value_use_iterator() {}
  149. bool operator==(const _Self &x) const {
  150. return U == x.U;
  151. }
  152. bool operator!=(const _Self &x) const {
  153. return !operator==(x);
  154. }
  155. /// atEnd - return true if this iterator is equal to use_end() on the value.
  156. bool atEnd() const { return U == 0; }
  157. // Iterator traversal: forward iteration only
  158. _Self &operator++() { // Preincrement
  159. assert(U && "Cannot increment end iterator!");
  160. U = U->getNext();
  161. return *this;
  162. }
  163. _Self operator++(int) { // Postincrement
  164. _Self tmp = *this; ++*this; return tmp;
  165. }
  166. // Retrieve a pointer to the current User.
  167. UserTy *operator*() const {
  168. assert(U && "Cannot dereference end iterator!");
  169. return U->getUser();
  170. }
  171. UserTy *operator->() const { return operator*(); }
  172. Use &getUse() const { return *U; }
  173. /// getOperandNo - Return the operand # of this use in its User. Defined in
  174. /// User.h
  175. ///
  176. unsigned getOperandNo() const;
  177. };
  178. } // End llvm namespace
  179. #endif