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