1 //===-- llvm/Target/TargetFrameLowering.h ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Interface to describe the layout of a stack frame on the target machine.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TARGET_TARGETFRAMELOWERING_H
15 #define LLVM_TARGET_TARGETFRAMELOWERING_H
17 #include "llvm/CodeGen/MachineBasicBlock.h"
23 class CalleeSavedInfo;
24 class MachineFunction;
27 /// Information about stack frame layout on the target. It holds the direction
28 /// of stack growth, the known stack alignment on entry to each function, and
29 /// the offset to the locals area.
31 /// The offset to the local area is the offset from the stack pointer on
32 /// function entry to the first location where function data (local variables,
33 /// spill locations) can be stored.
34 class TargetFrameLowering {
37 StackGrowsUp, // Adding to the stack increases the stack address
38 StackGrowsDown // Adding to the stack decreases the stack address
41 // Maps a callee saved register to a stack slot with a fixed offset.
44 int Offset; // Offset relative to stack pointer on function entry.
47 StackDirection StackDir;
48 unsigned StackAlignment;
49 unsigned TransientStackAlignment;
51 bool StackRealignable;
53 TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
54 unsigned TransAl = 1, bool StackReal = true)
55 : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
56 LocalAreaOffset(LAO), StackRealignable(StackReal) {}
58 virtual ~TargetFrameLowering();
60 // These methods return information that describes the abstract stack layout
61 // of the target machine.
63 /// getStackGrowthDirection - Return the direction the stack grows
65 StackDirection getStackGrowthDirection() const { return StackDir; }
67 /// getStackAlignment - This method returns the number of bytes to which the
68 /// stack pointer must be aligned on entry to a function. Typically, this
69 /// is the largest alignment for any data object in the target.
71 unsigned getStackAlignment() const { return StackAlignment; }
73 /// alignSPAdjust - This method aligns the stack adjustment to the correct
76 int alignSPAdjust(int SPAdj) const {
78 SPAdj = -RoundUpToAlignment(-SPAdj, StackAlignment);
80 SPAdj = RoundUpToAlignment(SPAdj, StackAlignment);
85 /// getTransientStackAlignment - This method returns the number of bytes to
86 /// which the stack pointer must be aligned at all times, even between
89 unsigned getTransientStackAlignment() const {
90 return TransientStackAlignment;
93 /// isStackRealignable - This method returns whether the stack can be
95 bool isStackRealignable() const {
96 return StackRealignable;
99 /// Return the skew that has to be applied to stack alignment under
100 /// certain conditions (e.g. stack was adjusted before function \p MF
102 virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const;
104 /// getOffsetOfLocalArea - This method returns the offset of the local area
105 /// from the stack pointer on entrance to a function.
107 int getOffsetOfLocalArea() const { return LocalAreaOffset; }
109 /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
110 /// the incoming stack pointer, false if it is close to the post-prologue
112 virtual bool isFPCloseToIncomingSP() const { return true; }
114 /// assignCalleeSavedSpillSlots - Allows target to override spill slot
115 /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
116 /// assign frame slots to all CSI entries and return true. If this method
117 /// returns false, spill slots will be assigned using generic implementation.
118 /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
121 assignCalleeSavedSpillSlots(MachineFunction &MF,
122 const TargetRegisterInfo *TRI,
123 std::vector<CalleeSavedInfo> &CSI) const {
127 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
128 /// pairs, that contains an entry for each callee saved register that must be
129 /// spilled to a particular stack location if it is spilled.
131 /// Each entry in this array contains a <register,offset> pair, indicating the
132 /// fixed offset from the incoming stack pointer that each register should be
133 /// spilled at. If a register is not listed here, the code generator is
134 /// allowed to spill it anywhere it chooses.
136 virtual const SpillSlot *
137 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
142 /// targetHandlesStackFrameRounding - Returns true if the target is
143 /// responsible for rounding up the stack frame (probably at emitPrologue
145 virtual bool targetHandlesStackFrameRounding() const {
149 /// Returns true if the target will correctly handle shrink wrapping.
150 virtual bool enableShrinkWrapping(const MachineFunction &MF) const {
154 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
156 virtual void emitPrologue(MachineFunction &MF,
157 MachineBasicBlock &MBB) const = 0;
158 virtual void emitEpilogue(MachineFunction &MF,
159 MachineBasicBlock &MBB) const = 0;
161 /// Replace a StackProbe stub (if any) with the actual probe code inline
162 virtual void inlineStackProbe(MachineFunction &MF,
163 MachineBasicBlock &PrologueMBB) const {}
165 /// Adjust the prologue to have the function use segmented stacks. This works
166 /// by adding a check even before the "normal" function prologue.
167 virtual void adjustForSegmentedStacks(MachineFunction &MF,
168 MachineBasicBlock &PrologueMBB) const {}
170 /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
171 /// the assembly prologue to explicitly handle the stack.
172 virtual void adjustForHiPEPrologue(MachineFunction &MF,
173 MachineBasicBlock &PrologueMBB) const {}
175 /// Adjust the prologue to add an allocation at a fixed offset from the frame
178 adjustForFrameAllocatePrologue(MachineFunction &MF,
179 MachineBasicBlock &PrologueMBB) const {}
181 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
182 /// saved registers and returns true if it isn't possible / profitable to do
183 /// so by issuing a series of store instructions via
184 /// storeRegToStackSlot(). Returns false otherwise.
185 virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
186 MachineBasicBlock::iterator MI,
187 const std::vector<CalleeSavedInfo> &CSI,
188 const TargetRegisterInfo *TRI) const {
192 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
193 /// saved registers and returns true if it isn't possible / profitable to do
194 /// so by issuing a series of load instructions via loadRegToStackSlot().
195 /// Returns false otherwise.
196 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
197 MachineBasicBlock::iterator MI,
198 const std::vector<CalleeSavedInfo> &CSI,
199 const TargetRegisterInfo *TRI) const {
203 /// Return true if the target needs to disable frame pointer elimination.
204 virtual bool noFramePointerElim(const MachineFunction &MF) const;
206 /// hasFP - Return true if the specified function should have a dedicated
207 /// frame pointer register. For most targets this is true only if the function
208 /// has variable sized allocas or if frame pointer elimination is disabled.
209 virtual bool hasFP(const MachineFunction &MF) const = 0;
211 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
212 /// not required, we reserve argument space for call sites in the function
213 /// immediately on entry to the current function. This eliminates the need for
214 /// add/sub sp brackets around call sites. Returns true if the call frame is
215 /// included as part of the stack frame.
216 virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
220 /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
221 /// call frame pseudo ops before doing frame index elimination. This is
222 /// possible only when frame index references between the pseudos won't
223 /// need adjusting for the call frame adjustments. Normally, that's true
224 /// if the function has a reserved call frame or a frame pointer. Some
225 /// targets (Thumb2, for example) may have more complicated criteria,
226 /// however, and can override this behavior.
227 virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
228 return hasReservedCallFrame(MF) || hasFP(MF);
231 // needsFrameIndexResolution - Do we need to perform FI resolution for
232 // this function. Normally, this is required only when the function
233 // has any stack objects. However, targets may want to override this.
234 virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
236 /// getFrameIndexReference - This method should return the base register
237 /// and offset used to reference a frame index location. The offset is
238 /// returned directly, and the base register is returned via FrameReg.
239 virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
240 unsigned &FrameReg) const;
242 /// Same as above, except that the 'base register' will always be RSP, not
243 /// RBP on x86. This is generally used for emitting statepoint or EH tables
244 /// that use offsets from RSP.
245 /// TODO: This should really be a parameterizable choice.
246 virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
247 unsigned &FrameReg) const {
248 // default to calling normal version, we override this on x86 only
249 llvm_unreachable("unimplemented for non-x86");
253 /// This method determines which of the registers reported by
254 /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
255 /// The default implementation checks populates the \p SavedRegs bitset with
256 /// all registers which are modified in the function, targets may override
257 /// this function to save additional registers.
258 /// This method also sets up the register scavenger ensuring there is a free
259 /// register or a frameindex available.
260 virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
261 RegScavenger *RS = nullptr) const;
263 /// processFunctionBeforeFrameFinalized - This method is called immediately
264 /// before the specified function's frame layout (MF.getFrameInfo()) is
265 /// finalized. Once the frame is finalized, MO_FrameIndex operands are
266 /// replaced with direct constants. This method is optional.
268 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
269 RegScavenger *RS = nullptr) const {
272 virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const {
273 report_fatal_error("WinEH not implemented for this target");
276 /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
277 /// code insertion to eliminate call frame setup and destroy pseudo
278 /// instructions (but only if the Target is using them). It is responsible
279 /// for eliminating these instructions, replacing them with concrete
280 /// instructions. This method need only be implemented if using call frame
281 /// setup/destroy pseudo instructions.
284 eliminateCallFramePseudoInstr(MachineFunction &MF,
285 MachineBasicBlock &MBB,
286 MachineBasicBlock::iterator MI) const {
287 llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
291 /// Check whether or not the given \p MBB can be used as a prologue
293 /// The prologue will be inserted first in this basic block.
294 /// This method is used by the shrink-wrapping pass to decide if
295 /// \p MBB will be correctly handled by the target.
296 /// As soon as the target enable shrink-wrapping without overriding
297 /// this method, we assume that each basic block is a valid
299 virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
303 /// Check whether or not the given \p MBB can be used as a epilogue
305 /// The epilogue will be inserted before the first terminator of that block.
306 /// This method is used by the shrink-wrapping pass to decide if
307 /// \p MBB will be correctly handled by the target.
308 /// As soon as the target enable shrink-wrapping without overriding
309 /// this method, we assume that each basic block is a valid
311 virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
316 } // End llvm namespace