blob: 964c65cd9523307b2b51f5df3f6cd94553706230 [file] [log] [blame]
Vincent Lejeune147700b2013-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 Lejeune147700b2013-04-30 00:14:27 +000017#include "llvm/Support/Debug.h"
Vincent Lejeune147700b2013-04-30 00:14:27 +000018#include "AMDGPU.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000019#include "AMDGPUSubtarget.h"
Vincent Lejeune147700b2013-04-30 00:14:27 +000020#include "R600InstrInfo.h"
Benjamin Kramerd78bb462013-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 Lejeune147700b2013-04-30 00:14:27 +000028
Benjamin Kramerd78bb462013-05-23 17:10:37 +000029using namespace llvm;
30
Chandler Carruth84e68b22014-04-22 02:41:26 +000031#define DEBUG_TYPE "packets"
32
Benjamin Kramerd78bb462013-05-23 17:10:37 +000033namespace {
Vincent Lejeune147700b2013-04-30 00:14:27 +000034
35class R600Packetizer : public MachineFunctionPass {
36
37public:
38 static char ID;
39 R600Packetizer(const TargetMachine &TM) : MachineFunctionPass(ID) {}
40
Craig Topper5656db42014-04-29 07:57:24 +000041 void getAnalysisUsage(AnalysisUsage &AU) const override {
Vincent Lejeune147700b2013-04-30 00:14:27 +000042 AU.setPreservesCFG();
43 AU.addRequired<MachineDominatorTree>();
44 AU.addPreserved<MachineDominatorTree>();
45 AU.addRequired<MachineLoopInfo>();
46 AU.addPreserved<MachineLoopInfo>();
47 MachineFunctionPass::getAnalysisUsage(AU);
48 }
49
Craig Topper5656db42014-04-29 07:57:24 +000050 const char *getPassName() const override {
Vincent Lejeune147700b2013-04-30 00:14:27 +000051 return "R600 Packetizer";
52 }
53
Craig Topper5656db42014-04-29 07:57:24 +000054 bool runOnMachineFunction(MachineFunction &Fn) override;
Vincent Lejeune147700b2013-04-30 00:14:27 +000055};
56char R600Packetizer::ID = 0;
57
58class R600PacketizerList : public VLIWPacketizerList {
59
60private:
61 const R600InstrInfo *TII;
62 const R600RegisterInfo &TRI;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000063 bool VLIW5;
64 bool ConsideredInstUsesAlreadyWrittenVectorElement;
Vincent Lejeune147700b2013-04-30 00:14:27 +000065
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +000066 unsigned getSlot(const MachineInstr &MI) const {
67 return TRI.getHWRegChan(MI.getOperand(0).getReg());
Vincent Lejeune147700b2013-04-30 00:14:27 +000068 }
69
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000070 /// \returns register to PV chan mapping for bundle/single instructions that
Alp Tokercb402912014-01-24 17:20:08 +000071 /// immediately precedes I.
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000072 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
73 const {
74 DenseMap<unsigned, unsigned> Result;
Vincent Lejeune147700b2013-04-30 00:14:27 +000075 I--;
76 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
77 return Result;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +000078 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
Vincent Lejeune147700b2013-04-30 00:14:27 +000079 if (I->isBundle())
80 BI++;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000081 int LastDstChan = -1;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000082 do {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000083 bool isTrans = false;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +000084 int BISlot = getSlot(*BI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000085 if (LastDstChan >= BISlot)
86 isTrans = true;
87 LastDstChan = BISlot;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +000088 if (TII->isPredicated(*BI))
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000089 continue;
Tom Stellard02661d92013-06-25 21:22:18 +000090 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
Vincent Lejeune91a942b2013-06-03 15:56:12 +000091 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000092 continue;
Tom Stellardce540332013-06-28 15:46:59 +000093 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
94 if (DstIdx == -1) {
95 continue;
96 }
97 unsigned Dst = BI->getOperand(DstIdx).getReg();
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +000098 if (isTrans || TII->isTransOnly(&*BI)) {
Vincent Lejeune77a83522013-06-29 19:32:43 +000099 Result[Dst] = AMDGPU::PS;
100 continue;
101 }
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000102 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
103 BI->getOpcode() == AMDGPU::DOT4_eg) {
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000104 Result[Dst] = AMDGPU::PV_X;
105 continue;
106 }
Tom Stellardc026e8b2013-06-28 15:47:08 +0000107 if (Dst == AMDGPU::OQAP) {
108 continue;
109 }
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000110 unsigned PVReg = 0;
111 switch (TRI.getHWRegChan(Dst)) {
112 case 0:
113 PVReg = AMDGPU::PV_X;
114 break;
115 case 1:
116 PVReg = AMDGPU::PV_Y;
117 break;
118 case 2:
119 PVReg = AMDGPU::PV_Z;
120 break;
121 case 3:
122 PVReg = AMDGPU::PV_W;
123 break;
124 default:
125 llvm_unreachable("Invalid Chan");
126 }
127 Result[Dst] = PVReg;
128 } while ((++BI)->isBundledWithPred());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000129 return Result;
130 }
131
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000132 void substitutePV(MachineInstr &MI, const DenseMap<unsigned, unsigned> &PVs)
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000133 const {
Tom Stellard02661d92013-06-25 21:22:18 +0000134 unsigned Ops[] = {
135 AMDGPU::OpName::src0,
136 AMDGPU::OpName::src1,
137 AMDGPU::OpName::src2
Vincent Lejeune147700b2013-04-30 00:14:27 +0000138 };
139 for (unsigned i = 0; i < 3; i++) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000140 int OperandIdx = TII->getOperandIdx(MI.getOpcode(), Ops[i]);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000141 if (OperandIdx < 0)
142 continue;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000143 unsigned Src = MI.getOperand(OperandIdx).getReg();
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000144 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
145 if (It != PVs.end())
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000146 MI.getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000147 }
148 }
149public:
150 // Ctor.
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000151 R600PacketizerList(MachineFunction &MF, MachineLoopInfo &MLI)
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000152 : VLIWPacketizerList(MF, MLI, nullptr),
153 TII(static_cast<const R600InstrInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000154 MF.getSubtarget().getInstrInfo())),
Eric Christopherd9134482014-08-04 21:25:23 +0000155 TRI(TII->getRegisterInfo()) {
Eric Christopher7792e322015-01-30 23:24:40 +0000156 VLIW5 = !MF.getSubtarget<AMDGPUSubtarget>().hasCaymanISA();
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000157 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000158
159 // initPacketizerState - initialize some internal flags.
Craig Topper5656db42014-04-29 07:57:24 +0000160 void initPacketizerState() override {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000161 ConsideredInstUsesAlreadyWrittenVectorElement = false;
162 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000163
164 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000165 bool ignorePseudoInstruction(const MachineInstr &MI,
Krzysztof Parzyszekd44a1fd2015-12-14 18:54:44 +0000166 const MachineBasicBlock *MBB) override {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000167 return false;
168 }
169
170 // isSoloInstruction - return true if instruction MI can not be packetized
171 // with any other instruction, which means that MI itself is a packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000172 bool isSoloInstruction(const MachineInstr &MI) override {
173 if (TII->isVector(MI))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000174 return true;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000175 if (!TII->isALUInstr(MI.getOpcode()))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000176 return true;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000177 if (MI.getOpcode() == AMDGPU::GROUP_BARRIER)
Tom Stellardce540332013-06-28 15:46:59 +0000178 return true;
Vincent Lejeune21de8ba2013-07-31 19:31:41 +0000179 // XXX: This can be removed once the packetizer properly handles all the
180 // LDS instruction group restrictions.
Matt Arsenault8226fc42016-03-02 23:00:21 +0000181 return TII->isLDSInstr(MI.getOpcode());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000182 }
183
184 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
185 // together.
Craig Topper5656db42014-04-29 07:57:24 +0000186 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) override {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000187 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000188 if (getSlot(*MII) == getSlot(*MIJ))
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000189 ConsideredInstUsesAlreadyWrittenVectorElement = true;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000190 // Does MII and MIJ share the same pred_sel ?
Tom Stellard02661d92013-06-25 21:22:18 +0000191 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
192 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000193 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
194 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
195 if (PredI != PredJ)
196 return false;
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)
201 continue;
202 if (Dep.getKind() == SDep::Anti)
203 continue;
204 if (Dep.getKind() == SDep::Output)
205 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
206 continue;
207 return false;
208 }
209 }
Tom Stellard26a3b672013-10-22 18:19:10 +0000210
211 bool ARDef = TII->definesAddressRegister(MII) ||
212 TII->definesAddressRegister(MIJ);
213 bool ARUse = TII->usesAddressRegister(MII) ||
214 TII->usesAddressRegister(MIJ);
Tom Stellard26a3b672013-10-22 18:19:10 +0000215
Matt Arsenault8226fc42016-03-02 23:00:21 +0000216 return !ARDef || !ARUse;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000217 }
218
219 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
220 // and SUJ.
Craig Topper5656db42014-04-29 07:57:24 +0000221 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) override {
222 return false;
223 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000224
225 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
Tom Stellard02661d92013-06-25 21:22:18 +0000226 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000227 MI->getOperand(LastOp).setImm(Bit);
228 }
229
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000230 bool isBundlableWithCurrentPMI(MachineInstr &MI,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000231 const DenseMap<unsigned, unsigned> &PV,
232 std::vector<R600InstrInfo::BankSwizzle> &BS,
233 bool &isTransSlot) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000234 isTransSlot = TII->isTransOnly(&MI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000235 assert (!isTransSlot || VLIW5);
236
237 // Is the dst reg sequence legal ?
238 if (!isTransSlot && !CurrentPacketMIs.empty()) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000239 if (getSlot(MI) <= getSlot(*CurrentPacketMIs.back())) {
240 if (ConsideredInstUsesAlreadyWrittenVectorElement &&
241 !TII->isVectorOnly(&MI) && VLIW5) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000242 isTransSlot = true;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000243 DEBUG({
244 dbgs() << "Considering as Trans Inst :";
245 MI.dump();
246 });
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000247 }
248 else
249 return false;
250 }
251 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000252
253 // Are the Constants limitations met ?
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000254 CurrentPacketMIs.push_back(&MI);
Vincent Lejeune77a83522013-06-29 19:32:43 +0000255 if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000256 DEBUG({
Vincent Lejeune147700b2013-04-30 00:14:27 +0000257 dbgs() << "Couldn't pack :\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000258 MI.dump();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000259 dbgs() << "with the following packets :\n";
260 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
261 CurrentPacketMIs[i]->dump();
262 dbgs() << "\n";
263 }
264 dbgs() << "because of Consts read limitations\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000265 });
Vincent Lejeune77a83522013-06-29 19:32:43 +0000266 CurrentPacketMIs.pop_back();
267 return false;
268 }
269
270 // Is there a BankSwizzle set that meet Read Port limitations ?
271 if (!TII->fitsReadPortLimitations(CurrentPacketMIs,
272 PV, BS, isTransSlot)) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000273 DEBUG({
Vincent Lejeune147700b2013-04-30 00:14:27 +0000274 dbgs() << "Couldn't pack :\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000275 MI.dump();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000276 dbgs() << "with the following packets :\n";
277 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
278 CurrentPacketMIs[i]->dump();
279 dbgs() << "\n";
280 }
281 dbgs() << "because of Read port limitations\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000282 });
Vincent Lejeune77a83522013-06-29 19:32:43 +0000283 CurrentPacketMIs.pop_back();
284 return false;
285 }
286
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000287 // We cannot read LDS source registrs from the Trans slot.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000288 if (isTransSlot && TII->readsLDSSrcReg(&MI))
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000289 return false;
290
Vincent Lejeune77a83522013-06-29 19:32:43 +0000291 CurrentPacketMIs.pop_back();
292 return true;
293 }
294
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000295 MachineBasicBlock::iterator addToPacket(MachineInstr &MI) override {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000296 MachineBasicBlock::iterator FirstInBundle =
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000297 CurrentPacketMIs.empty() ? &MI : CurrentPacketMIs.front();
Vincent Lejeune77a83522013-06-29 19:32:43 +0000298 const DenseMap<unsigned, unsigned> &PV =
299 getPreviousVector(FirstInBundle);
300 std::vector<R600InstrInfo::BankSwizzle> BS;
301 bool isTransSlot;
302
303 if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000304 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
305 MachineInstr *MI = CurrentPacketMIs[i];
Vincent Lejeune77a83522013-06-29 19:32:43 +0000306 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
307 AMDGPU::OpName::bank_swizzle);
308 MI->getOperand(Op).setImm(BS[i]);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000309 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000310 unsigned Op =
311 TII->getOperandIdx(MI.getOpcode(), AMDGPU::OpName::bank_swizzle);
312 MI.getOperand(Op).setImm(BS.back());
Vincent Lejeune77a83522013-06-29 19:32:43 +0000313 if (!CurrentPacketMIs.empty())
314 setIsLastBit(CurrentPacketMIs.back(), 0);
315 substitutePV(MI, PV);
316 MachineBasicBlock::iterator It = VLIWPacketizerList::addToPacket(MI);
317 if (isTransSlot) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000318 endPacket(std::next(It)->getParent(), std::next(It));
Vincent Lejeune77a83522013-06-29 19:32:43 +0000319 }
320 return It;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000321 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000322 endPacket(MI.getParent(), MI);
323 if (TII->isTransOnly(&MI))
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000324 return MI;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000325 return VLIWPacketizerList::addToPacket(MI);
326 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000327};
328
329bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000330 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000331 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000332
333 // Instantiate the packetizer.
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000334 R600PacketizerList Packetizer(Fn, MLI);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000335
336 // DFA state table should not be empty.
337 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
338
339 //
340 // Loop over all basic blocks and remove KILL pseudo-instructions
341 // These instructions confuse the dependence analysis. Consider:
342 // D0 = ... (Insn 0)
343 // R0 = KILL R0, D0 (Insn 1)
344 // R0 = ... (Insn 2)
345 // Here, Insn 1 will result in the dependence graph not emitting an output
346 // dependence between Insn 0 and Insn 2. This can lead to incorrect
347 // packetization
348 //
349 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
350 MBB != MBBe; ++MBB) {
351 MachineBasicBlock::iterator End = MBB->end();
352 MachineBasicBlock::iterator MI = MBB->begin();
353 while (MI != End) {
Tom Stellarded0ceec2013-10-10 17:11:12 +0000354 if (MI->isKill() || MI->getOpcode() == AMDGPU::IMPLICIT_DEF ||
Vincent Lejeunece499742013-07-09 15:03:33 +0000355 (MI->getOpcode() == AMDGPU::CF_ALU && !MI->getOperand(8).getImm())) {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000356 MachineBasicBlock::iterator DeleteMI = MI;
357 ++MI;
358 MBB->erase(DeleteMI);
359 End = MBB->end();
360 continue;
361 }
362 ++MI;
363 }
364 }
365
366 // Loop over all of the basic blocks.
367 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
368 MBB != MBBe; ++MBB) {
369 // Find scheduling regions and schedule / packetize each region.
370 unsigned RemainingCount = MBB->size();
371 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
372 RegionEnd != MBB->begin();) {
373 // The next region starts above the previous region. Look backward in the
374 // instruction stream until we find the nearest boundary.
375 MachineBasicBlock::iterator I = RegionEnd;
376 for(;I != MBB->begin(); --I, --RemainingCount) {
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000377 if (TII->isSchedulingBoundary(&*std::prev(I), &*MBB, Fn))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000378 break;
379 }
380 I = MBB->begin();
381
382 // Skip empty scheduling regions.
383 if (I == RegionEnd) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000384 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000385 --RemainingCount;
386 continue;
387 }
388 // Skip regions with one instruction.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000389 if (I == std::prev(RegionEnd)) {
390 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000391 continue;
392 }
393
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000394 Packetizer.PacketizeMIs(&*MBB, &*I, RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000395 RegionEnd = I;
396 }
397 }
398
399 return true;
400
401}
402
Benjamin Kramerd78bb462013-05-23 17:10:37 +0000403} // end anonymous namespace
Vincent Lejeune147700b2013-04-30 00:14:27 +0000404
405llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
406 return new R600Packetizer(tm);
407}