blob: 261eade9173c899c5bba71e045da3f88fe930695 [file] [log] [blame]
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001//===-- 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
19namespace llvm {
20
Reid Kleckner98d78032015-06-18 20:22:12 +000021class MachineInstrBuilder;
Reid Kleckner3854f7b2015-06-18 18:03:25 +000022class MCCFIInstruction;
Reid Klecknerf9977bf2015-06-17 21:50:02 +000023class X86Subtarget;
24class X86RegisterInfo;
25
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000026class X86FrameLowering : public TargetFrameLowering {
27public:
Reid Klecknerf9977bf2015-06-17 21:50:02 +000028 X86FrameLowering(const X86Subtarget &STI, unsigned StackAlignOverride);
29
30 // Cached subtarget predicates.
31
32 const X86Subtarget &STI;
33 const TargetInstrInfo &TII;
Reid Kleckner034ea962015-06-18 20:32:02 +000034 const X86RegisterInfo *TRI;
Reid Klecknerf9977bf2015-06-17 21:50:02 +000035
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;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000047
Reid Kleckner3854f7b2015-06-18 18:03:25 +000048 unsigned StackPtr;
49
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000050 /// 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.
Reid Klecknerf9977bf2015-06-17 21:50:02 +000053 void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
54 MachineBasicBlock::iterator MBBI, DebugLoc DL) const;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000055
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.
Quentin Colombet61b305e2015-05-05 17:38:16 +000062 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000063 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
64
Quentin Colombet61b305e2015-05-05 17:38:16 +000065 void adjustForSegmentedStacks(MachineFunction &MF,
66 MachineBasicBlock &PrologueMBB) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000067
Quentin Colombet61b305e2015-05-05 17:38:16 +000068 void adjustForHiPEPrologue(MachineFunction &MF,
69 MachineBasicBlock &PrologueMBB) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000070
Matthias Braun02564862015-07-14 17:17:13 +000071 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
72 RegScavenger *RS = nullptr) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000073
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;
Michael Kuperstein13fbd452015-02-01 16:56:04 +000091 bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
92 bool needsFrameIndexResolution(const MachineFunction &MF) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000093
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000094 int getFrameIndexReference(const MachineFunction &MF, int FI,
95 unsigned &FrameReg) const override;
96
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000097 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
Reid Kleckner28e49032015-10-16 23:43:27 +0000104 unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
105
Quentin Colombet494eb602015-05-22 18:10:47 +0000106 /// Check the instruction before/after the passed instruction. If
107 /// it is an ADD/SUB/LEA instruction it is deleted argument and the
108 /// stack adjustment is returned as a positive value for ADD/LEA and
109 /// a negative for SUB.
Reid Klecknerf9977bf2015-06-17 21:50:02 +0000110 int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Reid Kleckner3854f7b2015-06-18 18:03:25 +0000111 bool doMergeWithPrevious) const;
Quentin Colombet494eb602015-05-22 18:10:47 +0000112
113 /// Emit a series of instructions to increment / decrement the stack
114 /// pointer by a constant value.
Reid Klecknerf9977bf2015-06-17 21:50:02 +0000115 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Reid Kleckner98d78032015-06-18 20:22:12 +0000116 int64_t NumBytes, bool InEpilogue) const;
Quentin Colombet494eb602015-05-22 18:10:47 +0000117
Quentin Colombetaa8020752015-05-27 06:28:41 +0000118 /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
119 bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
120
121 /// Check whether or not the given \p MBB can be used as a epilogue
122 /// for the target.
123 /// The epilogue will be inserted before the first terminator of that block.
124 /// This method is used by the shrink-wrapping pass to decide if
125 /// \p MBB will be correctly handled by the target.
126 bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000127
Reid Kleckner3854f7b2015-06-18 18:03:25 +0000128 /// Wraps up getting a CFI index and building a MachineInstr for it.
129 void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
130 DebugLoc DL, MCCFIInstruction CFIInst) const;
131
Michael Kuperstein73dc8522015-11-03 08:17:25 +0000132private:
133 uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
134
Reid Kleckner3854f7b2015-06-18 18:03:25 +0000135 /// Aligns the stack pointer by ANDing it with -MaxAlign.
136 void BuildStackAlignAND(MachineBasicBlock &MBB,
137 MachineBasicBlock::iterator MBBI, DebugLoc DL,
138 uint64_t MaxAlign) const;
Reid Kleckner98d78032015-06-18 20:22:12 +0000139
Michael Kuperstein7337ee22015-08-11 08:48:48 +0000140 /// Make small positive stack adjustments using POPs.
141 bool adjustStackWithPops(MachineBasicBlock &MBB,
142 MachineBasicBlock::iterator MBBI, DebugLoc DL,
143 int Offset) const;
144
Reid Kleckner98d78032015-06-18 20:22:12 +0000145 /// Adjusts the stack pointer using LEA, SUB, or ADD.
146 MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
147 MachineBasicBlock::iterator MBBI,
148 DebugLoc DL, int64_t Offset,
149 bool InEpilogue) const;
Reid Klecknerdf129512015-09-08 22:44:41 +0000150
151 /// Sets up EBP and optionally ESI based on the incoming EBP value. Only
152 /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
153 MachineBasicBlock::iterator
Reid Kleckner5b8a46e2015-09-17 20:43:47 +0000154 restoreWin32EHStackPointers(MachineBasicBlock &MBB,
155 MachineBasicBlock::iterator MBBI, DebugLoc DL,
156 bool RestoreSP = false) const;
Reid Kleckner28e49032015-10-16 23:43:27 +0000157
158 unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000159};
160
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000161} // End llvm namespace
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000162
163#endif