Line data Source code
1 : //===-- llvm/Target/TargetFrameLowering.h ---------------------------*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // Interface to describe the layout of a stack frame on the target machine.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #ifndef LLVM_TARGET_TARGETFRAMELOWERING_H
15 : #define LLVM_TARGET_TARGETFRAMELOWERING_H
16 :
17 : #include "llvm/CodeGen/MachineBasicBlock.h"
18 : #include <utility>
19 : #include <vector>
20 :
21 : namespace llvm {
22 : class BitVector;
23 : class CalleeSavedInfo;
24 : class MachineFunction;
25 : class RegScavenger;
26 :
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.
30 : ///
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 27997 : class TargetFrameLowering {
35 : public:
36 : enum StackDirection {
37 : StackGrowsUp, // Adding to the stack increases the stack address
38 : StackGrowsDown // Adding to the stack decreases the stack address
39 : };
40 :
41 : // Maps a callee saved register to a stack slot with a fixed offset.
42 : struct SpillSlot {
43 : unsigned Reg;
44 : int Offset; // Offset relative to stack pointer on function entry.
45 : };
46 : private:
47 : StackDirection StackDir;
48 : unsigned StackAlignment;
49 : unsigned TransientStackAlignment;
50 : int LocalAreaOffset;
51 : bool StackRealignable;
52 : public:
53 : TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
54 : unsigned TransAl = 1, bool StackReal = true)
55 28520 : : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
56 28520 : LocalAreaOffset(LAO), StackRealignable(StackReal) {}
57 :
58 : virtual ~TargetFrameLowering();
59 :
60 : // These methods return information that describes the abstract stack layout
61 : // of the target machine.
62 :
63 : /// getStackGrowthDirection - Return the direction the stack grows
64 : ///
65 : StackDirection getStackGrowthDirection() const { return StackDir; }
66 :
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.
70 : ///
71 : unsigned getStackAlignment() const { return StackAlignment; }
72 :
73 : /// alignSPAdjust - This method aligns the stack adjustment to the correct
74 : /// alignment.
75 : ///
76 : int alignSPAdjust(int SPAdj) const {
77 130 : if (SPAdj < 0) {
78 0 : SPAdj = -alignTo(-SPAdj, StackAlignment);
79 : } else {
80 260 : SPAdj = alignTo(SPAdj, StackAlignment);
81 : }
82 : return SPAdj;
83 : }
84 :
85 : /// getTransientStackAlignment - This method returns the number of bytes to
86 : /// which the stack pointer must be aligned at all times, even between
87 : /// calls.
88 : ///
89 : unsigned getTransientStackAlignment() const {
90 : return TransientStackAlignment;
91 : }
92 :
93 : /// isStackRealignable - This method returns whether the stack can be
94 : /// realigned.
95 : bool isStackRealignable() const {
96 : return StackRealignable;
97 : }
98 :
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
101 : /// was called).
102 : virtual unsigned getStackAlignmentSkew(const MachineFunction &MF) const;
103 :
104 : /// getOffsetOfLocalArea - This method returns the offset of the local area
105 : /// from the stack pointer on entrance to a function.
106 : ///
107 : int getOffsetOfLocalArea() const { return LocalAreaOffset; }
108 :
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
111 : /// stack pointer.
112 6960 : virtual bool isFPCloseToIncomingSP() const { return true; }
113 :
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
119 : /// CSI.
120 : virtual bool
121 68220 : assignCalleeSavedSpillSlots(MachineFunction &MF,
122 : const TargetRegisterInfo *TRI,
123 : std::vector<CalleeSavedInfo> &CSI) const {
124 68220 : return false;
125 : }
126 :
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.
130 : ///
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.
135 : ///
136 : virtual const SpillSlot *
137 7251 : getCalleeSavedSpillSlots(unsigned &NumEntries) const {
138 7251 : NumEntries = 0;
139 7251 : return nullptr;
140 : }
141 :
142 : /// targetHandlesStackFrameRounding - Returns true if the target is
143 : /// responsible for rounding up the stack frame (probably at emitPrologue
144 : /// time).
145 132316 : virtual bool targetHandlesStackFrameRounding() const {
146 132316 : return false;
147 : }
148 :
149 : /// Returns true if the target will correctly handle shrink wrapping.
150 38257 : virtual bool enableShrinkWrapping(const MachineFunction &MF) const {
151 38257 : return false;
152 : }
153 :
154 : /// Returns true if the stack slot holes in the fixed and callee-save stack
155 : /// area should be used when allocating other stack locations to reduce stack
156 : /// size.
157 11793 : virtual bool enableStackSlotScavenging(const MachineFunction &MF) const {
158 11793 : return false;
159 : }
160 :
161 : /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
162 : /// the function.
163 : virtual void emitPrologue(MachineFunction &MF,
164 : MachineBasicBlock &MBB) const = 0;
165 : virtual void emitEpilogue(MachineFunction &MF,
166 : MachineBasicBlock &MBB) const = 0;
167 :
168 : /// Replace a StackProbe stub (if any) with the actual probe code inline
169 70570 : virtual void inlineStackProbe(MachineFunction &MF,
170 70570 : MachineBasicBlock &PrologueMBB) const {}
171 :
172 : /// Adjust the prologue to have the function use segmented stacks. This works
173 : /// by adding a check even before the "normal" function prologue.
174 0 : virtual void adjustForSegmentedStacks(MachineFunction &MF,
175 0 : MachineBasicBlock &PrologueMBB) const {}
176 :
177 : /// Adjust the prologue to add Erlang Run-Time System (ERTS) specific code in
178 : /// the assembly prologue to explicitly handle the stack.
179 0 : virtual void adjustForHiPEPrologue(MachineFunction &MF,
180 0 : MachineBasicBlock &PrologueMBB) const {}
181 :
182 : /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
183 : /// saved registers and returns true if it isn't possible / profitable to do
184 : /// so by issuing a series of store instructions via
185 : /// storeRegToStackSlot(). Returns false otherwise.
186 105 : virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
187 : MachineBasicBlock::iterator MI,
188 : const std::vector<CalleeSavedInfo> &CSI,
189 : const TargetRegisterInfo *TRI) const {
190 105 : return false;
191 : }
192 :
193 : /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
194 : /// saved registers and returns true if it isn't possible / profitable to do
195 : /// so by issuing a series of load instructions via loadRegToStackSlot().
196 : /// If it returns true, and any of the registers in CSI is not restored,
197 : /// it sets the corresponding Restored flag in CSI to false.
198 : /// Returns false otherwise.
199 1997 : virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
200 : MachineBasicBlock::iterator MI,
201 : std::vector<CalleeSavedInfo> &CSI,
202 : const TargetRegisterInfo *TRI) const {
203 1997 : return false;
204 : }
205 :
206 : /// Return true if the target needs to disable frame pointer elimination.
207 : virtual bool noFramePointerElim(const MachineFunction &MF) const;
208 :
209 : /// hasFP - Return true if the specified function should have a dedicated
210 : /// frame pointer register. For most targets this is true only if the function
211 : /// has variable sized allocas or if frame pointer elimination is disabled.
212 : virtual bool hasFP(const MachineFunction &MF) const = 0;
213 :
214 : /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
215 : /// not required, we reserve argument space for call sites in the function
216 : /// immediately on entry to the current function. This eliminates the need for
217 : /// add/sub sp brackets around call sites. Returns true if the call frame is
218 : /// included as part of the stack frame.
219 5942 : virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
220 5942 : return !hasFP(MF);
221 : }
222 :
223 : /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
224 : /// call frame pseudo ops before doing frame index elimination. This is
225 : /// possible only when frame index references between the pseudos won't
226 : /// need adjusting for the call frame adjustments. Normally, that's true
227 : /// if the function has a reserved call frame or a frame pointer. Some
228 : /// targets (Thumb2, for example) may have more complicated criteria,
229 : /// however, and can override this behavior.
230 15074 : virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
231 15074 : return hasReservedCallFrame(MF) || hasFP(MF);
232 : }
233 :
234 : // needsFrameIndexResolution - Do we need to perform FI resolution for
235 : // this function. Normally, this is required only when the function
236 : // has any stack objects. However, targets may want to override this.
237 : virtual bool needsFrameIndexResolution(const MachineFunction &MF) const;
238 :
239 : /// getFrameIndexReference - This method should return the base register
240 : /// and offset used to reference a frame index location. The offset is
241 : /// returned directly, and the base register is returned via FrameReg.
242 : virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
243 : unsigned &FrameReg) const;
244 :
245 : /// Same as \c getFrameIndexReference, except that the stack pointer (as
246 : /// opposed to the frame pointer) will be the preferred value for \p
247 : /// FrameReg. This is generally used for emitting statepoint or EH tables that
248 : /// use offsets from RSP. If \p IgnoreSPUpdates is true, the returned
249 : /// offset is only guaranteed to be valid with respect to the value of SP at
250 : /// the end of the prologue.
251 0 : virtual int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
252 : unsigned &FrameReg,
253 : bool IgnoreSPUpdates) const {
254 : // Always safe to dispatch to getFrameIndexReference.
255 0 : return getFrameIndexReference(MF, FI, FrameReg);
256 : }
257 :
258 : /// This method determines which of the registers reported by
259 : /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
260 : /// The default implementation checks populates the \p SavedRegs bitset with
261 : /// all registers which are modified in the function, targets may override
262 : /// this function to save additional registers.
263 : /// This method also sets up the register scavenger ensuring there is a free
264 : /// register or a frameindex available.
265 : virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
266 : RegScavenger *RS = nullptr) const;
267 :
268 : /// processFunctionBeforeFrameFinalized - This method is called immediately
269 : /// before the specified function's frame layout (MF.getFrameInfo()) is
270 : /// finalized. Once the frame is finalized, MO_FrameIndex operands are
271 : /// replaced with direct constants. This method is optional.
272 : ///
273 38874 : virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF,
274 : RegScavenger *RS = nullptr) const {
275 38874 : }
276 :
277 0 : virtual unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const {
278 0 : report_fatal_error("WinEH not implemented for this target");
279 : }
280 :
281 : /// This method is called during prolog/epilog code insertion to eliminate
282 : /// call frame setup and destroy pseudo instructions (but only if the Target
283 : /// is using them). It is responsible for eliminating these instructions,
284 : /// replacing them with concrete instructions. This method need only be
285 : /// implemented if using call frame setup/destroy pseudo instructions.
286 : /// Returns an iterator pointing to the instruction after the replaced one.
287 : virtual MachineBasicBlock::iterator
288 0 : eliminateCallFramePseudoInstr(MachineFunction &MF,
289 : MachineBasicBlock &MBB,
290 : MachineBasicBlock::iterator MI) const {
291 0 : llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
292 : "target!");
293 : }
294 :
295 :
296 : /// Order the symbols in the local stack frame.
297 : /// The list of objects that we want to order is in \p objectsToAllocate as
298 : /// indices into the MachineFrameInfo. The array can be reordered in any way
299 : /// upon return. The contents of the array, however, may not be modified (i.e.
300 : /// only their order may be changed).
301 : /// By default, just maintain the original order.
302 : virtual void
303 65920 : orderFrameObjects(const MachineFunction &MF,
304 : SmallVectorImpl<int> &objectsToAllocate) const {
305 65920 : }
306 :
307 : /// Check whether or not the given \p MBB can be used as a prologue
308 : /// for the target.
309 : /// The prologue will be inserted first in this basic block.
310 : /// This method is used by the shrink-wrapping pass to decide if
311 : /// \p MBB will be correctly handled by the target.
312 : /// As soon as the target enable shrink-wrapping without overriding
313 : /// this method, we assume that each basic block is a valid
314 : /// prologue.
315 115 : virtual bool canUseAsPrologue(const MachineBasicBlock &MBB) const {
316 115 : return true;
317 : }
318 :
319 : /// Check whether or not the given \p MBB can be used as a epilogue
320 : /// for the target.
321 : /// The epilogue will be inserted before the first terminator of that block.
322 : /// This method is used by the shrink-wrapping pass to decide if
323 : /// \p MBB will be correctly handled by the target.
324 : /// As soon as the target enable shrink-wrapping without overriding
325 : /// this method, we assume that each basic block is a valid
326 : /// epilogue.
327 168 : virtual bool canUseAsEpilogue(const MachineBasicBlock &MBB) const {
328 168 : return true;
329 : }
330 :
331 : /// Check if given function is safe for not having callee saved registers.
332 : /// This is used when interprocedural register allocation is enabled.
333 30 : static bool isSafeForNoCSROpt(const Function *F) {
334 34 : if (!F->hasLocalLinkage() || F->hasAddressTaken() ||
335 2 : !F->hasFnAttribute(Attribute::NoRecurse))
336 : return false;
337 : // Function should not be optimized as tail call.
338 10 : for (const User *U : F->users())
339 4 : if (auto CS = ImmutableCallSite(U))
340 : if (CS.isTailCall())
341 : return false;
342 : return true;
343 : }
344 : };
345 :
346 : } // End llvm namespace
347 :
348 : #endif
|