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>()), |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 43 | TII(*ST.getInstrInfo()), |
| 44 | TRI(TII.getRegisterInfo()), |
| 45 | ClauseUses(TRI.getNumRegUnits()), |
| 46 | ClauseDefs(TRI.getNumRegUnits()) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 47 | MaxLookAhead = 5; |
| 48 | } |
| 49 | |
| 50 | void GCNHazardRecognizer::EmitInstruction(SUnit *SU) { |
| 51 | EmitInstruction(SU->getInstr()); |
| 52 | } |
| 53 | |
| 54 | void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) { |
| 55 | CurrCycleInstr = MI; |
| 56 | } |
| 57 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 58 | static bool isDivFMas(unsigned Opcode) { |
| 59 | return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64; |
| 60 | } |
| 61 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 62 | static bool isSGetReg(unsigned Opcode) { |
| 63 | return Opcode == AMDGPU::S_GETREG_B32; |
| 64 | } |
| 65 | |
| 66 | static bool isSSetReg(unsigned Opcode) { |
| 67 | return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32; |
| 68 | } |
| 69 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 70 | static bool isRWLane(unsigned Opcode) { |
| 71 | return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32; |
| 72 | } |
| 73 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 74 | static bool isRFE(unsigned Opcode) { |
| 75 | return Opcode == AMDGPU::S_RFE_B64; |
| 76 | } |
| 77 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 78 | static bool isSMovRel(unsigned Opcode) { |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 79 | switch (Opcode) { |
| 80 | case AMDGPU::S_MOVRELS_B32: |
| 81 | case AMDGPU::S_MOVRELS_B64: |
| 82 | case AMDGPU::S_MOVRELD_B32: |
| 83 | case AMDGPU::S_MOVRELD_B64: |
| 84 | return true; |
| 85 | default: |
| 86 | return false; |
| 87 | } |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 90 | static bool isSendMsgTraceDataOrGDS(const MachineInstr &MI) { |
| 91 | switch (MI.getOpcode()) { |
| 92 | case AMDGPU::S_SENDMSG: |
| 93 | case AMDGPU::S_SENDMSGHALT: |
| 94 | case AMDGPU::S_TTRACEDATA: |
| 95 | return true; |
| 96 | default: |
| 97 | // TODO: GDS |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 102 | static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) { |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 103 | const MachineOperand *RegOp = TII->getNamedOperand(RegInstr, |
| 104 | AMDGPU::OpName::simm16); |
| 105 | return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_; |
| 106 | } |
| 107 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 108 | ScheduleHazardRecognizer::HazardType |
| 109 | GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 110 | MachineInstr *MI = SU->getInstr(); |
| 111 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 112 | if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 113 | return NoopHazard; |
| 114 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 115 | // FIXME: Should flat be considered vmem? |
| 116 | if ((SIInstrInfo::isVMEM(*MI) || |
| 117 | SIInstrInfo::isFLAT(*MI)) |
| 118 | && checkVMEMHazards(MI) > 0) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 119 | return NoopHazard; |
| 120 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 121 | if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0) |
| 122 | return NoopHazard; |
| 123 | |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 124 | if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0) |
| 125 | return NoopHazard; |
| 126 | |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 127 | if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0) |
| 128 | return NoopHazard; |
| 129 | |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 130 | if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0) |
| 131 | return NoopHazard; |
| 132 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 133 | if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0) |
| 134 | return NoopHazard; |
| 135 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 136 | if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0) |
| 137 | return NoopHazard; |
| 138 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 139 | if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0) |
| 140 | return NoopHazard; |
| 141 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 142 | if (ST.hasReadM0MovRelInterpHazard() && |
| 143 | (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) && |
| 144 | checkReadM0Hazards(MI) > 0) |
| 145 | return NoopHazard; |
| 146 | |
| 147 | if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(*MI) && |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 148 | checkReadM0Hazards(MI) > 0) |
| 149 | return NoopHazard; |
| 150 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 151 | if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0) |
| 152 | return NoopHazard; |
| 153 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 154 | if (checkAnyInstHazards(MI) > 0) |
| 155 | return NoopHazard; |
| 156 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 157 | return NoHazard; |
| 158 | } |
| 159 | |
| 160 | unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) { |
| 161 | return PreEmitNoops(SU->getInstr()); |
| 162 | } |
| 163 | |
| 164 | unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 165 | int WaitStates = std::max(0, checkAnyInstHazards(MI)); |
| 166 | |
Aaron Ballman | 5c190d0 | 2016-05-02 14:48:03 +0000 | [diff] [blame] | 167 | if (SIInstrInfo::isSMRD(*MI)) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 168 | return std::max(WaitStates, checkSMRDHazards(MI)); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 169 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 170 | if (SIInstrInfo::isVALU(*MI)) |
| 171 | WaitStates = std::max(WaitStates, checkVALUHazards(MI)); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 172 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 173 | if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI)) |
| 174 | WaitStates = std::max(WaitStates, checkVMEMHazards(MI)); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 175 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 176 | if (SIInstrInfo::isDPP(*MI)) |
| 177 | WaitStates = std::max(WaitStates, checkDPPHazards(MI)); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 178 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 179 | if (isDivFMas(MI->getOpcode())) |
| 180 | WaitStates = std::max(WaitStates, checkDivFMasHazards(MI)); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 181 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 182 | if (isRWLane(MI->getOpcode())) |
| 183 | WaitStates = std::max(WaitStates, checkRWLaneHazards(MI)); |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 184 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 185 | if (MI->isInlineAsm()) |
| 186 | return std::max(WaitStates, checkInlineAsmHazards(MI)); |
| 187 | |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 188 | if (isSGetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 189 | return std::max(WaitStates, checkGetRegHazards(MI)); |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 190 | |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 191 | if (isSSetReg(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 192 | return std::max(WaitStates, checkSetRegHazards(MI)); |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 193 | |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 194 | if (isRFE(MI->getOpcode())) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 195 | return std::max(WaitStates, checkRFEHazards(MI)); |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 196 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 197 | if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) || |
| 198 | isSMovRel(MI->getOpcode()))) |
| 199 | return std::max(WaitStates, checkReadM0Hazards(MI)); |
| 200 | |
| 201 | if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(*MI)) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 202 | return std::max(WaitStates, checkReadM0Hazards(MI)); |
| 203 | |
| 204 | return WaitStates; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void GCNHazardRecognizer::EmitNoop() { |
| 208 | EmittedInstrs.push_front(nullptr); |
| 209 | } |
| 210 | |
| 211 | void GCNHazardRecognizer::AdvanceCycle() { |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 212 | // When the scheduler detects a stall, it will call AdvanceCycle() without |
| 213 | // emitting any instructions. |
| 214 | if (!CurrCycleInstr) |
| 215 | return; |
| 216 | |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 217 | unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 218 | |
| 219 | // Keep track of emitted instructions |
| 220 | EmittedInstrs.push_front(CurrCycleInstr); |
| 221 | |
| 222 | // Add a nullptr for each additional wait state after the first. Make sure |
| 223 | // not to add more than getMaxLookAhead() items to the list, since we |
| 224 | // truncate the list to that size right after this loop. |
| 225 | for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead()); |
| 226 | i < e; ++i) { |
| 227 | EmittedInstrs.push_front(nullptr); |
| 228 | } |
| 229 | |
| 230 | // getMaxLookahead() is the largest number of wait states we will ever need |
| 231 | // to insert, so there is no point in keeping track of more than that many |
| 232 | // wait states. |
| 233 | EmittedInstrs.resize(getMaxLookAhead()); |
| 234 | |
| 235 | CurrCycleInstr = nullptr; |
| 236 | } |
| 237 | |
| 238 | void GCNHazardRecognizer::RecedeCycle() { |
| 239 | llvm_unreachable("hazard recognizer does not support bottom-up scheduling."); |
| 240 | } |
| 241 | |
| 242 | //===----------------------------------------------------------------------===// |
| 243 | // Helper Functions |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 246 | int GCNHazardRecognizer::getWaitStatesSince( |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 247 | function_ref<bool(MachineInstr *)> IsHazard) { |
Nicolai Haehnle | 75c98c3 | 2017-09-01 16:56:32 +0000 | [diff] [blame] | 248 | int WaitStates = 0; |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 249 | for (MachineInstr *MI : EmittedInstrs) { |
Nicolai Haehnle | 75c98c3 | 2017-09-01 16:56:32 +0000 | [diff] [blame] | 250 | if (MI) { |
| 251 | if (IsHazard(MI)) |
| 252 | return WaitStates; |
| 253 | |
| 254 | unsigned Opcode = MI->getOpcode(); |
Nicolai Haehnle | 5238271 | 2017-09-06 13:50:13 +0000 | [diff] [blame] | 255 | if (Opcode == AMDGPU::DBG_VALUE || Opcode == AMDGPU::IMPLICIT_DEF || |
| 256 | Opcode == AMDGPU::INLINEASM) |
Nicolai Haehnle | 75c98c3 | 2017-09-01 16:56:32 +0000 | [diff] [blame] | 257 | continue; |
| 258 | } |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 259 | ++WaitStates; |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 260 | } |
| 261 | return std::numeric_limits<int>::max(); |
| 262 | } |
| 263 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 264 | int GCNHazardRecognizer::getWaitStatesSinceDef( |
| 265 | unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) { |
| 266 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 267 | |
| 268 | auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) { |
| 269 | return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI); |
| 270 | }; |
| 271 | |
| 272 | return getWaitStatesSince(IsHazardFn); |
| 273 | } |
| 274 | |
| 275 | int GCNHazardRecognizer::getWaitStatesSinceSetReg( |
| 276 | function_ref<bool(MachineInstr *)> IsHazard) { |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 277 | auto IsHazardFn = [IsHazard] (MachineInstr *MI) { |
| 278 | return isSSetReg(MI->getOpcode()) && IsHazard(MI); |
| 279 | }; |
| 280 | |
| 281 | return getWaitStatesSince(IsHazardFn); |
| 282 | } |
| 283 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 284 | //===----------------------------------------------------------------------===// |
| 285 | // No-op Hazard Detection |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 288 | static void addRegUnits(const SIRegisterInfo &TRI, |
| 289 | BitVector &BV, unsigned Reg) { |
| 290 | for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI) |
| 291 | BV.set(*RUI); |
| 292 | } |
| 293 | |
| 294 | static void addRegsToSet(const SIRegisterInfo &TRI, |
| 295 | iterator_range<MachineInstr::const_mop_iterator> Ops, |
| 296 | BitVector &Set) { |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 297 | for (const MachineOperand &Op : Ops) { |
| 298 | if (Op.isReg()) |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 299 | addRegUnits(TRI, Set, Op.getReg()); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 303 | void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) { |
| 304 | // XXX: Do we need to worry about implicit operands |
| 305 | addRegsToSet(TRI, MI.defs(), ClauseDefs); |
| 306 | addRegsToSet(TRI, MI.uses(), ClauseUses); |
| 307 | } |
| 308 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 309 | int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) { |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 310 | // SMEM soft clause are only present on VI+, and only matter if xnack is |
| 311 | // enabled. |
| 312 | if (!ST.isXNACKEnabled()) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 313 | return 0; |
| 314 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 315 | bool IsSMRD = TII.isSMRD(*MEM); |
| 316 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 317 | resetClause(); |
| 318 | |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 319 | // A soft-clause is any group of consecutive SMEM instructions. The |
| 320 | // instructions in this group may return out of order and/or may be |
| 321 | // replayed (i.e. the same instruction issued more than once). |
| 322 | // |
| 323 | // In order to handle these situations correctly we need to make sure |
| 324 | // that when a clause has more than one instruction, no instruction in the |
| 325 | // clause writes to a register that is read another instruction in the clause |
| 326 | // (including itself). If we encounter this situaion, we need to break the |
| 327 | // clause by inserting a non SMEM instruction. |
| 328 | |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 329 | for (MachineInstr *MI : EmittedInstrs) { |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 330 | // When we hit a non-SMEM instruction then we have passed the start of the |
| 331 | // clause and we can stop. |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 332 | if (!MI) |
| 333 | break; |
| 334 | |
| 335 | if (IsSMRD != SIInstrInfo::isSMRD(*MI)) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 336 | break; |
| 337 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 338 | addClauseInst(*MI); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 341 | if (ClauseDefs.none()) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 342 | return 0; |
| 343 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 344 | // We need to make sure not to put loads and stores in the same clause if they |
| 345 | // use the same address. For now, just start a new clause whenever we see a |
| 346 | // store. |
| 347 | if (MEM->mayStore()) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 348 | return 1; |
| 349 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 350 | addClauseInst(*MEM); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 351 | |
| 352 | // If the set of defs and uses intersect then we cannot add this instruction |
| 353 | // to the clause, so we have a hazard. |
Matt Arsenault | 03c67d1 | 2017-11-17 04:18:24 +0000 | [diff] [blame] | 354 | return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0; |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 357 | int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 358 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 359 | int WaitStatesNeeded = 0; |
| 360 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 361 | WaitStatesNeeded = checkSoftClauseHazards(SMRD); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 362 | |
| 363 | // This SMRD hazard only affects SI. |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 364 | if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS) |
Tom Stellard | 1f520e5 | 2016-05-02 17:39:06 +0000 | [diff] [blame] | 365 | return WaitStatesNeeded; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 366 | |
| 367 | // A read of an SGPR by SMRD instruction requires 4 wait states when the |
| 368 | // SGPR was written by a VALU instruction. |
| 369 | int SmrdSgprWaitStates = 4; |
Matt Arsenault | 59ece95 | 2017-03-17 21:36:28 +0000 | [diff] [blame] | 370 | auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; |
Marek Olsak | 2232243 | 2017-10-26 14:43:02 +0000 | [diff] [blame] | 371 | auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); }; |
| 372 | |
Matt Arsenault | 4512d0a | 2017-11-17 04:18:26 +0000 | [diff] [blame] | 373 | bool IsBufferSMRD = TII.isBufferSMRD(*SMRD); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 374 | |
| 375 | for (const MachineOperand &Use : SMRD->uses()) { |
| 376 | if (!Use.isReg()) |
| 377 | continue; |
| 378 | int WaitStatesNeededForUse = |
| 379 | SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); |
| 380 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
Marek Olsak | 2232243 | 2017-10-26 14:43:02 +0000 | [diff] [blame] | 381 | |
| 382 | // This fixes what appears to be undocumented hardware behavior in SI where |
| 383 | // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor |
| 384 | // needs some number of nops in between. We don't know how many we need, but |
| 385 | // let's use 4. This wasn't discovered before probably because the only |
| 386 | // case when this happens is when we expand a 64-bit pointer into a full |
| 387 | // descriptor and use s_buffer_load_dword instead of s_load_dword, which was |
| 388 | // probably never encountered in the closed-source land. |
| 389 | if (IsBufferSMRD) { |
| 390 | int WaitStatesNeededForUse = |
| 391 | SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), |
| 392 | IsBufferHazardDefFn); |
| 393 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 394 | } |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 395 | } |
Marek Olsak | 2232243 | 2017-10-26 14:43:02 +0000 | [diff] [blame] | 396 | |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 397 | return WaitStatesNeeded; |
| 398 | } |
| 399 | |
| 400 | int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 401 | if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 402 | return 0; |
| 403 | |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 404 | int WaitStatesNeeded = checkSoftClauseHazards(VMEM); |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 405 | |
| 406 | // A read of an SGPR by a VMEM instruction requires 5 wait states when the |
| 407 | // SGPR was written by a VALU Instruction. |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 408 | const int VmemSgprWaitStates = 5; |
| 409 | auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); }; |
Tom Stellard | cb6ba62 | 2016-04-30 00:23:06 +0000 | [diff] [blame] | 410 | |
| 411 | for (const MachineOperand &Use : VMEM->uses()) { |
| 412 | if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg())) |
| 413 | continue; |
| 414 | |
| 415 | int WaitStatesNeededForUse = |
| 416 | VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn); |
| 417 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 418 | } |
| 419 | return WaitStatesNeeded; |
| 420 | } |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 421 | |
| 422 | int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 423 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 424 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 425 | |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 426 | // Check for DPP VGPR read after VALU VGPR write and EXEC write. |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 427 | int DppVgprWaitStates = 2; |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 428 | int DppExecWaitStates = 5; |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 429 | int WaitStatesNeeded = 0; |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 430 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 431 | |
| 432 | for (const MachineOperand &Use : DPP->uses()) { |
| 433 | if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 434 | continue; |
| 435 | int WaitStatesNeededForUse = |
| 436 | DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg()); |
| 437 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 438 | } |
| 439 | |
Connor Abbott | 0075536 | 2017-08-04 01:09:43 +0000 | [diff] [blame] | 440 | WaitStatesNeeded = std::max( |
| 441 | WaitStatesNeeded, |
| 442 | DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn)); |
| 443 | |
Tom Stellard | a27007e | 2016-05-02 16:23:09 +0000 | [diff] [blame] | 444 | return WaitStatesNeeded; |
| 445 | } |
Tom Stellard | 5ab6154 | 2016-10-07 23:42:48 +0000 | [diff] [blame] | 446 | |
| 447 | int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) { |
| 448 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 449 | |
| 450 | // v_div_fmas requires 4 wait states after a write to vcc from a VALU |
| 451 | // instruction. |
| 452 | const int DivFMasWaitStates = 4; |
| 453 | auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); }; |
| 454 | int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn); |
| 455 | |
| 456 | return DivFMasWaitStates - WaitStatesNeeded; |
| 457 | } |
Tom Stellard | 961811c | 2016-10-15 00:58:14 +0000 | [diff] [blame] | 458 | |
| 459 | int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) { |
| 460 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 461 | unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr); |
| 462 | |
| 463 | const int GetRegWaitStates = 2; |
| 464 | auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) { |
| 465 | return GetRegHWReg == getHWReg(TII, *MI); |
| 466 | }; |
| 467 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 468 | |
| 469 | return GetRegWaitStates - WaitStatesNeeded; |
| 470 | } |
Tom Stellard | 30d3082 | 2016-10-27 20:39:09 +0000 | [diff] [blame] | 471 | |
| 472 | int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) { |
| 473 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 474 | unsigned HWReg = getHWReg(TII, *SetRegInstr); |
| 475 | |
| 476 | const int SetRegWaitStates = |
| 477 | ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2; |
| 478 | auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) { |
| 479 | return HWReg == getHWReg(TII, *MI); |
| 480 | }; |
| 481 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 482 | return SetRegWaitStates - WaitStatesNeeded; |
| 483 | } |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 484 | |
| 485 | int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) { |
| 486 | if (!MI.mayStore()) |
| 487 | return -1; |
| 488 | |
| 489 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 490 | unsigned Opcode = MI.getOpcode(); |
| 491 | const MCInstrDesc &Desc = MI.getDesc(); |
| 492 | |
| 493 | int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
| 494 | int VDataRCID = -1; |
| 495 | if (VDataIdx != -1) |
| 496 | VDataRCID = Desc.OpInfo[VDataIdx].RegClass; |
| 497 | |
| 498 | if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) { |
Jan Vesely | e8cc395 | 2016-11-15 23:55:15 +0000 | [diff] [blame] | 499 | // There is no hazard if the instruction does not use vector regs |
| 500 | // (like wbinvl1) |
| 501 | if (VDataIdx == -1) |
| 502 | return -1; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 503 | // For MUBUF/MTBUF instructions this hazard only exists if the |
| 504 | // instruction is not using a register in the soffset field. |
| 505 | const MachineOperand *SOffset = |
| 506 | TII->getNamedOperand(MI, AMDGPU::OpName::soffset); |
| 507 | // If we have no soffset operand, then assume this field has been |
| 508 | // hardcoded to zero. |
| 509 | if (AMDGPU::getRegBitWidth(VDataRCID) > 64 && |
| 510 | (!SOffset || !SOffset->isReg())) |
| 511 | return VDataIdx; |
| 512 | } |
| 513 | |
| 514 | // MIMG instructions create a hazard if they don't use a 256-bit T# and |
| 515 | // the store size is greater than 8 bytes and they have more than two bits |
| 516 | // of their dmask set. |
| 517 | // All our MIMG definitions use a 256-bit T#, so we can skip checking for them. |
| 518 | if (TII->isMIMG(MI)) { |
| 519 | int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); |
| 520 | assert(SRsrcIdx != -1 && |
| 521 | AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256); |
Tom Stellard | 6b9c1be | 2016-10-27 23:28:03 +0000 | [diff] [blame] | 522 | (void)SRsrcIdx; |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (TII->isFLAT(MI)) { |
Matt Arsenault | 97279a8 | 2016-11-29 19:30:44 +0000 | [diff] [blame] | 526 | int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 527 | if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64) |
| 528 | return DataIdx; |
| 529 | } |
| 530 | |
| 531 | return -1; |
| 532 | } |
| 533 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 534 | int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def, |
| 535 | const MachineRegisterInfo &MRI) { |
| 536 | // Helper to check for the hazard where VMEM instructions that store more than |
| 537 | // 8 bytes can have there store data over written by the next instruction. |
| 538 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 539 | |
| 540 | const int VALUWaitStates = 1; |
| 541 | int WaitStatesNeeded = 0; |
| 542 | |
| 543 | if (!TRI->isVGPR(MRI, Def.getReg())) |
| 544 | return WaitStatesNeeded; |
| 545 | unsigned Reg = Def.getReg(); |
| 546 | auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) { |
| 547 | int DataIdx = createsVALUHazard(*MI); |
| 548 | return DataIdx >= 0 && |
| 549 | TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg); |
| 550 | }; |
| 551 | int WaitStatesNeededForDef = |
| 552 | VALUWaitStates - getWaitStatesSince(IsHazardFn); |
| 553 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef); |
| 554 | |
| 555 | return WaitStatesNeeded; |
| 556 | } |
| 557 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 558 | int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) { |
| 559 | // This checks for the hazard where VMEM instructions that store more than |
| 560 | // 8 bytes can have there store data over written by the next instruction. |
| 561 | if (!ST.has12DWordStoreHazard()) |
| 562 | return 0; |
| 563 | |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 564 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 565 | int WaitStatesNeeded = 0; |
| 566 | |
| 567 | for (const MachineOperand &Def : VALU->defs()) { |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 568 | WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI)); |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 569 | } |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 570 | |
| 571 | return WaitStatesNeeded; |
| 572 | } |
| 573 | |
| 574 | int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) { |
| 575 | // This checks for hazards associated with inline asm statements. |
| 576 | // Since inline asms can contain just about anything, we use this |
| 577 | // to call/leverage other check*Hazard routines. Note that |
| 578 | // this function doesn't attempt to address all possible inline asm |
| 579 | // hazards (good luck), but is a collection of what has been |
| 580 | // problematic thus far. |
| 581 | |
| 582 | // see checkVALUHazards() |
| 583 | if (!ST.has12DWordStoreHazard()) |
| 584 | return 0; |
| 585 | |
| 586 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 587 | int WaitStatesNeeded = 0; |
| 588 | |
| 589 | for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands(); |
| 590 | I != E; ++I) { |
| 591 | const MachineOperand &Op = IA->getOperand(I); |
| 592 | if (Op.isReg() && Op.isDef()) { |
| 593 | WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI)); |
| 594 | } |
| 595 | } |
| 596 | |
Tom Stellard | b133fbb | 2016-10-27 23:05:31 +0000 | [diff] [blame] | 597 | return WaitStatesNeeded; |
| 598 | } |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 599 | |
| 600 | int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) { |
| 601 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 602 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Mark Searles | d29f24a | 2017-12-07 20:34:25 +0000 | [diff] [blame] | 603 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | 04051b5 | 2016-10-27 23:42:29 +0000 | [diff] [blame] | 604 | |
| 605 | const MachineOperand *LaneSelectOp = |
| 606 | TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1); |
| 607 | |
| 608 | if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg())) |
| 609 | return 0; |
| 610 | |
| 611 | unsigned LaneSelectReg = LaneSelectOp->getReg(); |
| 612 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 613 | return TII->isVALU(*MI); |
| 614 | }; |
| 615 | |
| 616 | const int RWLaneWaitStates = 4; |
| 617 | int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn); |
| 618 | return RWLaneWaitStates - WaitStatesSince; |
| 619 | } |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 620 | |
| 621 | int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) { |
Tom Stellard | aea899e | 2016-10-27 23:50:21 +0000 | [diff] [blame] | 622 | if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 623 | return 0; |
| 624 | |
| 625 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 626 | |
| 627 | const int RFEWaitStates = 1; |
| 628 | |
| 629 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 630 | return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS; |
| 631 | }; |
| 632 | int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn); |
| 633 | return RFEWaitStates - WaitStatesNeeded; |
| 634 | } |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 635 | |
| 636 | int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) { |
| 637 | if (MI->isDebugValue()) |
| 638 | return 0; |
| 639 | |
| 640 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 641 | if (!ST.hasSMovFedHazard()) |
| 642 | return 0; |
| 643 | |
| 644 | // Check for any instruction reading an SGPR after a write from |
| 645 | // s_mov_fed_b32. |
| 646 | int MovFedWaitStates = 1; |
| 647 | int WaitStatesNeeded = 0; |
| 648 | |
| 649 | for (const MachineOperand &Use : MI->uses()) { |
| 650 | if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg())) |
| 651 | continue; |
| 652 | auto IsHazardFn = [] (MachineInstr *MI) { |
| 653 | return MI->getOpcode() == AMDGPU::S_MOV_FED_B32; |
| 654 | }; |
| 655 | int WaitStatesNeededForUse = |
| 656 | MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn); |
| 657 | WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse); |
| 658 | } |
| 659 | |
| 660 | return WaitStatesNeeded; |
| 661 | } |
| 662 | |
| 663 | int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 664 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | a41351e | 2017-11-17 21:35:32 +0000 | [diff] [blame] | 665 | const int SMovRelWaitStates = 1; |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 666 | auto IsHazardFn = [TII] (MachineInstr *MI) { |
| 667 | return TII->isSALU(*MI); |
| 668 | }; |
| 669 | return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn); |
| 670 | } |