blob: aeb2674f4e65db81a395ef843969e06ceb3f0ce1 [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"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/CodeGen/LiveIntervalAnalysis.h"
21#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 Lejeune62f38ca2013-03-05 18:41:32 +000037
38
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;
52 IsTopNode = true;
53 NextInstKind = IDOther;
54
55 // check if we might want to switch current clause type
56 bool AllowSwitchToAlu = (CurInstKind == IDOther) ||
Vincent Lejeunedcfcf1d2013-05-17 16:49:55 +000057 (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
62 if ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
63 (!AllowSwitchFromAlu && CurInstKind == IDAlu)) {
64 // try to pick ALU
65 SU = pickAlu();
66 if (SU) {
Vincent Lejeunedcfcf1d2013-05-17 16:49:55 +000067 if (CurEmitted >= InstKindLimit[IDAlu])
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000068 CurEmitted = 0;
69 NextInstKind = IDAlu;
70 }
71 }
72
73 if (!SU) {
74 // try to pick FETCH
75 SU = pickOther(IDFetch);
76 if (SU)
77 NextInstKind = IDFetch;
78 }
79
80 // try to pick other
81 if (!SU) {
82 SU = pickOther(IDOther);
83 if (SU)
84 NextInstKind = IDOther;
85 }
86
87 DEBUG(
88 if (SU) {
89 dbgs() << "picked node: ";
90 SU->dump(DAG);
91 } else {
92 dbgs() << "NO NODE ";
Vincent Lejeune62f38ca2013-03-05 18:41:32 +000093 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
94 const SUnit &S = DAG->SUnits[i];
95 if (!S.isScheduled)
96 S.dump(DAG);
97 }
98 }
99 );
100
101 return SU;
102}
103
104void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
105
106 DEBUG(dbgs() << "scheduled: ");
107 DEBUG(SU->dump(DAG));
108
109 if (NextInstKind != CurInstKind) {
110 DEBUG(dbgs() << "Instruction Type Switch\n");
111 if (NextInstKind != IDAlu)
112 OccupedSlotsMask = 15;
113 CurEmitted = 0;
114 CurInstKind = NextInstKind;
115 }
116
117 if (CurInstKind == IDAlu) {
118 switch (getAluKind(SU)) {
119 case AluT_XYZW:
120 CurEmitted += 4;
121 break;
122 case AluDiscarded:
123 break;
124 default: {
125 ++CurEmitted;
126 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
127 E = SU->getInstr()->operands_end(); It != E; ++It) {
128 MachineOperand &MO = *It;
129 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
130 ++CurEmitted;
131 }
132 }
133 }
134 } else {
135 ++CurEmitted;
136 }
137
138
139 DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
140
141 if (CurInstKind != IDFetch) {
142 MoveUnits(Pending[IDFetch], Available[IDFetch]);
143 }
144 MoveUnits(Pending[IDOther], Available[IDOther]);
145}
146
147void R600SchedStrategy::releaseTopNode(SUnit *SU) {
148 int IK = getInstKind(SU);
149
150 DEBUG(dbgs() << IK << " <= ");
151 DEBUG(SU->dump(DAG));
152
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000153 Pending[IK].push_back(SU);
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000154}
155
156void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
157}
158
159bool R600SchedStrategy::regBelongsToClass(unsigned Reg,
160 const TargetRegisterClass *RC) const {
161 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
162 return RC->contains(Reg);
163 } else {
164 return MRI->getRegClass(Reg) == RC;
165 }
166}
167
168R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
169 MachineInstr *MI = SU->getInstr();
170
171 switch (MI->getOpcode()) {
172 case AMDGPU::INTERP_PAIR_XY:
173 case AMDGPU::INTERP_PAIR_ZW:
174 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune4ed99172013-05-17 16:50:32 +0000175 case AMDGPU::DOT_4:
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000176 return AluT_XYZW;
177 case AMDGPU::COPY:
178 if (TargetRegisterInfo::isPhysicalRegister(MI->getOperand(1).getReg())) {
179 // %vregX = COPY Tn_X is likely to be discarded in favor of an
180 // assignement of Tn_X to %vregX, don't considers it in scheduling
181 return AluDiscarded;
182 }
183 else if (MI->getOperand(1).isUndef()) {
184 // MI will become a KILL, don't considers it in scheduling
185 return AluDiscarded;
186 }
187 default:
188 break;
189 }
190
191 // Does the instruction take a whole IG ?
192 if(TII->isVector(*MI) ||
193 TII->isCubeOp(MI->getOpcode()) ||
194 TII->isReductionOp(MI->getOpcode()))
195 return AluT_XYZW;
196
197 // Is the result already assigned to a channel ?
198 unsigned DestSubReg = MI->getOperand(0).getSubReg();
199 switch (DestSubReg) {
200 case AMDGPU::sub0:
201 return AluT_X;
202 case AMDGPU::sub1:
203 return AluT_Y;
204 case AMDGPU::sub2:
205 return AluT_Z;
206 case AMDGPU::sub3:
207 return AluT_W;
208 default:
209 break;
210 }
211
212 // Is the result already member of a X/Y/Z/W class ?
213 unsigned DestReg = MI->getOperand(0).getReg();
214 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) ||
215 regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass))
216 return AluT_X;
217 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass))
218 return AluT_Y;
219 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass))
220 return AluT_Z;
221 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass))
222 return AluT_W;
223 if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass))
224 return AluT_XYZW;
225
226 return AluAny;
227
228}
229
230int R600SchedStrategy::getInstKind(SUnit* SU) {
231 int Opcode = SU->getInstr()->getOpcode();
232
Vincent Lejeunef63f85a2013-05-17 16:50:37 +0000233 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
234 return IDFetch;
235
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000236 if (TII->isALUInstr(Opcode)) {
237 return IDAlu;
238 }
239
240 switch (Opcode) {
241 case AMDGPU::COPY:
242 case AMDGPU::CONST_COPY:
243 case AMDGPU::INTERP_PAIR_XY:
244 case AMDGPU::INTERP_PAIR_ZW:
245 case AMDGPU::INTERP_VEC_LOAD:
Vincent Lejeune4ed99172013-05-17 16:50:32 +0000246 case AMDGPU::DOT_4:
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000247 return IDAlu;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000248 default:
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000249 return IDOther;
250 }
251}
252
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000253SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000254 if (Q.empty())
255 return NULL;
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000256 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000257 It != E; ++It) {
258 SUnit *SU = *It;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000259 InstructionsGroupCandidate.push_back(SU->getInstr());
260 if (TII->canBundle(InstructionsGroupCandidate)) {
261 InstructionsGroupCandidate.pop_back();
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000262 Q.erase((It + 1).base());
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000263 return SU;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000264 } else {
265 InstructionsGroupCandidate.pop_back();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000266 }
267 }
268 return NULL;
269}
270
271void R600SchedStrategy::LoadAlu() {
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000272 std::vector<SUnit *> &QSrc = Pending[IDAlu];
273 for (unsigned i = 0, e = QSrc.size(); i < e; ++i) {
274 AluKind AK = getAluKind(QSrc[i]);
275 AvailableAlus[AK].push_back(QSrc[i]);
276 }
277 QSrc.clear();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000278}
279
280void R600SchedStrategy::PrepareNextSlot() {
281 DEBUG(dbgs() << "New Slot\n");
282 assert (OccupedSlotsMask && "Slot wasn't filled");
283 OccupedSlotsMask = 0;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000284 InstructionsGroupCandidate.clear();
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000285 LoadAlu();
286}
287
288void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
289 unsigned DestReg = MI->getOperand(0).getReg();
290 // PressureRegister crashes if an operand is def and used in the same inst
291 // and we try to constraint its regclass
292 for (MachineInstr::mop_iterator It = MI->operands_begin(),
293 E = MI->operands_end(); It != E; ++It) {
294 MachineOperand &MO = *It;
295 if (MO.isReg() && !MO.isDef() &&
296 MO.getReg() == MI->getOperand(0).getReg())
297 return;
298 }
299 // Constrains the regclass of DestReg to assign it to Slot
300 switch (Slot) {
301 case 0:
302 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
303 break;
304 case 1:
305 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
306 break;
307 case 2:
308 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
309 break;
310 case 3:
311 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
312 break;
313 }
314}
315
316SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot) {
317 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
318 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]]);
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000319 if (SlotedSU)
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000320 return SlotedSU;
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000321 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny]);
322 if (UnslotedSU)
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000323 AssignSlot(UnslotedSU->getInstr(), Slot);
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000324 return UnslotedSU;
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000325}
326
327bool R600SchedStrategy::isAvailablesAluEmpty() const {
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000328 return Pending[IDAlu].empty() && AvailableAlus[AluAny].empty() &&
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000329 AvailableAlus[AluT_XYZW].empty() && AvailableAlus[AluT_X].empty() &&
330 AvailableAlus[AluT_Y].empty() && AvailableAlus[AluT_Z].empty() &&
331 AvailableAlus[AluT_W].empty() && AvailableAlus[AluDiscarded].empty();
332}
333
334SUnit* R600SchedStrategy::pickAlu() {
335 while (!isAvailablesAluEmpty()) {
336 if (!OccupedSlotsMask) {
337 // Flush physical reg copies (RA will discard them)
338 if (!AvailableAlus[AluDiscarded].empty()) {
339 OccupedSlotsMask = 15;
340 return PopInst(AvailableAlus[AluDiscarded]);
341 }
342 // If there is a T_XYZW alu available, use it
343 if (!AvailableAlus[AluT_XYZW].empty()) {
344 OccupedSlotsMask = 15;
345 return PopInst(AvailableAlus[AluT_XYZW]);
346 }
347 }
348 for (unsigned Chan = 0; Chan < 4; ++Chan) {
349 bool isOccupied = OccupedSlotsMask & (1 << Chan);
350 if (!isOccupied) {
351 SUnit *SU = AttemptFillSlot(Chan);
352 if (SU) {
353 OccupedSlotsMask |= (1 << Chan);
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000354 InstructionsGroupCandidate.push_back(SU->getInstr());
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000355 return SU;
356 }
357 }
358 }
359 PrepareNextSlot();
360 }
361 return NULL;
362}
363
364SUnit* R600SchedStrategy::pickOther(int QID) {
365 SUnit *SU = 0;
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000366 std::vector<SUnit *> &AQ = Available[QID];
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000367
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000368 if (AQ.empty()) {
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000369 MoveUnits(Pending[QID], AQ);
370 }
Vincent Lejeune21ca0b32013-05-17 16:50:44 +0000371 if (!AQ.empty()) {
372 SU = AQ.back();
373 AQ.resize(AQ.size() - 1);
Vincent Lejeune62f38ca2013-03-05 18:41:32 +0000374 }
375 return SU;
376}
377