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