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/MC/MCDwarf.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
24 class CalleeSavedInfo;
25 class MachineFunction;
26 class MachineBasicBlock;
30 /// Information about stack frame layout on the target. It holds the direction
31 /// of stack growth, the known stack alignment on entry to each function, and
32 /// the offset to the locals area.
34 /// The offset to the local area is the offset from the stack pointer on
35 /// function entry to the first location where function data (local variables,
36 /// spill locations) can be stored.
37 class TargetFrameLowering {
40 StackGrowsUp, // Adding to the stack increases the stack address
41 StackGrowsDown // Adding to the stack decreases the stack address
44 // Maps a callee saved register to a stack slot with a fixed offset.
47 int Offset; // Offset relative to stack pointer on function entry.
50 StackDirection StackDir;
51 unsigned StackAlignment;
52 unsigned TransientStackAlignment;
55 TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
57 : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
58 LocalAreaOffset(LAO) {}
60 virtual ~TargetFrameLowering();
62 // These methods return information that describes the abstract stack layout
63 // of the target machine.
65 /// getStackGrowthDirection - Return the direction the stack grows
67 StackDirection getStackGrowthDirection() const { return StackDir; }
69 /// getStackAlignment - This method returns the number of bytes to which the
70 /// stack pointer must be aligned on entry to a function. Typically, this
71 /// is the largest alignment for any data object in the target.
73 unsigned getStackAlignment() const { return StackAlignment; }
75 /// getTransientStackAlignment - This method returns the number of bytes to
76 /// which the stack pointer must be aligned at all times, even between
79 unsigned getTransientStackAlignment() const {
80 return TransientStackAlignment;
83 /// getOffsetOfLocalArea - This method returns the offset of the local area
84 /// from the stack pointer on entrance to a function.
86 int getOffsetOfLocalArea() const { return LocalAreaOffset; }
88 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
89 /// pairs, that contains an entry for each callee saved register that must be
90 /// spilled to a particular stack location if it is spilled.
92 /// Each entry in this array contains a <register,offset> pair, indicating the
93 /// fixed offset from the incoming stack pointer that each register should be
94 /// spilled at. If a register is not listed here, the code generator is
95 /// allowed to spill it anywhere it chooses.
97 virtual const SpillSlot *
98 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
103 /// targetHandlesStackFrameRounding - Returns true if the target is
104 /// responsible for rounding up the stack frame (probably at emitPrologue
106 virtual bool targetHandlesStackFrameRounding() const {
110 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
112 virtual void emitPrologue(MachineFunction &MF) const = 0;
113 virtual void emitEpilogue(MachineFunction &MF,
114 MachineBasicBlock &MBB) const = 0;
116 /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
117 /// saved registers and returns true if it isn't possible / profitable to do
118 /// so by issuing a series of store instructions via
119 /// storeRegToStackSlot(). Returns false otherwise.
120 virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
121 MachineBasicBlock::iterator MI,
122 const std::vector<CalleeSavedInfo> &CSI,
123 const TargetRegisterInfo *TRI) const {
127 /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
128 /// saved registers and returns true if it isn't possible / profitable to do
129 /// so by issuing a series of load instructions via loadRegToStackSlot().
130 /// Returns false otherwise.
131 virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
132 MachineBasicBlock::iterator MI,
133 const std::vector<CalleeSavedInfo> &CSI,
134 const TargetRegisterInfo *TRI) const {
138 /// hasFP - Return true if the specified function should have a dedicated
139 /// frame pointer register. For most targets this is true only if the function
140 /// has variable sized allocas or if frame pointer elimination is disabled.
141 virtual bool hasFP(const MachineFunction &MF) const = 0;
143 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
144 /// not required, we reserve argument space for call sites in the function
145 /// immediately on entry to the current function. This eliminates the need for
146 /// add/sub sp brackets around call sites. Returns true if the call frame is
147 /// included as part of the stack frame.
148 virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
152 /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
153 /// call frame pseudo ops before doing frame index elimination. This is
154 /// possible only when frame index references between the pseudos won't
155 /// need adjusting for the call frame adjustments. Normally, that's true
156 /// if the function has a reserved call frame or a frame pointer. Some
157 /// targets (Thumb2, for example) may have more complicated criteria,
158 /// however, and can override this behavior.
159 virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
160 return hasReservedCallFrame(MF) || hasFP(MF);
163 /// getInitialFrameState - Returns a list of machine moves that are assumed
164 /// on entry to all functions. Note that LabelID is ignored (assumed to be
165 /// the beginning of the function.)
166 virtual void getInitialFrameState(std::vector<MachineMove> &Moves) const;
168 /// getFrameIndexOffset - Returns the displacement from the frame register to
169 /// the stack frame of the specified index.
170 virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
172 /// getFrameIndexReference - This method should return the base register
173 /// and offset used to reference a frame index location. The offset is
174 /// returned directly, and the base register is returned via FrameReg.
175 virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
176 unsigned &FrameReg) const;
178 /// processFunctionBeforeCalleeSavedScan - This method is called immediately
179 /// before PrologEpilogInserter scans the physical registers used to determine
180 /// what callee saved registers should be spilled. This method is optional.
181 virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
182 RegScavenger *RS = NULL) const {
186 /// processFunctionBeforeFrameFinalized - This method is called immediately
187 /// before the specified function's frame layout (MF.getFrameInfo()) is
188 /// finalized. Once the frame is finalized, MO_FrameIndex operands are
189 /// replaced with direct constants. This method is optional.
191 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
194 /// getCompactUnwindEncoding - Get the compact unwind encoding for the
195 /// function. Return 0 if the compact unwind isn't available.
196 virtual uint32_t getCompactUnwindEncoding(const std::vector<MCCFIInstruction>&,
197 int /*DataAlignmentFactor*/,
198 bool /*IsEH*/) const {
203 } // End llvm namespace