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
72 return new SIPostRABundlerLegacy();
73}
74
75bool SIPostRABundler::isDependentLoad(const MachineInstr &MI) const {
76 if (!MI.mayLoad())
77 return false;
78
79 for (const MachineOperand &Op : MI.explicit_operands()) {
80 if (!Op.isReg())
81 continue;
82 Register Reg = Op.getReg();
83 for (Register Def : Defs)
84 if (TRI->regsOverlap(Reg, Def))
85 return true;
86 }
87
88 return false;
89}
90
91void SIPostRABundler::collectUsedRegUnits(const MachineInstr &MI,
92 BitVector &UsedRegUnits) const {
93 if (MI.isDebugInstr())
94 return;
95
96 for (const MachineOperand &Op : MI.operands()) {
97 if (!Op.isReg() || !Op.readsReg())
98 continue;
99
100 Register Reg = Op.getReg();
101 assert(!Op.getSubReg() &&
102 "subregister indexes should not be present after RA");
103
104 for (MCRegUnit Unit : TRI->regunits(Reg))
105 UsedRegUnits.set(static_cast<unsigned>(Unit));
106 }
107}
108
115
126
127bool SIPostRABundler::isBundleCandidate(const MachineInstr &MI) const {
128 return isMemoryInst(MI) && MI.mayLoadOrStore() && !MI.isBundled();
129}
130
131bool SIPostRABundler::canBundle(const MachineInstr &MI,
132 const MachineInstr &NextMI) const {
133 return isMemoryInst(MI) && MI.mayLoadOrStore() && !NextMI.isBundled() &&
134 NextMI.mayLoad() == MI.mayLoad() &&
135 NextMI.mayStore() == MI.mayStore() && hasSameMemFormat(MI, NextMI) &&
136 !isDependentLoad(NextMI);
137}
138
139bool SIPostRABundlerLegacy::runOnMachineFunction(MachineFunction &MF) {
140 if (skipFunction(MF.getFunction()))
141 return false;
142 return SIPostRABundler().run(MF);
143}
144
150
151bool SIPostRABundler::run(MachineFunction &MF) {
152
153 TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
154 BitVector BundleUsedRegUnits(TRI->getNumRegUnits());
155 BitVector KillUsedRegUnits(TRI->getNumRegUnits());
156
157 bool Changed = false;
158 for (MachineBasicBlock &MBB : MF) {
159 bool HasIGLPInstrs = llvm::any_of(MBB.instrs(), [](MachineInstr &MI) {
160 unsigned Opc = MI.getOpcode();
161 return Opc == AMDGPU::SCHED_GROUP_BARRIER || Opc == AMDGPU::IGLP_OPT;
162 });
163
164 // Don't cluster with IGLP instructions.
165 if (HasIGLPInstrs)
166 continue;
167
171
172 for (auto I = B; I != E; I = Next) {
173 Next = std::next(I);
174 if (!isBundleCandidate(*I))
175 continue;
176
177 assert(Defs.empty());
178
179 if (I->getNumExplicitDefs() != 0)
180 Defs.insert(I->defs().begin()->getReg());
181
184 unsigned ClauseLength = 1;
185 for (I = Next; I != E; I = Next) {
186 Next = std::next(I);
187
188 assert(BundleEnd != I);
189 if (canBundle(*BundleEnd, *I)) {
190 BundleEnd = I;
191 if (I->getNumExplicitDefs() != 0)
192 Defs.insert(I->defs().begin()->getReg());
193 ++ClauseLength;
194 } else if (!I->isMetaInstruction() ||
195 I->getOpcode() == AMDGPU::SCHED_BARRIER) {
196 // SCHED_BARRIER is not bundled to be honored by scheduler later.
197 // Allow other meta instructions in between bundle candidates, but do
198 // not start or end a bundle on one.
199 //
200 // TODO: It may be better to move meta instructions like dbg_value
201 // after the bundle. We're relying on the memory legalizer to unbundle
202 // these.
203 break;
204 }
205 }
206
207 Next = std::next(BundleEnd);
208 if (ClauseLength > 1) {
209 Changed = true;
210
211 // Before register allocation, kills are inserted after potential soft
212 // clauses to hint register allocation. Look for kills that look like
213 // this, and erase them.
214 if (Next != E && Next->isKill()) {
215
216 // TODO: Should maybe back-propagate kill flags to the bundle.
217 for (const MachineInstr &BundleMI : make_range(BundleStart, Next))
218 collectUsedRegUnits(BundleMI, BundleUsedRegUnits);
219
220 BundleUsedRegUnits.flip();
221
222 while (Next != E && Next->isKill()) {
223 MachineInstr &Kill = *Next;
224 collectUsedRegUnits(Kill, KillUsedRegUnits);
225
226 KillUsedRegUnits &= BundleUsedRegUnits;
227
228 // Erase the kill if it's a subset of the used registers.
229 //
230 // TODO: Should we just remove all kills? Is there any real reason to
231 // keep them after RA?
232 if (KillUsedRegUnits.none()) {
233 ++Next;
234 Kill.eraseFromParent();
235 } else
236 break;
237
238 KillUsedRegUnits.reset();
239 }
240
241 BundleUsedRegUnits.reset();
242 }
243
244 finalizeBundle(MBB, BundleStart, Next);
245 }
246
247 Defs.clear();
248 }
249 }
250
251 return Changed;
252}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
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
Wrapper class representing virtual and physical registers.
Definition Register.h:20
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:286
constexpr bool isMTBUF(const T &...O)
Definition SIDefines.h:264
constexpr bool isVIMAGE(const T &...O)
Definition SIDefines.h:277
constexpr bool isSMRD(const T &...O)
Definition SIDefines.h:271
constexpr bool isMIMG(const T &...O)
Definition SIDefines.h:274
constexpr bool isMUBUF(const T &...O)
Definition SIDefines.h:261
constexpr bool isVSAMPLE(const T &...O)
Definition SIDefines.h:280
constexpr bool isDS(const T &...O)
Definition SIDefines.h:289
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:1746
FunctionPass * createSIPostRABundlerPass()
char & SIPostRABundlerLegacyID
DWARFExpression::Operation Op
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147