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"
22 class CalleeSavedInfo;
23 class MachineFunction;
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.
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 {
36 StackGrowsUp, // Adding to the stack increases the stack address
37 StackGrowsDown // Adding to the stack decreases the stack address
40 // Maps a callee saved register to a stack slot with a fixed offset.
43 int Offset; // Offset relative to stack pointer on function entry.
46 StackDirection StackDir;
47 unsigned StackAlignment;
48 unsigned TransientStackAlignment;
50 bool StackRealignable;
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) {}
57 virtual ~TargetFrameLowering();
59 // These methods return information that describes the abstract stack layout
60 // of the target machine.
62 /// getStackGrowthDirection - Return the direction the stack grows
64 StackDirection getStackGrowthDirection() const { return StackDir; }
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.
70 unsigned getStackAlignment() const { return StackAlignment; }
72 /// getTransientStackAlignment - This method returns the number of bytes to
73 /// which the stack pointer must be aligned at all times, even between
76 unsigned getTransientStackAlignment() const {
77 return TransientStackAlignment;
80 /// isStackRealignable - This method returns whether the stack can be
82 bool isStackRealignable() const {
83 return StackRealignable;
86 /// getOffsetOfLocalArea - This method returns the offset of the local area
87 /// from the stack pointer on entrance to a function.
89 int getOffsetOfLocalArea() const { return LocalAreaOffset; }
91 /// isFPCloseToIncomingSP - Return true if the frame pointer is close to
92 /// the incoming stack pointer, false if it is close to the post-prologue
94 virtual bool isFPCloseToIncomingSP() const { return true; }
96 /// assignCalleeSavedSpillSlots - Allows target to override spill slot
97 /// assignment logic. If implemented, assignCalleeSavedSpillSlots() should
98 /// assign frame slots to all CSI entries and return true. If this method
99 /// returns false, spill slots will be assigned using generic implementation.
100 /// assignCalleeSavedSpillSlots() may add, delete or rearrange elements of
103 assignCalleeSavedSpillSlots(MachineFunction &MF,
104 const TargetRegisterInfo *TRI,
105 std::vector<CalleeSavedInfo> &CSI) const {
109 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
110 /// pairs, that contains an entry for each callee saved register that must be
111 /// spilled to a particular stack location if it is spilled.
113 /// Each entry in this array contains a <register,offset> pair, indicating the
114 /// fixed offset from the incoming stack pointer that each register should be
115 /// spilled at. If a register is not listed here, the code generator is
116 /// allowed to spill it anywhere it chooses.
118 virtual const SpillSlot *
119 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
124 /// targetHandlesStackFrameRounding - Returns true if the target is
125 /// responsible for rounding up the stack frame (probably at emitPrologue
127 virtual bool targetHandlesStackFrameRounding() const {
131 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
133 virtual void emitPrologue(MachineFunction &MF) const = 0;
134 virtual void emitEpilogue(MachineFunction &MF,
135 MachineBasicBlock &MBB) const = 0;
137 /// Adjust the prologue to have the function use segmented stacks. This works
138 /// by adding a check even before the "normal" function prologue.
139 virtual void adjustForSegmentedStacks(MachineFunction &MF) const { }
141 /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
142 /// the assembly prologue to explicitly handle the stack.
143 virtual void adjustForHiPEPrologue(MachineFunction &MF) const { }
145 /// Adjust the prologue to add an allocation at a fixed offset from the frame
147 virtual void adjustForFrameAllocatePrologue(MachineFunction &MF) const { }
149 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
150 /// saved registers and returns true if it isn't possible / profitable to do
151 /// so by issuing a series of store instructions via
152 /// storeRegToStackSlot(). Returns false otherwise.
153 virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
154 MachineBasicBlock::iterator MI,
155 const std::vector<CalleeSavedInfo> &CSI,
156 const TargetRegisterInfo *TRI) const {
160 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
161 /// saved registers and returns true if it isn't possible / profitable to do
162 /// so by issuing a series of load instructions via loadRegToStackSlot().
163 /// Returns false otherwise.
164 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
165 MachineBasicBlock::iterator MI,
166 const std::vector<CalleeSavedInfo> &CSI,
167 const TargetRegisterInfo *TRI) const {
171 /// hasFP - Return true if the specified function should have a dedicated
172 /// frame pointer register. For most targets this is true only if the function
173 /// has variable sized allocas or if frame pointer elimination is disabled.
174 virtual bool hasFP(const MachineFunction &MF) const = 0;
176 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
177 /// not required, we reserve argument space for call sites in the function
178 /// immediately on entry to the current function. This eliminates the need for
179 /// add/sub sp brackets around call sites. Returns true if the call frame is
180 /// included as part of the stack frame.
181 virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
185 /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
186 /// call frame pseudo ops before doing frame index elimination. This is
187 /// possible only when frame index references between the pseudos won't
188 /// need adjusting for the call frame adjustments. Normally, that's true
189 /// if the function has a reserved call frame or a frame pointer. Some
190 /// targets (Thumb2, for example) may have more complicated criteria,
191 /// however, and can override this behavior.
192 virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
193 return hasReservedCallFrame(MF) || hasFP(MF);
196 /// getFrameIndexOffset - Returns the displacement from the frame register to
197 /// the stack frame of the specified index.
198 virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
200 /// getFrameIndexReference - This method should return the base register
201 /// and offset used to reference a frame index location. The offset is
202 /// returned directly, and the base register is returned via FrameReg.
203 virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
204 unsigned &FrameReg) const;
206 /// Same as above, except that the 'base register' will always be RSP, not
207 /// RBP on x86. This is used exclusively for lowering STATEPOINT nodes.
208 /// TODO: This should really be a parameterizable choice.
209 virtual int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
210 unsigned &FrameReg) const {
211 // default to calling normal version, we override this on x86 only
212 llvm_unreachable("unimplemented for non-x86");
216 /// processFunctionBeforeCalleeSavedScan - This method is called immediately
217 /// before PrologEpilogInserter scans the physical registers used to determine
218 /// what callee saved registers should be spilled. This method is optional.
219 virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
220 RegScavenger *RS = nullptr) const {
224 /// processFunctionBeforeFrameFinalized - This method is called immediately
225 /// before the specified function's frame layout (MF.getFrameInfo()) is
226 /// finalized. Once the frame is finalized, MO_FrameIndex operands are
227 /// replaced with direct constants. This method is optional.
229 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
230 RegScavenger *RS = nullptr) const {
233 /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
234 /// code insertion to eliminate call frame setup and destroy pseudo
235 /// instructions (but only if the Target is using them). It is responsible
236 /// for eliminating these instructions, replacing them with concrete
237 /// instructions. This method need only be implemented if using call frame
238 /// setup/destroy pseudo instructions.
241 eliminateCallFramePseudoInstr(MachineFunction &MF,
242 MachineBasicBlock &MBB,
243 MachineBasicBlock::iterator MI) const {
244 llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
249 } // End llvm namespace