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 #ifndef R600PACKETIZER_CPP
18 #define R600PACKETIZER_CPP
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"
30 #include "R600InstrInfo.h"
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 unsigned getSlot(const MachineInstr *MI) const {
64 return TRI.getHWRegChan(MI->getOperand(0).getReg());
67 /// \returns register to PV chan mapping for bundle/single instructions that
68 /// immediatly precedes I.
69 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
71 DenseMap<unsigned, unsigned> Result;
73 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
75 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
79 if (TII->isPredicated(BI))
81 if (TII->isTransOnly(BI))
83 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), R600Operands::WRITE);
86 if (BI->getOperand(OperandIdx).getImm() == 0)
88 unsigned Dst = BI->getOperand(0).getReg();
89 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
90 BI->getOpcode() == AMDGPU::DOT4_eg) {
91 Result[Dst] = AMDGPU::PV_X;
95 switch (TRI.getHWRegChan(Dst)) {
100 PVReg = AMDGPU::PV_Y;
103 PVReg = AMDGPU::PV_Z;
106 PVReg = AMDGPU::PV_W;
109 llvm_unreachable("Invalid Chan");
112 } while ((++BI)->isBundledWithPred());
116 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
118 R600Operands::Ops Ops[] = {
123 for (unsigned i = 0; i < 3; i++) {
124 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
127 unsigned Src = MI->getOperand(OperandIdx).getReg();
128 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
130 MI->getOperand(OperandIdx).setReg(It->second);
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()) { }
141 // initPacketizerState - initialize some internal flags.
142 void initPacketizerState() { }
144 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
145 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
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))
154 if (!TII->isALUInstr(MI->getOpcode()))
156 if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::TRANS_ONLY)
158 if (TII->isTransOnly(MI))
163 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
165 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
166 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
167 if (getSlot(MII) <= getSlot(MIJ))
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;
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)
181 if (Dep.getKind() == SDep::Anti)
183 if (Dep.getKind() == SDep::Output)
184 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
192 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
194 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
196 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
197 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), R600Operands::LAST);
198 MI->getOperand(LastOp).setImm(Bit);
201 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
202 CurrentPacketMIs.push_back(MI);
203 bool FitsConstLimits = TII->canBundle(CurrentPacketMIs);
205 if (!FitsConstLimits) {
206 dbgs() << "Couldn't pack :\n";
208 dbgs() << "with the following packets :\n";
209 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
210 CurrentPacketMIs[i]->dump();
213 dbgs() << "because of Consts read limitations\n";
215 const DenseMap<unsigned, unsigned> &PV =
216 getPreviousVector(CurrentPacketMIs.front());
217 std::vector<R600InstrInfo::BankSwizzle> BS;
218 bool FitsReadPortLimits =
219 TII->fitsReadPortLimitations(CurrentPacketMIs, PV, BS);
221 if (!FitsReadPortLimits) {
222 dbgs() << "Couldn't pack :\n";
224 dbgs() << "with the following packets :\n";
225 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
226 CurrentPacketMIs[i]->dump();
229 dbgs() << "because of Read port limitations\n";
231 bool isBundlable = FitsConstLimits && FitsReadPortLimits;
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]);
240 CurrentPacketMIs.pop_back();
242 endPacket(MI->getParent(), MI);
243 substitutePV(MI, getPreviousVector(MI));
244 return VLIWPacketizerList::addToPacket(MI);
246 if (!CurrentPacketMIs.empty())
247 setIsLastBit(CurrentPacketMIs.back(), 0);
248 substitutePV(MI, PV);
249 return VLIWPacketizerList::addToPacket(MI);
253 bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
254 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
255 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
256 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
258 // Instantiate the packetizer.
259 R600PacketizerList Packetizer(Fn, MLI, MDT);
261 // DFA state table should not be empty.
262 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
265 // Loop over all basic blocks and remove KILL pseudo-instructions
266 // These instructions confuse the dependence analysis. Consider:
268 // R0 = KILL R0, D0 (Insn 1)
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
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();
280 MachineBasicBlock::iterator DeleteMI = MI;
282 MBB->erase(DeleteMI);
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))
306 // Skip empty scheduling regions.
307 if (I == RegionEnd) {
308 RegionEnd = llvm::prior(RegionEnd);
312 // Skip regions with one instruction.
313 if (I == llvm::prior(RegionEnd)) {
314 RegionEnd = llvm::prior(RegionEnd);
318 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
329 llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
330 return new R600Packetizer(tm);
333 #endif // R600PACKETIZER_CPP