Use.h 5.2 KB

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