1 //===----- R600Packetizer.cpp - VLIW packetizer ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/Debug.h"
19 #include "R600InstrInfo.h"
20 #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"
30 #define DEBUG_TYPE "packets"
34 class R600Packetizer : public MachineFunctionPass {
38 R600Packetizer(const TargetMachine &TM) : MachineFunctionPass(ID) {}
40 void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<MachineDominatorTree>();
43 AU.addPreserved<MachineDominatorTree>();
44 AU.addRequired<MachineLoopInfo>();
45 AU.addPreserved<MachineLoopInfo>();
46 MachineFunctionPass::getAnalysisUsage(AU);
49 const char *getPassName() const {
50 return "R600 Packetizer";
53 bool runOnMachineFunction(MachineFunction &Fn);
55 char R600Packetizer::ID = 0;
57 class R600PacketizerList : public VLIWPacketizerList {
60 const R600InstrInfo *TII;
61 const R600RegisterInfo &TRI;
63 bool ConsideredInstUsesAlreadyWrittenVectorElement;
65 unsigned getSlot(const MachineInstr *MI) const {
66 return TRI.getHWRegChan(MI->getOperand(0).getReg());
69 /// \returns register to PV chan mapping for bundle/single instructions that
70 /// immediately precedes I.
71 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
73 DenseMap<unsigned, unsigned> Result;
75 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
77 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
83 int BISlot = getSlot(BI);
84 if (LastDstChan >= BISlot)
87 if (TII->isPredicated(BI))
89 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
90 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
92 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
96 unsigned Dst = BI->getOperand(DstIdx).getReg();
97 if (isTrans || TII->isTransOnly(BI)) {
98 Result[Dst] = AMDGPU::PS;
101 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
102 BI->getOpcode() == AMDGPU::DOT4_eg) {
103 Result[Dst] = AMDGPU::PV_X;
106 if (Dst == AMDGPU::OQAP) {
110 switch (TRI.getHWRegChan(Dst)) {
112 PVReg = AMDGPU::PV_X;
115 PVReg = AMDGPU::PV_Y;
118 PVReg = AMDGPU::PV_Z;
121 PVReg = AMDGPU::PV_W;
124 llvm_unreachable("Invalid Chan");
127 } while ((++BI)->isBundledWithPred());
131 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
134 AMDGPU::OpName::src0,
135 AMDGPU::OpName::src1,
138 for (unsigned i = 0; i < 3; i++) {
139 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
142 unsigned Src = MI->getOperand(OperandIdx).getReg();
143 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
145 MI->getOperand(OperandIdx).setReg(It->second);
150 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
151 MachineDominatorTree &MDT)
152 : VLIWPacketizerList(MF, MLI, MDT, true),
153 TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())),
154 TRI(TII->getRegisterInfo()) {
155 VLIW5 = !MF.getTarget().getSubtarget<AMDGPUSubtarget>().hasCaymanISA();
158 // initPacketizerState - initialize some internal flags.
159 void initPacketizerState() {
160 ConsideredInstUsesAlreadyWrittenVectorElement = false;
163 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
164 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
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))
173 if (!TII->isALUInstr(MI->getOpcode()))
175 if (MI->getOpcode() == AMDGPU::GROUP_BARRIER)
177 // XXX: This can be removed once the packetizer properly handles all the
178 // LDS instruction group restrictions.
179 if (TII->isLDSInstr(MI->getOpcode()))
184 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
186 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
187 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
188 if (getSlot(MII) == getSlot(MIJ))
189 ConsideredInstUsesAlreadyWrittenVectorElement = true;
190 // Does MII and MIJ share the same pred_sel ?
191 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
192 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
193 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
194 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
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)
202 if (Dep.getKind() == SDep::Anti)
204 if (Dep.getKind() == SDep::Output)
205 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
211 bool ARDef = TII->definesAddressRegister(MII) ||
212 TII->definesAddressRegister(MIJ);
213 bool ARUse = TII->usesAddressRegister(MII) ||
214 TII->usesAddressRegister(MIJ);
221 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
223 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
225 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
226 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
227 MI->getOperand(LastOp).setImm(Bit);
230 bool isBundlableWithCurrentPMI(MachineInstr *MI,
231 const DenseMap<unsigned, unsigned> &PV,
232 std::vector<R600InstrInfo::BankSwizzle> &BS,
234 isTransSlot = TII->isTransOnly(MI);
235 assert (!isTransSlot || VLIW5);
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) {
243 DEBUG(dbgs() << "Considering as Trans Inst :"; MI->dump(););
250 // Are the Constants limitations met ?
251 CurrentPacketMIs.push_back(MI);
252 if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) {
254 dbgs() << "Couldn't pack :\n";
256 dbgs() << "with the following packets :\n";
257 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
258 CurrentPacketMIs[i]->dump();
261 dbgs() << "because of Consts read limitations\n";
263 CurrentPacketMIs.pop_back();
267 // Is there a BankSwizzle set that meet Read Port limitations ?
268 if (!TII->fitsReadPortLimitations(CurrentPacketMIs,
269 PV, BS, isTransSlot)) {
271 dbgs() << "Couldn't pack :\n";
273 dbgs() << "with the following packets :\n";
274 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
275 CurrentPacketMIs[i]->dump();
278 dbgs() << "because of Read port limitations\n";
280 CurrentPacketMIs.pop_back();
284 // We cannot read LDS source registrs from the Trans slot.
285 if (isTransSlot && TII->readsLDSSrcReg(MI))
288 CurrentPacketMIs.pop_back();
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;
300 if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) {
301 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
302 MachineInstr *MI = CurrentPacketMIs[i];
303 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
304 AMDGPU::OpName::bank_swizzle);
305 MI->getOperand(Op).setImm(BS[i]);
307 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);
315 endPacket(std::next(It)->getParent(), std::next(It));
319 endPacket(MI->getParent(), MI);
320 if (TII->isTransOnly(MI))
322 return VLIWPacketizerList::addToPacket(MI);
326 bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
327 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
328 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
329 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
331 // Instantiate the packetizer.
332 R600PacketizerList Packetizer(Fn, MLI, MDT);
334 // DFA state table should not be empty.
335 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
338 // Loop over all basic blocks and remove KILL pseudo-instructions
339 // These instructions confuse the dependence analysis. Consider:
341 // R0 = KILL R0, D0 (Insn 1)
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
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();
352 if (MI->isKill() || MI->getOpcode() == AMDGPU::IMPLICIT_DEF ||
353 (MI->getOpcode() == AMDGPU::CF_ALU && !MI->getOperand(8).getImm())) {
354 MachineBasicBlock::iterator DeleteMI = MI;
356 MBB->erase(DeleteMI);
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) {
375 if (TII->isSchedulingBoundary(std::prev(I), MBB, Fn))
380 // Skip empty scheduling regions.
381 if (I == RegionEnd) {
382 RegionEnd = std::prev(RegionEnd);
386 // Skip regions with one instruction.
387 if (I == std::prev(RegionEnd)) {
388 RegionEnd = std::prev(RegionEnd);
392 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
401 } // end anonymous namespace
403 llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
404 return new R600Packetizer(tm);