LLVM 24.0.0git
SIPostRABundler.cpp
Go to the documentation of this file.
1//===-- SIPostRABundler.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//
9/// \file
10/// This pass creates bundles of memory instructions to protect adjacent loads
11/// and stores from being rescheduled apart from each other post-RA.
12///
13//===----------------------------------------------------------------------===//
14
15#include "SIPostRABundler.h"
16#include "AMDGPU.h"
17#include "GCNSubtarget.h"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "si-post-ra-bundler"
23
24namespace {
25
26class SIPostRABundlerLegacy : public MachineFunctionPass {
27public:
28 static char ID;
29
30public:
31 SIPostRABundlerLegacy() : MachineFunctionPass(ID) {}
32
33 bool runOnMachineFunction(MachineFunction &MF) override;
34
35 StringRef getPassName() const override {
36 return "SI post-RA bundler";
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesAll();
42 }
43};
44
45class SIPostRABundler {
46public:
47 bool run(MachineFunction &MF);
48
49private:
50 const SIRegisterInfo *TRI;
51
53
54 void collectUsedRegUnits(const MachineInstr &MI,
55 BitVector &UsedRegUnits) const;
56
57 bool isBundleCandidate(const MachineInstr &MI) const;
58 bool isDependentLoad(const MachineInstr &MI) const;
59 bool canBundle(const MachineInstr &MI, const MachineInstr &NextMI) const;
60};
61
62} // End anonymous namespace.
63
64INITIALIZE_PASS(SIPostRABundlerLegacy, DEBUG_TYPE, "SI post-RA bundler", false,
65 false)
66
67char SIPostRABundlerLegacy::ID = 0;
68
69char &llvm::SIPostRABundlerLegacyID = SIPostRABundlerLegacy::ID;
70
71bool SIPostRABundler::isDependentLoad(const MachineInstr &MI) const {
72 if (!MI.mayLoad())
73 return false;
74
75 for (const MachineOperand &Op : MI.explicit_operands()) {
76 if (!Op.isReg())
77 continue;
78 Register Reg = Op.getReg();
79 for (Register Def : Defs)
80 if (TRI->regsOverlap(Reg, Def))
81 return true;
82 }
83
84 return false;
85}
86
87void SIPostRABundler::collectUsedRegUnits(const MachineInstr &MI,
88 BitVector &UsedRegUnits) const {
89 if (MI.isDebugInstr())
90 return;
91
92 for (const MachineOperand &Op : MI.operands()) {
93 if (!Op.isReg() || !Op.readsReg())
94 continue;
95
96 Register Reg = Op.getReg();
97 assert(!Op.getSubReg() &&
98 "subregister indexes should not be present after RA");
99
100 for (MCRegUnit Unit : TRI->regunits(Reg))
101 UsedRegUnits.set(static_cast<unsigned>(Unit));
102 }
103}
104
111
122
123bool SIPostRABundler::isBundleCandidate(const MachineInstr &MI) const {
124 return isMemoryInst(MI) && MI.mayLoadOrStore() && !MI.isBundled();
125}
126
127bool SIPostRABundler::canBundle(const MachineInstr &MI,
128 const MachineInstr &NextMI) const {
129 return isMemoryInst(MI) && MI.mayLoadOrStore() && !NextMI.isBundled() &&
130 NextMI.mayLoad() == MI.mayLoad() &&
131 NextMI.mayStore() == MI.mayStore() && hasSameMemFormat(MI, NextMI) &&
132 !isDependentLoad(NextMI);
133}
134
135bool SIPostRABundlerLegacy::runOnMachineFunction(MachineFunction &MF) {
136 if (skipFunction(MF.getFunction()))
137 return false;
138 return SIPostRABundler().run(MF);
139}
140
146
147bool SIPostRABundler::run(MachineFunction &MF) {
148
149 TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
150 BitVector BundleUsedRegUnits(TRI->getNumRegUnits());
151 BitVector KillUsedRegUnits(TRI->getNumRegUnits());
152
153 bool Changed = false;
154 for (MachineBasicBlock &MBB : MF) {
155 bool HasIGLPInstrs = llvm::any_of(MBB.instrs(), [](MachineInstr &MI) {
156 unsigned Opc = MI.getOpcode();
157 return Opc == AMDGPU::SCHED_GROUP_BARRIER || Opc == AMDGPU::IGLP_OPT;
158 });
159
160 // Don't cluster with IGLP instructions.
161 if (HasIGLPInstrs)
162 continue;
163
167
168 for (auto I = B; I != E; I = Next) {
169 Next = std::next(I);
170 if (!isBundleCandidate(*I))
171 continue;
172
173 assert(Defs.empty());
174
175 if (I->getNumExplicitDefs() != 0)
176 Defs.insert(I->defs().begin()->getReg());
177
180 unsigned ClauseLength = 1;
181 for (I = Next; I != E; I = Next) {
182 Next = std::next(I);
183
184 assert(BundleEnd != I);
185 if (canBundle(*BundleEnd, *I)) {
186 BundleEnd = I;
187 if (I->getNumExplicitDefs() != 0)
188 Defs.insert(I->defs().begin()->getReg());
189 ++ClauseLength;
190 } else if (!I->isMetaInstruction() ||
191 I->getOpcode() == AMDGPU::SCHED_BARRIER) {
192 // SCHED_BARRIER is not bundled to be honored by scheduler later.
193 // Allow other meta instructions in between bundle candidates, but do
194 // not start or end a bundle on one.
195 //
196 // TODO: It may be better to move meta instructions like dbg_value
197 // after the bundle. We're relying on the memory legalizer to unbundle
198 // these.
199 break;
200 }
201 }
202
203 Next = std::next(BundleEnd);
204 if (ClauseLength > 1) {
205 Changed = true;
206
207 // Before register allocation, kills are inserted after potential soft
208 // clauses to hint register allocation. Look for kills that look like
209 // this, and erase them.
210 if (Next != E && Next->isKill()) {
211
212 // TODO: Should maybe back-propagate kill flags to the bundle.
213 for (const MachineInstr &BundleMI : make_range(BundleStart, Next))
214 collectUsedRegUnits(BundleMI, BundleUsedRegUnits);
215
216 BundleUsedRegUnits.flip();
217
218 while (Next != E && Next->isKill()) {
219 MachineInstr &Kill = *Next;
220 collectUsedRegUnits(Kill, KillUsedRegUnits);
221
222 KillUsedRegUnits &= BundleUsedRegUnits;
223
224 // Erase the kill if it's a subset of the used registers.
225 //
226 // TODO: Should we just remove all kills? Is there any real reason to
227 // keep them after RA?
228 if (KillUsedRegUnits.none()) {
229 ++Next;
230 Kill.eraseFromParent();
231 } else
232 break;
233
234 KillUsedRegUnits.reset();
235 }
236
237 BundleUsedRegUnits.reset();
238 }
239
240 finalizeBundle(MBB, BundleStart, Next);
241 }
242
243 Defs.clear();
244 }
245 }
246
247 return Changed;
248}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool hasSameMemFormat(const MachineInstr &A, const MachineInstr &B)
static bool isMemoryInst(const MachineInstr &MI)
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
Instructions::iterator instr_iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
bool isBundled() const
Return true if this instruction part of a bundle.
MachineOperand class - Representation of each machine instruction operand.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
bool empty() const
Definition SmallSet.h:169
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Changed
constexpr bool isFLAT(const T &...O)
Definition SIDefines.h:283
constexpr bool isMTBUF(const T &...O)
Definition SIDefines.h:261
constexpr bool isVIMAGE(const T &...O)
Definition SIDefines.h:274
constexpr bool isSMRD(const T &...O)
Definition SIDefines.h:268
constexpr bool isMIMG(const T &...O)
Definition SIDefines.h:271
constexpr bool isMUBUF(const T &...O)
Definition SIDefines.h:258
constexpr bool isVSAMPLE(const T &...O)
Definition SIDefines.h:277
constexpr bool isDS(const T &...O)
Definition SIDefines.h:286
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
@ Kill
The last use of a register.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
char & SIPostRABundlerLegacyID
DWARFExpression::Operation Op
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147