Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1 | //===-- X86TargetFrameLowering.h - Define frame lowering for X86 -*- C++ -*-==// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 6 | // |
| 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 Blaikie | 1be62f0 | 2017-11-03 22:32:11 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/TargetFrameLowering.h" |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 17 | |
| 18 | namespace llvm { |
| 19 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 20 | class MachineInstrBuilder; |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 21 | class MCCFIInstruction; |
Serge Pavlov | 49acf9c | 2017-04-13 14:10:52 +0000 | [diff] [blame] | 22 | class X86InstrInfo; |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 23 | class X86Subtarget; |
| 24 | class X86RegisterInfo; |
| 25 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 26 | class X86FrameLowering : public TargetFrameLowering { |
| 27 | public: |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 28 | X86FrameLowering(const X86Subtarget &STI, unsigned StackAlignOverride); |
| 29 | |
| 30 | // Cached subtarget predicates. |
| 31 | |
| 32 | const X86Subtarget &STI; |
Serge Pavlov | 49acf9c | 2017-04-13 14:10:52 +0000 | [diff] [blame] | 33 | const X86InstrInfo &TII; |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 34 | const X86RegisterInfo *TRI; |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 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; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 47 | |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 48 | unsigned StackPtr; |
| 49 | |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 50 | /// Emit target stack probe code. This is required for all |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 51 | /// large stack allocations on Windows. The caller is required to materialize |
Reid Kleckner | 468e793 | 2016-10-13 15:48:48 +0000 | [diff] [blame] | 52 | /// 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 Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 56 | |
| 57 | /// Replace a StackProbe inline-stub with the actual probe code inline. |
| 58 | void inlineStackProbe(MachineFunction &MF, |
| 59 | MachineBasicBlock &PrologMBB) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 60 | |
| 61 | void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB, |
| 62 | MachineBasicBlock::iterator MBBI, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 63 | const DebugLoc &DL) const; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 64 | |
| 65 | /// emitProlog/emitEpilog - These methods insert prolog and epilog code into |
| 66 | /// the function. |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 67 | void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 68 | void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override; |
| 69 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 70 | void adjustForSegmentedStacks(MachineFunction &MF, |
| 71 | MachineBasicBlock &PrologueMBB) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 72 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 73 | void adjustForHiPEPrologue(MachineFunction &MF, |
| 74 | MachineBasicBlock &PrologueMBB) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 75 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 76 | void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, |
| 77 | RegScavenger *RS = nullptr) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 78 | |
| 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 Parzyszek | bea30c6 | 2017-08-10 16:17:32 +0000 | [diff] [blame] | 91 | std::vector<CalleeSavedInfo> &CSI, |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 92 | const TargetRegisterInfo *TRI) const override; |
| 93 | |
| 94 | bool hasFP(const MachineFunction &MF) const override; |
| 95 | bool hasReservedCallFrame(const MachineFunction &MF) const override; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 96 | bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override; |
| 97 | bool needsFrameIndexResolution(const MachineFunction &MF) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 98 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 99 | int getFrameIndexReference(const MachineFunction &MF, int FI, |
| 100 | unsigned &FrameReg) const override; |
| 101 | |
Matthias Braun | f9796b7 | 2017-04-24 18:15:00 +0000 | [diff] [blame] | 102 | int getFrameIndexReferenceSP(const MachineFunction &MF, |
| 103 | int FI, unsigned &SPReg, int Adjustment) const; |
Sanjoy Das | 0ebc961 | 2016-06-16 18:54:06 +0000 | [diff] [blame] | 104 | int getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, |
| 105 | unsigned &FrameReg, |
| 106 | bool IgnoreSPUpdates) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 107 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 108 | MachineBasicBlock::iterator |
| 109 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 110 | MachineBasicBlock::iterator MI) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 111 | |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 112 | unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override; |
| 113 | |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 114 | void processFunctionBeforeFrameFinalized(MachineFunction &MF, |
| 115 | RegScavenger *RS) const override; |
| 116 | |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 117 | /// Check the instruction before/after the passed instruction. If |
| 118 | /// it is an ADD/SUB/LEA instruction it is deleted argument and the |
| 119 | /// stack adjustment is returned as a positive value for ADD/LEA and |
| 120 | /// a negative for SUB. |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 121 | int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 122 | bool doMergeWithPrevious) const; |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 123 | |
| 124 | /// Emit a series of instructions to increment / decrement the stack |
| 125 | /// pointer by a constant value. |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 126 | void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
Paul Robinson | ee88ed6 | 2018-02-14 17:35:52 +0000 | [diff] [blame] | 127 | const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const; |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 128 | |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 129 | /// Check that LEA can be used on SP in an epilogue sequence for \p MF. |
| 130 | bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const; |
| 131 | |
Quentin Colombet | 4ff3cfb | 2016-04-26 23:44:14 +0000 | [diff] [blame] | 132 | /// Check whether or not the given \p MBB can be used as a prologue |
| 133 | /// for the target. |
| 134 | /// The prologue will be inserted first in this basic block. |
| 135 | /// This method is used by the shrink-wrapping pass to decide if |
| 136 | /// \p MBB will be correctly handled by the target. |
| 137 | /// As soon as the target enable shrink-wrapping without overriding |
| 138 | /// this method, we assume that each basic block is a valid |
| 139 | /// prologue. |
| 140 | bool canUseAsPrologue(const MachineBasicBlock &MBB) const override; |
| 141 | |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 142 | /// Check whether or not the given \p MBB can be used as a epilogue |
| 143 | /// for the target. |
| 144 | /// The epilogue will be inserted before the first terminator of that block. |
| 145 | /// This method is used by the shrink-wrapping pass to decide if |
| 146 | /// \p MBB will be correctly handled by the target. |
| 147 | bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override; |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 148 | |
Quentin Colombet | 5d2f7cf | 2015-12-09 23:08:18 +0000 | [diff] [blame] | 149 | /// Returns true if the target will correctly handle shrink wrapping. |
| 150 | bool enableShrinkWrapping(const MachineFunction &MF) const override; |
| 151 | |
Zia Ansari | 30a0238 | 2016-02-15 23:44:13 +0000 | [diff] [blame] | 152 | /// Order the symbols in the local stack. |
| 153 | /// We want to place the local stack objects in some sort of sensible order. |
| 154 | /// The heuristic we use is to try and pack them according to static number |
| 155 | /// of uses and size in order to minimize code size. |
| 156 | void orderFrameObjects(const MachineFunction &MF, |
| 157 | SmallVectorImpl<int> &ObjectsToAllocate) const override; |
| 158 | |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 159 | /// Wraps up getting a CFI index and building a MachineInstr for it. |
| 160 | void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 161 | const DebugLoc &DL, const MCCFIInstruction &CFIInst) const; |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 162 | |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 163 | /// Sets up EBP and optionally ESI based on the incoming EBP value. Only |
| 164 | /// needed for 32-bit. Used in funclet prologues and at catchret destinations. |
| 165 | MachineBasicBlock::iterator |
| 166 | restoreWin32EHStackPointers(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 167 | MachineBasicBlock::iterator MBBI, |
| 168 | const DebugLoc &DL, bool RestoreSP = false) const; |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 169 | |
Petar Jovanovic | e2bfcd6 | 2018-04-24 10:32:08 +0000 | [diff] [blame] | 170 | int getInitialCFAOffset(const MachineFunction &MF) const override; |
| 171 | |
| 172 | unsigned getInitialCFARegister(const MachineFunction &MF) const override; |
| 173 | |
Philip Reames | 849ef82 | 2019-05-10 22:55:42 +0000 | [diff] [blame] | 174 | /// Return true if the function has a redzone (accessible bytes past the |
| 175 | /// frame of the top of stack function) as part of it's ABI. |
| 176 | bool has128ByteRedZone(const MachineFunction& MF) const; |
| 177 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 178 | private: |
| 179 | uint64_t calculateMaxStackAlign(const MachineFunction &MF) const; |
| 180 | |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 181 | /// Emit target stack probe as a call to a helper function |
Reid Kleckner | 468e793 | 2016-10-13 15:48:48 +0000 | [diff] [blame] | 182 | void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB, |
| 183 | MachineBasicBlock::iterator MBBI, const DebugLoc &DL, |
| 184 | bool InProlog) const; |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 185 | |
| 186 | /// Emit target stack probe as an inline sequence. |
Reid Kleckner | 468e793 | 2016-10-13 15:48:48 +0000 | [diff] [blame] | 187 | void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB, |
| 188 | MachineBasicBlock::iterator MBBI, |
| 189 | const DebugLoc &DL, bool InProlog) const; |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 190 | |
| 191 | /// Emit a stub to later inline the target stack probe. |
Reid Kleckner | 468e793 | 2016-10-13 15:48:48 +0000 | [diff] [blame] | 192 | void emitStackProbeInlineStub(MachineFunction &MF, MachineBasicBlock &MBB, |
| 193 | MachineBasicBlock::iterator MBBI, |
| 194 | const DebugLoc &DL, bool InProlog) const; |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 195 | |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 196 | /// Aligns the stack pointer by ANDing it with -MaxAlign. |
| 197 | void BuildStackAlignAND(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 198 | MachineBasicBlock::iterator MBBI, const DebugLoc &DL, |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 199 | unsigned Reg, uint64_t MaxAlign) const; |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 200 | |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 201 | /// Make small positive stack adjustments using POPs. |
| 202 | bool adjustStackWithPops(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 203 | MachineBasicBlock::iterator MBBI, const DebugLoc &DL, |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 204 | int Offset) const; |
| 205 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 206 | /// Adjusts the stack pointer using LEA, SUB, or ADD. |
| 207 | MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB, |
| 208 | MachineBasicBlock::iterator MBBI, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 209 | const DebugLoc &DL, int64_t Offset, |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 210 | bool InEpilogue) const; |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 211 | |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 212 | unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const; |
| 213 | |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 214 | unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const; |
Reid Kleckner | 6769419 | 2017-10-05 21:37:39 +0000 | [diff] [blame] | 215 | |
| 216 | /// Materialize the catchret target MBB in RAX. |
| 217 | void emitCatchRetReturnValue(MachineBasicBlock &MBB, |
| 218 | MachineBasicBlock::iterator MBBI, |
| 219 | MachineInstr *CatchRet) const; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 222 | } // End llvm namespace |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 223 | |
| 224 | #endif |