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