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 | |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 14 | #include "GCNHazardRecognizer.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "AMDGPUSubtarget.h" |
Eugene Zelenko | 734bb7b | 2017-01-20 17:52:16 +0000 | [diff] [blame] | 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), |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 42 | ST(MF.getSubtarget<SISubtarget>()), |
| 43 | TII(*ST.getInstrInfo()) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 44 | MaxLookAhead = 5; |
| 45 | } |
| 46 | |
| 47 | void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { |
| 48 | EmitInstruction(SU->getInstr()); |
| 49 | } |
| 50 | |
| 51 | void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { |
| 52 | CurrCycleInstr = MI; |
| 53 | } |
| 54 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 55 | static bool isDivFMas(unsigned Opcode) { |
| 56 | return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64; |
| 57 | } |
| 58 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 59 | static bool isSGetReg(unsigned Opcode) { |
| 60 | return Opcode == AMDGPU::S_GETREG_B32; |
| 61 | } |
| 62 | |
| 63 | static bool isSSetReg(unsigned Opcode) { |
| 64 | return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32; |
| 65 | } |
| 66 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 67 | static bool isRWLane(unsigned Opcode) { |
| 68 | return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; |
| 69 | } |
| 70 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 71 | static bool isRFE(unsigned Opcode) { |
| 72 | return Opcode == AMDGPU::S_RFE_B64; |
| 73 | } |
| 74 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 75 | static bool isSMovRel(unsigned Opcode) { |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 76 | switch (Opcode) { |
| 77 | case AMDGPU::S_MOVRELS_B32: |
| 78 | case AMDGPU::S_MOVRELS_B64: |
| 79 | case AMDGPU::S_MOVRELD_B32: |
| 80 | case AMDGPU::S_MOVRELD_B64: |
| 81 | return true; |
| 82 | default: |
| 83 | return false; |
| 84 | } |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 87 | static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 88 | const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, |
| 89 | AMDGPU::OpName::simm16); |
| 90 | return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; |
| 91 | } |
| 92 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 93 | ScheduleHazardRecognizer::HazardType |
| 94 | GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 95 | MachineInstr *MI = SU->getInstr(); |
| 96 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 97 | if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 98 | return NoopHazard; |
| 99 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 100 | if (SIInstrInfo::isVMEM(*MI) && checkVMEMHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 101 | return NoopHazard; |
| 102 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 103 | if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) |
| 104 | return NoopHazard; |
| 105 | |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 106 | if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) |
| 107 | return NoopHazard; |
| 108 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 109 | if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) |
| 110 | return NoopHazard; |
| 111 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 112 | if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) |
| 113 | return NoopHazard; |
| 114 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 115 | if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) |
| 116 | return NoopHazard; |
| 117 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 118 | if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) |
| 119 | return NoopHazard; |
| 120 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 121 | if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) |
| 122 | return NoopHazard; |
| 123 | |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 124 | if ((TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) && |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 125 | checkReadM0Hazards(MI) > 0) |
| 126 | return NoopHazard; |
| 127 | |
| 128 | if (checkAnyInstHazards(MI) > 0) |
| 129 | return NoopHazard; |
| 130 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 131 | return NoHazard; |
| 132 | } |
| 133 | |
| 134 | unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) { |
| 135 | return PreEmitNoops(SU->getInstr()); |
| 136 | } |
| 137 | |
| 138 | unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 139 | int WaitStates = std::max(0, checkAnyInstHazards(MI)); |
| 140 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 141 | if (SIInstrInfo::isSMRD(*MI)) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 142 | return std::max(WaitStates, checkSMRDHazards(MI)); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 143 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 144 | if (SIInstrInfo::isVALU(*MI)) { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 145 | WaitStates = std::max(WaitStates, checkVALUHazards(MI)); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 146 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 147 | if (SIInstrInfo::isVMEM(*MI)) |
| 148 | WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 149 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 150 | if (SIInstrInfo::isDPP(*MI)) |
| 151 | WaitStates = std::max(WaitStates, checkDPPHazards(MI)); |
| 152 | |
| 153 | if (isDivFMas(MI->getOpcode())) |
| 154 | WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); |
| 155 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 156 | if (isRWLane(MI->getOpcode())) |
| 157 | WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); |
| 158 | |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 159 | if (TII.isVINTRP(*MI)) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 160 | WaitStates = std::max(WaitStates, checkReadM0Hazards(MI)); |
| 161 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 162 | return WaitStates; |
| 163 | } |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 164 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 165 | if (isSGetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 166 | return std::max(WaitStates, checkGetRegHazards(MI)); |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 167 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 168 | if (isSSetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 169 | return std::max(WaitStates, checkSetRegHazards(MI)); |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 170 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 171 | if (isRFE(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 172 | return std::max(WaitStates, checkRFEHazards(MI)); |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 173 | |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 174 | if (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 175 | return std::max(WaitStates, checkReadM0Hazards(MI)); |
| 176 | |
| 177 | return WaitStates; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void GCNHazardRecognizer::EmitNoop() { |
| 181 | EmittedInstrs.push_front(nullptr); |
| 182 | } |
| 183 | |
| 184 | void GCNHazardRecognizer::AdvanceCycle() { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 185 | // When the scheduler detects a stall, it will call AdvanceCycle() without |
| 186 | // emitting any instructions. |
| 187 | if (!CurrCycleInstr) |
| 188 | return; |
| 189 | |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 190 | unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 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>(); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 320 | int WaitStatesNeeded = 0; |
| 321 | |
| 322 | WaitStatesNeeded = checkSMEMSoftClauseHazards(SMRD); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 323 | |
| 324 | // This SMRD hazard only affects SI. |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 325 | if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 326 | return WaitStatesNeeded; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 327 | |
| 328 | // A read of an SGPR by SMRD instruction requires 4 wait states when the |
| 329 | // SGPR was written by a VALU instruction. |
| 330 | int SmrdSgprWaitStates = 4; |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 331 | auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 332 | |
| 333 | for (const MachineOperand &Use : SMRD->uses()) { |
| 334 | if (!Use.isReg()) |
| 335 | continue; |
| 336 | int WaitStatesNeededForUse = |
| 337 | SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); |
| 338 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 339 | } |
| 340 | return WaitStatesNeeded; |
| 341 | } |
| 342 | |
| 343 | int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 344 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 345 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 346 | if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 347 | return 0; |
| 348 | |
| 349 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 350 | |
| 351 | // A read of an SGPR by a VMEM instruction requires 5 wait states when the |
| 352 | // SGPR was written by a VALU Instruction. |
| 353 | int VmemSgprWaitStates = 5; |
| 354 | int WaitStatesNeeded = 0; |
| 355 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
| 356 | |
| 357 | for (const MachineOperand &Use : VMEM->uses()) { |
| 358 | if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg())) |
| 359 | continue; |
| 360 | |
| 361 | int WaitStatesNeededForUse = |
| 362 | VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); |
| 363 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 364 | } |
| 365 | return WaitStatesNeeded; |
| 366 | } |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 367 | |
| 368 | int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 369 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 370 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 371 | |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 372 | // Check for DPP VGPR read after VALU VGPR write and EXEC write. |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 373 | int DppVgprWaitStates = 2; |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 374 | int DppExecWaitStates = 5; |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 375 | int WaitStatesNeeded = 0; |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 376 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 377 | |
| 378 | for (const MachineOperand &Use : DPP->uses()) { |
| 379 | if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 380 | continue; |
| 381 | int WaitStatesNeededForUse = |
| 382 | DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg()); |
| 383 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 384 | } |
| 385 | |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 386 | WaitStatesNeeded = std::max( |
| 387 | WaitStatesNeeded, |
| 388 | DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn)); |
| 389 | |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 390 | return WaitStatesNeeded; |
| 391 | } |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 392 | |
| 393 | int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { |
| 394 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 395 | |
| 396 | // v_div_fmas requires 4 wait states after a write to vcc from a VALU |
| 397 | // instruction. |
| 398 | const int DivFMasWaitStates = 4; |
| 399 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
| 400 | int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn); |
| 401 | |
| 402 | return DivFMasWaitStates - WaitStatesNeeded; |
| 403 | } |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 404 | |
| 405 | int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { |
| 406 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 407 | unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); |
| 408 | |
| 409 | const int GetRegWaitStates = 2; |
| 410 | auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) { |
| 411 | return GetRegHWReg == getHWReg(TII, *MI); |
| 412 | }; |
| 413 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 414 | |
| 415 | return GetRegWaitStates - WaitStatesNeeded; |
| 416 | } |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 417 | |
| 418 | int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { |
| 419 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 420 | unsigned HWReg = getHWReg(TII, *SetRegInstr); |
| 421 | |
| 422 | const int SetRegWaitStates = |
| 423 | ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2; |
| 424 | auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) { |
| 425 | return HWReg == getHWReg(TII, *MI); |
| 426 | }; |
| 427 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 428 | return SetRegWaitStates - WaitStatesNeeded; |
| 429 | } |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 430 | |
| 431 | int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { |
| 432 | if (!MI.mayStore()) |
| 433 | return -1; |
| 434 | |
| 435 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 436 | unsigned Opcode = MI.getOpcode(); |
| 437 | const MCInstrDesc &Desc = MI.getDesc(); |
| 438 | |
| 439 | int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
| 440 | int VDataRCID = -1; |
| 441 | if (VDataIdx != -1) |
| 442 | VDataRCID = Desc.OpInfo[VDataIdx].RegClass; |
| 443 | |
| 444 | if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { |
Jan Vesely | e8cc395 | 2016-11-15 23:55:15 +0000 | [diff] [blame] | 445 | // There is no hazard if the instruction does not use vector regs |
| 446 | // (like wbinvl1) |
| 447 | if (VDataIdx == -1) |
| 448 | return -1; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 449 | // For MUBUF/MTBUF instructions this hazard only exists if the |
| 450 | // instruction is not using a register in the soffset field. |
| 451 | const MachineOperand *SOffset = |
| 452 | TII->getNamedOperand(MI, AMDGPU::OpName::soffset); |
| 453 | // If we have no soffset operand, then assume this field has been |
| 454 | // hardcoded to zero. |
| 455 | if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && |
| 456 | (!SOffset || !SOffset->isReg())) |
| 457 | return VDataIdx; |
| 458 | } |
| 459 | |
| 460 | // MIMG instructions create a hazard if they don't use a 256-bit T# and |
| 461 | // the store size is greater than 8 bytes and they have more than two bits |
| 462 | // of their dmask set. |
| 463 | // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. |
| 464 | if (TII->isMIMG(MI)) { |
| 465 | int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); |
| 466 | assert(SRsrcIdx != -1 && |
| 467 | AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); |
Tom Stellard | 6b9c1be | 2016-10-27 23:28:03 +0000 | [diff] [blame] | 468 | (void)SRsrcIdx; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (TII->isFLAT(MI)) { |
Matt Arsenault | 97279a8 | 2016-11-29 19:30:44 +0000 | [diff] [blame] | 472 | int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 473 | if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) |
| 474 | return DataIdx; |
| 475 | } |
| 476 | |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { |
| 481 | // This checks for the hazard where VMEM instructions that store more than |
| 482 | // 8 bytes can have there store data over written by the next instruction. |
| 483 | if (!ST.has12DWordStoreHazard()) |
| 484 | return 0; |
| 485 | |
| 486 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 487 | const MachineRegisterInfo &MRI = VALU->getParent()->getParent()->getRegInfo(); |
| 488 | |
| 489 | const int VALUWaitStates = 1; |
| 490 | int WaitStatesNeeded = 0; |
| 491 | |
| 492 | for (const MachineOperand &Def : VALU->defs()) { |
| 493 | if (!TRI->isVGPR(MRI, Def.getReg())) |
| 494 | continue; |
| 495 | unsigned Reg = Def.getReg(); |
| 496 | auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) { |
| 497 | int DataIdx = createsVALUHazard(*MI); |
| 498 | return DataIdx >= 0 && |
| 499 | TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg); |
| 500 | }; |
| 501 | int WaitStatesNeededForDef = |
| 502 | VALUWaitStates - getWaitStatesSince(IsHazardFn); |
| 503 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); |
| 504 | } |
| 505 | return WaitStatesNeeded; |
| 506 | } |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 507 | |
| 508 | int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { |
| 509 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 510 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 511 | const MachineRegisterInfo &MRI = |
| 512 | RWLane->getParent()->getParent()->getRegInfo(); |
| 513 | |
| 514 | const MachineOperand *LaneSelectOp = |
| 515 | TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); |
| 516 | |
| 517 | if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) |
| 518 | return 0; |
| 519 | |
| 520 | unsigned LaneSelectReg = LaneSelectOp->getReg(); |
| 521 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 522 | return TII->isVALU(*MI); |
| 523 | }; |
| 524 | |
| 525 | const int RWLaneWaitStates = 4; |
| 526 | int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn); |
| 527 | return RWLaneWaitStates - WaitStatesSince; |
| 528 | } |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 529 | |
| 530 | int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 531 | if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 532 | return 0; |
| 533 | |
| 534 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 535 | |
| 536 | const int RFEWaitStates = 1; |
| 537 | |
| 538 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 539 | return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS; |
| 540 | }; |
| 541 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 542 | return RFEWaitStates - WaitStatesNeeded; |
| 543 | } |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 544 | |
| 545 | int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) { |
| 546 | if (MI->isDebugValue()) |
| 547 | return 0; |
| 548 | |
| 549 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 550 | if (!ST.hasSMovFedHazard()) |
| 551 | return 0; |
| 552 | |
| 553 | // Check for any instruction reading an SGPR after a write from |
| 554 | // s_mov_fed_b32. |
| 555 | int MovFedWaitStates = 1; |
| 556 | int WaitStatesNeeded = 0; |
| 557 | |
| 558 | for (const MachineOperand &Use : MI->uses()) { |
| 559 | if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 560 | continue; |
| 561 | auto IsHazardFn = [] (MachineInstr *MI) { |
| 562 | return MI->getOpcode() == AMDGPU::S_MOV_FED_B32; |
| 563 | }; |
| 564 | int WaitStatesNeededForUse = |
| 565 | MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn); |
| 566 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 567 | } |
| 568 | |
| 569 | return WaitStatesNeeded; |
| 570 | } |
| 571 | |
| 572 | int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { |
| 573 | if (!ST.hasReadM0Hazard()) |
| 574 | return 0; |
| 575 | |
| 576 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 577 | int SMovRelWaitStates = 1; |
| 578 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 579 | return TII->isSALU(*MI); |
| 580 | }; |
| 581 | return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn); |
| 582 | } |