Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 1 | //===-- R600MachineScheduler.cpp - R600 Scheduler Interface -*- C++ -*-----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// R600 Machine Scheduler interface |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 14 | #include "R600MachineScheduler.h" |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 15 | #include "AMDGPUSubtarget.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "R600InstrInfo.h" |
Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.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 | |
Evandro Menezes | 0cd23f56 | 2017-07-11 22:08:28 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "machine-scheduler" |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 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; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 81 | LLVM_DEBUG(dbgs() << NeededWF << " approx. Wavefronts Required\n"); |
Alexey Samsonov | cce5701 | 2014-09-17 17:47:21 +0000 | [diff] [blame] | 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 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 127 | LLVM_DEBUG(if (SU) { |
| 128 | dbgs() << " ** Pick node **\n"; |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 129 | DAG->dumpNode(*SU); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 130 | } else { |
| 131 | dbgs() << "NO NODE \n"; |
| 132 | for (unsigned i = 0; i < DAG->SUnits.size(); i++) { |
| 133 | const SUnit &S = DAG->SUnits[i]; |
| 134 | if (!S.isScheduled) |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 135 | DAG->dumpNode(S); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 136 | } |
| 137 | }); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 138 | |
| 139 | return SU; |
| 140 | } |
| 141 | |
| 142 | void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 143 | if (NextInstKind != CurInstKind) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 144 | LLVM_DEBUG(dbgs() << "Instruction Type Switch\n"); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 145 | if (NextInstKind != IDAlu) |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 146 | OccupedSlotsMask |= 31; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 147 | CurEmitted = 0; |
| 148 | CurInstKind = NextInstKind; |
| 149 | } |
| 150 | |
| 151 | if (CurInstKind == IDAlu) { |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 152 | AluInstCount ++; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 153 | switch (getAluKind(SU)) { |
| 154 | case AluT_XYZW: |
| 155 | CurEmitted += 4; |
| 156 | break; |
| 157 | case AluDiscarded: |
| 158 | break; |
| 159 | default: { |
| 160 | ++CurEmitted; |
| 161 | for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(), |
| 162 | E = SU->getInstr()->operands_end(); It != E; ++It) { |
| 163 | MachineOperand &MO = *It; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 164 | if (MO.isReg() && MO.getReg() == R600::ALU_LITERAL_X) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 165 | ++CurEmitted; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } else { |
| 170 | ++CurEmitted; |
| 171 | } |
| 172 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 173 | LLVM_DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n"); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 174 | |
| 175 | if (CurInstKind != IDFetch) { |
| 176 | MoveUnits(Pending[IDFetch], Available[IDFetch]); |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 177 | } else |
| 178 | FetchInstCount++; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Vincent Lejeune | 4b5b849 | 2013-06-05 20:27:35 +0000 | [diff] [blame] | 181 | static bool |
| 182 | isPhysicalRegCopy(MachineInstr *MI) { |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 183 | if (MI->getOpcode() != R600::COPY) |
Vincent Lejeune | 4b5b849 | 2013-06-05 20:27:35 +0000 | [diff] [blame] | 184 | return false; |
| 185 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 186 | return !Register::isVirtualRegister(MI->getOperand(1).getReg()); |
Vincent Lejeune | 4b5b849 | 2013-06-05 20:27:35 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 189 | void R600SchedStrategy::releaseTopNode(SUnit *SU) { |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 190 | LLVM_DEBUG(dbgs() << "Top Releasing "; DAG->dumpNode(*SU)); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void R600SchedStrategy::releaseBottomNode(SUnit *SU) { |
Matthias Braun | 726e12c | 2018-09-19 00:23:35 +0000 | [diff] [blame] | 194 | LLVM_DEBUG(dbgs() << "Bottom Releasing "; DAG->dumpNode(*SU)); |
Vincent Lejeune | 4b5b849 | 2013-06-05 20:27:35 +0000 | [diff] [blame] | 195 | if (isPhysicalRegCopy(SU->getInstr())) { |
| 196 | PhysicalRegCopy.push_back(SU); |
| 197 | return; |
| 198 | } |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 199 | |
| 200 | int IK = getInstKind(SU); |
Tom Stellard | aad5376 | 2013-06-05 03:43:06 +0000 | [diff] [blame] | 201 | |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 202 | // There is no export clause, we can schedule one as soon as its ready |
| 203 | if (IK == IDOther) |
| 204 | Available[IDOther].push_back(SU); |
| 205 | else |
| 206 | Pending[IK].push_back(SU); |
| 207 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | bool R600SchedStrategy::regBelongsToClass(unsigned Reg, |
| 211 | const TargetRegisterClass *RC) const { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 212 | if (!Register::isVirtualRegister(Reg)) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 213 | return RC->contains(Reg); |
| 214 | } else { |
| 215 | return MRI->getRegClass(Reg) == RC; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const { |
| 220 | MachineInstr *MI = SU->getInstr(); |
| 221 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 222 | if (TII->isTransOnly(*MI)) |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 223 | return AluTrans; |
| 224 | |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 225 | switch (MI->getOpcode()) { |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 226 | case R600::PRED_X: |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 227 | return AluPredX; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 228 | case R600::INTERP_PAIR_XY: |
| 229 | case R600::INTERP_PAIR_ZW: |
| 230 | case R600::INTERP_VEC_LOAD: |
| 231 | case R600::DOT_4: |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 232 | return AluT_XYZW; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 233 | case R600::COPY: |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 234 | if (MI->getOperand(1).isUndef()) { |
| 235 | // MI will become a KILL, don't considers it in scheduling |
| 236 | return AluDiscarded; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 237 | } |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 238 | break; |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 239 | default: |
| 240 | break; |
| 241 | } |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 242 | |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 243 | // Does the instruction take a whole IG ? |
| 244 | // XXX: Is it possible to add a helper function in R600InstrInfo that can |
| 245 | // be used here and in R600PacketizerList::isSoloInstruction() ? |
| 246 | if(TII->isVector(*MI) || |
| 247 | TII->isCubeOp(MI->getOpcode()) || |
| 248 | TII->isReductionOp(MI->getOpcode()) || |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 249 | MI->getOpcode() == R600::GROUP_BARRIER) { |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 250 | return AluT_XYZW; |
| 251 | } |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 252 | |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 253 | if (TII->isLDSInstr(MI->getOpcode())) { |
| 254 | return AluT_X; |
| 255 | } |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 256 | |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 257 | // Is the result already assigned to a channel ? |
| 258 | unsigned DestSubReg = MI->getOperand(0).getSubReg(); |
| 259 | switch (DestSubReg) { |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 260 | case R600::sub0: |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 261 | return AluT_X; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 262 | case R600::sub1: |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 263 | return AluT_Y; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 264 | case R600::sub2: |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 265 | return AluT_Z; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 266 | case R600::sub3: |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 267 | return AluT_W; |
| 268 | default: |
| 269 | break; |
| 270 | } |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 271 | |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 272 | // Is the result already member of a X/Y/Z/W class ? |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 273 | Register DestReg = MI->getOperand(0).getReg(); |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 274 | if (regBelongsToClass(DestReg, &R600::R600_TReg32_XRegClass) || |
| 275 | regBelongsToClass(DestReg, &R600::R600_AddrRegClass)) |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 276 | return AluT_X; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 277 | if (regBelongsToClass(DestReg, &R600::R600_TReg32_YRegClass)) |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 278 | return AluT_Y; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 279 | if (regBelongsToClass(DestReg, &R600::R600_TReg32_ZRegClass)) |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 280 | return AluT_Z; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 281 | if (regBelongsToClass(DestReg, &R600::R600_TReg32_WRegClass)) |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 282 | return AluT_W; |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 283 | if (regBelongsToClass(DestReg, &R600::R600_Reg128RegClass)) |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 284 | return AluT_XYZW; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 285 | |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 286 | // LDS src registers cannot be used in the Trans slot. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 287 | if (TII->readsLDSSrcReg(*MI)) |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 288 | return AluT_XYZW; |
Tom Stellard | 7f6fa4c | 2013-09-12 02:55:06 +0000 | [diff] [blame] | 289 | |
Matt Arsenault | 180e0d5 | 2016-06-22 01:53:49 +0000 | [diff] [blame] | 290 | return AluAny; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | int R600SchedStrategy::getInstKind(SUnit* SU) { |
| 294 | int Opcode = SU->getInstr()->getOpcode(); |
| 295 | |
Vincent Lejeune | e958c8e | 2013-05-17 16:50:37 +0000 | [diff] [blame] | 296 | if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode)) |
| 297 | return IDFetch; |
| 298 | |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 299 | if (TII->isALUInstr(Opcode)) { |
| 300 | return IDAlu; |
| 301 | } |
| 302 | |
| 303 | switch (Opcode) { |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 304 | case R600::PRED_X: |
| 305 | case R600::COPY: |
| 306 | case R600::CONST_COPY: |
| 307 | case R600::INTERP_PAIR_XY: |
| 308 | case R600::INTERP_PAIR_ZW: |
| 309 | case R600::INTERP_VEC_LOAD: |
| 310 | case R600::DOT_4: |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 311 | return IDAlu; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 312 | default: |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 313 | return IDOther; |
| 314 | } |
| 315 | } |
| 316 | |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 317 | SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 318 | if (Q.empty()) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 319 | return nullptr; |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 320 | for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 321 | It != E; ++It) { |
| 322 | SUnit *SU = *It; |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 323 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 324 | if (TII->fitsConstReadLimitations(InstructionsGroupCandidate) && |
| 325 | (!AnyALU || !TII->isVectorOnly(*SU->getInstr()))) { |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 326 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 327 | Q.erase((It + 1).base()); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 328 | return SU; |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 329 | } else { |
| 330 | InstructionsGroupCandidate.pop_back(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 333 | return nullptr; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void R600SchedStrategy::LoadAlu() { |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 337 | std::vector<SUnit *> &QSrc = Pending[IDAlu]; |
| 338 | for (unsigned i = 0, e = QSrc.size(); i < e; ++i) { |
| 339 | AluKind AK = getAluKind(QSrc[i]); |
| 340 | AvailableAlus[AK].push_back(QSrc[i]); |
| 341 | } |
| 342 | QSrc.clear(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void R600SchedStrategy::PrepareNextSlot() { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 346 | LLVM_DEBUG(dbgs() << "New Slot\n"); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 347 | assert (OccupedSlotsMask && "Slot wasn't filled"); |
| 348 | OccupedSlotsMask = 0; |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 349 | // if (HwGen == AMDGPUSubtarget::NORTHERN_ISLANDS) |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 350 | // OccupedSlotsMask |= 16; |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 351 | InstructionsGroupCandidate.clear(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 352 | LoadAlu(); |
| 353 | } |
| 354 | |
| 355 | void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) { |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 356 | int DstIndex = TII->getOperandIdx(MI->getOpcode(), R600::OpName::dst); |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 357 | if (DstIndex == -1) { |
| 358 | return; |
| 359 | } |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 360 | Register DestReg = MI->getOperand(DstIndex).getReg(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 361 | // PressureRegister crashes if an operand is def and used in the same inst |
| 362 | // and we try to constraint its regclass |
| 363 | for (MachineInstr::mop_iterator It = MI->operands_begin(), |
| 364 | E = MI->operands_end(); It != E; ++It) { |
| 365 | MachineOperand &MO = *It; |
| 366 | if (MO.isReg() && !MO.isDef() && |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 367 | MO.getReg() == DestReg) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | // Constrains the regclass of DestReg to assign it to Slot |
| 371 | switch (Slot) { |
| 372 | case 0: |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 373 | MRI->constrainRegClass(DestReg, &R600::R600_TReg32_XRegClass); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 374 | break; |
| 375 | case 1: |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 376 | MRI->constrainRegClass(DestReg, &R600::R600_TReg32_YRegClass); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 377 | break; |
| 378 | case 2: |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 379 | MRI->constrainRegClass(DestReg, &R600::R600_TReg32_ZRegClass); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 380 | break; |
| 381 | case 3: |
Tom Stellard | c5a154d | 2018-06-28 23:47:12 +0000 | [diff] [blame] | 382 | MRI->constrainRegClass(DestReg, &R600::R600_TReg32_WRegClass); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 387 | SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 388 | static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W}; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 389 | SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 390 | if (SlotedSU) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 391 | return SlotedSU; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 392 | SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 393 | if (UnslotedSU) |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 394 | AssignSlot(UnslotedSU->getInstr(), Slot); |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 395 | return UnslotedSU; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 398 | unsigned R600SchedStrategy::AvailablesAluCount() const { |
| 399 | return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() + |
| 400 | AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() + |
| 401 | AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() + |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 402 | AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() + |
| 403 | AvailableAlus[AluPredX].size(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | SUnit* R600SchedStrategy::pickAlu() { |
Vincent Lejeune | d1a9d18 | 2013-06-07 23:30:34 +0000 | [diff] [blame] | 407 | while (AvailablesAluCount() || !Pending[IDAlu].empty()) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 408 | if (!OccupedSlotsMask) { |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 409 | // Bottom up scheduling : predX must comes first |
| 410 | if (!AvailableAlus[AluPredX].empty()) { |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 411 | OccupedSlotsMask |= 31; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 412 | return PopInst(AvailableAlus[AluPredX], false); |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 413 | } |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 414 | // Flush physical reg copies (RA will discard them) |
| 415 | if (!AvailableAlus[AluDiscarded].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[AluDiscarded], false); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 418 | } |
| 419 | // If there is a T_XYZW alu available, use it |
| 420 | if (!AvailableAlus[AluT_XYZW].empty()) { |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 421 | OccupedSlotsMask |= 15; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 422 | return PopInst(AvailableAlus[AluT_XYZW], false); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 425 | bool TransSlotOccuped = OccupedSlotsMask & 16; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 426 | if (!TransSlotOccuped && VLIW5) { |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 427 | if (!AvailableAlus[AluTrans].empty()) { |
| 428 | OccupedSlotsMask |= 16; |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 429 | return PopInst(AvailableAlus[AluTrans], false); |
| 430 | } |
| 431 | SUnit *SU = AttemptFillSlot(3, true); |
| 432 | if (SU) { |
| 433 | OccupedSlotsMask |= 16; |
| 434 | return SU; |
Vincent Lejeune | 77a8352 | 2013-06-29 19:32:43 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
Vincent Lejeune | 3d5118c | 2013-05-17 16:50:56 +0000 | [diff] [blame] | 437 | for (int Chan = 3; Chan > -1; --Chan) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 438 | bool isOccupied = OccupedSlotsMask & (1 << Chan); |
| 439 | if (!isOccupied) { |
Vincent Lejeune | 7e2c832 | 2013-09-04 19:53:46 +0000 | [diff] [blame] | 440 | SUnit *SU = AttemptFillSlot(Chan, false); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 441 | if (SU) { |
| 442 | OccupedSlotsMask |= (1 << Chan); |
Vincent Lejeune | 0a22bc4 | 2013-03-14 15:50:45 +0000 | [diff] [blame] | 443 | InstructionsGroupCandidate.push_back(SU->getInstr()); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 444 | return SU; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | PrepareNextSlot(); |
| 449 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 450 | return nullptr; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | SUnit* R600SchedStrategy::pickOther(int QID) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 454 | SUnit *SU = nullptr; |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 455 | std::vector<SUnit *> &AQ = Available[QID]; |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 456 | |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 457 | if (AQ.empty()) { |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 458 | MoveUnits(Pending[QID], AQ); |
| 459 | } |
Vincent Lejeune | 4c81d4d | 2013-05-17 16:50:44 +0000 | [diff] [blame] | 460 | if (!AQ.empty()) { |
| 461 | SU = AQ.back(); |
George Burgess IV | c72204d | 2018-06-11 22:58:32 +0000 | [diff] [blame] | 462 | AQ.pop_back(); |
Vincent Lejeune | 68b6b6d | 2013-03-05 18:41:32 +0000 | [diff] [blame] | 463 | } |
| 464 | return SU; |
| 465 | } |