blob: 7ea654cb14cd198d56adf7cdb95d97a58b965a32 [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
11/// \brief 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"
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000019#include "llvm/Pass.h"
20#include "llvm/PassManager.h"
NAKAMURA Takumi756cf882013-03-11 08:19:28 +000021#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi756cf882013-03-11 08:19:28 +000022
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000023using namespace llvm;
24
Chandler Carruth84e68b22014-04-22 02:41:26 +000025#define DEBUG_TYPE "misched"
26
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000027void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
Andrew Trickd7f890e2013-12-28 21:56:47 +000028 assert(dag->hasVRegLiveness() && "R600SchedStrategy needs vreg liveness");
29 DAG = static_cast<ScheduleDAGMILive*>(dag);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000030 TII = static_cast<const R600InstrInfo*>(DAG->TII);
31 TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000032 VLIW5 = !DAG->MF.getTarget().getSubtarget<AMDGPUSubtarget>().hasCaymanISA();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000033 MRI = &DAG->MRI;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000034 CurInstKind = IDOther;
35 CurEmitted = 0;
Vincent Lejeune77a83522013-06-29 19:32:43 +000036 OccupedSlotsMask = 31;
Vincent Lejeune80031d9f2013-04-03 16:49:34 +000037 InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
Vincent Lejeune3d5118c2013-05-17 16:50:56 +000038 InstKindLimit[IDOther] = 32;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000039
40 const AMDGPUSubtarget &ST = DAG->TM.getSubtarget<AMDGPUSubtarget>();
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
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000053static
54unsigned getWFCountLimitedByGPR(unsigned GPRCount) {
55 assert (GPRCount && "GPRCount cannot be 0");
56 return 248 / GPRCount;
57}
58
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000059SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
Craig Topper062a2ba2014-04-25 05:30:21 +000060 SUnit *SU = nullptr;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000061 NextInstKind = IDOther;
62
Vincent Lejeune3d5118c2013-05-17 16:50:56 +000063 IsTopNode = false;
64
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000065 // check if we might want to switch current clause type
Vincent Lejeune3d5118c2013-05-17 16:50:56 +000066 bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) ||
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000067 (Available[CurInstKind].empty());
Vincent Lejeunef9f4e1e2013-05-17 16:49:55 +000068 bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) &&
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000069 (!Available[IDFetch].empty() || !Available[IDOther].empty());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000070
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000071 if (CurInstKind == IDAlu && !Available[IDFetch].empty()) {
72 // We use the heuristic provided by AMD Accelerated Parallel Processing
73 // OpenCL Programming Guide :
74 // The approx. number of WF that allows TEX inst to hide ALU inst is :
75 // 500 (cycles for TEX) / (AluFetchRatio * 8 (cycles for ALU))
Andrew Trickd7f890e2013-12-28 21:56:47 +000076 float ALUFetchRationEstimate =
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000077 (AluInstCount + AvailablesAluCount() + Pending[IDAlu].size()) /
78 (FetchInstCount + Available[IDFetch].size());
79 unsigned NeededWF = 62.5f / ALUFetchRationEstimate;
80 DEBUG( dbgs() << NeededWF << " approx. Wavefronts Required\n" );
81 // We assume the local GPR requirements to be "dominated" by the requirement
82 // of the TEX clause (which consumes 128 bits regs) ; ALU inst before and
83 // after TEX are indeed likely to consume or generate values from/for the
84 // TEX clause.
85 // Available[IDFetch].size() * 2 : GPRs required in the Fetch clause
86 // We assume that fetch instructions are either TnXYZW = TEX TnXYZW (need
87 // one GPR) or TmXYZW = TnXYZW (need 2 GPR).
88 // (TODO : use RegisterPressure)
89 // If we are going too use too many GPR, we flush Fetch instruction to lower
90 // register pressure on 128 bits regs.
91 unsigned NearRegisterRequirement = 2 * Available[IDFetch].size();
92 if (NeededWF > getWFCountLimitedByGPR(NearRegisterRequirement))
93 AllowSwitchFromAlu = true;
94 }
95
Tom Stellardaad53762013-06-05 03:43:06 +000096 if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
97 (!AllowSwitchFromAlu && CurInstKind == IDAlu))) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000098 // try to pick ALU
99 SU = pickAlu();
Vincent Lejeune4b5b8492013-06-05 20:27:35 +0000100 if (!SU && !PhysicalRegCopy.empty()) {
101 SU = PhysicalRegCopy.front();
102 PhysicalRegCopy.erase(PhysicalRegCopy.begin());
103 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000104 if (SU) {
Vincent Lejeunef9f4e1e2013-05-17 16:49:55 +0000105 if (CurEmitted >= InstKindLimit[IDAlu])
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000106 CurEmitted = 0;
107 NextInstKind = IDAlu;
108 }
109 }
110
111 if (!SU) {
112 // try to pick FETCH
113 SU = pickOther(IDFetch);
114 if (SU)
115 NextInstKind = IDFetch;
116 }
117
118 // try to pick other
119 if (!SU) {
120 SU = pickOther(IDOther);
121 if (SU)
122 NextInstKind = IDOther;
123 }
124
125 DEBUG(
126 if (SU) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000127 dbgs() << " ** Pick node **\n";
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000128 SU->dump(DAG);
129 } else {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000130 dbgs() << "NO NODE \n";
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000131 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
132 const SUnit &S = DAG->SUnits[i];
133 if (!S.isScheduled)
134 S.dump(DAG);
135 }
136 }
137 );
138
139 return SU;
140}
141
142void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000143 if (NextInstKind != CurInstKind) {
144 DEBUG(dbgs() << "Instruction Type Switch\n");
145 if (NextInstKind != IDAlu)
Vincent Lejeune77a83522013-06-29 19:32:43 +0000146 OccupedSlotsMask |= 31;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000147 CurEmitted = 0;
148 CurInstKind = NextInstKind;
149 }
150
151 if (CurInstKind == IDAlu) {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000152 AluInstCount ++;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000153 switch (getAluKind(SU)) {
154 case AluT_XYZW:
155 CurEmitted += 4;
156 break;
157 case AluDiscarded:
158 break;
159 default: {
160 ++CurEmitted;
161 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
162 E = SU->getInstr()->operands_end(); It != E; ++It) {
163 MachineOperand &MO = *It;
164 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
165 ++CurEmitted;
166 }
167 }
168 }
169 } else {
170 ++CurEmitted;
171 }
172
173
174 DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
175
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) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000191 DEBUG(dbgs() << "Top Releasing ";SU->dump(DAG););
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000192}
193
194void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000195 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
Vincent Lejeune77a83522013-06-29 19:32:43 +0000223 if (TII->isTransOnly(MI))
224 return AluTrans;
225
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000226 switch (MI->getOpcode()) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000227 case AMDGPU::PRED_X:
228 return AluPredX;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000229 case AMDGPU::INTERP_PAIR_XY:
230 case AMDGPU::INTERP_PAIR_ZW:
231 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000232 case AMDGPU::DOT_4:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000233 return AluT_XYZW;
234 case AMDGPU::COPY:
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000235 if (MI->getOperand(1).isUndef()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000236 // MI will become a KILL, don't considers it in scheduling
237 return AluDiscarded;
238 }
239 default:
240 break;
241 }
242
243 // Does the instruction take a whole IG ?
Tom Stellardce540332013-06-28 15:46:59 +0000244 // XXX: Is it possible to add a helper function in R600InstrInfo that can
245 // be used here and in R600PacketizerList::isSoloInstruction() ?
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000246 if(TII->isVector(*MI) ||
247 TII->isCubeOp(MI->getOpcode()) ||
Tom Stellardce540332013-06-28 15:46:59 +0000248 TII->isReductionOp(MI->getOpcode()) ||
249 MI->getOpcode() == AMDGPU::GROUP_BARRIER) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000250 return AluT_XYZW;
Tom Stellardce540332013-06-28 15:46:59 +0000251 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000252
Tom Stellardc026e8b2013-06-28 15:47:08 +0000253 if (TII->isLDSInstr(MI->getOpcode())) {
254 return AluT_X;
255 }
256
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +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 }
271
272 // 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;
285
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000286 // LDS src registers cannot be used in the Trans slot.
287 if (TII->readsLDSSrcReg(MI))
288 return AluT_XYZW;
289
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000290 return AluAny;
291
292}
293
294int R600SchedStrategy::getInstKind(SUnit* SU) {
295 int Opcode = SU->getInstr()->getOpcode();
296
Vincent Lejeunee958c8e2013-05-17 16:50:37 +0000297 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
298 return IDFetch;
299
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000300 if (TII->isALUInstr(Opcode)) {
301 return IDAlu;
302 }
303
304 switch (Opcode) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000305 case AMDGPU::PRED_X:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000306 case AMDGPU::COPY:
307 case AMDGPU::CONST_COPY:
308 case AMDGPU::INTERP_PAIR_XY:
309 case AMDGPU::INTERP_PAIR_ZW:
310 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000311 case AMDGPU::DOT_4:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000312 return IDAlu;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000313 default:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000314 return IDOther;
315 }
316}
317
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000318SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000319 if (Q.empty())
Craig Topper062a2ba2014-04-25 05:30:21 +0000320 return nullptr;
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000321 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000322 It != E; ++It) {
323 SUnit *SU = *It;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000324 InstructionsGroupCandidate.push_back(SU->getInstr());
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000325 if (TII->fitsConstReadLimitations(InstructionsGroupCandidate)
326 && (!AnyALU || !TII->isVectorOnly(SU->getInstr()))
327 ) {
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000328 InstructionsGroupCandidate.pop_back();
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000329 Q.erase((It + 1).base());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000330 return SU;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000331 } else {
332 InstructionsGroupCandidate.pop_back();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000333 }
334 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000335 return nullptr;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000336}
337
338void R600SchedStrategy::LoadAlu() {
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000339 std::vector<SUnit *> &QSrc = Pending[IDAlu];
340 for (unsigned i = 0, e = QSrc.size(); i < e; ++i) {
341 AluKind AK = getAluKind(QSrc[i]);
342 AvailableAlus[AK].push_back(QSrc[i]);
343 }
344 QSrc.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000345}
346
347void R600SchedStrategy::PrepareNextSlot() {
348 DEBUG(dbgs() << "New Slot\n");
349 assert (OccupedSlotsMask && "Slot wasn't filled");
350 OccupedSlotsMask = 0;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000351// if (HwGen == AMDGPUSubtarget::NORTHERN_ISLANDS)
352// OccupedSlotsMask |= 16;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000353 InstructionsGroupCandidate.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000354 LoadAlu();
355}
356
357void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000358 int DstIndex = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
359 if (DstIndex == -1) {
360 return;
361 }
362 unsigned DestReg = MI->getOperand(DstIndex).getReg();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000363 // PressureRegister crashes if an operand is def and used in the same inst
364 // and we try to constraint its regclass
365 for (MachineInstr::mop_iterator It = MI->operands_begin(),
366 E = MI->operands_end(); It != E; ++It) {
367 MachineOperand &MO = *It;
368 if (MO.isReg() && !MO.isDef() &&
Tom Stellardc026e8b2013-06-28 15:47:08 +0000369 MO.getReg() == DestReg)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000370 return;
371 }
372 // Constrains the regclass of DestReg to assign it to Slot
373 switch (Slot) {
374 case 0:
375 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
376 break;
377 case 1:
378 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
379 break;
380 case 2:
381 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
382 break;
383 case 3:
384 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
385 break;
386 }
387}
388
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000389SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000390 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000391 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000392 if (SlotedSU)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000393 return SlotedSU;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000394 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000395 if (UnslotedSU)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000396 AssignSlot(UnslotedSU->getInstr(), Slot);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000397 return UnslotedSU;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000398}
399
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000400unsigned R600SchedStrategy::AvailablesAluCount() const {
401 return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() +
402 AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() +
403 AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() +
Vincent Lejeune77a83522013-06-29 19:32:43 +0000404 AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() +
405 AvailableAlus[AluPredX].size();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000406}
407
408SUnit* R600SchedStrategy::pickAlu() {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000409 while (AvailablesAluCount() || !Pending[IDAlu].empty()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000410 if (!OccupedSlotsMask) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000411 // Bottom up scheduling : predX must comes first
412 if (!AvailableAlus[AluPredX].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000413 OccupedSlotsMask |= 31;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000414 return PopInst(AvailableAlus[AluPredX], false);
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000415 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000416 // Flush physical reg copies (RA will discard them)
417 if (!AvailableAlus[AluDiscarded].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000418 OccupedSlotsMask |= 31;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000419 return PopInst(AvailableAlus[AluDiscarded], false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000420 }
421 // If there is a T_XYZW alu available, use it
422 if (!AvailableAlus[AluT_XYZW].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000423 OccupedSlotsMask |= 15;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000424 return PopInst(AvailableAlus[AluT_XYZW], false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000425 }
426 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000427 bool TransSlotOccuped = OccupedSlotsMask & 16;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000428 if (!TransSlotOccuped && VLIW5) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000429 if (!AvailableAlus[AluTrans].empty()) {
430 OccupedSlotsMask |= 16;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000431 return PopInst(AvailableAlus[AluTrans], false);
432 }
433 SUnit *SU = AttemptFillSlot(3, true);
434 if (SU) {
435 OccupedSlotsMask |= 16;
436 return SU;
Vincent Lejeune77a83522013-06-29 19:32:43 +0000437 }
438 }
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000439 for (int Chan = 3; Chan > -1; --Chan) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000440 bool isOccupied = OccupedSlotsMask & (1 << Chan);
441 if (!isOccupied) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000442 SUnit *SU = AttemptFillSlot(Chan, false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000443 if (SU) {
444 OccupedSlotsMask |= (1 << Chan);
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000445 InstructionsGroupCandidate.push_back(SU->getInstr());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000446 return SU;
447 }
448 }
449 }
450 PrepareNextSlot();
451 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000452 return nullptr;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000453}
454
455SUnit* R600SchedStrategy::pickOther(int QID) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000456 SUnit *SU = nullptr;
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000457 std::vector<SUnit *> &AQ = Available[QID];
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000458
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000459 if (AQ.empty()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000460 MoveUnits(Pending[QID], AQ);
461 }
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000462 if (!AQ.empty()) {
463 SU = AQ.back();
464 AQ.resize(AQ.size() - 1);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000465 }
466 return SU;
467}