blob: 4c72d2296751b56ee58721ee16f6d98625af11d3 [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;
80 if (TII->isTransOnly(BI))
81 continue;
Tom Stellard5e48a0e2013-06-25 21:22:18 +000082 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
Vincent Lejeune0c922872013-06-03 15:56:12 +000083 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
Vincent Lejeune152ebee2013-05-02 21:52:55 +000084 continue;
Tom Stellardcedcfee2013-06-28 15:46:59 +000085 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
86 if (DstIdx == -1) {
87 continue;
88 }
89 unsigned Dst = BI->getOperand(DstIdx).getReg();
Vincent Lejeune4ed99172013-05-17 16:50:32 +000090 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
91 BI->getOpcode() == AMDGPU::DOT4_eg) {
Vincent Lejeune152ebee2013-05-02 21:52:55 +000092 Result[Dst] = AMDGPU::PV_X;
93 continue;
94 }
95 unsigned PVReg = 0;
96 switch (TRI.getHWRegChan(Dst)) {
97 case 0:
98 PVReg = AMDGPU::PV_X;
99 break;
100 case 1:
101 PVReg = AMDGPU::PV_Y;
102 break;
103 case 2:
104 PVReg = AMDGPU::PV_Z;
105 break;
106 case 3:
107 PVReg = AMDGPU::PV_W;
108 break;
109 default:
110 llvm_unreachable("Invalid Chan");
111 }
112 Result[Dst] = PVReg;
113 } while ((++BI)->isBundledWithPred());
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000114 return Result;
115 }
116
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000117 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
118 const {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000119 unsigned Ops[] = {
120 AMDGPU::OpName::src0,
121 AMDGPU::OpName::src1,
122 AMDGPU::OpName::src2
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000123 };
124 for (unsigned i = 0; i < 3; i++) {
125 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
126 if (OperandIdx < 0)
127 continue;
128 unsigned Src = MI->getOperand(OperandIdx).getReg();
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000129 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
130 if (It != PVs.end())
131 MI->getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000132 }
133 }
134public:
135 // Ctor.
136 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
137 MachineDominatorTree &MDT)
138 : VLIWPacketizerList(MF, MLI, MDT, true),
139 TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())),
140 TRI(TII->getRegisterInfo()) { }
141
142 // initPacketizerState - initialize some internal flags.
143 void initPacketizerState() { }
144
145 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
146 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
147 return false;
148 }
149
150 // isSoloInstruction - return true if instruction MI can not be packetized
151 // with any other instruction, which means that MI itself is a packet.
152 bool isSoloInstruction(MachineInstr *MI) {
153 if (TII->isVector(*MI))
154 return true;
155 if (!TII->isALUInstr(MI->getOpcode()))
156 return true;
157 if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::TRANS_ONLY)
158 return true;
159 if (TII->isTransOnly(MI))
160 return true;
Tom Stellardcedcfee2013-06-28 15:46:59 +0000161 if (MI->getOpcode() == AMDGPU::GROUP_BARRIER)
162 return true;
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000163 return false;
164 }
165
166 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
167 // together.
168 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
169 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
170 if (getSlot(MII) <= getSlot(MIJ))
171 return false;
172 // Does MII and MIJ share the same pred_sel ?
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000173 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
174 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000175 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
176 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
177 if (PredI != PredJ)
178 return false;
179 if (SUJ->isSucc(SUI)) {
180 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
181 const SDep &Dep = SUJ->Succs[i];
182 if (Dep.getSUnit() != SUI)
183 continue;
184 if (Dep.getKind() == SDep::Anti)
185 continue;
186 if (Dep.getKind() == SDep::Output)
187 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
188 continue;
189 return false;
190 }
191 }
192 return true;
193 }
194
195 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
196 // and SUJ.
197 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
198
199 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000200 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000201 MI->getOperand(LastOp).setImm(Bit);
202 }
203
204 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
205 CurrentPacketMIs.push_back(MI);
206 bool FitsConstLimits = TII->canBundle(CurrentPacketMIs);
207 DEBUG(
208 if (!FitsConstLimits) {
209 dbgs() << "Couldn't pack :\n";
210 MI->dump();
211 dbgs() << "with the following packets :\n";
212 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
213 CurrentPacketMIs[i]->dump();
214 dbgs() << "\n";
215 }
216 dbgs() << "because of Consts read limitations\n";
217 });
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000218 const DenseMap<unsigned, unsigned> &PV =
219 getPreviousVector(CurrentPacketMIs.front());
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000220 std::vector<R600InstrInfo::BankSwizzle> BS;
221 bool FitsReadPortLimits =
222 TII->fitsReadPortLimitations(CurrentPacketMIs, PV, BS);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000223 DEBUG(
224 if (!FitsReadPortLimits) {
225 dbgs() << "Couldn't pack :\n";
226 MI->dump();
227 dbgs() << "with the following packets :\n";
228 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
229 CurrentPacketMIs[i]->dump();
230 dbgs() << "\n";
231 }
232 dbgs() << "because of Read port limitations\n";
233 });
234 bool isBundlable = FitsConstLimits && FitsReadPortLimits;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000235 if (isBundlable) {
236 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
237 MachineInstr *MI = CurrentPacketMIs[i];
238 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000239 AMDGPU::OpName::bank_swizzle);
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000240 MI->getOperand(Op).setImm(BS[i]);
241 }
242 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000243 CurrentPacketMIs.pop_back();
244 if (!isBundlable) {
245 endPacket(MI->getParent(), MI);
246 substitutePV(MI, getPreviousVector(MI));
247 return VLIWPacketizerList::addToPacket(MI);
248 }
249 if (!CurrentPacketMIs.empty())
250 setIsLastBit(CurrentPacketMIs.back(), 0);
251 substitutePV(MI, PV);
252 return VLIWPacketizerList::addToPacket(MI);
253 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000254};
255
256bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
257 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
258 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
259 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
260
261 // Instantiate the packetizer.
262 R600PacketizerList Packetizer(Fn, MLI, MDT);
263
264 // DFA state table should not be empty.
265 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
266
267 //
268 // Loop over all basic blocks and remove KILL pseudo-instructions
269 // These instructions confuse the dependence analysis. Consider:
270 // D0 = ... (Insn 0)
271 // R0 = KILL R0, D0 (Insn 1)
272 // R0 = ... (Insn 2)
273 // Here, Insn 1 will result in the dependence graph not emitting an output
274 // dependence between Insn 0 and Insn 2. This can lead to incorrect
275 // packetization
276 //
277 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
278 MBB != MBBe; ++MBB) {
279 MachineBasicBlock::iterator End = MBB->end();
280 MachineBasicBlock::iterator MI = MBB->begin();
281 while (MI != End) {
282 if (MI->isKill()) {
283 MachineBasicBlock::iterator DeleteMI = MI;
284 ++MI;
285 MBB->erase(DeleteMI);
286 End = MBB->end();
287 continue;
288 }
289 ++MI;
290 }
291 }
292
293 // Loop over all of the basic blocks.
294 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
295 MBB != MBBe; ++MBB) {
296 // Find scheduling regions and schedule / packetize each region.
297 unsigned RemainingCount = MBB->size();
298 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
299 RegionEnd != MBB->begin();) {
300 // The next region starts above the previous region. Look backward in the
301 // instruction stream until we find the nearest boundary.
302 MachineBasicBlock::iterator I = RegionEnd;
303 for(;I != MBB->begin(); --I, --RemainingCount) {
304 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn))
305 break;
306 }
307 I = MBB->begin();
308
309 // Skip empty scheduling regions.
310 if (I == RegionEnd) {
311 RegionEnd = llvm::prior(RegionEnd);
312 --RemainingCount;
313 continue;
314 }
315 // Skip regions with one instruction.
316 if (I == llvm::prior(RegionEnd)) {
317 RegionEnd = llvm::prior(RegionEnd);
318 continue;
319 }
320
321 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
322 RegionEnd = I;
323 }
324 }
325
326 return true;
327
328}
329
Benjamin Kramer5c352902013-05-23 17:10:37 +0000330} // end anonymous namespace
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000331
332llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
333 return new R600Packetizer(tm);
334}