LLVM 22.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"
18#include "llvm/ADT/SmallSet.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "si-post-ra-bundler"
24
25namespace {
26
27class SIPostRABundlerLegacy : public MachineFunctionPass {
28public:
29 static char ID;
30
31public:
32 SIPostRABundlerLegacy() : MachineFunctionPass(ID) {
34 }
35
36 bool runOnMachineFunction(MachineFunction &MF) override;
37
38 StringRef getPassName() const override {
39 return "SI post-RA bundler";
40 }
41
42 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.setPreservesAll();
45 }
46};
47
48class SIPostRABundler {
49public:
50 bool run(MachineFunction &MF);
51
52private:
53 const SIRegisterInfo *TRI;
54
56
57 void collectUsedRegUnits(const MachineInstr &MI,
58 BitVector &UsedRegUnits) const;
59
60 bool isBundleCandidate(const MachineInstr &MI) const;
61 bool isDependentLoad(const MachineInstr &MI) const;
62 bool canBundle(const MachineInstr &MI, const MachineInstr &NextMI) const;
63};
64
69
70} // End anonymous namespace.
71
72INITIALIZE_PASS(SIPostRABundlerLegacy, DEBUG_TYPE, "SI post-RA bundler", false,
73 false)
74
75char SIPostRABundlerLegacy::ID = 0;
76
77char &llvm::SIPostRABundlerLegacyID = SIPostRABundlerLegacy::ID;
78
80 return new SIPostRABundlerLegacy();
81}
82
83bool SIPostRABundler::isDependentLoad(const MachineInstr &MI) const {
84 if (!MI.mayLoad())
85 return false;
86
87 for (const MachineOperand &Op : MI.explicit_operands()) {
88 if (!Op.isReg())
89 continue;
90 Register Reg = Op.getReg();
91 for (Register Def : Defs)
92 if (TRI->regsOverlap(Reg, Def))
93 return true;
94 }
95
96 return false;
97}
98
99void SIPostRABundler::collectUsedRegUnits(const MachineInstr &MI,
100 BitVector &UsedRegUnits) const {
101 if (MI.isDebugInstr())
102 return;
103
104 for (const MachineOperand &Op : MI.operands()) {
105 if (!Op.isReg() || !Op.readsReg())
106 continue;
107
108 Register Reg = Op.getReg();
109 assert(!Op.getSubReg() &&
110 "subregister indexes should not be present after RA");
111
112 for (MCRegUnit Unit : TRI->regunits(Reg))
113 UsedRegUnits.set(Unit);
114 }
115}
116
117bool SIPostRABundler::isBundleCandidate(const MachineInstr &MI) const {
118 const uint64_t IMemFlags = MI.getDesc().TSFlags & MemFlags;
119 return IMemFlags != 0 && MI.mayLoadOrStore() && !MI.isBundled();
120}
121
122bool SIPostRABundler::canBundle(const MachineInstr &MI,
123 const MachineInstr &NextMI) const {
124 const uint64_t IMemFlags = MI.getDesc().TSFlags & MemFlags;
125
126 return (IMemFlags != 0 && MI.mayLoadOrStore() && !NextMI.isBundled() &&
127 NextMI.mayLoad() == MI.mayLoad() && NextMI.mayStore() == MI.mayStore() &&
128 ((NextMI.getDesc().TSFlags & MemFlags) == IMemFlags) &&
129 !isDependentLoad(NextMI));
130}
131
132bool SIPostRABundlerLegacy::runOnMachineFunction(MachineFunction &MF) {
133 if (skipFunction(MF.getFunction()))
134 return false;
135 return SIPostRABundler().run(MF);
136}
137
140 SIPostRABundler().run(MF);
141 return PreservedAnalyses::all();
142}
143
144bool SIPostRABundler::run(MachineFunction &MF) {
145
146 TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
147 BitVector BundleUsedRegUnits(TRI->getNumRegUnits());
148 BitVector KillUsedRegUnits(TRI->getNumRegUnits());
149
150 bool Changed = false;
151 for (MachineBasicBlock &MBB : MF) {
152 bool HasIGLPInstrs = llvm::any_of(MBB.instrs(), [](MachineInstr &MI) {
153 unsigned Opc = MI.getOpcode();
154 return Opc == AMDGPU::SCHED_GROUP_BARRIER || Opc == AMDGPU::IGLP_OPT;
155 });
156
157 // Don't cluster with IGLP instructions.
158 if (HasIGLPInstrs)
159 continue;
160
164
165 for (auto I = B; I != E; I = Next) {
166 Next = std::next(I);
167 if (!isBundleCandidate(*I))
168 continue;
169
170 assert(Defs.empty());
171
172 if (I->getNumExplicitDefs() != 0)
173 Defs.insert(I->defs().begin()->getReg());
174
177 unsigned ClauseLength = 1;
178 for (I = Next; I != E; I = Next) {
179 Next = std::next(I);
180
181 assert(BundleEnd != I);
182 if (canBundle(*BundleEnd, *I)) {
183 BundleEnd = I;
184 if (I->getNumExplicitDefs() != 0)
185 Defs.insert(I->defs().begin()->getReg());
186 ++ClauseLength;
187 } else if (!I->isMetaInstruction() ||
188 I->getOpcode() == AMDGPU::SCHED_BARRIER) {
189 // SCHED_BARRIER is not bundled to be honored by scheduler later.
190 // Allow other meta instructions in between bundle candidates, but do
191 // not start or end a bundle on one.
192 //
193 // TODO: It may be better to move meta instructions like dbg_value
194 // after the bundle. We're relying on the memory legalizer to unbundle
195 // these.
196 break;
197 }
198 }
199
200 Next = std::next(BundleEnd);
201 if (ClauseLength > 1) {
202 Changed = true;
203
204 // Before register allocation, kills are inserted after potential soft
205 // clauses to hint register allocation. Look for kills that look like
206 // this, and erase them.
207 if (Next != E && Next->isKill()) {
208
209 // TODO: Should maybe back-propagate kill flags to the bundle.
210 for (const MachineInstr &BundleMI : make_range(BundleStart, Next))
211 collectUsedRegUnits(BundleMI, BundleUsedRegUnits);
212
213 BundleUsedRegUnits.flip();
214
215 while (Next != E && Next->isKill()) {
216 MachineInstr &Kill = *Next;
217 collectUsedRegUnits(Kill, KillUsedRegUnits);
218
219 KillUsedRegUnits &= BundleUsedRegUnits;
220
221 // Erase the kill if it's a subset of the used registers.
222 //
223 // TODO: Should we just remove all kills? Is there any real reason to
224 // keep them after RA?
225 if (KillUsedRegUnits.none()) {
226 ++Next;
227 Kill.eraseFromParent();
228 } else
229 break;
230
231 KillUsedRegUnits.reset();
232 }
233
234 BundleUsedRegUnits.reset();
235 }
236
237 finalizeBundle(MBB, BundleStart, Next);
238 }
239
240 Defs.clear();
241 }
242 }
243
244 return Changed;
245}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
#define DEBUG_TYPE
This file defines the SmallSet class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
BitVector & set()
Definition: BitVector.h:351
This class represents an Operation in the Expression.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
instr_iterator instr_begin()
Instructions::iterator instr_iterator
instr_iterator instr_end()
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
Definition: MachineInstr.h:72
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:584
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.
Definition: MachineInstr.h:484
MachineOperand class - Representation of each machine instruction operand.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
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:19
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ Kill
The last use of a register.
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
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:1751
FunctionPass * createSIPostRABundlerPass()
char & SIPostRABundlerLegacyID
void initializeSIPostRABundlerLegacyPass(PassRegistry &)