LLVM 20.0.0git
Value.h
Go to the documentation of this file.
1//===- Value.h --------------------------------------------------*- 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
9#ifndef LLVM_SANDBOXIR_VALUE_H
10#define LLVM_SANDBOXIR_VALUE_H
11
12#include "llvm/IR/Value.h"
13#include "llvm/SandboxIR/Use.h"
14
15namespace llvm::sandboxir {
16
17// Forward declare all classes to avoid some MSVC build errors.
18#define DEF_INSTR(ID, OPC, CLASS) class CLASS;
19#define DEF_CONST(ID, CLASS) class CLASS;
20#define DEF_USER(ID, CLASS) class CLASS;
21#include "llvm/SandboxIR/Values.def"
22class Context;
23class FuncletPadInst;
24class Type;
25class GlobalValue;
26class GlobalObject;
27class Module;
28class UnaryInstruction;
29class CmpInst;
30class IntrinsicInst;
31class Operator;
32class OverflowingBinaryOperator;
33class FPMathOperator;
34
35/// Iterator for the `Use` edges of a Value's users.
36/// \Returns a `Use` when dereferenced.
39 /// Don't let the user create a non-empty UserUseIterator.
40 UserUseIterator(const class Use &Use) : Use(Use) {}
41 friend class Value; // For constructor
42
43public:
44 using difference_type = std::ptrdiff_t;
48 using iterator_category = std::input_iterator_tag;
49
50 UserUseIterator() = default;
51 value_type operator*() const { return Use; }
53 bool operator==(const UserUseIterator &Other) const {
54 return Use == Other.Use;
55 }
56 bool operator!=(const UserUseIterator &Other) const {
57 return !(*this == Other);
58 }
59 const sandboxir::Use &getUse() const { return Use; }
60};
61
62/// A SandboxIR Value has users. This is the base class.
63class Value {
64public:
65 enum class ClassID : unsigned {
66#define DEF_VALUE(ID, CLASS) ID,
67#define DEF_USER(ID, CLASS) ID,
68#define DEF_CONST(ID, CLASS) ID,
69#define DEF_INSTR(ID, OPC, CLASS) ID,
70#include "llvm/SandboxIR/Values.def"
71 };
72
73protected:
74 static const char *getSubclassIDStr(ClassID ID) {
75 switch (ID) {
76#define DEF_VALUE(ID, CLASS) \
77 case ClassID::ID: \
78 return #ID;
79#define DEF_USER(ID, CLASS) \
80 case ClassID::ID: \
81 return #ID;
82#define DEF_CONST(ID, CLASS) \
83 case ClassID::ID: \
84 return #ID;
85#define DEF_INSTR(ID, OPC, CLASS) \
86 case ClassID::ID: \
87 return #ID;
88#include "llvm/SandboxIR/Values.def"
89 }
90 llvm_unreachable("Unimplemented ID");
91 }
92
93 /// For isa/dyn_cast.
95#ifndef NDEBUG
96 /// A unique ID used for forming the name (used for debugging).
97 unsigned UID;
98#endif
99 /// The LLVM Value that corresponds to this SandboxIR Value.
100 /// NOTE: Some sandboxir Instructions, like Packs, may include more than one
101 /// value and in these cases `Val` points to the last instruction in program
102 /// order.
103 llvm::Value *Val = nullptr;
104
105 friend class Context; // For getting `Val`.
106 friend class User; // For getting `Val`.
107 friend class Use; // For getting `Val`.
108 friend class VAArgInst; // For getting `Val`.
109 friend class FreezeInst; // For getting `Val`.
110 friend class FenceInst; // For getting `Val`.
111 friend class SelectInst; // For getting `Val`.
112 friend class ExtractElementInst; // For getting `Val`.
113 friend class InsertElementInst; // For getting `Val`.
114 friend class ShuffleVectorInst; // For getting `Val`.
115 friend class ExtractValueInst; // For getting `Val`.
116 friend class InsertValueInst; // For getting `Val`.
117 friend class BranchInst; // For getting `Val`.
118 friend class LoadInst; // For getting `Val`.
119 friend class StoreInst; // For getting `Val`.
120 friend class ReturnInst; // For getting `Val`.
121 friend class CallBase; // For getting `Val`.
122 friend class CallInst; // For getting `Val`.
123 friend class InvokeInst; // For getting `Val`.
124 friend class CallBrInst; // For getting `Val`.
125 friend class LandingPadInst; // For getting `Val`.
126 friend class FuncletPadInst; // For getting `Val`.
127 friend class CatchPadInst; // For getting `Val`.
128 friend class CleanupPadInst; // For getting `Val`.
129 friend class CatchReturnInst; // For getting `Val`.
130 friend class GetElementPtrInst; // For getting `Val`.
131 friend class ResumeInst; // For getting `Val`.
132 friend class CatchSwitchInst; // For getting `Val`.
133 friend class CleanupReturnInst; // For getting `Val`.
134 friend class SwitchInst; // For getting `Val`.
135 friend class UnaryOperator; // For getting `Val`.
136 friend class BinaryOperator; // For getting `Val`.
137 friend class AtomicRMWInst; // For getting `Val`.
138 friend class AtomicCmpXchgInst; // For getting `Val`.
139 friend class AllocaInst; // For getting `Val`.
140 friend class CastInst; // For getting `Val`.
141 friend class PHINode; // For getting `Val`.
142 friend class UnreachableInst; // For getting `Val`.
143 friend class CatchSwitchAddHandler; // For `Val`.
144 friend class CmpInst; // For getting `Val`.
145 friend class ConstantArray; // For `Val`.
146 friend class ConstantStruct; // For `Val`.
147 friend class ConstantAggregateZero; // For `Val`.
148 friend class ConstantPointerNull; // For `Val`.
149 friend class UndefValue; // For `Val`.
150 friend class PoisonValue; // For `Val`.
151 friend class BlockAddress; // For `Val`.
152 friend class GlobalValue; // For `Val`.
153 friend class DSOLocalEquivalent; // For `Val`.
154 friend class GlobalObject; // For `Val`.
155 friend class GlobalIFunc; // For `Val`.
156 friend class GlobalVariable; // For `Val`.
157 friend class GlobalAlias; // For `Val`.
158 friend class NoCFIValue; // For `Val`.
159 friend class ConstantPtrAuth; // For `Val`.
160 friend class ConstantExpr; // For `Val`.
161 friend class Utils; // For `Val`.
162 friend class Module; // For `Val`.
163 friend class IntrinsicInst; // For `Val`.
164 friend class Operator; // For `Val`.
165 friend class OverflowingBinaryOperator; // For `Val`.
166 friend class FPMathOperator; // For `Val`.
167 // Region needs to manipulate metadata in the underlying LLVM Value, we don't
168 // expose metadata in sandboxir.
169 friend class Region;
170
171 /// All values point to the context.
173 // This is used by eraseFromParent().
174 void clearValue() { Val = nullptr; }
175 template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
176
178 /// Disable copies.
179 Value(const Value &) = delete;
180 Value &operator=(const Value &) = delete;
181
182public:
183 virtual ~Value() = default;
184 ClassID getSubclassID() const { return SubclassID; }
185
188
191 return const_cast<Value *>(this)->use_begin();
192 }
193 use_iterator use_end() { return use_iterator(Use(nullptr, nullptr, Ctx)); }
195 return const_cast<Value *>(this)->use_end();
196 }
197
199 return make_range<use_iterator>(use_begin(), use_end());
200 }
202 return make_range<const_use_iterator>(use_begin(), use_end());
203 }
204
205 /// Helper for mapped_iterator.
206 struct UseToUser {
207 User *operator()(const Use &Use) const { return &*Use.getUser(); }
208 };
209
212
215 return user_iterator(Use(nullptr, nullptr, Ctx), UseToUser());
216 }
218 return const_cast<Value *>(this)->user_begin();
219 }
221 return const_cast<Value *>(this)->user_end();
222 }
223
225 return make_range<user_iterator>(user_begin(), user_end());
226 }
228 return make_range<const_user_iterator>(user_begin(), user_end());
229 }
230 /// \Returns the number of user edges (not necessarily to unique users).
231 /// WARNING: This is a linear-time operation.
232 unsigned getNumUses() const;
233 /// Return true if this value has N uses or more.
234 /// This is logically equivalent to getNumUses() >= N.
235 /// WARNING: This can be expensive, as it is linear to the number of users.
236 bool hasNUsesOrMore(unsigned Num) const {
237 unsigned Cnt = 0;
238 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
239 if (++Cnt >= Num)
240 return true;
241 }
242 return false;
243 }
244 /// Return true if this Value has exactly N uses.
245 bool hasNUses(unsigned Num) const {
246 unsigned Cnt = 0;
247 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
248 if (++Cnt > Num)
249 return false;
250 }
251 return Cnt == Num;
252 }
253
254 Type *getType() const;
255
256 Context &getContext() const { return Ctx; }
257
258 void replaceUsesWithIf(Value *OtherV,
259 llvm::function_ref<bool(const Use &)> ShouldReplace);
261
262 /// \Returns the LLVM IR name of the bottom-most LLVM value.
263 StringRef getName() const { return Val->getName(); }
264
265#ifndef NDEBUG
266 /// Should crash if there is something wrong with the instruction.
267 virtual void verify() const = 0;
268 /// Returns the unique id in the form 'SB<number>.' like 'SB1.'
269 std::string getUid() const;
270 virtual void dumpCommonHeader(raw_ostream &OS) const;
271 void dumpCommonFooter(raw_ostream &OS) const;
272 void dumpCommonPrefix(raw_ostream &OS) const;
273 void dumpCommonSuffix(raw_ostream &OS) const;
276 V.dumpOS(OS);
277 return OS;
278 }
279 virtual void dumpOS(raw_ostream &OS) const = 0;
280 LLVM_DUMP_METHOD void dump() const;
281#endif
282};
283
284} // namespace llvm::sandboxir
285
286#endif // LLVM_SANDBOXIR_VALUE_H
RelocType Type
Definition: COFFYAML.cpp:410
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
Machine Check Debug Module
raw_pwrite_stream & OS
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
In SandboxIR the Module is mainly used to access the list of global objects.
Definition: Module.h:31
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition: Region.h:54
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition: Type.h:43
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:32
class User * getUser() const
Definition: Use.h:54
Iterator for the Use edges of a Value's users.
Definition: Value.h:37
const sandboxir::Use & getUse() const
Definition: Value.h:59
std::input_iterator_tag iterator_category
Definition: Value.h:48
std::ptrdiff_t difference_type
Definition: Value.h:44
bool operator!=(const UserUseIterator &Other) const
Definition: Value.h:56
value_type operator*() const
Definition: Value.h:51
bool operator==(const UserUseIterator &Other) const
Definition: Value.h:53
UserUseIterator & operator++()
Definition: User.cpp:23
A sandboxir::User has operands.
Definition: User.h:58
A SandboxIR Value has users. This is the base class.
Definition: Value.h:63
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition: Value.h:210
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: Value.h:103
void dumpCommonFooter(raw_ostream &OS) const
Definition: Value.cpp:89
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition: Value.cpp:85
const_use_iterator use_end() const
Definition: Value.h:194
ClassID getSubclassID() const
Definition: Value.h:184
void dumpCommonSuffix(raw_ostream &OS) const
Definition: Value.cpp:105
StringRef getName() const
\Returns the LLVM IR name of the bottom-most LLVM value.
Definition: Value.h:263
virtual void verify() const =0
Should crash if there is something wrong with the instruction.
void replaceAllUsesWith(Value *Other)
Definition: Value.cpp:66
unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition: Value.cpp:44
UserUseIterator use_iterator
Definition: Value.h:186
void printAsOperandCommon(raw_ostream &OS) const
Definition: Value.cpp:109
Context & Ctx
All values point to the context.
Definition: Value.h:172
ClassID SubclassID
For isa/dyn_cast.
Definition: Value.h:94
user_iterator user_end()
Definition: Value.h:214
LLVM_DUMP_METHOD void dump() const
Definition: Value.cpp:116
Type * getType() const
Definition: Value.cpp:46
friend class Use
Definition: Value.h:107
friend class LLVMOpUserItToSBTy
Definition: Value.h:175
virtual ~Value()=default
const_use_iterator use_begin() const
Definition: Value.h:190
Context & getContext() const
Definition: Value.h:256
const_user_iterator user_begin() const
Definition: Value.h:217
bool hasNUsesOrMore(unsigned Num) const
Return true if this value has N uses or more.
Definition: Value.h:236
Value & operator=(const Value &)=delete
iterator_range< const_user_iterator > users() const
Definition: Value.h:227
iterator_range< user_iterator > users()
Definition: Value.h:224
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition: Value.cpp:79
void replaceUsesWithIf(Value *OtherV, llvm::function_ref< bool(const Use &)> ShouldReplace)
Definition: Value.cpp:48
bool hasNUses(unsigned Num) const
Return true if this Value has exactly N uses.
Definition: Value.h:245
unsigned UID
A unique ID used for forming the name (used for debugging).
Definition: Value.h:97
virtual void dumpOS(raw_ostream &OS) const =0
use_iterator use_end()
Definition: Value.h:193
void dumpCommonPrefix(raw_ostream &OS) const
Definition: Value.cpp:98
iterator_range< use_iterator > uses()
Definition: Value.h:198
const_user_iterator user_end() const
Definition: Value.h:220
static const char * getSubclassIDStr(ClassID ID)
Definition: Value.h:74
Value(const Value &)=delete
Disable copies.
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Value &V)
Definition: Value.h:275
use_iterator use_begin()
Definition: Value.cpp:23
user_iterator user_begin()
Definition: Value.cpp:33
iterator_range< const_use_iterator > uses() const
Definition: Value.h:201
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Other
Any other memory.
Helper for mapped_iterator.
Definition: Value.h:206
User * operator()(const Use &Use) const
Definition: Value.h:207