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" |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Benjamin Kramer | 5c35290 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 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 | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 37 | InstKindLimit[IDOther] = 32; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 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; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 52 | NextInstKind = IDOther; |
| 53 | |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 54 | IsTopNode = false; |
| 55 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 56 | // check if we might want to switch current clause type |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 57 | bool AllowSwitchToAlu = (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 | |
Tom Stellard | ad7ecc6 | 2013-06-05 03:43:06 +0000 | [diff] [blame] | 62 | // We want to scheduled AR defs as soon as possible to make sure they aren't |
| 63 | // put in a different ALU clause from their uses. |
| 64 | if (!SU && !UnscheduledARDefs.empty()) { |
| 65 | SU = UnscheduledARDefs[0]; |
| 66 | UnscheduledARDefs.erase(UnscheduledARDefs.begin()); |
| 67 | NextInstKind = IDAlu; |
| 68 | } |
| 69 | |
| 70 | if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) || |
| 71 | (!AllowSwitchFromAlu && CurInstKind == IDAlu))) { |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 72 | // try to pick ALU |
| 73 | SU = pickAlu(); |
Vincent Lejeune | 5121197 | 2013-06-05 20:27:35 +0000 | [diff] [blame^] | 74 | if (!SU && !PhysicalRegCopy.empty()) { |
| 75 | SU = PhysicalRegCopy.front(); |
| 76 | PhysicalRegCopy.erase(PhysicalRegCopy.begin()); |
| 77 | } |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 78 | if (SU) { |
Vincent Lejeune | dcfcf1d | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 79 | if (CurEmitted >= InstKindLimit[IDAlu]) |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 80 | CurEmitted = 0; |
| 81 | NextInstKind = IDAlu; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (!SU) { |
| 86 | // try to pick FETCH |
| 87 | SU = pickOther(IDFetch); |
| 88 | if (SU) |
| 89 | NextInstKind = IDFetch; |
| 90 | } |
| 91 | |
| 92 | // try to pick other |
| 93 | if (!SU) { |
| 94 | SU = pickOther(IDOther); |
| 95 | if (SU) |
| 96 | NextInstKind = IDOther; |
| 97 | } |
| 98 | |
Tom Stellard | ad7ecc6 | 2013-06-05 03:43:06 +0000 | [diff] [blame] | 99 | // We want to schedule the AR uses as late as possible to make sure that |
| 100 | // the AR defs have been released. |
| 101 | if (!SU && !UnscheduledARUses.empty()) { |
| 102 | SU = UnscheduledARUses[0]; |
| 103 | UnscheduledARUses.erase(UnscheduledARUses.begin()); |
| 104 | NextInstKind = IDAlu; |
| 105 | } |
| 106 | |
| 107 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 108 | DEBUG( |
| 109 | if (SU) { |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 110 | dbgs() << " ** Pick node **\n"; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 111 | SU->dump(DAG); |
| 112 | } else { |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 113 | dbgs() << "NO NODE \n"; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 114 | for (unsigned i = 0; i < DAG->SUnits.size(); i++) { |
| 115 | const SUnit &S = DAG->SUnits[i]; |
| 116 | if (!S.isScheduled) |
| 117 | S.dump(DAG); |
| 118 | } |
| 119 | } |
| 120 | ); |
| 121 | |
| 122 | return SU; |
| 123 | } |
| 124 | |
Vincent Lejeune | 5121197 | 2013-06-05 20:27:35 +0000 | [diff] [blame^] | 125 | bool IsUnScheduled(const SUnit *SU) { |
| 126 | return SU->isScheduled; |
| 127 | } |
| 128 | |
| 129 | static |
| 130 | void Filter(std::vector<SUnit *> &List) { |
| 131 | List.erase(std::remove_if(List.begin(), List.end(), IsUnScheduled), List.end()); |
| 132 | } |
| 133 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 134 | void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) { |
Vincent Lejeune | 5121197 | 2013-06-05 20:27:35 +0000 | [diff] [blame^] | 135 | if (IsTopNode) { |
| 136 | for (unsigned i = 0; i < AluLast; i++) { |
| 137 | Filter(Available[i]); |
| 138 | Filter(Pending[i]); |
| 139 | } |
| 140 | } |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 141 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 142 | if (NextInstKind != CurInstKind) { |
| 143 | DEBUG(dbgs() << "Instruction Type Switch\n"); |
| 144 | if (NextInstKind != IDAlu) |
| 145 | OccupedSlotsMask = 15; |
| 146 | CurEmitted = 0; |
| 147 | CurInstKind = NextInstKind; |
| 148 | } |
| 149 | |
| 150 | if (CurInstKind == IDAlu) { |
| 151 | switch (getAluKind(SU)) { |
| 152 | case AluT_XYZW: |
| 153 | CurEmitted += 4; |
| 154 | break; |
| 155 | case AluDiscarded: |
| 156 | break; |
| 157 | default: { |
| 158 | ++CurEmitted; |
| 159 | for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(), |
| 160 | E = SU->getInstr()->operands_end(); It != E; ++It) { |
| 161 | MachineOperand &MO = *It; |
| 162 | if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X) |
| 163 | ++CurEmitted; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } else { |
| 168 | ++CurEmitted; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n"); |
| 173 | |
| 174 | if (CurInstKind != IDFetch) { |
| 175 | MoveUnits(Pending[IDFetch], Available[IDFetch]); |
| 176 | } |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Vincent Lejeune | 5121197 | 2013-06-05 20:27:35 +0000 | [diff] [blame^] | 179 | static bool |
| 180 | isPhysicalRegCopy(MachineInstr *MI) { |
| 181 | if (MI->getOpcode() != AMDGPU::COPY) |
| 182 | return false; |
| 183 | |
| 184 | return !TargetRegisterInfo::isVirtualRegister(MI->getOperand(1).getReg()); |
| 185 | } |
| 186 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 187 | void R600SchedStrategy::releaseTopNode(SUnit *SU) { |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 188 | DEBUG(dbgs() << "Top Releasing ";SU->dump(DAG);); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void R600SchedStrategy::releaseBottomNode(SUnit *SU) { |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 192 | DEBUG(dbgs() << "Bottom Releasing ";SU->dump(DAG);); |
Vincent Lejeune | 5121197 | 2013-06-05 20:27:35 +0000 | [diff] [blame^] | 193 | if (isPhysicalRegCopy(SU->getInstr())) { |
| 194 | PhysicalRegCopy.push_back(SU); |
| 195 | return; |
| 196 | } |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 197 | |
| 198 | int IK = getInstKind(SU); |
Tom Stellard | ad7ecc6 | 2013-06-05 03:43:06 +0000 | [diff] [blame] | 199 | |
| 200 | // Check for AR register defines |
| 201 | for (MachineInstr::const_mop_iterator I = SU->getInstr()->operands_begin(), |
| 202 | E = SU->getInstr()->operands_end(); |
| 203 | I != E; ++I) { |
| 204 | if (I->isReg() && I->getReg() == AMDGPU::AR_X) { |
| 205 | if (I->isDef()) { |
| 206 | UnscheduledARDefs.push_back(SU); |
| 207 | } else { |
| 208 | UnscheduledARUses.push_back(SU); |
| 209 | } |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 214 | // There is no export clause, we can schedule one as soon as its ready |
| 215 | if (IK == IDOther) |
| 216 | Available[IDOther].push_back(SU); |
| 217 | else |
| 218 | Pending[IK].push_back(SU); |
| 219 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool R600SchedStrategy::regBelongsToClass(unsigned Reg, |
| 223 | const TargetRegisterClass *RC) const { |
| 224 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 225 | return RC->contains(Reg); |
| 226 | } else { |
| 227 | return MRI->getRegClass(Reg) == RC; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const { |
| 232 | MachineInstr *MI = SU->getInstr(); |
| 233 | |
| 234 | switch (MI->getOpcode()) { |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 235 | case AMDGPU::PRED_X: |
| 236 | return AluPredX; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 237 | case AMDGPU::INTERP_PAIR_XY: |
| 238 | case AMDGPU::INTERP_PAIR_ZW: |
| 239 | case AMDGPU::INTERP_VEC_LOAD: |
Vincent Lejeune | 4ed9917 | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 240 | case AMDGPU::DOT_4: |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 241 | return AluT_XYZW; |
| 242 | case AMDGPU::COPY: |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 243 | if (MI->getOperand(1).isUndef()) { |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 244 | // MI will become a KILL, don't considers it in scheduling |
| 245 | return AluDiscarded; |
| 246 | } |
| 247 | default: |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | // Does the instruction take a whole IG ? |
| 252 | if(TII->isVector(*MI) || |
| 253 | TII->isCubeOp(MI->getOpcode()) || |
| 254 | TII->isReductionOp(MI->getOpcode())) |
| 255 | return AluT_XYZW; |
| 256 | |
| 257 | // Is the result already assigned to a channel ? |
| 258 | unsigned DestSubReg = MI->getOperand(0).getSubReg(); |
| 259 | switch (DestSubReg) { |
| 260 | case AMDGPU::sub0: |
| 261 | return AluT_X; |
| 262 | case AMDGPU::sub1: |
| 263 | return AluT_Y; |
| 264 | case AMDGPU::sub2: |
| 265 | return AluT_Z; |
| 266 | case AMDGPU::sub3: |
| 267 | return AluT_W; |
| 268 | default: |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | // Is the result already member of a X/Y/Z/W class ? |
| 273 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 274 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) || |
| 275 | regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass)) |
| 276 | return AluT_X; |
| 277 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass)) |
| 278 | return AluT_Y; |
| 279 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass)) |
| 280 | return AluT_Z; |
| 281 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass)) |
| 282 | return AluT_W; |
| 283 | if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass)) |
| 284 | return AluT_XYZW; |
| 285 | |
| 286 | return AluAny; |
| 287 | |
| 288 | } |
| 289 | |
| 290 | int R600SchedStrategy::getInstKind(SUnit* SU) { |
| 291 | int Opcode = SU->getInstr()->getOpcode(); |
| 292 | |
Vincent Lejeune | f63f85a | 2013-05-17 16:50:37 +0000 | [diff] [blame] | 293 | if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode)) |
| 294 | return IDFetch; |
| 295 | |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 296 | if (TII->isALUInstr(Opcode)) { |
| 297 | return IDAlu; |
| 298 | } |
| 299 | |
| 300 | switch (Opcode) { |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 301 | case AMDGPU::PRED_X: |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 302 | case AMDGPU::COPY: |
| 303 | case AMDGPU::CONST_COPY: |
| 304 | case AMDGPU::INTERP_PAIR_XY: |
| 305 | case AMDGPU::INTERP_PAIR_ZW: |
| 306 | case AMDGPU::INTERP_VEC_LOAD: |
Vincent Lejeune | 4ed9917 | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 307 | case AMDGPU::DOT_4: |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 308 | return IDAlu; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 309 | default: |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 310 | return IDOther; |
| 311 | } |
| 312 | } |
| 313 | |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 314 | SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q) { |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 315 | if (Q.empty()) |
| 316 | return NULL; |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 317 | for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 318 | It != E; ++It) { |
| 319 | SUnit *SU = *It; |
Vincent Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 320 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
| 321 | if (TII->canBundle(InstructionsGroupCandidate)) { |
| 322 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 323 | Q.erase((It + 1).base()); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 324 | return SU; |
Vincent Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 325 | } else { |
| 326 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | void R600SchedStrategy::LoadAlu() { |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 333 | std::vector<SUnit *> &QSrc = Pending[IDAlu]; |
| 334 | for (unsigned i = 0, e = QSrc.size(); i < e; ++i) { |
| 335 | AluKind AK = getAluKind(QSrc[i]); |
| 336 | AvailableAlus[AK].push_back(QSrc[i]); |
| 337 | } |
| 338 | QSrc.clear(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void R600SchedStrategy::PrepareNextSlot() { |
| 342 | DEBUG(dbgs() << "New Slot\n"); |
| 343 | assert (OccupedSlotsMask && "Slot wasn't filled"); |
| 344 | OccupedSlotsMask = 0; |
Vincent Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 345 | InstructionsGroupCandidate.clear(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 346 | LoadAlu(); |
| 347 | } |
| 348 | |
| 349 | void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) { |
| 350 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 351 | // PressureRegister crashes if an operand is def and used in the same inst |
| 352 | // and we try to constraint its regclass |
| 353 | for (MachineInstr::mop_iterator It = MI->operands_begin(), |
| 354 | E = MI->operands_end(); It != E; ++It) { |
| 355 | MachineOperand &MO = *It; |
| 356 | if (MO.isReg() && !MO.isDef() && |
| 357 | MO.getReg() == MI->getOperand(0).getReg()) |
| 358 | return; |
| 359 | } |
| 360 | // Constrains the regclass of DestReg to assign it to Slot |
| 361 | switch (Slot) { |
| 362 | case 0: |
| 363 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass); |
| 364 | break; |
| 365 | case 1: |
| 366 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass); |
| 367 | break; |
| 368 | case 2: |
| 369 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass); |
| 370 | break; |
| 371 | case 3: |
| 372 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass); |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot) { |
| 378 | static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W}; |
| 379 | SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]]); |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 380 | if (SlotedSU) |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 381 | return SlotedSU; |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 382 | SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny]); |
| 383 | if (UnslotedSU) |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 384 | AssignSlot(UnslotedSU->getInstr(), Slot); |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 385 | return UnslotedSU; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | bool R600SchedStrategy::isAvailablesAluEmpty() const { |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 389 | return Pending[IDAlu].empty() && AvailableAlus[AluAny].empty() && |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 390 | AvailableAlus[AluT_XYZW].empty() && AvailableAlus[AluT_X].empty() && |
| 391 | AvailableAlus[AluT_Y].empty() && AvailableAlus[AluT_Z].empty() && |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 392 | AvailableAlus[AluT_W].empty() && AvailableAlus[AluDiscarded].empty() && |
| 393 | AvailableAlus[AluPredX].empty(); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | SUnit* R600SchedStrategy::pickAlu() { |
| 397 | while (!isAvailablesAluEmpty()) { |
| 398 | if (!OccupedSlotsMask) { |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 399 | // Bottom up scheduling : predX must comes first |
| 400 | if (!AvailableAlus[AluPredX].empty()) { |
| 401 | OccupedSlotsMask = 15; |
| 402 | return PopInst(AvailableAlus[AluPredX]); |
| 403 | } |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 404 | // Flush physical reg copies (RA will discard them) |
| 405 | if (!AvailableAlus[AluDiscarded].empty()) { |
| 406 | OccupedSlotsMask = 15; |
| 407 | return PopInst(AvailableAlus[AluDiscarded]); |
| 408 | } |
| 409 | // If there is a T_XYZW alu available, use it |
| 410 | if (!AvailableAlus[AluT_XYZW].empty()) { |
| 411 | OccupedSlotsMask = 15; |
| 412 | return PopInst(AvailableAlus[AluT_XYZW]); |
| 413 | } |
| 414 | } |
Vincent Lejeune | 76fc2d0 | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 415 | for (int Chan = 3; Chan > -1; --Chan) { |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 416 | bool isOccupied = OccupedSlotsMask & (1 << Chan); |
| 417 | if (!isOccupied) { |
| 418 | SUnit *SU = AttemptFillSlot(Chan); |
| 419 | if (SU) { |
| 420 | OccupedSlotsMask |= (1 << Chan); |
Vincent Lejeune | 3ab0ba3 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 421 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 422 | return SU; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | PrepareNextSlot(); |
| 427 | } |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | SUnit* R600SchedStrategy::pickOther(int QID) { |
| 432 | SUnit *SU = 0; |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 433 | std::vector<SUnit *> &AQ = Available[QID]; |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 434 | |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 435 | if (AQ.empty()) { |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 436 | MoveUnits(Pending[QID], AQ); |
| 437 | } |
Vincent Lejeune | 21ca0b3 | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 438 | if (!AQ.empty()) { |
| 439 | SU = AQ.back(); |
| 440 | AQ.resize(AQ.size() - 1); |
Vincent Lejeune | 62f38ca | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 441 | } |
| 442 | return SU; |
| 443 | } |
| 444 | |