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 /// getOffsetOfLocalArea - This method returns the offset of the local area
100 /// from the stack pointer on entrance to a function.
102 int getOffsetOfLocalArea() const { return LocalAreaOffset; }
104 /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
105 /// the incoming stack pointer, false if it is close to the post-prologue
107 virtual bool isFPCloseToIncomingSP() const { return true; }
109 /// assignCalleeSavedSpillSlots - Allows target to override spill slot
110 /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
111 /// assign frame slots to all CSI entries and return true. If this method
112 /// returns false, spill slots will be assigned using generic implementation.
113 /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
116 assignCalleeSavedSpillSlots(MachineFunction &MF,
117 const TargetRegisterInfo *TRI,
118 std::vector<CalleeSavedInfo> &CSI) const {
122 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
123 /// pairs, that contains an entry for each callee saved register that must be
124 /// spilled to a particular stack location if it is spilled.
126 /// Each entry in this array contains a <register,offset> pair, indicating the
127 /// fixed offset from the incoming stack pointer that each register should be
128 /// spilled at. If a register is not listed here, the code generator is
129 /// allowed to spill it anywhere it chooses.
131 virtual const SpillSlot *
132 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
137 /// targetHandlesStackFrameRounding - Returns true if the target is
138 /// responsible for rounding up the stack frame (probably at emitPrologue
140 virtual bool targetHandlesStackFrameRounding() const {
144 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
146 virtual void emitPrologue(MachineFunction &MF,
147 MachineBasicBlock &MBB) const = 0;
148 virtual void emitEpilogue(MachineFunction &MF,
149 MachineBasicBlock &MBB) const = 0;
151 /// Adjust the prologue to have the function use segmented stacks. This works
152 /// by adding a check even before the "normal" function prologue.
153 virtual void adjustForSegmentedStacks(MachineFunction &MF,
154 MachineBasicBlock &PrologueMBB) const {}
156 /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
157 /// the assembly prologue to explicitly handle the stack.
158 virtual void adjustForHiPEPrologue(MachineFunction &MF,
159 MachineBasicBlock &PrologueMBB) const {}
161 /// Adjust the prologue to add an allocation at a fixed offset from the frame
164 adjustForFrameAllocatePrologue(MachineFunction &MF,
165 MachineBasicBlock &PrologueMBB) const {}
167 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
168 /// saved registers and returns true if it isn't possible / profitable to do
169 /// so by issuing a series of store instructions via
170 /// storeRegToStackSlot(). Returns false otherwise.
171 virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
172 MachineBasicBlock::iterator MI,
173 const std::vector<CalleeSavedInfo> &CSI,
174 const TargetRegisterInfo *TRI) const {
178 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
179 /// saved registers and returns true if it isn't possible / profitable to do
180 /// so by issuing a series of load instructions via loadRegToStackSlot().
181 /// Returns false otherwise.
182 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
183 MachineBasicBlock::iterator MI,
184 const std::vector<CalleeSavedInfo> &CSI,
185 const TargetRegisterInfo *TRI) const {
189 /// Return true if the target needs to disable frame pointer elimination.
190 virtual bool noFramePointerElim(const MachineFunction &MF) const;
192 /// hasFP - Return true if the specified function should have a dedicated
193 /// frame pointer register. For most targets this is true only if the function
194 /// has variable sized allocas or if frame pointer elimination is disabled.
195 virtual bool hasFP(const MachineFunction &MF) const = 0;
197 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
198 /// not required, we reserve argument space for call sites in the function
199 /// immediately on entry to the current function. This eliminates the need for
200 /// add/sub sp brackets around call sites. Returns true if the call frame is
201 /// included as part of the stack frame.
202 virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
206 /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
207 /// call frame pseudo ops before doing frame index elimination. This is
208 /// possible only when frame index references between the pseudos won't
209 /// need adjusting for the call frame adjustments. Normally, that's true
210 /// if the function has a reserved call frame or a frame pointer. Some
211 /// targets (Thumb2, for example) may have more complicated criteria,
212 /// however, and can override this behavior.
213 virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
214 return hasReservedCallFrame(MF) || hasFP(MF);
217 // needsFrameIndexResolution - Do we need to perform FI resolution for
218 // this function. Normally, this is required only when the function
219 // has any stack objects. However, targets may want to override this.
220 virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
222 /// getFrameIndexReference - This method should return the base register
223 /// and offset used to reference a frame index location. The offset is
224 /// returned directly, and the base register is returned via FrameReg.
225 virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
226 unsigned &FrameReg) const;
228 /// Same as above, except that the 'base register' will always be RSP, not
229 /// RBP on x86. This is used exclusively for lowering STATEPOINT nodes.
230 /// TODO: This should really be a parameterizable choice.
231 virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
232 unsigned &FrameReg) const {
233 // default to calling normal version, we override this on x86 only
234 llvm_unreachable("unimplemented for non-x86");
238 /// This method determines which of the registers reported by
239 /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
240 /// The default implementation checks populates the \p SavedRegs bitset with
241 /// all registers which are modified in the function, targets may override
242 /// this function to save additional registers.
243 /// This method also sets up the register scavenger ensuring there is a free
244 /// register or a frameindex available.
245 virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
246 RegScavenger *RS = nullptr) const;
248 /// processFunctionBeforeFrameFinalized - This method is called immediately
249 /// before the specified function's frame layout (MF.getFrameInfo()) is
250 /// finalized. Once the frame is finalized, MO_FrameIndex operands are
251 /// replaced with direct constants. This method is optional.
253 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
254 RegScavenger *RS = nullptr) const {
257 /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
258 /// code insertion to eliminate call frame setup and destroy pseudo
259 /// instructions (but only if the Target is using them). It is responsible
260 /// for eliminating these instructions, replacing them with concrete
261 /// instructions. This method need only be implemented if using call frame
262 /// setup/destroy pseudo instructions.
265 eliminateCallFramePseudoInstr(MachineFunction &MF,
266 MachineBasicBlock &MBB,
267 MachineBasicBlock::iterator MI) const {
268 llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
272 /// Check whether or not the given \p MBB can be used as a prologue
274 /// The prologue will be inserted first in this basic block.
275 /// This method is used by the shrink-wrapping pass to decide if
276 /// \p MBB will be correctly handled by the target.
277 /// As soon as the target enable shrink-wrapping without overriding
278 /// this method, we assume that each basic block is a valid
280 virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
284 /// Check whether or not the given \p MBB can be used as a epilogue
286 /// The epilogue will be inserted before the first terminator of that block.
287 /// This method is used by the shrink-wrapping pass to decide if
288 /// \p MBB will be correctly handled by the target.
289 /// As soon as the target enable shrink-wrapping without overriding
290 /// this method, we assume that each basic block is a valid
292 virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
297 } // End llvm namespace