Use.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===- llvm/Use.h - Definition of the Use class -----------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  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 to a Use
  15. /// without having to store a User pointer in every Use. A User is preceded in
  16. /// memory by all the Uses corresponding to its operands, and the low bits of
  17. /// one of the fields (Prev) of the Use class are used to encode offsets to be
  18. /// able to find that User given a pointer to any Use. For details, see:
  19. ///
  20. /// http://www.llvm.org/docs/ProgrammersManual.html#UserLayout
  21. ///
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_IR_USE_H
  24. #define LLVM_IR_USE_H
  25. #include "llvm-c/Types.h"
  26. #include "llvm/ADT/PointerIntPair.h"
  27. #include "llvm/Support/CBindingWrapping.h"
  28. #include "llvm/Support/Compiler.h"
  29. namespace llvm {
  30. template <typename> struct simplify_type;
  31. class User;
  32. class Value;
  33. /// A Use represents the edge between a Value definition and its users.
  34. ///
  35. /// This is notionally a two-dimensional linked list. It supports traversing
  36. /// all of the uses for a particular value definition. It also supports jumping
  37. /// directly to the used value when we arrive from the User's operands, and
  38. /// jumping directly to the User when we arrive from the Value's uses.
  39. class Use {
  40. public:
  41. Use(const Use &U) = delete;
  42. /// Provide a fast substitute to std::swap<Use>
  43. /// that also works with less standard-compliant compilers
  44. void swap(Use &RHS);
  45. private:
  46. /// Destructor - Only for zap()
  47. ~Use() {
  48. if (Val)
  49. removeFromList();
  50. }
  51. /// Constructor
  52. Use(User *Parent) : Parent(Parent) {}
  53. public:
  54. friend class Value;
  55. friend class User;
  56. operator Value *() const { return Val; }
  57. Value *get() const { return Val; }
  58. /// Returns the User that contains this Use.
  59. ///
  60. /// For an instruction operand, for example, this will return the
  61. /// instruction.
  62. User *getUser() const { return Parent; };
  63. inline void set(Value *Val);
  64. inline Value *operator=(Value *RHS);
  65. inline const Use &operator=(const Use &RHS);
  66. Value *operator->() { return Val; }
  67. const Value *operator->() const { return Val; }
  68. Use *getNext() const { return Next; }
  69. /// Return the operand # of this use in its User.
  70. unsigned getOperandNo() const;
  71. /// Destroys Use operands when the number of operands of
  72. /// a User changes.
  73. static void zap(Use *Start, const Use *Stop, bool del = false);
  74. private:
  75. Value *Val = nullptr;
  76. Use *Next = nullptr;
  77. Use **Prev = nullptr;
  78. User *Parent = nullptr;
  79. void addToList(Use **List) {
  80. Next = *List;
  81. if (Next)
  82. Next->Prev = &Next;
  83. Prev = List;
  84. *Prev = this;
  85. }
  86. void removeFromList() {
  87. *Prev = Next;
  88. if (Next)
  89. Next->Prev = Prev;
  90. }
  91. };
  92. /// Allow clients to treat uses just like values when using
  93. /// casting operators.
  94. template <> struct simplify_type<Use> {
  95. using SimpleType = Value *;
  96. static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
  97. };
  98. template <> struct simplify_type<const Use> {
  99. using SimpleType = /*const*/ Value *;
  100. static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
  101. };
  102. // Create wrappers for C Binding types (see CBindingWrapping.h).
  103. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
  104. } // end namespace llvm
  105. #endif // LLVM_IR_USE_H