[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / include / llvm / IR / Use.h
blob034ca2c8ac23ecc03a140a4600640c219365c08e
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 //===----------------------------------------------------------------------===//
24 #ifndef LLVM_IR_USE_H
25 #define LLVM_IR_USE_H
27 #include "llvm-c/Types.h"
28 #include "llvm/ADT/PointerIntPair.h"
29 #include "llvm/Support/CBindingWrapping.h"
30 #include "llvm/Support/Compiler.h"
32 namespace llvm {
34 template <typename> struct simplify_type;
35 class User;
36 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 ///
45 /// The pointer to the used Value is explicit, and the pointer to the User is
46 /// implicit. The implicit pointer is found via a waymarking algorithm
47 /// described in the programmer's manual:
48 ///
49 /// http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
50 ///
51 /// This is essentially the single most memory intensive object in LLVM because
52 /// of the number of uses in the system. At the same time, the constant time
53 /// operations it allows are essential to many optimizations having reasonable
54 /// time complexity.
55 class Use {
56 public:
57 Use(const Use &U) = delete;
59 /// Provide a fast substitute to std::swap<Use>
60 /// that also works with less standard-compliant compilers
61 void swap(Use &RHS);
63 /// Pointer traits for the UserRef PointerIntPair. This ensures we always
64 /// use the LSB regardless of pointer alignment on different targets.
65 struct UserRefPointerTraits {
66 static inline void *getAsVoidPointer(User *P) { return P; }
68 static inline User *getFromVoidPointer(void *P) {
69 return (User *)P;
72 enum { NumLowBitsAvailable = 1 };
75 // A type for the word following an array of hung-off Uses in memory, which is
76 // a pointer back to their User with the bottom bit set.
77 using UserRef = PointerIntPair<User *, 1, unsigned, UserRefPointerTraits>;
79 /// Pointer traits for the Prev PointerIntPair. This ensures we always use
80 /// the two LSBs regardless of pointer alignment on different targets.
81 struct PrevPointerTraits {
82 static inline void *getAsVoidPointer(Use **P) { return P; }
84 static inline Use **getFromVoidPointer(void *P) {
85 return (Use **)P;
88 enum { NumLowBitsAvailable = 2 };
91 private:
92 /// Destructor - Only for zap()
93 ~Use() {
94 if (Val)
95 removeFromList();
98 enum PrevPtrTag { zeroDigitTag, oneDigitTag, stopTag, fullStopTag };
100 /// Constructor
101 Use(PrevPtrTag tag) { Prev.setInt(tag); }
103 public:
104 friend class Value;
106 operator Value *() const { return Val; }
107 Value *get() const { return Val; }
109 /// Returns the User that contains this Use.
111 /// For an instruction operand, for example, this will return the
112 /// instruction.
113 User *getUser() const LLVM_READONLY;
115 inline void set(Value *Val);
117 inline Value *operator=(Value *RHS);
118 inline const Use &operator=(const Use &RHS);
120 Value *operator->() { return Val; }
121 const Value *operator->() const { return Val; }
123 Use *getNext() const { return Next; }
125 /// Return the operand # of this use in its User.
126 unsigned getOperandNo() const;
128 /// Initializes the waymarking tags on an array of Uses.
130 /// This sets up the array of Uses such that getUser() can find the User from
131 /// any of those Uses.
132 static Use *initTags(Use *Start, Use *Stop);
134 /// Destroys Use operands when the number of operands of
135 /// a User changes.
136 static void zap(Use *Start, const Use *Stop, bool del = false);
138 private:
139 const Use *getImpliedUser() const LLVM_READONLY;
141 Value *Val = nullptr;
142 Use *Next = nullptr;
143 PointerIntPair<Use **, 2, PrevPtrTag, PrevPointerTraits> Prev;
145 void setPrev(Use **NewPrev) { Prev.setPointer(NewPrev); }
147 void addToList(Use **List) {
148 Next = *List;
149 if (Next)
150 Next->setPrev(&Next);
151 setPrev(List);
152 *List = this;
155 void removeFromList() {
156 Use **StrippedPrev = Prev.getPointer();
157 *StrippedPrev = Next;
158 if (Next)
159 Next->setPrev(StrippedPrev);
163 /// Allow clients to treat uses just like values when using
164 /// casting operators.
165 template <> struct simplify_type<Use> {
166 using SimpleType = Value *;
168 static SimpleType getSimplifiedValue(Use &Val) { return Val.get(); }
170 template <> struct simplify_type<const Use> {
171 using SimpleType = /*const*/ Value *;
173 static SimpleType getSimplifiedValue(const Use &Val) { return Val.get(); }
176 // Create wrappers for C Binding types (see CBindingWrapping.h).
177 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef)
179 } // end namespace llvm
181 #endif // LLVM_IR_USE_H