blob: 8793e9fb55ecac1490cde7a29f1889a8fed8df48 [file] [log] [blame]
Vincent Lejeune147700b2013-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 Lejeune147700b2013-04-30 00:14:27 +000017#include "llvm/Support/Debug.h"
Vincent Lejeune147700b2013-04-30 00:14:27 +000018#include "AMDGPU.h"
19#include "R600InstrInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000020#include "llvm/CodeGen/DFAPacketizer.h"
21#include "llvm/CodeGen/MachineDominators.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineLoopInfo.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/ScheduleDAG.h"
26#include "llvm/Support/raw_ostream.h"
Vincent Lejeune147700b2013-04-30 00:14:27 +000027
Benjamin Kramerd78bb462013-05-23 17:10:37 +000028using namespace llvm;
29
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "packets"
31
Benjamin Kramerd78bb462013-05-23 17:10:37 +000032namespace {
Vincent Lejeune147700b2013-04-30 00:14:27 +000033
34class R600Packetizer : public MachineFunctionPass {
35
36public:
37 static char ID;
38 R600Packetizer(const TargetMachine &TM) : MachineFunctionPass(ID) {}
39
40 void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.setPreservesCFG();
42 AU.addRequired<MachineDominatorTree>();
43 AU.addPreserved<MachineDominatorTree>();
44 AU.addRequired<MachineLoopInfo>();
45 AU.addPreserved<MachineLoopInfo>();
46 MachineFunctionPass::getAnalysisUsage(AU);
47 }
48
49 const char *getPassName() const {
50 return "R600 Packetizer";
51 }
52
53 bool runOnMachineFunction(MachineFunction &Fn);
54};
55char R600Packetizer::ID = 0;
56
57class R600PacketizerList : public VLIWPacketizerList {
58
59private:
60 const R600InstrInfo *TII;
61 const R600RegisterInfo &TRI;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000062 bool VLIW5;
63 bool ConsideredInstUsesAlreadyWrittenVectorElement;
Vincent Lejeune147700b2013-04-30 00:14:27 +000064
Vincent Lejeune147700b2013-04-30 00:14:27 +000065 unsigned getSlot(const MachineInstr *MI) const {
66 return TRI.getHWRegChan(MI->getOperand(0).getReg());
67 }
68
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000069 /// \returns register to PV chan mapping for bundle/single instructions that
Alp Tokercb402912014-01-24 17:20:08 +000070 /// immediately precedes I.
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000071 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
72 const {
73 DenseMap<unsigned, unsigned> Result;
Vincent Lejeune147700b2013-04-30 00:14:27 +000074 I--;
75 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
76 return Result;
77 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
78 if (I->isBundle())
79 BI++;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000080 int LastDstChan = -1;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000081 do {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000082 bool isTrans = false;
83 int BISlot = getSlot(BI);
84 if (LastDstChan >= BISlot)
85 isTrans = true;
86 LastDstChan = BISlot;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000087 if (TII->isPredicated(BI))
88 continue;
Tom Stellard02661d92013-06-25 21:22:18 +000089 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
Vincent Lejeune91a942b2013-06-03 15:56:12 +000090 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000091 continue;
Tom Stellardce540332013-06-28 15:46:59 +000092 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
93 if (DstIdx == -1) {
94 continue;
95 }
96 unsigned Dst = BI->getOperand(DstIdx).getReg();
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000097 if (isTrans || TII->isTransOnly(BI)) {
Vincent Lejeune77a83522013-06-29 19:32:43 +000098 Result[Dst] = AMDGPU::PS;
99 continue;
100 }
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000101 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
102 BI->getOpcode() == AMDGPU::DOT4_eg) {
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000103 Result[Dst] = AMDGPU::PV_X;
104 continue;
105 }
Tom Stellardc026e8b2013-06-28 15:47:08 +0000106 if (Dst == AMDGPU::OQAP) {
107 continue;
108 }
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000109 unsigned PVReg = 0;
110 switch (TRI.getHWRegChan(Dst)) {
111 case 0:
112 PVReg = AMDGPU::PV_X;
113 break;
114 case 1:
115 PVReg = AMDGPU::PV_Y;
116 break;
117 case 2:
118 PVReg = AMDGPU::PV_Z;
119 break;
120 case 3:
121 PVReg = AMDGPU::PV_W;
122 break;
123 default:
124 llvm_unreachable("Invalid Chan");
125 }
126 Result[Dst] = PVReg;
127 } while ((++BI)->isBundledWithPred());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000128 return Result;
129 }
130
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000131 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
132 const {
Tom Stellard02661d92013-06-25 21:22:18 +0000133 unsigned Ops[] = {
134 AMDGPU::OpName::src0,
135 AMDGPU::OpName::src1,
136 AMDGPU::OpName::src2
Vincent Lejeune147700b2013-04-30 00:14:27 +0000137 };
138 for (unsigned i = 0; i < 3; i++) {
139 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
140 if (OperandIdx < 0)
141 continue;
142 unsigned Src = MI->getOperand(OperandIdx).getReg();
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000143 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
144 if (It != PVs.end())
145 MI->getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000146 }
147 }
148public:
149 // Ctor.
150 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
151 MachineDominatorTree &MDT)
152 : VLIWPacketizerList(MF, MLI, MDT, true),
153 TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())),
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000154 TRI(TII->getRegisterInfo()) {
155 VLIW5 = !MF.getTarget().getSubtarget<AMDGPUSubtarget>().hasCaymanISA();
156 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000157
158 // initPacketizerState - initialize some internal flags.
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000159 void initPacketizerState() {
160 ConsideredInstUsesAlreadyWrittenVectorElement = false;
161 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000162
163 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
164 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
165 return false;
166 }
167
168 // isSoloInstruction - return true if instruction MI can not be packetized
169 // with any other instruction, which means that MI itself is a packet.
170 bool isSoloInstruction(MachineInstr *MI) {
171 if (TII->isVector(*MI))
172 return true;
173 if (!TII->isALUInstr(MI->getOpcode()))
174 return true;
Tom Stellardce540332013-06-28 15:46:59 +0000175 if (MI->getOpcode() == AMDGPU::GROUP_BARRIER)
176 return true;
Vincent Lejeune21de8ba2013-07-31 19:31:41 +0000177 // XXX: This can be removed once the packetizer properly handles all the
178 // LDS instruction group restrictions.
179 if (TII->isLDSInstr(MI->getOpcode()))
180 return true;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000181 return false;
182 }
183
184 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
185 // together.
186 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
187 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000188 if (getSlot(MII) == getSlot(MIJ))
189 ConsideredInstUsesAlreadyWrittenVectorElement = true;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000190 // Does MII and MIJ share the same pred_sel ?
Tom Stellard02661d92013-06-25 21:22:18 +0000191 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
192 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000193 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
194 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
195 if (PredI != PredJ)
196 return false;
197 if (SUJ->isSucc(SUI)) {
198 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
199 const SDep &Dep = SUJ->Succs[i];
200 if (Dep.getSUnit() != SUI)
201 continue;
202 if (Dep.getKind() == SDep::Anti)
203 continue;
204 if (Dep.getKind() == SDep::Output)
205 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
206 continue;
207 return false;
208 }
209 }
Tom Stellard26a3b672013-10-22 18:19:10 +0000210
211 bool ARDef = TII->definesAddressRegister(MII) ||
212 TII->definesAddressRegister(MIJ);
213 bool ARUse = TII->usesAddressRegister(MII) ||
214 TII->usesAddressRegister(MIJ);
215 if (ARDef && ARUse)
216 return false;
217
Vincent Lejeune147700b2013-04-30 00:14:27 +0000218 return true;
219 }
220
221 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
222 // and SUJ.
223 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
224
225 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
Tom Stellard02661d92013-06-25 21:22:18 +0000226 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000227 MI->getOperand(LastOp).setImm(Bit);
228 }
229
Vincent Lejeune77a83522013-06-29 19:32:43 +0000230 bool isBundlableWithCurrentPMI(MachineInstr *MI,
231 const DenseMap<unsigned, unsigned> &PV,
232 std::vector<R600InstrInfo::BankSwizzle> &BS,
233 bool &isTransSlot) {
234 isTransSlot = TII->isTransOnly(MI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000235 assert (!isTransSlot || VLIW5);
236
237 // Is the dst reg sequence legal ?
238 if (!isTransSlot && !CurrentPacketMIs.empty()) {
239 if (getSlot(MI) <= getSlot(CurrentPacketMIs.back())) {
240 if (ConsideredInstUsesAlreadyWrittenVectorElement &&
241 !TII->isVectorOnly(MI) && VLIW5) {
242 isTransSlot = true;
243 DEBUG(dbgs() << "Considering as Trans Inst :"; MI->dump(););
244 }
245 else
246 return false;
247 }
248 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000249
250 // Are the Constants limitations met ?
Vincent Lejeune147700b2013-04-30 00:14:27 +0000251 CurrentPacketMIs.push_back(MI);
Vincent Lejeune77a83522013-06-29 19:32:43 +0000252 if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) {
253 DEBUG(
Vincent Lejeune147700b2013-04-30 00:14:27 +0000254 dbgs() << "Couldn't pack :\n";
255 MI->dump();
256 dbgs() << "with the following packets :\n";
257 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
258 CurrentPacketMIs[i]->dump();
259 dbgs() << "\n";
260 }
261 dbgs() << "because of Consts read limitations\n";
Vincent Lejeune77a83522013-06-29 19:32:43 +0000262 );
263 CurrentPacketMIs.pop_back();
264 return false;
265 }
266
267 // Is there a BankSwizzle set that meet Read Port limitations ?
268 if (!TII->fitsReadPortLimitations(CurrentPacketMIs,
269 PV, BS, isTransSlot)) {
270 DEBUG(
Vincent Lejeune147700b2013-04-30 00:14:27 +0000271 dbgs() << "Couldn't pack :\n";
272 MI->dump();
273 dbgs() << "with the following packets :\n";
274 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
275 CurrentPacketMIs[i]->dump();
276 dbgs() << "\n";
277 }
278 dbgs() << "because of Read port limitations\n";
Vincent Lejeune77a83522013-06-29 19:32:43 +0000279 );
280 CurrentPacketMIs.pop_back();
281 return false;
282 }
283
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000284 // We cannot read LDS source registrs from the Trans slot.
285 if (isTransSlot && TII->readsLDSSrcReg(MI))
286 return false;
287
Vincent Lejeune77a83522013-06-29 19:32:43 +0000288 CurrentPacketMIs.pop_back();
289 return true;
290 }
291
292 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
293 MachineBasicBlock::iterator FirstInBundle =
294 CurrentPacketMIs.empty() ? MI : CurrentPacketMIs.front();
295 const DenseMap<unsigned, unsigned> &PV =
296 getPreviousVector(FirstInBundle);
297 std::vector<R600InstrInfo::BankSwizzle> BS;
298 bool isTransSlot;
299
300 if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000301 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
302 MachineInstr *MI = CurrentPacketMIs[i];
Vincent Lejeune77a83522013-06-29 19:32:43 +0000303 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
304 AMDGPU::OpName::bank_swizzle);
305 MI->getOperand(Op).setImm(BS[i]);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000306 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000307 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
308 AMDGPU::OpName::bank_swizzle);
309 MI->getOperand(Op).setImm(BS.back());
310 if (!CurrentPacketMIs.empty())
311 setIsLastBit(CurrentPacketMIs.back(), 0);
312 substitutePV(MI, PV);
313 MachineBasicBlock::iterator It = VLIWPacketizerList::addToPacket(MI);
314 if (isTransSlot) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000315 endPacket(std::next(It)->getParent(), std::next(It));
Vincent Lejeune77a83522013-06-29 19:32:43 +0000316 }
317 return It;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000318 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000319 endPacket(MI->getParent(), MI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000320 if (TII->isTransOnly(MI))
321 return MI;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000322 return VLIWPacketizerList::addToPacket(MI);
323 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000324};
325
326bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
327 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
328 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
329 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
330
331 // Instantiate the packetizer.
332 R600PacketizerList Packetizer(Fn, MLI, MDT);
333
334 // DFA state table should not be empty.
335 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
336
337 //
338 // Loop over all basic blocks and remove KILL pseudo-instructions
339 // These instructions confuse the dependence analysis. Consider:
340 // D0 = ... (Insn 0)
341 // R0 = KILL R0, D0 (Insn 1)
342 // R0 = ... (Insn 2)
343 // Here, Insn 1 will result in the dependence graph not emitting an output
344 // dependence between Insn 0 and Insn 2. This can lead to incorrect
345 // packetization
346 //
347 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
348 MBB != MBBe; ++MBB) {
349 MachineBasicBlock::iterator End = MBB->end();
350 MachineBasicBlock::iterator MI = MBB->begin();
351 while (MI != End) {
Tom Stellarded0ceec2013-10-10 17:11:12 +0000352 if (MI->isKill() || MI->getOpcode() == AMDGPU::IMPLICIT_DEF ||
Vincent Lejeunece499742013-07-09 15:03:33 +0000353 (MI->getOpcode() == AMDGPU::CF_ALU && !MI->getOperand(8).getImm())) {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000354 MachineBasicBlock::iterator DeleteMI = MI;
355 ++MI;
356 MBB->erase(DeleteMI);
357 End = MBB->end();
358 continue;
359 }
360 ++MI;
361 }
362 }
363
364 // Loop over all of the basic blocks.
365 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
366 MBB != MBBe; ++MBB) {
367 // Find scheduling regions and schedule / packetize each region.
368 unsigned RemainingCount = MBB->size();
369 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
370 RegionEnd != MBB->begin();) {
371 // The next region starts above the previous region. Look backward in the
372 // instruction stream until we find the nearest boundary.
373 MachineBasicBlock::iterator I = RegionEnd;
374 for(;I != MBB->begin(); --I, --RemainingCount) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000375 if (TII->isSchedulingBoundary(std::prev(I), MBB, Fn))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000376 break;
377 }
378 I = MBB->begin();
379
380 // Skip empty scheduling regions.
381 if (I == RegionEnd) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000382 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000383 --RemainingCount;
384 continue;
385 }
386 // Skip regions with one instruction.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000387 if (I == std::prev(RegionEnd)) {
388 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000389 continue;
390 }
391
392 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
393 RegionEnd = I;
394 }
395 }
396
397 return true;
398
399}
400
Benjamin Kramerd78bb462013-05-23 17:10:37 +0000401} // end anonymous namespace
Vincent Lejeune147700b2013-04-30 00:14:27 +0000402
403llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
404 return new R600Packetizer(tm);
405}