1 //===-- SILoadStoreOptimizer.cpp ------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass tries to fuse DS instructions with close by immediate offsets.
11 // This will fuse operations such as
12 // ds_read_b32 v0, v2 offset:16
13 // ds_read_b32 v1, v2 offset:32
15 // ds_read2_b32 v[0:1], v2, offset0:4 offset1:8
18 // Future improvements:
20 // - This currently relies on the scheduler to place loads and stores next to
21 // each other, and then only merges adjacent pairs of instructions. It would
22 // be good to be more flexible with interleaved instructions, and possibly run
23 // before scheduling. It currently missing stores of constants because loading
24 // the constant into the data register is placed between the stores, although
25 // this is arguably a scheduling problem.
27 // - Live interval recomputing seems inefficient. This currently only matches
28 // one pair, and recomputes live intervals and moves on to the next pair. It
29 // would be better to compute a list of all merges that need to occur
31 // - With a list of instructions to process, we can also merge more. If a
32 // cluster of loads have offsets that are too large to fit in the 8-bit
33 // offsets, but are close enough to fit in the 8 bits, we can add to the base
34 // pointer and use the new reduced offsets.
36 //===----------------------------------------------------------------------===//
39 #include "SIInstrInfo.h"
40 #include "SIRegisterInfo.h"
41 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
42 #include "llvm/CodeGen/LiveVariables.h"
43 #include "llvm/CodeGen/MachineFunction.h"
44 #include "llvm/CodeGen/MachineFunctionPass.h"
45 #include "llvm/CodeGen/MachineInstrBuilder.h"
46 #include "llvm/CodeGen/MachineRegisterInfo.h"
47 #include "llvm/Support/Debug.h"
48 #include "llvm/Target/TargetMachine.h"
52 #define DEBUG_TYPE "si-load-store-opt"
56 class SILoadStoreOptimizer : public MachineFunctionPass {
58 const TargetMachine *TM;
59 const SIInstrInfo *TII;
60 const SIRegisterInfo *TRI;
61 MachineRegisterInfo *MRI;
65 static bool offsetsCanBeCombined(unsigned Offset0,
69 MachineBasicBlock::iterator findMatchingDSInst(MachineBasicBlock::iterator I,
72 void updateRegDefsUses(unsigned SrcReg,
76 MachineBasicBlock::iterator mergeRead2Pair(
77 MachineBasicBlock::iterator I,
78 MachineBasicBlock::iterator Paired,
81 MachineBasicBlock::iterator mergeWrite2Pair(
82 MachineBasicBlock::iterator I,
83 MachineBasicBlock::iterator Paired,
89 SILoadStoreOptimizer() :
90 MachineFunctionPass(ID),
99 SILoadStoreOptimizer(const TargetMachine &TM_) :
100 MachineFunctionPass(ID),
102 TII(static_cast<const SIInstrInfo*>(TM->getSubtargetImpl()->getInstrInfo())) {
103 initializeSILoadStoreOptimizerPass(*PassRegistry::getPassRegistry());
106 bool optimizeBlock(MachineBasicBlock &MBB);
108 bool runOnMachineFunction(MachineFunction &MF) override;
110 const char *getPassName() const override {
111 return "SI Load / Store Optimizer";
114 void getAnalysisUsage(AnalysisUsage &AU) const override {
115 AU.setPreservesCFG();
116 AU.addPreserved<SlotIndexes>();
117 AU.addPreserved<LiveIntervals>();
118 AU.addPreserved<LiveVariables>();
119 AU.addRequired<LiveIntervals>();
121 MachineFunctionPass::getAnalysisUsage(AU);
125 } // End anonymous namespace.
127 INITIALIZE_PASS_BEGIN(SILoadStoreOptimizer, DEBUG_TYPE,
128 "SI Load / Store Optimizer", false, false)
129 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
130 INITIALIZE_PASS_DEPENDENCY(LiveVariables)
131 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
132 INITIALIZE_PASS_END(SILoadStoreOptimizer, DEBUG_TYPE,
133 "SI Load / Store Optimizer", false, false)
135 char SILoadStoreOptimizer::ID = 0;
137 char &llvm::SILoadStoreOptimizerID = SILoadStoreOptimizer::ID;
139 FunctionPass *llvm::createSILoadStoreOptimizerPass(TargetMachine &TM) {
140 return new SILoadStoreOptimizer(TM);
143 bool SILoadStoreOptimizer::offsetsCanBeCombined(unsigned Offset0,
146 // XXX - Would the same offset be OK? Is there any reason this would happen or
148 if (Offset0 == Offset1)
151 // This won't be valid if the offset isn't aligned.
152 if ((Offset0 % Size != 0) || (Offset1 % Size != 0))
155 unsigned EltOffset0 = Offset0 / Size;
156 unsigned EltOffset1 = Offset1 / Size;
158 // Check if the new offsets fit in the reduced 8-bit range.
159 if (isUInt<8>(EltOffset0) && isUInt<8>(EltOffset1))
162 // If the offset in elements doesn't fit in 8-bits, we might be able to use
163 // the stride 64 versions.
164 if ((EltOffset0 % 64 != 0) || (EltOffset1 % 64) != 0)
167 return isUInt<8>(EltOffset0 / 64) && isUInt<8>(EltOffset1 / 64);
170 MachineBasicBlock::iterator
171 SILoadStoreOptimizer::findMatchingDSInst(MachineBasicBlock::iterator I,
173 MachineBasicBlock::iterator E = I->getParent()->end();
174 MachineBasicBlock::iterator MBBI = I;
177 if (MBBI->getOpcode() != I->getOpcode())
180 // Don't merge volatiles.
181 if (MBBI->hasOrderedMemoryRef())
184 int AddrIdx = AMDGPU::getNamedOperandIdx(I->getOpcode(), AMDGPU::OpName::addr);
185 const MachineOperand &AddrReg0 = I->getOperand(AddrIdx);
186 const MachineOperand &AddrReg1 = MBBI->getOperand(AddrIdx);
188 // Check same base pointer. Be careful of subregisters, which can occur with
189 // vectors of pointers.
190 if (AddrReg0.getReg() == AddrReg1.getReg() &&
191 AddrReg0.getSubReg() == AddrReg1.getSubReg()) {
192 int OffsetIdx = AMDGPU::getNamedOperandIdx(I->getOpcode(),
193 AMDGPU::OpName::offset);
194 unsigned Offset0 = I->getOperand(OffsetIdx).getImm() & 0xffff;
195 unsigned Offset1 = MBBI->getOperand(OffsetIdx).getImm() & 0xffff;
197 // Check both offsets fit in the reduced range.
198 if (offsetsCanBeCombined(Offset0, Offset1, EltSize))
205 void SILoadStoreOptimizer::updateRegDefsUses(unsigned SrcReg,
208 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(SrcReg),
209 E = MRI->reg_end(); I != E; ) {
210 MachineOperand &O = *I;
212 O.substVirtReg(DstReg, SubIdx, *TRI);
216 MachineBasicBlock::iterator SILoadStoreOptimizer::mergeRead2Pair(
217 MachineBasicBlock::iterator I,
218 MachineBasicBlock::iterator Paired,
220 MachineBasicBlock *MBB = I->getParent();
222 // Be careful, since the addresses could be subregisters themselves in weird
223 // cases, like vectors of pointers.
224 const MachineOperand *AddrReg = TII->getNamedOperand(*I, AMDGPU::OpName::addr);
226 unsigned DestReg0 = TII->getNamedOperand(*I, AMDGPU::OpName::vdst)->getReg();
228 = TII->getNamedOperand(*Paired, AMDGPU::OpName::vdst)->getReg();
231 = TII->getNamedOperand(*I, AMDGPU::OpName::offset)->getImm() & 0xffff;
233 = TII->getNamedOperand(*Paired, AMDGPU::OpName::offset)->getImm() & 0xffff;
235 unsigned NewOffset0 = Offset0 / EltSize;
236 unsigned NewOffset1 = Offset1 / EltSize;
237 unsigned Opc = (EltSize == 4) ? AMDGPU::DS_READ2_B32 : AMDGPU::DS_READ2_B64;
239 // Prefer the st64 form if we can use it, even if we can fit the offset in the
240 // non st64 version. I'm not sure if there's any real reason to do this.
241 bool UseST64 = (NewOffset0 % 64 == 0) && (NewOffset1 % 64 == 0);
245 Opc = (EltSize == 4) ? AMDGPU::DS_READ2ST64_B32 : AMDGPU::DS_READ2ST64_B64;
248 assert((isUInt<8>(NewOffset0) && isUInt<8>(NewOffset1)) &&
249 (NewOffset0 != NewOffset1) &&
250 "Computed offset doesn't fit");
252 const MCInstrDesc &Read2Desc = TII->get(Opc);
254 const TargetRegisterClass *SuperRC
255 = (EltSize == 4) ? &AMDGPU::VReg_64RegClass : &AMDGPU::VReg_128RegClass;
256 unsigned DestReg = MRI->createVirtualRegister(SuperRC);
258 DebugLoc DL = I->getDebugLoc();
259 MachineInstrBuilder Read2
260 = BuildMI(*MBB, I, DL, Read2Desc, DestReg)
262 .addOperand(*AddrReg) // addr
263 .addImm(NewOffset0) // offset0
264 .addImm(NewOffset1) // offset1
265 .addMemOperand(*I->memoperands_begin())
266 .addMemOperand(*Paired->memoperands_begin());
268 LIS->InsertMachineInstrInMaps(Read2);
270 unsigned SubRegIdx0 = (EltSize == 4) ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
271 unsigned SubRegIdx1 = (EltSize == 4) ? AMDGPU::sub1 : AMDGPU::sub2_sub3;
272 updateRegDefsUses(DestReg0, DestReg, SubRegIdx0);
273 updateRegDefsUses(DestReg1, DestReg, SubRegIdx1);
275 LIS->RemoveMachineInstrFromMaps(I);
276 LIS->RemoveMachineInstrFromMaps(Paired);
277 I->eraseFromParent();
278 Paired->eraseFromParent();
280 LiveInterval &AddrRegLI = LIS->getInterval(AddrReg->getReg());
281 LIS->shrinkToUses(&AddrRegLI);
283 LIS->getInterval(DestReg); // Create new LI
285 DEBUG(dbgs() << "Inserted read2: " << *Read2 << '\n');
286 return Read2.getInstr();
289 MachineBasicBlock::iterator SILoadStoreOptimizer::mergeWrite2Pair(
290 MachineBasicBlock::iterator I,
291 MachineBasicBlock::iterator Paired,
293 MachineBasicBlock *MBB = I->getParent();
295 // Be sure to use .addOperand(), and not .addReg() with these. We want to be
296 // sure we preserve the subregister index and any register flags set on them.
297 const MachineOperand *Addr = TII->getNamedOperand(*I, AMDGPU::OpName::addr);
298 const MachineOperand *Data0 = TII->getNamedOperand(*I, AMDGPU::OpName::data0);
299 const MachineOperand *Data1
300 = TII->getNamedOperand(*Paired, AMDGPU::OpName::data0);
304 = TII->getNamedOperand(*I, AMDGPU::OpName::offset)->getImm() & 0xffff;
306 = TII->getNamedOperand(*Paired, AMDGPU::OpName::offset)->getImm() & 0xffff;
308 unsigned NewOffset0 = Offset0 / EltSize;
309 unsigned NewOffset1 = Offset1 / EltSize;
310 unsigned Opc = (EltSize == 4) ? AMDGPU::DS_WRITE2_B32 : AMDGPU::DS_WRITE2_B64;
312 // Prefer the st64 form if we can use it, even if we can fit the offset in the
313 // non st64 version. I'm not sure if there's any real reason to do this.
314 bool UseST64 = (NewOffset0 % 64 == 0) && (NewOffset1 % 64 == 0);
318 Opc = (EltSize == 4) ? AMDGPU::DS_WRITE2ST64_B32 : AMDGPU::DS_WRITE2ST64_B64;
321 assert((isUInt<8>(NewOffset0) && isUInt<8>(NewOffset1)) &&
322 (NewOffset0 != NewOffset1) &&
323 "Computed offset doesn't fit");
325 const MCInstrDesc &Write2Desc = TII->get(Opc);
326 DebugLoc DL = I->getDebugLoc();
328 MachineInstrBuilder Write2
329 = BuildMI(*MBB, I, DL, Write2Desc)
331 .addOperand(*Addr) // addr
332 .addOperand(*Data0) // data0
333 .addOperand(*Data1) // data1
334 .addImm(NewOffset0) // offset0
335 .addImm(NewOffset1) // offset1
336 .addMemOperand(*I->memoperands_begin())
337 .addMemOperand(*Paired->memoperands_begin());
339 // XXX - How do we express subregisters here?
340 unsigned OrigRegs[] = { Data0->getReg(), Data1->getReg(), Addr->getReg() };
342 LIS->RemoveMachineInstrFromMaps(I);
343 LIS->RemoveMachineInstrFromMaps(Paired);
344 I->eraseFromParent();
345 Paired->eraseFromParent();
347 LIS->repairIntervalsInRange(MBB, Write2, Write2, OrigRegs);
349 DEBUG(dbgs() << "Inserted write2 inst: " << *Write2 << '\n');
350 return Write2.getInstr();
353 // Scan through looking for adjacent LDS operations with constant offsets from
354 // the same base register. We rely on the scheduler to do the hard work of
355 // clustering nearby loads, and assume these are all adjacent.
356 bool SILoadStoreOptimizer::optimizeBlock(MachineBasicBlock &MBB) {
357 bool Modified = false;
359 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
360 MachineInstr &MI = *I;
362 // Don't combine if volatile.
363 if (MI.hasOrderedMemoryRef()) {
368 unsigned Opc = MI.getOpcode();
369 if (Opc == AMDGPU::DS_READ_B32 || Opc == AMDGPU::DS_READ_B64) {
370 unsigned Size = (Opc == AMDGPU::DS_READ_B64) ? 8 : 4;
371 MachineBasicBlock::iterator Match = findMatchingDSInst(I, Size);
374 I = mergeRead2Pair(I, Match, Size);
380 } else if (Opc == AMDGPU::DS_WRITE_B32 || Opc == AMDGPU::DS_WRITE_B64) {
381 unsigned Size = (Opc == AMDGPU::DS_WRITE_B64) ? 8 : 4;
382 MachineBasicBlock::iterator Match = findMatchingDSInst(I, Size);
385 I = mergeWrite2Pair(I, Match, Size);
399 bool SILoadStoreOptimizer::runOnMachineFunction(MachineFunction &MF) {
400 const TargetSubtargetInfo *STM = MF.getTarget().getSubtargetImpl();
401 TRI = static_cast<const SIRegisterInfo*>(STM->getRegisterInfo());
402 TII = static_cast<const SIInstrInfo*>(STM->getInstrInfo());
403 MRI = &MF.getRegInfo();
405 LIS = &getAnalysis<LiveIntervals>();
407 DEBUG(dbgs() << "Running SILoadStoreOptimizer\n");
409 assert(!MRI->isSSA());
411 bool Modified = false;
413 for (MachineBasicBlock &MBB : MF)
414 Modified |= optimizeBlock(MBB);