LLVM 23.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/Metadata.h"
13#include "llvm/IR/Value.h"
14#include "llvm/SandboxIR/Use.h"
16
17namespace llvm::sandboxir {
18
19// Forward declare all classes to avoid some MSVC build errors.
20#define DEF_INSTR(ID, OPC, CLASS) class CLASS;
21#define DEF_CONST(ID, CLASS) class CLASS;
22#define DEF_USER(ID, CLASS) class CLASS;
23#define DEF_DISABLE_AUTO_UNDEF // ValuesDefFilesList.def includes multiple .def
24#include "llvm/SandboxIR/ValuesDefFilesList.def"
25#undef DEF_INSTR
26#undef DEF_CONST
27#undef DEF_USER
28class Context;
29class FuncletPadInst;
30class Type;
31class GlobalValue;
32class GlobalObject;
33class Module;
34class UnaryInstruction;
35class CmpInst;
36class IntrinsicInst;
37class Operator;
38class OverflowingBinaryOperator;
39class FPMathOperator;
40class Region;
41class UncondBrInst;
42class CondBrInst;
43
44/// Iterator for the `Use` edges of a Value's users.
45/// \Returns a `Use` when dereferenced.
46class UserUseIterator {
48 /// Don't let the user create a non-empty UserUseIterator.
49 UserUseIterator(const class Use &Use) : Use(Use) {}
50 friend class Value; // For constructor
51
52public:
53 using difference_type = std::ptrdiff_t;
57 using iterator_category = std::input_iterator_tag;
58
59 UserUseIterator() = default;
60 value_type operator*() const { return Use; }
62 bool operator==(const UserUseIterator &Other) const {
63 return Use == Other.Use;
64 }
65 bool operator!=(const UserUseIterator &Other) const {
66 return !(*this == Other);
67 }
68 const sandboxir::Use &getUse() const { return Use; }
69};
70
71/// A SandboxIR Value has users. This is the base class.
72class Value {
73public:
74 enum class ClassID : unsigned {
75#define DEF_VALUE(ID, CLASS) ID,
76#define DEF_USER(ID, CLASS) ID,
77#define DEF_CONST(ID, CLASS) ID,
78#define DEF_INSTR(ID, OPC, CLASS) ID,
79#define DEF_DISABLE_AUTO_UNDEF // ValuesDefFilesList.def includes multiple .def
80#include "llvm/SandboxIR/ValuesDefFilesList.def"
81#undef DEF_VALUE
82#undef DEF_USER
83#undef DEF_CONST
84#undef DEF_INSTR
85 };
86
87protected:
88 static const char *getSubclassIDStr(ClassID ID) {
89 switch (ID) {
90#define DEF_VALUE(ID, CLASS) \
91 case ClassID::ID: \
92 return #ID;
93#define DEF_USER(ID, CLASS) \
94 case ClassID::ID: \
95 return #ID;
96#define DEF_CONST(ID, CLASS) \
97 case ClassID::ID: \
98 return #ID;
99#define DEF_INSTR(ID, OPC, CLASS) \
100 case ClassID::ID: \
101 return #ID;
102#define DEF_DISABLE_AUTO_UNDEF // ValuesDefFilesList.def includes multiple .def
103#include "llvm/SandboxIR/ValuesDefFilesList.def"
104#undef DEF_VALUE
105#undef DEF_USER
106#undef DEF_CONST
107#undef DEF_INSTR
108 }
109 llvm_unreachable("Unimplemented ID");
110 }
111
112 /// For isa/dyn_cast.
114#ifndef NDEBUG
115 /// A unique ID used for forming the name (used for debugging).
116 unsigned UID;
117#endif
118 /// The LLVM Value that corresponds to this SandboxIR Value.
119 /// NOTE: Some sandboxir Instructions, like Packs, may include more than one
120 /// value and in these cases `Val` points to the last instruction in program
121 /// order.
122 llvm::Value *Val = nullptr;
123
124 friend class Context; // For getting `Val`.
125 friend class User; // For getting `Val`.
126 friend class Use; // For getting `Val`.
127 friend class VAArgInst; // For getting `Val`.
128 friend class FreezeInst; // For getting `Val`.
129 friend class FenceInst; // For getting `Val`.
130 friend class SelectInst; // For getting `Val`.
131 friend class ExtractElementInst; // For getting `Val`.
132 friend class InsertElementInst; // For getting `Val`.
133 friend class ShuffleVectorInst; // For getting `Val`.
134 friend class ExtractValueInst; // For getting `Val`.
135 friend class InsertValueInst; // For getting `Val`.
136 friend class UncondBrInst; // For getting `Val`.
137 friend class CondBrInst; // For getting `Val`.
138 friend class LoadInst; // For getting `Val`.
139 friend class StoreInst; // For getting `Val`.
140 friend class ReturnInst; // For getting `Val`.
141 friend class CallBase; // For getting `Val`.
142 friend class CallInst; // For getting `Val`.
143 friend class InvokeInst; // For getting `Val`.
144 friend class CallBrInst; // For getting `Val`.
145 friend class LandingPadInst; // For getting `Val`.
146 friend class FuncletPadInst; // For getting `Val`.
147 friend class CatchPadInst; // For getting `Val`.
148 friend class CleanupPadInst; // For getting `Val`.
149 friend class CatchReturnInst; // For getting `Val`.
150 friend class GetElementPtrInst; // For getting `Val`.
151 friend class ResumeInst; // For getting `Val`.
152 friend class CatchSwitchInst; // For getting `Val`.
153 friend class CleanupReturnInst; // For getting `Val`.
154 friend class SwitchInst; // For getting `Val`.
155 friend class UnaryOperator; // For getting `Val`.
156 friend class BinaryOperator; // For getting `Val`.
157 friend class AtomicRMWInst; // For getting `Val`.
158 friend class AtomicCmpXchgInst; // For getting `Val`.
159 friend class AllocaInst; // For getting `Val`.
160 friend class CastInst; // For getting `Val`.
161 friend class PHINode; // For getting `Val`.
162 friend class UnreachableInst; // For getting `Val`.
163 friend class CatchSwitchAddHandler; // For `Val`.
164 friend class CmpInst; // For getting `Val`.
165 friend class ConstantArray; // For `Val`.
166 friend class ConstantStruct; // For `Val`.
167 friend class ConstantVector; // For `Val`.
168 friend class ConstantAggregateZero; // For `Val`.
169 friend class ConstantPointerNull; // For `Val`.
170 friend class UndefValue; // For `Val`.
171 friend class PoisonValue; // For `Val`.
172 friend class BlockAddress; // For `Val`.
173 friend class GlobalValue; // For `Val`.
174 friend class DSOLocalEquivalent; // For `Val`.
175 friend class GlobalObject; // For `Val`.
176 friend class GlobalIFunc; // For `Val`.
177 friend class GlobalVariable; // For `Val`.
178 friend class GlobalAlias; // For `Val`.
179 friend class NoCFIValue; // For `Val`.
180 friend class ConstantPtrAuth; // For `Val`.
181 friend class ConstantExpr; // For `Val`.
182 friend class Utils; // For `Val`.
183 friend class Module; // For `Val`.
184 friend class IntrinsicInst; // For `Val`.
185 friend class Operator; // For `Val`.
186 friend class OverflowingBinaryOperator; // For `Val`.
187 friend class FPMathOperator; // For `Val`.
188 // Region needs to manipulate metadata in the underlying LLVM Value, we don't
189 // expose metadata in sandboxir.
190 friend class Region;
191 friend class ScoreBoard; // Needs access to `Val` for the instruction cost.
192 friend class ConstantDataArray; // For `Val`
193 friend class ConstantDataVector; // For `Val`
194
195#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS;
196#define DEF_DISABLE_AUTO_UNDEF // ValuesDefFilesList.def includes multiple .def
197#include "llvm/SandboxIR/ValuesDefFilesList.def"
198#undef DEF_INSTR
199
200 /// All values point to the context.
202 // This is used by eraseFromParent().
203 void clearValue() { Val = nullptr; }
204 template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
205
207 /// Disable copies.
208 Value(const Value &) = delete;
209 Value &operator=(const Value &) = delete;
210
211public:
212 virtual ~Value() = default;
213 ClassID getSubclassID() const { return SubclassID; }
214
217
220 return const_cast<Value *>(this)->use_begin();
221 }
222 use_iterator use_end() { return use_iterator(Use(nullptr, nullptr, Ctx)); }
224 return const_cast<Value *>(this)->use_end();
225 }
226
233
234 /// Helper for mapped_iterator.
235 struct UseToUser {
236 User *operator()(const Use &Use) const { return &*Use.getUser(); }
237 };
238
241
244 return user_iterator(Use(nullptr, nullptr, Ctx), UseToUser());
245 }
247 return const_cast<Value *>(this)->user_begin();
248 }
250 return const_cast<Value *>(this)->user_end();
251 }
252
259 /// \Returns the number of user edges (not necessarily to unique users).
260 /// WARNING: This is a linear-time operation.
261 LLVM_ABI unsigned getNumUses() const;
262 /// Return true if this value has N uses or more.
263 /// This is logically equivalent to getNumUses() >= N.
264 /// WARNING: This can be expensive, as it is linear to the number of users.
265 bool hasNUsesOrMore(unsigned Num) const {
266 unsigned Cnt = 0;
267 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
268 if (++Cnt >= Num)
269 return true;
270 }
271 return false;
272 }
273 /// Return true if this Value has exactly N uses.
274 bool hasNUses(unsigned Num) const {
275 unsigned Cnt = 0;
276 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
277 if (++Cnt > Num)
278 return false;
279 }
280 return Cnt == Num;
281 }
282
283 LLVM_ABI Type *getType() const;
284
285 Context &getContext() const { return Ctx; }
286
287 LLVM_ABI void
288 replaceUsesWithIf(Value *OtherV,
289 llvm::function_ref<bool(const Use &)> ShouldReplace);
291
292 /// \Returns the LLVM IR name of the bottom-most LLVM value.
293 StringRef getName() const { return Val->getName(); }
294
295#ifndef NDEBUG
296 /// Should crash if there is something wrong with the instruction.
297 virtual void verify() const = 0;
298 /// Returns the unique id in the form 'SB<number>.' like 'SB1.'
299 std::string getUid() const;
300 virtual void dumpCommonHeader(raw_ostream &OS) const;
301 void dumpCommonFooter(raw_ostream &OS) const;
302 void dumpCommonPrefix(raw_ostream &OS) const;
303 void dumpCommonSuffix(raw_ostream &OS) const;
304 void printAsOperandCommon(raw_ostream &OS) const;
306 V.dumpOS(OS);
307 return OS;
308 }
309 virtual void dumpOS(raw_ostream &OS) const = 0;
310 LLVM_DUMP_METHOD void dump() const;
311#endif
312};
313
314class OpaqueValue : public Value {
315protected:
318 friend class Context; // For constructor.
319
320public:
321 static bool classof(const Value *From) {
322 return From->getSubclassID() == ClassID::OpaqueValue;
323 }
324#ifndef NDEBUG
325 void verify() const override {
327 "Expected Metadata or InlineAssembly!");
328 }
329 void dumpOS(raw_ostream &OS) const override {
332 }
333#endif // NDEBUG
334};
335
336} // namespace llvm::sandboxir
337
338#endif // LLVM_SANDBOXIR_VALUE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
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:53
static bool classof(const Value *From)
Definition Value.h:321
void dumpOS(raw_ostream &OS) const override
Definition Value.h:329
void verify() const override
Should crash if there is something wrong with the instruction.
Definition Value.h:325
OpaqueValue(llvm::Value *V, Context &Ctx)
Definition Value.h:316
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition Type.h:48
Represents a Def-use/Use-def edge in SandboxIR.
Definition Use.h:33
Iterator for the Use edges of a Value's users.
Definition Value.h:46
const sandboxir::Use & getUse() const
Definition Value.h:68
std::input_iterator_tag iterator_category
Definition Value.h:57
std::ptrdiff_t difference_type
Definition Value.h:53
bool operator!=(const UserUseIterator &Other) const
Definition Value.h:65
value_type operator*() const
Definition Value.h:60
bool operator==(const UserUseIterator &Other) const
Definition Value.h:62
LLVM_ABI UserUseIterator & operator++()
Definition User.cpp:23
sandboxir::Use value_type
Definition Value.h:54
A SandboxIR Value has users. This is the base class.
Definition Value.h:72
mapped_iterator< sandboxir::UserUseIterator, UseToUser > user_iterator
Definition Value.h:239
friend class InsertElementInst
Definition Value.h:132
friend class CatchReturnInst
Definition Value.h:149
friend class UnreachableInst
Definition Value.h:162
friend class UnaryOperator
Definition Value.h:155
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition Value.h:122
friend class SelectInst
Definition Value.h:130
void dumpCommonFooter(raw_ostream &OS) const
Definition Value.cpp:91
friend class CastInst
Definition Value.h:160
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition Value.cpp:87
friend class ConstantStruct
Definition Value.h:166
friend class FenceInst
Definition Value.h:129
friend class OverflowingBinaryOperator
Definition Value.h:186
const_use_iterator use_end() const
Definition Value.h:223
friend class ConstantVector
Definition Value.h:167
friend class FPMathOperator
Definition Value.h:187
friend class Module
Definition Value.h:183
friend class IntrinsicInst
Definition Value.h:184
ClassID getSubclassID() const
Definition Value.h:213
friend class ConstantDataVector
Definition Value.h:193
void dumpCommonSuffix(raw_ostream &OS) const
Definition Value.cpp:107
StringRef getName() const
\Returns the LLVM IR name of the bottom-most LLVM value.
Definition Value.h:293
virtual void verify() const =0
Should crash if there is something wrong with the instruction.
friend class Operator
Definition Value.h:185
friend class FreezeInst
Definition Value.h:128
LLVM_ABI void replaceAllUsesWith(Value *Other)
Definition Value.cpp:67
friend class ExtractElementInst
Definition Value.h:131
friend class User
Definition Value.h:125
LLVM_ABI unsigned getNumUses() const
\Returns the number of user edges (not necessarily to unique users).
Definition Value.cpp:44
UserUseIterator use_iterator
Definition Value.h:215
void printAsOperandCommon(raw_ostream &OS) const
Definition Value.cpp:111
friend class StoreInst
Definition Value.h:139
Context & Ctx
All values point to the context.
Definition Value.h:201
friend class CmpInst
Definition Value.h:164
friend class ShuffleVectorInst
Definition Value.h:133
friend class ExtractValueInst
Definition Value.h:134
friend class ReturnInst
Definition Value.h:140
friend class SwitchInst
Definition Value.h:154
ClassID SubclassID
For isa/dyn_cast.
Definition Value.h:113
user_iterator user_end()
Definition Value.h:243
friend class ConstantPointerNull
Definition Value.h:169
LLVM_DUMP_METHOD void dump() const
Definition Value.cpp:118
friend class ConstantPtrAuth
Definition Value.h:180
friend class VAArgInst
Definition Value.h:127
LLVM_ABI Type * getType() const
Definition Value.cpp:46
friend class InsertValueInst
Definition Value.h:135
friend class ScoreBoard
Definition Value.h:191
friend class InvokeInst
Definition Value.h:143
LLVM_ABI Value(ClassID SubclassID, llvm::Value *Val, Context &Ctx)
Definition Value.cpp:16
friend class GlobalAlias
Definition Value.h:178
friend class CatchSwitchInst
Definition Value.h:152
friend class BinaryOperator
Definition Value.h:156
user_iterator const_user_iterator
Definition Value.h:240
friend class Use
Definition Value.h:126
friend class LLVMOpUserItToSBTy
Definition Value.h:204
virtual ~Value()=default
const_use_iterator use_begin() const
Definition Value.h:219
friend class ConstantExpr
Definition Value.h:181
Context & getContext() const
Definition Value.h:285
const_user_iterator user_begin() const
Definition Value.h:246
friend class CatchPadInst
Definition Value.h:147
friend class ResumeInst
Definition Value.h:151
bool hasNUsesOrMore(unsigned Num) const
Return true if this value has N uses or more.
Definition Value.h:265
friend class PoisonValue
Definition Value.h:171
UserUseIterator const_use_iterator
Definition Value.h:216
friend class LoadInst
Definition Value.h:138
friend class AtomicRMWInst
Definition Value.h:157
friend class UncondBrInst
Definition Value.h:136
Value & operator=(const Value &)=delete
friend class GlobalValue
Definition Value.h:173
iterator_range< const_user_iterator > users() const
Definition Value.h:256
iterator_range< user_iterator > users()
Definition Value.h:253
friend class GlobalVariable
Definition Value.h:177
std::string getUid() const
Returns the unique id in the form 'SB<number>.' like 'SB1.'.
Definition Value.cpp:81
LLVM_ABI void replaceUsesWithIf(Value *OtherV, llvm::function_ref< bool(const Use &)> ShouldReplace)
Definition Value.cpp:48
friend class Region
Definition Value.h:190
friend class BlockAddress
Definition Value.h:172
friend class ConstantArray
Definition Value.h:165
friend class CallBase
Definition Value.h:141
friend class Utils
Definition Value.h:182
friend class GlobalIFunc
Definition Value.h:176
bool hasNUses(unsigned Num) const
Return true if this Value has exactly N uses.
Definition Value.h:274
unsigned UID
A unique ID used for forming the name (used for debugging).
Definition Value.h:116
friend class GlobalObject
Definition Value.h:175
friend class Context
Definition Value.h:124
friend class GetElementPtrInst
Definition Value.h:150
virtual void dumpOS(raw_ostream &OS) const =0
friend class PHINode
Definition Value.h:161
use_iterator use_end()
Definition Value.h:222
friend class ConstantDataArray
Definition Value.h:192
friend class CleanupPadInst
Definition Value.h:148
void dumpCommonPrefix(raw_ostream &OS) const
Definition Value.cpp:100
iterator_range< use_iterator > uses()
Definition Value.h:227
const_user_iterator user_end() const
Definition Value.h:249
friend class FuncletPadInst
Definition Value.h:146
friend class CondBrInst
Definition Value.h:137
friend class AtomicCmpXchgInst
Definition Value.h:158
friend class NoCFIValue
Definition Value.h:179
friend class CatchSwitchAddHandler
Definition Value.h:163
friend class LandingPadInst
Definition Value.h:145
friend class CallBrInst
Definition Value.h:144
static const char * getSubclassIDStr(ClassID ID)
Definition Value.h:88
Value(const Value &)=delete
Disable copies.
friend class CleanupReturnInst
Definition Value.h:153
friend class AllocaInst
Definition Value.h:159
friend class ConstantAggregateZero
Definition Value.h:168
friend class CallInst
Definition Value.h:142
friend raw_ostream & operator<<(raw_ostream &OS, const sandboxir::Value &V)
Definition Value.h:305
LLVM_ABI use_iterator use_begin()
Definition Value.cpp:23
LLVM_ABI user_iterator user_begin()
Definition Value.cpp:33
friend class UndefValue
Definition Value.h:170
friend class DSOLocalEquivalent
Definition Value.h:174
iterator_range< const_use_iterator > uses() const
Definition Value.h:230
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Other
Any other memory.
Definition ModRef.h:68
Helper for mapped_iterator.
Definition Value.h:235
User * operator()(const Use &Use) const
Definition Value.h:236