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/LiveIntervalAnalysis.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/Pass.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Support/raw_ostream.h"
27 void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
30 TII = static_cast<const R600InstrInfo*>(DAG->TII);
31 TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
33 CurInstKind = IDOther;
35 OccupedSlotsMask = 15;
36 InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
37 InstKindLimit[IDOther] = 32;
39 const AMDGPUSubtarget &ST = DAG->TM.getSubtarget<AMDGPUSubtarget>();
40 InstKindLimit[IDFetch] = ST.getTexVTXClauseSize();
43 void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc,
44 std::vector<SUnit *> &QDst)
46 QDst.insert(QDst.end(), QSrc.begin(), QSrc.end());
50 SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
52 NextInstKind = IDOther;
56 // check if we might want to switch current clause type
57 bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) ||
58 (Available[CurInstKind].empty());
59 bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) &&
60 (!Available[IDFetch].empty() || !Available[IDOther].empty());
62 if ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
63 (!AllowSwitchFromAlu && CurInstKind == IDAlu)) {
67 if (CurEmitted >= InstKindLimit[IDAlu])
75 SU = pickOther(IDFetch);
77 NextInstKind = IDFetch;
82 SU = pickOther(IDOther);
84 NextInstKind = IDOther;
89 dbgs() << " ** Pick node **\n";
92 dbgs() << "NO NODE \n";
93 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
94 const SUnit &S = DAG->SUnits[i];
104 void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
106 if (NextInstKind != CurInstKind) {
107 DEBUG(dbgs() << "Instruction Type Switch\n");
108 if (NextInstKind != IDAlu)
109 OccupedSlotsMask = 15;
111 CurInstKind = NextInstKind;
114 if (CurInstKind == IDAlu) {
115 switch (getAluKind(SU)) {
123 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
124 E = SU->getInstr()->operands_end(); It != E; ++It) {
125 MachineOperand &MO = *It;
126 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
136 DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
138 if (CurInstKind != IDFetch) {
139 MoveUnits(Pending[IDFetch], Available[IDFetch]);
143 void R600SchedStrategy::releaseTopNode(SUnit *SU) {
144 DEBUG(dbgs() << "Top Releasing ";SU->dump(DAG););
148 void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
149 DEBUG(dbgs() << "Bottom Releasing ";SU->dump(DAG););
151 int IK = getInstKind(SU);
152 // There is no export clause, we can schedule one as soon as its ready
154 Available[IDOther].push_back(SU);
156 Pending[IK].push_back(SU);
160 bool R600SchedStrategy::regBelongsToClass(unsigned Reg,
161 const TargetRegisterClass *RC) const {
162 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
163 return RC->contains(Reg);
165 return MRI->getRegClass(Reg) == RC;
169 R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
170 MachineInstr *MI = SU->getInstr();
172 switch (MI->getOpcode()) {
175 case AMDGPU::INTERP_PAIR_XY:
176 case AMDGPU::INTERP_PAIR_ZW:
177 case AMDGPU::INTERP_VEC_LOAD:
181 if (MI->getOperand(1).isUndef()) {
182 // MI will become a KILL, don't considers it in scheduling
189 // Does the instruction take a whole IG ?
190 if(TII->isVector(*MI) ||
191 TII->isCubeOp(MI->getOpcode()) ||
192 TII->isReductionOp(MI->getOpcode()))
195 // Is the result already assigned to a channel ?
196 unsigned DestSubReg = MI->getOperand(0).getSubReg();
197 switch (DestSubReg) {
210 // Is the result already member of a X/Y/Z/W class ?
211 unsigned DestReg = MI->getOperand(0).getReg();
212 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) ||
213 regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass))
215 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass))
217 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass))
219 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass))
221 if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass))
228 int R600SchedStrategy::getInstKind(SUnit* SU) {
229 int Opcode = SU->getInstr()->getOpcode();
231 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
234 if (TII->isALUInstr(Opcode)) {
241 case AMDGPU::CONST_COPY:
242 case AMDGPU::INTERP_PAIR_XY:
243 case AMDGPU::INTERP_PAIR_ZW:
244 case AMDGPU::INTERP_VEC_LOAD:
252 SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q) {
255 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
258 InstructionsGroupCandidate.push_back(SU->getInstr());
259 if (TII->canBundle(InstructionsGroupCandidate)) {
260 InstructionsGroupCandidate.pop_back();
261 Q.erase((It + 1).base());
264 InstructionsGroupCandidate.pop_back();
270 void R600SchedStrategy::LoadAlu() {
271 std::vector<SUnit *> &QSrc = Pending[IDAlu];
272 for (unsigned i = 0, e = QSrc.size(); i < e; ++i) {
273 AluKind AK = getAluKind(QSrc[i]);
274 AvailableAlus[AK].push_back(QSrc[i]);
279 void R600SchedStrategy::PrepareNextSlot() {
280 DEBUG(dbgs() << "New Slot\n");
281 assert (OccupedSlotsMask && "Slot wasn't filled");
282 OccupedSlotsMask = 0;
283 InstructionsGroupCandidate.clear();
287 void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
288 unsigned DestReg = MI->getOperand(0).getReg();
289 // PressureRegister crashes if an operand is def and used in the same inst
290 // and we try to constraint its regclass
291 for (MachineInstr::mop_iterator It = MI->operands_begin(),
292 E = MI->operands_end(); It != E; ++It) {
293 MachineOperand &MO = *It;
294 if (MO.isReg() && !MO.isDef() &&
295 MO.getReg() == MI->getOperand(0).getReg())
298 // Constrains the regclass of DestReg to assign it to Slot
301 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
304 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
307 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
310 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
315 SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot) {
316 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
317 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]]);
320 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny]);
322 AssignSlot(UnslotedSU->getInstr(), Slot);
326 bool R600SchedStrategy::isAvailablesAluEmpty() const {
327 return Pending[IDAlu].empty() && AvailableAlus[AluAny].empty() &&
328 AvailableAlus[AluT_XYZW].empty() && AvailableAlus[AluT_X].empty() &&
329 AvailableAlus[AluT_Y].empty() && AvailableAlus[AluT_Z].empty() &&
330 AvailableAlus[AluT_W].empty() && AvailableAlus[AluDiscarded].empty() &&
331 AvailableAlus[AluPredX].empty();
334 SUnit* R600SchedStrategy::pickAlu() {
335 while (!isAvailablesAluEmpty()) {
336 if (!OccupedSlotsMask) {
337 // Bottom up scheduling : predX must comes first
338 if (!AvailableAlus[AluPredX].empty()) {
339 OccupedSlotsMask = 15;
340 return PopInst(AvailableAlus[AluPredX]);
342 // Flush physical reg copies (RA will discard them)
343 if (!AvailableAlus[AluDiscarded].empty()) {
344 OccupedSlotsMask = 15;
345 return PopInst(AvailableAlus[AluDiscarded]);
347 // If there is a T_XYZW alu available, use it
348 if (!AvailableAlus[AluT_XYZW].empty()) {
349 OccupedSlotsMask = 15;
350 return PopInst(AvailableAlus[AluT_XYZW]);
353 for (int Chan = 3; Chan > -1; --Chan) {
354 bool isOccupied = OccupedSlotsMask & (1 << Chan);
356 SUnit *SU = AttemptFillSlot(Chan);
358 OccupedSlotsMask |= (1 << Chan);
359 InstructionsGroupCandidate.push_back(SU->getInstr());
369 SUnit* R600SchedStrategy::pickOther(int QID) {
371 std::vector<SUnit *> &AQ = Available[QID];
374 MoveUnits(Pending[QID], AQ);
378 AQ.resize(AQ.size() - 1);