LLVM 20.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
11
12namespace llvm::sandboxir {
13
14InstructionCost ScoreBoard::getCost(Instruction *I) const {
15 auto *LLVMI = cast<llvm::Instruction>(I->Val);
16 SmallVector<const llvm::Value *> Operands(LLVMI->operands());
17 return TTI.getInstructionCost(LLVMI, Operands, CostKind);
18}
19
21 auto Cost = getCost(I);
22 if (Rgn.contains(I))
23 // If `I` is one the newly added ones, then we should adjust `AfterCost`
24 AfterCost -= Cost;
25 else
26 // If `I` is one of the original instructions (outside the region) then it
27 // is part of the original code, so adjust `BeforeCost`.
28 BeforeCost += Cost;
29}
30
31#ifndef NDEBUG
32void ScoreBoard::dump() const { dump(dbgs()); }
33#endif
34
36 : Ctx(Ctx), Scoreboard(*this, TTI) {
37 LLVMContext &LLVMCtx = Ctx.LLVMCtx;
38 auto *RegionStrMD = MDString::get(LLVMCtx, RegionStr);
39 RegionMDN = MDNode::getDistinct(LLVMCtx, {RegionStrMD});
40
41 CreateInstCB = Ctx.registerCreateInstrCallback(
42 [this](Instruction *NewInst) { add(NewInst); });
43 EraseInstCB = Ctx.registerEraseInstrCallback(
44 [this](Instruction *ErasedInst) { remove(ErasedInst); });
45}
46
48 Ctx.unregisterCreateInstrCallback(CreateInstCB);
49 Ctx.unregisterEraseInstrCallback(EraseInstCB);
50}
51
53 Insts.insert(I);
54 // TODO: Consider tagging instructions lazily.
55 cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, RegionMDN);
56 // Keep track of the instruction cost.
57 Scoreboard.add(I);
58}
59
61 // Keep track of the instruction cost. This need to be done *before* we remove
62 // `I` from the region.
63 Scoreboard.remove(I);
64
65 Insts.remove(I);
66 cast<llvm::Instruction>(I->Val)->setMetadata(MDKind, nullptr);
67}
68
69#ifndef NDEBUG
70bool Region::operator==(const Region &Other) const {
71 if (Insts.size() != Other.Insts.size())
72 return false;
73 if (!std::is_permutation(Insts.begin(), Insts.end(), Other.Insts.begin()))
74 return false;
75 return true;
76}
77
79 for (auto *I : Insts)
80 OS << *I << "\n";
81}
82
83void Region::dump() const {
84 dump(dbgs());
85 dbgs() << "\n";
86}
87#endif // NDEBUG
88
93 auto &Ctx = F.getContext();
94 for (BasicBlock &BB : F) {
95 for (Instruction &Inst : BB) {
96 if (auto *MDN = cast<llvm::Instruction>(Inst.Val)->getMetadata(MDKind)) {
97 Region *R = nullptr;
98 auto It = MDNToRegion.find(MDN);
99 if (It == MDNToRegion.end()) {
100 Regions.push_back(std::make_unique<Region>(Ctx, TTI));
101 R = Regions.back().get();
102 MDNToRegion[MDN] = R;
103 } else {
104 R = It->second;
105 }
106 R->add(&Inst);
107 }
108 }
109 }
110 return Regions;
111}
112
113} // namespace llvm::sandboxir
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
raw_pwrite_stream & OS
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
iterator end()
Definition: DenseMap.h:84
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1557
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:606
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
InstructionCost getInstructionCost(const User *U, ArrayRef< const Value * > Operands, TargetCostKind CostKind) const
Estimate the cost of a given IR user when lowered.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Contains a list of sandboxir::Instruction's.
Definition: BasicBlock.h:67
CallbackID registerCreateInstrCallback(CreateInstrCallback CB)
Register a callback that gets called right after a SandboxIR instruction is created.
Definition: Context.cpp:703
void unregisterCreateInstrCallback(CallbackID ID)
Definition: Context.cpp:710
void unregisterEraseInstrCallback(CallbackID ID)
Definition: Context.cpp:696
LLVMContext & LLVMCtx
Definition: Context.h:65
CallbackID registerEraseInstrCallback(EraseInstrCallback CB)
Register a callback that gets called when a SandboxIR instruction is about to be removed from its par...
Definition: Context.cpp:689
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
Definition: Instruction.h:42
The main job of the Region is to point to new instructions generated by vectorization passes.
Definition: Region.h:91
void dump() const
Definition: Region.cpp:83
bool contains(Instruction *I) const
Returns true if I is in the Region.
Definition: Region.h:124
void add(Instruction *I)
Adds I to the set.
Definition: Region.cpp:52
bool operator==(const Region &Other) const
This is an expensive check, meant for testing.
Definition: Region.cpp:70
void remove(Instruction *I)
Removes I from the set.
Definition: Region.cpp:60
Region(Context &Ctx, TargetTransformInfo &TTI)
Definition: Region.cpp:35
static SmallVector< std::unique_ptr< Region > > createRegionsFromMD(Function &F, TargetTransformInfo &TTI)
Definition: Region.cpp:90
void remove(Instruction *I)
Mark I as a deleted instruction from the region.
Definition: Region.cpp:20
LLVM_DUMP_METHOD void dump() const
Definition: Region.cpp:32
void add(Instruction *I)
Mark I as a newly added instruction to the region.
Definition: Region.h:41
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.
InstructionCost Cost