Add a target hook to encode the compact unwind information.
[oota-llvm.git] / include / llvm / Target / TargetFrameLowering.h
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/MC/MCDwarf.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19
20 #include <utility>
21 #include <vector>
22
23 namespace llvm {
24   class CalleeSavedInfo;
25   class MachineFunction;
26   class MachineBasicBlock;
27   class MachineMove;
28   class RegScavenger;
29
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.
33 ///
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 {
38 public:
39   enum StackDirection {
40     StackGrowsUp,        // Adding to the stack increases the stack address
41     StackGrowsDown       // Adding to the stack decreases the stack address
42   };
43
44   // Maps a callee saved register to a stack slot with a fixed offset.
45   struct SpillSlot {
46     unsigned Reg;
47     int Offset; // Offset relative to stack pointer on function entry.
48   };
49 private:
50   StackDirection StackDir;
51   unsigned StackAlignment;
52   unsigned TransientStackAlignment;
53   int LocalAreaOffset;
54 public:
55   TargetFrameLowering(StackDirection D, unsigned StackAl, int LAO,
56                       unsigned TransAl = 1)
57     : StackDir(D), StackAlignment(StackAl), TransientStackAlignment(TransAl),
58       LocalAreaOffset(LAO) {}
59
60   virtual ~TargetFrameLowering();
61
62   // These methods return information that describes the abstract stack layout
63   // of the target machine.
64
65   /// getStackGrowthDirection - Return the direction the stack grows
66   ///
67   StackDirection getStackGrowthDirection() const { return StackDir; }
68
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.
72   ///
73   unsigned getStackAlignment() const { return StackAlignment; }
74
75   /// getTransientStackAlignment - This method returns the number of bytes to
76   /// which the stack pointer must be aligned at all times, even between
77   /// calls.
78   ///
79   unsigned getTransientStackAlignment() const {
80     return TransientStackAlignment;
81   }
82
83   /// getOffsetOfLocalArea - This method returns the offset of the local area
84   /// from the stack pointer on entrance to a function.
85   ///
86   int getOffsetOfLocalArea() const { return LocalAreaOffset; }
87
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.
91   ///
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.
96   ///
97   virtual const SpillSlot *
98   getCalleeSavedSpillSlots(unsigned &NumEntries) const {
99     NumEntries = 0;
100     return 0;
101   }
102
103   /// targetHandlesStackFrameRounding - Returns true if the target is
104   /// responsible for rounding up the stack frame (probably at emitPrologue
105   /// time).
106   virtual bool targetHandlesStackFrameRounding() const {
107     return false;
108   }
109
110   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
111   /// the function.
112   virtual void emitPrologue(MachineFunction &MF) const = 0;
113   virtual void emitEpilogue(MachineFunction &MF,
114                             MachineBasicBlock &MBB) const = 0;
115
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 {
124     return false;
125   }
126
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 {
135     return false;
136   }
137
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;
142
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 {
149     return !hasFP(MF);
150   }
151
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);
161   }
162
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;
167
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;
171
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;
177
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 {
183
184   }
185
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.
190   ///
191   virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
192   }
193
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 {
199     return 0;
200   }
201 };
202
203 } // End llvm namespace
204
205 #endif