LLVM 22.0.0git
User.cpp
Go to the documentation of this file.
1//===- User.cpp - The User class of Sandbox IR ----------------------------===//
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
11
12namespace llvm::sandboxir {
13
14Use OperandUseIterator::operator*() const { return Use; }
15
16OperandUseIterator &OperandUseIterator::operator++() {
17 assert(Use.LLVMUse != nullptr && "Already at end!");
18 User *User = Use.getUser();
19 Use = User->getOperandUseInternal(Use.getOperandNo() + 1, /*Verify=*/false);
20 return *this;
22
23UserUseIterator &UserUseIterator::operator++() {
24 // Get the corresponding llvm::Use, get the next in the list, and update the
25 // sandboxir::Use.
26 llvm::Use *&LLVMUse = Use.LLVMUse;
27 assert(LLVMUse != nullptr && "Already at end!");
28 LLVMUse = LLVMUse->getNext();
29 if (LLVMUse == nullptr) {
30 Use.Usr = nullptr;
31 return *this;
32 }
33 auto *Ctx = Use.Ctx;
34 auto *LLVMUser = LLVMUse->getUser();
35 Use.Usr = cast_or_null<sandboxir::User>(Ctx->getValue(LLVMUser));
36 return *this;
37}
38
39OperandUseIterator OperandUseIterator::operator+(unsigned Num) const {
41 Use.getOperandNo() + Num, /*Verify=*/true);
42 return OperandUseIterator(U);
43}
44
45OperandUseIterator OperandUseIterator::operator-(unsigned Num) const {
46 assert(Use.getOperandNo() >= Num && "Out of bounds!");
48 Use.getOperandNo() - Num, /*Verify=*/true);
49 return OperandUseIterator(U);
51
52int OperandUseIterator::operator-(const OperandUseIterator &Other) const {
53 int ThisOpNo = Use.getOperandNo();
54 int OtherOpNo = Other.Use.getOperandNo();
55 return ThisOpNo - OtherOpNo;
56}
57
59 assert((!Verify || OpIdx < getNumOperands()) && "Out of bounds!");
60 assert(isa<llvm::User>(Val) && "Non-users have no operands!");
61 llvm::Use *LLVMUse;
62 if (OpIdx != getNumOperands())
63 LLVMUse = &cast<llvm::User>(Val)->getOperandUse(OpIdx);
64 else
65 LLVMUse = cast<llvm::User>(Val)->op_end();
66 return Use(LLVMUse, const_cast<User *>(this), Ctx);
68
69#ifndef NDEBUG
71 assert(Ctx.getValue(Use.getUser()) == this &&
72 "Use not found in this SBUser's operands!");
73}
74#endif
75
76bool User::classof(const Value *From) {
77 switch (From->getSubclassID()) {
78#define DEF_VALUE(ID, CLASS)
79#define DEF_USER(ID, CLASS) \
80 case ClassID::ID: \
81 return true;
82#define DEF_INSTR(ID, OPC, CLASS) \
83 case ClassID::ID: \
84 return true;
85#include "llvm/SandboxIR/Values.def"
86 default:
87 return false;
88 }
89}
90
91void User::setOperand(unsigned OperandIdx, Value *Operand) {
92 assert(isa<llvm::User>(Val) && "No operands!");
93 const auto &U = getOperandUse(OperandIdx);
94 Ctx.getTracker().emplaceIfTracking<UseSet>(U);
95 Ctx.runSetUseCallbacks(U, Operand);
96 // We are delegating to llvm::User::setOperand().
97 cast<llvm::User>(Val)->setOperand(OperandIdx, Operand->Val);
98}
101 auto &Tracker = Ctx.getTracker();
102 for (auto OpIdx : seq<unsigned>(0, getNumOperands())) {
103 auto Use = getOperandUse(OpIdx);
104 if (Use.get() == FromV) {
105 Ctx.runSetUseCallbacks(Use, ToV);
106 if (Tracker.isTracking())
107 Tracker.emplaceIfTracking<UseSet>(Use);
108 }
109 }
110 // We are delegating RUOW to LLVM IR's RUOW.
111 return cast<llvm::User>(Val)->replaceUsesOfWith(FromV->Val, ToV->Val);
112}
113
114#ifndef NDEBUG
117 // TODO: This is incomplete
118}
119#endif // NDEBUG
120
121} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineInstr unsigned OpIdx
ppc ctr loops PowerPC CTR Loops Verify
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Value * get() const
Definition Use.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI OperandUseIterator operator-(unsigned Num) const
Definition User.cpp:45
LLVM_ABI value_type operator*() const
Definition User.cpp:14
LLVM_ABI OperandUseIterator & operator++()
Definition User.cpp:16
LLVM_ABI OperandUseIterator operator+(unsigned Num) const
Definition User.cpp:39
The tracker collects all the change objects and implements the main API for saving / reverting / acce...
Definition Tracker.h:442
Tracks the change of the source Value of a sandboxir::Use.
Definition Tracker.h:140
Represents a Def-use/Use-def edge in SandboxIR.
Definition Use.h:33
class User * getUser() const
Definition Use.h:55
LLVM_ABI UserUseIterator & operator++()
Definition User.cpp:23
void verifyUserOfLLVMUse(const llvm::Use &Use) const
Definition User.cpp:70
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition User.cpp:91
bool replaceUsesOfWith(Value *FromV, Value *ToV)
Replaces any operands that match FromV with ToV.
Definition User.cpp:100
Use getOperandUseDefault(unsigned OpIdx, bool Verify) const
\Returns the Use edge that corresponds to OpIdx.
Definition User.cpp:58
static bool classof(const Value *From)
For isa/dyn_cast.
Definition User.cpp:76
virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const =0
\Returns the Use for the OpIdx'th operand.
void dumpCommonHeader(raw_ostream &OS) const final
Definition User.cpp:115
virtual unsigned getNumOperands() const
Definition User.h:129
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition User.h:126
User(ClassID ID, llvm::Value *V, Context &Ctx)
Definition User.h:61
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition Value.h:106
virtual void dumpCommonHeader(raw_ostream &OS) const
Definition Value.cpp:87
ClassID getSubclassID() const
Definition Value.h:191
Context & Ctx
All values point to the context.
Definition Value.h:179
LLVM_ABI Value(ClassID SubclassID, llvm::Value *Val, Context &Ctx)
Definition Value.cpp:16
friend class Use
Definition Value.h:110
auto cast_or_null(const Y &Val)
Definition Casting.h:720
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
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305