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