7cbd18bd4d03632edfa6c8b03b1e131bdc920ad5
[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 a call to the target's stack probe function. 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.
53   void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
54                           MachineBasicBlock::iterator MBBI, DebugLoc DL) const;
55
56   void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
57                                  MachineBasicBlock::iterator MBBI,
58                                  DebugLoc DL) const;
59
60   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
61   /// the function.
62   void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
63   void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
64
65   void adjustForSegmentedStacks(MachineFunction &MF,
66                                 MachineBasicBlock &PrologueMBB) const override;
67
68   void adjustForHiPEPrologue(MachineFunction &MF,
69                              MachineBasicBlock &PrologueMBB) const override;
70
71   void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
72                             RegScavenger *RS = nullptr) const override;
73
74   bool
75   assignCalleeSavedSpillSlots(MachineFunction &MF,
76                               const TargetRegisterInfo *TRI,
77                               std::vector<CalleeSavedInfo> &CSI) const override;
78
79   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
80                                  MachineBasicBlock::iterator MI,
81                                  const std::vector<CalleeSavedInfo> &CSI,
82                                  const TargetRegisterInfo *TRI) const override;
83
84   bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
85                                   MachineBasicBlock::iterator MI,
86                                   const std::vector<CalleeSavedInfo> &CSI,
87                                   const TargetRegisterInfo *TRI) const override;
88
89   bool hasFP(const MachineFunction &MF) const override;
90   bool hasReservedCallFrame(const MachineFunction &MF) const override;
91   bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
92   bool needsFrameIndexResolution(const MachineFunction &MF) const override;
93
94   int getFrameIndexReference(const MachineFunction &MF, int FI,
95                              unsigned &FrameReg) const override;
96
97   int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI,
98                                    unsigned &FrameReg) const override;
99
100   void eliminateCallFramePseudoInstr(MachineFunction &MF,
101                                  MachineBasicBlock &MBB,
102                                  MachineBasicBlock::iterator MI) const override;
103
104   /// Check the instruction before/after the passed instruction. If
105   /// it is an ADD/SUB/LEA instruction it is deleted argument and the
106   /// stack adjustment is returned as a positive value for ADD/LEA and
107   /// a negative for SUB.
108   int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
109                      bool doMergeWithPrevious) const;
110
111   /// Emit a series of instructions to increment / decrement the stack
112   /// pointer by a constant value.
113   void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
114                     int64_t NumBytes, bool InEpilogue) const;
115
116   /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
117   bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
118
119   /// Check whether or not the given \p MBB can be used as a epilogue
120   /// for the target.
121   /// The epilogue will be inserted before the first terminator of that block.
122   /// This method is used by the shrink-wrapping pass to decide if
123   /// \p MBB will be correctly handled by the target.
124   bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
125
126 private:
127   /// convertArgMovsToPushes - This method tries to convert a call sequence
128   /// that uses sub and mov instructions to put the argument onto the stack
129   /// into a series of pushes.
130   /// Returns true if the transformation succeeded, false if not.
131   bool convertArgMovsToPushes(MachineFunction &MF, 
132                               MachineBasicBlock &MBB,
133                               MachineBasicBlock::iterator I, 
134                               uint64_t Amount) const;
135
136   uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
137
138   /// Wraps up getting a CFI index and building a MachineInstr for it.
139   void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
140                 DebugLoc DL, MCCFIInstruction CFIInst) const;
141
142   /// Aligns the stack pointer by ANDing it with -MaxAlign.
143   void BuildStackAlignAND(MachineBasicBlock &MBB,
144                           MachineBasicBlock::iterator MBBI, DebugLoc DL,
145                           uint64_t MaxAlign) const;
146
147   /// Make small positive stack adjustments using POPs.
148   bool adjustStackWithPops(MachineBasicBlock &MBB,
149                            MachineBasicBlock::iterator MBBI, DebugLoc DL,
150                            int Offset) const;
151
152   /// Adjusts the stack pointer using LEA, SUB, or ADD.
153   MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
154                                            MachineBasicBlock::iterator MBBI,
155                                            DebugLoc DL, int64_t Offset,
156                                            bool InEpilogue) const;
157
158   /// Sets up EBP and optionally ESI based on the incoming EBP value.  Only
159   /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
160   MachineBasicBlock::iterator
161   restoreWin32EHFrameAndBasePtr(MachineBasicBlock &MBB,
162                                 MachineBasicBlock::iterator MBBI,
163                                 DebugLoc DL) const;
164 };
165
166 } // End llvm namespace
167
168 #endif