Use.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/ADT/PointerIntPair.h"
  32. #include "llvm/Support/CBindingWrapping.h"
  33. #include "llvm/Support/Compiler.h"
  34. namespace llvm {
  35. template <typename> struct simplify_type;
  36. class User;
  37. class Value;
  38. /// A Use represents the edge between a Value definition and its users.
  39. ///
  40. /// This is notionally a two-dimensional linked list. It supports traversing
  41. /// all of the uses for a particular value definition. It also supports jumping
  42. /// directly to the used value when we arrive from the User's operands, and
  43. /// jumping directly to the User when we arrive from the Value's uses.
  44. class Use {
  45. public:
  46. Use(const Use &U) = delete;
  47. /// Provide a fast substitute to std::swap<Use>
  48. /// that also works with less standard-compliant compilers
  49. void swap(Use &RHS);
  50. private:
  51. /// Destructor - Only for zap()
  52. ~Use() {
  53. if (Val)
  54. removeFromList();
  55. }
  56. /// Constructor
  57. Use(User *Parent) : Parent(Parent) {}
  58. public:
  59. friend class Value;
  60. friend class User;
  61. operator Value *() const { return Val; }
  62. Value *get() const { return Val; }
  63. /// Returns the User that contains this Use.
  64. ///
  65. /// For an instruction operand, for example, this will return the
  66. /// instruction.
  67. User *getUser() const { return Parent; };
  68. inline void set(Value *Val);
  69. inline Value *operator=(Value *RHS);
  70. inline const Use &operator=(const Use &RHS);
  71. Value *operator->() { return Val; }
  72. const Value *operator->() const { return Val; }
  73. Use *getNext() const { return Next; }
  74. /// Return the operand # of this use in its User.
  75. unsigned getOperandNo() const;
  76. /// Destroys Use operands when the number of operands of
  77. /// a User changes.
  78. static void zap(Use *Start, const Use *Stop, bool del = false);
  79. private:
  80. Value *Val = nullptr;
  81. Use *Next = nullptr;
  82. Use **Prev = nullptr;
  83. User *Parent = nullptr;
  84. void addToList(Use **List) {
  85. Next = *List;
  86. if (Next)
  87. Next->Prev = &Next;
  88. Prev = List;
  89. *Prev = this;
  90. }
  91. void removeFromList() {
  92. *Prev = Next;
  93. if (Next)
  94. Next->Prev = Prev;
  95. }
  96. };
  97. /// Allow clients to treat uses just like values when using
  98. /// casting operators.
  99. template <> struct simplify_type<Use> {
  100. using SimpleType = Value *;
  101. static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
  102. };
  103. template <> struct simplify_type<const Use> {
  104. using SimpleType = /*const*/ Value *;
  105. static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
  106. };
  107. // Create wrappers for C Binding types (see CBindingWrapping.h).
  108. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
  109. } // end namespace llvm
  110. #endif // LLVM_IR_USE_H
  111. #ifdef __GNUC__
  112. #pragma GCC diagnostic pop
  113. #endif