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(), R600Operands::WRITE);
85 if (BI->getOperand(OperandIdx).getImm() == 0)
87 unsigned Dst = BI->getOperand(0).getReg();
88 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
89 BI->getOpcode() == AMDGPU::DOT4_eg) {
90 Result[Dst] = AMDGPU::PV_X;
94 switch (TRI.getHWRegChan(Dst)) {
102 PVReg = AMDGPU::PV_Z;
105 PVReg = AMDGPU::PV_W;
108 llvm_unreachable("Invalid Chan");
111 } while ((++BI)->isBundledWithPred());
115 void substitutePV(MachineInstr *MI, const DenseMap<unsigned, unsigned> &PVs)
117 R600Operands::Ops Ops[] = {
122 for (unsigned i = 0; i < 3; i++) {
123 int OperandIdx = TII->getOperandIdx(MI->getOpcode(), Ops[i]);
126 unsigned Src = MI->getOperand(OperandIdx).getReg();
127 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
129 MI->getOperand(OperandIdx).setReg(It->second);
134 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
135 MachineDominatorTree &MDT)
136 : VLIWPacketizerList(MF, MLI, MDT, true),
137 TII (static_cast<const R600InstrInfo *>(MF.getTarget().getInstrInfo())),
138 TRI(TII->getRegisterInfo()) { }
140 // initPacketizerState - initialize some internal flags.
141 void initPacketizerState() { }
143 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
144 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB) {
148 // isSoloInstruction - return true if instruction MI can not be packetized
149 // with any other instruction, which means that MI itself is a packet.
150 bool isSoloInstruction(MachineInstr *MI) {
151 if (TII->isVector(*MI))
153 if (!TII->isALUInstr(MI->getOpcode()))
155 if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::TRANS_ONLY)
157 if (TII->isTransOnly(MI))
162 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
164 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
165 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
166 if (getSlot(MII) <= getSlot(MIJ))
168 // Does MII and MIJ share the same pred_sel ?
169 int OpI = TII->getOperandIdx(MII->getOpcode(), R600Operands::PRED_SEL),
170 OpJ = TII->getOperandIdx(MIJ->getOpcode(), R600Operands::PRED_SEL);
171 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
172 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
175 if (SUJ->isSucc(SUI)) {
176 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
177 const SDep &Dep = SUJ->Succs[i];
178 if (Dep.getSUnit() != SUI)
180 if (Dep.getKind() == SDep::Anti)
182 if (Dep.getKind() == SDep::Output)
183 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
191 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
193 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {return false;}
195 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
196 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), R600Operands::LAST);
197 MI->getOperand(LastOp).setImm(Bit);
200 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
201 CurrentPacketMIs.push_back(MI);
202 bool FitsConstLimits = TII->canBundle(CurrentPacketMIs);
204 if (!FitsConstLimits) {
205 dbgs() << "Couldn't pack :\n";
207 dbgs() << "with the following packets :\n";
208 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
209 CurrentPacketMIs[i]->dump();
212 dbgs() << "because of Consts read limitations\n";
214 const DenseMap<unsigned, unsigned> &PV =
215 getPreviousVector(CurrentPacketMIs.front());
216 std::vector<R600InstrInfo::BankSwizzle> BS;
217 bool FitsReadPortLimits =
218 TII->fitsReadPortLimitations(CurrentPacketMIs, PV, BS);
220 if (!FitsReadPortLimits) {
221 dbgs() << "Couldn't pack :\n";
223 dbgs() << "with the following packets :\n";
224 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
225 CurrentPacketMIs[i]->dump();
228 dbgs() << "because of Read port limitations\n";
230 bool isBundlable = FitsConstLimits && FitsReadPortLimits;
232 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
233 MachineInstr *MI = CurrentPacketMIs[i];
234 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
235 R600Operands::BANK_SWIZZLE);
236 MI->getOperand(Op).setImm(BS[i]);
239 CurrentPacketMIs.pop_back();
241 endPacket(MI->getParent(), MI);
242 substitutePV(MI, getPreviousVector(MI));
243 return VLIWPacketizerList::addToPacket(MI);
245 if (!CurrentPacketMIs.empty())
246 setIsLastBit(CurrentPacketMIs.back(), 0);
247 substitutePV(MI, PV);
248 return VLIWPacketizerList::addToPacket(MI);
252 bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
253 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
254 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
255 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
257 // Instantiate the packetizer.
258 R600PacketizerList Packetizer(Fn, MLI, MDT);
260 // DFA state table should not be empty.
261 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
264 // Loop over all basic blocks and remove KILL pseudo-instructions
265 // These instructions confuse the dependence analysis. Consider:
267 // R0 = KILL R0, D0 (Insn 1)
269 // Here, Insn 1 will result in the dependence graph not emitting an output
270 // dependence between Insn 0 and Insn 2. This can lead to incorrect
273 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
274 MBB != MBBe; ++MBB) {
275 MachineBasicBlock::iterator End = MBB->end();
276 MachineBasicBlock::iterator MI = MBB->begin();
279 MachineBasicBlock::iterator DeleteMI = MI;
281 MBB->erase(DeleteMI);
289 // Loop over all of the basic blocks.
290 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
291 MBB != MBBe; ++MBB) {
292 // Find scheduling regions and schedule / packetize each region.
293 unsigned RemainingCount = MBB->size();
294 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
295 RegionEnd != MBB->begin();) {
296 // The next region starts above the previous region. Look backward in the
297 // instruction stream until we find the nearest boundary.
298 MachineBasicBlock::iterator I = RegionEnd;
299 for(;I != MBB->begin(); --I, --RemainingCount) {
300 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn))
305 // Skip empty scheduling regions.
306 if (I == RegionEnd) {
307 RegionEnd = llvm::prior(RegionEnd);
311 // Skip regions with one instruction.
312 if (I == llvm::prior(RegionEnd)) {
313 RegionEnd = llvm::prior(RegionEnd);
317 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
326 } // end anonymous namespace
328 llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
329 return new R600Packetizer(tm);