Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 1 | //===----------------------- SIFrameLowering.cpp --------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //==-----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "SIFrameLowering.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "AMDGPUSubtarget.h" |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 11 | #include "SIInstrInfo.h" |
| 12 | #include "SIMachineFunctionInfo.h" |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 13 | #include "SIRegisterInfo.h" |
Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 15 | |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/LivePhysRegs.h" |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/RegisterScavenging.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "frame-info" |
| 25 | |
| 26 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 27 | static ArrayRef<MCPhysReg> getAllSGPR128(const GCNSubtarget &ST, |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 28 | const MachineFunction &MF) { |
Matt Arsenault | ab3429c | 2016-05-18 15:19:50 +0000 | [diff] [blame] | 29 | return makeArrayRef(AMDGPU::SGPR_128RegClass.begin(), |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 30 | ST.getMaxNumSGPRs(MF) / 4); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 33 | static ArrayRef<MCPhysReg> getAllSGPRs(const GCNSubtarget &ST, |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 34 | const MachineFunction &MF) { |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 35 | return makeArrayRef(AMDGPU::SGPR_32RegClass.begin(), |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 36 | ST.getMaxNumSGPRs(MF)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 39 | // Find a scratch register that we can use at the start of the prologue to |
| 40 | // re-align the stack pointer. We avoid using callee-save registers since they |
| 41 | // may appear to be free when this is called from canUseAsPrologue (during |
| 42 | // shrink wrapping), but then no longer be free when this is called from |
| 43 | // emitPrologue. |
| 44 | // |
| 45 | // FIXME: This is a bit conservative, since in the above case we could use one |
| 46 | // of the callee-save registers as a scratch temp to re-align the stack pointer, |
| 47 | // but we would then have to make sure that we were in fact saving at least one |
| 48 | // callee-save register in the prologue, which is additional complexity that |
| 49 | // doesn't seem worth the benefit. |
| 50 | static unsigned findScratchNonCalleeSaveRegister(MachineRegisterInfo &MRI, |
| 51 | LivePhysRegs &LiveRegs, |
| 52 | const TargetRegisterClass &RC, |
| 53 | bool Unused = false) { |
| 54 | // Mark callee saved registers as used so we will not choose them. |
| 55 | const MCPhysReg *CSRegs = MRI.getCalleeSavedRegs(); |
| 56 | for (unsigned i = 0; CSRegs[i]; ++i) |
| 57 | LiveRegs.addReg(CSRegs[i]); |
| 58 | |
| 59 | if (Unused) { |
| 60 | // We are looking for a register that can be used throughout the entire |
| 61 | // function, so any use is unacceptable. |
| 62 | for (unsigned Reg : RC) { |
| 63 | if (!MRI.isPhysRegUsed(Reg) && LiveRegs.available(MRI, Reg)) |
| 64 | return Reg; |
| 65 | } |
| 66 | } else { |
| 67 | for (unsigned Reg : RC) { |
| 68 | if (LiveRegs.available(MRI, Reg)) |
| 69 | return Reg; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // If we require an unused register, this is used in contexts where failure is |
| 74 | // an option and has an alternative plan. In other contexts, this must |
| 75 | // succeed0. |
| 76 | if (!Unused) |
| 77 | report_fatal_error("failed to find free scratch register"); |
| 78 | |
| 79 | return AMDGPU::NoRegister; |
| 80 | } |
| 81 | |
| 82 | static MCPhysReg findUnusedSGPRNonCalleeSaved(MachineRegisterInfo &MRI) { |
| 83 | LivePhysRegs LiveRegs; |
| 84 | LiveRegs.init(*MRI.getTargetRegisterInfo()); |
| 85 | return findScratchNonCalleeSaveRegister( |
| 86 | MRI, LiveRegs, AMDGPU::SReg_32_XM0_XEXECRegClass, true); |
| 87 | } |
| 88 | |
| 89 | // We need to specially emit stack operations here because a different frame |
| 90 | // register is used than in the rest of the function, as getFrameRegister would |
| 91 | // use. |
| 92 | static void buildPrologSpill(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB, |
| 93 | MachineBasicBlock::iterator I, |
| 94 | const SIInstrInfo *TII, unsigned SpillReg, |
| 95 | unsigned ScratchRsrcReg, unsigned SPReg, int FI) { |
| 96 | MachineFunction *MF = MBB.getParent(); |
| 97 | MachineFrameInfo &MFI = MF->getFrameInfo(); |
| 98 | |
| 99 | int64_t Offset = MFI.getObjectOffset(FI); |
| 100 | |
| 101 | MachineMemOperand *MMO = MF->getMachineMemOperand( |
| 102 | MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore, 4, |
| 103 | MFI.getObjectAlignment(FI)); |
| 104 | |
| 105 | if (isUInt<12>(Offset)) { |
| 106 | BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::BUFFER_STORE_DWORD_OFFSET)) |
| 107 | .addReg(SpillReg, RegState::Kill) |
| 108 | .addReg(ScratchRsrcReg) |
| 109 | .addReg(SPReg) |
| 110 | .addImm(Offset) |
| 111 | .addImm(0) // glc |
| 112 | .addImm(0) // slc |
| 113 | .addImm(0) // tfe |
| 114 | .addImm(0) // dlc |
Piotr Sobczak | 265e94e | 2019-10-02 17:22:36 +0000 | [diff] [blame] | 115 | .addImm(0) // swz |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 116 | .addMemOperand(MMO); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | MCPhysReg OffsetReg = findScratchNonCalleeSaveRegister( |
| 121 | MF->getRegInfo(), LiveRegs, AMDGPU::VGPR_32RegClass); |
| 122 | |
| 123 | BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32), OffsetReg) |
| 124 | .addImm(Offset); |
| 125 | |
| 126 | BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::BUFFER_STORE_DWORD_OFFEN)) |
| 127 | .addReg(SpillReg, RegState::Kill) |
| 128 | .addReg(OffsetReg, RegState::Kill) |
| 129 | .addReg(ScratchRsrcReg) |
| 130 | .addReg(SPReg) |
| 131 | .addImm(0) |
| 132 | .addImm(0) // glc |
| 133 | .addImm(0) // slc |
| 134 | .addImm(0) // tfe |
| 135 | .addImm(0) // dlc |
Piotr Sobczak | 265e94e | 2019-10-02 17:22:36 +0000 | [diff] [blame] | 136 | .addImm(0) // swz |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 137 | .addMemOperand(MMO); |
| 138 | } |
| 139 | |
| 140 | static void buildEpilogReload(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB, |
| 141 | MachineBasicBlock::iterator I, |
| 142 | const SIInstrInfo *TII, unsigned SpillReg, |
| 143 | unsigned ScratchRsrcReg, unsigned SPReg, int FI) { |
| 144 | MachineFunction *MF = MBB.getParent(); |
| 145 | MachineFrameInfo &MFI = MF->getFrameInfo(); |
| 146 | int64_t Offset = MFI.getObjectOffset(FI); |
| 147 | |
| 148 | MachineMemOperand *MMO = MF->getMachineMemOperand( |
| 149 | MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad, 4, |
| 150 | MFI.getObjectAlignment(FI)); |
| 151 | |
| 152 | if (isUInt<12>(Offset)) { |
| 153 | BuildMI(MBB, I, DebugLoc(), |
| 154 | TII->get(AMDGPU::BUFFER_LOAD_DWORD_OFFSET), SpillReg) |
| 155 | .addReg(ScratchRsrcReg) |
| 156 | .addReg(SPReg) |
| 157 | .addImm(Offset) |
| 158 | .addImm(0) // glc |
| 159 | .addImm(0) // slc |
| 160 | .addImm(0) // tfe |
| 161 | .addImm(0) // dlc |
Piotr Sobczak | 265e94e | 2019-10-02 17:22:36 +0000 | [diff] [blame] | 162 | .addImm(0) // swz |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 163 | .addMemOperand(MMO); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | MCPhysReg OffsetReg = findScratchNonCalleeSaveRegister( |
| 168 | MF->getRegInfo(), LiveRegs, AMDGPU::VGPR_32RegClass); |
| 169 | |
| 170 | BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32), OffsetReg) |
| 171 | .addImm(Offset); |
| 172 | |
| 173 | BuildMI(MBB, I, DebugLoc(), |
| 174 | TII->get(AMDGPU::BUFFER_LOAD_DWORD_OFFEN), SpillReg) |
| 175 | .addReg(OffsetReg, RegState::Kill) |
| 176 | .addReg(ScratchRsrcReg) |
| 177 | .addReg(SPReg) |
| 178 | .addImm(0) |
| 179 | .addImm(0) // glc |
| 180 | .addImm(0) // slc |
| 181 | .addImm(0) // tfe |
| 182 | .addImm(0) // dlc |
Piotr Sobczak | 265e94e | 2019-10-02 17:22:36 +0000 | [diff] [blame] | 183 | .addImm(0) // swz |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 184 | .addMemOperand(MMO); |
| 185 | } |
| 186 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 187 | void SIFrameLowering::emitFlatScratchInit(const GCNSubtarget &ST, |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 188 | MachineFunction &MF, |
| 189 | MachineBasicBlock &MBB) const { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 190 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 191 | const SIRegisterInfo* TRI = &TII->getRegisterInfo(); |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 192 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 193 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 194 | // We don't need this if we only have spills since there is no user facing |
| 195 | // scratch. |
| 196 | |
| 197 | // TODO: If we know we don't have flat instructions earlier, we can omit |
| 198 | // this from the input registers. |
| 199 | // |
| 200 | // TODO: We only need to know if we access scratch space through a flat |
| 201 | // pointer. Because we only detect if flat instructions are used at all, |
| 202 | // this will be used more often than necessary on VI. |
| 203 | |
| 204 | // Debug location must be unknown since the first debug location is used to |
| 205 | // determine the end of the prologue. |
| 206 | DebugLoc DL; |
| 207 | MachineBasicBlock::iterator I = MBB.begin(); |
| 208 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 209 | Register FlatScratchInitReg = |
| 210 | MFI->getPreloadedReg(AMDGPUFunctionArgInfo::FLAT_SCRATCH_INIT); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 211 | |
| 212 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 213 | MRI.addLiveIn(FlatScratchInitReg); |
| 214 | MBB.addLiveIn(FlatScratchInitReg); |
| 215 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 216 | Register FlatScrInitLo = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub0); |
| 217 | Register FlatScrInitHi = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub1); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 218 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 219 | unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg(); |
| 220 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 221 | // Do a 64-bit pointer add. |
| 222 | if (ST.flatScratchIsPointer()) { |
Stanislav Mekhanoshin | a632294 | 2019-04-30 22:08:23 +0000 | [diff] [blame] | 223 | if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) { |
| 224 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), FlatScrInitLo) |
| 225 | .addReg(FlatScrInitLo) |
| 226 | .addReg(ScratchWaveOffsetReg); |
| 227 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADDC_U32), FlatScrInitHi) |
| 228 | .addReg(FlatScrInitHi) |
| 229 | .addImm(0); |
| 230 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SETREG_B32)). |
| 231 | addReg(FlatScrInitLo). |
| 232 | addImm(int16_t(AMDGPU::Hwreg::ID_FLAT_SCR_LO | |
| 233 | (31 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_))); |
| 234 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SETREG_B32)). |
| 235 | addReg(FlatScrInitHi). |
| 236 | addImm(int16_t(AMDGPU::Hwreg::ID_FLAT_SCR_HI | |
| 237 | (31 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_))); |
| 238 | return; |
| 239 | } |
| 240 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 241 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), AMDGPU::FLAT_SCR_LO) |
| 242 | .addReg(FlatScrInitLo) |
| 243 | .addReg(ScratchWaveOffsetReg); |
| 244 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADDC_U32), AMDGPU::FLAT_SCR_HI) |
| 245 | .addReg(FlatScrInitHi) |
| 246 | .addImm(0); |
| 247 | |
| 248 | return; |
| 249 | } |
| 250 | |
Stanislav Mekhanoshin | a632294 | 2019-04-30 22:08:23 +0000 | [diff] [blame] | 251 | assert(ST.getGeneration() < AMDGPUSubtarget::GFX10); |
| 252 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 253 | // Copy the size in bytes. |
| 254 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::FLAT_SCR_LO) |
| 255 | .addReg(FlatScrInitHi, RegState::Kill); |
| 256 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 257 | // Add wave offset in bytes to private base offset. |
| 258 | // See comment in AMDKernelCodeT.h for enable_sgpr_flat_scratch_init. |
| 259 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), FlatScrInitLo) |
| 260 | .addReg(FlatScrInitLo) |
| 261 | .addReg(ScratchWaveOffsetReg); |
| 262 | |
| 263 | // Convert offset to 256-byte units. |
| 264 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_LSHR_B32), AMDGPU::FLAT_SCR_HI) |
| 265 | .addReg(FlatScrInitLo, RegState::Kill) |
| 266 | .addImm(8); |
| 267 | } |
| 268 | |
| 269 | unsigned SIFrameLowering::getReservedPrivateSegmentBufferReg( |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 270 | const GCNSubtarget &ST, |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 271 | const SIInstrInfo *TII, |
| 272 | const SIRegisterInfo *TRI, |
| 273 | SIMachineFunctionInfo *MFI, |
| 274 | MachineFunction &MF) const { |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 275 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 276 | |
| 277 | // We need to insert initialization of the scratch resource descriptor. |
| 278 | unsigned ScratchRsrcReg = MFI->getScratchRSrcReg(); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 279 | if (ScratchRsrcReg == AMDGPU::NoRegister || |
| 280 | !MRI.isPhysRegUsed(ScratchRsrcReg)) |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 281 | return AMDGPU::NoRegister; |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 282 | |
| 283 | if (ST.hasSGPRInitBug() || |
| 284 | ScratchRsrcReg != TRI->reservedPrivateSegmentBufferReg(MF)) |
| 285 | return ScratchRsrcReg; |
| 286 | |
| 287 | // We reserved the last registers for this. Shift it down to the end of those |
| 288 | // which were actually used. |
| 289 | // |
| 290 | // FIXME: It might be safer to use a pseudoregister before replacement. |
| 291 | |
| 292 | // FIXME: We should be able to eliminate unused input registers. We only |
| 293 | // cannot do this for the resources required for scratch access. For now we |
| 294 | // skip over user SGPRs and may leave unused holes. |
| 295 | |
| 296 | // We find the resource first because it has an alignment requirement. |
| 297 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 298 | unsigned NumPreloaded = (MFI->getNumPreloadedSGPRs() + 3) / 4; |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 299 | ArrayRef<MCPhysReg> AllSGPR128s = getAllSGPR128(ST, MF); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 300 | AllSGPR128s = AllSGPR128s.slice(std::min(static_cast<unsigned>(AllSGPR128s.size()), NumPreloaded)); |
| 301 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 302 | // Skip the last N reserved elements because they should have already been |
| 303 | // reserved for VCC etc. |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 304 | for (MCPhysReg Reg : AllSGPR128s) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 305 | // Pick the first unallocated one. Make sure we don't clobber the other |
| 306 | // reserved input we needed. |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 307 | if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 308 | MRI.replaceRegWith(ScratchRsrcReg, Reg); |
| 309 | MFI->setScratchRSrcReg(Reg); |
| 310 | return Reg; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return ScratchRsrcReg; |
| 315 | } |
| 316 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 317 | // Shift down registers reserved for the scratch wave offset. |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 318 | std::pair<unsigned, bool> |
| 319 | SIFrameLowering::getReservedPrivateSegmentWaveByteOffsetReg( |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 320 | const GCNSubtarget &ST, const SIInstrInfo *TII, const SIRegisterInfo *TRI, |
| 321 | SIMachineFunctionInfo *MFI, MachineFunction &MF) const { |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 322 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 323 | unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg(); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 324 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 325 | assert(MFI->isEntryFunction()); |
| 326 | |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 327 | // No replacement necessary. |
| 328 | if (ScratchWaveOffsetReg == AMDGPU::NoRegister || |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 329 | (!hasFP(MF) && !MRI.isPhysRegUsed(ScratchWaveOffsetReg))) { |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 330 | return std::make_pair(AMDGPU::NoRegister, false); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 331 | } |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 332 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 333 | if (ST.hasSGPRInitBug()) |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 334 | return std::make_pair(ScratchWaveOffsetReg, false); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 335 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 336 | unsigned NumPreloaded = MFI->getNumPreloadedSGPRs(); |
| 337 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 338 | ArrayRef<MCPhysReg> AllSGPRs = getAllSGPRs(ST, MF); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 339 | if (NumPreloaded > AllSGPRs.size()) |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 340 | return std::make_pair(ScratchWaveOffsetReg, false); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 341 | |
| 342 | AllSGPRs = AllSGPRs.slice(NumPreloaded); |
| 343 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 344 | // We need to drop register from the end of the list that we cannot use |
| 345 | // for the scratch wave offset. |
| 346 | // + 2 s102 and s103 do not exist on VI. |
| 347 | // + 2 for vcc |
| 348 | // + 2 for xnack_mask |
| 349 | // + 2 for flat_scratch |
| 350 | // + 4 for registers reserved for scratch resource register |
| 351 | // + 1 for register reserved for scratch wave offset. (By exluding this |
| 352 | // register from the list to consider, it means that when this |
| 353 | // register is being used for the scratch wave offset and there |
| 354 | // are no other free SGPRs, then the value will stay in this register. |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 355 | // + 1 if stack pointer is used. |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 356 | // ---- |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 357 | // 13 (+1) |
| 358 | unsigned ReservedRegCount = 13; |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 359 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 360 | if (AllSGPRs.size() < ReservedRegCount) |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 361 | return std::make_pair(ScratchWaveOffsetReg, false); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 362 | |
| 363 | bool HandledScratchWaveOffsetReg = |
| 364 | ScratchWaveOffsetReg != TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 365 | bool FPAdjusted = false; |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 366 | |
| 367 | for (MCPhysReg Reg : AllSGPRs.drop_back(ReservedRegCount)) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 368 | // Pick the first unallocated SGPR. Be careful not to pick an alias of the |
| 369 | // scratch descriptor, since we haven’t added its uses yet. |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 370 | if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) { |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 371 | if (!HandledScratchWaveOffsetReg) { |
| 372 | HandledScratchWaveOffsetReg = true; |
| 373 | |
| 374 | MRI.replaceRegWith(ScratchWaveOffsetReg, Reg); |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 375 | if (MFI->getScratchWaveOffsetReg() == MFI->getStackPtrOffsetReg()) { |
| 376 | assert(!hasFP(MF)); |
| 377 | MFI->setStackPtrOffsetReg(Reg); |
| 378 | } |
| 379 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 380 | MFI->setScratchWaveOffsetReg(Reg); |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 381 | MFI->setFrameOffsetReg(Reg); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 382 | ScratchWaveOffsetReg = Reg; |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 383 | FPAdjusted = true; |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 384 | break; |
| 385 | } |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 389 | return std::make_pair(ScratchWaveOffsetReg, FPAdjusted); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 392 | void SIFrameLowering::emitEntryFunctionPrologue(MachineFunction &MF, |
| 393 | MachineBasicBlock &MBB) const { |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 394 | assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); |
| 395 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 396 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 397 | |
| 398 | // If we only have SGPR spills, we won't actually be using scratch memory |
| 399 | // since these spill to VGPRs. |
| 400 | // |
| 401 | // FIXME: We should be cleaning up these unused SGPR spill frame indices |
| 402 | // somewhere. |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 403 | |
Matt Arsenault | aa6fb4c | 2019-02-21 23:27:46 +0000 | [diff] [blame] | 404 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 405 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 406 | const SIRegisterInfo *TRI = &TII->getRegisterInfo(); |
Matt Arsenault | 296b849 | 2016-02-12 06:31:30 +0000 | [diff] [blame] | 407 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | ceafc55 | 2018-05-29 17:42:50 +0000 | [diff] [blame] | 408 | const Function &F = MF.getFunction(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 409 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 410 | // We need to do the replacement of the private segment buffer and wave offset |
| 411 | // register even if there are no stack objects. There could be stores to undef |
| 412 | // or a constant without an associated object. |
| 413 | |
| 414 | // FIXME: We still have implicit uses on SGPR spill instructions in case they |
| 415 | // need to spill to vector memory. It's likely that will not happen, but at |
| 416 | // this point it appears we need the setup. This part of the prolog should be |
| 417 | // emitted after frame indices are eliminated. |
| 418 | |
Matt Arsenault | 254ad3d | 2017-07-18 16:44:58 +0000 | [diff] [blame] | 419 | if (MFI->hasFlatScratchInit()) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 420 | emitFlatScratchInit(ST, MF, MBB); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 421 | |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 422 | unsigned ScratchRsrcReg |
| 423 | = getReservedPrivateSegmentBufferReg(ST, TII, TRI, MFI, MF); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 424 | |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 425 | unsigned ScratchWaveOffsetReg; |
| 426 | bool FPAdjusted; |
| 427 | std::tie(ScratchWaveOffsetReg, FPAdjusted) = |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 428 | getReservedPrivateSegmentWaveByteOffsetReg(ST, TII, TRI, MFI, MF); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 429 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 430 | // We need to insert initialization of the scratch resource descriptor. |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 431 | Register PreloadedScratchWaveOffsetReg = MFI->getPreloadedReg( |
| 432 | AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 433 | |
| 434 | unsigned PreloadedPrivateBufferReg = AMDGPU::NoRegister; |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 435 | if (ST.isAmdHsaOrMesa(F)) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 436 | PreloadedPrivateBufferReg = MFI->getPreloadedReg( |
| 437 | AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_BUFFER); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 440 | bool OffsetRegUsed = ScratchWaveOffsetReg != AMDGPU::NoRegister && |
| 441 | MRI.isPhysRegUsed(ScratchWaveOffsetReg); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 442 | bool ResourceRegUsed = ScratchRsrcReg != AMDGPU::NoRegister && |
| 443 | MRI.isPhysRegUsed(ScratchRsrcReg); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 444 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 445 | // FIXME: Hack to not crash in situations which emitted an error. |
| 446 | if (PreloadedScratchWaveOffsetReg == AMDGPU::NoRegister) |
| 447 | return; |
| 448 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 449 | // We added live-ins during argument lowering, but since they were not used |
| 450 | // they were deleted. We're adding the uses now, so add them back. |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 451 | MRI.addLiveIn(PreloadedScratchWaveOffsetReg); |
| 452 | MBB.addLiveIn(PreloadedScratchWaveOffsetReg); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 453 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 454 | if (ResourceRegUsed && PreloadedPrivateBufferReg != AMDGPU::NoRegister) { |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 455 | assert(ST.isAmdHsaOrMesa(F) || ST.isMesaGfxShader(F)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 456 | MRI.addLiveIn(PreloadedPrivateBufferReg); |
| 457 | MBB.addLiveIn(PreloadedPrivateBufferReg); |
| 458 | } |
| 459 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 460 | // Make the register selected live throughout the function. |
| 461 | for (MachineBasicBlock &OtherBB : MF) { |
| 462 | if (&OtherBB == &MBB) |
| 463 | continue; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 464 | |
Michael Liao | b3f967d | 2019-07-16 15:57:12 +0000 | [diff] [blame] | 465 | if (OffsetRegUsed || FPAdjusted) |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 466 | OtherBB.addLiveIn(ScratchWaveOffsetReg); |
| 467 | |
| 468 | if (ResourceRegUsed) |
| 469 | OtherBB.addLiveIn(ScratchRsrcReg); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 472 | DebugLoc DL; |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 473 | MachineBasicBlock::iterator I = MBB.begin(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 474 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 475 | // If we reserved the original input registers, we don't need to copy to the |
| 476 | // reserved registers. |
| 477 | |
| 478 | bool CopyBuffer = ResourceRegUsed && |
| 479 | PreloadedPrivateBufferReg != AMDGPU::NoRegister && |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 480 | ST.isAmdHsaOrMesa(F) && |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 481 | ScratchRsrcReg != PreloadedPrivateBufferReg; |
| 482 | |
| 483 | // This needs to be careful of the copying order to avoid overwriting one of |
| 484 | // the input registers before it's been copied to it's final |
| 485 | // destination. Usually the offset should be copied first. |
| 486 | bool CopyBufferFirst = TRI->isSubRegisterEq(PreloadedPrivateBufferReg, |
| 487 | ScratchWaveOffsetReg); |
| 488 | if (CopyBuffer && CopyBufferFirst) { |
| 489 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchRsrcReg) |
| 490 | .addReg(PreloadedPrivateBufferReg, RegState::Kill); |
| 491 | } |
| 492 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 493 | unsigned SPReg = MFI->getStackPtrOffsetReg(); |
| 494 | assert(SPReg != AMDGPU::SP_REG); |
| 495 | |
| 496 | // FIXME: Remove the isPhysRegUsed checks |
| 497 | const bool HasFP = hasFP(MF); |
| 498 | |
| 499 | if (HasFP || OffsetRegUsed) { |
| 500 | assert(ScratchWaveOffsetReg); |
Matt Arsenault | 1d21517 | 2016-08-31 21:52:25 +0000 | [diff] [blame] | 501 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchWaveOffsetReg) |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 502 | .addReg(PreloadedScratchWaveOffsetReg, HasFP ? RegState::Kill : 0); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 503 | } |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 504 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 505 | if (CopyBuffer && !CopyBufferFirst) { |
Matt Arsenault | 1d21517 | 2016-08-31 21:52:25 +0000 | [diff] [blame] | 506 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchRsrcReg) |
| 507 | .addReg(PreloadedPrivateBufferReg, RegState::Kill); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 510 | if (ResourceRegUsed) { |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 511 | emitEntryFunctionScratchSetup(ST, MF, MBB, MFI, I, |
| 512 | PreloadedPrivateBufferReg, ScratchRsrcReg); |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | if (HasFP) { |
| 516 | DebugLoc DL; |
| 517 | const MachineFrameInfo &FrameInfo = MF.getFrameInfo(); |
| 518 | int64_t StackSize = FrameInfo.getStackSize(); |
| 519 | |
| 520 | // On kernel entry, the private scratch wave offset is the SP value. |
| 521 | if (StackSize == 0) { |
| 522 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), SPReg) |
| 523 | .addReg(MFI->getScratchWaveOffsetReg()); |
| 524 | } else { |
| 525 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), SPReg) |
| 526 | .addReg(MFI->getScratchWaveOffsetReg()) |
| 527 | .addImm(StackSize * ST.getWavefrontSize()); |
| 528 | } |
| 529 | } |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // Emit scratch setup code for AMDPAL or Mesa, assuming ResourceRegUsed is set. |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 533 | void SIFrameLowering::emitEntryFunctionScratchSetup(const GCNSubtarget &ST, |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 534 | MachineFunction &MF, MachineBasicBlock &MBB, SIMachineFunctionInfo *MFI, |
| 535 | MachineBasicBlock::iterator I, unsigned PreloadedPrivateBufferReg, |
| 536 | unsigned ScratchRsrcReg) const { |
| 537 | |
| 538 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 539 | const SIRegisterInfo *TRI = &TII->getRegisterInfo(); |
Matt Arsenault | ceafc55 | 2018-05-29 17:42:50 +0000 | [diff] [blame] | 540 | const Function &Fn = MF.getFunction(); |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 541 | DebugLoc DL; |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 542 | |
| 543 | if (ST.isAmdPalOS()) { |
| 544 | // The pointer to the GIT is formed from the offset passed in and either |
| 545 | // the amdgpu-git-ptr-high function attribute or the top part of the PC |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 546 | Register RsrcLo = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0); |
| 547 | Register RsrcHi = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub1); |
| 548 | Register Rsrc01 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0_sub1); |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 549 | |
| 550 | const MCInstrDesc &SMovB32 = TII->get(AMDGPU::S_MOV_B32); |
| 551 | |
| 552 | if (MFI->getGITPtrHigh() != 0xffffffff) { |
| 553 | BuildMI(MBB, I, DL, SMovB32, RsrcHi) |
| 554 | .addImm(MFI->getGITPtrHigh()) |
| 555 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 556 | } else { |
| 557 | const MCInstrDesc &GetPC64 = TII->get(AMDGPU::S_GETPC_B64); |
| 558 | BuildMI(MBB, I, DL, GetPC64, Rsrc01); |
| 559 | } |
Tim Renouf | 832f90f | 2018-02-26 14:46:43 +0000 | [diff] [blame] | 560 | auto GitPtrLo = AMDGPU::SGPR0; // Low GIT address passed in |
| 561 | if (ST.hasMergedShaders()) { |
| 562 | switch (MF.getFunction().getCallingConv()) { |
| 563 | case CallingConv::AMDGPU_HS: |
| 564 | case CallingConv::AMDGPU_GS: |
| 565 | // Low GIT address is passed in s8 rather than s0 for an LS+HS or |
| 566 | // ES+GS merged shader on gfx9+. |
| 567 | GitPtrLo = AMDGPU::SGPR8; |
| 568 | break; |
| 569 | default: |
| 570 | break; |
| 571 | } |
| 572 | } |
Tim Renouf | 7190a46 | 2018-04-10 11:25:15 +0000 | [diff] [blame] | 573 | MF.getRegInfo().addLiveIn(GitPtrLo); |
Matt Arsenault | 302eedc | 2019-05-31 22:47:36 +0000 | [diff] [blame] | 574 | MBB.addLiveIn(GitPtrLo); |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 575 | BuildMI(MBB, I, DL, SMovB32, RsrcLo) |
Tim Renouf | 832f90f | 2018-02-26 14:46:43 +0000 | [diff] [blame] | 576 | .addReg(GitPtrLo) |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 577 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 578 | |
| 579 | // We now have the GIT ptr - now get the scratch descriptor from the entry |
Tim Renouf | 7190a46 | 2018-04-10 11:25:15 +0000 | [diff] [blame] | 580 | // at offset 0 (or offset 16 for a compute shader). |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 581 | PointerType *PtrTy = |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 582 | PointerType::get(Type::getInt64Ty(MF.getFunction().getContext()), |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 583 | AMDGPUAS::CONSTANT_ADDRESS); |
| 584 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 585 | const MCInstrDesc &LoadDwordX4 = TII->get(AMDGPU::S_LOAD_DWORDX4_IMM); |
| 586 | auto MMO = MF.getMachineMemOperand(PtrInfo, |
| 587 | MachineMemOperand::MOLoad | |
| 588 | MachineMemOperand::MOInvariant | |
| 589 | MachineMemOperand::MODereferenceable, |
Matt Arsenault | 2a64598 | 2019-01-31 01:38:47 +0000 | [diff] [blame] | 590 | 16, 4); |
Matt Arsenault | ceafc55 | 2018-05-29 17:42:50 +0000 | [diff] [blame] | 591 | unsigned Offset = Fn.getCallingConv() == CallingConv::AMDGPU_CS ? 16 : 0; |
Carl Ritson | 494b8ac | 2019-02-08 15:41:11 +0000 | [diff] [blame] | 592 | const GCNSubtarget &Subtarget = MF.getSubtarget<GCNSubtarget>(); |
| 593 | unsigned EncodedOffset = AMDGPU::getSMRDEncodedOffset(Subtarget, Offset); |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 594 | BuildMI(MBB, I, DL, LoadDwordX4, ScratchRsrcReg) |
| 595 | .addReg(Rsrc01) |
Carl Ritson | 494b8ac | 2019-02-08 15:41:11 +0000 | [diff] [blame] | 596 | .addImm(EncodedOffset) // offset |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 597 | .addImm(0) // glc |
Stanislav Mekhanoshin | a632294 | 2019-04-30 22:08:23 +0000 | [diff] [blame] | 598 | .addImm(0) // dlc |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 599 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine) |
| 600 | .addMemOperand(MMO); |
| 601 | return; |
| 602 | } |
Matt Arsenault | ceafc55 | 2018-05-29 17:42:50 +0000 | [diff] [blame] | 603 | if (ST.isMesaGfxShader(Fn) |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 604 | || (PreloadedPrivateBufferReg == AMDGPU::NoRegister)) { |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 605 | assert(!ST.isAmdHsaOrMesa(Fn)); |
Matt Arsenault | 1d21517 | 2016-08-31 21:52:25 +0000 | [diff] [blame] | 606 | const MCInstrDesc &SMovB32 = TII->get(AMDGPU::S_MOV_B32); |
| 607 | |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 608 | Register Rsrc2 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub2); |
| 609 | Register Rsrc3 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub3); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 610 | |
| 611 | // Use relocations to get the pointer, and setup the other bits manually. |
| 612 | uint64_t Rsrc23 = TII->getScratchRsrcWords23(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 613 | |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 614 | if (MFI->hasImplicitBufferPtr()) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 615 | Register Rsrc01 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0_sub1); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 616 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 617 | if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) { |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 618 | const MCInstrDesc &Mov64 = TII->get(AMDGPU::S_MOV_B64); |
| 619 | |
| 620 | BuildMI(MBB, I, DL, Mov64, Rsrc01) |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 621 | .addReg(MFI->getImplicitBufferPtrUserSGPR()) |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 622 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 623 | } else { |
| 624 | const MCInstrDesc &LoadDwordX2 = TII->get(AMDGPU::S_LOAD_DWORDX2_IMM); |
| 625 | |
| 626 | PointerType *PtrTy = |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 627 | PointerType::get(Type::getInt64Ty(MF.getFunction().getContext()), |
Konstantin Zhuravlyov | 435151a | 2017-11-01 19:12:38 +0000 | [diff] [blame] | 628 | AMDGPUAS::CONSTANT_ADDRESS); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 629 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 630 | auto MMO = MF.getMachineMemOperand(PtrInfo, |
| 631 | MachineMemOperand::MOLoad | |
| 632 | MachineMemOperand::MOInvariant | |
| 633 | MachineMemOperand::MODereferenceable, |
Matt Arsenault | 2a64598 | 2019-01-31 01:38:47 +0000 | [diff] [blame] | 634 | 8, 4); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 635 | BuildMI(MBB, I, DL, LoadDwordX2, Rsrc01) |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 636 | .addReg(MFI->getImplicitBufferPtrUserSGPR()) |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 637 | .addImm(0) // offset |
| 638 | .addImm(0) // glc |
Stanislav Mekhanoshin | a632294 | 2019-04-30 22:08:23 +0000 | [diff] [blame] | 639 | .addImm(0) // dlc |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 640 | .addMemOperand(MMO) |
| 641 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
Matt Arsenault | 302eedc | 2019-05-31 22:47:36 +0000 | [diff] [blame] | 642 | |
| 643 | MF.getRegInfo().addLiveIn(MFI->getImplicitBufferPtrUserSGPR()); |
| 644 | MBB.addLiveIn(MFI->getImplicitBufferPtrUserSGPR()); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 645 | } |
| 646 | } else { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 647 | Register Rsrc0 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0); |
| 648 | Register Rsrc1 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub1); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 649 | |
| 650 | BuildMI(MBB, I, DL, SMovB32, Rsrc0) |
| 651 | .addExternalSymbol("SCRATCH_RSRC_DWORD0") |
| 652 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 653 | |
| 654 | BuildMI(MBB, I, DL, SMovB32, Rsrc1) |
| 655 | .addExternalSymbol("SCRATCH_RSRC_DWORD1") |
| 656 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 657 | |
| 658 | } |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 659 | |
| 660 | BuildMI(MBB, I, DL, SMovB32, Rsrc2) |
| 661 | .addImm(Rsrc23 & 0xffffffff) |
| 662 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 663 | |
| 664 | BuildMI(MBB, I, DL, SMovB32, Rsrc3) |
| 665 | .addImm(Rsrc23 >> 32) |
| 666 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 667 | } |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Sander de Smalen | 5d6ee76 | 2019-06-17 09:13:29 +0000 | [diff] [blame] | 670 | bool SIFrameLowering::isSupportedStackID(TargetStackID::Value ID) const { |
Fangrui Song | 5401c2d | 2019-06-17 10:20:20 +0000 | [diff] [blame] | 671 | switch (ID) { |
| 672 | case TargetStackID::Default: |
| 673 | case TargetStackID::NoAlloc: |
| 674 | case TargetStackID::SGPRSpill: |
| 675 | return true; |
Sander de Smalen | 4f99b6f | 2019-10-03 11:33:50 +0000 | [diff] [blame] | 676 | case TargetStackID::SVEVector: |
| 677 | return false; |
Fangrui Song | 5401c2d | 2019-06-17 10:20:20 +0000 | [diff] [blame] | 678 | } |
| 679 | llvm_unreachable("Invalid TargetStackID::Value"); |
Sander de Smalen | 5d6ee76 | 2019-06-17 09:13:29 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 682 | void SIFrameLowering::emitPrologue(MachineFunction &MF, |
| 683 | MachineBasicBlock &MBB) const { |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 684 | SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 685 | if (FuncInfo->isEntryFunction()) { |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 686 | emitEntryFunctionPrologue(MF, MBB); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 687 | return; |
| 688 | } |
| 689 | |
| 690 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 691 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 692 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 693 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 694 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 695 | |
| 696 | unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg(); |
| 697 | unsigned FramePtrReg = FuncInfo->getFrameOffsetReg(); |
Matt Arsenault | 3d59e38 | 2019-05-24 18:18:51 +0000 | [diff] [blame] | 698 | LivePhysRegs LiveRegs; |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 699 | |
| 700 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 701 | DebugLoc DL; |
| 702 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 703 | bool HasFP = false; |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 704 | uint32_t NumBytes = MFI.getStackSize(); |
| 705 | uint32_t RoundedSize = NumBytes; |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 706 | // To avoid clobbering VGPRs in lanes that weren't active on function entry, |
| 707 | // turn on all lanes before doing the spill to memory. |
| 708 | unsigned ScratchExecCopy = AMDGPU::NoRegister; |
| 709 | |
| 710 | // Emit the copy if we need an FP, and are using a free SGPR to save it. |
| 711 | if (FuncInfo->SGPRForFPSaveRestoreCopy != AMDGPU::NoRegister) { |
| 712 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::COPY), FuncInfo->SGPRForFPSaveRestoreCopy) |
| 713 | .addReg(FramePtrReg) |
| 714 | .setMIFlag(MachineInstr::FrameSetup); |
| 715 | } |
| 716 | |
| 717 | for (const SIMachineFunctionInfo::SGPRSpillVGPRCSR &Reg |
| 718 | : FuncInfo->getSGPRSpillVGPRs()) { |
| 719 | if (!Reg.FI.hasValue()) |
| 720 | continue; |
| 721 | |
| 722 | if (ScratchExecCopy == AMDGPU::NoRegister) { |
| 723 | if (LiveRegs.empty()) { |
| 724 | LiveRegs.init(TRI); |
| 725 | LiveRegs.addLiveIns(MBB); |
| 726 | if (FuncInfo->SGPRForFPSaveRestoreCopy) |
| 727 | LiveRegs.removeReg(FuncInfo->SGPRForFPSaveRestoreCopy); |
| 728 | } |
| 729 | |
| 730 | ScratchExecCopy |
| 731 | = findScratchNonCalleeSaveRegister(MRI, LiveRegs, |
| 732 | *TRI.getWaveMaskRegClass()); |
| 733 | assert(FuncInfo->SGPRForFPSaveRestoreCopy != ScratchExecCopy); |
| 734 | |
| 735 | const unsigned OrSaveExec = ST.isWave32() ? |
| 736 | AMDGPU::S_OR_SAVEEXEC_B32 : AMDGPU::S_OR_SAVEEXEC_B64; |
| 737 | BuildMI(MBB, MBBI, DL, TII->get(OrSaveExec), |
| 738 | ScratchExecCopy) |
| 739 | .addImm(-1); |
| 740 | } |
| 741 | |
| 742 | buildPrologSpill(LiveRegs, MBB, MBBI, TII, Reg.VGPR, |
| 743 | FuncInfo->getScratchRSrcReg(), |
| 744 | StackPtrReg, |
| 745 | Reg.FI.getValue()); |
| 746 | } |
| 747 | |
| 748 | if (ScratchExecCopy != AMDGPU::NoRegister) { |
| 749 | // FIXME: Split block and make terminator. |
| 750 | unsigned ExecMov = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; |
| 751 | unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; |
| 752 | BuildMI(MBB, MBBI, DL, TII->get(ExecMov), Exec) |
| 753 | .addReg(ScratchExecCopy, RegState::Kill); |
| 754 | LiveRegs.addReg(ScratchExecCopy); |
| 755 | } |
| 756 | |
| 757 | |
| 758 | if (FuncInfo->FramePointerSaveIndex) { |
| 759 | const int FI = FuncInfo->FramePointerSaveIndex.getValue(); |
| 760 | assert(!MFI.isDeadObjectIndex(FI) && |
| 761 | MFI.getStackID(FI) == TargetStackID::SGPRSpill); |
| 762 | ArrayRef<SIMachineFunctionInfo::SpilledReg> Spill |
| 763 | = FuncInfo->getSGPRToVGPRSpills(FI); |
| 764 | assert(Spill.size() == 1); |
| 765 | |
| 766 | // Save FP before setting it up. |
| 767 | // FIXME: This should respect spillSGPRToVGPR; |
| 768 | BuildMI(MBB, MBBI, DL, TII->getMCOpcodeFromPseudo(AMDGPU::V_WRITELANE_B32), |
| 769 | Spill[0].VGPR) |
| 770 | .addReg(FramePtrReg) |
| 771 | .addImm(Spill[0].Lane) |
| 772 | .addReg(Spill[0].VGPR, RegState::Undef); |
| 773 | } |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 774 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 775 | if (TRI.needsStackRealignment(MF)) { |
| 776 | HasFP = true; |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 777 | const unsigned Alignment = MFI.getMaxAlignment(); |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 778 | |
| 779 | RoundedSize += Alignment; |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 780 | if (LiveRegs.empty()) { |
| 781 | LiveRegs.init(TRI); |
| 782 | LiveRegs.addLiveIns(MBB); |
| 783 | LiveRegs.addReg(FuncInfo->SGPRForFPSaveRestoreCopy); |
| 784 | } |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 785 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 786 | unsigned ScratchSPReg = findScratchNonCalleeSaveRegister( |
| 787 | MRI, LiveRegs, AMDGPU::SReg_32_XM0RegClass); |
| 788 | assert(ScratchSPReg != AMDGPU::NoRegister && |
| 789 | ScratchSPReg != FuncInfo->SGPRForFPSaveRestoreCopy); |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 790 | |
| 791 | // s_add_u32 tmp_reg, s32, NumBytes |
| 792 | // s_and_b32 s32, tmp_reg, 0b111...0000 |
| 793 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_ADD_U32), ScratchSPReg) |
| 794 | .addReg(StackPtrReg) |
| 795 | .addImm((Alignment - 1) * ST.getWavefrontSize()) |
| 796 | .setMIFlag(MachineInstr::FrameSetup); |
| 797 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_AND_B32), FramePtrReg) |
| 798 | .addReg(ScratchSPReg, RegState::Kill) |
| 799 | .addImm(-Alignment * ST.getWavefrontSize()) |
| 800 | .setMIFlag(MachineInstr::FrameSetup); |
| 801 | FuncInfo->setIsStackRealigned(true); |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 802 | } else if ((HasFP = hasFP(MF))) { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 803 | // If we need a base pointer, set it up here. It's whatever the value of |
| 804 | // the stack pointer is at this point. Any variable size objects will be |
| 805 | // allocated after this, so we can still use the base pointer to reference |
| 806 | // locals. |
| 807 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::COPY), FramePtrReg) |
| 808 | .addReg(StackPtrReg) |
| 809 | .setMIFlag(MachineInstr::FrameSetup); |
| 810 | } |
| 811 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 812 | if (HasFP && RoundedSize != 0) { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 813 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_ADD_U32), StackPtrReg) |
| 814 | .addReg(StackPtrReg) |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 815 | .addImm(RoundedSize * ST.getWavefrontSize()) |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 816 | .setMIFlag(MachineInstr::FrameSetup); |
| 817 | } |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 818 | |
Bill Wendling | c8933c4 | 2019-07-08 22:00:33 +0000 | [diff] [blame] | 819 | assert((!HasFP || (FuncInfo->SGPRForFPSaveRestoreCopy != AMDGPU::NoRegister || |
| 820 | FuncInfo->FramePointerSaveIndex)) && |
| 821 | "Needed to save FP but didn't save it anywhere"); |
Matt Arsenault | 24e80b8 | 2019-05-28 16:46:02 +0000 | [diff] [blame] | 822 | |
Bill Wendling | c8933c4 | 2019-07-08 22:00:33 +0000 | [diff] [blame] | 823 | assert((HasFP || (FuncInfo->SGPRForFPSaveRestoreCopy == AMDGPU::NoRegister && |
| 824 | !FuncInfo->FramePointerSaveIndex)) && |
| 825 | "Saved FP but didn't need it"); |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 828 | void SIFrameLowering::emitEpilogue(MachineFunction &MF, |
| 829 | MachineBasicBlock &MBB) const { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 830 | const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
| 831 | if (FuncInfo->isEntryFunction()) |
| 832 | return; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 833 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 834 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 835 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 836 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 837 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 838 | LivePhysRegs LiveRegs; |
Matt Arsenault | 3d59e38 | 2019-05-24 18:18:51 +0000 | [diff] [blame] | 839 | DebugLoc DL; |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 840 | |
Matt Arsenault | 5dc457c | 2019-06-20 17:03:23 +0000 | [diff] [blame] | 841 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 842 | uint32_t NumBytes = MFI.getStackSize(); |
| 843 | uint32_t RoundedSize = FuncInfo->isStackRealigned() ? |
| 844 | NumBytes + MFI.getMaxAlignment() : NumBytes; |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 845 | |
Matt Arsenault | 5dc457c | 2019-06-20 17:03:23 +0000 | [diff] [blame] | 846 | if (RoundedSize != 0 && hasFP(MF)) { |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 847 | const unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg(); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 848 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_SUB_U32), StackPtrReg) |
| 849 | .addReg(StackPtrReg) |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 850 | .addImm(RoundedSize * ST.getWavefrontSize()) |
| 851 | .setMIFlag(MachineInstr::FrameDestroy); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 852 | } |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 853 | |
| 854 | if (FuncInfo->SGPRForFPSaveRestoreCopy != AMDGPU::NoRegister) { |
| 855 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::COPY), FuncInfo->getFrameOffsetReg()) |
| 856 | .addReg(FuncInfo->SGPRForFPSaveRestoreCopy) |
| 857 | .setMIFlag(MachineInstr::FrameSetup); |
| 858 | } |
| 859 | |
| 860 | if (FuncInfo->FramePointerSaveIndex) { |
| 861 | const int FI = FuncInfo->FramePointerSaveIndex.getValue(); |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 862 | |
Matt Arsenault | 8561844 | 2019-07-08 19:47:42 +0000 | [diff] [blame] | 863 | assert(!MF.getFrameInfo().isDeadObjectIndex(FI) && |
| 864 | MF.getFrameInfo().getStackID(FI) == TargetStackID::SGPRSpill); |
| 865 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 866 | ArrayRef<SIMachineFunctionInfo::SpilledReg> Spill |
| 867 | = FuncInfo->getSGPRToVGPRSpills(FI); |
| 868 | assert(Spill.size() == 1); |
| 869 | BuildMI(MBB, MBBI, DL, TII->getMCOpcodeFromPseudo(AMDGPU::V_READLANE_B32), |
| 870 | FuncInfo->getFrameOffsetReg()) |
| 871 | .addReg(Spill[0].VGPR) |
| 872 | .addImm(Spill[0].Lane); |
| 873 | } |
| 874 | |
| 875 | unsigned ScratchExecCopy = AMDGPU::NoRegister; |
| 876 | for (const SIMachineFunctionInfo::SGPRSpillVGPRCSR &Reg |
| 877 | : FuncInfo->getSGPRSpillVGPRs()) { |
| 878 | if (!Reg.FI.hasValue()) |
| 879 | continue; |
| 880 | |
| 881 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 882 | if (ScratchExecCopy == AMDGPU::NoRegister) { |
| 883 | // See emitPrologue |
| 884 | if (LiveRegs.empty()) { |
| 885 | LiveRegs.init(*ST.getRegisterInfo()); |
| 886 | LiveRegs.addLiveOuts(MBB); |
| 887 | LiveRegs.stepBackward(*MBBI); |
| 888 | } |
| 889 | |
| 890 | ScratchExecCopy = findScratchNonCalleeSaveRegister( |
| 891 | MRI, LiveRegs, *TRI.getWaveMaskRegClass()); |
| 892 | LiveRegs.removeReg(ScratchExecCopy); |
| 893 | |
| 894 | const unsigned OrSaveExec = |
| 895 | ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32 : AMDGPU::S_OR_SAVEEXEC_B64; |
| 896 | |
| 897 | BuildMI(MBB, MBBI, DL, TII->get(OrSaveExec), ScratchExecCopy) |
| 898 | .addImm(-1); |
| 899 | } |
| 900 | |
| 901 | buildEpilogReload(LiveRegs, MBB, MBBI, TII, Reg.VGPR, |
| 902 | FuncInfo->getScratchRSrcReg(), |
| 903 | FuncInfo->getStackPtrOffsetReg(), Reg.FI.getValue()); |
| 904 | } |
| 905 | |
| 906 | if (ScratchExecCopy != AMDGPU::NoRegister) { |
| 907 | // FIXME: Split block and make terminator. |
| 908 | unsigned ExecMov = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; |
| 909 | unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; |
| 910 | BuildMI(MBB, MBBI, DL, TII->get(ExecMov), Exec) |
| 911 | .addReg(ScratchExecCopy, RegState::Kill); |
| 912 | } |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Matt Arsenault | 942404d | 2019-06-24 14:34:40 +0000 | [diff] [blame] | 915 | // Note SGPRSpill stack IDs should only be used for SGPR spilling to VGPRs, not |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 916 | // memory. They should have been removed by now. |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 917 | static bool allStackObjectsAreDead(const MachineFrameInfo &MFI) { |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 918 | for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); |
| 919 | I != E; ++I) { |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 920 | if (!MFI.isDeadObjectIndex(I)) |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 921 | return false; |
| 922 | } |
| 923 | |
| 924 | return true; |
| 925 | } |
| 926 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 927 | #ifndef NDEBUG |
| 928 | static bool allSGPRSpillsAreDead(const MachineFrameInfo &MFI, |
| 929 | Optional<int> FramePointerSaveIndex) { |
| 930 | for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); |
| 931 | I != E; ++I) { |
| 932 | if (!MFI.isDeadObjectIndex(I) && |
| 933 | MFI.getStackID(I) == TargetStackID::SGPRSpill && |
| 934 | FramePointerSaveIndex && I != FramePointerSaveIndex) { |
| 935 | return false; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return true; |
| 940 | } |
| 941 | #endif |
| 942 | |
Konstantin Zhuravlyov | ffdb00e | 2017-03-10 19:39:07 +0000 | [diff] [blame] | 943 | int SIFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
| 944 | unsigned &FrameReg) const { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 945 | const SIRegisterInfo *RI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo(); |
Konstantin Zhuravlyov | ffdb00e | 2017-03-10 19:39:07 +0000 | [diff] [blame] | 946 | |
| 947 | FrameReg = RI->getFrameRegister(MF); |
| 948 | return MF.getFrameInfo().getObjectOffset(FI); |
| 949 | } |
| 950 | |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 951 | void SIFrameLowering::processFunctionBeforeFrameFinalized( |
| 952 | MachineFunction &MF, |
| 953 | RegScavenger *RS) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 954 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 955 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 956 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 957 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 958 | SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 959 | |
Stanislav Mekhanoshin | 937ff6e7 | 2019-07-11 21:54:13 +0000 | [diff] [blame] | 960 | FuncInfo->removeDeadFrameIndices(MFI); |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 961 | assert(allSGPRSpillsAreDead(MFI, None) && |
| 962 | "SGPR spill should have been removed in SILowerSGPRSpills"); |
Sander de Smalen | 7f23e0a | 2019-04-02 09:46:52 +0000 | [diff] [blame] | 963 | |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 964 | // FIXME: The other checks should be redundant with allStackObjectsAreDead, |
| 965 | // but currently hasNonSpillStackObjects is set only from source |
| 966 | // allocas. Stack temps produced from legalization are not counted currently. |
| 967 | if (!allStackObjectsAreDead(MFI)) { |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 968 | assert(RS && "RegScavenger required if spilling"); |
| 969 | |
Matt Arsenault | 34c8b83 | 2019-06-05 22:37:50 +0000 | [diff] [blame] | 970 | if (FuncInfo->isEntryFunction()) { |
| 971 | int ScavengeFI = MFI.CreateFixedObject( |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 972 | TRI->getSpillSize(AMDGPU::SGPR_32RegClass), 0, false); |
Matt Arsenault | 34c8b83 | 2019-06-05 22:37:50 +0000 | [diff] [blame] | 973 | RS->addScavengingFrameIndex(ScavengeFI); |
| 974 | } else { |
| 975 | int ScavengeFI = MFI.CreateStackObject( |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 976 | TRI->getSpillSize(AMDGPU::SGPR_32RegClass), |
| 977 | TRI->getSpillAlignment(AMDGPU::SGPR_32RegClass), |
Matt Arsenault | 34c8b83 | 2019-06-05 22:37:50 +0000 | [diff] [blame] | 978 | false); |
| 979 | RS->addScavengingFrameIndex(ScavengeFI); |
| 980 | } |
Matt Arsenault | 707780b | 2017-02-22 21:05:25 +0000 | [diff] [blame] | 981 | } |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 982 | } |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 983 | |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 984 | // Only report VGPRs to generic code. |
| 985 | void SIFrameLowering::determineCalleeSaves(MachineFunction &MF, |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 986 | BitVector &SavedVGPRs, |
Matt Arsenault | ecb43ef | 2017-09-13 23:47:01 +0000 | [diff] [blame] | 987 | RegScavenger *RS) const { |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 988 | TargetFrameLowering::determineCalleeSaves(MF, SavedVGPRs, RS); |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 989 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Michael Liao | 16d3c1a | 2019-07-11 23:53:30 +0000 | [diff] [blame] | 990 | if (MFI->isEntryFunction()) |
| 991 | return; |
| 992 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 993 | const MachineFrameInfo &FrameInfo = MF.getFrameInfo(); |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 994 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
| 995 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 996 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 997 | // Ignore the SGPRs the default implementation found. |
| 998 | SavedVGPRs.clearBitsNotInMask(TRI->getAllVGPRRegMask()); |
| 999 | |
| 1000 | // hasFP only knows about stack objects that already exist. We're now |
| 1001 | // determining the stack slots that will be created, so we have to predict |
| 1002 | // them. Stack objects force FP usage with calls. |
| 1003 | // |
| 1004 | // Note a new VGPR CSR may be introduced if one is used for the spill, but we |
| 1005 | // don't want to report it here. |
| 1006 | // |
| 1007 | // FIXME: Is this really hasReservedCallFrame? |
| 1008 | const bool WillHaveFP = |
| 1009 | FrameInfo.hasCalls() && |
| 1010 | (SavedVGPRs.any() || !allStackObjectsAreDead(FrameInfo)); |
| 1011 | |
| 1012 | // VGPRs used for SGPR spilling need to be specially inserted in the prolog, |
| 1013 | // so don't allow the default insertion to handle them. |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 1014 | for (auto SSpill : MFI->getSGPRSpillVGPRs()) |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 1015 | SavedVGPRs.reset(SSpill.VGPR); |
| 1016 | |
| 1017 | const bool HasFP = WillHaveFP || hasFP(MF); |
| 1018 | if (!HasFP) |
| 1019 | return; |
| 1020 | |
| 1021 | if (MFI->haveFreeLanesForSGPRSpill(MF, 1)) { |
| 1022 | int NewFI = MF.getFrameInfo().CreateStackObject(4, 4, true, nullptr, |
| 1023 | TargetStackID::SGPRSpill); |
| 1024 | |
| 1025 | // If there is already a VGPR with free lanes, use it. We may already have |
| 1026 | // to pay the penalty for spilling a CSR VGPR. |
| 1027 | if (!MFI->allocateSGPRSpillToVGPR(MF, NewFI)) |
| 1028 | llvm_unreachable("allocate SGPR spill should have worked"); |
| 1029 | |
| 1030 | MFI->FramePointerSaveIndex = NewFI; |
| 1031 | |
| 1032 | LLVM_DEBUG( |
| 1033 | auto Spill = MFI->getSGPRToVGPRSpills(NewFI).front(); |
| 1034 | dbgs() << "Spilling FP to " << printReg(Spill.VGPR, TRI) |
| 1035 | << ':' << Spill.Lane << '\n'); |
| 1036 | return; |
| 1037 | } |
| 1038 | |
| 1039 | MFI->SGPRForFPSaveRestoreCopy = findUnusedSGPRNonCalleeSaved(MF.getRegInfo()); |
| 1040 | |
| 1041 | if (!MFI->SGPRForFPSaveRestoreCopy) { |
| 1042 | // There's no free lane to spill, and no free register to save FP, so we're |
| 1043 | // forced to spill another VGPR to use for the spill. |
| 1044 | int NewFI = MF.getFrameInfo().CreateStackObject(4, 4, true, nullptr, |
| 1045 | TargetStackID::SGPRSpill); |
| 1046 | if (!MFI->allocateSGPRSpillToVGPR(MF, NewFI)) |
| 1047 | llvm_unreachable("allocate SGPR spill should have worked"); |
| 1048 | MFI->FramePointerSaveIndex = NewFI; |
| 1049 | |
| 1050 | LLVM_DEBUG( |
| 1051 | auto Spill = MFI->getSGPRToVGPRSpills(NewFI).front(); |
| 1052 | dbgs() << "FP requires fallback spill to " << printReg(Spill.VGPR, TRI) |
| 1053 | << ':' << Spill.Lane << '\n';); |
| 1054 | } else { |
| 1055 | LLVM_DEBUG(dbgs() << "Saving FP with copy to " << |
| 1056 | printReg(MFI->SGPRForFPSaveRestoreCopy, TRI) << '\n'); |
| 1057 | } |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | void SIFrameLowering::determineCalleeSavesSGPR(MachineFunction &MF, |
| 1061 | BitVector &SavedRegs, |
| 1062 | RegScavenger *RS) const { |
| 1063 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
Matt Arsenault | ecb43ef | 2017-09-13 23:47:01 +0000 | [diff] [blame] | 1064 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Michael Liao | 16d3c1a | 2019-07-11 23:53:30 +0000 | [diff] [blame] | 1065 | if (MFI->isEntryFunction()) |
| 1066 | return; |
Matt Arsenault | ecb43ef | 2017-09-13 23:47:01 +0000 | [diff] [blame] | 1067 | |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 1068 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
| 1069 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
| 1070 | |
Matt Arsenault | ecb43ef | 2017-09-13 23:47:01 +0000 | [diff] [blame] | 1071 | // The SP is specifically managed and we don't want extra spills of it. |
| 1072 | SavedRegs.reset(MFI->getStackPtrOffsetReg()); |
Matt Arsenault | 5b0922f | 2019-07-03 23:32:29 +0000 | [diff] [blame] | 1073 | SavedRegs.clearBitsInMask(TRI->getAllVGPRRegMask()); |
Matt Arsenault | ecb43ef | 2017-09-13 23:47:01 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 1076 | bool SIFrameLowering::assignCalleeSavedSpillSlots( |
| 1077 | MachineFunction &MF, const TargetRegisterInfo *TRI, |
| 1078 | std::vector<CalleeSavedInfo> &CSI) const { |
| 1079 | if (CSI.empty()) |
| 1080 | return true; // Early exit if no callee saved registers are modified! |
| 1081 | |
| 1082 | const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
| 1083 | if (!FuncInfo->SGPRForFPSaveRestoreCopy) |
| 1084 | return false; |
| 1085 | |
| 1086 | for (auto &CS : CSI) { |
| 1087 | if (CS.getReg() == FuncInfo->getFrameOffsetReg()) { |
| 1088 | if (FuncInfo->SGPRForFPSaveRestoreCopy != AMDGPU::NoRegister) |
| 1089 | CS.setDstReg(FuncInfo->SGPRForFPSaveRestoreCopy); |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | return false; |
| 1095 | } |
| 1096 | |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 1097 | MachineBasicBlock::iterator SIFrameLowering::eliminateCallFramePseudoInstr( |
| 1098 | MachineFunction &MF, |
| 1099 | MachineBasicBlock &MBB, |
| 1100 | MachineBasicBlock::iterator I) const { |
| 1101 | int64_t Amount = I->getOperand(0).getImm(); |
| 1102 | if (Amount == 0) |
| 1103 | return MBB.erase(I); |
| 1104 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 1105 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 1106 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 1107 | const DebugLoc &DL = I->getDebugLoc(); |
| 1108 | unsigned Opc = I->getOpcode(); |
| 1109 | bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode(); |
| 1110 | uint64_t CalleePopAmount = IsDestroy ? I->getOperand(1).getImm() : 0; |
| 1111 | |
Matt Arsenault | 8fcc70f | 2019-06-25 20:53:35 +0000 | [diff] [blame] | 1112 | if (!hasReservedCallFrame(MF)) { |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 1113 | unsigned Align = getStackAlignment(); |
| 1114 | |
| 1115 | Amount = alignTo(Amount, Align); |
| 1116 | assert(isUInt<32>(Amount) && "exceeded stack address space size"); |
| 1117 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 1118 | unsigned SPReg = MFI->getStackPtrOffsetReg(); |
| 1119 | |
| 1120 | unsigned Op = IsDestroy ? AMDGPU::S_SUB_U32 : AMDGPU::S_ADD_U32; |
| 1121 | BuildMI(MBB, I, DL, TII->get(Op), SPReg) |
| 1122 | .addReg(SPReg) |
| 1123 | .addImm(Amount * ST.getWavefrontSize()); |
| 1124 | } else if (CalleePopAmount != 0) { |
| 1125 | llvm_unreachable("is this used?"); |
| 1126 | } |
| 1127 | |
| 1128 | return MBB.erase(I); |
| 1129 | } |
| 1130 | |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 1131 | bool SIFrameLowering::hasFP(const MachineFunction &MF) const { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 1132 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 1133 | if (MFI.hasCalls()) { |
| 1134 | // All offsets are unsigned, so need to be addressed in the same direction |
| 1135 | // as stack growth. |
Matt Arsenault | 71dfb7e | 2019-07-08 19:03:38 +0000 | [diff] [blame] | 1136 | |
| 1137 | // FIXME: This function is pretty broken, since it can be called before the |
| 1138 | // frame layout is determined or CSR spills are inserted. |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 1139 | if (MFI.getStackSize() != 0) |
| 1140 | return true; |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 1141 | |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 1142 | // For the entry point, the input wave scratch offset must be copied to the |
| 1143 | // API SP if there are calls. |
| 1144 | if (MF.getInfo<SIMachineFunctionInfo>()->isEntryFunction()) |
| 1145 | return true; |
Matt Arsenault | b812b7a | 2019-06-05 22:20:47 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() || |
| 1149 | MFI.hasStackMap() || MFI.hasPatchPoint() || |
Matt Arsenault | 5dc457c | 2019-06-20 17:03:23 +0000 | [diff] [blame] | 1150 | MF.getSubtarget<GCNSubtarget>().getRegisterInfo()->needsStackRealignment(MF) || |
| 1151 | MF.getTarget().Options.DisableFramePointerElim(MF); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 1152 | } |