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
19#include <utility>
20#include <vector>
21
22namespace llvm {
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.
34class TargetFrameLowering {
35public:
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  };
46private:
47  StackDirection StackDir;
48  unsigned StackAlignment;
49  unsigned TransientStackAlignment;
50  int LocalAreaOffset;
51public:
52  TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
53                      unsigned TransAl = 1)
54    : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
55      LocalAreaOffset(LAO) {}
56
57  virtual ~TargetFrameLowering();
58
59  // These methods return information that describes the abstract stack layout
60  // of the target machine.
61
62  /// getStackGrowthDirection - Return the direction the stack grows
63  ///
64  StackDirection getStackGrowthDirection() const { return StackDir; }
65
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.
69  ///
70  unsigned getStackAlignment() const { return StackAlignment; }
71
72  /// getTransientStackAlignment - This method returns the number of bytes to
73  /// which the stack pointer must be aligned at all times, even between
74  /// calls.
75  ///
76  unsigned getTransientStackAlignment() const {
77    return TransientStackAlignment;
78  }
79
80  /// getOffsetOfLocalArea - This method returns the offset of the local area
81  /// from the stack pointer on entrance to a function.
82  ///
83  int getOffsetOfLocalArea() const { return LocalAreaOffset; }
84
85  /// getCalleeSavedSpillSlots - This method returns a pointer to an array of
86  /// pairs, that contains an entry for each callee saved register that must be
87  /// spilled to a particular stack location if it is spilled.
88  ///
89  /// Each entry in this array contains a <register,offset> pair, indicating the
90  /// fixed offset from the incoming stack pointer that each register should be
91  /// spilled at. If a register is not listed here, the code generator is
92  /// allowed to spill it anywhere it chooses.
93  ///
94  virtual const SpillSlot *
95  getCalleeSavedSpillSlots(unsigned &NumEntries) const {
96    NumEntries = 0;
97    return 0;
98  }
99
100  /// targetHandlesStackFrameRounding - Returns true if the target is
101  /// responsible for rounding up the stack frame (probably at emitPrologue
102  /// time).
103  virtual bool targetHandlesStackFrameRounding() const {
104    return false;
105  }
106
107  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
108  /// the function.
109  virtual void emitPrologue(MachineFunction &MF) const = 0;
110  virtual void emitEpilogue(MachineFunction &MF,
111                            MachineBasicBlock &MBB) const = 0;
112
113  /// Adjust the prologue to have the function use segmented stacks. This works
114  /// by adding a check even before the "normal" function prologue.
115  virtual void adjustForSegmentedStacks(MachineFunction &MF) const { }
116
117  /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
118  /// saved registers and returns true if it isn't possible / profitable to do
119  /// so by issuing a series of store instructions via
120  /// storeRegToStackSlot(). Returns false otherwise.
121  virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
122                                         MachineBasicBlock::iterator MI,
123                                        const std::vector<CalleeSavedInfo> &CSI,
124                                         const TargetRegisterInfo *TRI) const {
125    return false;
126  }
127
128  /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
129  /// saved registers and returns true if it isn't possible / profitable to do
130  /// so by issuing a series of load instructions via loadRegToStackSlot().
131  /// Returns false otherwise.
132  virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
133                                           MachineBasicBlock::iterator MI,
134                                        const std::vector<CalleeSavedInfo> &CSI,
135                                        const TargetRegisterInfo *TRI) const {
136    return false;
137  }
138
139  /// hasFP - Return true if the specified function should have a dedicated
140  /// frame pointer register. For most targets this is true only if the function
141  /// has variable sized allocas or if frame pointer elimination is disabled.
142  virtual bool hasFP(const MachineFunction &MF) const = 0;
143
144  /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
145  /// not required, we reserve argument space for call sites in the function
146  /// immediately on entry to the current function. This eliminates the need for
147  /// add/sub sp brackets around call sites. Returns true if the call frame is
148  /// included as part of the stack frame.
149  virtual bool hasReservedCallFrame(const MachineFunction &MF) const {
150    return !hasFP(MF);
151  }
152
153  /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
154  /// call frame pseudo ops before doing frame index elimination. This is
155  /// possible only when frame index references between the pseudos won't
156  /// need adjusting for the call frame adjustments. Normally, that's true
157  /// if the function has a reserved call frame or a frame pointer. Some
158  /// targets (Thumb2, for example) may have more complicated criteria,
159  /// however, and can override this behavior.
160  virtual bool canSimplifyCallFramePseudos(const MachineFunction &MF) const {
161    return hasReservedCallFrame(MF) || hasFP(MF);
162  }
163
164  /// getFrameIndexOffset - Returns the displacement from the frame register to
165  /// the stack frame of the specified index.
166  virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
167
168  /// getFrameIndexReference - This method should return the base register
169  /// and offset used to reference a frame index location. The offset is
170  /// returned directly, and the base register is returned via FrameReg.
171  virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
172                                     unsigned &FrameReg) const;
173
174  /// processFunctionBeforeCalleeSavedScan - This method is called immediately
175  /// before PrologEpilogInserter scans the physical registers used to determine
176  /// what callee saved registers should be spilled. This method is optional.
177  virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
178                                                RegScavenger *RS = NULL) const {
179
180  }
181
182  /// processFunctionBeforeFrameFinalized - This method is called immediately
183  /// before the specified function's frame layout (MF.getFrameInfo()) is
184  /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
185  /// replaced with direct constants.  This method is optional.
186  ///
187  virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
188  }
189};
190
191} // End llvm namespace
192
193#endif
194