1 //===-- R600MachineScheduler.cpp - R600 Scheduler Interface -*- C++ -*-----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief R600 Machine Scheduler interface
12 // TODO: Scheduling is optimised for VLIW4 arch, modify it to support TRANS slot
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "misched"
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"
23 #include "llvm/Support/raw_ostream.h"
28 void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
31 TII = static_cast<const R600InstrInfo*>(DAG->TII);
32 TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
34 Available[IDAlu]->clear();
35 Available[IDFetch]->clear();
36 Available[IDOther]->clear();
37 CurInstKind = IDOther;
39 OccupedSlotsMask = 15;
40 memset(InstructionsGroupCandidate, 0, sizeof(InstructionsGroupCandidate));
41 InstKindLimit[IDAlu] = 120; // 120 minus 8 for security
44 const AMDGPUSubtarget &ST = DAG->TM.getSubtarget<AMDGPUSubtarget>();
45 if (ST.device()->getGeneration() <= AMDGPUDeviceInfo::HD5XXX) {
46 InstKindLimit[IDFetch] = 7; // 8 minus 1 for security
48 InstKindLimit[IDFetch] = 15; // 16 minus 1 for security
52 void R600SchedStrategy::MoveUnits(ReadyQueue *QSrc, ReadyQueue *QDst)
56 for (ReadyQueue::iterator I = QSrc->begin(),
57 E = QSrc->end(); I != E; ++I) {
58 (*I)->NodeQueueId &= ~QSrc->getID();
64 SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
67 NextInstKind = IDOther;
69 // check if we might want to switch current clause type
70 bool AllowSwitchToAlu = (CurInstKind == IDOther) ||
71 (CurEmitted > InstKindLimit[CurInstKind]) ||
72 (Available[CurInstKind]->empty());
73 bool AllowSwitchFromAlu = (CurEmitted > InstKindLimit[CurInstKind]) &&
74 (!Available[IDFetch]->empty() || !Available[IDOther]->empty());
76 if ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
77 (!AllowSwitchFromAlu && CurInstKind == IDAlu)) {
81 if (CurEmitted > InstKindLimit[IDAlu])
89 SU = pickOther(IDFetch);
91 NextInstKind = IDFetch;
96 SU = pickOther(IDOther);
98 NextInstKind = IDOther;
103 dbgs() << "picked node: ";
106 dbgs() << "NO NODE ";
107 for (int i = 0; i < IDLast; ++i) {
108 Available[i]->dump();
111 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
112 const SUnit &S = DAG->SUnits[i];
122 void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
124 DEBUG(dbgs() << "scheduled: ");
125 DEBUG(SU->dump(DAG));
127 if (NextInstKind != CurInstKind) {
128 DEBUG(dbgs() << "Instruction Type Switch\n");
129 if (NextInstKind != IDAlu)
130 OccupedSlotsMask = 15;
132 CurInstKind = NextInstKind;
135 if (CurInstKind == IDAlu) {
136 switch (getAluKind(SU)) {
144 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
145 E = SU->getInstr()->operands_end(); It != E; ++It) {
146 MachineOperand &MO = *It;
147 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
157 DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
159 if (CurInstKind != IDFetch) {
160 MoveUnits(Pending[IDFetch], Available[IDFetch]);
162 MoveUnits(Pending[IDOther], Available[IDOther]);
165 void R600SchedStrategy::releaseTopNode(SUnit *SU) {
166 int IK = getInstKind(SU);
168 DEBUG(dbgs() << IK << " <= ");
169 DEBUG(SU->dump(DAG));
171 Pending[IK]->push(SU);
174 void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
177 bool R600SchedStrategy::regBelongsToClass(unsigned Reg,
178 const TargetRegisterClass *RC) const {
179 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
180 return RC->contains(Reg);
182 return MRI->getRegClass(Reg) == RC;
186 R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
187 MachineInstr *MI = SU->getInstr();
189 switch (MI->getOpcode()) {
190 case AMDGPU::INTERP_PAIR_XY:
191 case AMDGPU::INTERP_PAIR_ZW:
192 case AMDGPU::INTERP_VEC_LOAD:
195 if (TargetRegisterInfo::isPhysicalRegister(MI->getOperand(1).getReg())) {
196 // %vregX = COPY Tn_X is likely to be discarded in favor of an
197 // assignement of Tn_X to %vregX, don't considers it in scheduling
200 else if (MI->getOperand(1).isUndef()) {
201 // MI will become a KILL, don't considers it in scheduling
208 // Does the instruction take a whole IG ?
209 if(TII->isVector(*MI) ||
210 TII->isCubeOp(MI->getOpcode()) ||
211 TII->isReductionOp(MI->getOpcode()))
214 // Is the result already assigned to a channel ?
215 unsigned DestSubReg = MI->getOperand(0).getSubReg();
216 switch (DestSubReg) {
229 // Is the result already member of a X/Y/Z/W class ?
230 unsigned DestReg = MI->getOperand(0).getReg();
231 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) ||
232 regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass))
234 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass))
236 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass))
238 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass))
240 if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass))
247 int R600SchedStrategy::getInstKind(SUnit* SU) {
248 int Opcode = SU->getInstr()->getOpcode();
250 if (TII->isALUInstr(Opcode)) {
256 case AMDGPU::CONST_COPY:
257 case AMDGPU::INTERP_PAIR_XY:
258 case AMDGPU::INTERP_PAIR_ZW:
259 case AMDGPU::INTERP_VEC_LOAD:
260 case AMDGPU::DOT4_eg_pseudo:
261 case AMDGPU::DOT4_r600_pseudo:
263 case AMDGPU::TEX_VTX_CONSTBUF:
264 case AMDGPU::TEX_VTX_TEXBUF:
266 case AMDGPU::TEX_GET_TEXTURE_RESINFO:
267 case AMDGPU::TEX_GET_GRADIENTS_H:
268 case AMDGPU::TEX_GET_GRADIENTS_V:
269 case AMDGPU::TEX_SET_GRADIENTS_H:
270 case AMDGPU::TEX_SET_GRADIENTS_V:
271 case AMDGPU::TEX_SAMPLE:
272 case AMDGPU::TEX_SAMPLE_C:
273 case AMDGPU::TEX_SAMPLE_L:
274 case AMDGPU::TEX_SAMPLE_C_L:
275 case AMDGPU::TEX_SAMPLE_LB:
276 case AMDGPU::TEX_SAMPLE_C_LB:
277 case AMDGPU::TEX_SAMPLE_G:
278 case AMDGPU::TEX_SAMPLE_C_G:
280 case AMDGPU::TXD_SHADOW:
284 dbgs() << "other inst: ";
296 ConstPairs(unsigned ReadConst[3]) : XYPair(0), ZWPair(0) {
297 for (unsigned i = 0; i < 3; i++) {
298 unsigned ReadConstChan = ReadConst[i] & 3;
299 unsigned ReadConstIndex = ReadConst[i] & (~3);
300 if (ReadConstChan < 2) {
302 XYPair = ReadConstIndex;
306 ZWPair = ReadConstIndex;
312 bool isCompatibleWith(const ConstPairs& CP) const {
313 return (!XYPair || !CP.XYPair || CP.XYPair == XYPair) &&
314 (!ZWPair || !CP.ZWPair || CP.ZWPair == ZWPair);
319 const ConstPairs getPairs(const R600InstrInfo *TII, const MachineInstr& MI) {
320 unsigned ReadConsts[3] = {0, 0, 0};
321 R600Operands::Ops OpTable[3][2] = {
322 {R600Operands::SRC0, R600Operands::SRC0_SEL},
323 {R600Operands::SRC1, R600Operands::SRC1_SEL},
324 {R600Operands::SRC2, R600Operands::SRC2_SEL},
327 if (!TII->isALUInstr(MI.getOpcode()))
328 return ConstPairs(ReadConsts);
330 for (unsigned i = 0; i < 3; i++) {
331 int SrcIdx = TII->getOperandIdx(MI.getOpcode(), OpTable[i][0]);
334 if (MI.getOperand(SrcIdx).getReg() == AMDGPU::ALU_CONST)
335 ReadConsts[i] =MI.getOperand(
336 TII->getOperandIdx(MI.getOpcode(), OpTable[i][1])).getImm();
338 return ConstPairs(ReadConsts);
342 R600SchedStrategy::isBundleable(const MachineInstr& MI) {
343 const ConstPairs &MIPair = getPairs(TII, MI);
344 for (unsigned i = 0; i < 4; i++) {
345 if (!InstructionsGroupCandidate[i])
347 const ConstPairs &IGPair = getPairs(TII,
348 *InstructionsGroupCandidate[i]->getInstr());
349 if (!IGPair.isCompatibleWith(MIPair))
355 SUnit *R600SchedStrategy::PopInst(std::multiset<SUnit *, CompareSUnit> &Q) {
358 for (std::set<SUnit *, CompareSUnit>::iterator It = Q.begin(), E = Q.end();
361 if (isBundleable(*SU->getInstr())) {
369 void R600SchedStrategy::LoadAlu() {
370 ReadyQueue *QSrc = Pending[IDAlu];
371 for (ReadyQueue::iterator I = QSrc->begin(),
372 E = QSrc->end(); I != E; ++I) {
373 (*I)->NodeQueueId &= ~QSrc->getID();
374 AluKind AK = getAluKind(*I);
375 AvailableAlus[AK].insert(*I);
380 void R600SchedStrategy::PrepareNextSlot() {
381 DEBUG(dbgs() << "New Slot\n");
382 assert (OccupedSlotsMask && "Slot wasn't filled");
383 OccupedSlotsMask = 0;
384 memset(InstructionsGroupCandidate, 0, sizeof(InstructionsGroupCandidate));
388 void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
389 unsigned DestReg = MI->getOperand(0).getReg();
390 // 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() &&
396 MO.getReg() == MI->getOperand(0).getReg())
399 // Constrains the regclass of DestReg to assign it to Slot
402 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
405 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
408 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
411 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
416 SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot) {
417 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
418 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]]);
419 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny]);
422 } else if (!SlotedSU) {
423 AssignSlot(UnslotedSU->getInstr(), Slot);
426 //Determine which one to pick (the lesser one)
427 if (CompareSUnit()(SlotedSU, UnslotedSU)) {
428 AvailableAlus[AluAny].insert(UnslotedSU);
431 AvailableAlus[IndexToID[Slot]].insert(SlotedSU);
432 AssignSlot(UnslotedSU->getInstr(), Slot);
438 bool R600SchedStrategy::isAvailablesAluEmpty() const {
439 return Pending[IDAlu]->empty() && AvailableAlus[AluAny].empty() &&
440 AvailableAlus[AluT_XYZW].empty() && AvailableAlus[AluT_X].empty() &&
441 AvailableAlus[AluT_Y].empty() && AvailableAlus[AluT_Z].empty() &&
442 AvailableAlus[AluT_W].empty() && AvailableAlus[AluDiscarded].empty();
445 SUnit* R600SchedStrategy::pickAlu() {
446 while (!isAvailablesAluEmpty()) {
447 if (!OccupedSlotsMask) {
448 // Flush physical reg copies (RA will discard them)
449 if (!AvailableAlus[AluDiscarded].empty()) {
450 OccupedSlotsMask = 15;
451 return PopInst(AvailableAlus[AluDiscarded]);
453 // If there is a T_XYZW alu available, use it
454 if (!AvailableAlus[AluT_XYZW].empty()) {
455 OccupedSlotsMask = 15;
456 return PopInst(AvailableAlus[AluT_XYZW]);
459 for (unsigned Chan = 0; Chan < 4; ++Chan) {
460 bool isOccupied = OccupedSlotsMask & (1 << Chan);
462 SUnit *SU = AttemptFillSlot(Chan);
464 OccupedSlotsMask |= (1 << Chan);
465 InstructionsGroupCandidate[Chan] = SU;
475 SUnit* R600SchedStrategy::pickOther(int QID) {
477 ReadyQueue *AQ = Available[QID];
480 MoveUnits(Pending[QID], AQ);
484 AQ->remove(AQ->begin());