Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 1 | //===-- 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 Takumi | 3f179b5 | 2013-03-11 08:19:28 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
NAKAMURA Takumi | 3f179b5 | 2013-03-11 08:19:28 +0000 | [diff] [blame] | 24 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | void 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 Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 33 | CurInstKind = IDOther; |
| 34 | CurEmitted = 0; |
| 35 | OccupedSlotsMask = 15; |
Vincent Lejeune | dae2a20 | 2013-04-03 16:49:34 +0000 | [diff] [blame] | 36 | InstKindLimit[IDAlu] = TII->getMaxAlusPerClause(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | const AMDGPUSubtarget &ST = DAG->TM.getSubtarget<AMDGPUSubtarget>(); |
Vincent Lejeune | dcfcf1d | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 40 | InstKindLimit[IDFetch] = ST.getTexVTXClauseSize(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 43 | void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc, |
| 44 | std::vector<SUnit *> &QDst) |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 45 | { |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 46 | QDst.insert(QDst.end(), QSrc.begin(), QSrc.end()); |
| 47 | QSrc.clear(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | SUnit* 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 Lejeune | dcfcf1d | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 57 | (CurEmitted >= InstKindLimit[CurInstKind]) || |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 58 | (Available[CurInstKind].empty()); |
Vincent Lejeune | dcfcf1d | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 59 | bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) && |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 60 | (!Available[IDFetch].empty() || !Available[IDOther].empty()); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 61 | |
| 62 | if ((AllowSwitchToAlu && CurInstKind != IDAlu) || |
| 63 | (!AllowSwitchFromAlu && CurInstKind == IDAlu)) { |
| 64 | // try to pick ALU |
| 65 | SU = pickAlu(); |
| 66 | if (SU) { |
Vincent Lejeune | dcfcf1d | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 67 | if (CurEmitted >= InstKindLimit[IDAlu]) |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 68 | 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 Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 93 | 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 | |
| 104 | void 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 | |
| 147 | void R600SchedStrategy::releaseTopNode(SUnit *SU) { |
| 148 | int IK = getInstKind(SU); |
| 149 | |
| 150 | DEBUG(dbgs() << IK << " <= "); |
| 151 | DEBUG(SU->dump(DAG)); |
| 152 | |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 153 | Pending[IK].push_back(SU); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void R600SchedStrategy::releaseBottomNode(SUnit *SU) { |
| 157 | } |
| 158 | |
| 159 | bool 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 | |
| 168 | R600SchedStrategy::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 Lejeune | 4ed9917 | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 175 | case AMDGPU::DOT_4: |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 176 | 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 | |
| 230 | int R600SchedStrategy::getInstKind(SUnit* SU) { |
| 231 | int Opcode = SU->getInstr()->getOpcode(); |
| 232 | |
Vincent Lejeune | f63f85a | 2013-05-17 16:50:37 +0000 | [diff] [blame] | 233 | if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode)) |
| 234 | return IDFetch; |
| 235 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 236 | 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 Lejeune | 4ed9917 | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 246 | case AMDGPU::DOT_4: |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 247 | return IDAlu; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 248 | default: |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 249 | return IDOther; |
| 250 | } |
| 251 | } |
| 252 | |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 253 | SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q) { |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 254 | if (Q.empty()) |
| 255 | return NULL; |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 256 | for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 257 | It != E; ++It) { |
| 258 | SUnit *SU = *It; |
Vincent Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 259 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
| 260 | if (TII->canBundle(InstructionsGroupCandidate)) { |
| 261 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 262 | Q.erase((It + 1).base()); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 263 | return SU; |
Vincent Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 264 | } else { |
| 265 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | void R600SchedStrategy::LoadAlu() { |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 272 | 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 Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void R600SchedStrategy::PrepareNextSlot() { |
| 281 | DEBUG(dbgs() << "New Slot\n"); |
| 282 | assert (OccupedSlotsMask && "Slot wasn't filled"); |
| 283 | OccupedSlotsMask = 0; |
Vincent Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 284 | InstructionsGroupCandidate.clear(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 285 | LoadAlu(); |
| 286 | } |
| 287 | |
| 288 | void 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 | |
| 316 | SUnit *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 Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 319 | if (SlotedSU) |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 320 | return SlotedSU; |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 321 | SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny]); |
| 322 | if (UnslotedSU) |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 323 | AssignSlot(UnslotedSU->getInstr(), Slot); |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 324 | return UnslotedSU; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | bool R600SchedStrategy::isAvailablesAluEmpty() const { |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 328 | return Pending[IDAlu].empty() && AvailableAlus[AluAny].empty() && |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 329 | 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 | |
| 334 | SUnit* 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 Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 354 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 355 | return SU; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | PrepareNextSlot(); |
| 360 | } |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | SUnit* R600SchedStrategy::pickOther(int QID) { |
| 365 | SUnit *SU = 0; |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 366 | std::vector<SUnit *> &AQ = Available[QID]; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 367 | |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 368 | if (AQ.empty()) { |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 369 | MoveUnits(Pending[QID], AQ); |
| 370 | } |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame^] | 371 | if (!AQ.empty()) { |
| 372 | SU = AQ.back(); |
| 373 | AQ.resize(AQ.size() - 1); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 374 | } |
| 375 | return SU; |
| 376 | } |
| 377 | |