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 friend class ScoreBoard; // Needs access to `Val` for the instruction cost.
171
172 /// All values point to the context.
174 // This is used by eraseFromParent().
175 void clearValue() { Val = nullptr; }
176 template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
177
179 /// Disable copies.
180 Value(const Value &) = delete;
181 Value &operator=(const Value &) = delete;
182
183public:
184 virtual ~Value() = default;
185 ClassID getSubclassID() const { return SubclassID; }
186
189
192 return const_cast<Value *>(this)->use_begin();
193 }
194 use_iterator use_end() { return use_iterator(Use(nullptr, nullptr, Ctx)); }
196 return const_cast<Value *>(this)->use_end();
197 }
198
200 return make_range<use_iterator>(use_begin(), use_end());
201 }
203 return make_range<const_use_iterator>(use_begin(), use_end());
204 }
205
206 /// Helper for mapped_iterator.
207 struct UseToUser {
208 User *operator()(const Use &Use) const { return &*Use.getUser(); }
209 };
210
213
216 return user_iterator(Use(nullptr, nullptr, Ctx), UseToUser());
217 }
219 return const_cast<Value *>(this)->user_begin();
220 }
222 return const_cast<Value *>(this)->user_end();
223 }
224
226 return make_range<user_iterator>(user_begin(), user_end());
227 }
229 return make_range<const_user_iterator>(user_begin(), user_end());
230 }
231 /// \Returns the number of user edges (not necessarily to unique users).
232 /// WARNING: This is a linear-time operation.
233 unsigned getNumUses() const;
234 /// Return true if this value has N uses or more.
235 /// This is logically equivalent to getNumUses() >= N.
236 /// WARNING: This can be expensive, as it is linear to the number of users.
237 bool hasNUsesOrMore(unsigned Num) const {
238 unsigned Cnt = 0;
239 for (auto It = use_begin(), ItE = use_end(); It != ItE; ++It) {
240 if (++Cnt >= Num)
241 return true;
242 }
243 return false;
244 }
245 /// Return true if this Value has exactly N uses.
246 bool hasNUses(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 false;
251 }
252 return Cnt == Num;
253 }
254
255 Type *getType() const;
256
257 Context &getContext() const { return Ctx; }
258
259 void replaceUsesWithIf(Value *OtherV,
260 llvm::function_ref<bool(const Use &)> ShouldReplace);
262
263 /// \Returns the LLVM IR name of the bottom-most LLVM value.
264 StringRef getName() const { return Val->getName(); }
265
266#ifndef NDEBUG
267 /// Should crash if there is something wrong with the instruction.
268 virtual void verify() const = 0;
269 /// Returns the unique id in the form 'SB<number>.' like 'SB1.'
270 std::string getUid() const;
271 virtual void dumpCommonHeader(raw_ostream &OS) const;
272 void dumpCommonFooter(raw_ostream &OS) const;
273 void dumpCommonPrefix(raw_ostream &OS) const;
274 void dumpCommonSuffix(raw_ostream &OS) const;
277 V.dumpOS(OS);
278 return OS;
279 }
280 virtual void dumpOS(raw_ostream &OS) const = 0;
281 LLVM_DUMP_METHOD void dump() const;
282#endif
283};
284
285} // namespace llvm::sandboxir
286
287#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:91
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:211
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:195
ClassID getSubclassID() const
Definition: Value.h:185
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:264
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:187
void printAsOperandCommon(raw_ostream &OS) const
Definition: Value.cpp:109
Context & Ctx
All values point to the context.
Definition: Value.h:173
ClassID SubclassID
For isa/dyn_cast.
Definition: Value.h:94
user_iterator user_end()
Definition: Value.h:215
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:176
virtual ~Value()=default
const_use_iterator use_begin() const
Definition: Value.h:191
Context & getContext() const
Definition: Value.h:257
const_user_iterator user_begin() const
Definition: Value.h:218
bool hasNUsesOrMore(unsigned Num) const
Return true if this value has N uses or more.
Definition: Value.h:237
Value & operator=(const Value &)=delete
iterator_range< const_user_iterator > users() const
Definition: Value.h:228
iterator_range< user_iterator > users()
Definition: Value.h:225
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:246
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:194
void dumpCommonPrefix(raw_ostream &OS) const
Definition: Value.cpp:98
iterator_range< use_iterator > uses()
Definition: Value.h:199
const_user_iterator user_end() const
Definition: Value.h:221
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:276
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:202
#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:207
User * operator()(const Use &Use) const
Definition: Value.h:208