Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 1 | //===----------------------- SIFrameLowering.cpp --------------------------===// |
| 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 | #include "SIFrameLowering.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 11 | #include "AMDGPUSubtarget.h" |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 12 | #include "SIInstrInfo.h" |
| 13 | #include "SIMachineFunctionInfo.h" |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 14 | #include "SIRegisterInfo.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 | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 24 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 25 | static ArrayRef<MCPhysReg> getAllSGPR128(const SISubtarget &ST, |
| 26 | const MachineFunction &MF) { |
Matt Arsenault | ab3429c | 2016-05-18 15:19:50 +0000 | [diff] [blame] | 27 | return makeArrayRef(AMDGPU::SGPR_128RegClass.begin(), |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 28 | ST.getMaxNumSGPRs(MF) / 4); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 31 | static ArrayRef<MCPhysReg> getAllSGPRs(const SISubtarget &ST, |
| 32 | const MachineFunction &MF) { |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 33 | return makeArrayRef(AMDGPU::SGPR_32RegClass.begin(), |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 34 | ST.getMaxNumSGPRs(MF)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 37 | void SIFrameLowering::emitFlatScratchInit(const SISubtarget &ST, |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 38 | MachineFunction &MF, |
| 39 | MachineBasicBlock &MBB) const { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 40 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 41 | const SIRegisterInfo* TRI = &TII->getRegisterInfo(); |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 42 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 43 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 44 | // We don't need this if we only have spills since there is no user facing |
| 45 | // scratch. |
| 46 | |
| 47 | // TODO: If we know we don't have flat instructions earlier, we can omit |
| 48 | // this from the input registers. |
| 49 | // |
| 50 | // TODO: We only need to know if we access scratch space through a flat |
| 51 | // pointer. Because we only detect if flat instructions are used at all, |
| 52 | // this will be used more often than necessary on VI. |
| 53 | |
| 54 | // Debug location must be unknown since the first debug location is used to |
| 55 | // determine the end of the prologue. |
| 56 | DebugLoc DL; |
| 57 | MachineBasicBlock::iterator I = MBB.begin(); |
| 58 | |
| 59 | unsigned FlatScratchInitReg |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 60 | = MFI->getPreloadedReg(AMDGPUFunctionArgInfo::FLAT_SCRATCH_INIT); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 61 | |
| 62 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 63 | MRI.addLiveIn(FlatScratchInitReg); |
| 64 | MBB.addLiveIn(FlatScratchInitReg); |
| 65 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 66 | unsigned FlatScrInitLo = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub0); |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 67 | unsigned FlatScrInitHi = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub1); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 68 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 69 | unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg(); |
| 70 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 71 | // Do a 64-bit pointer add. |
| 72 | if (ST.flatScratchIsPointer()) { |
| 73 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), AMDGPU::FLAT_SCR_LO) |
| 74 | .addReg(FlatScrInitLo) |
| 75 | .addReg(ScratchWaveOffsetReg); |
| 76 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADDC_U32), AMDGPU::FLAT_SCR_HI) |
| 77 | .addReg(FlatScrInitHi) |
| 78 | .addImm(0); |
| 79 | |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Copy the size in bytes. |
| 84 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::FLAT_SCR_LO) |
| 85 | .addReg(FlatScrInitHi, RegState::Kill); |
| 86 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 87 | // Add wave offset in bytes to private base offset. |
| 88 | // See comment in AMDKernelCodeT.h for enable_sgpr_flat_scratch_init. |
| 89 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), FlatScrInitLo) |
| 90 | .addReg(FlatScrInitLo) |
| 91 | .addReg(ScratchWaveOffsetReg); |
| 92 | |
| 93 | // Convert offset to 256-byte units. |
| 94 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_LSHR_B32), AMDGPU::FLAT_SCR_HI) |
| 95 | .addReg(FlatScrInitLo, RegState::Kill) |
| 96 | .addImm(8); |
| 97 | } |
| 98 | |
| 99 | unsigned SIFrameLowering::getReservedPrivateSegmentBufferReg( |
| 100 | const SISubtarget &ST, |
| 101 | const SIInstrInfo *TII, |
| 102 | const SIRegisterInfo *TRI, |
| 103 | SIMachineFunctionInfo *MFI, |
| 104 | MachineFunction &MF) const { |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 105 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 106 | |
| 107 | // We need to insert initialization of the scratch resource descriptor. |
| 108 | unsigned ScratchRsrcReg = MFI->getScratchRSrcReg(); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 109 | if (ScratchRsrcReg == AMDGPU::NoRegister || |
| 110 | !MRI.isPhysRegUsed(ScratchRsrcReg)) |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 111 | return AMDGPU::NoRegister; |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 112 | |
| 113 | if (ST.hasSGPRInitBug() || |
| 114 | ScratchRsrcReg != TRI->reservedPrivateSegmentBufferReg(MF)) |
| 115 | return ScratchRsrcReg; |
| 116 | |
| 117 | // We reserved the last registers for this. Shift it down to the end of those |
| 118 | // which were actually used. |
| 119 | // |
| 120 | // FIXME: It might be safer to use a pseudoregister before replacement. |
| 121 | |
| 122 | // FIXME: We should be able to eliminate unused input registers. We only |
| 123 | // cannot do this for the resources required for scratch access. For now we |
| 124 | // skip over user SGPRs and may leave unused holes. |
| 125 | |
| 126 | // We find the resource first because it has an alignment requirement. |
| 127 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 128 | unsigned NumPreloaded = (MFI->getNumPreloadedSGPRs() + 3) / 4; |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 129 | ArrayRef<MCPhysReg> AllSGPR128s = getAllSGPR128(ST, MF); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 130 | AllSGPR128s = AllSGPR128s.slice(std::min(static_cast<unsigned>(AllSGPR128s.size()), NumPreloaded)); |
| 131 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 132 | // Skip the last N reserved elements because they should have already been |
| 133 | // reserved for VCC etc. |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 134 | for (MCPhysReg Reg : AllSGPR128s) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 135 | // Pick the first unallocated one. Make sure we don't clobber the other |
| 136 | // reserved input we needed. |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 137 | if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 138 | MRI.replaceRegWith(ScratchRsrcReg, Reg); |
| 139 | MFI->setScratchRSrcReg(Reg); |
| 140 | return Reg; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return ScratchRsrcReg; |
| 145 | } |
| 146 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 147 | // Shift down registers reserved for the scratch wave offset and stack pointer |
| 148 | // SGPRs. |
| 149 | std::pair<unsigned, unsigned> |
| 150 | SIFrameLowering::getReservedPrivateSegmentWaveByteOffsetReg( |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 151 | const SISubtarget &ST, |
| 152 | const SIInstrInfo *TII, |
| 153 | const SIRegisterInfo *TRI, |
| 154 | SIMachineFunctionInfo *MFI, |
| 155 | MachineFunction &MF) const { |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 156 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 157 | unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg(); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 158 | |
| 159 | // No replacement necessary. |
| 160 | if (ScratchWaveOffsetReg == AMDGPU::NoRegister || |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 161 | !MRI.isPhysRegUsed(ScratchWaveOffsetReg)) { |
Matt Arsenault | 1cc47f8 | 2017-07-18 16:44:56 +0000 | [diff] [blame] | 162 | assert(MFI->getStackPtrOffsetReg() == AMDGPU::SP_REG); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 163 | return std::make_pair(AMDGPU::NoRegister, AMDGPU::NoRegister); |
| 164 | } |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 165 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 166 | unsigned SPReg = MFI->getStackPtrOffsetReg(); |
| 167 | if (ST.hasSGPRInitBug()) |
| 168 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 169 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 170 | unsigned NumPreloaded = MFI->getNumPreloadedSGPRs(); |
| 171 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 172 | ArrayRef<MCPhysReg> AllSGPRs = getAllSGPRs(ST, MF); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 173 | if (NumPreloaded > AllSGPRs.size()) |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 174 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 175 | |
| 176 | AllSGPRs = AllSGPRs.slice(NumPreloaded); |
| 177 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 178 | // We need to drop register from the end of the list that we cannot use |
| 179 | // for the scratch wave offset. |
| 180 | // + 2 s102 and s103 do not exist on VI. |
| 181 | // + 2 for vcc |
| 182 | // + 2 for xnack_mask |
| 183 | // + 2 for flat_scratch |
| 184 | // + 4 for registers reserved for scratch resource register |
| 185 | // + 1 for register reserved for scratch wave offset. (By exluding this |
| 186 | // register from the list to consider, it means that when this |
| 187 | // register is being used for the scratch wave offset and there |
| 188 | // 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] | 189 | // + 1 if stack pointer is used. |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 190 | // ---- |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 191 | // 13 (+1) |
| 192 | unsigned ReservedRegCount = 13; |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 193 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 194 | if (AllSGPRs.size() < ReservedRegCount) |
| 195 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
| 196 | |
| 197 | bool HandledScratchWaveOffsetReg = |
| 198 | ScratchWaveOffsetReg != TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); |
| 199 | |
| 200 | for (MCPhysReg Reg : AllSGPRs.drop_back(ReservedRegCount)) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 201 | // Pick the first unallocated SGPR. Be careful not to pick an alias of the |
| 202 | // scratch descriptor, since we haven’t added its uses yet. |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 203 | if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) { |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 204 | if (!HandledScratchWaveOffsetReg) { |
| 205 | HandledScratchWaveOffsetReg = true; |
| 206 | |
| 207 | MRI.replaceRegWith(ScratchWaveOffsetReg, Reg); |
| 208 | MFI->setScratchWaveOffsetReg(Reg); |
| 209 | ScratchWaveOffsetReg = Reg; |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 210 | break; |
| 211 | } |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 215 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 218 | void SIFrameLowering::emitEntryFunctionPrologue(MachineFunction &MF, |
| 219 | MachineBasicBlock &MBB) const { |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 220 | // Emit debugger prologue if "amdgpu-debugger-emit-prologue" attribute was |
| 221 | // specified. |
| 222 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 223 | if (ST.debuggerEmitPrologue()) |
| 224 | emitDebuggerPrologue(MF, MBB); |
| 225 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 226 | assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); |
| 227 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 228 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 229 | |
| 230 | // If we only have SGPR spills, we won't actually be using scratch memory |
| 231 | // since these spill to VGPRs. |
| 232 | // |
| 233 | // FIXME: We should be cleaning up these unused SGPR spill frame indices |
| 234 | // somewhere. |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 235 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 236 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 237 | const SIRegisterInfo *TRI = &TII->getRegisterInfo(); |
Matt Arsenault | 296b849 | 2016-02-12 06:31:30 +0000 | [diff] [blame] | 238 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 239 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 240 | // We need to do the replacement of the private segment buffer and wave offset |
| 241 | // register even if there are no stack objects. There could be stores to undef |
| 242 | // or a constant without an associated object. |
| 243 | |
| 244 | // FIXME: We still have implicit uses on SGPR spill instructions in case they |
| 245 | // need to spill to vector memory. It's likely that will not happen, but at |
| 246 | // this point it appears we need the setup. This part of the prolog should be |
| 247 | // emitted after frame indices are eliminated. |
| 248 | |
Matt Arsenault | 254ad3d | 2017-07-18 16:44:58 +0000 | [diff] [blame] | 249 | if (MFI->hasFlatScratchInit()) |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 250 | emitFlatScratchInit(ST, MF, MBB); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 251 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 252 | unsigned SPReg = MFI->getStackPtrOffsetReg(); |
Matt Arsenault | 1cc47f8 | 2017-07-18 16:44:56 +0000 | [diff] [blame] | 253 | if (SPReg != AMDGPU::SP_REG) { |
| 254 | assert(MRI.isReserved(SPReg) && "SPReg used but not reserved"); |
| 255 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 256 | DebugLoc DL; |
Matt Arsenault | 254ad3d | 2017-07-18 16:44:58 +0000 | [diff] [blame] | 257 | const MachineFrameInfo &FrameInfo = MF.getFrameInfo(); |
| 258 | int64_t StackSize = FrameInfo.getStackSize(); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 259 | |
| 260 | if (StackSize == 0) { |
| 261 | BuildMI(MBB, MBB.begin(), DL, TII->get(AMDGPU::COPY), SPReg) |
| 262 | .addReg(MFI->getScratchWaveOffsetReg()); |
| 263 | } else { |
| 264 | BuildMI(MBB, MBB.begin(), DL, TII->get(AMDGPU::S_ADD_U32), SPReg) |
| 265 | .addReg(MFI->getScratchWaveOffsetReg()) |
| 266 | .addImm(StackSize * ST.getWavefrontSize()); |
| 267 | } |
| 268 | } |
| 269 | |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 270 | unsigned ScratchRsrcReg |
| 271 | = getReservedPrivateSegmentBufferReg(ST, TII, TRI, MFI, MF); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 272 | |
| 273 | unsigned ScratchWaveOffsetReg; |
| 274 | std::tie(ScratchWaveOffsetReg, SPReg) |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 275 | = getReservedPrivateSegmentWaveByteOffsetReg(ST, TII, TRI, MFI, MF); |
| 276 | |
| 277 | // It's possible to have uses of only ScratchWaveOffsetReg without |
| 278 | // ScratchRsrcReg if it's only used for the initialization of flat_scratch, |
| 279 | // but the inverse is not true. |
| 280 | if (ScratchWaveOffsetReg == AMDGPU::NoRegister) { |
| 281 | assert(ScratchRsrcReg == AMDGPU::NoRegister); |
| 282 | return; |
| 283 | } |
| 284 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 285 | // We need to insert initialization of the scratch resource descriptor. |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 286 | unsigned PreloadedScratchWaveOffsetReg = MFI->getPreloadedReg( |
| 287 | AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 288 | |
| 289 | unsigned PreloadedPrivateBufferReg = AMDGPU::NoRegister; |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 290 | if (ST.isAmdCodeObjectV2(MF)) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 291 | PreloadedPrivateBufferReg = MFI->getPreloadedReg( |
| 292 | AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_BUFFER); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 295 | bool OffsetRegUsed = MRI.isPhysRegUsed(ScratchWaveOffsetReg); |
| 296 | bool ResourceRegUsed = ScratchRsrcReg != AMDGPU::NoRegister && |
| 297 | MRI.isPhysRegUsed(ScratchRsrcReg); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 298 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 299 | // We added live-ins during argument lowering, but since they were not used |
| 300 | // they were deleted. We're adding the uses now, so add them back. |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 301 | if (OffsetRegUsed) { |
| 302 | assert(PreloadedScratchWaveOffsetReg != AMDGPU::NoRegister && |
| 303 | "scratch wave offset input is required"); |
| 304 | MRI.addLiveIn(PreloadedScratchWaveOffsetReg); |
| 305 | MBB.addLiveIn(PreloadedScratchWaveOffsetReg); |
| 306 | } |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 307 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 308 | if (ResourceRegUsed && PreloadedPrivateBufferReg != AMDGPU::NoRegister) { |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 309 | assert(ST.isAmdCodeObjectV2(MF) || ST.isMesaGfxShader(MF)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 310 | MRI.addLiveIn(PreloadedPrivateBufferReg); |
| 311 | MBB.addLiveIn(PreloadedPrivateBufferReg); |
| 312 | } |
| 313 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 314 | // Make the register selected live throughout the function. |
| 315 | for (MachineBasicBlock &OtherBB : MF) { |
| 316 | if (&OtherBB == &MBB) |
| 317 | continue; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 318 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 319 | if (OffsetRegUsed) |
| 320 | OtherBB.addLiveIn(ScratchWaveOffsetReg); |
| 321 | |
| 322 | if (ResourceRegUsed) |
| 323 | OtherBB.addLiveIn(ScratchRsrcReg); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 326 | DebugLoc DL; |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 327 | MachineBasicBlock::iterator I = MBB.begin(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 328 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 329 | // If we reserved the original input registers, we don't need to copy to the |
| 330 | // reserved registers. |
| 331 | |
| 332 | bool CopyBuffer = ResourceRegUsed && |
| 333 | PreloadedPrivateBufferReg != AMDGPU::NoRegister && |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 334 | ST.isAmdCodeObjectV2(MF) && |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 335 | ScratchRsrcReg != PreloadedPrivateBufferReg; |
| 336 | |
| 337 | // This needs to be careful of the copying order to avoid overwriting one of |
| 338 | // the input registers before it's been copied to it's final |
| 339 | // destination. Usually the offset should be copied first. |
| 340 | bool CopyBufferFirst = TRI->isSubRegisterEq(PreloadedPrivateBufferReg, |
| 341 | ScratchWaveOffsetReg); |
| 342 | if (CopyBuffer && CopyBufferFirst) { |
| 343 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchRsrcReg) |
| 344 | .addReg(PreloadedPrivateBufferReg, RegState::Kill); |
| 345 | } |
| 346 | |
| 347 | if (OffsetRegUsed && |
| 348 | PreloadedScratchWaveOffsetReg != ScratchWaveOffsetReg) { |
Matt Arsenault | 1d21517 | 2016-08-31 21:52:25 +0000 | [diff] [blame] | 349 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchWaveOffsetReg) |
Marek Olsak | 584d2c0 | 2017-05-04 22:25:20 +0000 | [diff] [blame] | 350 | .addReg(PreloadedScratchWaveOffsetReg, |
| 351 | MRI.isPhysRegUsed(ScratchWaveOffsetReg) ? 0 : RegState::Kill); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 352 | } |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 353 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 354 | if (CopyBuffer && !CopyBufferFirst) { |
Matt Arsenault | 1d21517 | 2016-08-31 21:52:25 +0000 | [diff] [blame] | 355 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchRsrcReg) |
| 356 | .addReg(PreloadedPrivateBufferReg, RegState::Kill); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 359 | if (ResourceRegUsed) |
| 360 | emitEntryFunctionScratchSetup(ST, MF, MBB, MFI, I, |
| 361 | PreloadedPrivateBufferReg, ScratchRsrcReg); |
| 362 | } |
| 363 | |
| 364 | // Emit scratch setup code for AMDPAL or Mesa, assuming ResourceRegUsed is set. |
| 365 | void SIFrameLowering::emitEntryFunctionScratchSetup(const SISubtarget &ST, |
| 366 | MachineFunction &MF, MachineBasicBlock &MBB, SIMachineFunctionInfo *MFI, |
| 367 | MachineBasicBlock::iterator I, unsigned PreloadedPrivateBufferReg, |
| 368 | unsigned ScratchRsrcReg) const { |
| 369 | |
| 370 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 371 | const SIRegisterInfo *TRI = &TII->getRegisterInfo(); |
| 372 | DebugLoc DL; |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 373 | |
| 374 | if (ST.isAmdPalOS()) { |
| 375 | // The pointer to the GIT is formed from the offset passed in and either |
| 376 | // the amdgpu-git-ptr-high function attribute or the top part of the PC |
| 377 | unsigned RsrcLo = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0); |
| 378 | unsigned RsrcHi = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub1); |
| 379 | unsigned Rsrc01 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0_sub1); |
| 380 | |
| 381 | const MCInstrDesc &SMovB32 = TII->get(AMDGPU::S_MOV_B32); |
| 382 | |
| 383 | if (MFI->getGITPtrHigh() != 0xffffffff) { |
| 384 | BuildMI(MBB, I, DL, SMovB32, RsrcHi) |
| 385 | .addImm(MFI->getGITPtrHigh()) |
| 386 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 387 | } else { |
| 388 | const MCInstrDesc &GetPC64 = TII->get(AMDGPU::S_GETPC_B64); |
| 389 | BuildMI(MBB, I, DL, GetPC64, Rsrc01); |
| 390 | } |
Tim Renouf | 832f90f | 2018-02-26 14:46:43 +0000 | [diff] [blame] | 391 | auto GitPtrLo = AMDGPU::SGPR0; // Low GIT address passed in |
| 392 | if (ST.hasMergedShaders()) { |
| 393 | switch (MF.getFunction().getCallingConv()) { |
| 394 | case CallingConv::AMDGPU_HS: |
| 395 | case CallingConv::AMDGPU_GS: |
| 396 | // Low GIT address is passed in s8 rather than s0 for an LS+HS or |
| 397 | // ES+GS merged shader on gfx9+. |
| 398 | GitPtrLo = AMDGPU::SGPR8; |
| 399 | break; |
| 400 | default: |
| 401 | break; |
| 402 | } |
| 403 | } |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 404 | BuildMI(MBB, I, DL, SMovB32, RsrcLo) |
Tim Renouf | 832f90f | 2018-02-26 14:46:43 +0000 | [diff] [blame] | 405 | .addReg(GitPtrLo) |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 406 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 407 | |
| 408 | // We now have the GIT ptr - now get the scratch descriptor from the entry |
Tim Renouf | cdac172 | 2018-03-28 11:21:07 +0000 | [diff] [blame] | 409 | // at offset 0. |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 410 | PointerType *PtrTy = |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 411 | PointerType::get(Type::getInt64Ty(MF.getFunction().getContext()), |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 412 | AMDGPUAS::CONSTANT_ADDRESS); |
| 413 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 414 | const MCInstrDesc &LoadDwordX4 = TII->get(AMDGPU::S_LOAD_DWORDX4_IMM); |
| 415 | auto MMO = MF.getMachineMemOperand(PtrInfo, |
| 416 | MachineMemOperand::MOLoad | |
| 417 | MachineMemOperand::MOInvariant | |
| 418 | MachineMemOperand::MODereferenceable, |
| 419 | 0, 0); |
| 420 | BuildMI(MBB, I, DL, LoadDwordX4, ScratchRsrcReg) |
| 421 | .addReg(Rsrc01) |
Tim Renouf | cdac172 | 2018-03-28 11:21:07 +0000 | [diff] [blame] | 422 | .addImm(0) // offset |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 423 | .addImm(0) // glc |
| 424 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine) |
| 425 | .addMemOperand(MMO); |
| 426 | return; |
| 427 | } |
| 428 | if (ST.isMesaGfxShader(MF) |
| 429 | || (PreloadedPrivateBufferReg == AMDGPU::NoRegister)) { |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 430 | assert(!ST.isAmdCodeObjectV2(MF)); |
Matt Arsenault | 1d21517 | 2016-08-31 21:52:25 +0000 | [diff] [blame] | 431 | const MCInstrDesc &SMovB32 = TII->get(AMDGPU::S_MOV_B32); |
| 432 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 433 | unsigned Rsrc2 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub2); |
| 434 | unsigned Rsrc3 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub3); |
| 435 | |
| 436 | // Use relocations to get the pointer, and setup the other bits manually. |
| 437 | uint64_t Rsrc23 = TII->getScratchRsrcWords23(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 438 | |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 439 | if (MFI->hasImplicitBufferPtr()) { |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 440 | unsigned Rsrc01 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0_sub1); |
| 441 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 442 | if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) { |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 443 | const MCInstrDesc &Mov64 = TII->get(AMDGPU::S_MOV_B64); |
| 444 | |
| 445 | BuildMI(MBB, I, DL, Mov64, Rsrc01) |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 446 | .addReg(MFI->getImplicitBufferPtrUserSGPR()) |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 447 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 448 | } else { |
| 449 | const MCInstrDesc &LoadDwordX2 = TII->get(AMDGPU::S_LOAD_DWORDX2_IMM); |
| 450 | |
| 451 | PointerType *PtrTy = |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 452 | PointerType::get(Type::getInt64Ty(MF.getFunction().getContext()), |
Konstantin Zhuravlyov | 435151a | 2017-11-01 19:12:38 +0000 | [diff] [blame] | 453 | AMDGPUAS::CONSTANT_ADDRESS); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 454 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 455 | auto MMO = MF.getMachineMemOperand(PtrInfo, |
| 456 | MachineMemOperand::MOLoad | |
| 457 | MachineMemOperand::MOInvariant | |
| 458 | MachineMemOperand::MODereferenceable, |
| 459 | 0, 0); |
| 460 | BuildMI(MBB, I, DL, LoadDwordX2, Rsrc01) |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 461 | .addReg(MFI->getImplicitBufferPtrUserSGPR()) |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 462 | .addImm(0) // offset |
| 463 | .addImm(0) // glc |
| 464 | .addMemOperand(MMO) |
| 465 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 466 | } |
| 467 | } else { |
| 468 | unsigned Rsrc0 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0); |
| 469 | unsigned Rsrc1 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub1); |
| 470 | |
| 471 | BuildMI(MBB, I, DL, SMovB32, Rsrc0) |
| 472 | .addExternalSymbol("SCRATCH_RSRC_DWORD0") |
| 473 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 474 | |
| 475 | BuildMI(MBB, I, DL, SMovB32, Rsrc1) |
| 476 | .addExternalSymbol("SCRATCH_RSRC_DWORD1") |
| 477 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 478 | |
| 479 | } |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 480 | |
| 481 | BuildMI(MBB, I, DL, SMovB32, Rsrc2) |
| 482 | .addImm(Rsrc23 & 0xffffffff) |
| 483 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 484 | |
| 485 | BuildMI(MBB, I, DL, SMovB32, Rsrc3) |
| 486 | .addImm(Rsrc23 >> 32) |
| 487 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 488 | } |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 491 | // Find a scratch register that we can use at the start of the prologue to |
| 492 | // re-align the stack pointer. We avoid using callee-save registers since they |
| 493 | // may appear to be free when this is called from canUseAsPrologue (during |
| 494 | // shrink wrapping), but then no longer be free when this is called from |
| 495 | // emitPrologue. |
| 496 | // |
| 497 | // FIXME: This is a bit conservative, since in the above case we could use one |
| 498 | // of the callee-save registers as a scratch temp to re-align the stack pointer, |
| 499 | // but we would then have to make sure that we were in fact saving at least one |
| 500 | // callee-save register in the prologue, which is additional complexity that |
| 501 | // doesn't seem worth the benefit. |
| 502 | static unsigned findScratchNonCalleeSaveRegister(MachineBasicBlock &MBB) { |
| 503 | MachineFunction *MF = MBB.getParent(); |
| 504 | |
| 505 | const SISubtarget &Subtarget = MF->getSubtarget<SISubtarget>(); |
| 506 | const SIRegisterInfo &TRI = *Subtarget.getRegisterInfo(); |
| 507 | LivePhysRegs LiveRegs(TRI); |
| 508 | LiveRegs.addLiveIns(MBB); |
| 509 | |
| 510 | // Mark callee saved registers as used so we will not choose them. |
| 511 | const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(MF); |
| 512 | for (unsigned i = 0; CSRegs[i]; ++i) |
| 513 | LiveRegs.addReg(CSRegs[i]); |
| 514 | |
| 515 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 516 | |
| 517 | for (unsigned Reg : AMDGPU::SReg_32_XM0RegClass) { |
| 518 | if (LiveRegs.available(MRI, Reg)) |
| 519 | return Reg; |
| 520 | } |
| 521 | |
| 522 | return AMDGPU::NoRegister; |
| 523 | } |
| 524 | |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 525 | void SIFrameLowering::emitPrologue(MachineFunction &MF, |
| 526 | MachineBasicBlock &MBB) const { |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 527 | SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 528 | if (FuncInfo->isEntryFunction()) { |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 529 | emitEntryFunctionPrologue(MF, MBB); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | |
| 533 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 534 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 535 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 536 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 537 | |
| 538 | unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg(); |
| 539 | unsigned FramePtrReg = FuncInfo->getFrameOffsetReg(); |
| 540 | |
| 541 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 542 | DebugLoc DL; |
| 543 | |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 544 | // XXX - Is this the right predicate? |
| 545 | |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 546 | bool NeedFP = hasFP(MF); |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 547 | uint32_t NumBytes = MFI.getStackSize(); |
| 548 | uint32_t RoundedSize = NumBytes; |
| 549 | const bool NeedsRealignment = TRI.needsStackRealignment(MF); |
| 550 | |
| 551 | if (NeedsRealignment) { |
| 552 | assert(NeedFP); |
| 553 | const unsigned Alignment = MFI.getMaxAlignment(); |
| 554 | const unsigned ZeroLowBits = countTrailingZeros(Alignment); |
| 555 | assert(ZeroLowBits > 1); |
| 556 | |
| 557 | RoundedSize += Alignment; |
| 558 | |
| 559 | unsigned ScratchSPReg = findScratchNonCalleeSaveRegister(MBB); |
| 560 | assert(ScratchSPReg != AMDGPU::NoRegister); |
| 561 | |
| 562 | // s_add_u32 tmp_reg, s32, NumBytes |
| 563 | // s_and_b32 s32, tmp_reg, 0b111...0000 |
| 564 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_ADD_U32), ScratchSPReg) |
| 565 | .addReg(StackPtrReg) |
| 566 | .addImm((Alignment - 1) * ST.getWavefrontSize()) |
| 567 | .setMIFlag(MachineInstr::FrameSetup); |
| 568 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_AND_B32), FramePtrReg) |
| 569 | .addReg(ScratchSPReg, RegState::Kill) |
| 570 | .addImm(-Alignment * ST.getWavefrontSize()) |
| 571 | .setMIFlag(MachineInstr::FrameSetup); |
| 572 | FuncInfo->setIsStackRealigned(true); |
| 573 | } else if (NeedFP) { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 574 | // If we need a base pointer, set it up here. It's whatever the value of |
| 575 | // the stack pointer is at this point. Any variable size objects will be |
| 576 | // allocated after this, so we can still use the base pointer to reference |
| 577 | // locals. |
| 578 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::COPY), FramePtrReg) |
| 579 | .addReg(StackPtrReg) |
| 580 | .setMIFlag(MachineInstr::FrameSetup); |
| 581 | } |
| 582 | |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 583 | if (RoundedSize != 0 && hasSP(MF)) { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 584 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_ADD_U32), StackPtrReg) |
| 585 | .addReg(StackPtrReg) |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 586 | .addImm(RoundedSize * ST.getWavefrontSize()) |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 587 | .setMIFlag(MachineInstr::FrameSetup); |
| 588 | } |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 589 | |
| 590 | for (const SIMachineFunctionInfo::SGPRSpillVGPRCSR &Reg |
| 591 | : FuncInfo->getSGPRSpillVGPRs()) { |
| 592 | if (!Reg.FI.hasValue()) |
| 593 | continue; |
| 594 | TII->storeRegToStackSlot(MBB, MBBI, Reg.VGPR, true, |
| 595 | Reg.FI.getValue(), &AMDGPU::VGPR_32RegClass, |
| 596 | &TII->getRegisterInfo()); |
| 597 | } |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 600 | void SIFrameLowering::emitEpilogue(MachineFunction &MF, |
| 601 | MachineBasicBlock &MBB) const { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 602 | const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
| 603 | if (FuncInfo->isEntryFunction()) |
| 604 | return; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 605 | |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 606 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 607 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 608 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 609 | |
| 610 | for (const SIMachineFunctionInfo::SGPRSpillVGPRCSR &Reg |
| 611 | : FuncInfo->getSGPRSpillVGPRs()) { |
| 612 | if (!Reg.FI.hasValue()) |
| 613 | continue; |
| 614 | TII->loadRegFromStackSlot(MBB, MBBI, Reg.VGPR, |
| 615 | Reg.FI.getValue(), &AMDGPU::VGPR_32RegClass, |
| 616 | &TII->getRegisterInfo()); |
| 617 | } |
| 618 | |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 619 | unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg(); |
| 620 | if (StackPtrReg == AMDGPU::NoRegister) |
| 621 | return; |
| 622 | |
| 623 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 624 | uint32_t NumBytes = MFI.getStackSize(); |
| 625 | |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 626 | DebugLoc DL; |
| 627 | |
| 628 | // FIXME: Clarify distinction between no set SP and SP. For callee functions, |
| 629 | // it's really whether we need SP to be accurate or not. |
| 630 | |
| 631 | if (NumBytes != 0 && hasSP(MF)) { |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 632 | uint32_t RoundedSize = FuncInfo->isStackRealigned() ? |
| 633 | NumBytes + MFI.getMaxAlignment() : NumBytes; |
| 634 | |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 635 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_SUB_U32), StackPtrReg) |
| 636 | .addReg(StackPtrReg) |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 637 | .addImm(RoundedSize * ST.getWavefrontSize()); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 638 | } |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 641 | static bool allStackObjectsAreDead(const MachineFrameInfo &MFI) { |
| 642 | for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); |
| 643 | I != E; ++I) { |
| 644 | if (!MFI.isDeadObjectIndex(I)) |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | return true; |
| 649 | } |
| 650 | |
Konstantin Zhuravlyov | ffdb00e | 2017-03-10 19:39:07 +0000 | [diff] [blame] | 651 | int SIFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
| 652 | unsigned &FrameReg) const { |
| 653 | const SIRegisterInfo *RI = MF.getSubtarget<SISubtarget>().getRegisterInfo(); |
| 654 | |
| 655 | FrameReg = RI->getFrameRegister(MF); |
| 656 | return MF.getFrameInfo().getObjectOffset(FI); |
| 657 | } |
| 658 | |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 659 | void SIFrameLowering::processFunctionBeforeFrameFinalized( |
| 660 | MachineFunction &MF, |
| 661 | RegScavenger *RS) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 662 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 663 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 664 | if (!MFI.hasStackObjects()) |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 665 | return; |
| 666 | |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 667 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 668 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 669 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 670 | SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
| 671 | bool AllSGPRSpilledToVGPRs = false; |
| 672 | |
| 673 | if (TRI.spillSGPRToVGPR() && FuncInfo->hasSpilledSGPRs()) { |
| 674 | AllSGPRSpilledToVGPRs = true; |
| 675 | |
| 676 | // Process all SGPR spills before frame offsets are finalized. Ideally SGPRs |
| 677 | // are spilled to VGPRs, in which case we can eliminate the stack usage. |
| 678 | // |
| 679 | // XXX - This operates under the assumption that only other SGPR spills are |
| 680 | // users of the frame index. I'm not 100% sure this is correct. The |
| 681 | // StackColoring pass has a comment saying a future improvement would be to |
| 682 | // merging of allocas with spill slots, but for now according to |
| 683 | // MachineFrameInfo isSpillSlot can't alias any other object. |
| 684 | for (MachineBasicBlock &MBB : MF) { |
| 685 | MachineBasicBlock::iterator Next; |
| 686 | for (auto I = MBB.begin(), E = MBB.end(); I != E; I = Next) { |
| 687 | MachineInstr &MI = *I; |
| 688 | Next = std::next(I); |
| 689 | |
| 690 | if (TII->isSGPRSpill(MI)) { |
| 691 | int FI = TII->getNamedOperand(MI, AMDGPU::OpName::addr)->getIndex(); |
| 692 | if (FuncInfo->allocateSGPRSpillToVGPR(MF, FI)) { |
| 693 | bool Spilled = TRI.eliminateSGPRToVGPRSpillFrameIndex(MI, FI, RS); |
| 694 | (void)Spilled; |
| 695 | assert(Spilled && "failed to spill SGPR to VGPR when allocated"); |
| 696 | } else |
| 697 | AllSGPRSpilledToVGPRs = false; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | FuncInfo->removeSGPRToVGPRFrameIndices(MFI); |
| 703 | } |
| 704 | |
| 705 | // FIXME: The other checks should be redundant with allStackObjectsAreDead, |
| 706 | // but currently hasNonSpillStackObjects is set only from source |
| 707 | // allocas. Stack temps produced from legalization are not counted currently. |
| 708 | if (FuncInfo->hasNonSpillStackObjects() || FuncInfo->hasSpilledVGPRs() || |
| 709 | !AllSGPRSpilledToVGPRs || !allStackObjectsAreDead(MFI)) { |
| 710 | assert(RS && "RegScavenger required if spilling"); |
| 711 | |
Matt Arsenault | 707780b | 2017-02-22 21:05:25 +0000 | [diff] [blame] | 712 | // We force this to be at offset 0 so no user object ever has 0 as an |
| 713 | // address, so we may use 0 as an invalid pointer value. This is because |
| 714 | // LLVM assumes 0 is an invalid pointer in address space 0. Because alloca |
| 715 | // is required to be address space 0, we are forced to accept this for |
| 716 | // now. Ideally we could have the stack in another address space with 0 as a |
| 717 | // valid pointer, and -1 as the null value. |
| 718 | // |
| 719 | // This will also waste additional space when user stack objects require > 4 |
| 720 | // byte alignment. |
| 721 | // |
| 722 | // The main cost here is losing the offset for addressing modes. However |
| 723 | // this also ensures we shouldn't need a register for the offset when |
| 724 | // emergency scavenging. |
| 725 | int ScavengeFI = MFI.CreateFixedObject( |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 726 | TRI.getSpillSize(AMDGPU::SGPR_32RegClass), 0, false); |
Matt Arsenault | 707780b | 2017-02-22 21:05:25 +0000 | [diff] [blame] | 727 | RS->addScavengingFrameIndex(ScavengeFI); |
| 728 | } |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 729 | } |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 730 | |
Matt Arsenault | ecb43ef | 2017-09-13 23:47:01 +0000 | [diff] [blame] | 731 | void SIFrameLowering::determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, |
| 732 | RegScavenger *RS) const { |
| 733 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 734 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 735 | |
| 736 | // The SP is specifically managed and we don't want extra spills of it. |
| 737 | SavedRegs.reset(MFI->getStackPtrOffsetReg()); |
| 738 | } |
| 739 | |
Matt Arsenault | b62a4eb | 2017-08-01 19:54:18 +0000 | [diff] [blame] | 740 | MachineBasicBlock::iterator SIFrameLowering::eliminateCallFramePseudoInstr( |
| 741 | MachineFunction &MF, |
| 742 | MachineBasicBlock &MBB, |
| 743 | MachineBasicBlock::iterator I) const { |
| 744 | int64_t Amount = I->getOperand(0).getImm(); |
| 745 | if (Amount == 0) |
| 746 | return MBB.erase(I); |
| 747 | |
| 748 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 749 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 750 | const DebugLoc &DL = I->getDebugLoc(); |
| 751 | unsigned Opc = I->getOpcode(); |
| 752 | bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode(); |
| 753 | uint64_t CalleePopAmount = IsDestroy ? I->getOperand(1).getImm() : 0; |
| 754 | |
| 755 | const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); |
| 756 | if (!TFI->hasReservedCallFrame(MF)) { |
| 757 | unsigned Align = getStackAlignment(); |
| 758 | |
| 759 | Amount = alignTo(Amount, Align); |
| 760 | assert(isUInt<32>(Amount) && "exceeded stack address space size"); |
| 761 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 762 | unsigned SPReg = MFI->getStackPtrOffsetReg(); |
| 763 | |
| 764 | unsigned Op = IsDestroy ? AMDGPU::S_SUB_U32 : AMDGPU::S_ADD_U32; |
| 765 | BuildMI(MBB, I, DL, TII->get(Op), SPReg) |
| 766 | .addReg(SPReg) |
| 767 | .addImm(Amount * ST.getWavefrontSize()); |
| 768 | } else if (CalleePopAmount != 0) { |
| 769 | llvm_unreachable("is this used?"); |
| 770 | } |
| 771 | |
| 772 | return MBB.erase(I); |
| 773 | } |
| 774 | |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 775 | void SIFrameLowering::emitDebuggerPrologue(MachineFunction &MF, |
| 776 | MachineBasicBlock &MBB) const { |
| 777 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 778 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 779 | const SIRegisterInfo *TRI = &TII->getRegisterInfo(); |
| 780 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 781 | |
| 782 | MachineBasicBlock::iterator I = MBB.begin(); |
| 783 | DebugLoc DL; |
| 784 | |
| 785 | // For each dimension: |
| 786 | for (unsigned i = 0; i < 3; ++i) { |
| 787 | // Get work group ID SGPR, and make it live-in again. |
| 788 | unsigned WorkGroupIDSGPR = MFI->getWorkGroupIDSGPR(i); |
| 789 | MF.getRegInfo().addLiveIn(WorkGroupIDSGPR); |
| 790 | MBB.addLiveIn(WorkGroupIDSGPR); |
| 791 | |
| 792 | // Since SGPRs are spilled into VGPRs, copy work group ID SGPR to VGPR in |
| 793 | // order to spill it to scratch. |
| 794 | unsigned WorkGroupIDVGPR = |
| 795 | MF.getRegInfo().createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 796 | BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), WorkGroupIDVGPR) |
| 797 | .addReg(WorkGroupIDSGPR); |
| 798 | |
| 799 | // Spill work group ID. |
| 800 | int WorkGroupIDObjectIdx = MFI->getDebuggerWorkGroupIDStackObjectIndex(i); |
| 801 | TII->storeRegToStackSlot(MBB, I, WorkGroupIDVGPR, false, |
| 802 | WorkGroupIDObjectIdx, &AMDGPU::VGPR_32RegClass, TRI); |
| 803 | |
| 804 | // Get work item ID VGPR, and make it live-in again. |
| 805 | unsigned WorkItemIDVGPR = MFI->getWorkItemIDVGPR(i); |
| 806 | MF.getRegInfo().addLiveIn(WorkItemIDVGPR); |
| 807 | MBB.addLiveIn(WorkItemIDVGPR); |
| 808 | |
| 809 | // Spill work item ID. |
| 810 | int WorkItemIDObjectIdx = MFI->getDebuggerWorkItemIDStackObjectIndex(i); |
| 811 | TII->storeRegToStackSlot(MBB, I, WorkItemIDVGPR, false, |
| 812 | WorkItemIDObjectIdx, &AMDGPU::VGPR_32RegClass, TRI); |
| 813 | } |
| 814 | } |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 815 | |
| 816 | bool SIFrameLowering::hasFP(const MachineFunction &MF) const { |
| 817 | // All stack operations are relative to the frame offset SGPR. |
| 818 | // TODO: Still want to eliminate sometimes. |
| 819 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 820 | |
| 821 | // XXX - Is this only called after frame is finalized? Should be able to check |
| 822 | // frame size. |
| 823 | return MFI.hasStackObjects() && !allStackObjectsAreDead(MFI); |
| 824 | } |
| 825 | |
| 826 | bool SIFrameLowering::hasSP(const MachineFunction &MF) const { |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 827 | const SIRegisterInfo *TRI = MF.getSubtarget<SISubtarget>().getRegisterInfo(); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 828 | // All stack operations are relative to the frame offset SGPR. |
| 829 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame^] | 830 | return MFI.hasCalls() || MFI.hasVarSizedObjects() || TRI->needsStackRealignment(MF); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 831 | } |