blob: c5218cc09b8ab2b4ef072415f91822fb5170773c [file] [log] [blame]
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00001//===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- C++ -*-==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael Kupersteine86aa9a2015-02-01 16:15:07 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class implements X86-specific bits of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
14#define LLVM_LIB_TARGET_X86_X86FRAMELOWERING_H
15
David Blaikie1be62f02017-11-03 22:32:11 +000016#include "llvm/CodeGen/TargetFrameLowering.h"
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000017
18namespace llvm {
19
Reid Kleckner98d78032015-06-18 20:22:12 +000020class MachineInstrBuilder;
Reid Kleckner3854f7b2015-06-18 18:03:25 +000021class MCCFIInstruction;
Serge Pavlov49acf9c2017-04-13 14:10:52 +000022class X86InstrInfo;
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;
Serge Pavlov49acf9c2017-04-13 14:10:52 +000033 const X86InstrInfo &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
Andy Ayers809cbe92015-11-10 01:50:49 +000050 /// Emit target stack probe code. This is required for all
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000051 /// large stack allocations on Windows. The caller is required to materialize
Reid Kleckner468e7932016-10-13 15:48:48 +000052 /// the number of bytes to probe in RAX/EAX.
53 void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB,
54 MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
55 bool InProlog) const;
Andy Ayers809cbe92015-11-10 01:50:49 +000056
57 /// Replace a StackProbe inline-stub with the actual probe code inline.
58 void inlineStackProbe(MachineFunction &MF,
59 MachineBasicBlock &PrologMBB) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000060
61 void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
62 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000063 const DebugLoc &DL) const;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000064
65 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
66 /// the function.
Quentin Colombet61b305e2015-05-05 17:38:16 +000067 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000068 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
69
Quentin Colombet61b305e2015-05-05 17:38:16 +000070 void adjustForSegmentedStacks(MachineFunction &MF,
71 MachineBasicBlock &PrologueMBB) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000072
Quentin Colombet61b305e2015-05-05 17:38:16 +000073 void adjustForHiPEPrologue(MachineFunction &MF,
74 MachineBasicBlock &PrologueMBB) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000075
Matthias Braun02564862015-07-14 17:17:13 +000076 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
77 RegScavenger *RS = nullptr) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000078
79 bool
80 assignCalleeSavedSpillSlots(MachineFunction &MF,
81 const TargetRegisterInfo *TRI,
82 std::vector<CalleeSavedInfo> &CSI) const override;
83
84 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator MI,
86 const std::vector<CalleeSavedInfo> &CSI,
87 const TargetRegisterInfo *TRI) const override;
88
89 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
90 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +000091 std::vector<CalleeSavedInfo> &CSI,
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000092 const TargetRegisterInfo *TRI) const override;
93
94 bool hasFP(const MachineFunction &MF) const override;
95 bool hasReservedCallFrame(const MachineFunction &MF) const override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +000096 bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
97 bool needsFrameIndexResolution(const MachineFunction &MF) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000098
Michael Kupersteine86aa9a2015-02-01 16:15:07 +000099 int getFrameIndexReference(const MachineFunction &MF, int FI,
100 unsigned &FrameReg) const override;
101
Pengfei Wang564fb582019-08-27 01:53:24 +0000102 int getWin64EHFrameIndexRef(const MachineFunction &MF,
103 int FI, unsigned &SPReg) const;
Matthias Braunf9796b72017-04-24 18:15:00 +0000104 int getFrameIndexReferenceSP(const MachineFunction &MF,
105 int FI, unsigned &SPReg, int Adjustment) const;
Sanjoy Das0ebc9612016-06-16 18:54:06 +0000106 int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
107 unsigned &FrameReg,
108 bool IgnoreSPUpdates) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000109
Hans Wennborge1a2e902016-03-31 18:33:38 +0000110 MachineBasicBlock::iterator
111 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
112 MachineBasicBlock::iterator MI) const override;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000113
Reid Kleckner28e49032015-10-16 23:43:27 +0000114 unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
115
Reid Kleckner94b57062015-11-13 19:06:01 +0000116 void processFunctionBeforeFrameFinalized(MachineFunction &MF,
117 RegScavenger *RS) const override;
118
Quentin Colombet494eb602015-05-22 18:10:47 +0000119 /// Check the instruction before/after the passed instruction. If
120 /// it is an ADD/SUB/LEA instruction it is deleted argument and the
121 /// stack adjustment is returned as a positive value for ADD/LEA and
122 /// a negative for SUB.
Reid Klecknerf9977bf2015-06-17 21:50:02 +0000123 int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Reid Kleckner3854f7b2015-06-18 18:03:25 +0000124 bool doMergeWithPrevious) const;
Quentin Colombet494eb602015-05-22 18:10:47 +0000125
126 /// Emit a series of instructions to increment / decrement the stack
127 /// pointer by a constant value.
Reid Klecknerf9977bf2015-06-17 21:50:02 +0000128 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Paul Robinsonee88ed62018-02-14 17:35:52 +0000129 const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const;
Quentin Colombet494eb602015-05-22 18:10:47 +0000130
Quentin Colombetaa8020752015-05-27 06:28:41 +0000131 /// Check that LEA can be used on SP in an epilogue sequence for \p MF.
132 bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const;
133
Quentin Colombet4ff3cfb2016-04-26 23:44:14 +0000134 /// Check whether or not the given \p MBB can be used as a prologue
135 /// for the target.
136 /// The prologue will be inserted first in this basic block.
137 /// This method is used by the shrink-wrapping pass to decide if
138 /// \p MBB will be correctly handled by the target.
139 /// As soon as the target enable shrink-wrapping without overriding
140 /// this method, we assume that each basic block is a valid
141 /// prologue.
142 bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
143
Quentin Colombetaa8020752015-05-27 06:28:41 +0000144 /// Check whether or not the given \p MBB can be used as a epilogue
145 /// for the target.
146 /// The epilogue will be inserted before the first terminator of that block.
147 /// This method is used by the shrink-wrapping pass to decide if
148 /// \p MBB will be correctly handled by the target.
149 bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000150
Quentin Colombet5d2f7cf2015-12-09 23:08:18 +0000151 /// Returns true if the target will correctly handle shrink wrapping.
152 bool enableShrinkWrapping(const MachineFunction &MF) const override;
153
Zia Ansari30a02382016-02-15 23:44:13 +0000154 /// Order the symbols in the local stack.
155 /// We want to place the local stack objects in some sort of sensible order.
156 /// The heuristic we use is to try and pack them according to static number
157 /// of uses and size in order to minimize code size.
158 void orderFrameObjects(const MachineFunction &MF,
159 SmallVectorImpl<int> &ObjectsToAllocate) const override;
160
Reid Kleckner3854f7b2015-06-18 18:03:25 +0000161 /// Wraps up getting a CFI index and building a MachineInstr for it.
162 void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000163 const DebugLoc &DL, const MCCFIInstruction &CFIInst) const;
Reid Kleckner3854f7b2015-06-18 18:03:25 +0000164
Reid Kleckner51460c12015-11-06 01:49:05 +0000165 /// Sets up EBP and optionally ESI based on the incoming EBP value. Only
166 /// needed for 32-bit. Used in funclet prologues and at catchret destinations.
167 MachineBasicBlock::iterator
168 restoreWin32EHStackPointers(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000169 MachineBasicBlock::iterator MBBI,
170 const DebugLoc &DL, bool RestoreSP = false) const;
Reid Kleckner51460c12015-11-06 01:49:05 +0000171
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000172 int getInitialCFAOffset(const MachineFunction &MF) const override;
173
174 unsigned getInitialCFARegister(const MachineFunction &MF) const override;
175
Philip Reames849ef822019-05-10 22:55:42 +0000176 /// Return true if the function has a redzone (accessible bytes past the
177 /// frame of the top of stack function) as part of it's ABI.
178 bool has128ByteRedZone(const MachineFunction& MF) const;
179
Michael Kuperstein73dc8522015-11-03 08:17:25 +0000180private:
181 uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
182
Andy Ayers809cbe92015-11-10 01:50:49 +0000183 /// Emit target stack probe as a call to a helper function
Reid Kleckner468e7932016-10-13 15:48:48 +0000184 void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
185 MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
186 bool InProlog) const;
Andy Ayers809cbe92015-11-10 01:50:49 +0000187
188 /// Emit target stack probe as an inline sequence.
Reid Kleckner468e7932016-10-13 15:48:48 +0000189 void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
190 MachineBasicBlock::iterator MBBI,
191 const DebugLoc &DL, bool InProlog) const;
Andy Ayers809cbe92015-11-10 01:50:49 +0000192
193 /// Emit a stub to later inline the target stack probe.
Reid Kleckner468e7932016-10-13 15:48:48 +0000194 void emitStackProbeInlineStub(MachineFunction &MF, MachineBasicBlock &MBB,
195 MachineBasicBlock::iterator MBBI,
196 const DebugLoc &DL, bool InProlog) const;
Andy Ayers809cbe92015-11-10 01:50:49 +0000197
Reid Kleckner3854f7b2015-06-18 18:03:25 +0000198 /// Aligns the stack pointer by ANDing it with -MaxAlign.
199 void BuildStackAlignAND(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000200 MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
Reid Kleckner6ddae312015-11-05 21:09:49 +0000201 unsigned Reg, uint64_t MaxAlign) const;
Reid Kleckner98d78032015-06-18 20:22:12 +0000202
Michael Kuperstein7337ee22015-08-11 08:48:48 +0000203 /// Make small positive stack adjustments using POPs.
204 bool adjustStackWithPops(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000205 MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
Michael Kuperstein7337ee22015-08-11 08:48:48 +0000206 int Offset) const;
207
Reid Kleckner98d78032015-06-18 20:22:12 +0000208 /// Adjusts the stack pointer using LEA, SUB, or ADD.
209 MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
210 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000211 const DebugLoc &DL, int64_t Offset,
Reid Kleckner98d78032015-06-18 20:22:12 +0000212 bool InEpilogue) const;
Reid Klecknerdf129512015-09-08 22:44:41 +0000213
Joseph Tremoulet149c4332015-11-13 00:39:23 +0000214 unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
215
Reid Kleckner28e49032015-10-16 23:43:27 +0000216 unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
Reid Kleckner67694192017-10-05 21:37:39 +0000217
218 /// Materialize the catchret target MBB in RAX.
219 void emitCatchRetReturnValue(MachineBasicBlock &MBB,
220 MachineBasicBlock::iterator MBBI,
221 MachineInstr *CatchRet) const;
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000222};
223
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000224} // End llvm namespace
Michael Kupersteine86aa9a2015-02-01 16:15:07 +0000225
226#endif