Vincent Lejeune | 68b6b6d | 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 |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 15 | #include "R600MachineScheduler.h" |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 16 | #include "AMDGPUSubtarget.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/PassManager.h" |
NAKAMURA Takumi | 756cf88 | 2013-03-11 08:19:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
NAKAMURA Takumi | 756cf88 | 2013-03-11 08:19:28 +0000 | [diff] [blame] | 21 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "misched" |
| 25 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 26 | void R600SchedStrategy::initialize(ScheduleDAGMI *dag) { |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 27 | assert(dag->hasVRegLiveness() && "R600SchedStrategy needs vreg liveness"); |
| 28 | DAG = static_cast<ScheduleDAGMILive*>(dag); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 29 | TII = static_cast<const R600InstrInfo*>(DAG->TII); |
| 30 | TRI = static_cast<const R600RegisterInfo*>(DAG->TRI); |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 31 | VLIW5 = !DAG->MF.getTarget().getSubtarget<AMDGPUSubtarget>().hasCaymanISA(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 32 | MRI = &DAG->MRI; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 33 | CurInstKind = IDOther; |
| 34 | CurEmitted = 0; |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 35 | OccupedSlotsMask = 31; |
Vincent Lejeune | 80031d9f | 2013-04-03 16:49:34 +0000 | [diff] [blame] | 36 | InstKindLimit[IDAlu] = TII->getMaxAlusPerClause(); |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 37 | InstKindLimit[IDOther] = 32; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 38 | |
| 39 | const AMDGPUSubtarget &ST = DAG->TM.getSubtarget<AMDGPUSubtarget>(); |
Vincent Lejeune | f9f4e1e | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 40 | InstKindLimit[IDFetch] = ST.getTexVTXClauseSize(); |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 41 | AluInstCount = 0; |
| 42 | FetchInstCount = 0; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 45 | void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc, |
| 46 | std::vector<SUnit *> &QDst) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 47 | { |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 48 | QDst.insert(QDst.end(), QSrc.begin(), QSrc.end()); |
| 49 | QSrc.clear(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 52 | static |
| 53 | unsigned getWFCountLimitedByGPR(unsigned GPRCount) { |
| 54 | assert (GPRCount && "GPRCount cannot be 0"); |
| 55 | return 248 / GPRCount; |
| 56 | } |
| 57 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 58 | SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 59 | SUnit *SU = nullptr; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 60 | NextInstKind = IDOther; |
| 61 | |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 62 | IsTopNode = false; |
| 63 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 64 | // check if we might want to switch current clause type |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 65 | bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) || |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 66 | (Available[CurInstKind].empty()); |
Vincent Lejeune | f9f4e1e | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 67 | bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) && |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 68 | (!Available[IDFetch].empty() || !Available[IDOther].empty()); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 69 | |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 70 | if (CurInstKind == IDAlu && !Available[IDFetch].empty()) { |
| 71 | // We use the heuristic provided by AMD Accelerated Parallel Processing |
| 72 | // OpenCL Programming Guide : |
| 73 | // The approx. number of WF that allows TEX inst to hide ALU inst is : |
| 74 | // 500 (cycles for TEX) / (AluFetchRatio * 8 (cycles for ALU)) |
Andrew Trick | d7f890e | 2013-12-28 21:56:47 +0000 | [diff] [blame] | 75 | float ALUFetchRationEstimate = |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 76 | (AluInstCount + AvailablesAluCount() + Pending[IDAlu].size()) / |
| 77 | (FetchInstCount + Available[IDFetch].size()); |
Alexey Samsonov | cce5701 | 2014-09-17 17:47:21 +0000 | [diff] [blame] | 78 | if (ALUFetchRationEstimate == 0) { |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 79 | AllowSwitchFromAlu = true; |
Alexey Samsonov | cce5701 | 2014-09-17 17:47:21 +0000 | [diff] [blame] | 80 | } else { |
| 81 | unsigned NeededWF = 62.5f / ALUFetchRationEstimate; |
| 82 | DEBUG( dbgs() << NeededWF << " approx. Wavefronts Required\n" ); |
| 83 | // We assume the local GPR requirements to be "dominated" by the requirement |
| 84 | // of the TEX clause (which consumes 128 bits regs) ; ALU inst before and |
| 85 | // after TEX are indeed likely to consume or generate values from/for the |
| 86 | // TEX clause. |
| 87 | // Available[IDFetch].size() * 2 : GPRs required in the Fetch clause |
| 88 | // We assume that fetch instructions are either TnXYZW = TEX TnXYZW (need |
| 89 | // one GPR) or TmXYZW = TnXYZW (need 2 GPR). |
| 90 | // (TODO : use RegisterPressure) |
| 91 | // If we are going too use too many GPR, we flush Fetch instruction to lower |
| 92 | // register pressure on 128 bits regs. |
| 93 | unsigned NearRegisterRequirement = 2 * Available[IDFetch].size(); |
| 94 | if (NeededWF > getWFCountLimitedByGPR(NearRegisterRequirement)) |
| 95 | AllowSwitchFromAlu = true; |
| 96 | } |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Tom Stellard | aad5376 | 2013-06-05 03:43:06 +0000 | [diff] [blame] | 99 | if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) || |
| 100 | (!AllowSwitchFromAlu && CurInstKind == IDAlu))) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 101 | // try to pick ALU |
| 102 | SU = pickAlu(); |
Vincent Lejeune | 4b5b849 | 2013-06-05 20:27:35 +0000 | [diff] [blame] | 103 | if (!SU && !PhysicalRegCopy.empty()) { |
| 104 | SU = PhysicalRegCopy.front(); |
| 105 | PhysicalRegCopy.erase(PhysicalRegCopy.begin()); |
| 106 | } |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 107 | if (SU) { |
Vincent Lejeune | f9f4e1e | 2013-05-17 16:49:55 +0000 | [diff] [blame] | 108 | if (CurEmitted >= InstKindLimit[IDAlu]) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 109 | CurEmitted = 0; |
| 110 | NextInstKind = IDAlu; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (!SU) { |
| 115 | // try to pick FETCH |
| 116 | SU = pickOther(IDFetch); |
| 117 | if (SU) |
| 118 | NextInstKind = IDFetch; |
| 119 | } |
| 120 | |
| 121 | // try to pick other |
| 122 | if (!SU) { |
| 123 | SU = pickOther(IDOther); |
| 124 | if (SU) |
| 125 | NextInstKind = IDOther; |
| 126 | } |
| 127 | |
| 128 | DEBUG( |
| 129 | if (SU) { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 130 | dbgs() << " ** Pick node **\n"; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 131 | SU->dump(DAG); |
| 132 | } else { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 133 | dbgs() << "NO NODE \n"; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 134 | for (unsigned i = 0; i < DAG->SUnits.size(); i++) { |
| 135 | const SUnit &S = DAG->SUnits[i]; |
| 136 | if (!S.isScheduled) |
| 137 | S.dump(DAG); |
| 138 | } |
| 139 | } |
| 140 | ); |
| 141 | |
| 142 | return SU; |
| 143 | } |
| 144 | |
| 145 | void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 146 | if (NextInstKind != CurInstKind) { |
| 147 | DEBUG(dbgs() << "Instruction Type Switch\n"); |
| 148 | if (NextInstKind != IDAlu) |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 149 | OccupedSlotsMask |= 31; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 150 | CurEmitted = 0; |
| 151 | CurInstKind = NextInstKind; |
| 152 | } |
| 153 | |
| 154 | if (CurInstKind == IDAlu) { |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 155 | AluInstCount ++; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 156 | switch (getAluKind(SU)) { |
| 157 | case AluT_XYZW: |
| 158 | CurEmitted += 4; |
| 159 | break; |
| 160 | case AluDiscarded: |
| 161 | break; |
| 162 | default: { |
| 163 | ++CurEmitted; |
| 164 | for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(), |
| 165 | E = SU->getInstr()->operands_end(); It != E; ++It) { |
| 166 | MachineOperand &MO = *It; |
| 167 | if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X) |
| 168 | ++CurEmitted; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } else { |
| 173 | ++CurEmitted; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n"); |
| 178 | |
| 179 | if (CurInstKind != IDFetch) { |
| 180 | MoveUnits(Pending[IDFetch], Available[IDFetch]); |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 181 | } else |
| 182 | FetchInstCount++; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Vincent Lejeune | 4b5b849 | 2013-06-05 20:27:35 +0000 | [diff] [blame] | 185 | static bool |
| 186 | isPhysicalRegCopy(MachineInstr *MI) { |
| 187 | if (MI->getOpcode() != AMDGPU::COPY) |
| 188 | return false; |
| 189 | |
| 190 | return !TargetRegisterInfo::isVirtualRegister(MI->getOperand(1).getReg()); |
| 191 | } |
| 192 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 193 | void R600SchedStrategy::releaseTopNode(SUnit *SU) { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 194 | DEBUG(dbgs() << "Top Releasing ";SU->dump(DAG);); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void R600SchedStrategy::releaseBottomNode(SUnit *SU) { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 198 | DEBUG(dbgs() << "Bottom Releasing ";SU->dump(DAG);); |
Vincent Lejeune | 4b5b849 | 2013-06-05 20:27:35 +0000 | [diff] [blame] | 199 | if (isPhysicalRegCopy(SU->getInstr())) { |
| 200 | PhysicalRegCopy.push_back(SU); |
| 201 | return; |
| 202 | } |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 203 | |
| 204 | int IK = getInstKind(SU); |
Tom Stellard | aad5376 | 2013-06-05 03:43:06 +0000 | [diff] [blame] | 205 | |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 206 | // There is no export clause, we can schedule one as soon as its ready |
| 207 | if (IK == IDOther) |
| 208 | Available[IDOther].push_back(SU); |
| 209 | else |
| 210 | Pending[IK].push_back(SU); |
| 211 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | bool R600SchedStrategy::regBelongsToClass(unsigned Reg, |
| 215 | const TargetRegisterClass *RC) const { |
| 216 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 217 | return RC->contains(Reg); |
| 218 | } else { |
| 219 | return MRI->getRegClass(Reg) == RC; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const { |
| 224 | MachineInstr *MI = SU->getInstr(); |
| 225 | |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 226 | if (TII->isTransOnly(MI)) |
| 227 | return AluTrans; |
| 228 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 229 | switch (MI->getOpcode()) { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 230 | case AMDGPU::PRED_X: |
| 231 | return AluPredX; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 232 | case AMDGPU::INTERP_PAIR_XY: |
| 233 | case AMDGPU::INTERP_PAIR_ZW: |
| 234 | case AMDGPU::INTERP_VEC_LOAD: |
Vincent Lejeune | 519f21e | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 235 | case AMDGPU::DOT_4: |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 236 | return AluT_XYZW; |
| 237 | case AMDGPU::COPY: |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 238 | if (MI->getOperand(1).isUndef()) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 239 | // MI will become a KILL, don't considers it in scheduling |
| 240 | return AluDiscarded; |
| 241 | } |
| 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | // Does the instruction take a whole IG ? |
Tom Stellard | ce54033 | 2013-06-28 15:46:59 +0000 | [diff] [blame] | 247 | // XXX: Is it possible to add a helper function in R600InstrInfo that can |
| 248 | // be used here and in R600PacketizerList::isSoloInstruction() ? |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 249 | if(TII->isVector(*MI) || |
| 250 | TII->isCubeOp(MI->getOpcode()) || |
Tom Stellard | ce54033 | 2013-06-28 15:46:59 +0000 | [diff] [blame] | 251 | TII->isReductionOp(MI->getOpcode()) || |
| 252 | MI->getOpcode() == AMDGPU::GROUP_BARRIER) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 253 | return AluT_XYZW; |
Tom Stellard | ce54033 | 2013-06-28 15:46:59 +0000 | [diff] [blame] | 254 | } |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 255 | |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 256 | if (TII->isLDSInstr(MI->getOpcode())) { |
| 257 | return AluT_X; |
| 258 | } |
| 259 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 260 | // Is the result already assigned to a channel ? |
| 261 | unsigned DestSubReg = MI->getOperand(0).getSubReg(); |
| 262 | switch (DestSubReg) { |
| 263 | case AMDGPU::sub0: |
| 264 | return AluT_X; |
| 265 | case AMDGPU::sub1: |
| 266 | return AluT_Y; |
| 267 | case AMDGPU::sub2: |
| 268 | return AluT_Z; |
| 269 | case AMDGPU::sub3: |
| 270 | return AluT_W; |
| 271 | default: |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | // Is the result already member of a X/Y/Z/W class ? |
| 276 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 277 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) || |
| 278 | regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass)) |
| 279 | return AluT_X; |
| 280 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass)) |
| 281 | return AluT_Y; |
| 282 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass)) |
| 283 | return AluT_Z; |
| 284 | if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass)) |
| 285 | return AluT_W; |
| 286 | if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass)) |
| 287 | return AluT_XYZW; |
| 288 | |
Tom Stellard | 7f6fa4c | 2013-09-12 02:55:06 +0000 | [diff] [blame] | 289 | // LDS src registers cannot be used in the Trans slot. |
| 290 | if (TII->readsLDSSrcReg(MI)) |
| 291 | return AluT_XYZW; |
| 292 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 293 | return AluAny; |
| 294 | |
| 295 | } |
| 296 | |
| 297 | int R600SchedStrategy::getInstKind(SUnit* SU) { |
| 298 | int Opcode = SU->getInstr()->getOpcode(); |
| 299 | |
Vincent Lejeune | e958c8e | 2013-05-17 16:50:37 +0000 | [diff] [blame] | 300 | if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode)) |
| 301 | return IDFetch; |
| 302 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 303 | if (TII->isALUInstr(Opcode)) { |
| 304 | return IDAlu; |
| 305 | } |
| 306 | |
| 307 | switch (Opcode) { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 308 | case AMDGPU::PRED_X: |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 309 | case AMDGPU::COPY: |
| 310 | case AMDGPU::CONST_COPY: |
| 311 | case AMDGPU::INTERP_PAIR_XY: |
| 312 | case AMDGPU::INTERP_PAIR_ZW: |
| 313 | case AMDGPU::INTERP_VEC_LOAD: |
Vincent Lejeune | 519f21e | 2013-05-17 16:50:32 +0000 | [diff] [blame] | 314 | case AMDGPU::DOT_4: |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 315 | return IDAlu; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 316 | default: |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 317 | return IDOther; |
| 318 | } |
| 319 | } |
| 320 | |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 321 | SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 322 | if (Q.empty()) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 323 | return nullptr; |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 324 | for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 325 | It != E; ++It) { |
| 326 | SUnit *SU = *It; |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 327 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 328 | if (TII->fitsConstReadLimitations(InstructionsGroupCandidate) |
| 329 | && (!AnyALU || !TII->isVectorOnly(SU->getInstr())) |
| 330 | ) { |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 331 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 332 | Q.erase((It + 1).base()); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 333 | return SU; |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 334 | } else { |
| 335 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 338 | return nullptr; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void R600SchedStrategy::LoadAlu() { |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 342 | std::vector<SUnit *> &QSrc = Pending[IDAlu]; |
| 343 | for (unsigned i = 0, e = QSrc.size(); i < e; ++i) { |
| 344 | AluKind AK = getAluKind(QSrc[i]); |
| 345 | AvailableAlus[AK].push_back(QSrc[i]); |
| 346 | } |
| 347 | QSrc.clear(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void R600SchedStrategy::PrepareNextSlot() { |
| 351 | DEBUG(dbgs() << "New Slot\n"); |
| 352 | assert (OccupedSlotsMask && "Slot wasn't filled"); |
| 353 | OccupedSlotsMask = 0; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 354 | // if (HwGen == AMDGPUSubtarget::NORTHERN_ISLANDS) |
| 355 | // OccupedSlotsMask |= 16; |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 356 | InstructionsGroupCandidate.clear(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 357 | LoadAlu(); |
| 358 | } |
| 359 | |
| 360 | void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) { |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 361 | int DstIndex = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst); |
| 362 | if (DstIndex == -1) { |
| 363 | return; |
| 364 | } |
| 365 | unsigned DestReg = MI->getOperand(DstIndex).getReg(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 366 | // PressureRegister crashes if an operand is def and used in the same inst |
| 367 | // and we try to constraint its regclass |
| 368 | for (MachineInstr::mop_iterator It = MI->operands_begin(), |
| 369 | E = MI->operands_end(); It != E; ++It) { |
| 370 | MachineOperand &MO = *It; |
| 371 | if (MO.isReg() && !MO.isDef() && |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 372 | MO.getReg() == DestReg) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | // Constrains the regclass of DestReg to assign it to Slot |
| 376 | switch (Slot) { |
| 377 | case 0: |
| 378 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass); |
| 379 | break; |
| 380 | case 1: |
| 381 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass); |
| 382 | break; |
| 383 | case 2: |
| 384 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass); |
| 385 | break; |
| 386 | case 3: |
| 387 | MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 392 | SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 393 | static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W}; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 394 | SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 395 | if (SlotedSU) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 396 | return SlotedSU; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 397 | SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 398 | if (UnslotedSU) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 399 | AssignSlot(UnslotedSU->getInstr(), Slot); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 400 | return UnslotedSU; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 403 | unsigned R600SchedStrategy::AvailablesAluCount() const { |
| 404 | return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() + |
| 405 | AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() + |
| 406 | AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() + |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 407 | AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() + |
| 408 | AvailableAlus[AluPredX].size(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | SUnit* R600SchedStrategy::pickAlu() { |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 412 | while (AvailablesAluCount() || !Pending[IDAlu].empty()) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 413 | if (!OccupedSlotsMask) { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 414 | // Bottom up scheduling : predX must comes first |
| 415 | if (!AvailableAlus[AluPredX].empty()) { |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 416 | OccupedSlotsMask |= 31; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 417 | return PopInst(AvailableAlus[AluPredX], false); |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 418 | } |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 419 | // Flush physical reg copies (RA will discard them) |
| 420 | if (!AvailableAlus[AluDiscarded].empty()) { |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 421 | OccupedSlotsMask |= 31; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 422 | return PopInst(AvailableAlus[AluDiscarded], false); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 423 | } |
| 424 | // If there is a T_XYZW alu available, use it |
| 425 | if (!AvailableAlus[AluT_XYZW].empty()) { |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 426 | OccupedSlotsMask |= 15; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 427 | return PopInst(AvailableAlus[AluT_XYZW], false); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 430 | bool TransSlotOccuped = OccupedSlotsMask & 16; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 431 | if (!TransSlotOccuped && VLIW5) { |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 432 | if (!AvailableAlus[AluTrans].empty()) { |
| 433 | OccupedSlotsMask |= 16; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 434 | return PopInst(AvailableAlus[AluTrans], false); |
| 435 | } |
| 436 | SUnit *SU = AttemptFillSlot(3, true); |
| 437 | if (SU) { |
| 438 | OccupedSlotsMask |= 16; |
| 439 | return SU; |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 442 | for (int Chan = 3; Chan > -1; --Chan) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 443 | bool isOccupied = OccupedSlotsMask & (1 << Chan); |
| 444 | if (!isOccupied) { |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 445 | SUnit *SU = AttemptFillSlot(Chan, false); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 446 | if (SU) { |
| 447 | OccupedSlotsMask |= (1 << Chan); |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 448 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 449 | return SU; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | PrepareNextSlot(); |
| 454 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 455 | return nullptr; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | SUnit* R600SchedStrategy::pickOther(int QID) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 459 | SUnit *SU = nullptr; |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 460 | std::vector<SUnit *> &AQ = Available[QID]; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 461 | |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 462 | if (AQ.empty()) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 463 | MoveUnits(Pending[QID], AQ); |
| 464 | } |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 465 | if (!AQ.empty()) { |
| 466 | SU = AQ.back(); |
| 467 | AQ.resize(AQ.size() - 1); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 468 | } |
| 469 | return SU; |
| 470 | } |