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