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