blob: 0499dd52d923db8d37f4f8900742b995d489a8be [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
15#define DEBUG_TYPE "misched"
16
17#include "R600MachineScheduler.h"
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000018#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000020#include "llvm/Pass.h"
21#include "llvm/PassManager.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
26void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
27
28 DAG = dag;
29 TII = static_cast<const R600InstrInfo*>(DAG->TII);
30 TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
Vincent Lejeune7e2c8322013-09-04 19:53:46 +000031 VLIW5 = !DAG->MF.getTarget().getSubtarget<AMDGPUSubtarget>().hasCaymanISA();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000032 MRI = &DAG->MRI;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000033 CurInstKind = IDOther;
34 CurEmitted = 0;
Vincent Lejeune77a83522013-06-29 19:32:43 +000035 OccupedSlotsMask = 31;
Vincent Lejeune80031d9f2013-04-03 16:49:34 +000036 InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
Vincent Lejeune3d5118c2013-05-17 16:50:56 +000037 InstKindLimit[IDOther] = 32;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000038
39 const AMDGPUSubtarget &ST = DAG->TM.getSubtarget<AMDGPUSubtarget>();
Vincent Lejeunef9f4e1e2013-05-17 16:49:55 +000040 InstKindLimit[IDFetch] = ST.getTexVTXClauseSize();
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000041 AluInstCount = 0;
42 FetchInstCount = 0;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000043}
44
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000045void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc,
46 std::vector<SUnit *> &QDst)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000047{
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +000048 QDst.insert(QDst.end(), QSrc.begin(), QSrc.end());
49 QSrc.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000050}
51
Vincent Lejeuned1a9d182013-06-07 23:30:34 +000052static
53unsigned getWFCountLimitedByGPR(unsigned GPRCount) {
54 assert (GPRCount && "GPRCount cannot be 0");
55 return 248 / GPRCount;
56}
57
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +000058SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
59 SUnit *SU = 0;
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))
75 float ALUFetchRationEstimate =
76 (AluInstCount + AvailablesAluCount() + Pending[IDAlu].size()) /
77 (FetchInstCount + Available[IDFetch].size());
78 unsigned NeededWF = 62.5f / ALUFetchRationEstimate;
79 DEBUG( dbgs() << NeededWF << " approx. Wavefronts Required\n" );
80 // We assume the local GPR requirements to be "dominated" by the requirement
81 // of the TEX clause (which consumes 128 bits regs) ; ALU inst before and
82 // after TEX are indeed likely to consume or generate values from/for the
83 // TEX clause.
84 // Available[IDFetch].size() * 2 : GPRs required in the Fetch clause
85 // We assume that fetch instructions are either TnXYZW = TEX TnXYZW (need
86 // one GPR) or TmXYZW = TnXYZW (need 2 GPR).
87 // (TODO : use RegisterPressure)
88 // If we are going too use too many GPR, we flush Fetch instruction to lower
89 // register pressure on 128 bits regs.
90 unsigned NearRegisterRequirement = 2 * Available[IDFetch].size();
91 if (NeededWF > getWFCountLimitedByGPR(NearRegisterRequirement))
92 AllowSwitchFromAlu = true;
93 }
94
95
Tom Stellardaad53762013-06-05 03:43:06 +000096 // We want to scheduled AR defs as soon as possible to make sure they aren't
97 // put in a different ALU clause from their uses.
98 if (!SU && !UnscheduledARDefs.empty()) {
99 SU = UnscheduledARDefs[0];
100 UnscheduledARDefs.erase(UnscheduledARDefs.begin());
101 NextInstKind = IDAlu;
102 }
103
104 if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
105 (!AllowSwitchFromAlu && CurInstKind == IDAlu))) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000106 // try to pick ALU
107 SU = pickAlu();
Vincent Lejeune4b5b8492013-06-05 20:27:35 +0000108 if (!SU && !PhysicalRegCopy.empty()) {
109 SU = PhysicalRegCopy.front();
110 PhysicalRegCopy.erase(PhysicalRegCopy.begin());
111 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000112 if (SU) {
Vincent Lejeunef9f4e1e2013-05-17 16:49:55 +0000113 if (CurEmitted >= InstKindLimit[IDAlu])
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000114 CurEmitted = 0;
115 NextInstKind = IDAlu;
116 }
117 }
118
119 if (!SU) {
120 // try to pick FETCH
121 SU = pickOther(IDFetch);
122 if (SU)
123 NextInstKind = IDFetch;
124 }
125
126 // try to pick other
127 if (!SU) {
128 SU = pickOther(IDOther);
129 if (SU)
130 NextInstKind = IDOther;
131 }
132
Tom Stellardaad53762013-06-05 03:43:06 +0000133 // We want to schedule the AR uses as late as possible to make sure that
134 // the AR defs have been released.
135 if (!SU && !UnscheduledARUses.empty()) {
136 SU = UnscheduledARUses[0];
137 UnscheduledARUses.erase(UnscheduledARUses.begin());
138 NextInstKind = IDAlu;
139 }
140
141
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000142 DEBUG(
143 if (SU) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000144 dbgs() << " ** Pick node **\n";
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000145 SU->dump(DAG);
146 } else {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000147 dbgs() << "NO NODE \n";
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000148 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
149 const SUnit &S = DAG->SUnits[i];
150 if (!S.isScheduled)
151 S.dump(DAG);
152 }
153 }
154 );
155
156 return SU;
157}
158
159void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000160 if (NextInstKind != CurInstKind) {
161 DEBUG(dbgs() << "Instruction Type Switch\n");
162 if (NextInstKind != IDAlu)
Vincent Lejeune77a83522013-06-29 19:32:43 +0000163 OccupedSlotsMask |= 31;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000164 CurEmitted = 0;
165 CurInstKind = NextInstKind;
166 }
167
168 if (CurInstKind == IDAlu) {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000169 AluInstCount ++;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000170 switch (getAluKind(SU)) {
171 case AluT_XYZW:
172 CurEmitted += 4;
173 break;
174 case AluDiscarded:
175 break;
176 default: {
177 ++CurEmitted;
178 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
179 E = SU->getInstr()->operands_end(); It != E; ++It) {
180 MachineOperand &MO = *It;
181 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
182 ++CurEmitted;
183 }
184 }
185 }
186 } else {
187 ++CurEmitted;
188 }
189
190
191 DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
192
193 if (CurInstKind != IDFetch) {
194 MoveUnits(Pending[IDFetch], Available[IDFetch]);
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000195 } else
196 FetchInstCount++;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000197}
198
Vincent Lejeune4b5b8492013-06-05 20:27:35 +0000199static bool
200isPhysicalRegCopy(MachineInstr *MI) {
201 if (MI->getOpcode() != AMDGPU::COPY)
202 return false;
203
204 return !TargetRegisterInfo::isVirtualRegister(MI->getOperand(1).getReg());
205}
206
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000207void R600SchedStrategy::releaseTopNode(SUnit *SU) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000208 DEBUG(dbgs() << "Top Releasing ";SU->dump(DAG););
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000209}
210
211void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000212 DEBUG(dbgs() << "Bottom Releasing ";SU->dump(DAG););
Vincent Lejeune4b5b8492013-06-05 20:27:35 +0000213 if (isPhysicalRegCopy(SU->getInstr())) {
214 PhysicalRegCopy.push_back(SU);
215 return;
216 }
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000217
218 int IK = getInstKind(SU);
Tom Stellardaad53762013-06-05 03:43:06 +0000219
220 // Check for AR register defines
221 for (MachineInstr::const_mop_iterator I = SU->getInstr()->operands_begin(),
222 E = SU->getInstr()->operands_end();
223 I != E; ++I) {
224 if (I->isReg() && I->getReg() == AMDGPU::AR_X) {
225 if (I->isDef()) {
226 UnscheduledARDefs.push_back(SU);
227 } else {
228 UnscheduledARUses.push_back(SU);
229 }
230 return;
231 }
232 }
233
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000234 // There is no export clause, we can schedule one as soon as its ready
235 if (IK == IDOther)
236 Available[IDOther].push_back(SU);
237 else
238 Pending[IK].push_back(SU);
239
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000240}
241
242bool R600SchedStrategy::regBelongsToClass(unsigned Reg,
243 const TargetRegisterClass *RC) const {
244 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
245 return RC->contains(Reg);
246 } else {
247 return MRI->getRegClass(Reg) == RC;
248 }
249}
250
251R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
252 MachineInstr *MI = SU->getInstr();
253
Vincent Lejeune77a83522013-06-29 19:32:43 +0000254 if (TII->isTransOnly(MI))
255 return AluTrans;
256
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000257 switch (MI->getOpcode()) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000258 case AMDGPU::PRED_X:
259 return AluPredX;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000260 case AMDGPU::INTERP_PAIR_XY:
261 case AMDGPU::INTERP_PAIR_ZW:
262 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000263 case AMDGPU::DOT_4:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000264 return AluT_XYZW;
265 case AMDGPU::COPY:
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000266 if (MI->getOperand(1).isUndef()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000267 // MI will become a KILL, don't considers it in scheduling
268 return AluDiscarded;
269 }
270 default:
271 break;
272 }
273
274 // Does the instruction take a whole IG ?
Tom Stellardce540332013-06-28 15:46:59 +0000275 // XXX: Is it possible to add a helper function in R600InstrInfo that can
276 // be used here and in R600PacketizerList::isSoloInstruction() ?
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000277 if(TII->isVector(*MI) ||
278 TII->isCubeOp(MI->getOpcode()) ||
Tom Stellardce540332013-06-28 15:46:59 +0000279 TII->isReductionOp(MI->getOpcode()) ||
280 MI->getOpcode() == AMDGPU::GROUP_BARRIER) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000281 return AluT_XYZW;
Tom Stellardce540332013-06-28 15:46:59 +0000282 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000283
Tom Stellardc026e8b2013-06-28 15:47:08 +0000284 if (TII->isLDSInstr(MI->getOpcode())) {
285 return AluT_X;
286 }
287
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000288 // Is the result already assigned to a channel ?
289 unsigned DestSubReg = MI->getOperand(0).getSubReg();
290 switch (DestSubReg) {
291 case AMDGPU::sub0:
292 return AluT_X;
293 case AMDGPU::sub1:
294 return AluT_Y;
295 case AMDGPU::sub2:
296 return AluT_Z;
297 case AMDGPU::sub3:
298 return AluT_W;
299 default:
300 break;
301 }
302
303 // Is the result already member of a X/Y/Z/W class ?
304 unsigned DestReg = MI->getOperand(0).getReg();
305 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) ||
306 regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass))
307 return AluT_X;
308 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass))
309 return AluT_Y;
310 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass))
311 return AluT_Z;
312 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass))
313 return AluT_W;
314 if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass))
315 return AluT_XYZW;
316
317 return AluAny;
318
319}
320
321int R600SchedStrategy::getInstKind(SUnit* SU) {
322 int Opcode = SU->getInstr()->getOpcode();
323
Vincent Lejeunee958c8e2013-05-17 16:50:37 +0000324 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
325 return IDFetch;
326
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000327 if (TII->isALUInstr(Opcode)) {
328 return IDAlu;
329 }
330
331 switch (Opcode) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000332 case AMDGPU::PRED_X:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000333 case AMDGPU::COPY:
334 case AMDGPU::CONST_COPY:
335 case AMDGPU::INTERP_PAIR_XY:
336 case AMDGPU::INTERP_PAIR_ZW:
337 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune519f21e2013-05-17 16:50:32 +0000338 case AMDGPU::DOT_4:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000339 return IDAlu;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000340 default:
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000341 return IDOther;
342 }
343}
344
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000345SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000346 if (Q.empty())
347 return NULL;
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000348 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000349 It != E; ++It) {
350 SUnit *SU = *It;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000351 InstructionsGroupCandidate.push_back(SU->getInstr());
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000352 if (TII->fitsConstReadLimitations(InstructionsGroupCandidate)
353 && (!AnyALU || !TII->isVectorOnly(SU->getInstr()))
354 ) {
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000355 InstructionsGroupCandidate.pop_back();
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000356 Q.erase((It + 1).base());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000357 return SU;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000358 } else {
359 InstructionsGroupCandidate.pop_back();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000360 }
361 }
362 return NULL;
363}
364
365void R600SchedStrategy::LoadAlu() {
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000366 std::vector<SUnit *> &QSrc = Pending[IDAlu];
367 for (unsigned i = 0, e = QSrc.size(); i < e; ++i) {
368 AluKind AK = getAluKind(QSrc[i]);
369 AvailableAlus[AK].push_back(QSrc[i]);
370 }
371 QSrc.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000372}
373
374void R600SchedStrategy::PrepareNextSlot() {
375 DEBUG(dbgs() << "New Slot\n");
376 assert (OccupedSlotsMask && "Slot wasn't filled");
377 OccupedSlotsMask = 0;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000378// if (HwGen == AMDGPUSubtarget::NORTHERN_ISLANDS)
379// OccupedSlotsMask |= 16;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000380 InstructionsGroupCandidate.clear();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000381 LoadAlu();
382}
383
384void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000385 int DstIndex = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
386 if (DstIndex == -1) {
387 return;
388 }
389 unsigned DestReg = MI->getOperand(DstIndex).getReg();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000390 // PressureRegister crashes if an operand is def and used in the same inst
391 // and we try to constraint its regclass
392 for (MachineInstr::mop_iterator It = MI->operands_begin(),
393 E = MI->operands_end(); It != E; ++It) {
394 MachineOperand &MO = *It;
395 if (MO.isReg() && !MO.isDef() &&
Tom Stellardc026e8b2013-06-28 15:47:08 +0000396 MO.getReg() == DestReg)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000397 return;
398 }
399 // Constrains the regclass of DestReg to assign it to Slot
400 switch (Slot) {
401 case 0:
402 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
403 break;
404 case 1:
405 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
406 break;
407 case 2:
408 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
409 break;
410 case 3:
411 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
412 break;
413 }
414}
415
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000416SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000417 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000418 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000419 if (SlotedSU)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000420 return SlotedSU;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000421 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000422 if (UnslotedSU)
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000423 AssignSlot(UnslotedSU->getInstr(), Slot);
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000424 return UnslotedSU;
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000425}
426
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000427unsigned R600SchedStrategy::AvailablesAluCount() const {
428 return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() +
429 AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() +
430 AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() +
Vincent Lejeune77a83522013-06-29 19:32:43 +0000431 AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() +
432 AvailableAlus[AluPredX].size();
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000433}
434
435SUnit* R600SchedStrategy::pickAlu() {
Vincent Lejeuned1a9d182013-06-07 23:30:34 +0000436 while (AvailablesAluCount() || !Pending[IDAlu].empty()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000437 if (!OccupedSlotsMask) {
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000438 // Bottom up scheduling : predX must comes first
439 if (!AvailableAlus[AluPredX].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000440 OccupedSlotsMask |= 31;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000441 return PopInst(AvailableAlus[AluPredX], false);
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000442 }
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000443 // Flush physical reg copies (RA will discard them)
444 if (!AvailableAlus[AluDiscarded].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000445 OccupedSlotsMask |= 31;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000446 return PopInst(AvailableAlus[AluDiscarded], false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000447 }
448 // If there is a T_XYZW alu available, use it
449 if (!AvailableAlus[AluT_XYZW].empty()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000450 OccupedSlotsMask |= 15;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000451 return PopInst(AvailableAlus[AluT_XYZW], false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000452 }
453 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000454 bool TransSlotOccuped = OccupedSlotsMask & 16;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000455 if (!TransSlotOccuped && VLIW5) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000456 if (!AvailableAlus[AluTrans].empty()) {
457 OccupedSlotsMask |= 16;
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000458 return PopInst(AvailableAlus[AluTrans], false);
459 }
460 SUnit *SU = AttemptFillSlot(3, true);
461 if (SU) {
462 OccupedSlotsMask |= 16;
463 return SU;
Vincent Lejeune77a83522013-06-29 19:32:43 +0000464 }
465 }
Vincent Lejeune3d5118c2013-05-17 16:50:56 +0000466 for (int Chan = 3; Chan > -1; --Chan) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000467 bool isOccupied = OccupedSlotsMask & (1 << Chan);
468 if (!isOccupied) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000469 SUnit *SU = AttemptFillSlot(Chan, false);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000470 if (SU) {
471 OccupedSlotsMask |= (1 << Chan);
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000472 InstructionsGroupCandidate.push_back(SU->getInstr());
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000473 return SU;
474 }
475 }
476 }
477 PrepareNextSlot();
478 }
479 return NULL;
480}
481
482SUnit* R600SchedStrategy::pickOther(int QID) {
483 SUnit *SU = 0;
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000484 std::vector<SUnit *> &AQ = Available[QID];
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000485
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000486 if (AQ.empty()) {
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000487 MoveUnits(Pending[QID], AQ);
488 }
Vincent Lejeune4c81d4d2013-05-17 16:50:44 +0000489 if (!AQ.empty()) {
490 SU = AQ.back();
491 AQ.resize(AQ.size() - 1);
Vincent Lejeune68b6b6d2013-03-05 18:41:32 +0000492 }
493 return SU;
494}
495