blob: bed91157519a9dac81152b65ae59251ce4b3b329 [file] [log] [blame]
Vincent Lejeune25f259c2013-04-30 00:14:27 +00001//===----- R600Packetizer.cpp - VLIW packetizer ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// This pass implements instructions packetization for R600. It unsets isLast
12/// bit of instructions inside a bundle and substitutes src register with
13/// PreviousVector when applicable.
14//
15//===----------------------------------------------------------------------===//
16
Vincent Lejeune25f259c2013-04-30 00:14:27 +000017#define DEBUG_TYPE "packets"
18#include "llvm/Support/Debug.h"
Vincent Lejeune25f259c2013-04-30 00:14:27 +000019#include "AMDGPU.h"
20#include "R600InstrInfo.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000021#include "llvm/CodeGen/DFAPacketizer.h"
22#include "llvm/CodeGen/MachineDominators.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineLoopInfo.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/CodeGen/ScheduleDAG.h"
27#include "llvm/Support/raw_ostream.h"
Vincent Lejeune25f259c2013-04-30 00:14:27 +000028
Benjamin Kramer5c352902013-05-23 17:10:37 +000029using namespace llvm;
30
31namespace {
Vincent Lejeune25f259c2013-04-30 00:14:27 +000032
33class R600Packetizer : public MachineFunctionPass {
34
35public:
36 static char ID;
37 R600Packetizer(const TargetMachine &TM) : MachineFunctionPass(ID) {}
38
39 void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesCFG();
41 AU.addRequired<MachineDominatorTree>();
42 AU.addPreserved<MachineDominatorTree>();
43 AU.addRequired<MachineLoopInfo>();
44 AU.addPreserved<MachineLoopInfo>();
45 MachineFunctionPass::getAnalysisUsage(AU);
46 }
47
48 const char *getPassName() const {
49 return "R600 Packetizer";
50 }
51
52 bool runOnMachineFunction(MachineFunction &Fn);
53};
54char R600Packetizer::ID = 0;
55
56class R600PacketizerList : public VLIWPacketizerList {
57
58private:
59 const R600InstrInfo *TII;
60 const R600RegisterInfo &TRI;
Vincent Lejeunebb25a012013-09-04 19:53:46 +000061 bool VLIW5;
62 bool ConsideredInstUsesAlreadyWrittenVectorElement;
Vincent Lejeune25f259c2013-04-30 00:14:27 +000063
Vincent Lejeune25f259c2013-04-30 00:14:27 +000064 unsigned getSlot(const MachineInstr *MI) const {
65 return TRI.getHWRegChan(MI->getOperand(0).getReg());
66 }
67
Vincent Lejeune152ebee2013-05-02 21:52:55 +000068 /// \returns register to PV chan mapping for bundle/single instructions that
69 /// immediatly precedes I.
70 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
71 const {
72 DenseMap<unsigned, unsigned> Result;
Vincent Lejeune25f259c2013-04-30 00:14:27 +000073 I--;
74 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
75 return Result;
76 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
77 if (I->isBundle())
78 BI++;
Vincent Lejeunebb25a012013-09-04 19:53:46 +000079 int LastDstChan = -1;
Vincent Lejeune152ebee2013-05-02 21:52:55 +000080 do {
Vincent Lejeunebb25a012013-09-04 19:53:46 +000081 bool isTrans = false;
82 int BISlot = getSlot(BI);
83 if (LastDstChan >= BISlot)
84 isTrans = true;
85 LastDstChan = BISlot;
Vincent Lejeune152ebee2013-05-02 21:52:55 +000086 if (TII->isPredicated(BI))
87 continue;
Tom Stellard5e48a0e2013-06-25 21:22:18 +000088 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
Vincent Lejeune0c922872013-06-03 15:56:12 +000089 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
Vincent Lejeune152ebee2013-05-02 21:52:55 +000090 continue;
Tom Stellardcedcfee2013-06-28 15:46:59 +000091 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
92 if (DstIdx == -1) {
93 continue;
94 }
95 unsigned Dst = BI->getOperand(DstIdx).getReg();
Vincent Lejeunebb25a012013-09-04 19:53:46 +000096 if (isTrans || TII->isTransOnly(BI)) {
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +000097 Result[Dst] = AMDGPU::PS;
98 continue;
99 }
Vincent Lejeune4ed99172013-05-17 16:50:32 +0000100 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
101 BI->getOpcode() == AMDGPU::DOT4_eg) {
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000102 Result[Dst] = AMDGPU::PV_X;
103 continue;
104 }
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000105 if (Dst == AMDGPU::OQAP) {
106 continue;
107 }
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000108 unsigned PVReg = 0;
109 switch (TRI.getHWRegChan(Dst)) {
110 case 0:
111 PVReg = AMDGPU::PV_X;
112 break;
113 case 1:
114 PVReg = AMDGPU::PV_Y;
115 break;
116 case 2:
117 PVReg = AMDGPU::PV_Z;
118 break;
119 case 3:
120 PVReg = AMDGPU::PV_W;
121 break;
122 default:
123 llvm_unreachable("Invalid Chan");
124 }
125 Result[Dst] = PVReg;
126 } while ((++BI)->isBundledWithPred());
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000127 return Result;
128 }
129
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000130 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
131 const {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000132 unsigned Ops[] = {
133 AMDGPU::OpName::src0,
134 AMDGPU::OpName::src1,
135 AMDGPU::OpName::src2
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000136 };
137 for (unsigned i = 0; i < 3; i++) {
138 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
139 if (OperandIdx < 0)
140 continue;
141 unsigned Src = MI->getOperand(OperandIdx).getReg();
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000142 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
143 if (It != PVs.end())
144 MI->getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000145 }
146 }
147public:
148 // Ctor.
149 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
150 MachineDominatorTree &MDT)
151 : VLIWPacketizerList(MF, MLI, MDT, true),
152 TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())),
Vincent Lejeunebb25a012013-09-04 19:53:46 +0000153 TRI(TII->getRegisterInfo()) {
154 VLIW5 = !MF.getTarget().getSubtarget<AMDGPUSubtarget>().hasCaymanISA();
155 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000156
157 // initPacketizerState - initialize some internal flags.
Vincent Lejeunebb25a012013-09-04 19:53:46 +0000158 void initPacketizerState() {
159 ConsideredInstUsesAlreadyWrittenVectorElement = false;
160 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000161
162 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
163 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
164 return false;
165 }
166
167 // isSoloInstruction - return true if instruction MI can not be packetized
168 // with any other instruction, which means that MI itself is a packet.
169 bool isSoloInstruction(MachineInstr *MI) {
170 if (TII->isVector(*MI))
171 return true;
172 if (!TII->isALUInstr(MI->getOpcode()))
173 return true;
Tom Stellardcedcfee2013-06-28 15:46:59 +0000174 if (MI->getOpcode() == AMDGPU::GROUP_BARRIER)
175 return true;
Vincent Lejeunea92f8ee2013-07-31 19:31:41 +0000176 // XXX: This can be removed once the packetizer properly handles all the
177 // LDS instruction group restrictions.
178 if (TII->isLDSInstr(MI->getOpcode()))
179 return true;
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000180 return false;
181 }
182
183 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
184 // together.
185 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
186 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
Vincent Lejeunebb25a012013-09-04 19:53:46 +0000187 if (getSlot(MII) == getSlot(MIJ))
188 ConsideredInstUsesAlreadyWrittenVectorElement = true;
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000189 // Does MII and MIJ share the same pred_sel ?
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000190 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
191 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000192 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
193 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
194 if (PredI != PredJ)
195 return false;
196 if (SUJ->isSucc(SUI)) {
197 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
198 const SDep &Dep = SUJ->Succs[i];
199 if (Dep.getSUnit() != SUI)
200 continue;
201 if (Dep.getKind() == SDep::Anti)
202 continue;
203 if (Dep.getKind() == SDep::Output)
204 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
205 continue;
206 return false;
207 }
208 }
209 return true;
210 }
211
212 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
213 // and SUJ.
214 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
215
216 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000217 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000218 MI->getOperand(LastOp).setImm(Bit);
219 }
220
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000221 bool isBundlableWithCurrentPMI(MachineInstr *MI,
222 const DenseMap<unsigned, unsigned> &PV,
223 std::vector<R600InstrInfo::BankSwizzle> &BS,
224 bool &isTransSlot) {
225 isTransSlot = TII->isTransOnly(MI);
Vincent Lejeunebb25a012013-09-04 19:53:46 +0000226 assert (!isTransSlot || VLIW5);
227
228 // Is the dst reg sequence legal ?
229 if (!isTransSlot && !CurrentPacketMIs.empty()) {
230 if (getSlot(MI) <= getSlot(CurrentPacketMIs.back())) {
231 if (ConsideredInstUsesAlreadyWrittenVectorElement &&
232 !TII->isVectorOnly(MI) && VLIW5) {
233 isTransSlot = true;
234 DEBUG(dbgs() << "Considering as Trans Inst :"; MI->dump(););
235 }
236 else
237 return false;
238 }
239 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000240
241 // Are the Constants limitations met ?
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000242 CurrentPacketMIs.push_back(MI);
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000243 if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) {
244 DEBUG(
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000245 dbgs() << "Couldn't pack :\n";
246 MI->dump();
247 dbgs() << "with the following packets :\n";
248 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
249 CurrentPacketMIs[i]->dump();
250 dbgs() << "\n";
251 }
252 dbgs() << "because of Consts read limitations\n";
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000253 );
254 CurrentPacketMIs.pop_back();
255 return false;
256 }
257
258 // Is there a BankSwizzle set that meet Read Port limitations ?
259 if (!TII->fitsReadPortLimitations(CurrentPacketMIs,
260 PV, BS, isTransSlot)) {
261 DEBUG(
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000262 dbgs() << "Couldn't pack :\n";
263 MI->dump();
264 dbgs() << "with the following packets :\n";
265 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
266 CurrentPacketMIs[i]->dump();
267 dbgs() << "\n";
268 }
269 dbgs() << "because of Read port limitations\n";
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000270 );
271 CurrentPacketMIs.pop_back();
272 return false;
273 }
274
Tom Stellardac779b82013-09-12 02:55:06 +0000275 // We cannot read LDS source registrs from the Trans slot.
276 if (isTransSlot && TII->readsLDSSrcReg(MI))
277 return false;
278
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000279 CurrentPacketMIs.pop_back();
280 return true;
281 }
282
283 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
284 MachineBasicBlock::iterator FirstInBundle =
285 CurrentPacketMIs.empty() ? MI : CurrentPacketMIs.front();
286 const DenseMap<unsigned, unsigned> &PV =
287 getPreviousVector(FirstInBundle);
288 std::vector<R600InstrInfo::BankSwizzle> BS;
289 bool isTransSlot;
290
291 if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) {
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000292 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
293 MachineInstr *MI = CurrentPacketMIs[i];
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000294 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
295 AMDGPU::OpName::bank_swizzle);
296 MI->getOperand(Op).setImm(BS[i]);
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000297 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000298 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
299 AMDGPU::OpName::bank_swizzle);
300 MI->getOperand(Op).setImm(BS.back());
301 if (!CurrentPacketMIs.empty())
302 setIsLastBit(CurrentPacketMIs.back(), 0);
303 substitutePV(MI, PV);
304 MachineBasicBlock::iterator It = VLIWPacketizerList::addToPacket(MI);
305 if (isTransSlot) {
306 endPacket(llvm::next(It)->getParent(), llvm::next(It));
307 }
308 return It;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000309 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000310 endPacket(MI->getParent(), MI);
Vincent Lejeunebb25a012013-09-04 19:53:46 +0000311 if (TII->isTransOnly(MI))
312 return MI;
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000313 return VLIWPacketizerList::addToPacket(MI);
314 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000315};
316
317bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
318 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
319 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
320 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
321
322 // Instantiate the packetizer.
323 R600PacketizerList Packetizer(Fn, MLI, MDT);
324
325 // DFA state table should not be empty.
326 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
327
328 //
329 // Loop over all basic blocks and remove KILL pseudo-instructions
330 // These instructions confuse the dependence analysis. Consider:
331 // D0 = ... (Insn 0)
332 // R0 = KILL R0, D0 (Insn 1)
333 // R0 = ... (Insn 2)
334 // Here, Insn 1 will result in the dependence graph not emitting an output
335 // dependence between Insn 0 and Insn 2. This can lead to incorrect
336 // packetization
337 //
338 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
339 MBB != MBBe; ++MBB) {
340 MachineBasicBlock::iterator End = MBB->end();
341 MachineBasicBlock::iterator MI = MBB->begin();
342 while (MI != End) {
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000343 if (MI->isKill() ||
344 (MI->getOpcode() == AMDGPU::CF_ALU && !MI->getOperand(8).getImm())) {
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000345 MachineBasicBlock::iterator DeleteMI = MI;
346 ++MI;
347 MBB->erase(DeleteMI);
348 End = MBB->end();
349 continue;
350 }
351 ++MI;
352 }
353 }
354
355 // Loop over all of the basic blocks.
356 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
357 MBB != MBBe; ++MBB) {
358 // Find scheduling regions and schedule / packetize each region.
359 unsigned RemainingCount = MBB->size();
360 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
361 RegionEnd != MBB->begin();) {
362 // The next region starts above the previous region. Look backward in the
363 // instruction stream until we find the nearest boundary.
364 MachineBasicBlock::iterator I = RegionEnd;
365 for(;I != MBB->begin(); --I, --RemainingCount) {
366 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn))
367 break;
368 }
369 I = MBB->begin();
370
371 // Skip empty scheduling regions.
372 if (I == RegionEnd) {
373 RegionEnd = llvm::prior(RegionEnd);
374 --RemainingCount;
375 continue;
376 }
377 // Skip regions with one instruction.
378 if (I == llvm::prior(RegionEnd)) {
379 RegionEnd = llvm::prior(RegionEnd);
380 continue;
381 }
382
383 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
384 RegionEnd = I;
385 }
386 }
387
388 return true;
389
390}
391
Benjamin Kramer5c352902013-05-23 17:10:37 +0000392} // end anonymous namespace
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000393
394llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
395 return new R600Packetizer(tm);
396}