Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- llvm/CodeGen/TargetFrameLowering.h ----------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Interface to describe the layout of a stack frame on the target machine. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_CODEGEN_TARGETFRAMELOWERING_H |
| 14 | #define LLVM_CODEGEN_TARGETFRAMELOWERING_H |
| 15 | |
| 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
| 20 | namespace llvm { |
| 21 | class BitVector; |
| 22 | class CalleeSavedInfo; |
| 23 | class MachineFunction; |
| 24 | class RegScavenger; |
| 25 | |
| 26 | /// Information about stack frame layout on the target. It holds the direction |
| 27 | /// of stack growth, the known stack alignment on entry to each function, and |
| 28 | /// the offset to the locals area. |
| 29 | /// |
| 30 | /// The offset to the local area is the offset from the stack pointer on |
| 31 | /// function entry to the first location where function data (local variables, |
| 32 | /// spill locations) can be stored. |
| 33 | class TargetFrameLowering { |
| 34 | public: |
| 35 | enum StackDirection { |
| 36 | StackGrowsUp, // Adding to the stack increases the stack address |
| 37 | StackGrowsDown // Adding to the stack decreases the stack address |
| 38 | }; |
| 39 | |
| 40 | // Maps a callee saved register to a stack slot with a fixed offset. |
| 41 | struct SpillSlot { |
| 42 | unsigned Reg; |
| 43 | int Offset; // Offset relative to stack pointer on function entry. |
| 44 | }; |
| 45 | private: |
| 46 | StackDirection StackDir; |
| 47 | unsigned StackAlignment; |
| 48 | unsigned TransientStackAlignment; |
| 49 | int LocalAreaOffset; |
| 50 | bool StackRealignable; |
| 51 | public: |
| 52 | TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO, |
| 53 | unsigned TransAl = 1, bool StackReal = true) |
| 54 | : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl), |
| 55 | LocalAreaOffset(LAO), StackRealignable(StackReal) {} |
| 56 | |
| 57 | virtual ~TargetFrameLowering(); |
| 58 | |
| 59 | // These methods return information that describes the abstract stack layout |
| 60 | // of the target machine. |
| 61 | |
| 62 | /// getStackGrowthDirection - Return the direction the stack grows |
| 63 | /// |
| 64 | StackDirection getStackGrowthDirection() const { return StackDir; } |
| 65 | |
| 66 | /// getStackAlignment - This method returns the number of bytes to which the |
| 67 | /// stack pointer must be aligned on entry to a function. Typically, this |
| 68 | /// is the largest alignment for any data object in the target. |
| 69 | /// |
| 70 | unsigned getStackAlignment() const { return StackAlignment; } |
| 71 | |
| 72 | /// alignSPAdjust - This method aligns the stack adjustment to the correct |
| 73 | /// alignment. |
| 74 | /// |
| 75 | int alignSPAdjust(int SPAdj) const { |
| 76 | if (SPAdj < 0) { |
| 77 | SPAdj = -alignTo(-SPAdj, StackAlignment); |
| 78 | } else { |
| 79 | SPAdj = alignTo(SPAdj, StackAlignment); |
| 80 | } |
| 81 | return SPAdj; |
| 82 | } |
| 83 | |
| 84 | /// getTransientStackAlignment - This method returns the number of bytes to |
| 85 | /// which the stack pointer must be aligned at all times, even between |
| 86 | /// calls. |
| 87 | /// |
| 88 | unsigned getTransientStackAlignment() const { |
| 89 | return TransientStackAlignment; |
| 90 | } |
| 91 | |
| 92 | /// isStackRealignable - This method returns whether the stack can be |
| 93 | /// realigned. |
| 94 | bool isStackRealignable() const { |
| 95 | return StackRealignable; |
| 96 | } |
| 97 | |
| 98 | /// Return the skew that has to be applied to stack alignment under |
| 99 | /// certain conditions (e.g. stack was adjusted before function \p MF |
| 100 | /// was called). |
| 101 | virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const; |
| 102 | |
| 103 | /// getOffsetOfLocalArea - This method returns the offset of the local area |
| 104 | /// from the stack pointer on entrance to a function. |
| 105 | /// |
| 106 | int getOffsetOfLocalArea() const { return LocalAreaOffset; } |
| 107 | |
| 108 | /// isFPCloseToIncomingSP - Return true if the frame pointer is close to |
| 109 | /// the incoming stack pointer, false if it is close to the post-prologue |
| 110 | /// stack pointer. |
| 111 | virtual bool isFPCloseToIncomingSP() const { return true; } |
| 112 | |
| 113 | /// assignCalleeSavedSpillSlots - Allows target to override spill slot |
| 114 | /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should |
| 115 | /// assign frame slots to all CSI entries and return true. If this method |
| 116 | /// returns false, spill slots will be assigned using generic implementation. |
| 117 | /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of |
| 118 | /// CSI. |
| 119 | virtual bool |
| 120 | assignCalleeSavedSpillSlots(MachineFunction &MF, |
| 121 | const TargetRegisterInfo *TRI, |
| 122 | std::vector<CalleeSavedInfo> &CSI) const { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | /// getCalleeSavedSpillSlots - This method returns a pointer to an array of |
| 127 | /// pairs, that contains an entry for each callee saved register that must be |
| 128 | /// spilled to a particular stack location if it is spilled. |
| 129 | /// |
| 130 | /// Each entry in this array contains a <register,offset> pair, indicating the |
| 131 | /// fixed offset from the incoming stack pointer that each register should be |
| 132 | /// spilled at. If a register is not listed here, the code generator is |
| 133 | /// allowed to spill it anywhere it chooses. |
| 134 | /// |
| 135 | virtual const SpillSlot * |
| 136 | getCalleeSavedSpillSlots(unsigned &NumEntries) const { |
| 137 | NumEntries = 0; |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
| 141 | /// targetHandlesStackFrameRounding - Returns true if the target is |
| 142 | /// responsible for rounding up the stack frame (probably at emitPrologue |
| 143 | /// time). |
| 144 | virtual bool targetHandlesStackFrameRounding() const { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | /// Returns true if the target will correctly handle shrink wrapping. |
| 149 | virtual bool enableShrinkWrapping(const MachineFunction &MF) const { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | /// Returns true if the stack slot holes in the fixed and callee-save stack |
| 154 | /// area should be used when allocating other stack locations to reduce stack |
| 155 | /// size. |
| 156 | virtual bool enableStackSlotScavenging(const MachineFunction &MF) const { |
| 157 | return false; |
| 158 | } |
| 159 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 160 | /// Returns true if the target can safely skip saving callee-saved registers |
| 161 | /// for noreturn nounwind functions. |
| 162 | virtual bool enableCalleeSaveSkip(const MachineFunction &MF) const; |
| 163 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 164 | /// emitProlog/emitEpilog - These methods insert prolog and epilog code into |
| 165 | /// the function. |
| 166 | virtual void emitPrologue(MachineFunction &MF, |
| 167 | MachineBasicBlock &MBB) const = 0; |
| 168 | virtual void emitEpilogue(MachineFunction &MF, |
| 169 | MachineBasicBlock &MBB) const = 0; |
| 170 | |
| 171 | /// Replace a StackProbe stub (if any) with the actual probe code inline |
| 172 | virtual void inlineStackProbe(MachineFunction &MF, |
| 173 | MachineBasicBlock &PrologueMBB) const {} |
| 174 | |
| 175 | /// Adjust the prologue to have the function use segmented stacks. This works |
| 176 | /// by adding a check even before the "normal" function prologue. |
| 177 | virtual void adjustForSegmentedStacks(MachineFunction &MF, |
| 178 | MachineBasicBlock &PrologueMBB) const {} |
| 179 | |
| 180 | /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in |
| 181 | /// the assembly prologue to explicitly handle the stack. |
| 182 | virtual void adjustForHiPEPrologue(MachineFunction &MF, |
| 183 | MachineBasicBlock &PrologueMBB) const {} |
| 184 | |
| 185 | /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee |
| 186 | /// saved registers and returns true if it isn't possible / profitable to do |
| 187 | /// so by issuing a series of store instructions via |
| 188 | /// storeRegToStackSlot(). Returns false otherwise. |
| 189 | virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 190 | MachineBasicBlock::iterator MI, |
| 191 | const std::vector<CalleeSavedInfo> &CSI, |
| 192 | const TargetRegisterInfo *TRI) const { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee |
| 197 | /// saved registers and returns true if it isn't possible / profitable to do |
| 198 | /// so by issuing a series of load instructions via loadRegToStackSlot(). |
| 199 | /// If it returns true, and any of the registers in CSI is not restored, |
| 200 | /// it sets the corresponding Restored flag in CSI to false. |
| 201 | /// Returns false otherwise. |
| 202 | virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 203 | MachineBasicBlock::iterator MI, |
| 204 | std::vector<CalleeSavedInfo> &CSI, |
| 205 | const TargetRegisterInfo *TRI) const { |
| 206 | return false; |
| 207 | } |
| 208 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 209 | /// Return true if the target wants to keep the frame pointer regardless of |
| 210 | /// the function attribute "frame-pointer". |
| 211 | virtual bool keepFramePointer(const MachineFunction &MF) const { |
| 212 | return false; |
| 213 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 214 | |
| 215 | /// hasFP - Return true if the specified function should have a dedicated |
| 216 | /// frame pointer register. For most targets this is true only if the function |
| 217 | /// has variable sized allocas or if frame pointer elimination is disabled. |
| 218 | virtual bool hasFP(const MachineFunction &MF) const = 0; |
| 219 | |
| 220 | /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is |
| 221 | /// not required, we reserve argument space for call sites in the function |
| 222 | /// immediately on entry to the current function. This eliminates the need for |
| 223 | /// add/sub sp brackets around call sites. Returns true if the call frame is |
| 224 | /// included as part of the stack frame. |
| 225 | virtual bool hasReservedCallFrame(const MachineFunction &MF) const { |
| 226 | return !hasFP(MF); |
| 227 | } |
| 228 | |
| 229 | /// canSimplifyCallFramePseudos - When possible, it's best to simplify the |
| 230 | /// call frame pseudo ops before doing frame index elimination. This is |
| 231 | /// possible only when frame index references between the pseudos won't |
| 232 | /// need adjusting for the call frame adjustments. Normally, that's true |
| 233 | /// if the function has a reserved call frame or a frame pointer. Some |
| 234 | /// targets (Thumb2, for example) may have more complicated criteria, |
| 235 | /// however, and can override this behavior. |
| 236 | virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const { |
| 237 | return hasReservedCallFrame(MF) || hasFP(MF); |
| 238 | } |
| 239 | |
| 240 | // needsFrameIndexResolution - Do we need to perform FI resolution for |
| 241 | // this function. Normally, this is required only when the function |
| 242 | // has any stack objects. However, targets may want to override this. |
| 243 | virtual bool needsFrameIndexResolution(const MachineFunction &MF) const; |
| 244 | |
| 245 | /// getFrameIndexReference - This method should return the base register |
| 246 | /// and offset used to reference a frame index location. The offset is |
| 247 | /// returned directly, and the base register is returned via FrameReg. |
| 248 | virtual int getFrameIndexReference(const MachineFunction &MF, int FI, |
| 249 | unsigned &FrameReg) const; |
| 250 | |
| 251 | /// Same as \c getFrameIndexReference, except that the stack pointer (as |
| 252 | /// opposed to the frame pointer) will be the preferred value for \p |
| 253 | /// FrameReg. This is generally used for emitting statepoint or EH tables that |
| 254 | /// use offsets from RSP. If \p IgnoreSPUpdates is true, the returned |
| 255 | /// offset is only guaranteed to be valid with respect to the value of SP at |
| 256 | /// the end of the prologue. |
| 257 | virtual int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, |
| 258 | unsigned &FrameReg, |
| 259 | bool IgnoreSPUpdates) const { |
| 260 | // Always safe to dispatch to getFrameIndexReference. |
| 261 | return getFrameIndexReference(MF, FI, FrameReg); |
| 262 | } |
| 263 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 264 | /// getNonLocalFrameIndexReference - This method returns the offset used to |
| 265 | /// reference a frame index location. The offset can be from either FP/BP/SP |
| 266 | /// based on which base register is returned by llvm.localaddress. |
| 267 | virtual int getNonLocalFrameIndexReference(const MachineFunction &MF, |
| 268 | int FI) const { |
| 269 | // By default, dispatch to getFrameIndexReference. Interested targets can |
| 270 | // override this. |
| 271 | unsigned FrameReg; |
| 272 | return getFrameIndexReference(MF, FI, FrameReg); |
| 273 | } |
| 274 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 275 | /// This method determines which of the registers reported by |
| 276 | /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved. |
| 277 | /// The default implementation checks populates the \p SavedRegs bitset with |
| 278 | /// all registers which are modified in the function, targets may override |
| 279 | /// this function to save additional registers. |
| 280 | /// This method also sets up the register scavenger ensuring there is a free |
| 281 | /// register or a frameindex available. |
| 282 | virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, |
| 283 | RegScavenger *RS = nullptr) const; |
| 284 | |
| 285 | /// processFunctionBeforeFrameFinalized - This method is called immediately |
| 286 | /// before the specified function's frame layout (MF.getFrameInfo()) is |
| 287 | /// finalized. Once the frame is finalized, MO_FrameIndex operands are |
| 288 | /// replaced with direct constants. This method is optional. |
| 289 | /// |
| 290 | virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF, |
| 291 | RegScavenger *RS = nullptr) const { |
| 292 | } |
| 293 | |
| 294 | virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const { |
| 295 | report_fatal_error("WinEH not implemented for this target"); |
| 296 | } |
| 297 | |
| 298 | /// This method is called during prolog/epilog code insertion to eliminate |
| 299 | /// call frame setup and destroy pseudo instructions (but only if the Target |
| 300 | /// is using them). It is responsible for eliminating these instructions, |
| 301 | /// replacing them with concrete instructions. This method need only be |
| 302 | /// implemented if using call frame setup/destroy pseudo instructions. |
| 303 | /// Returns an iterator pointing to the instruction after the replaced one. |
| 304 | virtual MachineBasicBlock::iterator |
| 305 | eliminateCallFramePseudoInstr(MachineFunction &MF, |
| 306 | MachineBasicBlock &MBB, |
| 307 | MachineBasicBlock::iterator MI) const { |
| 308 | llvm_unreachable("Call Frame Pseudo Instructions do not exist on this " |
| 309 | "target!"); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /// Order the symbols in the local stack frame. |
| 314 | /// The list of objects that we want to order is in \p objectsToAllocate as |
| 315 | /// indices into the MachineFrameInfo. The array can be reordered in any way |
| 316 | /// upon return. The contents of the array, however, may not be modified (i.e. |
| 317 | /// only their order may be changed). |
| 318 | /// By default, just maintain the original order. |
| 319 | virtual void |
| 320 | orderFrameObjects(const MachineFunction &MF, |
| 321 | SmallVectorImpl<int> &objectsToAllocate) const { |
| 322 | } |
| 323 | |
| 324 | /// Check whether or not the given \p MBB can be used as a prologue |
| 325 | /// for the target. |
| 326 | /// The prologue will be inserted first in this basic block. |
| 327 | /// This method is used by the shrink-wrapping pass to decide if |
| 328 | /// \p MBB will be correctly handled by the target. |
| 329 | /// As soon as the target enable shrink-wrapping without overriding |
| 330 | /// this method, we assume that each basic block is a valid |
| 331 | /// prologue. |
| 332 | virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const { |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | /// Check whether or not the given \p MBB can be used as a epilogue |
| 337 | /// for the target. |
| 338 | /// The epilogue will be inserted before the first terminator of that block. |
| 339 | /// This method is used by the shrink-wrapping pass to decide if |
| 340 | /// \p MBB will be correctly handled by the target. |
| 341 | /// As soon as the target enable shrink-wrapping without overriding |
| 342 | /// this method, we assume that each basic block is a valid |
| 343 | /// epilogue. |
| 344 | virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const { |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | /// Check if given function is safe for not having callee saved registers. |
| 349 | /// This is used when interprocedural register allocation is enabled. |
| 350 | static bool isSafeForNoCSROpt(const Function &F) { |
| 351 | if (!F.hasLocalLinkage() || F.hasAddressTaken() || |
| 352 | !F.hasFnAttribute(Attribute::NoRecurse)) |
| 353 | return false; |
| 354 | // Function should not be optimized as tail call. |
| 355 | for (const User *U : F.users()) |
| 356 | if (auto CS = ImmutableCallSite(U)) |
| 357 | if (CS.isTailCall()) |
| 358 | return false; |
| 359 | return true; |
| 360 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 361 | |
| 362 | /// Return initial CFA offset value i.e. the one valid at the beginning of the |
| 363 | /// function (before any stack operations). |
| 364 | virtual int getInitialCFAOffset(const MachineFunction &MF) const; |
| 365 | |
| 366 | /// Return initial CFA register value i.e. the one valid at the beginning of |
| 367 | /// the function (before any stack operations). |
| 368 | virtual unsigned getInitialCFARegister(const MachineFunction &MF) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | } // End llvm namespace |
| 372 | |
| 373 | #endif |