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 #define DEBUG_TYPE "packets"
18 #include "llvm/Support/Debug.h"
20 #include "R600InstrInfo.h"
21 #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"
33 class R600Packetizer : public MachineFunctionPass {
37 R600Packetizer(const TargetMachine &TM) : MachineFunctionPass(ID) {}
39 void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.addRequired<MachineDominatorTree>();
42 AU.addPreserved<MachineDominatorTree>();
43 AU.addRequired<MachineLoopInfo>();
44 AU.addPreserved<MachineLoopInfo>();
45 MachineFunctionPass::getAnalysisUsage(AU);
48 const char *getPassName() const {
49 return "R600 Packetizer";
52 bool runOnMachineFunction(MachineFunction &Fn);
54 char R600Packetizer::ID = 0;
56 class R600PacketizerList : public VLIWPacketizerList {
59 const R600InstrInfo *TII;
60 const R600RegisterInfo &TRI;
62 unsigned getSlot(const MachineInstr *MI) const {
63 return TRI.getHWRegChan(MI->getOperand(0).getReg());
66 /// \returns register to PV chan mapping for bundle/single instructions that
67 /// immediatly precedes I.
68 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
70 DenseMap<unsigned, unsigned> Result;
72 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
74 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
78 if (TII->isPredicated(BI))
80 if (TII->isTransOnly(BI))
82 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
83 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
85 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
89 unsigned Dst = BI->getOperand(DstIdx).getReg();
90 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
91 BI->getOpcode() == AMDGPU::DOT4_eg) {
92 Result[Dst] = AMDGPU::PV_X;
96 switch (TRI.getHWRegChan(Dst)) {
101 PVReg = AMDGPU::PV_Y;
104 PVReg = AMDGPU::PV_Z;
107 PVReg = AMDGPU::PV_W;
110 llvm_unreachable("Invalid Chan");
113 } while ((++BI)->isBundledWithPred());
117 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
120 AMDGPU::OpName::src0,
121 AMDGPU::OpName::src1,
124 for (unsigned i = 0; i < 3; i++) {
125 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
128 unsigned Src = MI->getOperand(OperandIdx).getReg();
129 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
131 MI->getOperand(OperandIdx).setReg(It->second);
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()) { }
142 // initPacketizerState - initialize some internal flags.
143 void initPacketizerState() { }
145 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
146 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
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))
155 if (!TII->isALUInstr(MI->getOpcode()))
157 if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::TRANS_ONLY)
159 if (TII->isTransOnly(MI))
161 if (MI->getOpcode() == AMDGPU::GROUP_BARRIER)
166 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
168 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
169 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
170 if (getSlot(MII) <= getSlot(MIJ))
172 // Does MII and MIJ share the same pred_sel ?
173 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
174 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
175 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
176 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
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)
184 if (Dep.getKind() == SDep::Anti)
186 if (Dep.getKind() == SDep::Output)
187 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
195 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
197 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
199 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
200 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
201 MI->getOperand(LastOp).setImm(Bit);
204 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
205 CurrentPacketMIs.push_back(MI);
206 bool FitsConstLimits = TII->canBundle(CurrentPacketMIs);
208 if (!FitsConstLimits) {
209 dbgs() << "Couldn't pack :\n";
211 dbgs() << "with the following packets :\n";
212 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
213 CurrentPacketMIs[i]->dump();
216 dbgs() << "because of Consts read limitations\n";
218 const DenseMap<unsigned, unsigned> &PV =
219 getPreviousVector(CurrentPacketMIs.front());
220 std::vector<R600InstrInfo::BankSwizzle> BS;
221 bool FitsReadPortLimits =
222 TII->fitsReadPortLimitations(CurrentPacketMIs, PV, BS);
224 if (!FitsReadPortLimits) {
225 dbgs() << "Couldn't pack :\n";
227 dbgs() << "with the following packets :\n";
228 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
229 CurrentPacketMIs[i]->dump();
232 dbgs() << "because of Read port limitations\n";
234 bool isBundlable = FitsConstLimits && FitsReadPortLimits;
236 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
237 MachineInstr *MI = CurrentPacketMIs[i];
238 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
239 AMDGPU::OpName::bank_swizzle);
240 MI->getOperand(Op).setImm(BS[i]);
243 CurrentPacketMIs.pop_back();
245 endPacket(MI->getParent(), MI);
246 substitutePV(MI, getPreviousVector(MI));
247 return VLIWPacketizerList::addToPacket(MI);
249 if (!CurrentPacketMIs.empty())
250 setIsLastBit(CurrentPacketMIs.back(), 0);
251 substitutePV(MI, PV);
252 return VLIWPacketizerList::addToPacket(MI);
256 bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
257 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
258 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
259 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
261 // Instantiate the packetizer.
262 R600PacketizerList Packetizer(Fn, MLI, MDT);
264 // DFA state table should not be empty.
265 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
268 // Loop over all basic blocks and remove KILL pseudo-instructions
269 // These instructions confuse the dependence analysis. Consider:
271 // R0 = KILL R0, D0 (Insn 1)
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
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();
283 MachineBasicBlock::iterator DeleteMI = MI;
285 MBB->erase(DeleteMI);
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))
309 // Skip empty scheduling regions.
310 if (I == RegionEnd) {
311 RegionEnd = llvm::prior(RegionEnd);
315 // Skip regions with one instruction.
316 if (I == llvm::prior(RegionEnd)) {
317 RegionEnd = llvm::prior(RegionEnd);
321 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
330 } // end anonymous namespace
332 llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
333 return new R600Packetizer(tm);