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 | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 17 | #include "llvm/CodeGen/MachineFunction.h" |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/RegisterScavenging.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 23 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 24 | static ArrayRef<MCPhysReg> getAllSGPR128(const SISubtarget &ST, |
| 25 | const MachineFunction &MF) { |
Matt Arsenault | ab3429c | 2016-05-18 15:19:50 +0000 | [diff] [blame] | 26 | return makeArrayRef(AMDGPU::SGPR_128RegClass.begin(), |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 27 | ST.getMaxNumSGPRs(MF) / 4); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 30 | static ArrayRef<MCPhysReg> getAllSGPRs(const SISubtarget &ST, |
| 31 | const MachineFunction &MF) { |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 32 | return makeArrayRef(AMDGPU::SGPR_32RegClass.begin(), |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 33 | ST.getMaxNumSGPRs(MF)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 36 | void SIFrameLowering::emitFlatScratchInit(const SISubtarget &ST, |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 37 | MachineFunction &MF, |
| 38 | MachineBasicBlock &MBB) const { |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 39 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 40 | const SIRegisterInfo* TRI = &TII->getRegisterInfo(); |
| 41 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 42 | // We don't need this if we only have spills since there is no user facing |
| 43 | // scratch. |
| 44 | |
| 45 | // TODO: If we know we don't have flat instructions earlier, we can omit |
| 46 | // this from the input registers. |
| 47 | // |
| 48 | // TODO: We only need to know if we access scratch space through a flat |
| 49 | // pointer. Because we only detect if flat instructions are used at all, |
| 50 | // this will be used more often than necessary on VI. |
| 51 | |
| 52 | // Debug location must be unknown since the first debug location is used to |
| 53 | // determine the end of the prologue. |
| 54 | DebugLoc DL; |
| 55 | MachineBasicBlock::iterator I = MBB.begin(); |
| 56 | |
| 57 | unsigned FlatScratchInitReg |
| 58 | = TRI->getPreloadedValue(MF, SIRegisterInfo::FLAT_SCRATCH_INIT); |
| 59 | |
| 60 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 61 | MRI.addLiveIn(FlatScratchInitReg); |
| 62 | MBB.addLiveIn(FlatScratchInitReg); |
| 63 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 64 | unsigned FlatScrInitLo = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub0); |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 65 | unsigned FlatScrInitHi = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub1); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 66 | |
| 67 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 68 | unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg(); |
| 69 | |
Matt Arsenault | e823d92 | 2017-02-18 18:29:53 +0000 | [diff] [blame] | 70 | // Do a 64-bit pointer add. |
| 71 | if (ST.flatScratchIsPointer()) { |
| 72 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), AMDGPU::FLAT_SCR_LO) |
| 73 | .addReg(FlatScrInitLo) |
| 74 | .addReg(ScratchWaveOffsetReg); |
| 75 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADDC_U32), AMDGPU::FLAT_SCR_HI) |
| 76 | .addReg(FlatScrInitHi) |
| 77 | .addImm(0); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Copy the size in bytes. |
| 83 | BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::FLAT_SCR_LO) |
| 84 | .addReg(FlatScrInitHi, RegState::Kill); |
| 85 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 86 | // Add wave offset in bytes to private base offset. |
| 87 | // See comment in AMDKernelCodeT.h for enable_sgpr_flat_scratch_init. |
| 88 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), FlatScrInitLo) |
| 89 | .addReg(FlatScrInitLo) |
| 90 | .addReg(ScratchWaveOffsetReg); |
| 91 | |
| 92 | // Convert offset to 256-byte units. |
| 93 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_LSHR_B32), AMDGPU::FLAT_SCR_HI) |
| 94 | .addReg(FlatScrInitLo, RegState::Kill) |
| 95 | .addImm(8); |
| 96 | } |
| 97 | |
| 98 | unsigned SIFrameLowering::getReservedPrivateSegmentBufferReg( |
| 99 | const SISubtarget &ST, |
| 100 | const SIInstrInfo *TII, |
| 101 | const SIRegisterInfo *TRI, |
| 102 | SIMachineFunctionInfo *MFI, |
| 103 | MachineFunction &MF) const { |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 104 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 105 | |
| 106 | // We need to insert initialization of the scratch resource descriptor. |
| 107 | unsigned ScratchRsrcReg = MFI->getScratchRSrcReg(); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 108 | if (ScratchRsrcReg == AMDGPU::NoRegister || |
| 109 | !MRI.isPhysRegUsed(ScratchRsrcReg)) |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 110 | return AMDGPU::NoRegister; |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 111 | |
| 112 | if (ST.hasSGPRInitBug() || |
| 113 | ScratchRsrcReg != TRI->reservedPrivateSegmentBufferReg(MF)) |
| 114 | return ScratchRsrcReg; |
| 115 | |
| 116 | // We reserved the last registers for this. Shift it down to the end of those |
| 117 | // which were actually used. |
| 118 | // |
| 119 | // FIXME: It might be safer to use a pseudoregister before replacement. |
| 120 | |
| 121 | // FIXME: We should be able to eliminate unused input registers. We only |
| 122 | // cannot do this for the resources required for scratch access. For now we |
| 123 | // skip over user SGPRs and may leave unused holes. |
| 124 | |
| 125 | // We find the resource first because it has an alignment requirement. |
| 126 | |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 127 | unsigned NumPreloaded = (MFI->getNumPreloadedSGPRs() + 3) / 4; |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 128 | ArrayRef<MCPhysReg> AllSGPR128s = getAllSGPR128(ST, MF); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 129 | AllSGPR128s = AllSGPR128s.slice(std::min(static_cast<unsigned>(AllSGPR128s.size()), NumPreloaded)); |
| 130 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 131 | // Skip the last N reserved elements because they should have already been |
| 132 | // reserved for VCC etc. |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 133 | for (MCPhysReg Reg : AllSGPR128s) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 134 | // Pick the first unallocated one. Make sure we don't clobber the other |
| 135 | // reserved input we needed. |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 136 | if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 137 | MRI.replaceRegWith(ScratchRsrcReg, Reg); |
| 138 | MFI->setScratchRSrcReg(Reg); |
| 139 | return Reg; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return ScratchRsrcReg; |
| 144 | } |
| 145 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 146 | // Shift down registers reserved for the scratch wave offset and stack pointer |
| 147 | // SGPRs. |
| 148 | std::pair<unsigned, unsigned> |
| 149 | SIFrameLowering::getReservedPrivateSegmentWaveByteOffsetReg( |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 150 | const SISubtarget &ST, |
| 151 | const SIInstrInfo *TII, |
| 152 | const SIRegisterInfo *TRI, |
| 153 | SIMachineFunctionInfo *MFI, |
| 154 | MachineFunction &MF) const { |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 155 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 156 | unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg(); |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 157 | |
| 158 | // No replacement necessary. |
| 159 | if (ScratchWaveOffsetReg == AMDGPU::NoRegister || |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 160 | !MRI.isPhysRegUsed(ScratchWaveOffsetReg)) { |
Matt Arsenault | 1cc47f8 | 2017-07-18 16:44:56 +0000 | [diff] [blame] | 161 | assert(MFI->getStackPtrOffsetReg() == AMDGPU::SP_REG); |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 162 | return std::make_pair(AMDGPU::NoRegister, AMDGPU::NoRegister); |
| 163 | } |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 164 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 165 | unsigned SPReg = MFI->getStackPtrOffsetReg(); |
| 166 | if (ST.hasSGPRInitBug()) |
| 167 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 168 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 169 | unsigned NumPreloaded = MFI->getNumPreloadedSGPRs(); |
| 170 | |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 171 | ArrayRef<MCPhysReg> AllSGPRs = getAllSGPRs(ST, MF); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 172 | if (NumPreloaded > AllSGPRs.size()) |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 173 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 174 | |
| 175 | AllSGPRs = AllSGPRs.slice(NumPreloaded); |
| 176 | |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 177 | // We need to drop register from the end of the list that we cannot use |
| 178 | // for the scratch wave offset. |
| 179 | // + 2 s102 and s103 do not exist on VI. |
| 180 | // + 2 for vcc |
| 181 | // + 2 for xnack_mask |
| 182 | // + 2 for flat_scratch |
| 183 | // + 4 for registers reserved for scratch resource register |
| 184 | // + 1 for register reserved for scratch wave offset. (By exluding this |
| 185 | // register from the list to consider, it means that when this |
| 186 | // register is being used for the scratch wave offset and there |
| 187 | // 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] | 188 | // + 1 if stack pointer is used. |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 189 | // ---- |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 190 | // 13 (+1) |
| 191 | unsigned ReservedRegCount = 13; |
Matt Arsenault | 08906a3 | 2016-10-28 19:43:31 +0000 | [diff] [blame] | 192 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 193 | if (AllSGPRs.size() < ReservedRegCount) |
| 194 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
| 195 | |
| 196 | bool HandledScratchWaveOffsetReg = |
| 197 | ScratchWaveOffsetReg != TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); |
| 198 | |
| 199 | for (MCPhysReg Reg : AllSGPRs.drop_back(ReservedRegCount)) { |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 200 | // Pick the first unallocated SGPR. Be careful not to pick an alias of the |
| 201 | // scratch descriptor, since we haven’t added its uses yet. |
Matt Arsenault | e221849 | 2017-04-24 21:08:32 +0000 | [diff] [blame] | 202 | if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) { |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 203 | if (!HandledScratchWaveOffsetReg) { |
| 204 | HandledScratchWaveOffsetReg = true; |
| 205 | |
| 206 | MRI.replaceRegWith(ScratchWaveOffsetReg, Reg); |
| 207 | MFI->setScratchWaveOffsetReg(Reg); |
| 208 | ScratchWaveOffsetReg = Reg; |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 209 | break; |
| 210 | } |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Matt Arsenault | 36c3122 | 2017-04-25 23:40:57 +0000 | [diff] [blame] | 214 | return std::make_pair(ScratchWaveOffsetReg, SPReg); |
Matt Arsenault | 57bc432 | 2016-08-31 21:52:21 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 217 | void SIFrameLowering::emitEntryFunctionPrologue(MachineFunction &MF, |
| 218 | MachineBasicBlock &MBB) const { |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 219 | // Emit debugger prologue if "amdgpu-debugger-emit-prologue" attribute was |
| 220 | // specified. |
| 221 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 222 | auto AMDGPUASI = ST.getAMDGPUAS(); |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 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 | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 286 | unsigned PreloadedScratchWaveOffsetReg = TRI->getPreloadedValue( |
| 287 | MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); |
| 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 | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 291 | PreloadedPrivateBufferReg = TRI->getPreloadedValue( |
| 292 | MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); |
| 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 | |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 359 | if (ResourceRegUsed && (ST.isMesaGfxShader(MF) || (PreloadedPrivateBufferReg == AMDGPU::NoRegister))) { |
| 360 | assert(!ST.isAmdCodeObjectV2(MF)); |
Matt Arsenault | 1d21517 | 2016-08-31 21:52:25 +0000 | [diff] [blame] | 361 | const MCInstrDesc &SMovB32 = TII->get(AMDGPU::S_MOV_B32); |
| 362 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 363 | unsigned Rsrc2 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub2); |
| 364 | unsigned Rsrc3 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub3); |
| 365 | |
| 366 | // Use relocations to get the pointer, and setup the other bits manually. |
| 367 | uint64_t Rsrc23 = TII->getScratchRsrcWords23(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 368 | |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 369 | if (MFI->hasImplicitBufferPtr()) { |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 370 | unsigned Rsrc01 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0_sub1); |
| 371 | |
| 372 | if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) { |
| 373 | const MCInstrDesc &Mov64 = TII->get(AMDGPU::S_MOV_B64); |
| 374 | |
| 375 | BuildMI(MBB, I, DL, Mov64, Rsrc01) |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 376 | .addReg(MFI->getImplicitBufferPtrUserSGPR()) |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 377 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 378 | } else { |
| 379 | const MCInstrDesc &LoadDwordX2 = TII->get(AMDGPU::S_LOAD_DWORDX2_IMM); |
| 380 | |
| 381 | PointerType *PtrTy = |
| 382 | PointerType::get(Type::getInt64Ty(MF.getFunction()->getContext()), |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 383 | AMDGPUASI.CONSTANT_ADDRESS); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 384 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 385 | auto MMO = MF.getMachineMemOperand(PtrInfo, |
| 386 | MachineMemOperand::MOLoad | |
| 387 | MachineMemOperand::MOInvariant | |
| 388 | MachineMemOperand::MODereferenceable, |
| 389 | 0, 0); |
| 390 | BuildMI(MBB, I, DL, LoadDwordX2, Rsrc01) |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 391 | .addReg(MFI->getImplicitBufferPtrUserSGPR()) |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 392 | .addImm(0) // offset |
| 393 | .addImm(0) // glc |
| 394 | .addMemOperand(MMO) |
| 395 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 396 | } |
| 397 | } else { |
| 398 | unsigned Rsrc0 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0); |
| 399 | unsigned Rsrc1 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub1); |
| 400 | |
| 401 | BuildMI(MBB, I, DL, SMovB32, Rsrc0) |
| 402 | .addExternalSymbol("SCRATCH_RSRC_DWORD0") |
| 403 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 404 | |
| 405 | BuildMI(MBB, I, DL, SMovB32, Rsrc1) |
| 406 | .addExternalSymbol("SCRATCH_RSRC_DWORD1") |
| 407 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 408 | |
| 409 | } |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 410 | |
| 411 | BuildMI(MBB, I, DL, SMovB32, Rsrc2) |
| 412 | .addImm(Rsrc23 & 0xffffffff) |
| 413 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 414 | |
| 415 | BuildMI(MBB, I, DL, SMovB32, Rsrc3) |
| 416 | .addImm(Rsrc23 >> 32) |
| 417 | .addReg(ScratchRsrcReg, RegState::ImplicitDefine); |
| 418 | } |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 421 | void SIFrameLowering::emitPrologue(MachineFunction &MF, |
| 422 | MachineBasicBlock &MBB) const { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 423 | const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
| 424 | if (FuncInfo->isEntryFunction()) { |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 425 | emitEntryFunctionPrologue(MF, MBB); |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | |
| 429 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 430 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 431 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 432 | |
| 433 | unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg(); |
| 434 | unsigned FramePtrReg = FuncInfo->getFrameOffsetReg(); |
| 435 | |
| 436 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 437 | DebugLoc DL; |
| 438 | |
| 439 | bool NeedFP = hasFP(MF); |
| 440 | if (NeedFP) { |
| 441 | // If we need a base pointer, set it up here. It's whatever the value of |
| 442 | // the stack pointer is at this point. Any variable size objects will be |
| 443 | // allocated after this, so we can still use the base pointer to reference |
| 444 | // locals. |
| 445 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::COPY), FramePtrReg) |
| 446 | .addReg(StackPtrReg) |
| 447 | .setMIFlag(MachineInstr::FrameSetup); |
| 448 | } |
| 449 | |
| 450 | uint32_t NumBytes = MFI.getStackSize(); |
| 451 | if (NumBytes != 0 && hasSP(MF)) { |
| 452 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_ADD_U32), StackPtrReg) |
| 453 | .addReg(StackPtrReg) |
| 454 | .addImm(NumBytes * ST.getWavefrontSize()) |
| 455 | .setMIFlag(MachineInstr::FrameSetup); |
| 456 | } |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 459 | void SIFrameLowering::emitEpilogue(MachineFunction &MF, |
| 460 | MachineBasicBlock &MBB) const { |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 461 | const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
| 462 | if (FuncInfo->isEntryFunction()) |
| 463 | return; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 464 | |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 465 | unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg(); |
| 466 | if (StackPtrReg == AMDGPU::NoRegister) |
| 467 | return; |
| 468 | |
| 469 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 470 | uint32_t NumBytes = MFI.getStackSize(); |
| 471 | |
| 472 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 473 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 474 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 475 | DebugLoc DL; |
| 476 | |
| 477 | // FIXME: Clarify distinction between no set SP and SP. For callee functions, |
| 478 | // it's really whether we need SP to be accurate or not. |
| 479 | |
| 480 | if (NumBytes != 0 && hasSP(MF)) { |
| 481 | BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_SUB_U32), StackPtrReg) |
| 482 | .addReg(StackPtrReg) |
| 483 | .addImm(NumBytes * ST.getWavefrontSize()) |
| 484 | .setMIFlag(MachineInstr::FrameDestroy); |
| 485 | } |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 488 | static bool allStackObjectsAreDead(const MachineFrameInfo &MFI) { |
| 489 | for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); |
| 490 | I != E; ++I) { |
| 491 | if (!MFI.isDeadObjectIndex(I)) |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
Konstantin Zhuravlyov | ffdb00e | 2017-03-10 19:39:07 +0000 | [diff] [blame] | 498 | int SIFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
| 499 | unsigned &FrameReg) const { |
| 500 | const SIRegisterInfo *RI = MF.getSubtarget<SISubtarget>().getRegisterInfo(); |
| 501 | |
| 502 | FrameReg = RI->getFrameRegister(MF); |
| 503 | return MF.getFrameInfo().getObjectOffset(FI); |
| 504 | } |
| 505 | |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 506 | void SIFrameLowering::processFunctionBeforeFrameFinalized( |
| 507 | MachineFunction &MF, |
| 508 | RegScavenger *RS) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 509 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 510 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 511 | if (!MFI.hasStackObjects()) |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 512 | return; |
| 513 | |
Matt Arsenault | 7b6c5d2 | 2017-02-22 22:23:32 +0000 | [diff] [blame] | 514 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 515 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 516 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 517 | SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); |
| 518 | bool AllSGPRSpilledToVGPRs = false; |
| 519 | |
| 520 | if (TRI.spillSGPRToVGPR() && FuncInfo->hasSpilledSGPRs()) { |
| 521 | AllSGPRSpilledToVGPRs = true; |
| 522 | |
| 523 | // Process all SGPR spills before frame offsets are finalized. Ideally SGPRs |
| 524 | // are spilled to VGPRs, in which case we can eliminate the stack usage. |
| 525 | // |
| 526 | // XXX - This operates under the assumption that only other SGPR spills are |
| 527 | // users of the frame index. I'm not 100% sure this is correct. The |
| 528 | // StackColoring pass has a comment saying a future improvement would be to |
| 529 | // merging of allocas with spill slots, but for now according to |
| 530 | // MachineFrameInfo isSpillSlot can't alias any other object. |
| 531 | for (MachineBasicBlock &MBB : MF) { |
| 532 | MachineBasicBlock::iterator Next; |
| 533 | for (auto I = MBB.begin(), E = MBB.end(); I != E; I = Next) { |
| 534 | MachineInstr &MI = *I; |
| 535 | Next = std::next(I); |
| 536 | |
| 537 | if (TII->isSGPRSpill(MI)) { |
| 538 | int FI = TII->getNamedOperand(MI, AMDGPU::OpName::addr)->getIndex(); |
| 539 | if (FuncInfo->allocateSGPRSpillToVGPR(MF, FI)) { |
| 540 | bool Spilled = TRI.eliminateSGPRToVGPRSpillFrameIndex(MI, FI, RS); |
| 541 | (void)Spilled; |
| 542 | assert(Spilled && "failed to spill SGPR to VGPR when allocated"); |
| 543 | } else |
| 544 | AllSGPRSpilledToVGPRs = false; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | FuncInfo->removeSGPRToVGPRFrameIndices(MFI); |
| 550 | } |
| 551 | |
| 552 | // FIXME: The other checks should be redundant with allStackObjectsAreDead, |
| 553 | // but currently hasNonSpillStackObjects is set only from source |
| 554 | // allocas. Stack temps produced from legalization are not counted currently. |
| 555 | if (FuncInfo->hasNonSpillStackObjects() || FuncInfo->hasSpilledVGPRs() || |
| 556 | !AllSGPRSpilledToVGPRs || !allStackObjectsAreDead(MFI)) { |
| 557 | assert(RS && "RegScavenger required if spilling"); |
| 558 | |
Matt Arsenault | 707780b | 2017-02-22 21:05:25 +0000 | [diff] [blame] | 559 | // We force this to be at offset 0 so no user object ever has 0 as an |
| 560 | // address, so we may use 0 as an invalid pointer value. This is because |
| 561 | // LLVM assumes 0 is an invalid pointer in address space 0. Because alloca |
| 562 | // is required to be address space 0, we are forced to accept this for |
| 563 | // now. Ideally we could have the stack in another address space with 0 as a |
| 564 | // valid pointer, and -1 as the null value. |
| 565 | // |
| 566 | // This will also waste additional space when user stack objects require > 4 |
| 567 | // byte alignment. |
| 568 | // |
| 569 | // The main cost here is losing the offset for addressing modes. However |
| 570 | // this also ensures we shouldn't need a register for the offset when |
| 571 | // emergency scavenging. |
| 572 | int ScavengeFI = MFI.CreateFixedObject( |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 573 | TRI.getSpillSize(AMDGPU::SGPR_32RegClass), 0, false); |
Matt Arsenault | 707780b | 2017-02-22 21:05:25 +0000 | [diff] [blame] | 574 | RS->addScavengingFrameIndex(ScavengeFI); |
| 575 | } |
Matt Arsenault | 0c90e95 | 2015-11-06 18:17:45 +0000 | [diff] [blame] | 576 | } |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 577 | |
| 578 | void SIFrameLowering::emitDebuggerPrologue(MachineFunction &MF, |
| 579 | MachineBasicBlock &MBB) const { |
| 580 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 581 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 582 | const SIRegisterInfo *TRI = &TII->getRegisterInfo(); |
| 583 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 584 | |
| 585 | MachineBasicBlock::iterator I = MBB.begin(); |
| 586 | DebugLoc DL; |
| 587 | |
| 588 | // For each dimension: |
| 589 | for (unsigned i = 0; i < 3; ++i) { |
| 590 | // Get work group ID SGPR, and make it live-in again. |
| 591 | unsigned WorkGroupIDSGPR = MFI->getWorkGroupIDSGPR(i); |
| 592 | MF.getRegInfo().addLiveIn(WorkGroupIDSGPR); |
| 593 | MBB.addLiveIn(WorkGroupIDSGPR); |
| 594 | |
| 595 | // Since SGPRs are spilled into VGPRs, copy work group ID SGPR to VGPR in |
| 596 | // order to spill it to scratch. |
| 597 | unsigned WorkGroupIDVGPR = |
| 598 | MF.getRegInfo().createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 599 | BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), WorkGroupIDVGPR) |
| 600 | .addReg(WorkGroupIDSGPR); |
| 601 | |
| 602 | // Spill work group ID. |
| 603 | int WorkGroupIDObjectIdx = MFI->getDebuggerWorkGroupIDStackObjectIndex(i); |
| 604 | TII->storeRegToStackSlot(MBB, I, WorkGroupIDVGPR, false, |
| 605 | WorkGroupIDObjectIdx, &AMDGPU::VGPR_32RegClass, TRI); |
| 606 | |
| 607 | // Get work item ID VGPR, and make it live-in again. |
| 608 | unsigned WorkItemIDVGPR = MFI->getWorkItemIDVGPR(i); |
| 609 | MF.getRegInfo().addLiveIn(WorkItemIDVGPR); |
| 610 | MBB.addLiveIn(WorkItemIDVGPR); |
| 611 | |
| 612 | // Spill work item ID. |
| 613 | int WorkItemIDObjectIdx = MFI->getDebuggerWorkItemIDStackObjectIndex(i); |
| 614 | TII->storeRegToStackSlot(MBB, I, WorkItemIDVGPR, false, |
| 615 | WorkItemIDObjectIdx, &AMDGPU::VGPR_32RegClass, TRI); |
| 616 | } |
| 617 | } |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 618 | |
| 619 | bool SIFrameLowering::hasFP(const MachineFunction &MF) const { |
| 620 | // All stack operations are relative to the frame offset SGPR. |
| 621 | // TODO: Still want to eliminate sometimes. |
| 622 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 623 | |
| 624 | // XXX - Is this only called after frame is finalized? Should be able to check |
| 625 | // frame size. |
| 626 | return MFI.hasStackObjects() && !allStackObjectsAreDead(MFI); |
| 627 | } |
| 628 | |
| 629 | bool SIFrameLowering::hasSP(const MachineFunction &MF) const { |
| 630 | // All stack operations are relative to the frame offset SGPR. |
| 631 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 632 | return MFI.hasCalls() || MFI.hasVarSizedObjects(); |
| 633 | } |