blob: e4a78bf8cc4223c3b3ad5e7c81e078c9bdb7d81b [file] [log] [blame]
Vincent Lejeune62f38ca2013-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
12// TODO: Scheduling is optimised for VLIW4 arch, modify it to support TRANS slot
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "misched"
17
18#include "R600MachineScheduler.h"
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000021#include "llvm/Pass.h"
22#include "llvm/PassManager.h"
NAKAMURA Takumi3f179b52013-03-11 08:19:28 +000023#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi3f179b52013-03-11 08:19:28 +000024
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000025using namespace llvm;
26
27void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
28
29 DAG = dag;
30 TII = static_cast<const R600InstrInfo*>(DAG->TII);
31 TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
32 MRI = &DAG->MRI;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000033 CurInstKind = IDOther;
34 CurEmitted = 0;
35 OccupedSlotsMask = 15;
Vincent Lejeunedae2a202013-04-03 16:49:34 +000036 InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
Vincent Lejeune76fc2d02013-05-17 16:50:56 +000037 InstKindLimit[IDOther] = 32;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000038
39 const AMDGPUSubtarget &ST = DAG->TM.getSubtarget<AMDGPUSubtarget>();
Vincent Lejeunedcfcf1d2013-05-17 16:49:55 +000040 InstKindLimit[IDFetch] = ST.getTexVTXClauseSize();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000041}
42
Vincent Lejeune21ca0b32013-05-17 16:50:44 +000043void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc,
44 std::vector<SUnit *> &QDst)
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000045{
Vincent Lejeune21ca0b32013-05-17 16:50:44 +000046 QDst.insert(QDst.end(), QSrc.begin(), QSrc.end());
47 QSrc.clear();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000048}
49
50SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
51 SUnit *SU = 0;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000052 NextInstKind = IDOther;
53
Vincent Lejeune76fc2d02013-05-17 16:50:56 +000054 IsTopNode = false;
55
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000056 // check if we might want to switch current clause type
Vincent Lejeune76fc2d02013-05-17 16:50:56 +000057 bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) ||
Vincent Lejeune21ca0b32013-05-17 16:50:44 +000058 (Available[CurInstKind].empty());
Vincent Lejeunedcfcf1d2013-05-17 16:49:55 +000059 bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) &&
Vincent Lejeune21ca0b32013-05-17 16:50:44 +000060 (!Available[IDFetch].empty() || !Available[IDOther].empty());
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000061
Tom Stellardad7ecc62013-06-05 03:43:06 +000062 // We want to scheduled AR defs as soon as possible to make sure they aren't
63 // put in a different ALU clause from their uses.
64 if (!SU && !UnscheduledARDefs.empty()) {
65 SU = UnscheduledARDefs[0];
66 UnscheduledARDefs.erase(UnscheduledARDefs.begin());
67 NextInstKind = IDAlu;
68 }
69
70 if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
71 (!AllowSwitchFromAlu && CurInstKind == IDAlu))) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000072 // try to pick ALU
73 SU = pickAlu();
Vincent Lejeune51211972013-06-05 20:27:35 +000074 if (!SU && !PhysicalRegCopy.empty()) {
75 SU = PhysicalRegCopy.front();
76 PhysicalRegCopy.erase(PhysicalRegCopy.begin());
77 }
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000078 if (SU) {
Vincent Lejeunedcfcf1d2013-05-17 16:49:55 +000079 if (CurEmitted >= InstKindLimit[IDAlu])
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000080 CurEmitted = 0;
81 NextInstKind = IDAlu;
82 }
83 }
84
85 if (!SU) {
86 // try to pick FETCH
87 SU = pickOther(IDFetch);
88 if (SU)
89 NextInstKind = IDFetch;
90 }
91
92 // try to pick other
93 if (!SU) {
94 SU = pickOther(IDOther);
95 if (SU)
96 NextInstKind = IDOther;
97 }
98
Tom Stellardad7ecc62013-06-05 03:43:06 +000099 // We want to schedule the AR uses as late as possible to make sure that
100 // the AR defs have been released.
101 if (!SU && !UnscheduledARUses.empty()) {
102 SU = UnscheduledARUses[0];
103 UnscheduledARUses.erase(UnscheduledARUses.begin());
104 NextInstKind = IDAlu;
105 }
106
107
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000108 DEBUG(
109 if (SU) {
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000110 dbgs() << " ** Pick node **\n";
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000111 SU->dump(DAG);
112 } else {
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000113 dbgs() << "NO NODE \n";
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000114 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
115 const SUnit &S = DAG->SUnits[i];
116 if (!S.isScheduled)
117 S.dump(DAG);
118 }
119 }
120 );
121
122 return SU;
123}
124
125void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000126 if (NextInstKind != CurInstKind) {
127 DEBUG(dbgs() << "Instruction Type Switch\n");
128 if (NextInstKind != IDAlu)
129 OccupedSlotsMask = 15;
130 CurEmitted = 0;
131 CurInstKind = NextInstKind;
132 }
133
134 if (CurInstKind == IDAlu) {
135 switch (getAluKind(SU)) {
136 case AluT_XYZW:
137 CurEmitted += 4;
138 break;
139 case AluDiscarded:
140 break;
141 default: {
142 ++CurEmitted;
143 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
144 E = SU->getInstr()->operands_end(); It != E; ++It) {
145 MachineOperand &MO = *It;
146 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
147 ++CurEmitted;
148 }
149 }
150 }
151 } else {
152 ++CurEmitted;
153 }
154
155
156 DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
157
158 if (CurInstKind != IDFetch) {
159 MoveUnits(Pending[IDFetch], Available[IDFetch]);
160 }
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000161}
162
Vincent Lejeune51211972013-06-05 20:27:35 +0000163static bool
164isPhysicalRegCopy(MachineInstr *MI) {
165 if (MI->getOpcode() != AMDGPU::COPY)
166 return false;
167
168 return !TargetRegisterInfo::isVirtualRegister(MI->getOperand(1).getReg());
169}
170
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000171void R600SchedStrategy::releaseTopNode(SUnit *SU) {
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000172 DEBUG(dbgs() << "Top Releasing ";SU->dump(DAG););
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000173}
174
175void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000176 DEBUG(dbgs() << "Bottom Releasing ";SU->dump(DAG););
Vincent Lejeune51211972013-06-05 20:27:35 +0000177 if (isPhysicalRegCopy(SU->getInstr())) {
178 PhysicalRegCopy.push_back(SU);
179 return;
180 }
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000181
182 int IK = getInstKind(SU);
Tom Stellardad7ecc62013-06-05 03:43:06 +0000183
184 // Check for AR register defines
185 for (MachineInstr::const_mop_iterator I = SU->getInstr()->operands_begin(),
186 E = SU->getInstr()->operands_end();
187 I != E; ++I) {
188 if (I->isReg() && I->getReg() == AMDGPU::AR_X) {
189 if (I->isDef()) {
190 UnscheduledARDefs.push_back(SU);
191 } else {
192 UnscheduledARUses.push_back(SU);
193 }
194 return;
195 }
196 }
197
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000198 // There is no export clause, we can schedule one as soon as its ready
199 if (IK == IDOther)
200 Available[IDOther].push_back(SU);
201 else
202 Pending[IK].push_back(SU);
203
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000204}
205
206bool R600SchedStrategy::regBelongsToClass(unsigned Reg,
207 const TargetRegisterClass *RC) const {
208 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
209 return RC->contains(Reg);
210 } else {
211 return MRI->getRegClass(Reg) == RC;
212 }
213}
214
215R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
216 MachineInstr *MI = SU->getInstr();
217
218 switch (MI->getOpcode()) {
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000219 case AMDGPU::PRED_X:
220 return AluPredX;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000221 case AMDGPU::INTERP_PAIR_XY:
222 case AMDGPU::INTERP_PAIR_ZW:
223 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune4ed99172013-05-17 16:50:32 +0000224 case AMDGPU::DOT_4:
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000225 return AluT_XYZW;
226 case AMDGPU::COPY:
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000227 if (MI->getOperand(1).isUndef()) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000228 // MI will become a KILL, don't considers it in scheduling
229 return AluDiscarded;
230 }
231 default:
232 break;
233 }
234
235 // Does the instruction take a whole IG ?
236 if(TII->isVector(*MI) ||
237 TII->isCubeOp(MI->getOpcode()) ||
238 TII->isReductionOp(MI->getOpcode()))
239 return AluT_XYZW;
240
241 // Is the result already assigned to a channel ?
242 unsigned DestSubReg = MI->getOperand(0).getSubReg();
243 switch (DestSubReg) {
244 case AMDGPU::sub0:
245 return AluT_X;
246 case AMDGPU::sub1:
247 return AluT_Y;
248 case AMDGPU::sub2:
249 return AluT_Z;
250 case AMDGPU::sub3:
251 return AluT_W;
252 default:
253 break;
254 }
255
256 // Is the result already member of a X/Y/Z/W class ?
257 unsigned DestReg = MI->getOperand(0).getReg();
258 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) ||
259 regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass))
260 return AluT_X;
261 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass))
262 return AluT_Y;
263 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass))
264 return AluT_Z;
265 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass))
266 return AluT_W;
267 if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass))
268 return AluT_XYZW;
269
270 return AluAny;
271
272}
273
274int R600SchedStrategy::getInstKind(SUnit* SU) {
275 int Opcode = SU->getInstr()->getOpcode();
276
Vincent Lejeunef63f85a2013-05-17 16:50:37 +0000277 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
278 return IDFetch;
279
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000280 if (TII->isALUInstr(Opcode)) {
281 return IDAlu;
282 }
283
284 switch (Opcode) {
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000285 case AMDGPU::PRED_X:
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000286 case AMDGPU::COPY:
287 case AMDGPU::CONST_COPY:
288 case AMDGPU::INTERP_PAIR_XY:
289 case AMDGPU::INTERP_PAIR_ZW:
290 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune4ed99172013-05-17 16:50:32 +0000291 case AMDGPU::DOT_4:
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000292 return IDAlu;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000293 default:
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000294 return IDOther;
295 }
296}
297
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000298SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000299 if (Q.empty())
300 return NULL;
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000301 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000302 It != E; ++It) {
303 SUnit *SU = *It;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000304 InstructionsGroupCandidate.push_back(SU->getInstr());
305 if (TII->canBundle(InstructionsGroupCandidate)) {
306 InstructionsGroupCandidate.pop_back();
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000307 Q.erase((It + 1).base());
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000308 return SU;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000309 } else {
310 InstructionsGroupCandidate.pop_back();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000311 }
312 }
313 return NULL;
314}
315
316void R600SchedStrategy::LoadAlu() {
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000317 std::vector<SUnit *> &QSrc = Pending[IDAlu];
318 for (unsigned i = 0, e = QSrc.size(); i < e; ++i) {
319 AluKind AK = getAluKind(QSrc[i]);
320 AvailableAlus[AK].push_back(QSrc[i]);
321 }
322 QSrc.clear();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000323}
324
325void R600SchedStrategy::PrepareNextSlot() {
326 DEBUG(dbgs() << "New Slot\n");
327 assert (OccupedSlotsMask && "Slot wasn't filled");
328 OccupedSlotsMask = 0;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000329 InstructionsGroupCandidate.clear();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000330 LoadAlu();
331}
332
333void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
334 unsigned DestReg = MI->getOperand(0).getReg();
335 // PressureRegister crashes if an operand is def and used in the same inst
336 // and we try to constraint its regclass
337 for (MachineInstr::mop_iterator It = MI->operands_begin(),
338 E = MI->operands_end(); It != E; ++It) {
339 MachineOperand &MO = *It;
340 if (MO.isReg() && !MO.isDef() &&
341 MO.getReg() == MI->getOperand(0).getReg())
342 return;
343 }
344 // Constrains the regclass of DestReg to assign it to Slot
345 switch (Slot) {
346 case 0:
347 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
348 break;
349 case 1:
350 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
351 break;
352 case 2:
353 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
354 break;
355 case 3:
356 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
357 break;
358 }
359}
360
361SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot) {
362 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
363 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]]);
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000364 if (SlotedSU)
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000365 return SlotedSU;
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000366 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny]);
367 if (UnslotedSU)
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000368 AssignSlot(UnslotedSU->getInstr(), Slot);
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000369 return UnslotedSU;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000370}
371
372bool R600SchedStrategy::isAvailablesAluEmpty() const {
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000373 return Pending[IDAlu].empty() && AvailableAlus[AluAny].empty() &&
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000374 AvailableAlus[AluT_XYZW].empty() && AvailableAlus[AluT_X].empty() &&
375 AvailableAlus[AluT_Y].empty() && AvailableAlus[AluT_Z].empty() &&
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000376 AvailableAlus[AluT_W].empty() && AvailableAlus[AluDiscarded].empty() &&
377 AvailableAlus[AluPredX].empty();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000378}
379
380SUnit* R600SchedStrategy::pickAlu() {
381 while (!isAvailablesAluEmpty()) {
382 if (!OccupedSlotsMask) {
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000383 // Bottom up scheduling : predX must comes first
384 if (!AvailableAlus[AluPredX].empty()) {
385 OccupedSlotsMask = 15;
386 return PopInst(AvailableAlus[AluPredX]);
387 }
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000388 // Flush physical reg copies (RA will discard them)
389 if (!AvailableAlus[AluDiscarded].empty()) {
390 OccupedSlotsMask = 15;
391 return PopInst(AvailableAlus[AluDiscarded]);
392 }
393 // If there is a T_XYZW alu available, use it
394 if (!AvailableAlus[AluT_XYZW].empty()) {
395 OccupedSlotsMask = 15;
396 return PopInst(AvailableAlus[AluT_XYZW]);
397 }
398 }
Vincent Lejeune76fc2d02013-05-17 16:50:56 +0000399 for (int Chan = 3; Chan > -1; --Chan) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000400 bool isOccupied = OccupedSlotsMask & (1 << Chan);
401 if (!isOccupied) {
402 SUnit *SU = AttemptFillSlot(Chan);
403 if (SU) {
404 OccupedSlotsMask |= (1 << Chan);
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000405 InstructionsGroupCandidate.push_back(SU->getInstr());
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000406 return SU;
407 }
408 }
409 }
410 PrepareNextSlot();
411 }
412 return NULL;
413}
414
415SUnit* R600SchedStrategy::pickOther(int QID) {
416 SUnit *SU = 0;
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000417 std::vector<SUnit *> &AQ = Available[QID];
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000418
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000419 if (AQ.empty()) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000420 MoveUnits(Pending[QID], AQ);
421 }
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000422 if (!AQ.empty()) {
423 SU = AQ.back();
424 AQ.resize(AQ.size() - 1);
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000425 }
426 return SU;
427}
428