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