Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Use.h - Definition of the Use class -----------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
| 24 | #ifndef LLVM_IR_USE_H |
| 25 | #define LLVM_IR_USE_H |
| 26 | |
| 27 | #include "llvm-c/Types.h" |
| 28 | #include "llvm/ADT/PointerIntPair.h" |
| 29 | #include "llvm/Support/CBindingWrapping.h" |
| 30 | #include "llvm/Support/Compiler.h" |
| 31 | |
| 32 | namespace llvm { |
| 33 | |
| 34 | template <typename> struct simplify_type; |
| 35 | class User; |
| 36 | class Value; |
| 37 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 38 | /// A Use represents the edge between a Value definition and its users. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 44 | class Use { |
| 45 | public: |
| 46 | Use(const Use &U) = delete; |
| 47 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | /// Provide a fast substitute to std::swap<Use> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | /// that also works with less standard-compliant compilers |
| 50 | void swap(Use &RHS); |
| 51 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | private: |
| 53 | /// Destructor - Only for zap() |
| 54 | ~Use() { |
| 55 | if (Val) |
| 56 | removeFromList(); |
| 57 | } |
| 58 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | /// Constructor |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 60 | Use(User *Parent) : Parent(Parent) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | |
| 62 | public: |
| 63 | friend class Value; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 64 | friend class User; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | |
| 66 | operator Value *() const { return Val; } |
| 67 | Value *get() const { return Val; } |
| 68 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 69 | /// Returns the User that contains this Use. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | /// |
| 71 | /// For an instruction operand, for example, this will return the |
| 72 | /// instruction. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 73 | User *getUser() const { return Parent; }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | |
| 75 | inline void set(Value *Val); |
| 76 | |
| 77 | inline Value *operator=(Value *RHS); |
| 78 | inline const Use &operator=(const Use &RHS); |
| 79 | |
| 80 | Value *operator->() { return Val; } |
| 81 | const Value *operator->() const { return Val; } |
| 82 | |
| 83 | Use *getNext() const { return Next; } |
| 84 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 85 | /// Return the operand # of this use in its User. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 86 | unsigned getOperandNo() const; |
| 87 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 88 | /// Destroys Use operands when the number of operands of |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 89 | /// a User changes. |
| 90 | static void zap(Use *Start, const Use *Stop, bool del = false); |
| 91 | |
| 92 | private: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 93 | |
| 94 | Value *Val = nullptr; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 95 | Use *Next = nullptr; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 96 | Use **Prev = nullptr; |
| 97 | User *Parent = nullptr; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 98 | |
| 99 | void addToList(Use **List) { |
| 100 | Next = *List; |
| 101 | if (Next) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 102 | Next->Prev = &Next; |
| 103 | Prev = List; |
| 104 | *Prev = this; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void removeFromList() { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 108 | *Prev = Next; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | if (Next) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 110 | Next->Prev = Prev; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 114 | /// Allow clients to treat uses just like values when using |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 115 | /// casting operators. |
| 116 | template <> struct simplify_type<Use> { |
| 117 | using SimpleType = Value *; |
| 118 | |
| 119 | static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); } |
| 120 | }; |
| 121 | template <> struct simplify_type<const Use> { |
| 122 | using SimpleType = /*const*/ Value *; |
| 123 | |
| 124 | static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); } |
| 125 | }; |
| 126 | |
| 127 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 128 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef) |
| 129 | |
| 130 | } // end namespace llvm |
| 131 | |
| 132 | #endif // LLVM_IR_USE_H |