LLVM 20.0.0git
User.h
Go to the documentation of this file.
1//===- User.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_USER_H
10#define LLVM_SANDBOXIR_USER_H
11
12#include "llvm/IR/User.h"
13#include "llvm/IR/Value.h"
14#include "llvm/SandboxIR/Use.h"
16
17namespace llvm::sandboxir {
18
19class Context;
20
21/// Iterator for the `Use` edges of a User's operands.
22/// \Returns the operand `Use` when dereferenced.
25 /// Don't let the user create a non-empty OperandUseIterator.
26 OperandUseIterator(const class Use &Use) : Use(Use) {}
27 friend class User; // For constructor
28#define DEF_INSTR(ID, OPC, CLASS) friend class CLASS; // For constructor
29#include "llvm/SandboxIR/Values.def"
30
31public:
32 using difference_type = std::ptrdiff_t;
36 using iterator_category = std::input_iterator_tag;
37
38 OperandUseIterator() = default;
39 value_type operator*() const;
42 auto Copy = *this;
43 this->operator++();
44 return Copy;
45 }
46 bool operator==(const OperandUseIterator &Other) const {
47 return Use == Other.Use;
48 }
49 bool operator!=(const OperandUseIterator &Other) const {
50 return !(*this == Other);
51 }
52 OperandUseIterator operator+(unsigned Num) const;
53 OperandUseIterator operator-(unsigned Num) const;
54 int operator-(const OperandUseIterator &Other) const;
55};
56
57/// A sandboxir::User has operands.
58class User : public Value {
59protected:
61
62 /// \Returns the Use edge that corresponds to \p OpIdx.
63 /// Note: This is the default implementation that works for instructions that
64 /// match the underlying LLVM instruction. All others should use a different
65 /// implementation.
66 Use getOperandUseDefault(unsigned OpIdx, bool Verify) const;
67 /// \Returns the Use for the \p OpIdx'th operand. This is virtual to allow
68 /// instructions to deviate from the LLVM IR operands, which is a requirement
69 /// for sandboxir Instructions that consist of more than one LLVM Instruction.
70 virtual Use getOperandUseInternal(unsigned OpIdx, bool Verify) const = 0;
71 friend class OperandUseIterator; // for getOperandUseInternal()
72
73 /// The default implementation works only for single-LLVMIR-instruction
74 /// Users and only if they match exactly the LLVM instruction.
75 unsigned getUseOperandNoDefault(const Use &Use) const {
76 return Use.LLVMUse->getOperandNo();
77 }
78 /// \Returns the operand index of \p Use.
79 virtual unsigned getUseOperandNo(const Use &Use) const = 0;
80 friend unsigned Use::getOperandNo() const; // For getUseOperandNo()
81
82 void swapOperandsInternal(unsigned OpIdxA, unsigned OpIdxB) {
83 assert(OpIdxA < getNumOperands() && "OpIdxA out of bounds!");
84 assert(OpIdxB < getNumOperands() && "OpIdxB out of bounds!");
85 auto UseA = getOperandUse(OpIdxA);
86 auto UseB = getOperandUse(OpIdxB);
87 UseA.swap(UseB);
88 }
89
90#ifndef NDEBUG
91 void verifyUserOfLLVMUse(const llvm::Use &Use) const;
92#endif // NDEBUG
93
94public:
95 /// For isa/dyn_cast.
96 static bool classof(const Value *From);
101
103 assert(isa<llvm::User>(Val) && "Expect User value!");
104 return op_iterator(getOperandUseInternal(0, /*Verify=*/false));
105 }
106 virtual op_iterator op_end() {
107 assert(isa<llvm::User>(Val) && "Expect User value!");
108 return op_iterator(
109 getOperandUseInternal(getNumOperands(), /*Verify=*/false));
110 }
111 virtual const_op_iterator op_begin() const {
112 return const_cast<User *>(this)->op_begin();
113 }
114 virtual const_op_iterator op_end() const {
115 return const_cast<User *>(this)->op_end();
116 }
117
118 op_range operands() { return make_range<op_iterator>(op_begin(), op_end()); }
120 return make_range<const_op_iterator>(op_begin(), op_end());
121 }
122 Value *getOperand(unsigned OpIdx) const { return getOperandUse(OpIdx).get(); }
123 /// \Returns the operand edge for \p OpIdx. NOTE: This should also work for
124 /// OpIdx == getNumOperands(), which is used for op_end().
125 Use getOperandUse(unsigned OpIdx) const {
126 return getOperandUseInternal(OpIdx, /*Verify=*/true);
127 }
128 virtual unsigned getNumOperands() const {
129 return isa<llvm::User>(Val) ? cast<llvm::User>(Val)->getNumOperands() : 0;
130 }
131
132 virtual void setOperand(unsigned OperandIdx, Value *Operand);
133 /// Replaces any operands that match \p FromV with \p ToV. Returns whether any
134 /// operands were replaced.
135 bool replaceUsesOfWith(Value *FromV, Value *ToV);
136
137#ifndef NDEBUG
138 void verify() const override {
139 assert(isa<llvm::User>(Val) && "Expected User!");
140 }
141 void dumpCommonHeader(raw_ostream &OS) const final;
142 void dumpOS(raw_ostream &OS) const override {
143 // TODO: Remove this tmp implementation once we get the Instruction classes.
144 }
145#endif
146};
147
148} // namespace llvm::sandboxir
149
150#endif // LLVM_SANDBOXIR_USER_H
BlockVerifier::State From
ppc ctr loops PowerPC CTR Loops Verify
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
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
Iterator for the Use edges of a User's operands.
Definition: User.h:23
std::ptrdiff_t difference_type
Definition: User.h:32
std::input_iterator_tag iterator_category
Definition: User.h:36
OperandUseIterator operator++(int)
Definition: User.h:41
bool operator!=(const OperandUseIterator &Other) const
Definition: User.h:49
bool operator==(const OperandUseIterator &Other) const
Definition: User.h:46
OperandUseIterator operator-(unsigned Num) const
Definition: User.cpp:45
value_type operator*() const
Definition: User.cpp:14
OperandUseIterator & operator++()
Definition: User.cpp:16
OperandUseIterator operator+(unsigned Num) const
Definition: User.cpp:39
Represents a Def-use/Use-def edge in SandboxIR.
Definition: Use.h:32
unsigned getOperandNo() const
Definition: Use.cpp:22
Value * get() const
Definition: Use.cpp:15
A sandboxir::User has operands.
Definition: User.h:58
virtual op_iterator op_begin()
Definition: User.h:102
void dumpOS(raw_ostream &OS) const override
Definition: User.h:142
friend class OperandUseIterator
Definition: User.h:71
Value * getOperand(unsigned OpIdx) const
Definition: User.h:122
virtual unsigned getUseOperandNo(const Use &Use) const =0
\Returns the operand index of Use.
void verifyUserOfLLVMUse(const llvm::Use &Use) const
Definition: User.cpp:70
unsigned getUseOperandNoDefault(const Use &Use) const
The default implementation works only for single-LLVMIR-instruction Users and only if they match exac...
Definition: User.h:75
virtual const_op_iterator op_end() const
Definition: User.h:114
virtual void setOperand(unsigned OperandIdx, Value *Operand)
Definition: User.cpp:91
op_range operands()
Definition: User.h:118
void verify() const override
Should crash if there is something wrong with the instruction.
Definition: User.h:138
bool replaceUsesOfWith(Value *FromV, Value *ToV)
Replaces any operands that match FromV with ToV.
Definition: User.cpp:98
Use getOperandUseDefault(unsigned OpIdx, bool Verify) const
\Returns the Use edge that corresponds to OpIdx.
Definition: User.cpp:58
OperandUseIterator op_iterator
Definition: User.h:97
const_op_range operands() const
Definition: User.h:119
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:112
void swapOperandsInternal(unsigned OpIdxA, unsigned OpIdxB)
Definition: User.h:82
virtual unsigned getNumOperands() const
Definition: User.h:128
virtual const_op_iterator op_begin() const
Definition: User.h:111
Use getOperandUse(unsigned OpIdx) const
\Returns the operand edge for OpIdx.
Definition: User.h:125
virtual op_iterator op_end()
Definition: User.h:106
User(ClassID ID, llvm::Value *V, Context &Ctx)
Definition: User.h:60
A SandboxIR Value has users. This is the base class.
Definition: Value.h:63
llvm::Value * Val
The LLVM Value that corresponds to this SandboxIR Value.
Definition: Value.h:103
Context & Ctx
All values point to the context.
Definition: Value.h:172
@ Other
Any other memory.