145bb48eaSTom Stellard //===-- R600MachineScheduler.cpp - R600 Scheduler Interface -*- C++ -*-----===//
245bb48eaSTom Stellard //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
645bb48eaSTom Stellard //
745bb48eaSTom Stellard //===----------------------------------------------------------------------===//
845bb48eaSTom Stellard //
945bb48eaSTom Stellard /// \file
105f8f34e4SAdrian Prantl /// R600 Machine Scheduler interface
1145bb48eaSTom Stellard //
1245bb48eaSTom Stellard //===----------------------------------------------------------------------===//
1345bb48eaSTom Stellard 
1445bb48eaSTom Stellard #include "R600MachineScheduler.h"
1548958d02SDaniil Fukalov #include "MCTargetDesc/R600MCTargetDesc.h"
16560d7e04Sdfukalov #include "R600Subtarget.h"
1745bb48eaSTom Stellard 
1845bb48eaSTom Stellard using namespace llvm;
1945bb48eaSTom Stellard 
200cd23f56SEvandro Menezes #define DEBUG_TYPE "machine-scheduler"
2145bb48eaSTom Stellard 
initialize(ScheduleDAGMI * dag)2245bb48eaSTom Stellard void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
2345bb48eaSTom Stellard   assert(dag->hasVRegLiveness() && "R600SchedStrategy needs vreg liveness");
2445bb48eaSTom Stellard   DAG = static_cast<ScheduleDAGMILive*>(dag);
2543e92fe3SMatt Arsenault   const R600Subtarget &ST = DAG->MF.getSubtarget<R600Subtarget>();
2645bb48eaSTom Stellard   TII = static_cast<const R600InstrInfo*>(DAG->TII);
2745bb48eaSTom Stellard   TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
2845bb48eaSTom Stellard   VLIW5 = !ST.hasCaymanISA();
2945bb48eaSTom Stellard   MRI = &DAG->MRI;
3045bb48eaSTom Stellard   CurInstKind = IDOther;
3145bb48eaSTom Stellard   CurEmitted = 0;
32d1f45ed5SNeubauer, Sebastian   OccupiedSlotsMask = 31;
3345bb48eaSTom Stellard   InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
3445bb48eaSTom Stellard   InstKindLimit[IDOther] = 32;
3545bb48eaSTom Stellard   InstKindLimit[IDFetch] = ST.getTexVTXClauseSize();
3645bb48eaSTom Stellard   AluInstCount = 0;
3745bb48eaSTom Stellard   FetchInstCount = 0;
3845bb48eaSTom Stellard }
3945bb48eaSTom Stellard 
MoveUnits(std::vector<SUnit * > & QSrc,std::vector<SUnit * > & QDst)4045bb48eaSTom Stellard void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc,
4145bb48eaSTom Stellard                                   std::vector<SUnit *> &QDst)
4245bb48eaSTom Stellard {
43985f899bSKazu Hirata   llvm::append_range(QDst, QSrc);
4445bb48eaSTom Stellard   QSrc.clear();
4545bb48eaSTom Stellard }
4645bb48eaSTom Stellard 
getWFCountLimitedByGPR(unsigned GPRCount)4743e92fe3SMatt Arsenault static unsigned getWFCountLimitedByGPR(unsigned GPRCount) {
4845bb48eaSTom Stellard   assert (GPRCount && "GPRCount cannot be 0");
4945bb48eaSTom Stellard   return 248 / GPRCount;
5045bb48eaSTom Stellard }
5145bb48eaSTom Stellard 
pickNode(bool & IsTopNode)5245bb48eaSTom Stellard SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
5345bb48eaSTom Stellard   SUnit *SU = nullptr;
5445bb48eaSTom Stellard   NextInstKind = IDOther;
5545bb48eaSTom Stellard 
5645bb48eaSTom Stellard   IsTopNode = false;
5745bb48eaSTom Stellard 
5845bb48eaSTom Stellard   // check if we might want to switch current clause type
5945bb48eaSTom Stellard   bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) ||
6045bb48eaSTom Stellard       (Available[CurInstKind].empty());
6145bb48eaSTom Stellard   bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) &&
6245bb48eaSTom Stellard       (!Available[IDFetch].empty() || !Available[IDOther].empty());
6345bb48eaSTom Stellard 
6445bb48eaSTom Stellard   if (CurInstKind == IDAlu && !Available[IDFetch].empty()) {
6545bb48eaSTom Stellard     // We use the heuristic provided by AMD Accelerated Parallel Processing
6645bb48eaSTom Stellard     // OpenCL Programming Guide :
6745bb48eaSTom Stellard     // The approx. number of WF that allows TEX inst to hide ALU inst is :
6845bb48eaSTom Stellard     // 500 (cycles for TEX) / (AluFetchRatio * 8 (cycles for ALU))
6945bb48eaSTom Stellard     float ALUFetchRationEstimate =
7045bb48eaSTom Stellard         (AluInstCount + AvailablesAluCount() + Pending[IDAlu].size()) /
7145bb48eaSTom Stellard         (FetchInstCount + Available[IDFetch].size());
7245bb48eaSTom Stellard     if (ALUFetchRationEstimate == 0) {
7345bb48eaSTom Stellard       AllowSwitchFromAlu = true;
7445bb48eaSTom Stellard     } else {
7545bb48eaSTom Stellard       unsigned NeededWF = 62.5f / ALUFetchRationEstimate;
76d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << NeededWF << " approx. Wavefronts Required\n");
7745bb48eaSTom Stellard       // We assume the local GPR requirements to be "dominated" by the requirement
7845bb48eaSTom Stellard       // of the TEX clause (which consumes 128 bits regs) ; ALU inst before and
7945bb48eaSTom Stellard       // after TEX are indeed likely to consume or generate values from/for the
8045bb48eaSTom Stellard       // TEX clause.
8145bb48eaSTom Stellard       // Available[IDFetch].size() * 2 : GPRs required in the Fetch clause
8245bb48eaSTom Stellard       // We assume that fetch instructions are either TnXYZW = TEX TnXYZW (need
8345bb48eaSTom Stellard       // one GPR) or TmXYZW = TnXYZW (need 2 GPR).
8445bb48eaSTom Stellard       // (TODO : use RegisterPressure)
8545bb48eaSTom Stellard       // If we are going too use too many GPR, we flush Fetch instruction to lower
8645bb48eaSTom Stellard       // register pressure on 128 bits regs.
8745bb48eaSTom Stellard       unsigned NearRegisterRequirement = 2 * Available[IDFetch].size();
8845bb48eaSTom Stellard       if (NeededWF > getWFCountLimitedByGPR(NearRegisterRequirement))
8945bb48eaSTom Stellard         AllowSwitchFromAlu = true;
9045bb48eaSTom Stellard     }
9145bb48eaSTom Stellard   }
9245bb48eaSTom Stellard 
9345bb48eaSTom Stellard   if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
9445bb48eaSTom Stellard       (!AllowSwitchFromAlu && CurInstKind == IDAlu))) {
9545bb48eaSTom Stellard     // try to pick ALU
9645bb48eaSTom Stellard     SU = pickAlu();
9745bb48eaSTom Stellard     if (!SU && !PhysicalRegCopy.empty()) {
9845bb48eaSTom Stellard       SU = PhysicalRegCopy.front();
9945bb48eaSTom Stellard       PhysicalRegCopy.erase(PhysicalRegCopy.begin());
10045bb48eaSTom Stellard     }
10145bb48eaSTom Stellard     if (SU) {
10245bb48eaSTom Stellard       if (CurEmitted >= InstKindLimit[IDAlu])
10345bb48eaSTom Stellard         CurEmitted = 0;
10445bb48eaSTom Stellard       NextInstKind = IDAlu;
10545bb48eaSTom Stellard     }
10645bb48eaSTom Stellard   }
10745bb48eaSTom Stellard 
10845bb48eaSTom Stellard   if (!SU) {
10945bb48eaSTom Stellard     // try to pick FETCH
11045bb48eaSTom Stellard     SU = pickOther(IDFetch);
11145bb48eaSTom Stellard     if (SU)
11245bb48eaSTom Stellard       NextInstKind = IDFetch;
11345bb48eaSTom Stellard   }
11445bb48eaSTom Stellard 
11545bb48eaSTom Stellard   // try to pick other
11645bb48eaSTom Stellard   if (!SU) {
11745bb48eaSTom Stellard     SU = pickOther(IDOther);
11845bb48eaSTom Stellard     if (SU)
11945bb48eaSTom Stellard       NextInstKind = IDOther;
12045bb48eaSTom Stellard   }
12145bb48eaSTom Stellard 
122d34e60caSNicola Zaghen   LLVM_DEBUG(if (SU) {
12345bb48eaSTom Stellard     dbgs() << " ** Pick node **\n";
124726e12cfSMatthias Braun     DAG->dumpNode(*SU);
12545bb48eaSTom Stellard   } else {
12645bb48eaSTom Stellard     dbgs() << "NO NODE \n";
127fd7d4064SKazu Hirata     for (const SUnit &S : DAG->SUnits)
12845bb48eaSTom Stellard       if (!S.isScheduled)
129726e12cfSMatthias Braun         DAG->dumpNode(S);
130d34e60caSNicola Zaghen   });
13145bb48eaSTom Stellard 
13245bb48eaSTom Stellard   return SU;
13345bb48eaSTom Stellard }
13445bb48eaSTom Stellard 
schedNode(SUnit * SU,bool IsTopNode)13545bb48eaSTom Stellard void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
13645bb48eaSTom Stellard   if (NextInstKind != CurInstKind) {
137d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Instruction Type Switch\n");
13845bb48eaSTom Stellard     if (NextInstKind != IDAlu)
139d1f45ed5SNeubauer, Sebastian       OccupiedSlotsMask |= 31;
14045bb48eaSTom Stellard     CurEmitted = 0;
14145bb48eaSTom Stellard     CurInstKind = NextInstKind;
14245bb48eaSTom Stellard   }
14345bb48eaSTom Stellard 
14445bb48eaSTom Stellard   if (CurInstKind == IDAlu) {
14545bb48eaSTom Stellard     AluInstCount ++;
14645bb48eaSTom Stellard     switch (getAluKind(SU)) {
14745bb48eaSTom Stellard     case AluT_XYZW:
14845bb48eaSTom Stellard       CurEmitted += 4;
14945bb48eaSTom Stellard       break;
15045bb48eaSTom Stellard     case AluDiscarded:
15145bb48eaSTom Stellard       break;
15245bb48eaSTom Stellard     default: {
15345bb48eaSTom Stellard       ++CurEmitted;
15445bb48eaSTom Stellard       for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
15545bb48eaSTom Stellard           E = SU->getInstr()->operands_end(); It != E; ++It) {
15645bb48eaSTom Stellard         MachineOperand &MO = *It;
157c5a154dbSTom Stellard         if (MO.isReg() && MO.getReg() == R600::ALU_LITERAL_X)
15845bb48eaSTom Stellard           ++CurEmitted;
15945bb48eaSTom Stellard       }
16045bb48eaSTom Stellard     }
16145bb48eaSTom Stellard     }
16245bb48eaSTom Stellard   } else {
16345bb48eaSTom Stellard     ++CurEmitted;
16445bb48eaSTom Stellard   }
16545bb48eaSTom Stellard 
166d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
16745bb48eaSTom Stellard 
16845bb48eaSTom Stellard   if (CurInstKind != IDFetch) {
16945bb48eaSTom Stellard     MoveUnits(Pending[IDFetch], Available[IDFetch]);
17045bb48eaSTom Stellard   } else
17145bb48eaSTom Stellard     FetchInstCount++;
17245bb48eaSTom Stellard }
17345bb48eaSTom Stellard 
17445bb48eaSTom Stellard static bool
isPhysicalRegCopy(MachineInstr * MI)17545bb48eaSTom Stellard isPhysicalRegCopy(MachineInstr *MI) {
176c5a154dbSTom Stellard   if (MI->getOpcode() != R600::COPY)
17745bb48eaSTom Stellard     return false;
17845bb48eaSTom Stellard 
17934978602SJay Foad   return !MI->getOperand(1).getReg().isVirtual();
18045bb48eaSTom Stellard }
18145bb48eaSTom Stellard 
releaseTopNode(SUnit * SU)18245bb48eaSTom Stellard void R600SchedStrategy::releaseTopNode(SUnit *SU) {
183726e12cfSMatthias Braun   LLVM_DEBUG(dbgs() << "Top Releasing "; DAG->dumpNode(*SU));
18445bb48eaSTom Stellard }
18545bb48eaSTom Stellard 
releaseBottomNode(SUnit * SU)18645bb48eaSTom Stellard void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
187726e12cfSMatthias Braun   LLVM_DEBUG(dbgs() << "Bottom Releasing "; DAG->dumpNode(*SU));
18845bb48eaSTom Stellard   if (isPhysicalRegCopy(SU->getInstr())) {
18945bb48eaSTom Stellard     PhysicalRegCopy.push_back(SU);
19045bb48eaSTom Stellard     return;
19145bb48eaSTom Stellard   }
19245bb48eaSTom Stellard 
19345bb48eaSTom Stellard   int IK = getInstKind(SU);
19445bb48eaSTom Stellard 
19545bb48eaSTom Stellard   // There is no export clause, we can schedule one as soon as its ready
19645bb48eaSTom Stellard   if (IK == IDOther)
19745bb48eaSTom Stellard     Available[IDOther].push_back(SU);
19845bb48eaSTom Stellard   else
19945bb48eaSTom Stellard     Pending[IK].push_back(SU);
20045bb48eaSTom Stellard 
20145bb48eaSTom Stellard }
20245bb48eaSTom Stellard 
regBelongsToClass(Register Reg,const TargetRegisterClass * RC) const20334978602SJay Foad bool R600SchedStrategy::regBelongsToClass(Register Reg,
20445bb48eaSTom Stellard                                           const TargetRegisterClass *RC) const {
20534978602SJay Foad   if (!Reg.isVirtual()) {
20645bb48eaSTom Stellard     return RC->contains(Reg);
20745bb48eaSTom Stellard   } else {
20845bb48eaSTom Stellard     return MRI->getRegClass(Reg) == RC;
20945bb48eaSTom Stellard   }
21045bb48eaSTom Stellard }
21145bb48eaSTom Stellard 
getAluKind(SUnit * SU) const21245bb48eaSTom Stellard R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
21345bb48eaSTom Stellard   MachineInstr *MI = SU->getInstr();
21445bb48eaSTom Stellard 
2159cfc75c2SDuncan P. N. Exon Smith   if (TII->isTransOnly(*MI))
21645bb48eaSTom Stellard     return AluTrans;
21745bb48eaSTom Stellard 
21845bb48eaSTom Stellard   switch (MI->getOpcode()) {
219c5a154dbSTom Stellard   case R600::PRED_X:
22045bb48eaSTom Stellard     return AluPredX;
221c5a154dbSTom Stellard   case R600::INTERP_PAIR_XY:
222c5a154dbSTom Stellard   case R600::INTERP_PAIR_ZW:
223c5a154dbSTom Stellard   case R600::INTERP_VEC_LOAD:
224c5a154dbSTom Stellard   case R600::DOT_4:
22545bb48eaSTom Stellard     return AluT_XYZW;
226c5a154dbSTom Stellard   case R600::COPY:
22745bb48eaSTom Stellard     if (MI->getOperand(1).isUndef()) {
22845bb48eaSTom Stellard       // MI will become a KILL, don't considers it in scheduling
22945bb48eaSTom Stellard       return AluDiscarded;
23045bb48eaSTom Stellard     }
2314dc0b1acSReid Kleckner     break;
23245bb48eaSTom Stellard   default:
23345bb48eaSTom Stellard     break;
23445bb48eaSTom Stellard   }
23545bb48eaSTom Stellard 
23645bb48eaSTom Stellard   // Does the instruction take a whole IG ?
23745bb48eaSTom Stellard   // XXX: Is it possible to add a helper function in R600InstrInfo that can
23845bb48eaSTom Stellard   // be used here and in R600PacketizerList::isSoloInstruction() ?
23945bb48eaSTom Stellard   if(TII->isVector(*MI) ||
24045bb48eaSTom Stellard      TII->isCubeOp(MI->getOpcode()) ||
24145bb48eaSTom Stellard      TII->isReductionOp(MI->getOpcode()) ||
242c5a154dbSTom Stellard      MI->getOpcode() == R600::GROUP_BARRIER) {
24345bb48eaSTom Stellard     return AluT_XYZW;
24445bb48eaSTom Stellard   }
24545bb48eaSTom Stellard 
24645bb48eaSTom Stellard   if (TII->isLDSInstr(MI->getOpcode())) {
24745bb48eaSTom Stellard     return AluT_X;
24845bb48eaSTom Stellard   }
24945bb48eaSTom Stellard 
25045bb48eaSTom Stellard   // Is the result already assigned to a channel ?
25145bb48eaSTom Stellard   unsigned DestSubReg = MI->getOperand(0).getSubReg();
25245bb48eaSTom Stellard   switch (DestSubReg) {
253c5a154dbSTom Stellard   case R600::sub0:
25445bb48eaSTom Stellard     return AluT_X;
255c5a154dbSTom Stellard   case R600::sub1:
25645bb48eaSTom Stellard     return AluT_Y;
257c5a154dbSTom Stellard   case R600::sub2:
25845bb48eaSTom Stellard     return AluT_Z;
259c5a154dbSTom Stellard   case R600::sub3:
26045bb48eaSTom Stellard     return AluT_W;
26145bb48eaSTom Stellard   default:
26245bb48eaSTom Stellard     break;
26345bb48eaSTom Stellard   }
26445bb48eaSTom Stellard 
26545bb48eaSTom Stellard   // Is the result already member of a X/Y/Z/W class ?
2660c476111SDaniel Sanders   Register DestReg = MI->getOperand(0).getReg();
267c5a154dbSTom Stellard   if (regBelongsToClass(DestReg, &R600::R600_TReg32_XRegClass) ||
268c5a154dbSTom Stellard       regBelongsToClass(DestReg, &R600::R600_AddrRegClass))
26945bb48eaSTom Stellard     return AluT_X;
270c5a154dbSTom Stellard   if (regBelongsToClass(DestReg, &R600::R600_TReg32_YRegClass))
27145bb48eaSTom Stellard     return AluT_Y;
272c5a154dbSTom Stellard   if (regBelongsToClass(DestReg, &R600::R600_TReg32_ZRegClass))
27345bb48eaSTom Stellard     return AluT_Z;
274c5a154dbSTom Stellard   if (regBelongsToClass(DestReg, &R600::R600_TReg32_WRegClass))
27545bb48eaSTom Stellard     return AluT_W;
276c5a154dbSTom Stellard   if (regBelongsToClass(DestReg, &R600::R600_Reg128RegClass))
27745bb48eaSTom Stellard     return AluT_XYZW;
27845bb48eaSTom Stellard 
27945bb48eaSTom Stellard   // LDS src registers cannot be used in the Trans slot.
2809cfc75c2SDuncan P. N. Exon Smith   if (TII->readsLDSSrcReg(*MI))
28145bb48eaSTom Stellard     return AluT_XYZW;
28245bb48eaSTom Stellard 
28345bb48eaSTom Stellard   return AluAny;
28445bb48eaSTom Stellard }
28545bb48eaSTom Stellard 
getInstKind(SUnit * SU)28645bb48eaSTom Stellard int R600SchedStrategy::getInstKind(SUnit* SU) {
28745bb48eaSTom Stellard   int Opcode = SU->getInstr()->getOpcode();
28845bb48eaSTom Stellard 
28945bb48eaSTom Stellard   if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
29045bb48eaSTom Stellard     return IDFetch;
29145bb48eaSTom Stellard 
29245bb48eaSTom Stellard   if (TII->isALUInstr(Opcode)) {
29345bb48eaSTom Stellard     return IDAlu;
29445bb48eaSTom Stellard   }
29545bb48eaSTom Stellard 
29645bb48eaSTom Stellard   switch (Opcode) {
297c5a154dbSTom Stellard   case R600::PRED_X:
298c5a154dbSTom Stellard   case R600::COPY:
299c5a154dbSTom Stellard   case R600::CONST_COPY:
300c5a154dbSTom Stellard   case R600::INTERP_PAIR_XY:
301c5a154dbSTom Stellard   case R600::INTERP_PAIR_ZW:
302c5a154dbSTom Stellard   case R600::INTERP_VEC_LOAD:
303c5a154dbSTom Stellard   case R600::DOT_4:
30445bb48eaSTom Stellard     return IDAlu;
30545bb48eaSTom Stellard   default:
30645bb48eaSTom Stellard     return IDOther;
30745bb48eaSTom Stellard   }
30845bb48eaSTom Stellard }
30945bb48eaSTom Stellard 
PopInst(std::vector<SUnit * > & Q,bool AnyALU)31045bb48eaSTom Stellard SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) {
31145bb48eaSTom Stellard   if (Q.empty())
31245bb48eaSTom Stellard     return nullptr;
31345bb48eaSTom Stellard   for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
31445bb48eaSTom Stellard       It != E; ++It) {
31545bb48eaSTom Stellard     SUnit *SU = *It;
31645bb48eaSTom Stellard     InstructionsGroupCandidate.push_back(SU->getInstr());
3179cfc75c2SDuncan P. N. Exon Smith     if (TII->fitsConstReadLimitations(InstructionsGroupCandidate) &&
3189cfc75c2SDuncan P. N. Exon Smith         (!AnyALU || !TII->isVectorOnly(*SU->getInstr()))) {
31945bb48eaSTom Stellard       InstructionsGroupCandidate.pop_back();
32045bb48eaSTom Stellard       Q.erase((It + 1).base());
32145bb48eaSTom Stellard       return SU;
32245bb48eaSTom Stellard     } else {
32345bb48eaSTom Stellard       InstructionsGroupCandidate.pop_back();
32445bb48eaSTom Stellard     }
32545bb48eaSTom Stellard   }
32645bb48eaSTom Stellard   return nullptr;
32745bb48eaSTom Stellard }
32845bb48eaSTom Stellard 
LoadAlu()32945bb48eaSTom Stellard void R600SchedStrategy::LoadAlu() {
33045bb48eaSTom Stellard   std::vector<SUnit *> &QSrc = Pending[IDAlu];
331*d395befaSKazu Hirata   for (SUnit *SU : QSrc) {
332*d395befaSKazu Hirata     AluKind AK = getAluKind(SU);
333*d395befaSKazu Hirata     AvailableAlus[AK].push_back(SU);
33445bb48eaSTom Stellard   }
33545bb48eaSTom Stellard   QSrc.clear();
33645bb48eaSTom Stellard }
33745bb48eaSTom Stellard 
PrepareNextSlot()33845bb48eaSTom Stellard void R600SchedStrategy::PrepareNextSlot() {
339d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "New Slot\n");
340d1f45ed5SNeubauer, Sebastian   assert(OccupiedSlotsMask && "Slot wasn't filled");
341d1f45ed5SNeubauer, Sebastian   OccupiedSlotsMask = 0;
3425bfbae5cSTom Stellard   //  if (HwGen == AMDGPUSubtarget::NORTHERN_ISLANDS)
343d1f45ed5SNeubauer, Sebastian   //    OccupiedSlotsMask |= 16;
34445bb48eaSTom Stellard   InstructionsGroupCandidate.clear();
34545bb48eaSTom Stellard   LoadAlu();
34645bb48eaSTom Stellard }
34745bb48eaSTom Stellard 
AssignSlot(MachineInstr * MI,unsigned Slot)34845bb48eaSTom Stellard void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
349c5a154dbSTom Stellard   int DstIndex = TII->getOperandIdx(MI->getOpcode(), R600::OpName::dst);
35045bb48eaSTom Stellard   if (DstIndex == -1) {
35145bb48eaSTom Stellard     return;
35245bb48eaSTom Stellard   }
3530c476111SDaniel Sanders   Register DestReg = MI->getOperand(DstIndex).getReg();
35445bb48eaSTom Stellard   // PressureRegister crashes if an operand is def and used in the same inst
35545bb48eaSTom Stellard   // and we try to constraint its regclass
35645bb48eaSTom Stellard   for (MachineInstr::mop_iterator It = MI->operands_begin(),
35745bb48eaSTom Stellard       E = MI->operands_end(); It != E; ++It) {
35845bb48eaSTom Stellard     MachineOperand &MO = *It;
35945bb48eaSTom Stellard     if (MO.isReg() && !MO.isDef() &&
36045bb48eaSTom Stellard         MO.getReg() == DestReg)
36145bb48eaSTom Stellard       return;
36245bb48eaSTom Stellard   }
36345bb48eaSTom Stellard   // Constrains the regclass of DestReg to assign it to Slot
36445bb48eaSTom Stellard   switch (Slot) {
36545bb48eaSTom Stellard   case 0:
366c5a154dbSTom Stellard     MRI->constrainRegClass(DestReg, &R600::R600_TReg32_XRegClass);
36745bb48eaSTom Stellard     break;
36845bb48eaSTom Stellard   case 1:
369c5a154dbSTom Stellard     MRI->constrainRegClass(DestReg, &R600::R600_TReg32_YRegClass);
37045bb48eaSTom Stellard     break;
37145bb48eaSTom Stellard   case 2:
372c5a154dbSTom Stellard     MRI->constrainRegClass(DestReg, &R600::R600_TReg32_ZRegClass);
37345bb48eaSTom Stellard     break;
37445bb48eaSTom Stellard   case 3:
375c5a154dbSTom Stellard     MRI->constrainRegClass(DestReg, &R600::R600_TReg32_WRegClass);
37645bb48eaSTom Stellard     break;
37745bb48eaSTom Stellard   }
37845bb48eaSTom Stellard }
37945bb48eaSTom Stellard 
AttemptFillSlot(unsigned Slot,bool AnyAlu)38045bb48eaSTom Stellard SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) {
38145bb48eaSTom Stellard   static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
38245bb48eaSTom Stellard   SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu);
38345bb48eaSTom Stellard   if (SlotedSU)
38445bb48eaSTom Stellard     return SlotedSU;
38545bb48eaSTom Stellard   SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu);
38645bb48eaSTom Stellard   if (UnslotedSU)
38745bb48eaSTom Stellard     AssignSlot(UnslotedSU->getInstr(), Slot);
38845bb48eaSTom Stellard   return UnslotedSU;
38945bb48eaSTom Stellard }
39045bb48eaSTom Stellard 
AvailablesAluCount() const39145bb48eaSTom Stellard unsigned R600SchedStrategy::AvailablesAluCount() const {
39245bb48eaSTom Stellard   return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() +
39345bb48eaSTom Stellard       AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() +
39445bb48eaSTom Stellard       AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() +
39545bb48eaSTom Stellard       AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() +
39645bb48eaSTom Stellard       AvailableAlus[AluPredX].size();
39745bb48eaSTom Stellard }
39845bb48eaSTom Stellard 
pickAlu()39945bb48eaSTom Stellard SUnit* R600SchedStrategy::pickAlu() {
40045bb48eaSTom Stellard   while (AvailablesAluCount() || !Pending[IDAlu].empty()) {
401d1f45ed5SNeubauer, Sebastian     if (!OccupiedSlotsMask) {
40245bb48eaSTom Stellard       // Bottom up scheduling : predX must comes first
40345bb48eaSTom Stellard       if (!AvailableAlus[AluPredX].empty()) {
404d1f45ed5SNeubauer, Sebastian         OccupiedSlotsMask |= 31;
40545bb48eaSTom Stellard         return PopInst(AvailableAlus[AluPredX], false);
40645bb48eaSTom Stellard       }
40745bb48eaSTom Stellard       // Flush physical reg copies (RA will discard them)
40845bb48eaSTom Stellard       if (!AvailableAlus[AluDiscarded].empty()) {
409d1f45ed5SNeubauer, Sebastian         OccupiedSlotsMask |= 31;
41045bb48eaSTom Stellard         return PopInst(AvailableAlus[AluDiscarded], false);
41145bb48eaSTom Stellard       }
41245bb48eaSTom Stellard       // If there is a T_XYZW alu available, use it
41345bb48eaSTom Stellard       if (!AvailableAlus[AluT_XYZW].empty()) {
414d1f45ed5SNeubauer, Sebastian         OccupiedSlotsMask |= 15;
41545bb48eaSTom Stellard         return PopInst(AvailableAlus[AluT_XYZW], false);
41645bb48eaSTom Stellard       }
41745bb48eaSTom Stellard     }
418d1f45ed5SNeubauer, Sebastian     bool TransSlotOccupied = OccupiedSlotsMask & 16;
419d1f45ed5SNeubauer, Sebastian     if (!TransSlotOccupied && VLIW5) {
42045bb48eaSTom Stellard       if (!AvailableAlus[AluTrans].empty()) {
421d1f45ed5SNeubauer, Sebastian         OccupiedSlotsMask |= 16;
42245bb48eaSTom Stellard         return PopInst(AvailableAlus[AluTrans], false);
42345bb48eaSTom Stellard       }
42445bb48eaSTom Stellard       SUnit *SU = AttemptFillSlot(3, true);
42545bb48eaSTom Stellard       if (SU) {
426d1f45ed5SNeubauer, Sebastian         OccupiedSlotsMask |= 16;
42745bb48eaSTom Stellard         return SU;
42845bb48eaSTom Stellard       }
42945bb48eaSTom Stellard     }
43045bb48eaSTom Stellard     for (int Chan = 3; Chan > -1; --Chan) {
431d1f45ed5SNeubauer, Sebastian       bool isOccupied = OccupiedSlotsMask & (1 << Chan);
43245bb48eaSTom Stellard       if (!isOccupied) {
43345bb48eaSTom Stellard         SUnit *SU = AttemptFillSlot(Chan, false);
43445bb48eaSTom Stellard         if (SU) {
435d1f45ed5SNeubauer, Sebastian           OccupiedSlotsMask |= (1 << Chan);
43645bb48eaSTom Stellard           InstructionsGroupCandidate.push_back(SU->getInstr());
43745bb48eaSTom Stellard           return SU;
43845bb48eaSTom Stellard         }
43945bb48eaSTom Stellard       }
44045bb48eaSTom Stellard     }
44145bb48eaSTom Stellard     PrepareNextSlot();
44245bb48eaSTom Stellard   }
44345bb48eaSTom Stellard   return nullptr;
44445bb48eaSTom Stellard }
44545bb48eaSTom Stellard 
pickOther(int QID)44645bb48eaSTom Stellard SUnit* R600SchedStrategy::pickOther(int QID) {
44745bb48eaSTom Stellard   SUnit *SU = nullptr;
44845bb48eaSTom Stellard   std::vector<SUnit *> &AQ = Available[QID];
44945bb48eaSTom Stellard 
45045bb48eaSTom Stellard   if (AQ.empty()) {
45145bb48eaSTom Stellard     MoveUnits(Pending[QID], AQ);
45245bb48eaSTom Stellard   }
45345bb48eaSTom Stellard   if (!AQ.empty()) {
45445bb48eaSTom Stellard     SU = AQ.back();
455c72204d5SGeorge Burgess IV     AQ.pop_back();
45645bb48eaSTom Stellard   }
45745bb48eaSTom Stellard   return SU;
45845bb48eaSTom Stellard }
459