| 1 | //===--------------------- SIFrameLowering.h --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIB_TARGET_AMDGPU_SIFRAMELOWERING_H |
| 10 | #define LLVM_LIB_TARGET_AMDGPU_SIFRAMELOWERING_H |
| 11 | |
| 12 | #include "AMDGPUFrameLowering.h" |
| 13 | #include "SIRegisterInfo.h" |
| 14 | #include "llvm/ADT/SparseBitVector.h" |
| 15 | |
| 16 | namespace llvm { |
| 17 | |
| 18 | class SIInstrInfo; |
| 19 | |
| 20 | class SIFrameLowering final : public AMDGPUFrameLowering { |
| 21 | public: |
| 22 | SIFrameLowering(StackDirection D, Align StackAl, int LAO, |
| 23 | Align TransAl = Align(1)) |
| 24 | : AMDGPUFrameLowering(D, StackAl, LAO, TransAl) {} |
| 25 | ~SIFrameLowering() override = default; |
| 26 | |
| 27 | void emitEntryFunctionPrologue(MachineFunction &MF, |
| 28 | MachineBasicBlock &MBB) const; |
| 29 | void emitEntryFunctionEpilogue(MachineFunction &MF, |
| 30 | MachineBasicBlock &MBB) const; |
| 31 | |
| 32 | void emitPrologue(MachineFunction &MF, |
| 33 | MachineBasicBlock &MBB) const override; |
| 34 | void emitEpilogue(MachineFunction &MF, |
| 35 | MachineBasicBlock &MBB) const override; |
| 36 | StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, |
| 37 | Register &FrameReg) const override; |
| 38 | |
| 39 | void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, |
| 40 | RegScavenger *RS = nullptr) const override; |
| 41 | void determineCalleeSavesSGPR(MachineFunction &MF, BitVector &SavedRegs, |
| 42 | RegScavenger *RS = nullptr) const; |
| 43 | void determinePrologEpilogSGPRSaves(MachineFunction &MF, BitVector &SavedRegs, |
| 44 | bool NeedExecCopyReservedReg) const; |
| 45 | void emitCSRSpillStores(MachineFunction &MF, MachineBasicBlock &MBB, |
| 46 | MachineBasicBlock::iterator MBBI, const DebugLoc &DL, |
| 47 | LiveRegUnits &LiveUnits, Register FrameReg, |
| 48 | Register FramePtrRegScratchCopy, |
| 49 | const bool NeedsFrameMoves) const; |
| 50 | void emitCSRSpillRestores(MachineFunction &MF, MachineBasicBlock &MBB, |
| 51 | MachineBasicBlock::iterator MBBI, |
| 52 | const DebugLoc &DL, LiveRegUnits &LiveUnits, |
| 53 | Register FrameReg, |
| 54 | Register FramePtrRegScratchCopy) const; |
| 55 | bool |
| 56 | assignCalleeSavedSpillSlots(MachineFunction &MF, |
| 57 | const TargetRegisterInfo *TRI, |
| 58 | std::vector<CalleeSavedInfo> &CSI) const override; |
| 59 | |
| 60 | bool assignCalleeSavedSpillSlotsImpl(MachineFunction &MF, |
| 61 | const TargetRegisterInfo *TRI, |
| 62 | std::vector<CalleeSavedInfo> &CSI) const; |
| 63 | |
| 64 | private: |
| 65 | /// Spill a single CSR according to @p CS |
| 66 | /// |
| 67 | /// This is a separate method so it an be shared between the block-ops enabled |
| 68 | /// and disabled paths. Even when block-ops are enabled we may not have a |
| 69 | /// viable block for a specific register, so it will fall back to this |
| 70 | /// implementation. |
| 71 | /// |
| 72 | /// @p LiveInRoots conveys whether we are tracking liveness, and if we are |
| 73 | /// it captures the original live-ins before spilling in a way that can be |
| 74 | /// (relatively) efficiently checked without enumerating all register aliases. |
| 75 | /// See @c buildLiveInRoots in the implementation. |
| 76 | void spillCalleeSavedRegisterWithoutBlockOps( |
| 77 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 78 | const CalleeSavedInfo &CS, const SIInstrInfo *TII, |
| 79 | const SIRegisterInfo &TRI, |
| 80 | const std::optional<SparseBitVector<>> &LiveInRoots) const; |
| 81 | |
| 82 | public: |
| 83 | bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 84 | MachineBasicBlock::iterator MI, |
| 85 | ArrayRef<CalleeSavedInfo> CSI, |
| 86 | const TargetRegisterInfo *TRI) const override; |
| 87 | |
| 88 | bool |
| 89 | restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 90 | MachineBasicBlock::iterator MI, |
| 91 | MutableArrayRef<CalleeSavedInfo> CSI, |
| 92 | const TargetRegisterInfo *TRI) const override; |
| 93 | |
| 94 | bool allocateScavengingFrameIndexesNearIncomingSP( |
| 95 | const MachineFunction &MF) const override; |
| 96 | |
| 97 | bool isSupportedStackID(TargetStackID::Value ID) const override; |
| 98 | |
| 99 | void processFunctionBeforeFrameFinalized( |
| 100 | MachineFunction &MF, |
| 101 | RegScavenger *RS = nullptr) const override; |
| 102 | |
| 103 | void processFunctionBeforeFrameIndicesReplaced( |
| 104 | MachineFunction &MF, RegScavenger *RS = nullptr) const override; |
| 105 | |
| 106 | MachineBasicBlock::iterator |
| 107 | eliminateCallFramePseudoInstr(MachineFunction &MF, |
| 108 | MachineBasicBlock &MBB, |
| 109 | MachineBasicBlock::iterator MI) const override; |
| 110 | |
| 111 | protected: |
| 112 | bool hasFPImpl(const MachineFunction &MF) const override; |
| 113 | |
| 114 | private: |
| 115 | void emitEntryFunctionFlatScratchInit(MachineFunction &MF, |
| 116 | MachineBasicBlock &MBB, |
| 117 | MachineBasicBlock::iterator I, |
| 118 | const DebugLoc &DL, |
| 119 | Register ScratchWaveOffsetReg) const; |
| 120 | |
| 121 | Register getEntryFunctionReservedScratchRsrcReg(MachineFunction &MF) const; |
| 122 | |
| 123 | void emitEntryFunctionScratchRsrcRegSetup( |
| 124 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 125 | MachineBasicBlock::iterator I, const DebugLoc &DL, |
| 126 | Register PreloadedPrivateBufferReg, Register ScratchRsrcReg, |
| 127 | Register ScratchWaveOffsetReg) const; |
| 128 | |
| 129 | void emitPrologueEntryCFI(MachineBasicBlock &MBB, |
| 130 | MachineBasicBlock::iterator MBBI, |
| 131 | const DebugLoc &DL) const; |
| 132 | |
| 133 | void emitDefCFA(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
| 134 | DebugLoc const &DL, MCRegister StackPtrReg, |
| 135 | bool AspaceAlreadyDefined, |
| 136 | MachineInstr::MIFlag Flags = MachineInstr::NoFlags) const; |
| 137 | |
| 138 | public: |
| 139 | bool requiresStackPointerReference(const MachineFunction &MF) const; |
| 140 | |
| 141 | /// Create a CFI index for CFIInst and build a MachineInstr around it. |
| 142 | MachineInstr * |
| 143 | buildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
| 144 | const DebugLoc &DL, const MCCFIInstruction &CFIInst, |
| 145 | MachineInstr::MIFlag flag = MachineInstr::FrameSetup) const; |
| 146 | |
| 147 | /// Create a CFI index describing a spill of the VGPR/AGPR \p Reg to another |
| 148 | /// VGPR/AGPR \p RegCopy and build a MachineInstr around it. |
| 149 | MachineInstr *buildCFIForVRegToVRegSpill(MachineBasicBlock &MBB, |
| 150 | MachineBasicBlock::iterator MBBI, |
| 151 | const DebugLoc &DL, |
| 152 | const MCRegister Reg, |
| 153 | const MCRegister RegCopy) const; |
| 154 | /// Create a CFI index describing a spill of an SGPR to a single lane of |
| 155 | /// a VGPR and build a MachineInstr around it. |
| 156 | MachineInstr *buildCFIForSGPRToVGPRSpill(MachineBasicBlock &MBB, |
| 157 | MachineBasicBlock::iterator MBBI, |
| 158 | const DebugLoc &DL, |
| 159 | const MCRegister SGPR, |
| 160 | const MCRegister VGPR, |
| 161 | const int Lane) const; |
| 162 | /// Create a CFI index describing a spill of an SGPR to multiple lanes of |
| 163 | /// VGPRs and build a MachineInstr around it. |
| 164 | MachineInstr *buildCFIForSGPRToVGPRSpill( |
| 165 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
| 166 | const DebugLoc &DL, MCRegister SGPR, |
| 167 | ArrayRef<SIRegisterInfo::SpilledReg> VGPRSpills) const; |
| 168 | /// Create a CFI index describing a spill of a SGPR to VMEM and |
| 169 | /// build a MachineInstr around it. |
| 170 | MachineInstr *buildCFIForSGPRToVMEMSpill(MachineBasicBlock &MBB, |
| 171 | MachineBasicBlock::iterator MBBI, |
| 172 | const DebugLoc &DL, MCRegister SGPR, |
| 173 | int64_t Offset) const; |
| 174 | /// Create a CFI index describing a spill of a VGPR to VMEM and |
| 175 | /// build a MachineInstr around it. |
| 176 | MachineInstr *buildCFIForVGPRToVMEMSpill(MachineBasicBlock &MBB, |
| 177 | MachineBasicBlock::iterator MBBI, |
| 178 | const DebugLoc &DL, MCRegister VGPR, |
| 179 | int64_t Offset) const; |
| 180 | void buildCFIForSRegToSRegSpill(MachineBasicBlock &MBB, |
| 181 | MachineBasicBlock::iterator MBBI, |
| 182 | const DebugLoc &DL, MCRegister Reg, |
| 183 | MCRegister CopyReg) const; |
| 184 | /// Create a CFI index describing a copy of the 32-bit \p SGPR to \p VCCHalf |
| 185 | /// (vcc_lo or vcc_hi) in wave64 and build a MachineInstr around it. |
| 186 | MachineInstr *buildCFIForSGPRToVCCHalfCopy(MachineBasicBlock &MBB, |
| 187 | MachineBasicBlock::iterator MBBI, |
| 188 | const DebugLoc &DL, |
| 189 | MCRegister SGPR, |
| 190 | MCRegister VCCHalf) const; |
| 191 | MachineInstr *buildCFIForSameValue(MachineBasicBlock &MBB, |
| 192 | MachineBasicBlock::iterator MBBI, |
| 193 | const DebugLoc &DL, MCRegister Reg) const; |
| 194 | // Returns true if the function may need to reserve space on the stack for the |
| 195 | // CWSR trap handler. |
| 196 | bool mayReserveScratchForCWSR(const MachineFunction &MF) const; |
| 197 | }; |
| 198 | |
| 199 | } // end namespace llvm |
| 200 | |
| 201 | #endif // LLVM_LIB_TARGET_AMDGPU_SIFRAMELOWERING_H |
| 202 | |