blob: c84866469ae8e2d5e9ae7d7ea7ab98f362b544a4 [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 {
Vincent Lejeune147700b2013-04-30 00:14:27 +000059private:
60 const R600InstrInfo *TII;
61 const R600RegisterInfo &TRI;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000062 bool VLIW5;
63 bool ConsideredInstUsesAlreadyWrittenVectorElement;
Vincent Lejeune147700b2013-04-30 00:14:27 +000064
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +000065 unsigned getSlot(const MachineInstr &MI) const {
66 return TRI.getHWRegChan(MI.getOperand(0).getReg());
Vincent Lejeune147700b2013-04-30 00:14:27 +000067 }
68
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000069 /// \returns register to PV chan mapping for bundle/single instructions that
Alp Tokercb402912014-01-24 17:20:08 +000070 /// immediately precedes I.
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000071 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
72 const {
73 DenseMap<unsigned, unsigned> Result;
Vincent Lejeune147700b2013-04-30 00:14:27 +000074 I--;
75 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
76 return Result;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +000077 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
Vincent Lejeune147700b2013-04-30 00:14:27 +000078 if (I->isBundle())
79 BI++;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000080 int LastDstChan = -1;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000081 do {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000082 bool isTrans = false;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +000083 int BISlot = getSlot(*BI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000084 if (LastDstChan >= BISlot)
85 isTrans = true;
86 LastDstChan = BISlot;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +000087 if (TII->isPredicated(*BI))
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000088 continue;
Tom Stellard02661d92013-06-25 21:22:18 +000089 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::write);
Vincent Lejeune91a942b2013-06-03 15:56:12 +000090 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000091 continue;
Tom Stellardce540332013-06-28 15:46:59 +000092 int DstIdx = TII->getOperandIdx(BI->getOpcode(), AMDGPU::OpName::dst);
93 if (DstIdx == -1) {
94 continue;
95 }
96 unsigned Dst = BI->getOperand(DstIdx).getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000097 if (isTrans || TII->isTransOnly(*BI)) {
Vincent Lejeune77a83522013-06-29 19:32:43 +000098 Result[Dst] = AMDGPU::PS;
99 continue;
100 }
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000101 if (BI->getOpcode() == AMDGPU::DOT4_r600 ||
102 BI->getOpcode() == AMDGPU::DOT4_eg) {
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000103 Result[Dst] = AMDGPU::PV_X;
104 continue;
105 }
Tom Stellardc026e8b2013-06-28 15:47:08 +0000106 if (Dst == AMDGPU::OQAP) {
107 continue;
108 }
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000109 unsigned PVReg = 0;
110 switch (TRI.getHWRegChan(Dst)) {
111 case 0:
112 PVReg = AMDGPU::PV_X;
113 break;
114 case 1:
115 PVReg = AMDGPU::PV_Y;
116 break;
117 case 2:
118 PVReg = AMDGPU::PV_Z;
119 break;
120 case 3:
121 PVReg = AMDGPU::PV_W;
122 break;
123 default:
124 llvm_unreachable("Invalid Chan");
125 }
126 Result[Dst] = PVReg;
127 } while ((++BI)->isBundledWithPred());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000128 return Result;
129 }
130
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000131 void substitutePV(MachineInstr &MI, const DenseMap<unsigned, unsigned> &PVs)
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000132 const {
Tom Stellard02661d92013-06-25 21:22:18 +0000133 unsigned Ops[] = {
134 AMDGPU::OpName::src0,
135 AMDGPU::OpName::src1,
136 AMDGPU::OpName::src2
Vincent Lejeune147700b2013-04-30 00:14:27 +0000137 };
138 for (unsigned i = 0; i < 3; i++) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000139 int OperandIdx = TII->getOperandIdx(MI.getOpcode(), Ops[i]);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000140 if (OperandIdx < 0)
141 continue;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000142 unsigned Src = MI.getOperand(OperandIdx).getReg();
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000143 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
144 if (It != PVs.end())
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000145 MI.getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000146 }
147 }
148public:
149 // Ctor.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000150 R600PacketizerList(MachineFunction &MF, const R600Subtarget &ST,
151 MachineLoopInfo &MLI)
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000152 : VLIWPacketizerList(MF, MLI, nullptr),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000153 TII(ST.getInstrInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000154 TRI(TII->getRegisterInfo()) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000155 VLIW5 = !ST.hasCaymanISA();
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000156 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000157
158 // initPacketizerState - initialize some internal flags.
Craig Topper5656db42014-04-29 07:57:24 +0000159 void initPacketizerState() override {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000160 ConsideredInstUsesAlreadyWrittenVectorElement = false;
161 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000162
163 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000164 bool ignorePseudoInstruction(const MachineInstr &MI,
Krzysztof Parzyszekd44a1fd2015-12-14 18:54:44 +0000165 const MachineBasicBlock *MBB) override {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000166 return false;
167 }
168
169 // isSoloInstruction - return true if instruction MI can not be packetized
170 // with any other instruction, which means that MI itself is a packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000171 bool isSoloInstruction(const MachineInstr &MI) override {
172 if (TII->isVector(MI))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000173 return true;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000174 if (!TII->isALUInstr(MI.getOpcode()))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000175 return true;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000176 if (MI.getOpcode() == AMDGPU::GROUP_BARRIER)
Tom Stellardce540332013-06-28 15:46:59 +0000177 return true;
Vincent Lejeune21de8ba2013-07-31 19:31:41 +0000178 // XXX: This can be removed once the packetizer properly handles all the
179 // LDS instruction group restrictions.
Matt Arsenault8226fc42016-03-02 23:00:21 +0000180 return TII->isLDSInstr(MI.getOpcode());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000181 }
182
183 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
184 // together.
Craig Topper5656db42014-04-29 07:57:24 +0000185 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) override {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000186 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000187 if (getSlot(*MII) == getSlot(*MIJ))
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000188 ConsideredInstUsesAlreadyWrittenVectorElement = true;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000189 // Does MII and MIJ share the same pred_sel ?
Tom Stellard02661d92013-06-25 21:22:18 +0000190 int OpI = TII->getOperandIdx(MII->getOpcode(), AMDGPU::OpName::pred_sel),
191 OpJ = TII->getOperandIdx(MIJ->getOpcode(), AMDGPU::OpName::pred_sel);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000192 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
193 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
194 if (PredI != PredJ)
195 return false;
196 if (SUJ->isSucc(SUI)) {
197 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
198 const SDep &Dep = SUJ->Succs[i];
199 if (Dep.getSUnit() != SUI)
200 continue;
201 if (Dep.getKind() == SDep::Anti)
202 continue;
203 if (Dep.getKind() == SDep::Output)
204 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
205 continue;
206 return false;
207 }
208 }
Tom Stellard26a3b672013-10-22 18:19:10 +0000209
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000210 bool ARDef =
211 TII->definesAddressRegister(*MII) || TII->definesAddressRegister(*MIJ);
212 bool ARUse =
213 TII->usesAddressRegister(*MII) || TII->usesAddressRegister(*MIJ);
Tom Stellard26a3b672013-10-22 18:19:10 +0000214
Matt Arsenault8226fc42016-03-02 23:00:21 +0000215 return !ARDef || !ARUse;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000216 }
217
218 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
219 // and SUJ.
Craig Topper5656db42014-04-29 07:57:24 +0000220 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) override {
221 return false;
222 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000223
224 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
Tom Stellard02661d92013-06-25 21:22:18 +0000225 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::last);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000226 MI->getOperand(LastOp).setImm(Bit);
227 }
228
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000229 bool isBundlableWithCurrentPMI(MachineInstr &MI,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000230 const DenseMap<unsigned, unsigned> &PV,
231 std::vector<R600InstrInfo::BankSwizzle> &BS,
232 bool &isTransSlot) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000233 isTransSlot = TII->isTransOnly(MI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000234 assert (!isTransSlot || VLIW5);
235
236 // Is the dst reg sequence legal ?
237 if (!isTransSlot && !CurrentPacketMIs.empty()) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000238 if (getSlot(MI) <= getSlot(*CurrentPacketMIs.back())) {
239 if (ConsideredInstUsesAlreadyWrittenVectorElement &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000240 !TII->isVectorOnly(MI) && VLIW5) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000241 isTransSlot = true;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000242 DEBUG({
243 dbgs() << "Considering as Trans Inst :";
244 MI.dump();
245 });
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000246 }
247 else
248 return false;
249 }
250 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000251
252 // Are the Constants limitations met ?
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000253 CurrentPacketMIs.push_back(&MI);
Vincent Lejeune77a83522013-06-29 19:32:43 +0000254 if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000255 DEBUG({
Vincent Lejeune147700b2013-04-30 00:14:27 +0000256 dbgs() << "Couldn't pack :\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000257 MI.dump();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000258 dbgs() << "with the following packets :\n";
259 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
260 CurrentPacketMIs[i]->dump();
261 dbgs() << "\n";
262 }
263 dbgs() << "because of Consts read limitations\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000264 });
Vincent Lejeune77a83522013-06-29 19:32:43 +0000265 CurrentPacketMIs.pop_back();
266 return false;
267 }
268
269 // Is there a BankSwizzle set that meet Read Port limitations ?
270 if (!TII->fitsReadPortLimitations(CurrentPacketMIs,
271 PV, BS, isTransSlot)) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000272 DEBUG({
Vincent Lejeune147700b2013-04-30 00:14:27 +0000273 dbgs() << "Couldn't pack :\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000274 MI.dump();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000275 dbgs() << "with the following packets :\n";
276 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
277 CurrentPacketMIs[i]->dump();
278 dbgs() << "\n";
279 }
280 dbgs() << "because of Read port limitations\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000281 });
Vincent Lejeune77a83522013-06-29 19:32:43 +0000282 CurrentPacketMIs.pop_back();
283 return false;
284 }
285
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000286 // We cannot read LDS source registrs from the Trans slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000287 if (isTransSlot && TII->readsLDSSrcReg(MI))
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000288 return false;
289
Vincent Lejeune77a83522013-06-29 19:32:43 +0000290 CurrentPacketMIs.pop_back();
291 return true;
292 }
293
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000294 MachineBasicBlock::iterator addToPacket(MachineInstr &MI) override {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000295 MachineBasicBlock::iterator FirstInBundle =
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000296 CurrentPacketMIs.empty() ? &MI : CurrentPacketMIs.front();
Vincent Lejeune77a83522013-06-29 19:32:43 +0000297 const DenseMap<unsigned, unsigned> &PV =
298 getPreviousVector(FirstInBundle);
299 std::vector<R600InstrInfo::BankSwizzle> BS;
300 bool isTransSlot;
301
302 if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000303 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
304 MachineInstr *MI = CurrentPacketMIs[i];
Vincent Lejeune77a83522013-06-29 19:32:43 +0000305 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
306 AMDGPU::OpName::bank_swizzle);
307 MI->getOperand(Op).setImm(BS[i]);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000308 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000309 unsigned Op =
310 TII->getOperandIdx(MI.getOpcode(), AMDGPU::OpName::bank_swizzle);
311 MI.getOperand(Op).setImm(BS.back());
Vincent Lejeune77a83522013-06-29 19:32:43 +0000312 if (!CurrentPacketMIs.empty())
313 setIsLastBit(CurrentPacketMIs.back(), 0);
314 substitutePV(MI, PV);
315 MachineBasicBlock::iterator It = VLIWPacketizerList::addToPacket(MI);
316 if (isTransSlot) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000317 endPacket(std::next(It)->getParent(), std::next(It));
Vincent Lejeune77a83522013-06-29 19:32:43 +0000318 }
319 return It;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000320 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000321 endPacket(MI.getParent(), MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000322 if (TII->isTransOnly(MI))
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000323 return MI;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000324 return VLIWPacketizerList::addToPacket(MI);
325 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000326};
327
328bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000329 const R600Subtarget &ST = Fn.getSubtarget<R600Subtarget>();
330 const R600InstrInfo *TII = ST.getInstrInfo();
331
Vincent Lejeune147700b2013-04-30 00:14:27 +0000332 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000333
334 // Instantiate the packetizer.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000335 R600PacketizerList Packetizer(Fn, ST, MLI);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000336
337 // DFA state table should not be empty.
338 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
339
Matt Arsenault8e001942016-06-02 18:37:16 +0000340 if (Packetizer.getResourceTracker()->getInstrItins()->isEmpty())
341 return false;
342
Vincent Lejeune147700b2013-04-30 00:14:27 +0000343 //
344 // Loop over all basic blocks and remove KILL pseudo-instructions
345 // These instructions confuse the dependence analysis. Consider:
346 // D0 = ... (Insn 0)
347 // R0 = KILL R0, D0 (Insn 1)
348 // R0 = ... (Insn 2)
349 // Here, Insn 1 will result in the dependence graph not emitting an output
350 // dependence between Insn 0 and Insn 2. This can lead to incorrect
351 // packetization
352 //
353 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
354 MBB != MBBe; ++MBB) {
355 MachineBasicBlock::iterator End = MBB->end();
356 MachineBasicBlock::iterator MI = MBB->begin();
357 while (MI != End) {
Tom Stellarded0ceec2013-10-10 17:11:12 +0000358 if (MI->isKill() || MI->getOpcode() == AMDGPU::IMPLICIT_DEF ||
Vincent Lejeunece499742013-07-09 15:03:33 +0000359 (MI->getOpcode() == AMDGPU::CF_ALU && !MI->getOperand(8).getImm())) {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000360 MachineBasicBlock::iterator DeleteMI = MI;
361 ++MI;
362 MBB->erase(DeleteMI);
363 End = MBB->end();
364 continue;
365 }
366 ++MI;
367 }
368 }
369
370 // Loop over all of the basic blocks.
371 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
372 MBB != MBBe; ++MBB) {
373 // Find scheduling regions and schedule / packetize each region.
374 unsigned RemainingCount = MBB->size();
375 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
376 RegionEnd != MBB->begin();) {
377 // The next region starts above the previous region. Look backward in the
378 // instruction stream until we find the nearest boundary.
379 MachineBasicBlock::iterator I = RegionEnd;
380 for(;I != MBB->begin(); --I, --RemainingCount) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000381 if (TII->isSchedulingBoundary(*std::prev(I), &*MBB, Fn))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000382 break;
383 }
384 I = MBB->begin();
385
386 // Skip empty scheduling regions.
387 if (I == RegionEnd) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000388 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000389 --RemainingCount;
390 continue;
391 }
392 // Skip regions with one instruction.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000393 if (I == std::prev(RegionEnd)) {
394 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000395 continue;
396 }
397
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000398 Packetizer.PacketizeMIs(&*MBB, &*I, RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000399 RegionEnd = I;
400 }
401 }
402
403 return true;
404
405}
406
Benjamin Kramerd78bb462013-05-23 17:10:37 +0000407} // end anonymous namespace
Vincent Lejeune147700b2013-04-30 00:14:27 +0000408
409llvm::FunctionPass *llvm::createR600Packetizer(TargetMachine &tm) {
410 return new R600Packetizer(tm);
411}