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;
51 TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
53 : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
54 LocalAreaOffset(LAO) {}
56 virtual ~TargetFrameLowering();
58 // These methods return information that describes the abstract stack layout
59 // of the target machine.
61 /// getStackGrowthDirection - Return the direction the stack grows
63 StackDirection getStackGrowthDirection() const { return StackDir; }
65 /// getStackAlignment - This method returns the number of bytes to which the
66 /// stack pointer must be aligned on entry to a function. Typically, this
67 /// is the largest alignment for any data object in the target.
69 unsigned getStackAlignment() const { return StackAlignment; }
71 /// getTransientStackAlignment - This method returns the number of bytes to
72 /// which the stack pointer must be aligned at all times, even between
75 unsigned getTransientStackAlignment() const {
76 return TransientStackAlignment;
79 /// getOffsetOfLocalArea - This method returns the offset of the local area
80 /// from the stack pointer on entrance to a function.
82 int getOffsetOfLocalArea() const { return LocalAreaOffset; }
84 /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
85 /// pairs, that contains an entry for each callee saved register that must be
86 /// spilled to a particular stack location if it is spilled.
88 /// Each entry in this array contains a <register,offset> pair, indicating the
89 /// fixed offset from the incoming stack pointer that each register should be
90 /// spilled at. If a register is not listed here, the code generator is
91 /// allowed to spill it anywhere it chooses.
93 virtual const SpillSlot *
94 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
99 /// targetHandlesStackFrameRounding - Returns true if the target is
100 /// responsible for rounding up the stack frame (probably at emitPrologue
102 virtual bool targetHandlesStackFrameRounding() const {
106 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
108 virtual void emitPrologue(MachineFunction &MF) const = 0;
109 virtual void emitEpilogue(MachineFunction &MF,
110 MachineBasicBlock &MBB) const = 0;
112 /// Adjust the prologue to have the function use segmented stacks. This works
113 /// by adding a check even before the "normal" function prologue.
114 virtual void adjustForSegmentedStacks(MachineFunction &MF) const { }
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 /// getFrameIndexOffset - Returns the displacement from the frame register to
164 /// the stack frame of the specified index.
165 virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
167 /// getFrameIndexReference - This method should return the base register
168 /// and offset used to reference a frame index location. The offset is
169 /// returned directly, and the base register is returned via FrameReg.
170 virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
171 unsigned &FrameReg) const;
173 /// processFunctionBeforeCalleeSavedScan - This method is called immediately
174 /// before PrologEpilogInserter scans the physical registers used to determine
175 /// what callee saved registers should be spilled. This method is optional.
176 virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
177 RegScavenger *RS = NULL) const {
181 /// processFunctionBeforeFrameFinalized - This method is called immediately
182 /// before the specified function's frame layout (MF.getFrameInfo()) is
183 /// finalized. Once the frame is finalized, MO_FrameIndex operands are
184 /// replaced with direct constants. This method is optional.
186 virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
190 } // End llvm namespace