[X86] Enable shrink-wrapping by default.
[oota-llvm.git] / lib / Target / X86 / X86FrameLowering.h
1 //===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- 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 // This class implements X86-specific bits of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
15 #define LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
16
17 #include "llvm/Target/TargetFrameLowering.h"
18
19 namespace llvm {
20
21 class MachineInstrBuilder;
22 class MCCFIInstruction;
23 class X86Subtarget;
24 class X86RegisterInfo;
25
26 class X86FrameLowering : public TargetFrameLowering {
27 public:
28   X86FrameLowering(const X86Subtarget &STI, unsigned StackAlignOverride);
29
30   // Cached subtarget predicates.
31
32   const X86Subtarget &STI;
33   const TargetInstrInfo &TII;
34   const X86RegisterInfo *TRI;
35
36   unsigned SlotSize;
37
38   /// Is64Bit implies that x86_64 instructions are available.
39   bool Is64Bit;
40
41   bool IsLP64;
42
43   /// True if the 64-bit frame or stack pointer should be used. True for most
44   /// 64-bit targets with the exception of x32. If this is false, 32-bit
45   /// instruction operands should be used to manipulate StackPtr and FramePtr.
46   bool Uses64BitFramePtr;
47
48   unsigned StackPtr;
49
50   /// Emit target stack probe code. This is required for all
51   /// large stack allocations on Windows. The caller is required to materialize
52   /// the number of bytes to probe in RAX/EAX. Returns instruction just
53   /// after the expansion.
54   MachineInstr *emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB,
55                                MachineBasicBlock::iterator MBBI, DebugLoc DL,
56                                bool InProlog) const;
57
58   /// Replace a StackProbe inline-stub with the actual probe code inline.
59   void inlineStackProbe(MachineFunction &MF,
60                         MachineBasicBlock &PrologMBB) const override;
61
62   void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
63                                  MachineBasicBlock::iterator MBBI,
64                                  DebugLoc DL) const;
65
66   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
67   /// the function.
68   void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
69   void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
70
71   void adjustForSegmentedStacks(MachineFunction &MF,
72                                 MachineBasicBlock &PrologueMBB) const override;
73
74   void adjustForHiPEPrologue(MachineFunction &MF,
75                              MachineBasicBlock &PrologueMBB) const override;
76
77   void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
78                             RegScavenger *RS = nullptr) const override;
79
80   bool
81   assignCalleeSavedSpillSlots(MachineFunction &MF,
82                               const TargetRegisterInfo *TRI,
83                               std::vector<CalleeSavedInfo> &CSI) const override;
84
85   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
86                                  MachineBasicBlock::iterator MI,
87                                  const std::vector<CalleeSavedInfo> &CSI,
88                                  const TargetRegisterInfo *TRI) const override;
89
90   bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
91                                   MachineBasicBlock::iterator MI,
92                                   const std::vector<CalleeSavedInfo> &CSI,
93                                   const TargetRegisterInfo *TRI) const override;
94
95   bool hasFP(const MachineFunction &MF) const override;
96   bool hasReservedCallFrame(const MachineFunction &MF) const override;
97   bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
98   bool needsFrameIndexResolution(const MachineFunction &MF) const override;
99
100   int getFrameIndexReference(const MachineFunction &MF, int FI,
101                              unsigned &FrameReg) const override;
102
103   int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
104                                    unsigned &FrameReg) const override;
105
106   void eliminateCallFramePseudoInstr(MachineFunction &MF,
107                                  MachineBasicBlock &MBB,
108                                  MachineBasicBlock::iterator MI) const override;
109
110   unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
111
112   void processFunctionBeforeFrameFinalized(MachineFunction &MF,
113                                            RegScavenger *RS) const override;
114
115   /// Check the instruction before/after the passed instruction. If
116   /// it is an ADD/SUB/LEA instruction it is deleted argument and the
117   /// stack adjustment is returned as a positive value for ADD/LEA and
118   /// a negative for SUB.
119   int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
120                      bool doMergeWithPrevious) const;
121
122   /// Emit a series of instructions to increment / decrement the stack
123   /// pointer by a constant value.
124   void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
125                     int64_t NumBytes, bool InEpilogue) const;
126
127   /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
128   bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
129
130   /// Check whether or not the given \p MBB can be used as a epilogue
131   /// for the target.
132   /// The epilogue will be inserted before the first terminator of that block.
133   /// This method is used by the shrink-wrapping pass to decide if
134   /// \p MBB will be correctly handled by the target.
135   bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
136
137   /// convertArgMovsToPushes - This method tries to convert a call sequence
138   /// that uses sub and mov instructions to put the argument onto the stack
139   /// into a series of pushes.
140   /// Returns true if the transformation succeeded, false if not.
141   bool convertArgMovsToPushes(MachineFunction &MF, 
142                               MachineBasicBlock &MBB,
143                               MachineBasicBlock::iterator I, 
144                               uint64_t Amount) const;
145
146   /// Returns true if the target will correctly handle shrink wrapping.
147   bool enableShrinkWrapping(const MachineFunction &MF) const override {
148     return true;
149   }
150
151   /// Wraps up getting a CFI index and building a MachineInstr for it.
152   void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
153                 DebugLoc DL, MCCFIInstruction CFIInst) const;
154
155   /// Sets up EBP and optionally ESI based on the incoming EBP value.  Only
156   /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
157   MachineBasicBlock::iterator
158   restoreWin32EHStackPointers(MachineBasicBlock &MBB,
159                               MachineBasicBlock::iterator MBBI, DebugLoc DL,
160                               bool RestoreSP = false) const;
161
162 private:
163   uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
164
165   /// Emit target stack probe as a call to a helper function
166   MachineInstr *emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
167                                    MachineBasicBlock::iterator MBBI,
168                                    DebugLoc DL, bool InProlog) const;
169
170   /// Emit target stack probe as an inline sequence.
171   MachineInstr *emitStackProbeInline(MachineFunction &MF,
172                                      MachineBasicBlock &MBB,
173                                      MachineBasicBlock::iterator MBBI,
174                                      DebugLoc DL, bool InProlog) const;
175
176   /// Emit a stub to later inline the target stack probe.
177   MachineInstr *emitStackProbeInlineStub(MachineFunction &MF,
178                                          MachineBasicBlock &MBB,
179                                          MachineBasicBlock::iterator MBBI,
180                                          DebugLoc DL, bool InProlog) const;
181
182   /// Aligns the stack pointer by ANDing it with -MaxAlign.
183   void BuildStackAlignAND(MachineBasicBlock &MBB,
184                           MachineBasicBlock::iterator MBBI, DebugLoc DL,
185                           unsigned Reg, uint64_t MaxAlign) const;
186
187   /// Make small positive stack adjustments using POPs.
188   bool adjustStackWithPops(MachineBasicBlock &MBB,
189                            MachineBasicBlock::iterator MBBI, DebugLoc DL,
190                            int Offset) const;
191
192   /// Adjusts the stack pointer using LEA, SUB, or ADD.
193   MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
194                                            MachineBasicBlock::iterator MBBI,
195                                            DebugLoc DL, int64_t Offset,
196                                            bool InEpilogue) const;
197
198   unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
199
200   unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
201 };
202
203 } // End llvm namespace
204
205 #endif