| 1 | //===----------------------------------------------------------------------===// |
| 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 | /// \file |
| 10 | /// This file contains the declaration of the AArch64PrologueEmitter and |
| 11 | /// AArch64EpilogueEmitter classes, which are is used to emit the prologue and |
| 12 | /// epilogue on AArch64. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64PROLOGUEEPILOGUE_H |
| 17 | #define LLVM_LIB_TARGET_AARCH64_AARCH64PROLOGUEEPILOGUE_H |
| 18 | |
| 19 | #include "AArch64RegisterInfo.h" |
| 20 | #include "llvm/CodeGen/LivePhysRegs.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | class AArch64FunctionInfo; |
| 26 | class AArch64FrameLowering; |
| 27 | class AArch64InstrInfo; |
| 28 | class AArch64Subtarget; |
| 29 | class TargetLowering; |
| 30 | |
| 31 | struct SVEFrameSizes { |
| 32 | struct { |
| 33 | StackOffset CalleeSavesSize, LocalsSize; |
| 34 | } PPR, ZPR; |
| 35 | }; |
| 36 | |
| 37 | struct SVEStackAllocations { |
| 38 | StackOffset BeforePPRs, AfterPPRs, AfterZPRs; |
| 39 | StackOffset totalSize() const { return BeforePPRs + AfterPPRs + AfterZPRs; } |
| 40 | }; |
| 41 | |
| 42 | class AArch64PrologueEpilogueCommon { |
| 43 | public: |
| 44 | AArch64PrologueEpilogueCommon(MachineFunction &MF, MachineBasicBlock &MBB, |
| 45 | const AArch64FrameLowering &AFL); |
| 46 | |
| 47 | enum class SVEStackLayout { |
| 48 | Default, |
| 49 | Split, |
| 50 | CalleeSavesAboveFrameRecord, |
| 51 | }; |
| 52 | |
| 53 | protected: |
| 54 | bool requiresGetVGCall() const; |
| 55 | |
| 56 | bool isVGInstruction(MachineBasicBlock::iterator MBBI, |
| 57 | const TargetLowering &TLI) const; |
| 58 | |
| 59 | // Convert callee-save register save/restore instruction to do stack pointer |
| 60 | // decrement/increment to allocate/deallocate the callee-save stack area by |
| 61 | // converting store/load to use pre/post increment version. |
| 62 | MachineBasicBlock::iterator convertCalleeSaveRestoreToSPPrePostIncDec( |
| 63 | MachineBasicBlock::iterator MBBI, const DebugLoc &DL, int CSStackSizeInc, |
| 64 | bool EmitCFI, MachineInstr::MIFlag FrameFlag = MachineInstr::FrameSetup, |
| 65 | int CFAOffset = 0) const; |
| 66 | |
| 67 | // Fixup callee-save register save/restore instructions to take into account |
| 68 | // combined SP bump by adding the local stack size to the stack offsets. |
| 69 | void fixupCalleeSaveRestoreStackOffset(MachineInstr &MI, |
| 70 | uint64_t LocalStackSize) const; |
| 71 | |
| 72 | bool shouldCombineCSRLocalStackBump(uint64_t StackBumpBytes) const; |
| 73 | |
| 74 | SVEFrameSizes getSVEStackFrameSizes() const; |
| 75 | SVEStackAllocations getSVEStackAllocations(SVEFrameSizes const &); |
| 76 | |
| 77 | MachineFunction &MF; |
| 78 | MachineBasicBlock &MBB; |
| 79 | |
| 80 | const MachineFrameInfo &MFI; |
| 81 | const AArch64Subtarget &Subtarget; |
| 82 | const AArch64FrameLowering &AFL; |
| 83 | const AArch64RegisterInfo &RegInfo; |
| 84 | |
| 85 | // Common flags. These generally should not change outside of the (possibly |
| 86 | // derived) constructor. |
| 87 | bool HasFP = false; |
| 88 | bool EmitCFI = false; // Note: Set in derived constructors. |
| 89 | bool IsFunclet = false; // Note: Set in derived constructors. |
| 90 | bool NeedsWinCFI = false; // Note: Can be changed in emitFramePointerSetup. |
| 91 | bool HomPrologEpilog = false; // Note: Set in derived constructors. |
| 92 | SVEStackLayout SVELayout = SVEStackLayout::Default; |
| 93 | |
| 94 | // Note: "HasWinCFI" is mutable as it can change in any "emit" function. |
| 95 | mutable bool HasWinCFI = false; |
| 96 | |
| 97 | const AArch64InstrInfo *TII = nullptr; |
| 98 | AArch64FunctionInfo *AFI = nullptr; |
| 99 | }; |
| 100 | |
| 101 | /// A helper class for emitting the prologue. Substantial new functionality |
| 102 | /// should be factored into a new method. Where possible "emit*" methods should |
| 103 | /// be const, and any flags that change how the prologue is emitted should be |
| 104 | /// set in the constructor. |
| 105 | class AArch64PrologueEmitter final : public AArch64PrologueEpilogueCommon { |
| 106 | public: |
| 107 | AArch64PrologueEmitter(MachineFunction &MF, MachineBasicBlock &MBB, |
| 108 | const AArch64FrameLowering &AFL); |
| 109 | |
| 110 | /// Emit the prologue. |
| 111 | void emitPrologue(); |
| 112 | |
| 113 | ~AArch64PrologueEmitter() { |
| 114 | MF.setHasWinCFI(HasWinCFI); |
| 115 | #ifndef NDEBUG |
| 116 | verifyPrologueClobbers(); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | void allocateStackSpace(MachineBasicBlock::iterator MBBI, |
| 122 | int64_t RealignmentPadding, StackOffset AllocSize, |
| 123 | bool EmitCFI, StackOffset InitialOffset, |
| 124 | bool FollowupAllocs); |
| 125 | |
| 126 | void emitShadowCallStackPrologue(MachineBasicBlock::iterator MBBI, |
| 127 | const DebugLoc &DL) const; |
| 128 | |
| 129 | void emitSwiftAsyncContextFramePointer(MachineBasicBlock::iterator MBBI, |
| 130 | const DebugLoc &DL) const; |
| 131 | |
| 132 | void emitEmptyStackFramePrologue(int64_t NumBytes, |
| 133 | MachineBasicBlock::iterator MBBI, |
| 134 | const DebugLoc &DL) const; |
| 135 | |
| 136 | void emitFramePointerSetup(MachineBasicBlock::iterator MBBI, |
| 137 | const DebugLoc &DL, unsigned FixedObject); |
| 138 | |
| 139 | void emitDefineCFAWithFP(MachineBasicBlock::iterator MBBI, |
| 140 | unsigned FixedObject) const; |
| 141 | |
| 142 | void emitWindowsStackProbe(MachineBasicBlock::iterator MBBI, |
| 143 | const DebugLoc &DL, int64_t &NumBytes, |
| 144 | int64_t RealignmentPadding) const; |
| 145 | |
| 146 | void emitCalleeSavedGPRLocations(MachineBasicBlock::iterator MBBI) const; |
| 147 | void emitCalleeSavedSVELocations(MachineBasicBlock::iterator MBBI) const; |
| 148 | |
| 149 | void determineLocalsStackSize(uint64_t StackSize, uint64_t PrologueSaveSize); |
| 150 | |
| 151 | const Function &F; |
| 152 | |
| 153 | #ifndef NDEBUG |
| 154 | mutable LivePhysRegs LiveRegs{RegInfo}; |
| 155 | MachineBasicBlock::iterator PrologueEndI; |
| 156 | |
| 157 | void collectBlockLiveins(); |
| 158 | void verifyPrologueClobbers() const; |
| 159 | #endif |
| 160 | |
| 161 | // Prologue flags. These generally should not change outside of the |
| 162 | // constructor. |
| 163 | bool EmitAsyncCFI = false; |
| 164 | bool CombineSPBump = false; // Note: This is set in determineLocalsStackSize. |
| 165 | }; |
| 166 | |
| 167 | /// A helper class for emitting the epilogue. Substantial new functionality |
| 168 | /// should be factored into a new method. Where possible "emit*" methods should |
| 169 | /// be const, and any flags that change how the epilogue is emitted should be |
| 170 | /// set in the constructor. |
| 171 | class AArch64EpilogueEmitter final : public AArch64PrologueEpilogueCommon { |
| 172 | public: |
| 173 | AArch64EpilogueEmitter(MachineFunction &MF, MachineBasicBlock &MBB, |
| 174 | const AArch64FrameLowering &AFL); |
| 175 | |
| 176 | /// Emit the epilogue. |
| 177 | void emitEpilogue(); |
| 178 | |
| 179 | ~AArch64EpilogueEmitter() { finalizeEpilogue(); } |
| 180 | |
| 181 | private: |
| 182 | bool shouldCombineCSRLocalStackBump(uint64_t StackBumpBytes) const; |
| 183 | |
| 184 | /// A helper for moving the SP to a negative offset from the FP, without |
| 185 | /// deallocating any stack in the range FP to FP + Offset. |
| 186 | void moveSPBelowFP(MachineBasicBlock::iterator MBBI, StackOffset Offset); |
| 187 | |
| 188 | void emitSwiftAsyncContextFramePointer(MachineBasicBlock::iterator MBBI, |
| 189 | const DebugLoc &DL) const; |
| 190 | |
| 191 | void emitShadowCallStackEpilogue(MachineBasicBlock::iterator MBBI, |
| 192 | const DebugLoc &DL) const; |
| 193 | |
| 194 | void emitCalleeSavedRestores(MachineBasicBlock::iterator MBBI, |
| 195 | bool SVE) const; |
| 196 | |
| 197 | void emitCalleeSavedGPRRestores(MachineBasicBlock::iterator MBBI) const { |
| 198 | emitCalleeSavedRestores(MBBI, /*SVE=*/SVE: false); |
| 199 | } |
| 200 | |
| 201 | void emitCalleeSavedSVERestores(MachineBasicBlock::iterator MBBI) const { |
| 202 | emitCalleeSavedRestores(MBBI, /*SVE=*/SVE: true); |
| 203 | } |
| 204 | |
| 205 | void finalizeEpilogue() const; |
| 206 | |
| 207 | MachineBasicBlock::iterator SEHEpilogueStartI; |
| 208 | DebugLoc DL; |
| 209 | }; |
| 210 | |
| 211 | } // namespace llvm |
| 212 | |
| 213 | #endif |
| 214 | |