blob: 1dfd840fdc643872dd7180fce328bade0287a15d [file] [log] [blame]
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +00001//===-- R600MachineScheduler.cpp - R600 Scheduler Interface -*- C++ -*-----===//
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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// R600 Machine Scheduler interface
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000012//
13//===----------------------------------------------------------------------===//
14
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000015#include "R600MachineScheduler.h"
Tom Stellard2e59a452014-06-13 01:32:00 +000016#include "AMDGPUSubtarget.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "R600InstrInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000018#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000020#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/Pass.h"
NAKAMURA Takumi756cf882013-03-11 08:19:28 +000022#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi756cf882013-03-11 08:19:28 +000023
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000024using namespace llvm;
25
Evandro Menezes0cd23f562017-07-11 22:08:28 +000026#define DEBUG_TYPE "machine-scheduler"
Chandler Carruth84e68b22014-04-22 02:41:26 +000027
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000028void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
Andrew Trickd7f890e2013-12-28 21:56:47 +000029 assert(dag->hasVRegLiveness() && "R600SchedStrategy needs vreg liveness");
30 DAG = static_cast<ScheduleDAGMILive*>(dag);
Matt Arsenault43e92fe2016-06-24 06:30:11 +000031 const R600Subtarget &ST = DAG->MF.getSubtarget<R600Subtarget>();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000032 TII = static_cast<const R600InstrInfo*>(DAG->TII);
33 TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
Eric Christopher7792e322015-01-30 23:24:40 +000034 VLIW5 = !ST.hasCaymanISA();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000035 MRI = &DAG->MRI;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000036 CurInstKind = IDOther;
37 CurEmitted = 0;
Vincent Lejeune77a83522013-06-29 19:32:43 +000038 OccupedSlotsMask = 31;
Vincent Lejeune80031d9f2013-04-03 16:49:34 +000039 InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
Vincent Lejeune3d5118c2013-05-17 16:50:56 +000040 InstKindLimit[IDOther] = 32;
Vincent Lejeunef9f4e1e2013-05-17 16:49:55 +000041 InstKindLimit[IDFetch] = ST.getTexVTXClauseSize();
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000042 AluInstCount = 0;
43 FetchInstCount = 0;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000044}
45
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000046void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc,
47 std::vector<SUnit *> &QDst)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000048{
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000049 QDst.insert(QDst.end(), QSrc.begin(), QSrc.end());
50 QSrc.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000051}
52
Matt Arsenault43e92fe2016-06-24 06:30:11 +000053static unsigned getWFCountLimitedByGPR(unsigned GPRCount) {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000054 assert (GPRCount && "GPRCount cannot be 0");
55 return 248 / GPRCount;
56}
57
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000058SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
Craig Topper062a2ba2014-04-25 05:30:21 +000059 SUnit *SU = nullptr;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000060 NextInstKind = IDOther;
61
Vincent Lejeune3d5118c2013-05-17 16:50:56 +000062 IsTopNode = false;
63
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000064 // check if we might want to switch current clause type
Vincent Lejeune3d5118c2013-05-17 16:50:56 +000065 bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) ||
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000066 (Available[CurInstKind].empty());
Vincent Lejeunef9f4e1e2013-05-17 16:49:55 +000067 bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) &&
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000068 (!Available[IDFetch].empty() || !Available[IDOther].empty());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000069
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000070 if (CurInstKind == IDAlu && !Available[IDFetch].empty()) {
71 // We use the heuristic provided by AMD Accelerated Parallel Processing
72 // OpenCL Programming Guide :
73 // The approx. number of WF that allows TEX inst to hide ALU inst is :
74 // 500 (cycles for TEX) / (AluFetchRatio * 8 (cycles for ALU))
Andrew Trickd7f890e2013-12-28 21:56:47 +000075 float ALUFetchRationEstimate =
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000076 (AluInstCount + AvailablesAluCount() + Pending[IDAlu].size()) /
77 (FetchInstCount + Available[IDFetch].size());
Alexey Samsonovcce57012014-09-17 17:47:21 +000078 if (ALUFetchRationEstimate == 0) {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000079 AllowSwitchFromAlu = true;
Alexey Samsonovcce57012014-09-17 17:47:21 +000080 } else {
81 unsigned NeededWF = 62.5f / ALUFetchRationEstimate;
Nicola Zaghend34e60c2018-05-14 12:53:11 +000082 LLVM_DEBUG(dbgs() << NeededWF << " approx. Wavefronts Required\n");
Alexey Samsonovcce57012014-09-17 17:47:21 +000083 // We assume the local GPR requirements to be "dominated" by the requirement
84 // of the TEX clause (which consumes 128 bits regs) ; ALU inst before and
85 // after TEX are indeed likely to consume or generate values from/for the
86 // TEX clause.
87 // Available[IDFetch].size() * 2 : GPRs required in the Fetch clause
88 // We assume that fetch instructions are either TnXYZW = TEX TnXYZW (need
89 // one GPR) or TmXYZW = TnXYZW (need 2 GPR).
90 // (TODO : use RegisterPressure)
91 // If we are going too use too many GPR, we flush Fetch instruction to lower
92 // register pressure on 128 bits regs.
93 unsigned NearRegisterRequirement = 2 * Available[IDFetch].size();
94 if (NeededWF > getWFCountLimitedByGPR(NearRegisterRequirement))
95 AllowSwitchFromAlu = true;
96 }
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000097 }
98
Tom Stellardaad53762013-06-05 03:43:06 +000099 if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
100 (!AllowSwitchFromAlu && CurInstKind == IDAlu))) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000101 // try to pick ALU
102 SU = pickAlu();
Vincent Lejeune4b5b8492013-06-05 20:27:35 +0000103 if (!SU && !PhysicalRegCopy.empty()) {
104 SU = PhysicalRegCopy.front();
105 PhysicalRegCopy.erase(PhysicalRegCopy.begin());
106 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000107 if (SU) {
Vincent Lejeunef9f4e1e2013-05-17 16:49:55 +0000108 if (CurEmitted >= InstKindLimit[IDAlu])
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000109 CurEmitted = 0;
110 NextInstKind = IDAlu;
111 }
112 }
113
114 if (!SU) {
115 // try to pick FETCH
116 SU = pickOther(IDFetch);
117 if (SU)
118 NextInstKind = IDFetch;
119 }
120
121 // try to pick other
122 if (!SU) {
123 SU = pickOther(IDOther);
124 if (SU)
125 NextInstKind = IDOther;
126 }
127
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000128 LLVM_DEBUG(if (SU) {
129 dbgs() << " ** Pick node **\n";
130 SU->dump(DAG);
131 } else {
132 dbgs() << "NO NODE \n";
133 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
134 const SUnit &S = DAG->SUnits[i];
135 if (!S.isScheduled)
136 S.dump(DAG);
137 }
138 });
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000139
140 return SU;
141}
142
143void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000144 if (NextInstKind != CurInstKind) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000145 LLVM_DEBUG(dbgs() << "Instruction Type Switch\n");
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000146 if (NextInstKind != IDAlu)
Vincent Lejeune77a83522013-06-29 19:32:43 +0000147 OccupedSlotsMask |= 31;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000148 CurEmitted = 0;
149 CurInstKind = NextInstKind;
150 }
151
152 if (CurInstKind == IDAlu) {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000153 AluInstCount ++;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000154 switch (getAluKind(SU)) {
155 case AluT_XYZW:
156 CurEmitted += 4;
157 break;
158 case AluDiscarded:
159 break;
160 default: {
161 ++CurEmitted;
162 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
163 E = SU->getInstr()->operands_end(); It != E; ++It) {
164 MachineOperand &MO = *It;
165 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
166 ++CurEmitted;
167 }
168 }
169 }
170 } else {
171 ++CurEmitted;
172 }
173
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000174 LLVM_DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000175
176 if (CurInstKind != IDFetch) {
177 MoveUnits(Pending[IDFetch], Available[IDFetch]);
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000178 } else
179 FetchInstCount++;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000180}
181
Vincent Lejeune4b5b8492013-06-05 20:27:35 +0000182static bool
183isPhysicalRegCopy(MachineInstr *MI) {
184 if (MI->getOpcode() != AMDGPU::COPY)
185 return false;
186
187 return !TargetRegisterInfo::isVirtualRegister(MI->getOperand(1).getReg());
188}
189
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000190void R600SchedStrategy::releaseTopNode(SUnit *SU) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000191 LLVM_DEBUG(dbgs() << "Top Releasing "; SU->dump(DAG););
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000192}
193
194void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000195 LLVM_DEBUG(dbgs() << "Bottom Releasing "; SU->dump(DAG););
Vincent Lejeune4b5b8492013-06-05 20:27:35 +0000196 if (isPhysicalRegCopy(SU->getInstr())) {
197 PhysicalRegCopy.push_back(SU);
198 return;
199 }
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000200
201 int IK = getInstKind(SU);
Tom Stellardaad53762013-06-05 03:43:06 +0000202
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000203 // There is no export clause, we can schedule one as soon as its ready
204 if (IK == IDOther)
205 Available[IDOther].push_back(SU);
206 else
207 Pending[IK].push_back(SU);
208
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000209}
210
211bool R600SchedStrategy::regBelongsToClass(unsigned Reg,
212 const TargetRegisterClass *RC) const {
213 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
214 return RC->contains(Reg);
215 } else {
216 return MRI->getRegClass(Reg) == RC;
217 }
218}
219
220R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
221 MachineInstr *MI = SU->getInstr();
222
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 if (TII->isTransOnly(*MI))
Vincent Lejeune77a83522013-06-29 19:32:43 +0000224 return AluTrans;
225
Matt Arsenault180e0d52016-06-22 01:53:49 +0000226 switch (MI->getOpcode()) {
227 case AMDGPU::PRED_X:
228 return AluPredX;
229 case AMDGPU::INTERP_PAIR_XY:
230 case AMDGPU::INTERP_PAIR_ZW:
231 case AMDGPU::INTERP_VEC_LOAD:
232 case AMDGPU::DOT_4:
233 return AluT_XYZW;
234 case AMDGPU::COPY:
235 if (MI->getOperand(1).isUndef()) {
236 // MI will become a KILL, don't considers it in scheduling
237 return AluDiscarded;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000238 }
Matt Arsenault180e0d52016-06-22 01:53:49 +0000239 default:
240 break;
241 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000242
Matt Arsenault180e0d52016-06-22 01:53:49 +0000243 // Does the instruction take a whole IG ?
244 // XXX: Is it possible to add a helper function in R600InstrInfo that can
245 // be used here and in R600PacketizerList::isSoloInstruction() ?
246 if(TII->isVector(*MI) ||
247 TII->isCubeOp(MI->getOpcode()) ||
248 TII->isReductionOp(MI->getOpcode()) ||
249 MI->getOpcode() == AMDGPU::GROUP_BARRIER) {
250 return AluT_XYZW;
251 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000252
Matt Arsenault180e0d52016-06-22 01:53:49 +0000253 if (TII->isLDSInstr(MI->getOpcode())) {
254 return AluT_X;
255 }
Tom Stellardc026e8b2013-06-28 15:47:08 +0000256
Matt Arsenault180e0d52016-06-22 01:53:49 +0000257 // Is the result already assigned to a channel ?
258 unsigned DestSubReg = MI->getOperand(0).getSubReg();
259 switch (DestSubReg) {
260 case AMDGPU::sub0:
261 return AluT_X;
262 case AMDGPU::sub1:
263 return AluT_Y;
264 case AMDGPU::sub2:
265 return AluT_Z;
266 case AMDGPU::sub3:
267 return AluT_W;
268 default:
269 break;
270 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000271
Matt Arsenault180e0d52016-06-22 01:53:49 +0000272 // Is the result already member of a X/Y/Z/W class ?
273 unsigned DestReg = MI->getOperand(0).getReg();
274 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) ||
275 regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass))
276 return AluT_X;
277 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass))
278 return AluT_Y;
279 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass))
280 return AluT_Z;
281 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass))
282 return AluT_W;
283 if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass))
284 return AluT_XYZW;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000285
Matt Arsenault180e0d52016-06-22 01:53:49 +0000286 // LDS src registers cannot be used in the Trans slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000287 if (TII->readsLDSSrcReg(*MI))
Matt Arsenault180e0d52016-06-22 01:53:49 +0000288 return AluT_XYZW;
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000289
Matt Arsenault180e0d52016-06-22 01:53:49 +0000290 return AluAny;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000291}
292
293int R600SchedStrategy::getInstKind(SUnit* SU) {
294 int Opcode = SU->getInstr()->getOpcode();
295
Vincent Lejeunee958c8e2013-05-17 16:50:37 +0000296 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
297 return IDFetch;
298
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000299 if (TII->isALUInstr(Opcode)) {
300 return IDAlu;
301 }
302
303 switch (Opcode) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000304 case AMDGPU::PRED_X:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000305 case AMDGPU::COPY:
306 case AMDGPU::CONST_COPY:
307 case AMDGPU::INTERP_PAIR_XY:
308 case AMDGPU::INTERP_PAIR_ZW:
309 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000310 case AMDGPU::DOT_4:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000311 return IDAlu;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000312 default:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000313 return IDOther;
314 }
315}
316
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000317SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000318 if (Q.empty())
Craig Topper062a2ba2014-04-25 05:30:21 +0000319 return nullptr;
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000320 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000321 It != E; ++It) {
322 SUnit *SU = *It;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000323 InstructionsGroupCandidate.push_back(SU->getInstr());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000324 if (TII->fitsConstReadLimitations(InstructionsGroupCandidate) &&
325 (!AnyALU || !TII->isVectorOnly(*SU->getInstr()))) {
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000326 InstructionsGroupCandidate.pop_back();
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000327 Q.erase((It + 1).base());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000328 return SU;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000329 } else {
330 InstructionsGroupCandidate.pop_back();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000331 }
332 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000333 return nullptr;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000334}
335
336void R600SchedStrategy::LoadAlu() {
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000337 std::vector<SUnit *> &QSrc = Pending[IDAlu];
338 for (unsigned i = 0, e = QSrc.size(); i < e; ++i) {
339 AluKind AK = getAluKind(QSrc[i]);
340 AvailableAlus[AK].push_back(QSrc[i]);
341 }
342 QSrc.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000343}
344
345void R600SchedStrategy::PrepareNextSlot() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000346 LLVM_DEBUG(dbgs() << "New Slot\n");
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000347 assert (OccupedSlotsMask && "Slot wasn't filled");
348 OccupedSlotsMask = 0;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000349// if (HwGen == R600Subtarget::NORTHERN_ISLANDS)
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000350// OccupedSlotsMask |= 16;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000351 InstructionsGroupCandidate.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000352 LoadAlu();
353}
354
355void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000356 int DstIndex = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
357 if (DstIndex == -1) {
358 return;
359 }
360 unsigned DestReg = MI->getOperand(DstIndex).getReg();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000361 // PressureRegister crashes if an operand is def and used in the same inst
362 // and we try to constraint its regclass
363 for (MachineInstr::mop_iterator It = MI->operands_begin(),
364 E = MI->operands_end(); It != E; ++It) {
365 MachineOperand &MO = *It;
366 if (MO.isReg() && !MO.isDef() &&
Tom Stellardc026e8b2013-06-28 15:47:08 +0000367 MO.getReg() == DestReg)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000368 return;
369 }
370 // Constrains the regclass of DestReg to assign it to Slot
371 switch (Slot) {
372 case 0:
373 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
374 break;
375 case 1:
376 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
377 break;
378 case 2:
379 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
380 break;
381 case 3:
382 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
383 break;
384 }
385}
386
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000387SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000388 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000389 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000390 if (SlotedSU)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000391 return SlotedSU;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000392 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000393 if (UnslotedSU)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000394 AssignSlot(UnslotedSU->getInstr(), Slot);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000395 return UnslotedSU;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000396}
397
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000398unsigned R600SchedStrategy::AvailablesAluCount() const {
399 return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() +
400 AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() +
401 AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() +
Vincent Lejeune77a83522013-06-29 19:32:43 +0000402 AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() +
403 AvailableAlus[AluPredX].size();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000404}
405
406SUnit* R600SchedStrategy::pickAlu() {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000407 while (AvailablesAluCount() || !Pending[IDAlu].empty()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000408 if (!OccupedSlotsMask) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000409 // Bottom up scheduling : predX must comes first
410 if (!AvailableAlus[AluPredX].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000411 OccupedSlotsMask |= 31;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000412 return PopInst(AvailableAlus[AluPredX], false);
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000413 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000414 // Flush physical reg copies (RA will discard them)
415 if (!AvailableAlus[AluDiscarded].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000416 OccupedSlotsMask |= 31;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000417 return PopInst(AvailableAlus[AluDiscarded], false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000418 }
419 // If there is a T_XYZW alu available, use it
420 if (!AvailableAlus[AluT_XYZW].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000421 OccupedSlotsMask |= 15;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000422 return PopInst(AvailableAlus[AluT_XYZW], false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000423 }
424 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000425 bool TransSlotOccuped = OccupedSlotsMask & 16;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000426 if (!TransSlotOccuped && VLIW5) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000427 if (!AvailableAlus[AluTrans].empty()) {
428 OccupedSlotsMask |= 16;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000429 return PopInst(AvailableAlus[AluTrans], false);
430 }
431 SUnit *SU = AttemptFillSlot(3, true);
432 if (SU) {
433 OccupedSlotsMask |= 16;
434 return SU;
Vincent Lejeune77a83522013-06-29 19:32:43 +0000435 }
436 }
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000437 for (int Chan = 3; Chan > -1; --Chan) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000438 bool isOccupied = OccupedSlotsMask & (1 << Chan);
439 if (!isOccupied) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000440 SUnit *SU = AttemptFillSlot(Chan, false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000441 if (SU) {
442 OccupedSlotsMask |= (1 << Chan);
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000443 InstructionsGroupCandidate.push_back(SU->getInstr());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000444 return SU;
445 }
446 }
447 }
448 PrepareNextSlot();
449 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000450 return nullptr;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000451}
452
453SUnit* R600SchedStrategy::pickOther(int QID) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000454 SUnit *SU = nullptr;
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000455 std::vector<SUnit *> &AQ = Available[QID];
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000456
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000457 if (AQ.empty()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000458 MoveUnits(Pending[QID], AQ);
459 }
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000460 if (!AQ.empty()) {
461 SU = AQ.back();
462 AQ.resize(AQ.size() - 1);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000463 }
464 return SU;
465}