Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 1 | //===-- GCNHazardRecognizers.cpp - GCN Hazard Recognizer Impls ------------===// |
| 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 | // This file implements hazard recognizers for scheduling on GCN processors. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 14 | #include "AMDGPUSubtarget.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 15 | #include "GCNHazardRecognizer.h" |
| 16 | #include "SIDefines.h" |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 17 | #include "SIInstrInfo.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 18 | #include "SIRegisterInfo.h" |
| 19 | #include "Utils/AMDGPUBaseInfo.h" |
| 20 | #include "llvm/ADT/iterator_range.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstr.h" |
| 23 | #include "llvm/CodeGen/MachineOperand.h" |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/ScheduleDAG.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCInstrDesc.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include <algorithm> |
| 28 | #include <cassert> |
| 29 | #include <limits> |
| 30 | #include <set> |
| 31 | #include <vector> |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Hazard Recoginizer Implementation |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) : |
| 40 | CurrCycleInstr(nullptr), |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 41 | MF(MF), |
| 42 | ST(MF.getSubtarget<SISubtarget>()) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 43 | MaxLookAhead = 5; |
| 44 | } |
| 45 | |
| 46 | void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { |
| 47 | EmitInstruction(SU->getInstr()); |
| 48 | } |
| 49 | |
| 50 | void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { |
| 51 | CurrCycleInstr = MI; |
| 52 | } |
| 53 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 54 | static bool isDivFMas(unsigned Opcode) { |
| 55 | return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64; |
| 56 | } |
| 57 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 58 | static bool isSGetReg(unsigned Opcode) { |
| 59 | return Opcode == AMDGPU::S_GETREG_B32; |
| 60 | } |
| 61 | |
| 62 | static bool isSSetReg(unsigned Opcode) { |
| 63 | return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32; |
| 64 | } |
| 65 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 66 | static bool isRWLane(unsigned Opcode) { |
| 67 | return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; |
| 68 | } |
| 69 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 70 | static bool isRFE(unsigned Opcode) { |
| 71 | return Opcode == AMDGPU::S_RFE_B64; |
| 72 | } |
| 73 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 74 | static bool isSMovRel(unsigned Opcode) { |
| 75 | return Opcode == AMDGPU::S_MOVRELS_B32 || AMDGPU::S_MOVRELS_B64 || |
| 76 | Opcode == AMDGPU::S_MOVRELD_B32 || AMDGPU::S_MOVRELD_B64; |
| 77 | } |
| 78 | |
| 79 | static bool isVInterp(unsigned Opcode) { |
| 80 | return Opcode == AMDGPU::V_INTERP_P1_F32 || |
| 81 | Opcode == AMDGPU::V_INTERP_P1_F32_16bank || |
| 82 | Opcode == AMDGPU::V_INTERP_P2_F32 || |
| 83 | Opcode == AMDGPU::V_INTERP_MOV_F32; |
| 84 | } |
| 85 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 86 | static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 87 | const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, |
| 88 | AMDGPU::OpName::simm16); |
| 89 | return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; |
| 90 | } |
| 91 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 92 | ScheduleHazardRecognizer::HazardType |
| 93 | GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 94 | MachineInstr *MI = SU->getInstr(); |
| 95 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 96 | if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 97 | return NoopHazard; |
| 98 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 99 | if (SIInstrInfo::isVMEM(*MI) && checkVMEMHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 100 | return NoopHazard; |
| 101 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 102 | if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) |
| 103 | return NoopHazard; |
| 104 | |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 105 | if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) |
| 106 | return NoopHazard; |
| 107 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 108 | if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) |
| 109 | return NoopHazard; |
| 110 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 111 | if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) |
| 112 | return NoopHazard; |
| 113 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 114 | if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) |
| 115 | return NoopHazard; |
| 116 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 117 | if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) |
| 118 | return NoopHazard; |
| 119 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 120 | if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) |
| 121 | return NoopHazard; |
| 122 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 123 | if ((isVInterp(MI->getOpcode()) || isSMovRel(MI->getOpcode())) && |
| 124 | checkReadM0Hazards(MI) > 0) |
| 125 | return NoopHazard; |
| 126 | |
| 127 | if (checkAnyInstHazards(MI) > 0) |
| 128 | return NoopHazard; |
| 129 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 130 | return NoHazard; |
| 131 | } |
| 132 | |
| 133 | unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) { |
| 134 | return PreEmitNoops(SU->getInstr()); |
| 135 | } |
| 136 | |
| 137 | unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 138 | int WaitStates = std::max(0, checkAnyInstHazards(MI)); |
| 139 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 140 | if (SIInstrInfo::isSMRD(*MI)) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 141 | return std::max(WaitStates, checkSMRDHazards(MI)); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 142 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 143 | if (SIInstrInfo::isVALU(*MI)) { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 144 | WaitStates = std::max(WaitStates, checkVALUHazards(MI)); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 145 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 146 | if (SIInstrInfo::isVMEM(*MI)) |
| 147 | WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 148 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 149 | if (SIInstrInfo::isDPP(*MI)) |
| 150 | WaitStates = std::max(WaitStates, checkDPPHazards(MI)); |
| 151 | |
| 152 | if (isDivFMas(MI->getOpcode())) |
| 153 | WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); |
| 154 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 155 | if (isRWLane(MI->getOpcode())) |
| 156 | WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); |
| 157 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 158 | if (isVInterp(MI->getOpcode())) |
| 159 | WaitStates = std::max(WaitStates, checkReadM0Hazards(MI)); |
| 160 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 161 | return WaitStates; |
| 162 | } |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 163 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 164 | if (isSGetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 165 | return std::max(WaitStates, checkGetRegHazards(MI)); |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 166 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 167 | if (isSSetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 168 | return std::max(WaitStates, checkSetRegHazards(MI)); |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 169 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 170 | if (isRFE(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 171 | return std::max(WaitStates, checkRFEHazards(MI)); |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 172 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 173 | if (isSMovRel(MI->getOpcode())) |
| 174 | return std::max(WaitStates, checkReadM0Hazards(MI)); |
| 175 | |
| 176 | return WaitStates; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void GCNHazardRecognizer::EmitNoop() { |
| 180 | EmittedInstrs.push_front(nullptr); |
| 181 | } |
| 182 | |
| 183 | void GCNHazardRecognizer::AdvanceCycle() { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 184 | // When the scheduler detects a stall, it will call AdvanceCycle() without |
| 185 | // emitting any instructions. |
| 186 | if (!CurrCycleInstr) |
| 187 | return; |
| 188 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 189 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 190 | unsigned NumWaitStates = TII->getNumWaitStates(*CurrCycleInstr); |
| 191 | |
| 192 | // Keep track of emitted instructions |
| 193 | EmittedInstrs.push_front(CurrCycleInstr); |
| 194 | |
| 195 | // Add a nullptr for each additional wait state after the first. Make sure |
| 196 | // not to add more than getMaxLookAhead() items to the list, since we |
| 197 | // truncate the list to that size right after this loop. |
| 198 | for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); |
| 199 | i < e; ++i) { |
| 200 | EmittedInstrs.push_front(nullptr); |
| 201 | } |
| 202 | |
| 203 | // getMaxLookahead() is the largest number of wait states we will ever need |
| 204 | // to insert, so there is no point in keeping track of more than that many |
| 205 | // wait states. |
| 206 | EmittedInstrs.resize(getMaxLookAhead()); |
| 207 | |
| 208 | CurrCycleInstr = nullptr; |
| 209 | } |
| 210 | |
| 211 | void GCNHazardRecognizer::RecedeCycle() { |
| 212 | llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); |
| 213 | } |
| 214 | |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | // Helper Functions |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 219 | int GCNHazardRecognizer::getWaitStatesSince( |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 220 | function_ref<bool(MachineInstr *)> IsHazard) { |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 221 | int WaitStates = -1; |
| 222 | for (MachineInstr *MI : EmittedInstrs) { |
| 223 | ++WaitStates; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 224 | if (!MI || !IsHazard(MI)) |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 225 | continue; |
| 226 | return WaitStates; |
| 227 | } |
| 228 | return std::numeric_limits<int>::max(); |
| 229 | } |
| 230 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 231 | int GCNHazardRecognizer::getWaitStatesSinceDef( |
| 232 | unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) { |
| 233 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 234 | |
| 235 | auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) { |
| 236 | return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI); |
| 237 | }; |
| 238 | |
| 239 | return getWaitStatesSince(IsHazardFn); |
| 240 | } |
| 241 | |
| 242 | int GCNHazardRecognizer::getWaitStatesSinceSetReg( |
| 243 | function_ref<bool(MachineInstr *)> IsHazard) { |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 244 | auto IsHazardFn = [IsHazard] (MachineInstr *MI) { |
| 245 | return isSSetReg(MI->getOpcode()) && IsHazard(MI); |
| 246 | }; |
| 247 | |
| 248 | return getWaitStatesSince(IsHazardFn); |
| 249 | } |
| 250 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 251 | //===----------------------------------------------------------------------===// |
| 252 | // No-op Hazard Detection |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 255 | static void addRegsToSet(iterator_range<MachineInstr::const_mop_iterator> Ops, |
| 256 | std::set<unsigned> &Set) { |
| 257 | for (const MachineOperand &Op : Ops) { |
| 258 | if (Op.isReg()) |
| 259 | Set.insert(Op.getReg()); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | int GCNHazardRecognizer::checkSMEMSoftClauseHazards(MachineInstr *SMEM) { |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 264 | // SMEM soft clause are only present on VI+ |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 265 | if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | |
| 268 | // A soft-clause is any group of consecutive SMEM instructions. The |
| 269 | // instructions in this group may return out of order and/or may be |
| 270 | // replayed (i.e. the same instruction issued more than once). |
| 271 | // |
| 272 | // In order to handle these situations correctly we need to make sure |
| 273 | // that when a clause has more than one instruction, no instruction in the |
| 274 | // clause writes to a register that is read another instruction in the clause |
| 275 | // (including itself). If we encounter this situaion, we need to break the |
| 276 | // clause by inserting a non SMEM instruction. |
| 277 | |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 278 | std::set<unsigned> ClauseDefs; |
| 279 | std::set<unsigned> ClauseUses; |
| 280 | |
| 281 | for (MachineInstr *MI : EmittedInstrs) { |
| 282 | |
| 283 | // When we hit a non-SMEM instruction then we have passed the start of the |
| 284 | // clause and we can stop. |
Aaron Ballman | 3bd56b3 | 2016-05-03 15:17:25 +0000 | [diff] [blame] | 285 | if (!MI || !SIInstrInfo::isSMRD(*MI)) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 286 | break; |
| 287 | |
| 288 | addRegsToSet(MI->defs(), ClauseDefs); |
| 289 | addRegsToSet(MI->uses(), ClauseUses); |
| 290 | } |
| 291 | |
| 292 | if (ClauseDefs.empty()) |
| 293 | return 0; |
| 294 | |
| 295 | // FIXME: When we support stores, we need to make sure not to put loads and |
| 296 | // stores in the same clause if they use the same address. For now, just |
| 297 | // start a new clause whenever we see a store. |
| 298 | if (SMEM->mayStore()) |
| 299 | return 1; |
| 300 | |
| 301 | addRegsToSet(SMEM->defs(), ClauseDefs); |
| 302 | addRegsToSet(SMEM->uses(), ClauseUses); |
| 303 | |
| 304 | std::vector<unsigned> Result(std::max(ClauseDefs.size(), ClauseUses.size())); |
| 305 | std::vector<unsigned>::iterator End; |
| 306 | |
| 307 | End = std::set_intersection(ClauseDefs.begin(), ClauseDefs.end(), |
| 308 | ClauseUses.begin(), ClauseUses.end(), Result.begin()); |
| 309 | |
| 310 | // If the set of defs and uses intersect then we cannot add this instruction |
| 311 | // to the clause, so we have a hazard. |
| 312 | if (End != Result.begin()) |
| 313 | return 1; |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 318 | int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 319 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 320 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 321 | int WaitStatesNeeded = 0; |
| 322 | |
| 323 | WaitStatesNeeded = checkSMEMSoftClauseHazards(SMRD); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 324 | |
| 325 | // This SMRD hazard only affects SI. |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 326 | if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 327 | return WaitStatesNeeded; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 328 | |
| 329 | // A read of an SGPR by SMRD instruction requires 4 wait states when the |
| 330 | // SGPR was written by a VALU instruction. |
| 331 | int SmrdSgprWaitStates = 4; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 332 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
| 333 | |
| 334 | for (const MachineOperand &Use : SMRD->uses()) { |
| 335 | if (!Use.isReg()) |
| 336 | continue; |
| 337 | int WaitStatesNeededForUse = |
| 338 | SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); |
| 339 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 340 | } |
| 341 | return WaitStatesNeeded; |
| 342 | } |
| 343 | |
| 344 | int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 345 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 346 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 347 | if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 348 | return 0; |
| 349 | |
| 350 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 351 | |
| 352 | // A read of an SGPR by a VMEM instruction requires 5 wait states when the |
| 353 | // SGPR was written by a VALU Instruction. |
| 354 | int VmemSgprWaitStates = 5; |
| 355 | int WaitStatesNeeded = 0; |
| 356 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
| 357 | |
| 358 | for (const MachineOperand &Use : VMEM->uses()) { |
| 359 | if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg())) |
| 360 | continue; |
| 361 | |
| 362 | int WaitStatesNeededForUse = |
| 363 | VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); |
| 364 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 365 | } |
| 366 | return WaitStatesNeeded; |
| 367 | } |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 368 | |
| 369 | int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 370 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 371 | |
| 372 | // Check for DPP VGPR read after VALU VGPR write. |
| 373 | int DppVgprWaitStates = 2; |
| 374 | int WaitStatesNeeded = 0; |
| 375 | |
| 376 | for (const MachineOperand &Use : DPP->uses()) { |
| 377 | if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 378 | continue; |
| 379 | int WaitStatesNeededForUse = |
| 380 | DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg()); |
| 381 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 382 | } |
| 383 | |
| 384 | return WaitStatesNeeded; |
| 385 | } |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 386 | |
| 387 | int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { |
| 388 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 389 | |
| 390 | // v_div_fmas requires 4 wait states after a write to vcc from a VALU |
| 391 | // instruction. |
| 392 | const int DivFMasWaitStates = 4; |
| 393 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
| 394 | int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn); |
| 395 | |
| 396 | return DivFMasWaitStates - WaitStatesNeeded; |
| 397 | } |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 398 | |
| 399 | int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { |
| 400 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 401 | unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); |
| 402 | |
| 403 | const int GetRegWaitStates = 2; |
| 404 | auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) { |
| 405 | return GetRegHWReg == getHWReg(TII, *MI); |
| 406 | }; |
| 407 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 408 | |
| 409 | return GetRegWaitStates - WaitStatesNeeded; |
| 410 | } |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 411 | |
| 412 | int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { |
| 413 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 414 | unsigned HWReg = getHWReg(TII, *SetRegInstr); |
| 415 | |
| 416 | const int SetRegWaitStates = |
| 417 | ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2; |
| 418 | auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) { |
| 419 | return HWReg == getHWReg(TII, *MI); |
| 420 | }; |
| 421 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 422 | return SetRegWaitStates - WaitStatesNeeded; |
| 423 | } |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 424 | |
| 425 | int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { |
| 426 | if (!MI.mayStore()) |
| 427 | return -1; |
| 428 | |
| 429 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 430 | unsigned Opcode = MI.getOpcode(); |
| 431 | const MCInstrDesc &Desc = MI.getDesc(); |
| 432 | |
| 433 | int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
| 434 | int VDataRCID = -1; |
| 435 | if (VDataIdx != -1) |
| 436 | VDataRCID = Desc.OpInfo[VDataIdx].RegClass; |
| 437 | |
| 438 | if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { |
Jan Vesely | e8cc395 | 2016-11-15 23:55:15 +0000 | [diff] [blame] | 439 | // There is no hazard if the instruction does not use vector regs |
| 440 | // (like wbinvl1) |
| 441 | if (VDataIdx == -1) |
| 442 | return -1; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 443 | // For MUBUF/MTBUF instructions this hazard only exists if the |
| 444 | // instruction is not using a register in the soffset field. |
| 445 | const MachineOperand *SOffset = |
| 446 | TII->getNamedOperand(MI, AMDGPU::OpName::soffset); |
| 447 | // If we have no soffset operand, then assume this field has been |
| 448 | // hardcoded to zero. |
| 449 | if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && |
| 450 | (!SOffset || !SOffset->isReg())) |
| 451 | return VDataIdx; |
| 452 | } |
| 453 | |
| 454 | // MIMG instructions create a hazard if they don't use a 256-bit T# and |
| 455 | // the store size is greater than 8 bytes and they have more than two bits |
| 456 | // of their dmask set. |
| 457 | // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. |
| 458 | if (TII->isMIMG(MI)) { |
| 459 | int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); |
| 460 | assert(SRsrcIdx != -1 && |
| 461 | AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); |
Tom Stellard | 6b9c1be | 2016-10-27 23:28:03 +0000 | [diff] [blame] | 462 | (void)SRsrcIdx; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if (TII->isFLAT(MI)) { |
Matt Arsenault | 97279a8 | 2016-11-29 19:30:44 +0000 | [diff] [blame] | 466 | int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 467 | if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) |
| 468 | return DataIdx; |
| 469 | } |
| 470 | |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { |
| 475 | // This checks for the hazard where VMEM instructions that store more than |
| 476 | // 8 bytes can have there store data over written by the next instruction. |
| 477 | if (!ST.has12DWordStoreHazard()) |
| 478 | return 0; |
| 479 | |
| 480 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 481 | const MachineRegisterInfo &MRI = VALU->getParent()->getParent()->getRegInfo(); |
| 482 | |
| 483 | const int VALUWaitStates = 1; |
| 484 | int WaitStatesNeeded = 0; |
| 485 | |
| 486 | for (const MachineOperand &Def : VALU->defs()) { |
| 487 | if (!TRI->isVGPR(MRI, Def.getReg())) |
| 488 | continue; |
| 489 | unsigned Reg = Def.getReg(); |
| 490 | auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) { |
| 491 | int DataIdx = createsVALUHazard(*MI); |
| 492 | return DataIdx >= 0 && |
| 493 | TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg); |
| 494 | }; |
| 495 | int WaitStatesNeededForDef = |
| 496 | VALUWaitStates - getWaitStatesSince(IsHazardFn); |
| 497 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); |
| 498 | } |
| 499 | return WaitStatesNeeded; |
| 500 | } |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 501 | |
| 502 | int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { |
| 503 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 504 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 505 | const MachineRegisterInfo &MRI = |
| 506 | RWLane->getParent()->getParent()->getRegInfo(); |
| 507 | |
| 508 | const MachineOperand *LaneSelectOp = |
| 509 | TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); |
| 510 | |
| 511 | if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) |
| 512 | return 0; |
| 513 | |
| 514 | unsigned LaneSelectReg = LaneSelectOp->getReg(); |
| 515 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 516 | return TII->isVALU(*MI); |
| 517 | }; |
| 518 | |
| 519 | const int RWLaneWaitStates = 4; |
| 520 | int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn); |
| 521 | return RWLaneWaitStates - WaitStatesSince; |
| 522 | } |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 523 | |
| 524 | int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 525 | if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 526 | return 0; |
| 527 | |
| 528 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 529 | |
| 530 | const int RFEWaitStates = 1; |
| 531 | |
| 532 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 533 | return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS; |
| 534 | }; |
| 535 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 536 | return RFEWaitStates - WaitStatesNeeded; |
| 537 | } |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame^] | 538 | |
| 539 | int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) { |
| 540 | if (MI->isDebugValue()) |
| 541 | return 0; |
| 542 | |
| 543 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 544 | if (!ST.hasSMovFedHazard()) |
| 545 | return 0; |
| 546 | |
| 547 | // Check for any instruction reading an SGPR after a write from |
| 548 | // s_mov_fed_b32. |
| 549 | int MovFedWaitStates = 1; |
| 550 | int WaitStatesNeeded = 0; |
| 551 | |
| 552 | for (const MachineOperand &Use : MI->uses()) { |
| 553 | if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 554 | continue; |
| 555 | auto IsHazardFn = [] (MachineInstr *MI) { |
| 556 | return MI->getOpcode() == AMDGPU::S_MOV_FED_B32; |
| 557 | }; |
| 558 | int WaitStatesNeededForUse = |
| 559 | MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn); |
| 560 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 561 | } |
| 562 | |
| 563 | return WaitStatesNeeded; |
| 564 | } |
| 565 | |
| 566 | int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { |
| 567 | if (!ST.hasReadM0Hazard()) |
| 568 | return 0; |
| 569 | |
| 570 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 571 | int SMovRelWaitStates = 1; |
| 572 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 573 | return TII->isSALU(*MI); |
| 574 | }; |
| 575 | return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn); |
| 576 | } |