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