blob: f4219bd476c07634961fc8637462573f274e69f3 [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;
61
Vincent Lejeune25f259c2013-04-30 00:14:27 +000062 unsigned getSlot(const MachineInstr *MI) const {
63 return TRI.getHWRegChan(MI->getOperand(0).getReg());
64 }
65
Vincent Lejeune152ebee2013-05-02 21:52:55 +000066 /// \returns register to PV chan mapping for bundle/single instructions that
67 /// immediatly precedes I.
68 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
69 const {
70 DenseMap<unsigned, unsigned> Result;
Vincent Lejeune25f259c2013-04-30 00:14:27 +000071 I--;
72 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
73 return Result;
74 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
75 if (I->isBundle())
76 BI++;
Vincent Lejeune152ebee2013-05-02 21:52:55 +000077 do {
78 if (TII->isPredicated(BI))
79 continue;
Tom Stellard5e48a0e2013-06-25 21:22:18 +000080 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
Vincent Lejeune0c922872013-06-03 15:56:12 +000081 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
Vincent Lejeune152ebee2013-05-02 21:52:55 +000082 continue;
Tom Stellardcedcfee2013-06-28 15:46:59 +000083 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
84 if (DstIdx == -1) {
85 continue;
86 }
87 unsigned Dst = BI->getOperand(DstIdx).getReg();
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +000088 if (TII->isTransOnly(BI)) {
89 Result[Dst] = AMDGPU::PS;
90 continue;
91 }
Vincent Lejeune4ed99172013-05-17 16:50:32 +000092 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
93 BI->getOpcode() == AMDGPU::DOT4_eg) {
Vincent Lejeune152ebee2013-05-02 21:52:55 +000094 Result[Dst] = AMDGPU::PV_X;
95 continue;
96 }
Tom Stellarde3d4cbc2013-06-28 15:47:08 +000097 if (Dst == AMDGPU::OQAP) {
98 continue;
99 }
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000100 unsigned PVReg = 0;
101 switch (TRI.getHWRegChan(Dst)) {
102 case 0:
103 PVReg = AMDGPU::PV_X;
104 break;
105 case 1:
106 PVReg = AMDGPU::PV_Y;
107 break;
108 case 2:
109 PVReg = AMDGPU::PV_Z;
110 break;
111 case 3:
112 PVReg = AMDGPU::PV_W;
113 break;
114 default:
115 llvm_unreachable("Invalid Chan");
116 }
117 Result[Dst] = PVReg;
118 } while ((++BI)->isBundledWithPred());
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000119 return Result;
120 }
121
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000122 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
123 const {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000124 unsigned Ops[] = {
125 AMDGPU::OpName::src0,
126 AMDGPU::OpName::src1,
127 AMDGPU::OpName::src2
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000128 };
129 for (unsigned i = 0; i < 3; i++) {
130 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
131 if (OperandIdx < 0)
132 continue;
133 unsigned Src = MI->getOperand(OperandIdx).getReg();
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000134 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
135 if (It != PVs.end())
136 MI->getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000137 }
138 }
139public:
140 // Ctor.
141 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
142 MachineDominatorTree &MDT)
143 : VLIWPacketizerList(MF, MLI, MDT, true),
144 TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())),
145 TRI(TII->getRegisterInfo()) { }
146
147 // initPacketizerState - initialize some internal flags.
148 void initPacketizerState() { }
149
150 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
151 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
152 return false;
153 }
154
155 // isSoloInstruction - return true if instruction MI can not be packetized
156 // with any other instruction, which means that MI itself is a packet.
157 bool isSoloInstruction(MachineInstr *MI) {
158 if (TII->isVector(*MI))
159 return true;
160 if (!TII->isALUInstr(MI->getOpcode()))
161 return true;
Tom Stellardcedcfee2013-06-28 15:46:59 +0000162 if (MI->getOpcode() == AMDGPU::GROUP_BARRIER)
163 return true;
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000164 return false;
165 }
166
167 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
168 // together.
169 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
170 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000171 if (getSlot(MII) <= getSlot(MIJ) && !TII->isTransOnly(MII))
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000172 return false;
173 // Does MII and MIJ share the same pred_sel ?
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000174 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
175 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000176 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
177 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
178 if (PredI != PredJ)
179 return false;
180 if (SUJ->isSucc(SUI)) {
181 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
182 const SDep &Dep = SUJ->Succs[i];
183 if (Dep.getSUnit() != SUI)
184 continue;
185 if (Dep.getKind() == SDep::Anti)
186 continue;
187 if (Dep.getKind() == SDep::Output)
188 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
189 continue;
190 return false;
191 }
192 }
193 return true;
194 }
195
196 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
197 // and SUJ.
198 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
199
200 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000201 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000202 MI->getOperand(LastOp).setImm(Bit);
203 }
204
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000205 bool isBundlableWithCurrentPMI(MachineInstr *MI,
206 const DenseMap<unsigned, unsigned> &PV,
207 std::vector<R600InstrInfo::BankSwizzle> &BS,
208 bool &isTransSlot) {
209 isTransSlot = TII->isTransOnly(MI);
210
211 // Are the Constants limitations met ?
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000212 CurrentPacketMIs.push_back(MI);
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000213 if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) {
214 DEBUG(
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000215 dbgs() << "Couldn't pack :\n";
216 MI->dump();
217 dbgs() << "with the following packets :\n";
218 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
219 CurrentPacketMIs[i]->dump();
220 dbgs() << "\n";
221 }
222 dbgs() << "because of Consts read limitations\n";
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000223 );
224 CurrentPacketMIs.pop_back();
225 return false;
226 }
227
228 // Is there a BankSwizzle set that meet Read Port limitations ?
229 if (!TII->fitsReadPortLimitations(CurrentPacketMIs,
230 PV, BS, isTransSlot)) {
231 DEBUG(
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000232 dbgs() << "Couldn't pack :\n";
233 MI->dump();
234 dbgs() << "with the following packets :\n";
235 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
236 CurrentPacketMIs[i]->dump();
237 dbgs() << "\n";
238 }
239 dbgs() << "because of Read port limitations\n";
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000240 );
241 CurrentPacketMIs.pop_back();
242 return false;
243 }
244
245 CurrentPacketMIs.pop_back();
246 return true;
247 }
248
249 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
250 MachineBasicBlock::iterator FirstInBundle =
251 CurrentPacketMIs.empty() ? MI : CurrentPacketMIs.front();
252 const DenseMap<unsigned, unsigned> &PV =
253 getPreviousVector(FirstInBundle);
254 std::vector<R600InstrInfo::BankSwizzle> BS;
255 bool isTransSlot;
256
257 if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) {
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000258 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
259 MachineInstr *MI = CurrentPacketMIs[i];
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000260 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
261 AMDGPU::OpName::bank_swizzle);
262 MI->getOperand(Op).setImm(BS[i]);
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000263 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000264 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
265 AMDGPU::OpName::bank_swizzle);
266 MI->getOperand(Op).setImm(BS.back());
267 if (!CurrentPacketMIs.empty())
268 setIsLastBit(CurrentPacketMIs.back(), 0);
269 substitutePV(MI, PV);
270 MachineBasicBlock::iterator It = VLIWPacketizerList::addToPacket(MI);
271 if (isTransSlot) {
272 endPacket(llvm::next(It)->getParent(), llvm::next(It));
273 }
274 return It;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000275 }
Vincent Lejeune8f9fbd62013-06-29 19:32:43 +0000276 endPacket(MI->getParent(), MI);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000277 return VLIWPacketizerList::addToPacket(MI);
278 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000279};
280
281bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
282 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
283 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
284 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
285
286 // Instantiate the packetizer.
287 R600PacketizerList Packetizer(Fn, MLI, MDT);
288
289 // DFA state table should not be empty.
290 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
291
292 //
293 // Loop over all basic blocks and remove KILL pseudo-instructions
294 // These instructions confuse the dependence analysis. Consider:
295 // D0 = ... (Insn 0)
296 // R0 = KILL R0, D0 (Insn 1)
297 // R0 = ... (Insn 2)
298 // Here, Insn 1 will result in the dependence graph not emitting an output
299 // dependence between Insn 0 and Insn 2. This can lead to incorrect
300 // packetization
301 //
302 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
303 MBB != MBBe; ++MBB) {
304 MachineBasicBlock::iterator End = MBB->end();
305 MachineBasicBlock::iterator MI = MBB->begin();
306 while (MI != End) {
Vincent Lejeunef2cfef82013-07-09 15:03:33 +0000307 if (MI->isKill() ||
308 (MI->getOpcode() == AMDGPU::CF_ALU && !MI->getOperand(8).getImm())) {
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000309 MachineBasicBlock::iterator DeleteMI = MI;
310 ++MI;
311 MBB->erase(DeleteMI);
312 End = MBB->end();
313 continue;
314 }
315 ++MI;
316 }
317 }
318
319 // Loop over all of the basic blocks.
320 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
321 MBB != MBBe; ++MBB) {
322 // Find scheduling regions and schedule / packetize each region.
323 unsigned RemainingCount = MBB->size();
324 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
325 RegionEnd != MBB->begin();) {
326 // The next region starts above the previous region. Look backward in the
327 // instruction stream until we find the nearest boundary.
328 MachineBasicBlock::iterator I = RegionEnd;
329 for(;I != MBB->begin(); --I, --RemainingCount) {
330 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn))
331 break;
332 }
333 I = MBB->begin();
334
335 // Skip empty scheduling regions.
336 if (I == RegionEnd) {
337 RegionEnd = llvm::prior(RegionEnd);
338 --RemainingCount;
339 continue;
340 }
341 // Skip regions with one instruction.
342 if (I == llvm::prior(RegionEnd)) {
343 RegionEnd = llvm::prior(RegionEnd);
344 continue;
345 }
346
347 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
348 RegionEnd = I;
349 }
350 }
351
352 return true;
353
354}
355
Benjamin Kramer5c352902013-05-23 17:10:37 +0000356} // end anonymous namespace
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000357
358llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
359 return new R600Packetizer(tm);
360}