blob: ec89bff3c1bfbb52e0f4228105b9d5a9235a0489 [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
17#ifndef R600PACKETIZER_CPP
18#define R600PACKETIZER_CPP
19
20#define DEBUG_TYPE "packets"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/CodeGen/DFAPacketizer.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineDominators.h"
27#include "llvm/CodeGen/MachineLoopInfo.h"
28#include "llvm/CodeGen/ScheduleDAG.h"
29#include "AMDGPU.h"
30#include "R600InstrInfo.h"
31
32namespace llvm {
33
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;
62
Vincent Lejeune25f259c2013-04-30 00:14:27 +000063 unsigned getSlot(const MachineInstr *MI) const {
64 return TRI.getHWRegChan(MI->getOperand(0).getReg());
65 }
66
Vincent Lejeune152ebee2013-05-02 21:52:55 +000067 /// \returns register to PV chan mapping for bundle/single instructions that
68 /// immediatly precedes I.
69 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
70 const {
71 DenseMap<unsigned, unsigned> Result;
Vincent Lejeune25f259c2013-04-30 00:14:27 +000072 I--;
73 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
74 return Result;
75 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
76 if (I->isBundle())
77 BI++;
Vincent Lejeune152ebee2013-05-02 21:52:55 +000078 do {
79 if (TII->isPredicated(BI))
80 continue;
81 if (TII->isTransOnly(BI))
82 continue;
Vincent Lejeune25f259c2013-04-30 00:14:27 +000083 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), R600Operands::WRITE);
Vincent Lejeune152ebee2013-05-02 21:52:55 +000084 if (OperandIdx < 0)
85 continue;
86 if (BI->getOperand(OperandIdx).getImm() == 0)
87 continue;
88 unsigned Dst = BI->getOperand(0).getReg();
Vincent Lejeune4ed99172013-05-17 16:50:32 +000089 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
90 BI->getOpcode() == AMDGPU::DOT4_eg) {
Vincent Lejeune152ebee2013-05-02 21:52:55 +000091 Result[Dst] = AMDGPU::PV_X;
92 continue;
93 }
94 unsigned PVReg = 0;
95 switch (TRI.getHWRegChan(Dst)) {
96 case 0:
97 PVReg = AMDGPU::PV_X;
98 break;
99 case 1:
100 PVReg = AMDGPU::PV_Y;
101 break;
102 case 2:
103 PVReg = AMDGPU::PV_Z;
104 break;
105 case 3:
106 PVReg = AMDGPU::PV_W;
107 break;
108 default:
109 llvm_unreachable("Invalid Chan");
110 }
111 Result[Dst] = PVReg;
112 } while ((++BI)->isBundledWithPred());
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000113 return Result;
114 }
115
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000116 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
117 const {
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000118 R600Operands::Ops Ops[] = {
119 R600Operands::SRC0,
120 R600Operands::SRC1,
121 R600Operands::SRC2
122 };
123 for (unsigned i = 0; i < 3; i++) {
124 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
125 if (OperandIdx < 0)
126 continue;
127 unsigned Src = MI->getOperand(OperandIdx).getReg();
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000128 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
129 if (It != PVs.end())
130 MI->getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000131 }
132 }
133public:
134 // Ctor.
135 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
136 MachineDominatorTree &MDT)
137 : VLIWPacketizerList(MF, MLI, MDT, true),
138 TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())),
139 TRI(TII->getRegisterInfo()) { }
140
141 // initPacketizerState - initialize some internal flags.
142 void initPacketizerState() { }
143
144 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
145 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
146 return false;
147 }
148
149 // isSoloInstruction - return true if instruction MI can not be packetized
150 // with any other instruction, which means that MI itself is a packet.
151 bool isSoloInstruction(MachineInstr *MI) {
152 if (TII->isVector(*MI))
153 return true;
154 if (!TII->isALUInstr(MI->getOpcode()))
155 return true;
156 if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::TRANS_ONLY)
157 return true;
158 if (TII->isTransOnly(MI))
159 return true;
160 return false;
161 }
162
163 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
164 // together.
165 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
166 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
167 if (getSlot(MII) <= getSlot(MIJ))
168 return false;
169 // Does MII and MIJ share the same pred_sel ?
170 int OpI = TII->getOperandIdx(MII->getOpcode(), R600Operands::PRED_SEL),
171 OpJ = TII->getOperandIdx(MIJ->getOpcode(), R600Operands::PRED_SEL);
172 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
173 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
174 if (PredI != PredJ)
175 return false;
176 if (SUJ->isSucc(SUI)) {
177 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
178 const SDep &Dep = SUJ->Succs[i];
179 if (Dep.getSUnit() != SUI)
180 continue;
181 if (Dep.getKind() == SDep::Anti)
182 continue;
183 if (Dep.getKind() == SDep::Output)
184 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
185 continue;
186 return false;
187 }
188 }
189 return true;
190 }
191
192 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
193 // and SUJ.
194 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
195
196 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
197 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), R600Operands::LAST);
198 MI->getOperand(LastOp).setImm(Bit);
199 }
200
201 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
202 CurrentPacketMIs.push_back(MI);
203 bool FitsConstLimits = TII->canBundle(CurrentPacketMIs);
204 DEBUG(
205 if (!FitsConstLimits) {
206 dbgs() << "Couldn't pack :\n";
207 MI->dump();
208 dbgs() << "with the following packets :\n";
209 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
210 CurrentPacketMIs[i]->dump();
211 dbgs() << "\n";
212 }
213 dbgs() << "because of Consts read limitations\n";
214 });
Vincent Lejeune152ebee2013-05-02 21:52:55 +0000215 const DenseMap<unsigned, unsigned> &PV =
216 getPreviousVector(CurrentPacketMIs.front());
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000217 std::vector<R600InstrInfo::BankSwizzle> BS;
218 bool FitsReadPortLimits =
219 TII->fitsReadPortLimitations(CurrentPacketMIs, PV, BS);
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000220 DEBUG(
221 if (!FitsReadPortLimits) {
222 dbgs() << "Couldn't pack :\n";
223 MI->dump();
224 dbgs() << "with the following packets :\n";
225 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
226 CurrentPacketMIs[i]->dump();
227 dbgs() << "\n";
228 }
229 dbgs() << "because of Read port limitations\n";
230 });
231 bool isBundlable = FitsConstLimits && FitsReadPortLimits;
Vincent Lejeune25c209e2013-05-17 16:50:02 +0000232 if (isBundlable) {
233 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
234 MachineInstr *MI = CurrentPacketMIs[i];
235 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
236 R600Operands::BANK_SWIZZLE);
237 MI->getOperand(Op).setImm(BS[i]);
238 }
239 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000240 CurrentPacketMIs.pop_back();
241 if (!isBundlable) {
242 endPacket(MI->getParent(), MI);
243 substitutePV(MI, getPreviousVector(MI));
244 return VLIWPacketizerList::addToPacket(MI);
245 }
246 if (!CurrentPacketMIs.empty())
247 setIsLastBit(CurrentPacketMIs.back(), 0);
248 substitutePV(MI, PV);
249 return VLIWPacketizerList::addToPacket(MI);
250 }
Vincent Lejeune25f259c2013-04-30 00:14:27 +0000251};
252
253bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
254 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
255 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
256 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
257
258 // Instantiate the packetizer.
259 R600PacketizerList Packetizer(Fn, MLI, MDT);
260
261 // DFA state table should not be empty.
262 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
263
264 //
265 // Loop over all basic blocks and remove KILL pseudo-instructions
266 // These instructions confuse the dependence analysis. Consider:
267 // D0 = ... (Insn 0)
268 // R0 = KILL R0, D0 (Insn 1)
269 // R0 = ... (Insn 2)
270 // Here, Insn 1 will result in the dependence graph not emitting an output
271 // dependence between Insn 0 and Insn 2. This can lead to incorrect
272 // packetization
273 //
274 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
275 MBB != MBBe; ++MBB) {
276 MachineBasicBlock::iterator End = MBB->end();
277 MachineBasicBlock::iterator MI = MBB->begin();
278 while (MI != End) {
279 if (MI->isKill()) {
280 MachineBasicBlock::iterator DeleteMI = MI;
281 ++MI;
282 MBB->erase(DeleteMI);
283 End = MBB->end();
284 continue;
285 }
286 ++MI;
287 }
288 }
289
290 // Loop over all of the basic blocks.
291 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
292 MBB != MBBe; ++MBB) {
293 // Find scheduling regions and schedule / packetize each region.
294 unsigned RemainingCount = MBB->size();
295 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
296 RegionEnd != MBB->begin();) {
297 // The next region starts above the previous region. Look backward in the
298 // instruction stream until we find the nearest boundary.
299 MachineBasicBlock::iterator I = RegionEnd;
300 for(;I != MBB->begin(); --I, --RemainingCount) {
301 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn))
302 break;
303 }
304 I = MBB->begin();
305
306 // Skip empty scheduling regions.
307 if (I == RegionEnd) {
308 RegionEnd = llvm::prior(RegionEnd);
309 --RemainingCount;
310 continue;
311 }
312 // Skip regions with one instruction.
313 if (I == llvm::prior(RegionEnd)) {
314 RegionEnd = llvm::prior(RegionEnd);
315 continue;
316 }
317
318 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
319 RegionEnd = I;
320 }
321 }
322
323 return true;
324
325}
326
327}
328
329llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
330 return new R600Packetizer(tm);
331}
332
333#endif // R600PACKETIZER_CPP