Use.h 3.9 KB

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