Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 21 | class MachineInstrBuilder; |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 22 | class MCCFIInstruction; |
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; |
| 33 | const TargetInstrInfo &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 |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 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; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 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. |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 68 | void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 69 | void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override; |
| 70 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 71 | void adjustForSegmentedStacks(MachineFunction &MF, |
| 72 | MachineBasicBlock &PrologueMBB) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 73 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 74 | void adjustForHiPEPrologue(MachineFunction &MF, |
| 75 | MachineBasicBlock &PrologueMBB) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 76 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 77 | void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, |
| 78 | RegScavenger *RS = nullptr) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 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; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 97 | bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override; |
| 98 | bool needsFrameIndexResolution(const MachineFunction &MF) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 99 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 100 | int getFrameIndexReference(const MachineFunction &MF, int FI, |
| 101 | unsigned &FrameReg) const override; |
| 102 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 103 | int getFrameIndexReferenceFromSP(const MachineFunction &MF, int FI, |
| 104 | unsigned &FrameReg) const override; |
| 105 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame^] | 106 | MachineBasicBlock::iterator |
| 107 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 108 | MachineBasicBlock::iterator MI) const override; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 109 | |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 110 | unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override; |
| 111 | |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 112 | void processFunctionBeforeFrameFinalized(MachineFunction &MF, |
| 113 | RegScavenger *RS) const override; |
| 114 | |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 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. |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 119 | int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 120 | bool doMergeWithPrevious) const; |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 121 | |
| 122 | /// Emit a series of instructions to increment / decrement the stack |
| 123 | /// pointer by a constant value. |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 124 | void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 125 | int64_t NumBytes, bool InEpilogue) const; |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 126 | |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 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; |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 136 | |
Quentin Colombet | 5d2f7cf | 2015-12-09 23:08:18 +0000 | [diff] [blame] | 137 | /// Returns true if the target will correctly handle shrink wrapping. |
| 138 | bool enableShrinkWrapping(const MachineFunction &MF) const override; |
| 139 | |
Zia Ansari | 30a0238 | 2016-02-15 23:44:13 +0000 | [diff] [blame] | 140 | /// Order the symbols in the local stack. |
| 141 | /// We want to place the local stack objects in some sort of sensible order. |
| 142 | /// The heuristic we use is to try and pack them according to static number |
| 143 | /// of uses and size in order to minimize code size. |
| 144 | void orderFrameObjects(const MachineFunction &MF, |
| 145 | SmallVectorImpl<int> &ObjectsToAllocate) const override; |
| 146 | |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 147 | /// convertArgMovsToPushes - This method tries to convert a call sequence |
| 148 | /// that uses sub and mov instructions to put the argument onto the stack |
| 149 | /// into a series of pushes. |
| 150 | /// Returns true if the transformation succeeded, false if not. |
| 151 | bool convertArgMovsToPushes(MachineFunction &MF, |
| 152 | MachineBasicBlock &MBB, |
| 153 | MachineBasicBlock::iterator I, |
| 154 | uint64_t Amount) const; |
| 155 | |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 156 | /// Wraps up getting a CFI index and building a MachineInstr for it. |
| 157 | void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
| 158 | DebugLoc DL, MCCFIInstruction CFIInst) const; |
| 159 | |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 160 | /// Sets up EBP and optionally ESI based on the incoming EBP value. Only |
| 161 | /// needed for 32-bit. Used in funclet prologues and at catchret destinations. |
| 162 | MachineBasicBlock::iterator |
| 163 | restoreWin32EHStackPointers(MachineBasicBlock &MBB, |
| 164 | MachineBasicBlock::iterator MBBI, DebugLoc DL, |
| 165 | bool RestoreSP = false) const; |
| 166 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 167 | private: |
| 168 | uint64_t calculateMaxStackAlign(const MachineFunction &MF) const; |
| 169 | |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 170 | /// Emit target stack probe as a call to a helper function |
| 171 | MachineInstr *emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB, |
| 172 | MachineBasicBlock::iterator MBBI, |
| 173 | DebugLoc DL, bool InProlog) const; |
| 174 | |
| 175 | /// Emit target stack probe as an inline sequence. |
| 176 | MachineInstr *emitStackProbeInline(MachineFunction &MF, |
| 177 | MachineBasicBlock &MBB, |
| 178 | MachineBasicBlock::iterator MBBI, |
| 179 | DebugLoc DL, bool InProlog) const; |
| 180 | |
| 181 | /// Emit a stub to later inline the target stack probe. |
| 182 | MachineInstr *emitStackProbeInlineStub(MachineFunction &MF, |
| 183 | MachineBasicBlock &MBB, |
| 184 | MachineBasicBlock::iterator MBBI, |
| 185 | DebugLoc DL, bool InProlog) const; |
| 186 | |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 187 | /// Aligns the stack pointer by ANDing it with -MaxAlign. |
| 188 | void BuildStackAlignAND(MachineBasicBlock &MBB, |
| 189 | MachineBasicBlock::iterator MBBI, DebugLoc DL, |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 190 | unsigned Reg, uint64_t MaxAlign) const; |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 191 | |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 192 | /// Make small positive stack adjustments using POPs. |
| 193 | bool adjustStackWithPops(MachineBasicBlock &MBB, |
| 194 | MachineBasicBlock::iterator MBBI, DebugLoc DL, |
| 195 | int Offset) const; |
| 196 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 197 | /// Adjusts the stack pointer using LEA, SUB, or ADD. |
| 198 | MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB, |
| 199 | MachineBasicBlock::iterator MBBI, |
| 200 | DebugLoc DL, int64_t Offset, |
| 201 | bool InEpilogue) const; |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 202 | |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 203 | unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const; |
| 204 | |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 205 | unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 208 | } // End llvm namespace |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 209 | |
| 210 | #endif |