blob: 768a4cfc38b6875b997b5e8e4a2abb648d0e28fd [file] [log] [blame]
Vincent Lejeune147700b2013-04-30 00:14:27 +00001//===----- R600Packetizer.cpp - VLIW packetizer ---------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Vincent Lejeune147700b2013-04-30 00:14:27 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// This pass implements instructions packetization for R600. It unsets isLast
11/// bit of instructions inside a bundle and substitutes src register with
12/// PreviousVector when applicable.
13//
14//===----------------------------------------------------------------------===//
15
Vincent Lejeune147700b2013-04-30 00:14:27 +000016#include "AMDGPU.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000017#include "AMDGPUSubtarget.h"
Vincent Lejeune147700b2013-04-30 00:14:27 +000018#include "R600InstrInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000019#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000020#include "llvm/CodeGen/DFAPacketizer.h"
21#include "llvm/CodeGen/MachineDominators.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineLoopInfo.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/ScheduleDAG.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000026#include "llvm/Support/Debug.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000027#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;
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +000039 R600Packetizer() : MachineFunctionPass(ID) {}
Vincent Lejeune147700b2013-04-30 00:14:27 +000040
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
Mehdi Amini117296c2016-10-01 02:56:57 +000050 StringRef getPassName() const override { return "R600 Packetizer"; }
Vincent Lejeune147700b2013-04-30 00:14:27 +000051
Craig Topper5656db42014-04-29 07:57:24 +000052 bool runOnMachineFunction(MachineFunction &Fn) override;
Vincent Lejeune147700b2013-04-30 00:14:27 +000053};
Vincent Lejeune147700b2013-04-30 00:14:27 +000054
55class R600PacketizerList : public VLIWPacketizerList {
Vincent Lejeune147700b2013-04-30 00:14:27 +000056private:
57 const R600InstrInfo *TII;
58 const R600RegisterInfo &TRI;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000059 bool VLIW5;
60 bool ConsideredInstUsesAlreadyWrittenVectorElement;
Vincent Lejeune147700b2013-04-30 00:14:27 +000061
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +000062 unsigned getSlot(const MachineInstr &MI) const {
63 return TRI.getHWRegChan(MI.getOperand(0).getReg());
Vincent Lejeune147700b2013-04-30 00:14:27 +000064 }
65
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000066 /// \returns register to PV chan mapping for bundle/single instructions that
Alp Tokercb402912014-01-24 17:20:08 +000067 /// immediately precedes I.
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000068 DenseMap<unsigned, unsigned> getPreviousVector(MachineBasicBlock::iterator I)
69 const {
70 DenseMap<unsigned, unsigned> Result;
Vincent Lejeune147700b2013-04-30 00:14:27 +000071 I--;
72 if (!TII->isALUInstr(I->getOpcode()) && !I->isBundle())
73 return Result;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +000074 MachineBasicBlock::instr_iterator BI = I.getInstrIterator();
Vincent Lejeune147700b2013-04-30 00:14:27 +000075 if (I->isBundle())
76 BI++;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000077 int LastDstChan = -1;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000078 do {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000079 bool isTrans = false;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +000080 int BISlot = getSlot(*BI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000081 if (LastDstChan >= BISlot)
82 isTrans = true;
83 LastDstChan = BISlot;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +000084 if (TII->isPredicated(*BI))
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000085 continue;
Tom Stellardc5a154d2018-06-28 23:47:12 +000086 int OperandIdx = TII->getOperandIdx(BI->getOpcode(), R600::OpName::write);
Vincent Lejeune91a942b2013-06-03 15:56:12 +000087 if (OperandIdx > -1 && BI->getOperand(OperandIdx).getImm() == 0)
Vincent Lejeune2a44ae02013-05-02 21:52:55 +000088 continue;
Tom Stellardc5a154d2018-06-28 23:47:12 +000089 int DstIdx = TII->getOperandIdx(BI->getOpcode(), R600::OpName::dst);
Tom Stellardce540332013-06-28 15:46:59 +000090 if (DstIdx == -1) {
91 continue;
92 }
93 unsigned Dst = BI->getOperand(DstIdx).getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000094 if (isTrans || TII->isTransOnly(*BI)) {
Tom Stellardc5a154d2018-06-28 23:47:12 +000095 Result[Dst] = R600::PS;
Vincent Lejeune77a83522013-06-29 19:32:43 +000096 continue;
97 }
Tom Stellardc5a154d2018-06-28 23:47:12 +000098 if (BI->getOpcode() == R600::DOT4_r600 ||
99 BI->getOpcode() == R600::DOT4_eg) {
100 Result[Dst] = R600::PV_X;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000101 continue;
102 }
Tom Stellardc5a154d2018-06-28 23:47:12 +0000103 if (Dst == R600::OQAP) {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000104 continue;
105 }
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000106 unsigned PVReg = 0;
107 switch (TRI.getHWRegChan(Dst)) {
108 case 0:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000109 PVReg = R600::PV_X;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000110 break;
111 case 1:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000112 PVReg = R600::PV_Y;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000113 break;
114 case 2:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000115 PVReg = R600::PV_Z;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000116 break;
117 case 3:
Tom Stellardc5a154d2018-06-28 23:47:12 +0000118 PVReg = R600::PV_W;
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000119 break;
120 default:
121 llvm_unreachable("Invalid Chan");
122 }
123 Result[Dst] = PVReg;
124 } while ((++BI)->isBundledWithPred());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000125 return Result;
126 }
127
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000128 void substitutePV(MachineInstr &MI, const DenseMap<unsigned, unsigned> &PVs)
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000129 const {
Tom Stellard02661d92013-06-25 21:22:18 +0000130 unsigned Ops[] = {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000131 R600::OpName::src0,
132 R600::OpName::src1,
133 R600::OpName::src2
Vincent Lejeune147700b2013-04-30 00:14:27 +0000134 };
135 for (unsigned i = 0; i < 3; i++) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000136 int OperandIdx = TII->getOperandIdx(MI.getOpcode(), Ops[i]);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000137 if (OperandIdx < 0)
138 continue;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000139 unsigned Src = MI.getOperand(OperandIdx).getReg();
Vincent Lejeune2a44ae02013-05-02 21:52:55 +0000140 const DenseMap<unsigned, unsigned>::const_iterator It = PVs.find(Src);
141 if (It != PVs.end())
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000142 MI.getOperand(OperandIdx).setReg(It->second);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000143 }
144 }
145public:
146 // Ctor.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000147 R600PacketizerList(MachineFunction &MF, const R600Subtarget &ST,
148 MachineLoopInfo &MLI)
Krzysztof Parzyszekdac71022015-12-14 20:35:13 +0000149 : VLIWPacketizerList(MF, MLI, nullptr),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000150 TII(ST.getInstrInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +0000151 TRI(TII->getRegisterInfo()) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000152 VLIW5 = !ST.hasCaymanISA();
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000153 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000154
155 // initPacketizerState - initialize some internal flags.
Craig Topper5656db42014-04-29 07:57:24 +0000156 void initPacketizerState() override {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000157 ConsideredInstUsesAlreadyWrittenVectorElement = false;
158 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000159
160 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000161 bool ignorePseudoInstruction(const MachineInstr &MI,
Krzysztof Parzyszekd44a1fd2015-12-14 18:54:44 +0000162 const MachineBasicBlock *MBB) override {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000163 return false;
164 }
165
166 // isSoloInstruction - return true if instruction MI can not be packetized
167 // with any other instruction, which means that MI itself is a packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000168 bool isSoloInstruction(const MachineInstr &MI) override {
169 if (TII->isVector(MI))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000170 return true;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000171 if (!TII->isALUInstr(MI.getOpcode()))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000172 return true;
Tom Stellardc5a154d2018-06-28 23:47:12 +0000173 if (MI.getOpcode() == R600::GROUP_BARRIER)
Tom Stellardce540332013-06-28 15:46:59 +0000174 return true;
Vincent Lejeune21de8ba2013-07-31 19:31:41 +0000175 // XXX: This can be removed once the packetizer properly handles all the
176 // LDS instruction group restrictions.
Matt Arsenault8226fc42016-03-02 23:00:21 +0000177 return TII->isLDSInstr(MI.getOpcode());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000178 }
179
180 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
181 // together.
Craig Topper5656db42014-04-29 07:57:24 +0000182 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) override {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000183 MachineInstr *MII = SUI->getInstr(), *MIJ = SUJ->getInstr();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000184 if (getSlot(*MII) == getSlot(*MIJ))
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000185 ConsideredInstUsesAlreadyWrittenVectorElement = true;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000186 // Does MII and MIJ share the same pred_sel ?
Tom Stellardc5a154d2018-06-28 23:47:12 +0000187 int OpI = TII->getOperandIdx(MII->getOpcode(), R600::OpName::pred_sel),
188 OpJ = TII->getOperandIdx(MIJ->getOpcode(), R600::OpName::pred_sel);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000189 unsigned PredI = (OpI > -1)?MII->getOperand(OpI).getReg():0,
190 PredJ = (OpJ > -1)?MIJ->getOperand(OpJ).getReg():0;
191 if (PredI != PredJ)
192 return false;
193 if (SUJ->isSucc(SUI)) {
194 for (unsigned i = 0, e = SUJ->Succs.size(); i < e; ++i) {
195 const SDep &Dep = SUJ->Succs[i];
196 if (Dep.getSUnit() != SUI)
197 continue;
198 if (Dep.getKind() == SDep::Anti)
199 continue;
200 if (Dep.getKind() == SDep::Output)
201 if (MII->getOperand(0).getReg() != MIJ->getOperand(0).getReg())
202 continue;
203 return false;
204 }
205 }
Tom Stellard26a3b672013-10-22 18:19:10 +0000206
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000207 bool ARDef =
208 TII->definesAddressRegister(*MII) || TII->definesAddressRegister(*MIJ);
209 bool ARUse =
210 TII->usesAddressRegister(*MII) || TII->usesAddressRegister(*MIJ);
Tom Stellard26a3b672013-10-22 18:19:10 +0000211
Matt Arsenault8226fc42016-03-02 23:00:21 +0000212 return !ARDef || !ARUse;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000213 }
214
215 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
216 // and SUJ.
Craig Topper5656db42014-04-29 07:57:24 +0000217 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) override {
218 return false;
219 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000220
221 void setIsLastBit(MachineInstr *MI, unsigned Bit) const {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000222 unsigned LastOp = TII->getOperandIdx(MI->getOpcode(), R600::OpName::last);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000223 MI->getOperand(LastOp).setImm(Bit);
224 }
225
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000226 bool isBundlableWithCurrentPMI(MachineInstr &MI,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000227 const DenseMap<unsigned, unsigned> &PV,
228 std::vector<R600InstrInfo::BankSwizzle> &BS,
229 bool &isTransSlot) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000230 isTransSlot = TII->isTransOnly(MI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000231 assert (!isTransSlot || VLIW5);
232
233 // Is the dst reg sequence legal ?
234 if (!isTransSlot && !CurrentPacketMIs.empty()) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000235 if (getSlot(MI) <= getSlot(*CurrentPacketMIs.back())) {
236 if (ConsideredInstUsesAlreadyWrittenVectorElement &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000237 !TII->isVectorOnly(MI) && VLIW5) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000238 isTransSlot = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000239 LLVM_DEBUG({
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000240 dbgs() << "Considering as Trans Inst :";
241 MI.dump();
242 });
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000243 }
244 else
245 return false;
246 }
247 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000248
249 // Are the Constants limitations met ?
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000250 CurrentPacketMIs.push_back(&MI);
Vincent Lejeune77a83522013-06-29 19:32:43 +0000251 if (!TII->fitsConstReadLimitations(CurrentPacketMIs)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000252 LLVM_DEBUG({
Vincent Lejeune147700b2013-04-30 00:14:27 +0000253 dbgs() << "Couldn't pack :\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000254 MI.dump();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000255 dbgs() << "with the following packets :\n";
256 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
257 CurrentPacketMIs[i]->dump();
258 dbgs() << "\n";
259 }
260 dbgs() << "because of Consts read limitations\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000261 });
Vincent Lejeune77a83522013-06-29 19:32:43 +0000262 CurrentPacketMIs.pop_back();
263 return false;
264 }
265
266 // Is there a BankSwizzle set that meet Read Port limitations ?
267 if (!TII->fitsReadPortLimitations(CurrentPacketMIs,
268 PV, BS, isTransSlot)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000269 LLVM_DEBUG({
Vincent Lejeune147700b2013-04-30 00:14:27 +0000270 dbgs() << "Couldn't pack :\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000271 MI.dump();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000272 dbgs() << "with the following packets :\n";
273 for (unsigned i = 0, e = CurrentPacketMIs.size() - 1; i < e; i++) {
274 CurrentPacketMIs[i]->dump();
275 dbgs() << "\n";
276 }
277 dbgs() << "because of Read port limitations\n";
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000278 });
Vincent Lejeune77a83522013-06-29 19:32:43 +0000279 CurrentPacketMIs.pop_back();
280 return false;
281 }
282
Simon Pilgrime995a8082016-11-18 11:04:02 +0000283 // We cannot read LDS source registers from the Trans slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000284 if (isTransSlot && TII->readsLDSSrcReg(MI))
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000285 return false;
286
Vincent Lejeune77a83522013-06-29 19:32:43 +0000287 CurrentPacketMIs.pop_back();
288 return true;
289 }
290
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000291 MachineBasicBlock::iterator addToPacket(MachineInstr &MI) override {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000292 MachineBasicBlock::iterator FirstInBundle =
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000293 CurrentPacketMIs.empty() ? &MI : CurrentPacketMIs.front();
Vincent Lejeune77a83522013-06-29 19:32:43 +0000294 const DenseMap<unsigned, unsigned> &PV =
295 getPreviousVector(FirstInBundle);
296 std::vector<R600InstrInfo::BankSwizzle> BS;
297 bool isTransSlot;
298
299 if (isBundlableWithCurrentPMI(MI, PV, BS, isTransSlot)) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000300 for (unsigned i = 0, e = CurrentPacketMIs.size(); i < e; i++) {
301 MachineInstr *MI = CurrentPacketMIs[i];
Vincent Lejeune77a83522013-06-29 19:32:43 +0000302 unsigned Op = TII->getOperandIdx(MI->getOpcode(),
Tom Stellardc5a154d2018-06-28 23:47:12 +0000303 R600::OpName::bank_swizzle);
Vincent Lejeune77a83522013-06-29 19:32:43 +0000304 MI->getOperand(Op).setImm(BS[i]);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000305 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000306 unsigned Op =
Tom Stellardc5a154d2018-06-28 23:47:12 +0000307 TII->getOperandIdx(MI.getOpcode(), R600::OpName::bank_swizzle);
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000308 MI.getOperand(Op).setImm(BS.back());
Vincent Lejeune77a83522013-06-29 19:32:43 +0000309 if (!CurrentPacketMIs.empty())
310 setIsLastBit(CurrentPacketMIs.back(), 0);
311 substitutePV(MI, PV);
312 MachineBasicBlock::iterator It = VLIWPacketizerList::addToPacket(MI);
313 if (isTransSlot) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000314 endPacket(std::next(It)->getParent(), std::next(It));
Vincent Lejeune77a83522013-06-29 19:32:43 +0000315 }
316 return It;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000317 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000318 endPacket(MI.getParent(), MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000319 if (TII->isTransOnly(MI))
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000320 return MI;
Vincent Lejeune147700b2013-04-30 00:14:27 +0000321 return VLIWPacketizerList::addToPacket(MI);
322 }
Vincent Lejeune147700b2013-04-30 00:14:27 +0000323};
324
325bool R600Packetizer::runOnMachineFunction(MachineFunction &Fn) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000326 const R600Subtarget &ST = Fn.getSubtarget<R600Subtarget>();
327 const R600InstrInfo *TII = ST.getInstrInfo();
328
Vincent Lejeune147700b2013-04-30 00:14:27 +0000329 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000330
331 // Instantiate the packetizer.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000332 R600PacketizerList Packetizer(Fn, ST, MLI);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000333
334 // DFA state table should not be empty.
335 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
Tom Stellardc5a154d2018-06-28 23:47:12 +0000336 assert(Packetizer.getResourceTracker()->getInstrItins());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000337
Matt Arsenault8e001942016-06-02 18:37:16 +0000338 if (Packetizer.getResourceTracker()->getInstrItins()->isEmpty())
339 return false;
340
Vincent Lejeune147700b2013-04-30 00:14:27 +0000341 //
342 // Loop over all basic blocks and remove KILL pseudo-instructions
343 // These instructions confuse the dependence analysis. Consider:
344 // D0 = ... (Insn 0)
345 // R0 = KILL R0, D0 (Insn 1)
346 // R0 = ... (Insn 2)
347 // Here, Insn 1 will result in the dependence graph not emitting an output
348 // dependence between Insn 0 and Insn 2. This can lead to incorrect
349 // packetization
350 //
351 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
352 MBB != MBBe; ++MBB) {
353 MachineBasicBlock::iterator End = MBB->end();
354 MachineBasicBlock::iterator MI = MBB->begin();
355 while (MI != End) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000356 if (MI->isKill() || MI->getOpcode() == R600::IMPLICIT_DEF ||
357 (MI->getOpcode() == R600::CF_ALU && !MI->getOperand(8).getImm())) {
Vincent Lejeune147700b2013-04-30 00:14:27 +0000358 MachineBasicBlock::iterator DeleteMI = MI;
359 ++MI;
360 MBB->erase(DeleteMI);
361 End = MBB->end();
362 continue;
363 }
364 ++MI;
365 }
366 }
367
368 // Loop over all of the basic blocks.
369 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
370 MBB != MBBe; ++MBB) {
371 // Find scheduling regions and schedule / packetize each region.
372 unsigned RemainingCount = MBB->size();
373 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
374 RegionEnd != MBB->begin();) {
375 // The next region starts above the previous region. Look backward in the
376 // instruction stream until we find the nearest boundary.
377 MachineBasicBlock::iterator I = RegionEnd;
378 for(;I != MBB->begin(); --I, --RemainingCount) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000379 if (TII->isSchedulingBoundary(*std::prev(I), &*MBB, Fn))
Vincent Lejeune147700b2013-04-30 00:14:27 +0000380 break;
381 }
382 I = MBB->begin();
383
384 // Skip empty scheduling regions.
385 if (I == RegionEnd) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000386 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000387 --RemainingCount;
388 continue;
389 }
390 // Skip regions with one instruction.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000391 if (I == std::prev(RegionEnd)) {
392 RegionEnd = std::prev(RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000393 continue;
394 }
395
Duncan P. N. Exon Smitha73371a2015-10-13 20:07:10 +0000396 Packetizer.PacketizeMIs(&*MBB, &*I, RegionEnd);
Vincent Lejeune147700b2013-04-30 00:14:27 +0000397 RegionEnd = I;
398 }
399 }
400
401 return true;
402
403}
404
Benjamin Kramerd78bb462013-05-23 17:10:37 +0000405} // end anonymous namespace
Vincent Lejeune147700b2013-04-30 00:14:27 +0000406
Tom Stellarda2f57be2017-08-02 22:19:45 +0000407INITIALIZE_PASS_BEGIN(R600Packetizer, DEBUG_TYPE,
408 "R600 Packetizer", false, false)
409INITIALIZE_PASS_END(R600Packetizer, DEBUG_TYPE,
410 "R600 Packetizer", false, false)
411
412char R600Packetizer::ID = 0;
413
414char &llvm::R600PacketizerID = R600Packetizer::ID;
415
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000416llvm::FunctionPass *llvm::createR600Packetizer() {
417 return new R600Packetizer();
Vincent Lejeune147700b2013-04-30 00:14:27 +0000418}