LLVM 23.0.0git
Region.cpp
Go to the documentation of this file.
1//===- Region.cpp ---------------------------------------------------------===//
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
10
11namespace llvm::sandboxir {
12
14 LLVMContext &LLVMCtx = Ctx.LLVMCtx;
15 auto *RegionStrMD = MDString::get(LLVMCtx, RegionStr);
16 RegionMDN = MDNode::getDistinct(LLVMCtx, {RegionStrMD});
17
18 CreateInstCB = Ctx.registerCreateInstrCallback(
19 [this](Instruction *NewInst) { addRaw(NewInst); });
20 EraseInstCB = Ctx.registerEraseInstrCallback([this](Instruction *ErasedInst) {
21 remove(ErasedInst);
22 removeFromAux(ErasedInst);
23 });
24}
25
27 Ctx.unregisterCreateInstrCallback(CreateInstCB);
28 Ctx.unregisterEraseInstrCallback(EraseInstCB);
29}
30
33 auto &LLVMCtx = Ctx.LLVMCtx;
34 for (auto [Idx, I] : enumerate(Aux)) {
35 llvm::ConstantInt *IdxC =
36 llvm::ConstantInt::get(llvm::Type::getInt32Ty(LLVMCtx), Idx, false);
37 assert(cast<llvm::Instruction>(I->Val)->getMetadata(AuxMDKind) == nullptr &&
38 "Instruction already in Aux!");
39 cast<llvm::Instruction>(I->Val)->setMetadata(
41 // Aux instrs should always be in a region.
42 addRaw(I);
43 }
44}
45
46void Region::setAux(unsigned Idx, Instruction *I) {
47 assert((Idx >= Aux.size() || Aux[Idx] == nullptr) &&
48 "There is already an Instruction at Idx in Aux!");
49 unsigned ExpectedSz = Idx + 1;
50 if (Aux.size() < ExpectedSz) {
51 auto SzBefore = Aux.size();
52 Aux.resize(ExpectedSz);
53 // Initialize the gap with nullptr.
54 for (unsigned Idx = SzBefore; Idx + 1 < ExpectedSz; ++Idx)
55 Aux[Idx] = nullptr;
56 }
57 Aux[Idx] = I;
58 // Aux instrs should always be in a region.
59 addRaw(I);
60}
61
63 auto *LLVMI = cast<llvm::Instruction>(I->Val);
64 LLVMI->setMetadata(AuxMDKind, nullptr);
65}
66
68 auto It = find(Aux, I);
69 if (It == Aux.end())
70 return;
72 Aux.erase(It);
73}
74
76 for (unsigned Idx : seq<unsigned>(0, Aux.size()))
77 dropAuxMetadata(Aux[Idx]);
78 Aux.clear();
79}
80
82 Insts.remove(I);
83 cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, nullptr);
84}
85
86#ifndef NDEBUG
87bool Region::operator==(const Region &Other) const {
88 if (Insts.size() != Other.Insts.size())
89 return false;
90 if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))
91 return false;
92 return true;
93}
94
95void Region::dump(raw_ostream &OS) const {
96 for (auto *I : Insts)
97 OS << *I << "\n";
98 if (!Aux.empty()) {
99 OS << "\nAux:\n";
100 for (auto *I : Aux) {
101 if (I == nullptr)
102 OS << "NULL\n";
103 else
104 OS << *I << "\n";
105 }
106 }
107}
108
109void Region::dump() const {
110 dump(dbgs());
111 dbgs() << "\n";
112}
113#endif // NDEBUG
114
117 F, [&F]() { return std::make_unique<Region>(F.getContext()); });
118}
119
120} // namespace llvm::sandboxir
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1580
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:614
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition Instruction.h:43
static constexpr const char * RegionStr
Definition Region.h:77
void addRaw(Instruction *I)
Adds I to the set.
Definition Region.h:97
void removeFromAux(Instruction *I)
Remove instruction I from Aux and drop metadata.
Definition Region.cpp:67
static constexpr const char * MDKind
Definition Region.h:76
RegionClassID ID
Definition Region.h:82
Context::CallbackID EraseInstCB
ID (for later deregistration) of the "erase instruction" callback.
Definition Region.h:87
LLVM_ABI_FOR_TEST bool operator==(const Region &Other) const
This is an expensive check, meant for testing.
Definition Region.cpp:87
virtual LLVM_ABI void remove(Instruction *I)
Removes I from the set.
Definition Region.cpp:81
static SmallVector< std::unique_ptr< RegionT > > createRegionsFromMD(Function &F, RegionFactoryT Factory)
Definition Region.h:122
Context::CallbackID CreateInstCB
ID (for later deregistration) of the "create instruction" callback.
Definition Region.h:85
void dropAuxMetadata(Instruction *I)
Helper for dropping Aux metadata for I.
Definition Region.cpp:62
SetVector< Instruction * > Insts
All the instructions in the Region.
Definition Region.h:70
static constexpr const char * AuxMDKind
Definition Region.h:78
friend class Context
Definition Region.h:107
LLVM_ABI Region(Context &Ctx, RegionClassID ID)
Definition Region.cpp:13
LLVM_ABI void clearAux()
Clears all auxiliary data.
Definition Region.cpp:75
virtual LLVM_ABI ~Region()
Definition Region.cpp:26
void setAux(unsigned Idx, Instruction *I)
Set I as the Idx'th element in the auxiliary vector.
Definition Region.cpp:46
MDNode * RegionMDN
MDNode that we'll use to mark instructions as being part of the region.
Definition Region.h:75
SmallVector< Instruction * > Aux
An auxiliary sequence of Instruction-Index pairs.
Definition Region.h:72
RegionClassID
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition Region.h:61
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1764
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2553
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ 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:559
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305