Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1 | //===-- X86FrameLowering.cpp - X86 Frame Information ----------------------===// |
| 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 file contains the X86 implementation of TargetFrameLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86FrameLowering.h" |
| 15 | #include "X86InstrBuilder.h" |
| 16 | #include "X86InstrInfo.h" |
| 17 | #include "X86MachineFunctionInfo.h" |
| 18 | #include "X86Subtarget.h" |
| 19 | #include "X86TargetMachine.h" |
| 20 | #include "llvm/ADT/SmallSet.h" |
David Majnemer | 70497c6 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/EHPersonalities.h" |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DataLayout.h" |
| 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/MC/MCAsmInfo.h" |
| 31 | #include "llvm/MC/MCSymbol.h" |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
| 33 | #include "llvm/Support/Debug.h" |
| 34 | #include <cstdlib> |
| 35 | |
| 36 | using namespace llvm; |
| 37 | |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 38 | X86FrameLowering::X86FrameLowering(const X86Subtarget &STI, |
| 39 | unsigned StackAlignOverride) |
| 40 | : TargetFrameLowering(StackGrowsDown, StackAlignOverride, |
| 41 | STI.is64Bit() ? -8 : -4), |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 42 | STI(STI), TII(*STI.getInstrInfo()), TRI(STI.getRegisterInfo()) { |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 43 | // Cache a bunch of frame-related predicates for this subtarget. |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 44 | SlotSize = TRI->getSlotSize(); |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 45 | Is64Bit = STI.is64Bit(); |
| 46 | IsLP64 = STI.isTarget64BitLP64(); |
| 47 | // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit. |
| 48 | Uses64BitFramePtr = STI.isTarget64BitLP64() || STI.isTargetNaCl64(); |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 49 | StackPtr = TRI->getStackRegister(); |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 52 | bool X86FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 53 | return !MF.getFrameInfo()->hasVarSizedObjects() && |
| 54 | !MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences(); |
| 55 | } |
| 56 | |
| 57 | /// canSimplifyCallFramePseudos - If there is a reserved call frame, the |
| 58 | /// call frame pseudos can be simplified. Having a FP, as in the default |
| 59 | /// implementation, is not sufficient here since we can't always use it. |
| 60 | /// Use a more nuanced condition. |
| 61 | bool |
| 62 | X86FrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 63 | return hasReservedCallFrame(MF) || |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 64 | (hasFP(MF) && !TRI->needsStackRealignment(MF)) || |
| 65 | TRI->hasBasePointer(MF); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // needsFrameIndexResolution - Do we need to perform FI resolution for |
| 69 | // this function. Normally, this is required only when the function |
| 70 | // has any stack objects. However, FI resolution actually has another job, |
| 71 | // not apparent from the title - it resolves callframesetup/destroy |
| 72 | // that were not simplified earlier. |
| 73 | // So, this is required for x86 functions that have push sequences even |
| 74 | // when there are no stack objects. |
| 75 | bool |
| 76 | X86FrameLowering::needsFrameIndexResolution(const MachineFunction &MF) const { |
| 77 | return MF.getFrameInfo()->hasStackObjects() || |
| 78 | MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /// hasFP - Return true if the specified function should have a dedicated frame |
| 82 | /// pointer register. This is true if the function has variable sized allocas |
| 83 | /// or if frame pointer elimination is disabled. |
| 84 | bool X86FrameLowering::hasFP(const MachineFunction &MF) const { |
| 85 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 86 | const MachineModuleInfo &MMI = MF.getMMI(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 87 | |
| 88 | return (MF.getTarget().Options.DisableFramePointerElim(MF) || |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 89 | TRI->needsStackRealignment(MF) || |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 90 | MFI->hasVarSizedObjects() || |
Reid Kleckner | e69bdb8 | 2015-07-07 23:45:58 +0000 | [diff] [blame] | 91 | MFI->isFrameAddressTaken() || MFI->hasOpaqueSPAdjustment() || |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 92 | MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() || |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 93 | MMI.callsUnwindInit() || MMI.hasEHFunclets() || MMI.callsEHReturn() || |
David Majnemer | 3463e69 | 2016-01-14 01:20:03 +0000 | [diff] [blame] | 94 | MFI->hasStackMap() || MFI->hasPatchPoint() || |
| 95 | MFI->hasCopyImplyingStackAdjustment()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static unsigned getSUBriOpcode(unsigned IsLP64, int64_t Imm) { |
| 99 | if (IsLP64) { |
| 100 | if (isInt<8>(Imm)) |
| 101 | return X86::SUB64ri8; |
| 102 | return X86::SUB64ri32; |
| 103 | } else { |
| 104 | if (isInt<8>(Imm)) |
| 105 | return X86::SUB32ri8; |
| 106 | return X86::SUB32ri; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static unsigned getADDriOpcode(unsigned IsLP64, int64_t Imm) { |
| 111 | if (IsLP64) { |
| 112 | if (isInt<8>(Imm)) |
| 113 | return X86::ADD64ri8; |
| 114 | return X86::ADD64ri32; |
| 115 | } else { |
| 116 | if (isInt<8>(Imm)) |
| 117 | return X86::ADD32ri8; |
| 118 | return X86::ADD32ri; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static unsigned getSUBrrOpcode(unsigned isLP64) { |
| 123 | return isLP64 ? X86::SUB64rr : X86::SUB32rr; |
| 124 | } |
| 125 | |
| 126 | static unsigned getADDrrOpcode(unsigned isLP64) { |
| 127 | return isLP64 ? X86::ADD64rr : X86::ADD32rr; |
| 128 | } |
| 129 | |
| 130 | static unsigned getANDriOpcode(bool IsLP64, int64_t Imm) { |
| 131 | if (IsLP64) { |
| 132 | if (isInt<8>(Imm)) |
| 133 | return X86::AND64ri8; |
| 134 | return X86::AND64ri32; |
| 135 | } |
| 136 | if (isInt<8>(Imm)) |
| 137 | return X86::AND32ri8; |
| 138 | return X86::AND32ri; |
| 139 | } |
| 140 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 141 | static unsigned getLEArOpcode(unsigned IsLP64) { |
| 142 | return IsLP64 ? X86::LEA64r : X86::LEA32r; |
| 143 | } |
| 144 | |
| 145 | /// findDeadCallerSavedReg - Return a caller-saved register that isn't live |
| 146 | /// when it reaches the "return" instruction. We can then pop a stack object |
| 147 | /// to this register without worry about clobbering it. |
| 148 | static unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB, |
| 149 | MachineBasicBlock::iterator &MBBI, |
Andy Ayers | 9f75018 | 2015-11-23 22:17:44 +0000 | [diff] [blame] | 150 | const X86RegisterInfo *TRI, |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 151 | bool Is64Bit) { |
| 152 | const MachineFunction *MF = MBB.getParent(); |
| 153 | const Function *F = MF->getFunction(); |
| 154 | if (!F || MF->getMMI().callsEHReturn()) |
| 155 | return 0; |
| 156 | |
Andy Ayers | 9f75018 | 2015-11-23 22:17:44 +0000 | [diff] [blame] | 157 | const TargetRegisterClass &AvailableRegs = *TRI->getGPRsForTailCall(*MF); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 158 | |
| 159 | unsigned Opc = MBBI->getOpcode(); |
| 160 | switch (Opc) { |
| 161 | default: return 0; |
David Majnemer | d2f767d | 2016-03-04 22:56:17 +0000 | [diff] [blame] | 162 | case X86::RET: |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 163 | case X86::RETL: |
| 164 | case X86::RETQ: |
| 165 | case X86::RETIL: |
| 166 | case X86::RETIQ: |
| 167 | case X86::TCRETURNdi: |
| 168 | case X86::TCRETURNri: |
| 169 | case X86::TCRETURNmi: |
| 170 | case X86::TCRETURNdi64: |
| 171 | case X86::TCRETURNri64: |
| 172 | case X86::TCRETURNmi64: |
| 173 | case X86::EH_RETURN: |
| 174 | case X86::EH_RETURN64: { |
| 175 | SmallSet<uint16_t, 8> Uses; |
| 176 | for (unsigned i = 0, e = MBBI->getNumOperands(); i != e; ++i) { |
| 177 | MachineOperand &MO = MBBI->getOperand(i); |
| 178 | if (!MO.isReg() || MO.isDef()) |
| 179 | continue; |
| 180 | unsigned Reg = MO.getReg(); |
| 181 | if (!Reg) |
| 182 | continue; |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 183 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 184 | Uses.insert(*AI); |
| 185 | } |
| 186 | |
Andy Ayers | 9f75018 | 2015-11-23 22:17:44 +0000 | [diff] [blame] | 187 | for (auto CS : AvailableRegs) |
| 188 | if (!Uses.count(CS) && CS != X86::RIP) |
| 189 | return CS; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Reid Kleckner | 8de35fe | 2016-02-17 00:17:33 +0000 | [diff] [blame] | 196 | static bool isEAXLiveIn(MachineBasicBlock &MBB) { |
| 197 | for (MachineBasicBlock::RegisterMaskPair RegMask : MBB.liveins()) { |
| 198 | unsigned Reg = RegMask.PhysReg; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 199 | |
| 200 | if (Reg == X86::RAX || Reg == X86::EAX || Reg == X86::AX || |
| 201 | Reg == X86::AH || Reg == X86::AL) |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 208 | /// Check if the flags need to be preserved before the terminators. |
| 209 | /// This would be the case, if the eflags is live-in of the region |
| 210 | /// composed by the terminators or live-out of that region, without |
| 211 | /// being defined by a terminator. |
| 212 | static bool |
| 213 | flagsNeedToBePreservedBeforeTheTerminators(const MachineBasicBlock &MBB) { |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 214 | for (const MachineInstr &MI : MBB.terminators()) { |
Quentin Colombet | bbdebef | 2015-12-02 02:07:00 +0000 | [diff] [blame] | 215 | bool BreakNext = false; |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 216 | for (const MachineOperand &MO : MI.operands()) { |
| 217 | if (!MO.isReg()) |
| 218 | continue; |
| 219 | unsigned Reg = MO.getReg(); |
| 220 | if (Reg != X86::EFLAGS) |
| 221 | continue; |
| 222 | |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 223 | // This terminator needs an eflags that is not defined |
| 224 | // by a previous another terminator: |
| 225 | // EFLAGS is live-in of the region composed by the terminators. |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 226 | if (!MO.isDef()) |
| 227 | return true; |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 228 | // This terminator defines the eflags, i.e., we don't need to preserve it. |
Quentin Colombet | bbdebef | 2015-12-02 02:07:00 +0000 | [diff] [blame] | 229 | // However, we still need to check this specific terminator does not |
| 230 | // read a live-in value. |
| 231 | BreakNext = true; |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 232 | } |
Quentin Colombet | bbdebef | 2015-12-02 02:07:00 +0000 | [diff] [blame] | 233 | // We found a definition of the eflags, no need to preserve them. |
| 234 | if (BreakNext) |
| 235 | return false; |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 236 | } |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 237 | |
| 238 | // None of the terminators use or define the eflags. |
| 239 | // Check if they are live-out, that would imply we need to preserve them. |
| 240 | for (const MachineBasicBlock *Succ : MBB.successors()) |
| 241 | if (Succ->isLiveIn(X86::EFLAGS)) |
| 242 | return true; |
| 243 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 247 | /// emitSPUpdate - Emit a series of instructions to increment / decrement the |
| 248 | /// stack pointer by a constant value. |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 249 | void X86FrameLowering::emitSPUpdate(MachineBasicBlock &MBB, |
| 250 | MachineBasicBlock::iterator &MBBI, |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 251 | int64_t NumBytes, bool InEpilogue) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 252 | bool isSub = NumBytes < 0; |
| 253 | uint64_t Offset = isSub ? -NumBytes : NumBytes; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 254 | |
| 255 | uint64_t Chunk = (1LL << 31) - 1; |
| 256 | DebugLoc DL = MBB.findDebugLoc(MBBI); |
| 257 | |
| 258 | while (Offset) { |
| 259 | if (Offset > Chunk) { |
| 260 | // Rather than emit a long series of instructions for large offsets, |
| 261 | // load the offset into a register and do one sub/add |
| 262 | unsigned Reg = 0; |
| 263 | |
Reid Kleckner | 8de35fe | 2016-02-17 00:17:33 +0000 | [diff] [blame] | 264 | if (isSub && !isEAXLiveIn(MBB)) |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 265 | Reg = (unsigned)(Is64Bit ? X86::RAX : X86::EAX); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 266 | else |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 267 | Reg = findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 268 | |
| 269 | if (Reg) { |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 270 | unsigned Opc = Is64Bit ? X86::MOV64ri : X86::MOV32ri; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 271 | BuildMI(MBB, MBBI, DL, TII.get(Opc), Reg) |
| 272 | .addImm(Offset); |
| 273 | Opc = isSub |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 274 | ? getSUBrrOpcode(Is64Bit) |
| 275 | : getADDrrOpcode(Is64Bit); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 276 | MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) |
| 277 | .addReg(StackPtr) |
| 278 | .addReg(Reg); |
| 279 | MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. |
| 280 | Offset = 0; |
| 281 | continue; |
| 282 | } |
| 283 | } |
| 284 | |
David Majnemer | 3aa0bd8 | 2015-02-24 00:11:32 +0000 | [diff] [blame] | 285 | uint64_t ThisVal = std::min(Offset, Chunk); |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 286 | if (ThisVal == (Is64Bit ? 8 : 4)) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 287 | // Use push / pop instead. |
| 288 | unsigned Reg = isSub |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 289 | ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX) |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 290 | : findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 291 | if (Reg) { |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 292 | unsigned Opc = isSub |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 293 | ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r) |
| 294 | : (Is64Bit ? X86::POP64r : X86::POP32r); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 295 | MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opc)) |
| 296 | .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub)); |
| 297 | if (isSub) |
| 298 | MI->setFlag(MachineInstr::FrameSetup); |
Michael Kuperstein | 098cd9f | 2015-09-16 11:18:25 +0000 | [diff] [blame] | 299 | else |
| 300 | MI->setFlag(MachineInstr::FrameDestroy); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 301 | Offset -= ThisVal; |
| 302 | continue; |
| 303 | } |
| 304 | } |
| 305 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 306 | MachineInstrBuilder MI = BuildStackAdjustment( |
| 307 | MBB, MBBI, DL, isSub ? -ThisVal : ThisVal, InEpilogue); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 308 | if (isSub) |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 309 | MI.setMIFlag(MachineInstr::FrameSetup); |
Michael Kuperstein | 098cd9f | 2015-09-16 11:18:25 +0000 | [diff] [blame] | 310 | else |
| 311 | MI.setMIFlag(MachineInstr::FrameDestroy); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 312 | |
| 313 | Offset -= ThisVal; |
| 314 | } |
| 315 | } |
| 316 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 317 | MachineInstrBuilder X86FrameLowering::BuildStackAdjustment( |
| 318 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, DebugLoc DL, |
| 319 | int64_t Offset, bool InEpilogue) const { |
| 320 | assert(Offset != 0 && "zero offset stack adjustment requested"); |
| 321 | |
| 322 | // On Atom, using LEA to adjust SP is preferred, but using it in the epilogue |
| 323 | // is tricky. |
| 324 | bool UseLEA; |
| 325 | if (!InEpilogue) { |
Quentin Colombet | 9cb01aa | 2015-12-01 19:49:31 +0000 | [diff] [blame] | 326 | // Check if inserting the prologue at the beginning |
| 327 | // of MBB would require to use LEA operations. |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 328 | // We need to use LEA operations if EFLAGS is live in, because |
| 329 | // it means an instruction will read it before it gets defined. |
| 330 | UseLEA = STI.useLeaForSP() || MBB.isLiveIn(X86::EFLAGS); |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 331 | } else { |
| 332 | // If we can use LEA for SP but we shouldn't, check that none |
| 333 | // of the terminators uses the eflags. Otherwise we will insert |
| 334 | // a ADD that will redefine the eflags and break the condition. |
| 335 | // Alternatively, we could move the ADD, but this may not be possible |
| 336 | // and is an optimization anyway. |
| 337 | UseLEA = canUseLEAForSPInEpilogue(*MBB.getParent()); |
| 338 | if (UseLEA && !STI.useLeaForSP()) |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 339 | UseLEA = flagsNeedToBePreservedBeforeTheTerminators(MBB); |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 340 | // If that assert breaks, that means we do not do the right thing |
| 341 | // in canUseAsEpilogue. |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 342 | assert((UseLEA || !flagsNeedToBePreservedBeforeTheTerminators(MBB)) && |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 343 | "We shouldn't have allowed this insertion point"); |
| 344 | } |
| 345 | |
| 346 | MachineInstrBuilder MI; |
| 347 | if (UseLEA) { |
| 348 | MI = addRegOffset(BuildMI(MBB, MBBI, DL, |
| 349 | TII.get(getLEArOpcode(Uses64BitFramePtr)), |
| 350 | StackPtr), |
| 351 | StackPtr, false, Offset); |
| 352 | } else { |
| 353 | bool IsSub = Offset < 0; |
| 354 | uint64_t AbsOffset = IsSub ? -Offset : Offset; |
| 355 | unsigned Opc = IsSub ? getSUBriOpcode(Uses64BitFramePtr, AbsOffset) |
| 356 | : getADDriOpcode(Uses64BitFramePtr, AbsOffset); |
| 357 | MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) |
| 358 | .addReg(StackPtr) |
| 359 | .addImm(AbsOffset); |
| 360 | MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. |
| 361 | } |
| 362 | return MI; |
| 363 | } |
| 364 | |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 365 | int X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB, |
| 366 | MachineBasicBlock::iterator &MBBI, |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 367 | bool doMergeWithPrevious) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 368 | if ((doMergeWithPrevious && MBBI == MBB.begin()) || |
| 369 | (!doMergeWithPrevious && MBBI == MBB.end())) |
| 370 | return 0; |
| 371 | |
| 372 | MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI; |
| 373 | MachineBasicBlock::iterator NI = doMergeWithPrevious ? nullptr |
| 374 | : std::next(MBBI); |
| 375 | unsigned Opc = PI->getOpcode(); |
| 376 | int Offset = 0; |
| 377 | |
| 378 | if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || |
Quentin Colombet | b8fb2ba | 2016-02-02 20:11:17 +0000 | [diff] [blame] | 379 | Opc == X86::ADD32ri || Opc == X86::ADD32ri8) && |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 380 | PI->getOperand(0).getReg() == StackPtr){ |
| 381 | Offset += PI->getOperand(2).getImm(); |
| 382 | MBB.erase(PI); |
| 383 | if (!doMergeWithPrevious) MBBI = NI; |
Quentin Colombet | b8fb2ba | 2016-02-02 20:11:17 +0000 | [diff] [blame] | 384 | } else if ((Opc == X86::LEA32r || Opc == X86::LEA64_32r) && |
| 385 | PI->getOperand(0).getReg() == StackPtr) { |
| 386 | // For LEAs we have: def = lea SP, FI, noreg, Offset, noreg. |
| 387 | Offset += PI->getOperand(4).getImm(); |
| 388 | MBB.erase(PI); |
| 389 | if (!doMergeWithPrevious) MBBI = NI; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 390 | } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || |
| 391 | Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && |
| 392 | PI->getOperand(0).getReg() == StackPtr) { |
| 393 | Offset -= PI->getOperand(2).getImm(); |
| 394 | MBB.erase(PI); |
| 395 | if (!doMergeWithPrevious) MBBI = NI; |
| 396 | } |
| 397 | |
| 398 | return Offset; |
| 399 | } |
| 400 | |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 401 | void X86FrameLowering::BuildCFI(MachineBasicBlock &MBB, |
| 402 | MachineBasicBlock::iterator MBBI, DebugLoc DL, |
| 403 | MCCFIInstruction CFIInst) const { |
Reid Kleckner | 7f189f8 | 2015-06-15 23:45:08 +0000 | [diff] [blame] | 404 | MachineFunction &MF = *MBB.getParent(); |
| 405 | unsigned CFIIndex = MF.getMMI().addFrameInst(CFIInst); |
| 406 | BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 407 | .addCFIIndex(CFIIndex); |
| 408 | } |
| 409 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 410 | void |
| 411 | X86FrameLowering::emitCalleeSavedFrameMoves(MachineBasicBlock &MBB, |
| 412 | MachineBasicBlock::iterator MBBI, |
| 413 | DebugLoc DL) const { |
| 414 | MachineFunction &MF = *MBB.getParent(); |
| 415 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 416 | MachineModuleInfo &MMI = MF.getMMI(); |
| 417 | const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 418 | |
| 419 | // Add callee saved registers to move list. |
| 420 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 421 | if (CSI.empty()) return; |
| 422 | |
| 423 | // Calculate offsets. |
| 424 | for (std::vector<CalleeSavedInfo>::const_iterator |
| 425 | I = CSI.begin(), E = CSI.end(); I != E; ++I) { |
| 426 | int64_t Offset = MFI->getObjectOffset(I->getFrameIdx()); |
| 427 | unsigned Reg = I->getReg(); |
| 428 | |
| 429 | unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 430 | BuildCFI(MBB, MBBI, DL, |
Reid Kleckner | 7f189f8 | 2015-06-15 23:45:08 +0000 | [diff] [blame] | 431 | MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 435 | MachineInstr *X86FrameLowering::emitStackProbe(MachineFunction &MF, |
| 436 | MachineBasicBlock &MBB, |
| 437 | MachineBasicBlock::iterator MBBI, |
| 438 | DebugLoc DL, |
| 439 | bool InProlog) const { |
| 440 | const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>(); |
| 441 | if (STI.isTargetWindowsCoreCLR()) { |
| 442 | if (InProlog) { |
| 443 | return emitStackProbeInlineStub(MF, MBB, MBBI, DL, true); |
| 444 | } else { |
| 445 | return emitStackProbeInline(MF, MBB, MBBI, DL, false); |
| 446 | } |
| 447 | } else { |
| 448 | return emitStackProbeCall(MF, MBB, MBBI, DL, InProlog); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void X86FrameLowering::inlineStackProbe(MachineFunction &MF, |
| 453 | MachineBasicBlock &PrologMBB) const { |
| 454 | const StringRef ChkStkStubSymbol = "__chkstk_stub"; |
| 455 | MachineInstr *ChkStkStub = nullptr; |
| 456 | |
| 457 | for (MachineInstr &MI : PrologMBB) { |
| 458 | if (MI.isCall() && MI.getOperand(0).isSymbol() && |
| 459 | ChkStkStubSymbol == MI.getOperand(0).getSymbolName()) { |
| 460 | ChkStkStub = &MI; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (ChkStkStub != nullptr) { |
Duncan P. N. Exon Smith | d6de2a7 | 2016-02-22 02:32:35 +0000 | [diff] [blame] | 466 | assert(!ChkStkStub->isBundled() && |
| 467 | "Not expecting bundled instructions here"); |
Duncan P. N. Exon Smith | c5b668d | 2016-02-22 20:49:58 +0000 | [diff] [blame] | 468 | MachineBasicBlock::iterator MBBI = std::next(ChkStkStub->getIterator()); |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 469 | assert(std::prev(MBBI).operator==(ChkStkStub) && |
| 470 | "MBBI expected after __chkstk_stub."); |
| 471 | DebugLoc DL = PrologMBB.findDebugLoc(MBBI); |
| 472 | emitStackProbeInline(MF, PrologMBB, MBBI, DL, true); |
| 473 | ChkStkStub->eraseFromParent(); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | MachineInstr *X86FrameLowering::emitStackProbeInline( |
| 478 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 479 | MachineBasicBlock::iterator MBBI, DebugLoc DL, bool InProlog) const { |
| 480 | const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>(); |
| 481 | assert(STI.is64Bit() && "different expansion needed for 32 bit"); |
| 482 | assert(STI.isTargetWindowsCoreCLR() && "custom expansion expects CoreCLR"); |
| 483 | const TargetInstrInfo &TII = *STI.getInstrInfo(); |
| 484 | const BasicBlock *LLVM_BB = MBB.getBasicBlock(); |
| 485 | |
| 486 | // RAX contains the number of bytes of desired stack adjustment. |
| 487 | // The handling here assumes this value has already been updated so as to |
| 488 | // maintain stack alignment. |
| 489 | // |
| 490 | // We need to exit with RSP modified by this amount and execute suitable |
| 491 | // page touches to notify the OS that we're growing the stack responsibly. |
| 492 | // All stack probing must be done without modifying RSP. |
| 493 | // |
| 494 | // MBB: |
| 495 | // SizeReg = RAX; |
| 496 | // ZeroReg = 0 |
| 497 | // CopyReg = RSP |
| 498 | // Flags, TestReg = CopyReg - SizeReg |
| 499 | // FinalReg = !Flags.Ovf ? TestReg : ZeroReg |
| 500 | // LimitReg = gs magic thread env access |
| 501 | // if FinalReg >= LimitReg goto ContinueMBB |
| 502 | // RoundBB: |
| 503 | // RoundReg = page address of FinalReg |
| 504 | // LoopMBB: |
| 505 | // LoopReg = PHI(LimitReg,ProbeReg) |
| 506 | // ProbeReg = LoopReg - PageSize |
| 507 | // [ProbeReg] = 0 |
| 508 | // if (ProbeReg > RoundReg) goto LoopMBB |
| 509 | // ContinueMBB: |
| 510 | // RSP = RSP - RAX |
| 511 | // [rest of original MBB] |
| 512 | |
| 513 | // Set up the new basic blocks |
| 514 | MachineBasicBlock *RoundMBB = MF.CreateMachineBasicBlock(LLVM_BB); |
| 515 | MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB); |
| 516 | MachineBasicBlock *ContinueMBB = MF.CreateMachineBasicBlock(LLVM_BB); |
| 517 | |
| 518 | MachineFunction::iterator MBBIter = std::next(MBB.getIterator()); |
| 519 | MF.insert(MBBIter, RoundMBB); |
| 520 | MF.insert(MBBIter, LoopMBB); |
| 521 | MF.insert(MBBIter, ContinueMBB); |
| 522 | |
| 523 | // Split MBB and move the tail portion down to ContinueMBB. |
| 524 | MachineBasicBlock::iterator BeforeMBBI = std::prev(MBBI); |
| 525 | ContinueMBB->splice(ContinueMBB->begin(), &MBB, MBBI, MBB.end()); |
| 526 | ContinueMBB->transferSuccessorsAndUpdatePHIs(&MBB); |
| 527 | |
| 528 | // Some useful constants |
| 529 | const int64_t ThreadEnvironmentStackLimit = 0x10; |
| 530 | const int64_t PageSize = 0x1000; |
| 531 | const int64_t PageMask = ~(PageSize - 1); |
| 532 | |
| 533 | // Registers we need. For the normal case we use virtual |
| 534 | // registers. For the prolog expansion we use RAX, RCX and RDX. |
| 535 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 536 | const TargetRegisterClass *RegClass = &X86::GR64RegClass; |
Aaron Ballman | 107bb0d | 2015-11-11 13:44:06 +0000 | [diff] [blame] | 537 | const unsigned SizeReg = InProlog ? (unsigned)X86::RAX |
| 538 | : MRI.createVirtualRegister(RegClass), |
| 539 | ZeroReg = InProlog ? (unsigned)X86::RCX |
| 540 | : MRI.createVirtualRegister(RegClass), |
| 541 | CopyReg = InProlog ? (unsigned)X86::RDX |
| 542 | : MRI.createVirtualRegister(RegClass), |
| 543 | TestReg = InProlog ? (unsigned)X86::RDX |
| 544 | : MRI.createVirtualRegister(RegClass), |
| 545 | FinalReg = InProlog ? (unsigned)X86::RDX |
| 546 | : MRI.createVirtualRegister(RegClass), |
| 547 | RoundedReg = InProlog ? (unsigned)X86::RDX |
| 548 | : MRI.createVirtualRegister(RegClass), |
| 549 | LimitReg = InProlog ? (unsigned)X86::RCX |
| 550 | : MRI.createVirtualRegister(RegClass), |
| 551 | JoinReg = InProlog ? (unsigned)X86::RCX |
| 552 | : MRI.createVirtualRegister(RegClass), |
| 553 | ProbeReg = InProlog ? (unsigned)X86::RCX |
| 554 | : MRI.createVirtualRegister(RegClass); |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 555 | |
| 556 | // SP-relative offsets where we can save RCX and RDX. |
| 557 | int64_t RCXShadowSlot = 0; |
| 558 | int64_t RDXShadowSlot = 0; |
| 559 | |
| 560 | // If inlining in the prolog, save RCX and RDX. |
| 561 | // Future optimization: don't save or restore if not live in. |
| 562 | if (InProlog) { |
| 563 | // Compute the offsets. We need to account for things already |
| 564 | // pushed onto the stack at this point: return address, frame |
| 565 | // pointer (if used), and callee saves. |
| 566 | X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
| 567 | const int64_t CalleeSaveSize = X86FI->getCalleeSavedFrameSize(); |
| 568 | const bool HasFP = hasFP(MF); |
| 569 | RCXShadowSlot = 8 + CalleeSaveSize + (HasFP ? 8 : 0); |
| 570 | RDXShadowSlot = RCXShadowSlot + 8; |
| 571 | // Emit the saves. |
| 572 | addRegOffset(BuildMI(&MBB, DL, TII.get(X86::MOV64mr)), X86::RSP, false, |
| 573 | RCXShadowSlot) |
| 574 | .addReg(X86::RCX); |
| 575 | addRegOffset(BuildMI(&MBB, DL, TII.get(X86::MOV64mr)), X86::RSP, false, |
| 576 | RDXShadowSlot) |
| 577 | .addReg(X86::RDX); |
| 578 | } else { |
| 579 | // Not in the prolog. Copy RAX to a virtual reg. |
| 580 | BuildMI(&MBB, DL, TII.get(X86::MOV64rr), SizeReg).addReg(X86::RAX); |
| 581 | } |
| 582 | |
| 583 | // Add code to MBB to check for overflow and set the new target stack pointer |
| 584 | // to zero if so. |
| 585 | BuildMI(&MBB, DL, TII.get(X86::XOR64rr), ZeroReg) |
| 586 | .addReg(ZeroReg, RegState::Undef) |
| 587 | .addReg(ZeroReg, RegState::Undef); |
| 588 | BuildMI(&MBB, DL, TII.get(X86::MOV64rr), CopyReg).addReg(X86::RSP); |
| 589 | BuildMI(&MBB, DL, TII.get(X86::SUB64rr), TestReg) |
| 590 | .addReg(CopyReg) |
| 591 | .addReg(SizeReg); |
| 592 | BuildMI(&MBB, DL, TII.get(X86::CMOVB64rr), FinalReg) |
| 593 | .addReg(TestReg) |
| 594 | .addReg(ZeroReg); |
| 595 | |
| 596 | // FinalReg now holds final stack pointer value, or zero if |
| 597 | // allocation would overflow. Compare against the current stack |
| 598 | // limit from the thread environment block. Note this limit is the |
| 599 | // lowest touched page on the stack, not the point at which the OS |
| 600 | // will cause an overflow exception, so this is just an optimization |
| 601 | // to avoid unnecessarily touching pages that are below the current |
| 602 | // SP but already commited to the stack by the OS. |
| 603 | BuildMI(&MBB, DL, TII.get(X86::MOV64rm), LimitReg) |
| 604 | .addReg(0) |
| 605 | .addImm(1) |
| 606 | .addReg(0) |
| 607 | .addImm(ThreadEnvironmentStackLimit) |
| 608 | .addReg(X86::GS); |
| 609 | BuildMI(&MBB, DL, TII.get(X86::CMP64rr)).addReg(FinalReg).addReg(LimitReg); |
| 610 | // Jump if the desired stack pointer is at or above the stack limit. |
| 611 | BuildMI(&MBB, DL, TII.get(X86::JAE_1)).addMBB(ContinueMBB); |
| 612 | |
| 613 | // Add code to roundMBB to round the final stack pointer to a page boundary. |
| 614 | BuildMI(RoundMBB, DL, TII.get(X86::AND64ri32), RoundedReg) |
| 615 | .addReg(FinalReg) |
| 616 | .addImm(PageMask); |
| 617 | BuildMI(RoundMBB, DL, TII.get(X86::JMP_1)).addMBB(LoopMBB); |
| 618 | |
| 619 | // LimitReg now holds the current stack limit, RoundedReg page-rounded |
| 620 | // final RSP value. Add code to loopMBB to decrement LimitReg page-by-page |
| 621 | // and probe until we reach RoundedReg. |
| 622 | if (!InProlog) { |
| 623 | BuildMI(LoopMBB, DL, TII.get(X86::PHI), JoinReg) |
| 624 | .addReg(LimitReg) |
| 625 | .addMBB(RoundMBB) |
| 626 | .addReg(ProbeReg) |
| 627 | .addMBB(LoopMBB); |
| 628 | } |
| 629 | |
| 630 | addRegOffset(BuildMI(LoopMBB, DL, TII.get(X86::LEA64r), ProbeReg), JoinReg, |
| 631 | false, -PageSize); |
| 632 | |
| 633 | // Probe by storing a byte onto the stack. |
| 634 | BuildMI(LoopMBB, DL, TII.get(X86::MOV8mi)) |
| 635 | .addReg(ProbeReg) |
| 636 | .addImm(1) |
| 637 | .addReg(0) |
| 638 | .addImm(0) |
| 639 | .addReg(0) |
| 640 | .addImm(0); |
| 641 | BuildMI(LoopMBB, DL, TII.get(X86::CMP64rr)) |
| 642 | .addReg(RoundedReg) |
| 643 | .addReg(ProbeReg); |
| 644 | BuildMI(LoopMBB, DL, TII.get(X86::JNE_1)).addMBB(LoopMBB); |
| 645 | |
| 646 | MachineBasicBlock::iterator ContinueMBBI = ContinueMBB->getFirstNonPHI(); |
| 647 | |
| 648 | // If in prolog, restore RDX and RCX. |
| 649 | if (InProlog) { |
| 650 | addRegOffset(BuildMI(*ContinueMBB, ContinueMBBI, DL, TII.get(X86::MOV64rm), |
| 651 | X86::RCX), |
| 652 | X86::RSP, false, RCXShadowSlot); |
| 653 | addRegOffset(BuildMI(*ContinueMBB, ContinueMBBI, DL, TII.get(X86::MOV64rm), |
| 654 | X86::RDX), |
| 655 | X86::RSP, false, RDXShadowSlot); |
| 656 | } |
| 657 | |
| 658 | // Now that the probing is done, add code to continueMBB to update |
| 659 | // the stack pointer for real. |
| 660 | BuildMI(*ContinueMBB, ContinueMBBI, DL, TII.get(X86::SUB64rr), X86::RSP) |
| 661 | .addReg(X86::RSP) |
| 662 | .addReg(SizeReg); |
| 663 | |
| 664 | // Add the control flow edges we need. |
| 665 | MBB.addSuccessor(ContinueMBB); |
| 666 | MBB.addSuccessor(RoundMBB); |
| 667 | RoundMBB->addSuccessor(LoopMBB); |
| 668 | LoopMBB->addSuccessor(ContinueMBB); |
| 669 | LoopMBB->addSuccessor(LoopMBB); |
| 670 | |
| 671 | // Mark all the instructions added to the prolog as frame setup. |
| 672 | if (InProlog) { |
| 673 | for (++BeforeMBBI; BeforeMBBI != MBB.end(); ++BeforeMBBI) { |
| 674 | BeforeMBBI->setFlag(MachineInstr::FrameSetup); |
| 675 | } |
| 676 | for (MachineInstr &MI : *RoundMBB) { |
| 677 | MI.setFlag(MachineInstr::FrameSetup); |
| 678 | } |
| 679 | for (MachineInstr &MI : *LoopMBB) { |
| 680 | MI.setFlag(MachineInstr::FrameSetup); |
| 681 | } |
| 682 | for (MachineBasicBlock::iterator CMBBI = ContinueMBB->begin(); |
| 683 | CMBBI != ContinueMBBI; ++CMBBI) { |
| 684 | CMBBI->setFlag(MachineInstr::FrameSetup); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | // Possible TODO: physreg liveness for InProlog case. |
| 689 | |
| 690 | return ContinueMBBI; |
| 691 | } |
| 692 | |
| 693 | MachineInstr *X86FrameLowering::emitStackProbeCall( |
| 694 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 695 | MachineBasicBlock::iterator MBBI, DebugLoc DL, bool InProlog) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 696 | bool IsLargeCodeModel = MF.getTarget().getCodeModel() == CodeModel::Large; |
| 697 | |
| 698 | unsigned CallOp; |
| 699 | if (Is64Bit) |
| 700 | CallOp = IsLargeCodeModel ? X86::CALL64r : X86::CALL64pcrel32; |
| 701 | else |
| 702 | CallOp = X86::CALLpcrel32; |
| 703 | |
| 704 | const char *Symbol; |
| 705 | if (Is64Bit) { |
| 706 | if (STI.isTargetCygMing()) { |
| 707 | Symbol = "___chkstk_ms"; |
| 708 | } else { |
| 709 | Symbol = "__chkstk"; |
| 710 | } |
| 711 | } else if (STI.isTargetCygMing()) |
| 712 | Symbol = "_alloca"; |
| 713 | else |
| 714 | Symbol = "_chkstk"; |
| 715 | |
| 716 | MachineInstrBuilder CI; |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 717 | MachineBasicBlock::iterator ExpansionMBBI = std::prev(MBBI); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 718 | |
| 719 | // All current stack probes take AX and SP as input, clobber flags, and |
| 720 | // preserve all registers. x86_64 probes leave RSP unmodified. |
| 721 | if (Is64Bit && MF.getTarget().getCodeModel() == CodeModel::Large) { |
| 722 | // For the large code model, we have to call through a register. Use R11, |
| 723 | // as it is scratch in all supported calling conventions. |
| 724 | BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::R11) |
| 725 | .addExternalSymbol(Symbol); |
| 726 | CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)).addReg(X86::R11); |
| 727 | } else { |
| 728 | CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)).addExternalSymbol(Symbol); |
| 729 | } |
| 730 | |
| 731 | unsigned AX = Is64Bit ? X86::RAX : X86::EAX; |
| 732 | unsigned SP = Is64Bit ? X86::RSP : X86::ESP; |
| 733 | CI.addReg(AX, RegState::Implicit) |
| 734 | .addReg(SP, RegState::Implicit) |
| 735 | .addReg(AX, RegState::Define | RegState::Implicit) |
| 736 | .addReg(SP, RegState::Define | RegState::Implicit) |
| 737 | .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); |
| 738 | |
| 739 | if (Is64Bit) { |
| 740 | // MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp |
| 741 | // themselves. It also does not clobber %rax so we can reuse it when |
| 742 | // adjusting %rsp. |
| 743 | BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64rr), X86::RSP) |
| 744 | .addReg(X86::RSP) |
| 745 | .addReg(X86::RAX); |
| 746 | } |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 747 | |
| 748 | if (InProlog) { |
| 749 | // Apply the frame setup flag to all inserted instrs. |
| 750 | for (++ExpansionMBBI; ExpansionMBBI != MBBI; ++ExpansionMBBI) |
| 751 | ExpansionMBBI->setFlag(MachineInstr::FrameSetup); |
| 752 | } |
| 753 | |
| 754 | return MBBI; |
| 755 | } |
| 756 | |
| 757 | MachineInstr *X86FrameLowering::emitStackProbeInlineStub( |
| 758 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 759 | MachineBasicBlock::iterator MBBI, DebugLoc DL, bool InProlog) const { |
| 760 | |
| 761 | assert(InProlog && "ChkStkStub called outside prolog!"); |
| 762 | |
David Blaikie | e35168f | 2015-11-10 03:16:28 +0000 | [diff] [blame] | 763 | BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32)) |
| 764 | .addExternalSymbol("__chkstk_stub"); |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 765 | |
| 766 | return MBBI; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 767 | } |
| 768 | |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 769 | static unsigned calculateSetFPREG(uint64_t SPAdjust) { |
| 770 | // Win64 ABI has a less restrictive limitation of 240; 128 works equally well |
| 771 | // and might require smaller successive adjustments. |
| 772 | const uint64_t Win64MaxSEHOffset = 128; |
| 773 | uint64_t SEHFrameOffset = std::min(SPAdjust, Win64MaxSEHOffset); |
| 774 | // Win64 ABI requires 16-byte alignment for the UWOP_SET_FPREG opcode. |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 775 | return SEHFrameOffset & -16; |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | // If we're forcing a stack realignment we can't rely on just the frame |
| 779 | // info, we need to know the ABI stack alignment as well in case we |
| 780 | // have a call out. Otherwise just make sure we have some alignment - we'll |
| 781 | // go with the minimum SlotSize. |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 782 | uint64_t X86FrameLowering::calculateMaxStackAlign(const MachineFunction &MF) const { |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 783 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 784 | uint64_t MaxAlign = MFI->getMaxAlignment(); // Desired stack alignment. |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 785 | unsigned StackAlign = getStackAlignment(); |
Akira Hatanaka | bc497c9 | 2015-09-11 18:54:38 +0000 | [diff] [blame] | 786 | if (MF.getFunction()->hasFnAttribute("stackrealign")) { |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 787 | if (MFI->hasCalls()) |
| 788 | MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign; |
| 789 | else if (MaxAlign < SlotSize) |
| 790 | MaxAlign = SlotSize; |
| 791 | } |
| 792 | return MaxAlign; |
| 793 | } |
| 794 | |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 795 | void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB, |
| 796 | MachineBasicBlock::iterator MBBI, |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 797 | DebugLoc DL, unsigned Reg, |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 798 | uint64_t MaxAlign) const { |
| 799 | uint64_t Val = -MaxAlign; |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 800 | unsigned AndOp = getANDriOpcode(Uses64BitFramePtr, Val); |
| 801 | MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AndOp), Reg) |
| 802 | .addReg(Reg) |
| 803 | .addImm(Val) |
| 804 | .setMIFlag(MachineInstr::FrameSetup); |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 805 | |
| 806 | // The EFLAGS implicit def is dead. |
| 807 | MI->getOperand(3).setIsDead(); |
| 808 | } |
| 809 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 810 | /// emitPrologue - Push callee-saved registers onto the stack, which |
| 811 | /// automatically adjust the stack pointer. Adjust the stack pointer to allocate |
| 812 | /// space for local variables. Also emit labels used by the exception handler to |
| 813 | /// generate the exception handling frames. |
| 814 | |
| 815 | /* |
| 816 | Here's a gist of what gets emitted: |
| 817 | |
| 818 | ; Establish frame pointer, if needed |
| 819 | [if needs FP] |
| 820 | push %rbp |
| 821 | .cfi_def_cfa_offset 16 |
| 822 | .cfi_offset %rbp, -16 |
| 823 | .seh_pushreg %rpb |
| 824 | mov %rsp, %rbp |
| 825 | .cfi_def_cfa_register %rbp |
| 826 | |
| 827 | ; Spill general-purpose registers |
| 828 | [for all callee-saved GPRs] |
| 829 | pushq %<reg> |
| 830 | [if not needs FP] |
| 831 | .cfi_def_cfa_offset (offset from RETADDR) |
| 832 | .seh_pushreg %<reg> |
| 833 | |
| 834 | ; If the required stack alignment > default stack alignment |
| 835 | ; rsp needs to be re-aligned. This creates a "re-alignment gap" |
| 836 | ; of unknown size in the stack frame. |
| 837 | [if stack needs re-alignment] |
| 838 | and $MASK, %rsp |
| 839 | |
| 840 | ; Allocate space for locals |
| 841 | [if target is Windows and allocated space > 4096 bytes] |
| 842 | ; Windows needs special care for allocations larger |
| 843 | ; than one page. |
| 844 | mov $NNN, %rax |
| 845 | call ___chkstk_ms/___chkstk |
| 846 | sub %rax, %rsp |
| 847 | [else] |
| 848 | sub $NNN, %rsp |
| 849 | |
| 850 | [if needs FP] |
| 851 | .seh_stackalloc (size of XMM spill slots) |
| 852 | .seh_setframe %rbp, SEHFrameOffset ; = size of all spill slots |
| 853 | [else] |
| 854 | .seh_stackalloc NNN |
| 855 | |
| 856 | ; Spill XMMs |
| 857 | ; Note, that while only Windows 64 ABI specifies XMMs as callee-preserved, |
| 858 | ; they may get spilled on any platform, if the current function |
| 859 | ; calls @llvm.eh.unwind.init |
| 860 | [if needs FP] |
| 861 | [for all callee-saved XMM registers] |
| 862 | movaps %<xmm reg>, -MMM(%rbp) |
| 863 | [for all callee-saved XMM registers] |
| 864 | .seh_savexmm %<xmm reg>, (-MMM + SEHFrameOffset) |
| 865 | ; i.e. the offset relative to (%rbp - SEHFrameOffset) |
| 866 | [else] |
| 867 | [for all callee-saved XMM registers] |
| 868 | movaps %<xmm reg>, KKK(%rsp) |
| 869 | [for all callee-saved XMM registers] |
| 870 | .seh_savexmm %<xmm reg>, KKK |
| 871 | |
| 872 | .seh_endprologue |
| 873 | |
| 874 | [if needs base pointer] |
| 875 | mov %rsp, %rbx |
| 876 | [if needs to restore base pointer] |
| 877 | mov %rsp, -MMM(%rbp) |
| 878 | |
| 879 | ; Emit CFI info |
| 880 | [if needs FP] |
| 881 | [for all callee-saved registers] |
| 882 | .cfi_offset %<reg>, (offset from %rbp) |
| 883 | [else] |
| 884 | .cfi_def_cfa_offset (offset from RETADDR) |
| 885 | [for all callee-saved registers] |
| 886 | .cfi_offset %<reg>, (offset from %rsp) |
| 887 | |
| 888 | Notes: |
| 889 | - .seh directives are emitted only for Windows 64 ABI |
| 890 | - .cfi directives are emitted for all other ABIs |
| 891 | - for 32-bit code, substitute %e?? registers for %r?? |
| 892 | */ |
| 893 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 894 | void X86FrameLowering::emitPrologue(MachineFunction &MF, |
| 895 | MachineBasicBlock &MBB) const { |
Reid Kleckner | f9977bf | 2015-06-17 21:50:02 +0000 | [diff] [blame] | 896 | assert(&STI == &MF.getSubtarget<X86Subtarget>() && |
| 897 | "MF used frame lowering for wrong subtarget"); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 898 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 899 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 900 | const Function *Fn = MF.getFunction(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 901 | MachineModuleInfo &MMI = MF.getMMI(); |
| 902 | X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 903 | uint64_t MaxAlign = calculateMaxStackAlign(MF); // Desired stack alignment. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 904 | uint64_t StackSize = MFI->getStackSize(); // Number of bytes to allocate. |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 905 | bool IsFunclet = MBB.isEHFuncletEntry(); |
Reid Kleckner | 7850c9f | 2015-12-15 23:40:58 +0000 | [diff] [blame] | 906 | EHPersonality Personality = EHPersonality::Unknown; |
| 907 | if (Fn->hasPersonalityFn()) |
| 908 | Personality = classifyEHPersonality(Fn->getPersonalityFn()); |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 909 | bool FnHasClrFunclet = |
Reid Kleckner | 7850c9f | 2015-12-15 23:40:58 +0000 | [diff] [blame] | 910 | MMI.hasEHFunclets() && Personality == EHPersonality::CoreCLR; |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 911 | bool IsClrFunclet = IsFunclet && FnHasClrFunclet; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 912 | bool HasFP = hasFP(MF); |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 913 | bool IsWin64CC = STI.isCallingConvWin64(Fn->getCallingConv()); |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 914 | bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); |
| 915 | bool NeedsWinCFI = IsWin64Prologue && Fn->needsUnwindTableEntry(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 916 | bool NeedsDwarfCFI = |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 917 | !IsWin64Prologue && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 918 | unsigned FramePtr = TRI->getFrameRegister(MF); |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame] | 919 | const unsigned MachineFramePtr = |
| 920 | STI.isTarget64BitILP32() |
Craig Topper | 91dab7b | 2015-12-25 22:09:45 +0000 | [diff] [blame] | 921 | ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; |
Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 922 | unsigned BasePtr = TRI->getBaseRegister(); |
| 923 | |
| 924 | // Debug location must be unknown since the first debug location is used |
| 925 | // to determine the end of the prologue. |
| 926 | DebugLoc DL; |
| 927 | |
| 928 | // Add RETADDR move area to callee saved frame size. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 929 | int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 930 | if (TailCallReturnAddrDelta && IsWin64Prologue) |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 931 | report_fatal_error("Can't handle guaranteed tail call under win64 yet"); |
| 932 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 933 | if (TailCallReturnAddrDelta < 0) |
| 934 | X86FI->setCalleeSavedFrameSize( |
| 935 | X86FI->getCalleeSavedFrameSize() - TailCallReturnAddrDelta); |
| 936 | |
| 937 | bool UseStackProbe = (STI.isOSWindows() && !STI.isTargetMachO()); |
| 938 | |
| 939 | // The default stack probe size is 4096 if the function has no stackprobesize |
| 940 | // attribute. |
| 941 | unsigned StackProbeSize = 4096; |
| 942 | if (Fn->hasFnAttribute("stack-probe-size")) |
| 943 | Fn->getFnAttribute("stack-probe-size") |
| 944 | .getValueAsString() |
| 945 | .getAsInteger(0, StackProbeSize); |
| 946 | |
| 947 | // If this is x86-64 and the Red Zone is not disabled, if we are a leaf |
| 948 | // function, and use up to 128 bytes of stack space, don't have a frame |
| 949 | // pointer, calls, or dynamic alloca then we do not need to adjust the |
| 950 | // stack pointer (we fit in the Red Zone). We also check that we don't |
| 951 | // push and pop from the stack. |
Duncan P. N. Exon Smith | 5975a70 | 2015-02-14 01:59:52 +0000 | [diff] [blame] | 952 | if (Is64Bit && !Fn->hasFnAttribute(Attribute::NoRedZone) && |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 953 | !TRI->needsStackRealignment(MF) && |
David Majnemer | 3463e69 | 2016-01-14 01:20:03 +0000 | [diff] [blame] | 954 | !MFI->hasVarSizedObjects() && // No dynamic alloca. |
| 955 | !MFI->adjustsStack() && // No calls. |
| 956 | !IsWin64CC && // Win64 has no Red Zone |
| 957 | !MFI->hasCopyImplyingStackAdjustment() && // Don't push and pop. |
| 958 | !MF.shouldSplitStack()) { // Regular stack |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 959 | uint64_t MinSize = X86FI->getCalleeSavedFrameSize(); |
| 960 | if (HasFP) MinSize += SlotSize; |
| 961 | StackSize = std::max(MinSize, StackSize > 128 ? StackSize - 128 : 0); |
| 962 | MFI->setStackSize(StackSize); |
| 963 | } |
| 964 | |
| 965 | // Insert stack pointer adjustment for later moving of return addr. Only |
| 966 | // applies to tail call optimized functions where the callee argument stack |
| 967 | // size is bigger than the callers. |
| 968 | if (TailCallReturnAddrDelta < 0) { |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 969 | BuildStackAdjustment(MBB, MBBI, DL, TailCallReturnAddrDelta, |
| 970 | /*InEpilogue=*/false) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 971 | .setMIFlag(MachineInstr::FrameSetup); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | // Mapping for machine moves: |
| 975 | // |
| 976 | // DST: VirtualFP AND |
| 977 | // SRC: VirtualFP => DW_CFA_def_cfa_offset |
| 978 | // ELSE => DW_CFA_def_cfa |
| 979 | // |
| 980 | // SRC: VirtualFP AND |
| 981 | // DST: Register => DW_CFA_def_cfa_register |
| 982 | // |
| 983 | // ELSE |
| 984 | // OFFSET < 0 => DW_CFA_offset_extended_sf |
| 985 | // REG < 64 => DW_CFA_offset + Reg |
| 986 | // ELSE => DW_CFA_offset_extended |
| 987 | |
| 988 | uint64_t NumBytes = 0; |
| 989 | int stackGrowth = -SlotSize; |
| 990 | |
Joseph Tremoulet | 6afccf6 | 2015-11-05 02:20:07 +0000 | [diff] [blame] | 991 | // Find the funclet establisher parameter |
| 992 | unsigned Establisher = X86::NoRegister; |
| 993 | if (IsClrFunclet) |
| 994 | Establisher = Uses64BitFramePtr ? X86::RCX : X86::ECX; |
| 995 | else if (IsFunclet) |
| 996 | Establisher = Uses64BitFramePtr ? X86::RDX : X86::EDX; |
| 997 | |
Craig Topper | 8e44b9a | 2015-12-10 06:09:41 +0000 | [diff] [blame] | 998 | if (IsWin64Prologue && IsFunclet && !IsClrFunclet) { |
Joseph Tremoulet | 6afccf6 | 2015-11-05 02:20:07 +0000 | [diff] [blame] | 999 | // Immediately spill establisher into the home slot. |
| 1000 | // The runtime cares about this. |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1001 | // MOV64mr %rdx, 16(%rsp) |
| 1002 | unsigned MOVmr = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr; |
| 1003 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(MOVmr)), StackPtr, true, 16) |
Joseph Tremoulet | 6afccf6 | 2015-11-05 02:20:07 +0000 | [diff] [blame] | 1004 | .addReg(Establisher) |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1005 | .setMIFlag(MachineInstr::FrameSetup); |
Reid Kleckner | 64b003f | 2015-11-09 21:04:00 +0000 | [diff] [blame] | 1006 | MBB.addLiveIn(Establisher); |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1007 | } |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1008 | |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1009 | if (HasFP) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1010 | // Calculate required stack adjustment. |
| 1011 | uint64_t FrameSize = StackSize - SlotSize; |
| 1012 | // If required, include space for extra hidden slot for stashing base pointer. |
| 1013 | if (X86FI->getRestoreBasePointer()) |
| 1014 | FrameSize += SlotSize; |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1015 | |
| 1016 | NumBytes = FrameSize - X86FI->getCalleeSavedFrameSize(); |
| 1017 | |
| 1018 | // Callee-saved registers are pushed on stack before the stack is realigned. |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1019 | if (TRI->needsStackRealignment(MF) && !IsWin64Prologue) |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 1020 | NumBytes = alignTo(NumBytes, MaxAlign); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1021 | |
| 1022 | // Get the offset of the stack slot for the EBP register, which is |
| 1023 | // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. |
| 1024 | // Update the frame offset adjustment. |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1025 | if (!IsFunclet) |
| 1026 | MFI->setOffsetAdjustment(-NumBytes); |
| 1027 | else |
David Blaikie | 757908e | 2015-09-30 20:37:48 +0000 | [diff] [blame] | 1028 | assert(MFI->getOffsetAdjustment() == -(int)NumBytes && |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1029 | "should calculate same local variable offset for funclets"); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1030 | |
| 1031 | // Save EBP/RBP into the appropriate stack slot. |
| 1032 | BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r)) |
| 1033 | .addReg(MachineFramePtr, RegState::Kill) |
| 1034 | .setMIFlag(MachineInstr::FrameSetup); |
| 1035 | |
| 1036 | if (NeedsDwarfCFI) { |
| 1037 | // Mark the place where EBP/RBP was saved. |
| 1038 | // Define the current CFA rule to use the provided offset. |
| 1039 | assert(StackSize); |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 1040 | BuildCFI(MBB, MBBI, DL, |
Reid Kleckner | 7f189f8 | 2015-06-15 23:45:08 +0000 | [diff] [blame] | 1041 | MCCFIInstruction::createDefCfaOffset(nullptr, 2 * stackGrowth)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1042 | |
| 1043 | // Change the rule for the FramePtr to be an "offset" rule. |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1044 | unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 1045 | BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createOffset( |
| 1046 | nullptr, DwarfFramePtr, 2 * stackGrowth)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1049 | if (NeedsWinCFI) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1050 | BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)) |
| 1051 | .addImm(FramePtr) |
| 1052 | .setMIFlag(MachineInstr::FrameSetup); |
| 1053 | } |
| 1054 | |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1055 | if (!IsWin64Prologue && !IsFunclet) { |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1056 | // Update EBP with the new base value. |
| 1057 | BuildMI(MBB, MBBI, DL, |
| 1058 | TII.get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), |
| 1059 | FramePtr) |
| 1060 | .addReg(StackPtr) |
| 1061 | .setMIFlag(MachineInstr::FrameSetup); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1062 | |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1063 | if (NeedsDwarfCFI) { |
| 1064 | // Mark effective beginning of when frame pointer becomes valid. |
| 1065 | // Define the current CFA to use the EBP/RBP register. |
| 1066 | unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); |
| 1067 | BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaRegister( |
| 1068 | nullptr, DwarfFramePtr)); |
| 1069 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Reid Kleckner | 64b003f | 2015-11-09 21:04:00 +0000 | [diff] [blame] | 1072 | // Mark the FramePtr as live-in in every block. Don't do this again for |
| 1073 | // funclet prologues. |
| 1074 | if (!IsFunclet) { |
| 1075 | for (MachineBasicBlock &EveryMBB : MF) |
| 1076 | EveryMBB.addLiveIn(MachineFramePtr); |
| 1077 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1078 | } else { |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1079 | assert(!IsFunclet && "funclets without FPs not yet implemented"); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1080 | NumBytes = StackSize - X86FI->getCalleeSavedFrameSize(); |
| 1081 | } |
| 1082 | |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1083 | // For EH funclets, only allocate enough space for outgoing calls. Save the |
| 1084 | // NumBytes value that we would've used for the parent frame. |
| 1085 | unsigned ParentFrameNumBytes = NumBytes; |
| 1086 | if (IsFunclet) |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 1087 | NumBytes = getWinEHFuncletFrameSize(MF); |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1088 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1089 | // Skip the callee-saved push instructions. |
| 1090 | bool PushedRegs = false; |
| 1091 | int StackOffset = 2 * stackGrowth; |
| 1092 | |
| 1093 | while (MBBI != MBB.end() && |
Michael Kuperstein | e1ea4e7d | 2015-07-16 12:27:59 +0000 | [diff] [blame] | 1094 | MBBI->getFlag(MachineInstr::FrameSetup) && |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1095 | (MBBI->getOpcode() == X86::PUSH32r || |
| 1096 | MBBI->getOpcode() == X86::PUSH64r)) { |
| 1097 | PushedRegs = true; |
| 1098 | unsigned Reg = MBBI->getOperand(0).getReg(); |
| 1099 | ++MBBI; |
| 1100 | |
| 1101 | if (!HasFP && NeedsDwarfCFI) { |
| 1102 | // Mark callee-saved push instruction. |
| 1103 | // Define the current CFA rule to use the provided offset. |
| 1104 | assert(StackSize); |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 1105 | BuildCFI(MBB, MBBI, DL, |
Reid Kleckner | 7f189f8 | 2015-06-15 23:45:08 +0000 | [diff] [blame] | 1106 | MCCFIInstruction::createDefCfaOffset(nullptr, StackOffset)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1107 | StackOffset += stackGrowth; |
| 1108 | } |
| 1109 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1110 | if (NeedsWinCFI) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1111 | BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)).addImm(Reg).setMIFlag( |
| 1112 | MachineInstr::FrameSetup); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | // Realign stack after we pushed callee-saved registers (so that we'll be |
| 1117 | // able to calculate their offsets from the frame pointer). |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1118 | // Don't do this for Win64, it needs to realign the stack after the prologue. |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1119 | if (!IsWin64Prologue && !IsFunclet && TRI->needsStackRealignment(MF)) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1120 | assert(HasFP && "There should be a frame pointer if stack is realigned."); |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1121 | BuildStackAlignAND(MBB, MBBI, DL, StackPtr, MaxAlign); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | // If there is an SUB32ri of ESP immediately before this instruction, merge |
| 1125 | // the two. This can be the case when tail call elimination is enabled and |
| 1126 | // the callee has more arguments then the caller. |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 1127 | NumBytes -= mergeSPUpdates(MBB, MBBI, true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1128 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1129 | // Adjust stack pointer: ESP -= numbytes. |
| 1130 | |
| 1131 | // Windows and cygwin/mingw require a prologue helper routine when allocating |
| 1132 | // more than 4K bytes on the stack. Windows uses __chkstk and cygwin/mingw |
| 1133 | // uses __alloca. __alloca and the 32-bit version of __chkstk will probe the |
| 1134 | // stack and adjust the stack pointer in one go. The 64-bit version of |
| 1135 | // __chkstk is only responsible for probing the stack. The 64-bit prologue is |
| 1136 | // responsible for adjusting the stack pointer. Touching the stack at 4K |
| 1137 | // increments is necessary to ensure that the guard pages used by the OS |
| 1138 | // virtual memory manager are allocated in correct sequence. |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1139 | uint64_t AlignedNumBytes = NumBytes; |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1140 | if (IsWin64Prologue && !IsFunclet && TRI->needsStackRealignment(MF)) |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 1141 | AlignedNumBytes = alignTo(AlignedNumBytes, MaxAlign); |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1142 | if (AlignedNumBytes >= StackProbeSize && UseStackProbe) { |
Reid Kleckner | 8de35fe | 2016-02-17 00:17:33 +0000 | [diff] [blame] | 1143 | // Check whether EAX is livein for this block. |
| 1144 | bool isEAXAlive = isEAXLiveIn(MBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1145 | |
| 1146 | if (isEAXAlive) { |
| 1147 | // Sanity check that EAX is not livein for this function. |
| 1148 | // It should not be, so throw an assert. |
| 1149 | assert(!Is64Bit && "EAX is livein in x64 case!"); |
| 1150 | |
| 1151 | // Save EAX |
| 1152 | BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH32r)) |
| 1153 | .addReg(X86::EAX, RegState::Kill) |
| 1154 | .setMIFlag(MachineInstr::FrameSetup); |
| 1155 | } |
| 1156 | |
| 1157 | if (Is64Bit) { |
| 1158 | // Handle the 64-bit Windows ABI case where we need to call __chkstk. |
| 1159 | // Function prologue is responsible for adjusting the stack pointer. |
David Majnemer | 006c490 | 2015-02-23 21:50:30 +0000 | [diff] [blame] | 1160 | if (isUInt<32>(NumBytes)) { |
| 1161 | BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) |
| 1162 | .addImm(NumBytes) |
| 1163 | .setMIFlag(MachineInstr::FrameSetup); |
| 1164 | } else if (isInt<32>(NumBytes)) { |
| 1165 | BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri32), X86::RAX) |
| 1166 | .addImm(NumBytes) |
| 1167 | .setMIFlag(MachineInstr::FrameSetup); |
| 1168 | } else { |
| 1169 | BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::RAX) |
| 1170 | .addImm(NumBytes) |
| 1171 | .setMIFlag(MachineInstr::FrameSetup); |
| 1172 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1173 | } else { |
| 1174 | // Allocate NumBytes-4 bytes on stack in case of isEAXAlive. |
| 1175 | // We'll also use 4 already allocated bytes for EAX. |
| 1176 | BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 1177 | .addImm(isEAXAlive ? NumBytes - 4 : NumBytes) |
| 1178 | .setMIFlag(MachineInstr::FrameSetup); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1181 | // Call __chkstk, __chkstk_ms, or __alloca. |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 1182 | emitStackProbe(MF, MBB, MBBI, DL, true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (isEAXAlive) { |
| 1185 | // Restore EAX |
Andy Ayers | 809cbe9 | 2015-11-10 01:50:49 +0000 | [diff] [blame] | 1186 | MachineInstr *MI = |
| 1187 | addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV32rm), X86::EAX), |
| 1188 | StackPtr, false, NumBytes - 4); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1189 | MI->setFlag(MachineInstr::FrameSetup); |
| 1190 | MBB.insert(MBBI, MI); |
| 1191 | } |
| 1192 | } else if (NumBytes) { |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 1193 | emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, /*InEpilogue=*/false); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1196 | if (NeedsWinCFI && NumBytes) |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1197 | BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc)) |
| 1198 | .addImm(NumBytes) |
| 1199 | .setMIFlag(MachineInstr::FrameSetup); |
| 1200 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1201 | int SEHFrameOffset = 0; |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 1202 | unsigned SPOrEstablisher; |
| 1203 | if (IsFunclet) { |
| 1204 | if (IsClrFunclet) { |
| 1205 | // The establisher parameter passed to a CLR funclet is actually a pointer |
| 1206 | // to the (mostly empty) frame of its nearest enclosing funclet; we have |
| 1207 | // to find the root function establisher frame by loading the PSPSym from |
| 1208 | // the intermediate frame. |
| 1209 | unsigned PSPSlotOffset = getPSPSlotOffsetFromSP(MF); |
| 1210 | MachinePointerInfo NoInfo; |
| 1211 | MBB.addLiveIn(Establisher); |
| 1212 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rm), Establisher), |
| 1213 | Establisher, false, PSPSlotOffset) |
| 1214 | .addMemOperand(MF.getMachineMemOperand( |
| 1215 | NoInfo, MachineMemOperand::MOLoad, SlotSize, SlotSize)); |
| 1216 | ; |
| 1217 | // Save the root establisher back into the current funclet's (mostly |
| 1218 | // empty) frame, in case a sub-funclet or the GC needs it. |
| 1219 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mr)), StackPtr, |
| 1220 | false, PSPSlotOffset) |
| 1221 | .addReg(Establisher) |
| 1222 | .addMemOperand( |
| 1223 | MF.getMachineMemOperand(NoInfo, MachineMemOperand::MOStore | |
| 1224 | MachineMemOperand::MOVolatile, |
| 1225 | SlotSize, SlotSize)); |
| 1226 | } |
| 1227 | SPOrEstablisher = Establisher; |
| 1228 | } else { |
| 1229 | SPOrEstablisher = StackPtr; |
| 1230 | } |
| 1231 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1232 | if (IsWin64Prologue && HasFP) { |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1233 | // Set RBP to a small fixed offset from RSP. In the funclet case, we base |
Joseph Tremoulet | 6afccf6 | 2015-11-05 02:20:07 +0000 | [diff] [blame] | 1234 | // this calculation on the incoming establisher, which holds the value of |
| 1235 | // RSP from the parent frame at the end of the prologue. |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1236 | SEHFrameOffset = calculateSetFPREG(ParentFrameNumBytes); |
David Majnemer | 31d868b | 2015-02-23 21:50:27 +0000 | [diff] [blame] | 1237 | if (SEHFrameOffset) |
| 1238 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::LEA64r), FramePtr), |
Joseph Tremoulet | 6afccf6 | 2015-11-05 02:20:07 +0000 | [diff] [blame] | 1239 | SPOrEstablisher, false, SEHFrameOffset); |
David Majnemer | 31d868b | 2015-02-23 21:50:27 +0000 | [diff] [blame] | 1240 | else |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1241 | BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rr), FramePtr) |
Joseph Tremoulet | 6afccf6 | 2015-11-05 02:20:07 +0000 | [diff] [blame] | 1242 | .addReg(SPOrEstablisher); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1243 | |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1244 | // If this is not a funclet, emit the CFI describing our frame pointer. |
Reid Kleckner | 7850c9f | 2015-12-15 23:40:58 +0000 | [diff] [blame] | 1245 | if (NeedsWinCFI && !IsFunclet) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1246 | BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame)) |
| 1247 | .addImm(FramePtr) |
| 1248 | .addImm(SEHFrameOffset) |
| 1249 | .setMIFlag(MachineInstr::FrameSetup); |
Reid Kleckner | 7850c9f | 2015-12-15 23:40:58 +0000 | [diff] [blame] | 1250 | if (isAsynchronousEHPersonality(Personality)) |
| 1251 | MF.getWinEHFuncInfo()->SEHSetFrameOffset = SEHFrameOffset; |
| 1252 | } |
Reid Kleckner | a13dfd5 | 2015-09-29 23:32:01 +0000 | [diff] [blame] | 1253 | } else if (IsFunclet && STI.is32Bit()) { |
| 1254 | // Reset EBP / ESI to something good for funclets. |
| 1255 | MBBI = restoreWin32EHStackPointers(MBB, MBBI, DL); |
Reid Kleckner | 75b4be9 | 2015-11-13 21:27:00 +0000 | [diff] [blame] | 1256 | // If we're a catch funclet, we can be returned to via catchret. Save ESP |
| 1257 | // into the registration node so that the runtime will restore it for us. |
| 1258 | if (!MBB.isCleanupFuncletEntry()) { |
Reid Kleckner | 7850c9f | 2015-12-15 23:40:58 +0000 | [diff] [blame] | 1259 | assert(Personality == EHPersonality::MSVC_CXX); |
Reid Kleckner | 75b4be9 | 2015-11-13 21:27:00 +0000 | [diff] [blame] | 1260 | unsigned FrameReg; |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 1261 | int FI = MF.getWinEHFuncInfo()->EHRegNodeFrameIndex; |
Reid Kleckner | 75b4be9 | 2015-11-13 21:27:00 +0000 | [diff] [blame] | 1262 | int64_t EHRegOffset = getFrameIndexReference(MF, FI, FrameReg); |
| 1263 | // ESP is the first field, so no extra displacement is needed. |
| 1264 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32mr)), FrameReg, |
| 1265 | false, EHRegOffset) |
| 1266 | .addReg(X86::ESP); |
| 1267 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
David Majnemer | a7d908e | 2015-02-10 19:01:47 +0000 | [diff] [blame] | 1270 | while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup)) { |
| 1271 | const MachineInstr *FrameInstr = &*MBBI; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1272 | ++MBBI; |
| 1273 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1274 | if (NeedsWinCFI) { |
David Majnemer | a7d908e | 2015-02-10 19:01:47 +0000 | [diff] [blame] | 1275 | int FI; |
| 1276 | if (unsigned Reg = TII.isStoreToStackSlot(FrameInstr, FI)) { |
| 1277 | if (X86::FR64RegClass.contains(Reg)) { |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 1278 | unsigned IgnoredFrameReg; |
| 1279 | int Offset = getFrameIndexReference(MF, FI, IgnoredFrameReg); |
David Majnemer | a7d908e | 2015-02-10 19:01:47 +0000 | [diff] [blame] | 1280 | Offset += SEHFrameOffset; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1281 | |
David Majnemer | a7d908e | 2015-02-10 19:01:47 +0000 | [diff] [blame] | 1282 | BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SaveXMM)) |
| 1283 | .addImm(Reg) |
| 1284 | .addImm(Offset) |
| 1285 | .setMIFlag(MachineInstr::FrameSetup); |
| 1286 | } |
| 1287 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1288 | } |
David Majnemer | a7d908e | 2015-02-10 19:01:47 +0000 | [diff] [blame] | 1289 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1290 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1291 | if (NeedsWinCFI) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1292 | BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_EndPrologue)) |
| 1293 | .setMIFlag(MachineInstr::FrameSetup); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1294 | |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 1295 | if (FnHasClrFunclet && !IsFunclet) { |
| 1296 | // Save the so-called Initial-SP (i.e. the value of the stack pointer |
| 1297 | // immediately after the prolog) into the PSPSlot so that funclets |
| 1298 | // and the GC can recover it. |
| 1299 | unsigned PSPSlotOffset = getPSPSlotOffsetFromSP(MF); |
| 1300 | auto PSPInfo = MachinePointerInfo::getFixedStack( |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 1301 | MF, MF.getWinEHFuncInfo()->PSPSymFrameIdx); |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 1302 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mr)), StackPtr, false, |
| 1303 | PSPSlotOffset) |
| 1304 | .addReg(StackPtr) |
| 1305 | .addMemOperand(MF.getMachineMemOperand( |
| 1306 | PSPInfo, MachineMemOperand::MOStore | MachineMemOperand::MOVolatile, |
| 1307 | SlotSize, SlotSize)); |
| 1308 | } |
| 1309 | |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1310 | // Realign stack after we spilled callee-saved registers (so that we'll be |
| 1311 | // able to calculate their offsets from the frame pointer). |
| 1312 | // Win64 requires aligning the stack after the prologue. |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1313 | if (IsWin64Prologue && TRI->needsStackRealignment(MF)) { |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1314 | assert(HasFP && "There should be a frame pointer if stack is realigned."); |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1315 | BuildStackAlignAND(MBB, MBBI, DL, SPOrEstablisher, MaxAlign); |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1318 | // We already dealt with stack realignment and funclets above. |
| 1319 | if (IsFunclet && STI.is32Bit()) |
| 1320 | return; |
| 1321 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1322 | // If we need a base pointer, set it up here. It's whatever the value |
| 1323 | // of the stack pointer is at this point. Any variable size objects |
| 1324 | // will be allocated after this, so we can still use the base pointer |
| 1325 | // to reference locals. |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1326 | if (TRI->hasBasePointer(MF)) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1327 | // Update the base pointer with the current stack pointer. |
| 1328 | unsigned Opc = Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr; |
| 1329 | BuildMI(MBB, MBBI, DL, TII.get(Opc), BasePtr) |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1330 | .addReg(SPOrEstablisher) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1331 | .setMIFlag(MachineInstr::FrameSetup); |
| 1332 | if (X86FI->getRestoreBasePointer()) { |
Reid Kleckner | e69bdb8 | 2015-07-07 23:45:58 +0000 | [diff] [blame] | 1333 | // Stash value of base pointer. Saving RSP instead of EBP shortens |
| 1334 | // dependence chain. Used by SjLj EH. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1335 | unsigned Opm = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr; |
| 1336 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opm)), |
| 1337 | FramePtr, true, X86FI->getRestoreBasePointerOffset()) |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1338 | .addReg(SPOrEstablisher) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1339 | .setMIFlag(MachineInstr::FrameSetup); |
| 1340 | } |
Reid Kleckner | e69bdb8 | 2015-07-07 23:45:58 +0000 | [diff] [blame] | 1341 | |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1342 | if (X86FI->getHasSEHFramePtrSave() && !IsFunclet) { |
Reid Kleckner | e69bdb8 | 2015-07-07 23:45:58 +0000 | [diff] [blame] | 1343 | // Stash the value of the frame pointer relative to the base pointer for |
| 1344 | // Win32 EH. This supports Win32 EH, which does the inverse of the above: |
| 1345 | // it recovers the frame pointer from the base pointer rather than the |
| 1346 | // other way around. |
| 1347 | unsigned Opm = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr; |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1348 | unsigned UsedReg; |
| 1349 | int Offset = |
| 1350 | getFrameIndexReference(MF, X86FI->getSEHFramePtrSaveIndex(), UsedReg); |
| 1351 | assert(UsedReg == BasePtr); |
| 1352 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opm)), UsedReg, true, Offset) |
Reid Kleckner | e69bdb8 | 2015-07-07 23:45:58 +0000 | [diff] [blame] | 1353 | .addReg(FramePtr) |
| 1354 | .setMIFlag(MachineInstr::FrameSetup); |
| 1355 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI) { |
| 1359 | // Mark end of stack pointer adjustment. |
| 1360 | if (!HasFP && NumBytes) { |
| 1361 | // Define the current CFA rule to use the provided offset. |
| 1362 | assert(StackSize); |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 1363 | BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset( |
| 1364 | nullptr, -StackSize + stackGrowth)); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | // Emit DWARF info specifying the offsets of the callee-saved registers. |
| 1368 | if (PushedRegs) |
| 1369 | emitCalleeSavedFrameMoves(MBB, MBBI, DL); |
| 1370 | } |
| 1371 | } |
| 1372 | |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 1373 | bool X86FrameLowering::canUseLEAForSPInEpilogue( |
| 1374 | const MachineFunction &MF) const { |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 1375 | // We can't use LEA instructions for adjusting the stack pointer if this is a |
| 1376 | // leaf function in the Win64 ABI. Only ADD instructions may be used to |
| 1377 | // deallocate the stack. |
| 1378 | // This means that we can use LEA for SP in two situations: |
| 1379 | // 1. We *aren't* using the Win64 ABI which means we are free to use LEA. |
| 1380 | // 2. We *have* a frame pointer which means we are permitted to use LEA. |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 1381 | return !MF.getTarget().getMCAsmInfo()->usesWindowsCFI() || hasFP(MF); |
| 1382 | } |
| 1383 | |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1384 | static bool isFuncletReturnInstr(MachineInstr *MI) { |
| 1385 | switch (MI->getOpcode()) { |
| 1386 | case X86::CATCHRET: |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 1387 | case X86::CLEANUPRET: |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1388 | return true; |
| 1389 | default: |
| 1390 | return false; |
| 1391 | } |
| 1392 | llvm_unreachable("impossible"); |
| 1393 | } |
| 1394 | |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 1395 | // CLR funclets use a special "Previous Stack Pointer Symbol" slot on the |
| 1396 | // stack. It holds a pointer to the bottom of the root function frame. The |
| 1397 | // establisher frame pointer passed to a nested funclet may point to the |
| 1398 | // (mostly empty) frame of its parent funclet, but it will need to find |
| 1399 | // the frame of the root function to access locals. To facilitate this, |
| 1400 | // every funclet copies the pointer to the bottom of the root function |
| 1401 | // frame into a PSPSym slot in its own (mostly empty) stack frame. Using the |
| 1402 | // same offset for the PSPSym in the root function frame that's used in the |
| 1403 | // funclets' frames allows each funclet to dynamically accept any ancestor |
| 1404 | // frame as its establisher argument (the runtime doesn't guarantee the |
| 1405 | // immediate parent for some reason lost to history), and also allows the GC, |
| 1406 | // which uses the PSPSym for some bookkeeping, to find it in any funclet's |
| 1407 | // frame with only a single offset reported for the entire method. |
| 1408 | unsigned |
| 1409 | X86FrameLowering::getPSPSlotOffsetFromSP(const MachineFunction &MF) const { |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 1410 | const WinEHFuncInfo &Info = *MF.getWinEHFuncInfo(); |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 1411 | // getFrameIndexReferenceFromSP has an out ref parameter for the stack |
| 1412 | // pointer register; pass a dummy that we ignore |
| 1413 | unsigned SPReg; |
| 1414 | int Offset = getFrameIndexReferenceFromSP(MF, Info.PSPSymFrameIdx, SPReg); |
| 1415 | assert(Offset >= 0); |
| 1416 | return static_cast<unsigned>(Offset); |
| 1417 | } |
| 1418 | |
| 1419 | unsigned |
| 1420 | X86FrameLowering::getWinEHFuncletFrameSize(const MachineFunction &MF) const { |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 1421 | // This is the size of the pushed CSRs. |
| 1422 | unsigned CSSize = |
| 1423 | MF.getInfo<X86MachineFunctionInfo>()->getCalleeSavedFrameSize(); |
| 1424 | // This is the amount of stack a funclet needs to allocate. |
Joseph Tremoulet | 149c433 | 2015-11-13 00:39:23 +0000 | [diff] [blame] | 1425 | unsigned UsedSize; |
| 1426 | EHPersonality Personality = |
| 1427 | classifyEHPersonality(MF.getFunction()->getPersonalityFn()); |
| 1428 | if (Personality == EHPersonality::CoreCLR) { |
| 1429 | // CLR funclets need to hold enough space to include the PSPSym, at the |
| 1430 | // same offset from the stack pointer (immediately after the prolog) as it |
| 1431 | // resides at in the main function. |
| 1432 | UsedSize = getPSPSlotOffsetFromSP(MF) + SlotSize; |
| 1433 | } else { |
| 1434 | // Other funclets just need enough stack for outgoing call arguments. |
| 1435 | UsedSize = MF.getFrameInfo()->getMaxCallFrameSize(); |
| 1436 | } |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 1437 | // RBP is not included in the callee saved register block. After pushing RBP, |
| 1438 | // everything is 16 byte aligned. Everything we allocate before an outgoing |
| 1439 | // call must also be 16 byte aligned. |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 1440 | unsigned FrameSizeMinusRBP = alignTo(CSSize + UsedSize, getStackAlignment()); |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 1441 | // Subtract out the size of the callee saved registers. This is how much stack |
| 1442 | // each funclet will allocate. |
| 1443 | return FrameSizeMinusRBP - CSSize; |
| 1444 | } |
| 1445 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1446 | void X86FrameLowering::emitEpilogue(MachineFunction &MF, |
| 1447 | MachineBasicBlock &MBB) const { |
| 1448 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1449 | X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 1450 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 1451 | DebugLoc DL; |
| 1452 | if (MBBI != MBB.end()) |
| 1453 | DL = MBBI->getDebugLoc(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1454 | // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1455 | const bool Is64BitILP32 = STI.isTarget64BitILP32(); |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1456 | unsigned FramePtr = TRI->getFrameRegister(MF); |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame] | 1457 | unsigned MachineFramePtr = |
Craig Topper | 91dab7b | 2015-12-25 22:09:45 +0000 | [diff] [blame] | 1458 | Is64BitILP32 ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1459 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1460 | bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); |
| 1461 | bool NeedsWinCFI = |
| 1462 | IsWin64Prologue && MF.getFunction()->needsUnwindTableEntry(); |
Reid Kleckner | 9779741 | 2015-10-07 23:55:01 +0000 | [diff] [blame] | 1463 | bool IsFunclet = isFuncletReturnInstr(MBBI); |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1464 | MachineBasicBlock *TargetMBB = nullptr; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1465 | |
| 1466 | // Get the number of bytes to allocate from the FrameInfo. |
| 1467 | uint64_t StackSize = MFI->getStackSize(); |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1468 | uint64_t MaxAlign = calculateMaxStackAlign(MF); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1469 | unsigned CSSize = X86FI->getCalleeSavedFrameSize(); |
| 1470 | uint64_t NumBytes = 0; |
| 1471 | |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 1472 | if (MBBI->getOpcode() == X86::CATCHRET) { |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1473 | // SEH shouldn't use catchret. |
| 1474 | assert(!isAsynchronousEHPersonality( |
| 1475 | classifyEHPersonality(MF.getFunction()->getPersonalityFn())) && |
| 1476 | "SEH should not use CATCHRET"); |
| 1477 | |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 1478 | NumBytes = getWinEHFuncletFrameSize(MF); |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 1479 | assert(hasFP(MF) && "EH funclets without FP not yet implemented"); |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1480 | TargetMBB = MBBI->getOperand(0).getMBB(); |
David Majnemer | 91b0ab9 | 2015-09-29 22:33:36 +0000 | [diff] [blame] | 1481 | |
Reid Kleckner | da6dcc5 | 2015-09-10 22:00:02 +0000 | [diff] [blame] | 1482 | // Pop EBP. |
| 1483 | BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 1484 | MachineFramePtr) |
| 1485 | .setMIFlag(MachineInstr::FrameDestroy); |
David Majnemer | 91b0ab9 | 2015-09-29 22:33:36 +0000 | [diff] [blame] | 1486 | } else if (MBBI->getOpcode() == X86::CLEANUPRET) { |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 1487 | NumBytes = getWinEHFuncletFrameSize(MF); |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 1488 | assert(hasFP(MF) && "EH funclets without FP not yet implemented"); |
| 1489 | BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), |
| 1490 | MachineFramePtr) |
| 1491 | .setMIFlag(MachineInstr::FrameDestroy); |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1492 | } else if (hasFP(MF)) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1493 | // Calculate required stack adjustment. |
| 1494 | uint64_t FrameSize = StackSize - SlotSize; |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1495 | NumBytes = FrameSize - CSSize; |
| 1496 | |
| 1497 | // Callee-saved registers were pushed on stack before the stack was |
| 1498 | // realigned. |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1499 | if (TRI->needsStackRealignment(MF) && !IsWin64Prologue) |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 1500 | NumBytes = alignTo(FrameSize, MaxAlign); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Pop EBP. |
| 1503 | BuildMI(MBB, MBBI, DL, |
Michael Kuperstein | 098cd9f | 2015-09-16 11:18:25 +0000 | [diff] [blame] | 1504 | TII.get(Is64Bit ? X86::POP64r : X86::POP32r), MachineFramePtr) |
| 1505 | .setMIFlag(MachineInstr::FrameDestroy); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1506 | } else { |
| 1507 | NumBytes = StackSize - CSSize; |
| 1508 | } |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1509 | uint64_t SEHStackAllocAmt = NumBytes; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Skip the callee-saved pop instructions. |
| 1512 | while (MBBI != MBB.begin()) { |
| 1513 | MachineBasicBlock::iterator PI = std::prev(MBBI); |
| 1514 | unsigned Opc = PI->getOpcode(); |
| 1515 | |
Michael Kuperstein | 098cd9f | 2015-09-16 11:18:25 +0000 | [diff] [blame] | 1516 | if ((Opc != X86::POP32r || !PI->getFlag(MachineInstr::FrameDestroy)) && |
| 1517 | (Opc != X86::POP64r || !PI->getFlag(MachineInstr::FrameDestroy)) && |
| 1518 | Opc != X86::DBG_VALUE && !PI->isTerminator()) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1519 | break; |
| 1520 | |
| 1521 | --MBBI; |
| 1522 | } |
| 1523 | MachineBasicBlock::iterator FirstCSPop = MBBI; |
| 1524 | |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1525 | if (TargetMBB) { |
David Majnemer | 35d27b2 | 2015-10-09 22:18:45 +0000 | [diff] [blame] | 1526 | // Fill EAX/RAX with the address of the target block. |
| 1527 | unsigned ReturnReg = STI.is64Bit() ? X86::RAX : X86::EAX; |
| 1528 | if (STI.is64Bit()) { |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1529 | // LEA64r TargetMBB(%rip), %rax |
David Majnemer | 35d27b2 | 2015-10-09 22:18:45 +0000 | [diff] [blame] | 1530 | BuildMI(MBB, FirstCSPop, DL, TII.get(X86::LEA64r), ReturnReg) |
| 1531 | .addReg(X86::RIP) |
| 1532 | .addImm(0) |
| 1533 | .addReg(0) |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1534 | .addMBB(TargetMBB) |
David Majnemer | 35d27b2 | 2015-10-09 22:18:45 +0000 | [diff] [blame] | 1535 | .addReg(0); |
| 1536 | } else { |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1537 | // MOV32ri $TargetMBB, %eax |
Reid Kleckner | 64b003f | 2015-11-09 21:04:00 +0000 | [diff] [blame] | 1538 | BuildMI(MBB, FirstCSPop, DL, TII.get(X86::MOV32ri), ReturnReg) |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1539 | .addMBB(TargetMBB); |
David Majnemer | 35d27b2 | 2015-10-09 22:18:45 +0000 | [diff] [blame] | 1540 | } |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1541 | // Record that we've taken the address of TargetMBB and no longer just |
Joseph Tremoulet | 3d0fbf1 | 2015-10-23 15:06:05 +0000 | [diff] [blame] | 1542 | // reference it in a terminator. |
Reid Kleckner | 51460c1 | 2015-11-06 01:49:05 +0000 | [diff] [blame] | 1543 | TargetMBB->setHasAddressTaken(); |
David Majnemer | 35d27b2 | 2015-10-09 22:18:45 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 1546 | if (MBBI != MBB.end()) |
| 1547 | DL = MBBI->getDebugLoc(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1548 | |
| 1549 | // If there is an ADD32ri or SUB32ri of ESP immediately before this |
| 1550 | // instruction, merge the two instructions. |
| 1551 | if (NumBytes || MFI->hasVarSizedObjects()) |
Michael Kuperstein | cba308c | 2015-07-28 08:56:13 +0000 | [diff] [blame] | 1552 | NumBytes += mergeSPUpdates(MBB, MBBI, true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1553 | |
| 1554 | // If dynamic alloca is used, then reset esp to point to the last callee-saved |
| 1555 | // slot before popping them off! Same applies for the case, when stack was |
Reid Kleckner | 9779741 | 2015-10-07 23:55:01 +0000 | [diff] [blame] | 1556 | // realigned. Don't do this if this was a funclet epilogue, since the funclets |
| 1557 | // will not do realignment or dynamic stack allocation. |
| 1558 | if ((TRI->needsStackRealignment(MF) || MFI->hasVarSizedObjects()) && |
| 1559 | !IsFunclet) { |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1560 | if (TRI->needsStackRealignment(MF)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1561 | MBBI = FirstCSPop; |
David Majnemer | e1bbad9 | 2015-02-25 21:13:37 +0000 | [diff] [blame] | 1562 | unsigned SEHFrameOffset = calculateSetFPREG(SEHStackAllocAmt); |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1563 | uint64_t LEAAmount = |
| 1564 | IsWin64Prologue ? SEHStackAllocAmt - SEHFrameOffset : -CSSize; |
David Majnemer | e1bbad9 | 2015-02-25 21:13:37 +0000 | [diff] [blame] | 1565 | |
| 1566 | // There are only two legal forms of epilogue: |
| 1567 | // - add SEHAllocationSize, %rsp |
| 1568 | // - lea SEHAllocationSize(%FramePtr), %rsp |
| 1569 | // |
| 1570 | // 'mov %FramePtr, %rsp' will not be recognized as an epilogue sequence. |
| 1571 | // However, we may use this sequence if we have a frame pointer because the |
| 1572 | // effects of the prologue can safely be undone. |
| 1573 | if (LEAAmount != 0) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1574 | unsigned Opc = getLEArOpcode(Uses64BitFramePtr); |
| 1575 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr), |
David Majnemer | e1bbad9 | 2015-02-25 21:13:37 +0000 | [diff] [blame] | 1576 | FramePtr, false, LEAAmount); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1577 | --MBBI; |
| 1578 | } else { |
| 1579 | unsigned Opc = (Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr); |
| 1580 | BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) |
| 1581 | .addReg(FramePtr); |
| 1582 | --MBBI; |
| 1583 | } |
| 1584 | } else if (NumBytes) { |
| 1585 | // Adjust stack pointer back: ESP += numbytes. |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 1586 | emitSPUpdate(MBB, MBBI, NumBytes, /*InEpilogue=*/true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1587 | --MBBI; |
| 1588 | } |
| 1589 | |
| 1590 | // Windows unwinder will not invoke function's exception handler if IP is |
| 1591 | // either in prologue or in epilogue. This behavior causes a problem when a |
| 1592 | // call immediately precedes an epilogue, because the return address points |
| 1593 | // into the epilogue. To cope with that, we insert an epilogue marker here, |
| 1594 | // then replace it with a 'nop' if it ends up immediately after a CALL in the |
| 1595 | // final emitted code. |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1596 | if (NeedsWinCFI) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1597 | BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue)); |
| 1598 | |
Quentin Colombet | 494eb60 | 2015-05-22 18:10:47 +0000 | [diff] [blame] | 1599 | // Add the return addr area delta back since we are not tail calling. |
| 1600 | int Offset = -1 * X86FI->getTCReturnAddrDelta(); |
| 1601 | assert(Offset >= 0 && "TCDelta should never be positive"); |
| 1602 | if (Offset) { |
| 1603 | MBBI = MBB.getFirstTerminator(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1604 | |
| 1605 | // Check for possible merge with preceding ADD instruction. |
Reid Kleckner | 3854f7b | 2015-06-18 18:03:25 +0000 | [diff] [blame] | 1606 | Offset += mergeSPUpdates(MBB, MBBI, true); |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 1607 | emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1608 | } |
| 1609 | } |
| 1610 | |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 1611 | // NOTE: this only has a subset of the full frame index logic. In |
| 1612 | // particular, the FI < 0 and AfterFPPop logic is handled in |
| 1613 | // X86RegisterInfo::eliminateFrameIndex, but not here. Possibly |
| 1614 | // (probably?) it should be moved into here. |
| 1615 | int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
| 1616 | unsigned &FrameReg) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1617 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 1618 | |
| 1619 | // We can't calculate offset from frame pointer if the stack is realigned, |
| 1620 | // so enforce usage of stack/base pointer. The base pointer is used when we |
| 1621 | // have dynamic allocas in addition to dynamic realignment. |
| 1622 | if (TRI->hasBasePointer(MF)) |
| 1623 | FrameReg = TRI->getBaseRegister(); |
| 1624 | else if (TRI->needsStackRealignment(MF)) |
| 1625 | FrameReg = TRI->getStackRegister(); |
| 1626 | else |
| 1627 | FrameReg = TRI->getFrameRegister(MF); |
| 1628 | |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1629 | // Offset will hold the offset from the stack pointer at function entry to the |
| 1630 | // object. |
| 1631 | // We need to factor in additional offsets applied during the prologue to the |
| 1632 | // frame, base, and stack pointer depending on which is used. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1633 | int Offset = MFI->getObjectOffset(FI) - getOffsetOfLocalArea(); |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1634 | const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
| 1635 | unsigned CSSize = X86FI->getCalleeSavedFrameSize(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1636 | uint64_t StackSize = MFI->getStackSize(); |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1637 | bool HasFP = hasFP(MF); |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1638 | bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1639 | int64_t FPDelta = 0; |
| 1640 | |
Reid Kleckner | 1c140bd | 2015-06-16 18:08:57 +0000 | [diff] [blame] | 1641 | if (IsWin64Prologue) { |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1642 | assert(!MFI->hasCalls() || (StackSize % 16) == 8); |
| 1643 | |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1644 | // Calculate required stack adjustment. |
| 1645 | uint64_t FrameSize = StackSize - SlotSize; |
| 1646 | // If required, include space for extra hidden slot for stashing base pointer. |
| 1647 | if (X86FI->getRestoreBasePointer()) |
| 1648 | FrameSize += SlotSize; |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1649 | uint64_t NumBytes = FrameSize - CSSize; |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1650 | |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1651 | uint64_t SEHFrameOffset = calculateSetFPREG(NumBytes); |
David Majnemer | 13d0b11 | 2015-02-10 21:22:05 +0000 | [diff] [blame] | 1652 | if (FI && FI == X86FI->getFAIndex()) |
| 1653 | return -SEHFrameOffset; |
| 1654 | |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1655 | // FPDelta is the offset from the "traditional" FP location of the old base |
| 1656 | // pointer followed by return address and the location required by the |
| 1657 | // restricted Win64 prologue. |
| 1658 | // Add FPDelta to all offsets below that go through the frame pointer. |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1659 | FPDelta = FrameSize - SEHFrameOffset; |
| 1660 | assert((!MFI->hasCalls() || (FPDelta % 16) == 0) && |
| 1661 | "FPDelta isn't aligned per the Win64 ABI!"); |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1664 | |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1665 | if (TRI->hasBasePointer(MF)) { |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1666 | assert(HasFP && "VLAs and dynamic stack realign, but no FP?!"); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1667 | if (FI < 0) { |
| 1668 | // Skip the saved EBP. |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1669 | return Offset + SlotSize + FPDelta; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1670 | } else { |
| 1671 | assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0); |
| 1672 | return Offset + StackSize; |
| 1673 | } |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1674 | } else if (TRI->needsStackRealignment(MF)) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1675 | if (FI < 0) { |
| 1676 | // Skip the saved EBP. |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1677 | return Offset + SlotSize + FPDelta; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1678 | } else { |
| 1679 | assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0); |
| 1680 | return Offset + StackSize; |
| 1681 | } |
| 1682 | // FIXME: Support tail calls |
| 1683 | } else { |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1684 | if (!HasFP) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1685 | return Offset + StackSize; |
| 1686 | |
| 1687 | // Skip the saved EBP. |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 1688 | Offset += SlotSize; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1689 | |
| 1690 | // Skip the RETADDR move area |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1691 | int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); |
| 1692 | if (TailCallReturnAddrDelta < 0) |
| 1693 | Offset -= TailCallReturnAddrDelta; |
| 1694 | } |
| 1695 | |
David Majnemer | 89d0564 | 2015-02-21 01:04:47 +0000 | [diff] [blame] | 1696 | return Offset + FPDelta; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 1699 | // Simplified from getFrameIndexReference keeping only StackPointer cases |
| 1700 | int X86FrameLowering::getFrameIndexReferenceFromSP(const MachineFunction &MF, |
| 1701 | int FI, |
| 1702 | unsigned &FrameReg) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1703 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1704 | // Does not include any dynamic realign. |
| 1705 | const uint64_t StackSize = MFI->getStackSize(); |
| 1706 | { |
| 1707 | #ifndef NDEBUG |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1708 | // LLVM arranges the stack as follows: |
| 1709 | // ... |
| 1710 | // ARG2 |
| 1711 | // ARG1 |
| 1712 | // RETADDR |
| 1713 | // PUSH RBP <-- RBP points here |
| 1714 | // PUSH CSRs |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 1715 | // ~~~~~~~ <-- possible stack realignment (non-win64) |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1716 | // ... |
| 1717 | // STACK OBJECTS |
| 1718 | // ... <-- RSP after prologue points here |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 1719 | // ~~~~~~~ <-- possible stack realignment (win64) |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1720 | // |
| 1721 | // if (hasVarSizedObjects()): |
| 1722 | // ... <-- "base pointer" (ESI/RBX) points here |
| 1723 | // DYNAMIC ALLOCAS |
| 1724 | // ... <-- RSP points here |
| 1725 | // |
| 1726 | // Case 1: In the simple case of no stack realignment and no dynamic |
| 1727 | // allocas, both "fixed" stack objects (arguments and CSRs) are addressable |
| 1728 | // with fixed offsets from RSP. |
| 1729 | // |
| 1730 | // Case 2: In the case of stack realignment with no dynamic allocas, fixed |
| 1731 | // stack objects are addressed with RBP and regular stack objects with RSP. |
| 1732 | // |
| 1733 | // Case 3: In the case of dynamic allocas and stack realignment, RSP is used |
| 1734 | // to address stack arguments for outgoing calls and nothing else. The "base |
| 1735 | // pointer" points to local variables, and RBP points to fixed objects. |
| 1736 | // |
| 1737 | // In cases 2 and 3, we can only answer for non-fixed stack objects, and the |
| 1738 | // answer we give is relative to the SP after the prologue, and not the |
| 1739 | // SP in the middle of the function. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1740 | |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 1741 | assert((!MFI->isFixedObjectIndex(FI) || !TRI->needsStackRealignment(MF) || |
| 1742 | STI.isTargetWin64()) && |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1743 | "offset from fixed object to SP is not static"); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1744 | |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 1745 | // We don't handle tail calls, and shouldn't be seeing them either. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1746 | int TailCallReturnAddrDelta = |
| 1747 | MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta(); |
| 1748 | assert(!(TailCallReturnAddrDelta < 0) && "we don't handle this case!"); |
| 1749 | #endif |
| 1750 | } |
| 1751 | |
James Y Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 1752 | // Fill in FrameReg output argument. |
| 1753 | FrameReg = TRI->getStackRegister(); |
| 1754 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1755 | // This is how the math works out: |
| 1756 | // |
| 1757 | // %rsp grows (i.e. gets lower) left to right. Each box below is |
| 1758 | // one word (eight bytes). Obj0 is the stack slot we're trying to |
| 1759 | // get to. |
| 1760 | // |
| 1761 | // ---------------------------------- |
| 1762 | // | BP | Obj0 | Obj1 | ... | ObjN | |
| 1763 | // ---------------------------------- |
| 1764 | // ^ ^ ^ ^ |
| 1765 | // A B C E |
| 1766 | // |
| 1767 | // A is the incoming stack pointer. |
| 1768 | // (B - A) is the local area offset (-8 for x86-64) [1] |
| 1769 | // (C - A) is the Offset returned by MFI->getObjectOffset for Obj0 [2] |
| 1770 | // |
| 1771 | // |(E - B)| is the StackSize (absolute value, positive). For a |
| 1772 | // stack that grown down, this works out to be (B - E). [3] |
| 1773 | // |
| 1774 | // E is also the value of %rsp after stack has been set up, and we |
| 1775 | // want (C - E) -- the value we can add to %rsp to get to Obj0. Now |
| 1776 | // (C - E) == (C - A) - (B - A) + (B - E) |
| 1777 | // { Using [1], [2] and [3] above } |
| 1778 | // == getObjectOffset - LocalAreaOffset + StackSize |
| 1779 | // |
| 1780 | |
| 1781 | // Get the Offset from the StackPointer |
| 1782 | int Offset = MFI->getObjectOffset(FI) - getOffsetOfLocalArea(); |
| 1783 | |
| 1784 | return Offset + StackSize; |
| 1785 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1786 | |
| 1787 | bool X86FrameLowering::assignCalleeSavedSpillSlots( |
| 1788 | MachineFunction &MF, const TargetRegisterInfo *TRI, |
| 1789 | std::vector<CalleeSavedInfo> &CSI) const { |
| 1790 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1791 | X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
| 1792 | |
| 1793 | unsigned CalleeSavedFrameSize = 0; |
| 1794 | int SpillSlotOffset = getOffsetOfLocalArea() + X86FI->getTCReturnAddrDelta(); |
| 1795 | |
| 1796 | if (hasFP(MF)) { |
| 1797 | // emitPrologue always spills frame register the first thing. |
| 1798 | SpillSlotOffset -= SlotSize; |
| 1799 | MFI->CreateFixedSpillStackObject(SlotSize, SpillSlotOffset); |
| 1800 | |
| 1801 | // Since emitPrologue and emitEpilogue will handle spilling and restoring of |
| 1802 | // the frame register, we can delete it from CSI list and not have to worry |
| 1803 | // about avoiding it later. |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1804 | unsigned FPReg = TRI->getFrameRegister(MF); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1805 | for (unsigned i = 0; i < CSI.size(); ++i) { |
| 1806 | if (TRI->regsOverlap(CSI[i].getReg(),FPReg)) { |
| 1807 | CSI.erase(CSI.begin() + i); |
| 1808 | break; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | // Assign slots for GPRs. It increases frame size. |
| 1814 | for (unsigned i = CSI.size(); i != 0; --i) { |
| 1815 | unsigned Reg = CSI[i - 1].getReg(); |
| 1816 | |
| 1817 | if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg)) |
| 1818 | continue; |
| 1819 | |
| 1820 | SpillSlotOffset -= SlotSize; |
| 1821 | CalleeSavedFrameSize += SlotSize; |
| 1822 | |
| 1823 | int SlotIndex = MFI->CreateFixedSpillStackObject(SlotSize, SpillSlotOffset); |
| 1824 | CSI[i - 1].setFrameIdx(SlotIndex); |
| 1825 | } |
| 1826 | |
| 1827 | X86FI->setCalleeSavedFrameSize(CalleeSavedFrameSize); |
| 1828 | |
| 1829 | // Assign slots for XMMs. |
| 1830 | for (unsigned i = CSI.size(); i != 0; --i) { |
| 1831 | unsigned Reg = CSI[i - 1].getReg(); |
| 1832 | if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg)) |
| 1833 | continue; |
| 1834 | |
Reid Kleckner | 034ea96 | 2015-06-18 20:32:02 +0000 | [diff] [blame] | 1835 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1836 | // ensure alignment |
| 1837 | SpillSlotOffset -= std::abs(SpillSlotOffset) % RC->getAlignment(); |
| 1838 | // spill into slot |
| 1839 | SpillSlotOffset -= RC->getSize(); |
| 1840 | int SlotIndex = |
| 1841 | MFI->CreateFixedSpillStackObject(RC->getSize(), SpillSlotOffset); |
| 1842 | CSI[i - 1].setFrameIdx(SlotIndex); |
| 1843 | MFI->ensureMaxAlignment(RC->getAlignment()); |
| 1844 | } |
| 1845 | |
| 1846 | return true; |
| 1847 | } |
| 1848 | |
| 1849 | bool X86FrameLowering::spillCalleeSavedRegisters( |
| 1850 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 1851 | const std::vector<CalleeSavedInfo> &CSI, |
| 1852 | const TargetRegisterInfo *TRI) const { |
| 1853 | DebugLoc DL = MBB.findDebugLoc(MI); |
| 1854 | |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1855 | // Don't save CSRs in 32-bit EH funclets. The caller saves EBX, EBP, ESI, EDI |
| 1856 | // for us, and there are no XMM CSRs on Win32. |
| 1857 | if (MBB.isEHFuncletEntry() && STI.is32Bit() && STI.isOSWindows()) |
| 1858 | return true; |
| 1859 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1860 | // Push GPRs. It increases frame size. |
| 1861 | unsigned Opc = STI.is64Bit() ? X86::PUSH64r : X86::PUSH32r; |
| 1862 | for (unsigned i = CSI.size(); i != 0; --i) { |
| 1863 | unsigned Reg = CSI[i - 1].getReg(); |
| 1864 | |
| 1865 | if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg)) |
| 1866 | continue; |
| 1867 | // Add the callee-saved register as live-in. It's killed at the spill. |
| 1868 | MBB.addLiveIn(Reg); |
| 1869 | |
| 1870 | BuildMI(MBB, MI, DL, TII.get(Opc)).addReg(Reg, RegState::Kill) |
| 1871 | .setMIFlag(MachineInstr::FrameSetup); |
| 1872 | } |
| 1873 | |
| 1874 | // Make XMM regs spilled. X86 does not have ability of push/pop XMM. |
| 1875 | // It can be done by spilling XMMs to stack frame. |
| 1876 | for (unsigned i = CSI.size(); i != 0; --i) { |
| 1877 | unsigned Reg = CSI[i-1].getReg(); |
David Majnemer | a7d908e | 2015-02-10 19:01:47 +0000 | [diff] [blame] | 1878 | if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg)) |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1879 | continue; |
| 1880 | // Add the callee-saved register as live-in. It's killed at the spill. |
| 1881 | MBB.addLiveIn(Reg); |
| 1882 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); |
| 1883 | |
| 1884 | TII.storeRegToStackSlot(MBB, MI, Reg, true, CSI[i - 1].getFrameIdx(), RC, |
| 1885 | TRI); |
| 1886 | --MI; |
| 1887 | MI->setFlag(MachineInstr::FrameSetup); |
| 1888 | ++MI; |
| 1889 | } |
| 1890 | |
| 1891 | return true; |
| 1892 | } |
| 1893 | |
| 1894 | bool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 1895 | MachineBasicBlock::iterator MI, |
| 1896 | const std::vector<CalleeSavedInfo> &CSI, |
| 1897 | const TargetRegisterInfo *TRI) const { |
| 1898 | if (CSI.empty()) |
| 1899 | return false; |
| 1900 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 1901 | if (isFuncletReturnInstr(MI) && STI.isOSWindows()) { |
| 1902 | // Don't restore CSRs in 32-bit EH funclets. Matches |
| 1903 | // spillCalleeSavedRegisters. |
| 1904 | if (STI.is32Bit()) |
| 1905 | return true; |
| 1906 | // Don't restore CSRs before an SEH catchret. SEH except blocks do not form |
| 1907 | // funclets. emitEpilogue transforms these to normal jumps. |
| 1908 | if (MI->getOpcode() == X86::CATCHRET) { |
| 1909 | const Function *Func = MBB.getParent()->getFunction(); |
| 1910 | bool IsSEH = isAsynchronousEHPersonality( |
| 1911 | classifyEHPersonality(Func->getPersonalityFn())); |
| 1912 | if (IsSEH) |
| 1913 | return true; |
| 1914 | } |
| 1915 | } |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1916 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1917 | DebugLoc DL = MBB.findDebugLoc(MI); |
| 1918 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1919 | // Reload XMMs from stack frame. |
| 1920 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1921 | unsigned Reg = CSI[i].getReg(); |
| 1922 | if (X86::GR64RegClass.contains(Reg) || |
| 1923 | X86::GR32RegClass.contains(Reg)) |
| 1924 | continue; |
| 1925 | |
| 1926 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); |
| 1927 | TII.loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RC, TRI); |
| 1928 | } |
| 1929 | |
| 1930 | // POP GPRs. |
| 1931 | unsigned Opc = STI.is64Bit() ? X86::POP64r : X86::POP32r; |
| 1932 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1933 | unsigned Reg = CSI[i].getReg(); |
| 1934 | if (!X86::GR64RegClass.contains(Reg) && |
| 1935 | !X86::GR32RegClass.contains(Reg)) |
| 1936 | continue; |
| 1937 | |
Michael Kuperstein | 098cd9f | 2015-09-16 11:18:25 +0000 | [diff] [blame] | 1938 | BuildMI(MBB, MI, DL, TII.get(Opc), Reg) |
| 1939 | .setMIFlag(MachineInstr::FrameDestroy); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1940 | } |
| 1941 | return true; |
| 1942 | } |
| 1943 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1944 | void X86FrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 1945 | BitVector &SavedRegs, |
| 1946 | RegScavenger *RS) const { |
| 1947 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 1948 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1949 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1950 | |
| 1951 | X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
| 1952 | int64_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); |
| 1953 | |
| 1954 | if (TailCallReturnAddrDelta < 0) { |
| 1955 | // create RETURNADDR area |
| 1956 | // arg |
| 1957 | // arg |
| 1958 | // RETADDR |
| 1959 | // { ... |
| 1960 | // RETADDR area |
| 1961 | // ... |
| 1962 | // } |
| 1963 | // [EBP] |
| 1964 | MFI->CreateFixedObject(-TailCallReturnAddrDelta, |
| 1965 | TailCallReturnAddrDelta - SlotSize, true); |
| 1966 | } |
| 1967 | |
| 1968 | // Spill the BasePtr if it's used. |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1969 | if (TRI->hasBasePointer(MF)) { |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1970 | SavedRegs.set(TRI->getBaseRegister()); |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 1971 | |
| 1972 | // Allocate a spill slot for EBP if we have a base pointer and EH funclets. |
| 1973 | if (MF.getMMI().hasEHFunclets()) { |
| 1974 | int FI = MFI->CreateSpillStackObject(SlotSize, SlotSize); |
| 1975 | X86FI->setHasSEHFramePtrSave(true); |
| 1976 | X86FI->setSEHFramePtrSaveIndex(FI); |
| 1977 | } |
| 1978 | } |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | static bool |
| 1982 | HasNestArgument(const MachineFunction *MF) { |
| 1983 | const Function *F = MF->getFunction(); |
| 1984 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1985 | I != E; I++) { |
| 1986 | if (I->hasNestAttr()) |
| 1987 | return true; |
| 1988 | } |
| 1989 | return false; |
| 1990 | } |
| 1991 | |
| 1992 | /// GetScratchRegister - Get a temp register for performing work in the |
| 1993 | /// segmented stack and the Erlang/HiPE stack prologue. Depending on platform |
| 1994 | /// and the properties of the function either one or two registers will be |
| 1995 | /// needed. Set primary to true for the first register, false for the second. |
| 1996 | static unsigned |
| 1997 | GetScratchRegister(bool Is64Bit, bool IsLP64, const MachineFunction &MF, bool Primary) { |
| 1998 | CallingConv::ID CallingConvention = MF.getFunction()->getCallingConv(); |
| 1999 | |
| 2000 | // Erlang stuff. |
| 2001 | if (CallingConvention == CallingConv::HiPE) { |
| 2002 | if (Is64Bit) |
| 2003 | return Primary ? X86::R14 : X86::R13; |
| 2004 | else |
| 2005 | return Primary ? X86::EBX : X86::EDI; |
| 2006 | } |
| 2007 | |
| 2008 | if (Is64Bit) { |
| 2009 | if (IsLP64) |
| 2010 | return Primary ? X86::R11 : X86::R12; |
| 2011 | else |
| 2012 | return Primary ? X86::R11D : X86::R12D; |
| 2013 | } |
| 2014 | |
| 2015 | bool IsNested = HasNestArgument(&MF); |
| 2016 | |
| 2017 | if (CallingConvention == CallingConv::X86_FastCall || |
| 2018 | CallingConvention == CallingConv::Fast) { |
| 2019 | if (IsNested) |
| 2020 | report_fatal_error("Segmented stacks does not support fastcall with " |
| 2021 | "nested function."); |
| 2022 | return Primary ? X86::EAX : X86::ECX; |
| 2023 | } |
| 2024 | if (IsNested) |
| 2025 | return Primary ? X86::EDX : X86::EAX; |
| 2026 | return Primary ? X86::ECX : X86::EAX; |
| 2027 | } |
| 2028 | |
| 2029 | // The stack limit in the TCB is set to this many bytes above the actual stack |
| 2030 | // limit. |
| 2031 | static const uint64_t kSplitStackAvailable = 256; |
| 2032 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 2033 | void X86FrameLowering::adjustForSegmentedStacks( |
| 2034 | MachineFunction &MF, MachineBasicBlock &PrologueMBB) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2035 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2036 | uint64_t StackSize; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2037 | unsigned TlsReg, TlsOffset; |
| 2038 | DebugLoc DL; |
| 2039 | |
Quentin Colombet | 4cf5691 | 2016-01-19 23:29:03 +0000 | [diff] [blame] | 2040 | // To support shrink-wrapping we would need to insert the new blocks |
| 2041 | // at the right place and update the branches to PrologueMBB. |
| 2042 | assert(&(*MF.begin()) == &PrologueMBB && "Shrink-wrapping not supported yet"); |
| 2043 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2044 | unsigned ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true); |
| 2045 | assert(!MF.getRegInfo().isLiveIn(ScratchReg) && |
| 2046 | "Scratch register is live-in"); |
| 2047 | |
| 2048 | if (MF.getFunction()->isVarArg()) |
| 2049 | report_fatal_error("Segmented stacks do not support vararg functions."); |
| 2050 | if (!STI.isTargetLinux() && !STI.isTargetDarwin() && !STI.isTargetWin32() && |
| 2051 | !STI.isTargetWin64() && !STI.isTargetFreeBSD() && |
| 2052 | !STI.isTargetDragonFly()) |
| 2053 | report_fatal_error("Segmented stacks not supported on this platform."); |
| 2054 | |
| 2055 | // Eventually StackSize will be calculated by a link-time pass; which will |
| 2056 | // also decide whether checking code needs to be injected into this particular |
| 2057 | // prologue. |
| 2058 | StackSize = MFI->getStackSize(); |
| 2059 | |
| 2060 | // Do not generate a prologue for functions with a stack of size zero |
| 2061 | if (StackSize == 0) |
| 2062 | return; |
| 2063 | |
| 2064 | MachineBasicBlock *allocMBB = MF.CreateMachineBasicBlock(); |
| 2065 | MachineBasicBlock *checkMBB = MF.CreateMachineBasicBlock(); |
| 2066 | X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
| 2067 | bool IsNested = false; |
| 2068 | |
| 2069 | // We need to know if the function has a nest argument only in 64 bit mode. |
| 2070 | if (Is64Bit) |
| 2071 | IsNested = HasNestArgument(&MF); |
| 2072 | |
| 2073 | // The MOV R10, RAX needs to be in a different block, since the RET we emit in |
| 2074 | // allocMBB needs to be last (terminating) instruction. |
| 2075 | |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 2076 | for (const auto &LI : PrologueMBB.liveins()) { |
Matthias Braun | b2b7ef1 | 2015-08-24 22:59:52 +0000 | [diff] [blame] | 2077 | allocMBB->addLiveIn(LI); |
| 2078 | checkMBB->addLiveIn(LI); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | if (IsNested) |
| 2082 | allocMBB->addLiveIn(IsLP64 ? X86::R10 : X86::R10D); |
| 2083 | |
| 2084 | MF.push_front(allocMBB); |
| 2085 | MF.push_front(checkMBB); |
| 2086 | |
| 2087 | // When the frame size is less than 256 we just compare the stack |
| 2088 | // boundary directly to the value of the stack pointer, per gcc. |
| 2089 | bool CompareStackPointer = StackSize < kSplitStackAvailable; |
| 2090 | |
| 2091 | // Read the limit off the current stacklet off the stack_guard location. |
| 2092 | if (Is64Bit) { |
| 2093 | if (STI.isTargetLinux()) { |
| 2094 | TlsReg = X86::FS; |
| 2095 | TlsOffset = IsLP64 ? 0x70 : 0x40; |
| 2096 | } else if (STI.isTargetDarwin()) { |
| 2097 | TlsReg = X86::GS; |
| 2098 | TlsOffset = 0x60 + 90*8; // See pthread_machdep.h. Steal TLS slot 90. |
| 2099 | } else if (STI.isTargetWin64()) { |
| 2100 | TlsReg = X86::GS; |
| 2101 | TlsOffset = 0x28; // pvArbitrary, reserved for application use |
| 2102 | } else if (STI.isTargetFreeBSD()) { |
| 2103 | TlsReg = X86::FS; |
| 2104 | TlsOffset = 0x18; |
| 2105 | } else if (STI.isTargetDragonFly()) { |
| 2106 | TlsReg = X86::FS; |
| 2107 | TlsOffset = 0x20; // use tls_tcb.tcb_segstack |
| 2108 | } else { |
| 2109 | report_fatal_error("Segmented stacks not supported on this platform."); |
| 2110 | } |
| 2111 | |
| 2112 | if (CompareStackPointer) |
| 2113 | ScratchReg = IsLP64 ? X86::RSP : X86::ESP; |
| 2114 | else |
| 2115 | BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::LEA64r : X86::LEA64_32r), ScratchReg).addReg(X86::RSP) |
| 2116 | .addImm(1).addReg(0).addImm(-StackSize).addReg(0); |
| 2117 | |
| 2118 | BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::CMP64rm : X86::CMP32rm)).addReg(ScratchReg) |
| 2119 | .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg); |
| 2120 | } else { |
| 2121 | if (STI.isTargetLinux()) { |
| 2122 | TlsReg = X86::GS; |
| 2123 | TlsOffset = 0x30; |
| 2124 | } else if (STI.isTargetDarwin()) { |
| 2125 | TlsReg = X86::GS; |
| 2126 | TlsOffset = 0x48 + 90*4; |
| 2127 | } else if (STI.isTargetWin32()) { |
| 2128 | TlsReg = X86::FS; |
| 2129 | TlsOffset = 0x14; // pvArbitrary, reserved for application use |
| 2130 | } else if (STI.isTargetDragonFly()) { |
| 2131 | TlsReg = X86::FS; |
| 2132 | TlsOffset = 0x10; // use tls_tcb.tcb_segstack |
| 2133 | } else if (STI.isTargetFreeBSD()) { |
| 2134 | report_fatal_error("Segmented stacks not supported on FreeBSD i386."); |
| 2135 | } else { |
| 2136 | report_fatal_error("Segmented stacks not supported on this platform."); |
| 2137 | } |
| 2138 | |
| 2139 | if (CompareStackPointer) |
| 2140 | ScratchReg = X86::ESP; |
| 2141 | else |
| 2142 | BuildMI(checkMBB, DL, TII.get(X86::LEA32r), ScratchReg).addReg(X86::ESP) |
| 2143 | .addImm(1).addReg(0).addImm(-StackSize).addReg(0); |
| 2144 | |
| 2145 | if (STI.isTargetLinux() || STI.isTargetWin32() || STI.isTargetWin64() || |
| 2146 | STI.isTargetDragonFly()) { |
| 2147 | BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)).addReg(ScratchReg) |
| 2148 | .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg); |
| 2149 | } else if (STI.isTargetDarwin()) { |
| 2150 | |
| 2151 | // TlsOffset doesn't fit into a mod r/m byte so we need an extra register. |
| 2152 | unsigned ScratchReg2; |
| 2153 | bool SaveScratch2; |
| 2154 | if (CompareStackPointer) { |
| 2155 | // The primary scratch register is available for holding the TLS offset. |
| 2156 | ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, true); |
| 2157 | SaveScratch2 = false; |
| 2158 | } else { |
| 2159 | // Need to use a second register to hold the TLS offset |
| 2160 | ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, false); |
| 2161 | |
| 2162 | // Unfortunately, with fastcc the second scratch register may hold an |
| 2163 | // argument. |
| 2164 | SaveScratch2 = MF.getRegInfo().isLiveIn(ScratchReg2); |
| 2165 | } |
| 2166 | |
| 2167 | // If Scratch2 is live-in then it needs to be saved. |
| 2168 | assert((!MF.getRegInfo().isLiveIn(ScratchReg2) || SaveScratch2) && |
| 2169 | "Scratch register is live-in and not saved"); |
| 2170 | |
| 2171 | if (SaveScratch2) |
| 2172 | BuildMI(checkMBB, DL, TII.get(X86::PUSH32r)) |
| 2173 | .addReg(ScratchReg2, RegState::Kill); |
| 2174 | |
| 2175 | BuildMI(checkMBB, DL, TII.get(X86::MOV32ri), ScratchReg2) |
| 2176 | .addImm(TlsOffset); |
| 2177 | BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)) |
| 2178 | .addReg(ScratchReg) |
| 2179 | .addReg(ScratchReg2).addImm(1).addReg(0) |
| 2180 | .addImm(0) |
| 2181 | .addReg(TlsReg); |
| 2182 | |
| 2183 | if (SaveScratch2) |
| 2184 | BuildMI(checkMBB, DL, TII.get(X86::POP32r), ScratchReg2); |
| 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | // This jump is taken if SP >= (Stacklet Limit + Stack Space required). |
| 2189 | // It jumps to normal execution of the function body. |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 2190 | BuildMI(checkMBB, DL, TII.get(X86::JA_1)).addMBB(&PrologueMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2191 | |
| 2192 | // On 32 bit we first push the arguments size and then the frame size. On 64 |
| 2193 | // bit, we pass the stack frame size in r10 and the argument size in r11. |
| 2194 | if (Is64Bit) { |
| 2195 | // Functions with nested arguments use R10, so it needs to be saved across |
| 2196 | // the call to _morestack |
| 2197 | |
| 2198 | const unsigned RegAX = IsLP64 ? X86::RAX : X86::EAX; |
| 2199 | const unsigned Reg10 = IsLP64 ? X86::R10 : X86::R10D; |
| 2200 | const unsigned Reg11 = IsLP64 ? X86::R11 : X86::R11D; |
| 2201 | const unsigned MOVrr = IsLP64 ? X86::MOV64rr : X86::MOV32rr; |
| 2202 | const unsigned MOVri = IsLP64 ? X86::MOV64ri : X86::MOV32ri; |
| 2203 | |
| 2204 | if (IsNested) |
| 2205 | BuildMI(allocMBB, DL, TII.get(MOVrr), RegAX).addReg(Reg10); |
| 2206 | |
| 2207 | BuildMI(allocMBB, DL, TII.get(MOVri), Reg10) |
| 2208 | .addImm(StackSize); |
| 2209 | BuildMI(allocMBB, DL, TII.get(MOVri), Reg11) |
| 2210 | .addImm(X86FI->getArgumentStackSize()); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2211 | } else { |
| 2212 | BuildMI(allocMBB, DL, TII.get(X86::PUSHi32)) |
| 2213 | .addImm(X86FI->getArgumentStackSize()); |
| 2214 | BuildMI(allocMBB, DL, TII.get(X86::PUSHi32)) |
| 2215 | .addImm(StackSize); |
| 2216 | } |
| 2217 | |
| 2218 | // __morestack is in libgcc |
| 2219 | if (Is64Bit && MF.getTarget().getCodeModel() == CodeModel::Large) { |
| 2220 | // Under the large code model, we cannot assume that __morestack lives |
| 2221 | // within 2^31 bytes of the call site, so we cannot use pc-relative |
| 2222 | // addressing. We cannot perform the call via a temporary register, |
| 2223 | // as the rax register may be used to store the static chain, and all |
| 2224 | // other suitable registers may be either callee-save or used for |
| 2225 | // parameter passing. We cannot use the stack at this point either |
| 2226 | // because __morestack manipulates the stack directly. |
| 2227 | // |
| 2228 | // To avoid these issues, perform an indirect call via a read-only memory |
| 2229 | // location containing the address. |
| 2230 | // |
| 2231 | // This solution is not perfect, as it assumes that the .rodata section |
| 2232 | // is laid out within 2^31 bytes of each function body, but this seems |
| 2233 | // to be sufficient for JIT. |
| 2234 | BuildMI(allocMBB, DL, TII.get(X86::CALL64m)) |
| 2235 | .addReg(X86::RIP) |
| 2236 | .addImm(0) |
| 2237 | .addReg(0) |
| 2238 | .addExternalSymbol("__morestack_addr") |
| 2239 | .addReg(0); |
| 2240 | MF.getMMI().setUsesMorestackAddr(true); |
| 2241 | } else { |
| 2242 | if (Is64Bit) |
| 2243 | BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32)) |
| 2244 | .addExternalSymbol("__morestack"); |
| 2245 | else |
| 2246 | BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32)) |
| 2247 | .addExternalSymbol("__morestack"); |
| 2248 | } |
| 2249 | |
| 2250 | if (IsNested) |
| 2251 | BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET_RESTORE_R10)); |
| 2252 | else |
| 2253 | BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET)); |
| 2254 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 2255 | allocMBB->addSuccessor(&PrologueMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2256 | |
| 2257 | checkMBB->addSuccessor(allocMBB); |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 2258 | checkMBB->addSuccessor(&PrologueMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2259 | |
| 2260 | #ifdef XDEBUG |
| 2261 | MF.verify(); |
| 2262 | #endif |
| 2263 | } |
| 2264 | |
| 2265 | /// Erlang programs may need a special prologue to handle the stack size they |
| 2266 | /// might need at runtime. That is because Erlang/OTP does not implement a C |
| 2267 | /// stack but uses a custom implementation of hybrid stack/heap architecture. |
| 2268 | /// (for more information see Eric Stenman's Ph.D. thesis: |
| 2269 | /// http://publications.uu.se/uu/fulltext/nbn_se_uu_diva-2688.pdf) |
| 2270 | /// |
| 2271 | /// CheckStack: |
| 2272 | /// temp0 = sp - MaxStack |
| 2273 | /// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart |
| 2274 | /// OldStart: |
| 2275 | /// ... |
| 2276 | /// IncStack: |
| 2277 | /// call inc_stack # doubles the stack space |
| 2278 | /// temp0 = sp - MaxStack |
| 2279 | /// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 2280 | void X86FrameLowering::adjustForHiPEPrologue( |
| 2281 | MachineFunction &MF, MachineBasicBlock &PrologueMBB) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2282 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2283 | DebugLoc DL; |
Quentin Colombet | 4cf5691 | 2016-01-19 23:29:03 +0000 | [diff] [blame] | 2284 | |
| 2285 | // To support shrink-wrapping we would need to insert the new blocks |
| 2286 | // at the right place and update the branches to PrologueMBB. |
| 2287 | assert(&(*MF.begin()) == &PrologueMBB && "Shrink-wrapping not supported yet"); |
| 2288 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2289 | // HiPE-specific values |
| 2290 | const unsigned HipeLeafWords = 24; |
| 2291 | const unsigned CCRegisteredArgs = Is64Bit ? 6 : 5; |
| 2292 | const unsigned Guaranteed = HipeLeafWords * SlotSize; |
| 2293 | unsigned CallerStkArity = MF.getFunction()->arg_size() > CCRegisteredArgs ? |
| 2294 | MF.getFunction()->arg_size() - CCRegisteredArgs : 0; |
| 2295 | unsigned MaxStack = MFI->getStackSize() + CallerStkArity*SlotSize + SlotSize; |
| 2296 | |
| 2297 | assert(STI.isTargetLinux() && |
| 2298 | "HiPE prologue is only supported on Linux operating systems."); |
| 2299 | |
| 2300 | // Compute the largest caller's frame that is needed to fit the callees' |
| 2301 | // frames. This 'MaxStack' is computed from: |
| 2302 | // |
| 2303 | // a) the fixed frame size, which is the space needed for all spilled temps, |
| 2304 | // b) outgoing on-stack parameter areas, and |
| 2305 | // c) the minimum stack space this function needs to make available for the |
| 2306 | // functions it calls (a tunable ABI property). |
| 2307 | if (MFI->hasCalls()) { |
| 2308 | unsigned MoreStackForCalls = 0; |
| 2309 | |
| 2310 | for (MachineFunction::iterator MBBI = MF.begin(), MBBE = MF.end(); |
| 2311 | MBBI != MBBE; ++MBBI) |
| 2312 | for (MachineBasicBlock::iterator MI = MBBI->begin(), ME = MBBI->end(); |
| 2313 | MI != ME; ++MI) { |
| 2314 | if (!MI->isCall()) |
| 2315 | continue; |
| 2316 | |
| 2317 | // Get callee operand. |
| 2318 | const MachineOperand &MO = MI->getOperand(0); |
| 2319 | |
| 2320 | // Only take account of global function calls (no closures etc.). |
| 2321 | if (!MO.isGlobal()) |
| 2322 | continue; |
| 2323 | |
| 2324 | const Function *F = dyn_cast<Function>(MO.getGlobal()); |
| 2325 | if (!F) |
| 2326 | continue; |
| 2327 | |
| 2328 | // Do not update 'MaxStack' for primitive and built-in functions |
| 2329 | // (encoded with names either starting with "erlang."/"bif_" or not |
| 2330 | // having a ".", such as a simple <Module>.<Function>.<Arity>, or an |
| 2331 | // "_", such as the BIF "suspend_0") as they are executed on another |
| 2332 | // stack. |
| 2333 | if (F->getName().find("erlang.") != StringRef::npos || |
| 2334 | F->getName().find("bif_") != StringRef::npos || |
| 2335 | F->getName().find_first_of("._") == StringRef::npos) |
| 2336 | continue; |
| 2337 | |
| 2338 | unsigned CalleeStkArity = |
| 2339 | F->arg_size() > CCRegisteredArgs ? F->arg_size()-CCRegisteredArgs : 0; |
| 2340 | if (HipeLeafWords - 1 > CalleeStkArity) |
| 2341 | MoreStackForCalls = std::max(MoreStackForCalls, |
| 2342 | (HipeLeafWords - 1 - CalleeStkArity) * SlotSize); |
| 2343 | } |
| 2344 | MaxStack += MoreStackForCalls; |
| 2345 | } |
| 2346 | |
| 2347 | // If the stack frame needed is larger than the guaranteed then runtime checks |
| 2348 | // and calls to "inc_stack_0" BIF should be inserted in the assembly prologue. |
| 2349 | if (MaxStack > Guaranteed) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2350 | MachineBasicBlock *stackCheckMBB = MF.CreateMachineBasicBlock(); |
| 2351 | MachineBasicBlock *incStackMBB = MF.CreateMachineBasicBlock(); |
| 2352 | |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 2353 | for (const auto &LI : PrologueMBB.liveins()) { |
Matthias Braun | b2b7ef1 | 2015-08-24 22:59:52 +0000 | [diff] [blame] | 2354 | stackCheckMBB->addLiveIn(LI); |
| 2355 | incStackMBB->addLiveIn(LI); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | MF.push_front(incStackMBB); |
| 2359 | MF.push_front(stackCheckMBB); |
| 2360 | |
| 2361 | unsigned ScratchReg, SPReg, PReg, SPLimitOffset; |
| 2362 | unsigned LEAop, CMPop, CALLop; |
| 2363 | if (Is64Bit) { |
| 2364 | SPReg = X86::RSP; |
| 2365 | PReg = X86::RBP; |
| 2366 | LEAop = X86::LEA64r; |
| 2367 | CMPop = X86::CMP64rm; |
| 2368 | CALLop = X86::CALL64pcrel32; |
| 2369 | SPLimitOffset = 0x90; |
| 2370 | } else { |
| 2371 | SPReg = X86::ESP; |
| 2372 | PReg = X86::EBP; |
| 2373 | LEAop = X86::LEA32r; |
| 2374 | CMPop = X86::CMP32rm; |
| 2375 | CALLop = X86::CALLpcrel32; |
| 2376 | SPLimitOffset = 0x4c; |
| 2377 | } |
| 2378 | |
| 2379 | ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true); |
| 2380 | assert(!MF.getRegInfo().isLiveIn(ScratchReg) && |
| 2381 | "HiPE prologue scratch register is live-in"); |
| 2382 | |
| 2383 | // Create new MBB for StackCheck: |
| 2384 | addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(LEAop), ScratchReg), |
| 2385 | SPReg, false, -MaxStack); |
| 2386 | // SPLimitOffset is in a fixed heap location (pointed by BP). |
| 2387 | addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(CMPop)) |
| 2388 | .addReg(ScratchReg), PReg, false, SPLimitOffset); |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 2389 | BuildMI(stackCheckMBB, DL, TII.get(X86::JAE_1)).addMBB(&PrologueMBB); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2390 | |
| 2391 | // Create new MBB for IncStack: |
| 2392 | BuildMI(incStackMBB, DL, TII.get(CALLop)). |
| 2393 | addExternalSymbol("inc_stack_0"); |
| 2394 | addRegOffset(BuildMI(incStackMBB, DL, TII.get(LEAop), ScratchReg), |
| 2395 | SPReg, false, -MaxStack); |
| 2396 | addRegOffset(BuildMI(incStackMBB, DL, TII.get(CMPop)) |
| 2397 | .addReg(ScratchReg), PReg, false, SPLimitOffset); |
| 2398 | BuildMI(incStackMBB, DL, TII.get(X86::JLE_1)).addMBB(incStackMBB); |
| 2399 | |
Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 2400 | stackCheckMBB->addSuccessor(&PrologueMBB, {99, 100}); |
| 2401 | stackCheckMBB->addSuccessor(incStackMBB, {1, 100}); |
| 2402 | incStackMBB->addSuccessor(&PrologueMBB, {99, 100}); |
| 2403 | incStackMBB->addSuccessor(incStackMBB, {1, 100}); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2404 | } |
| 2405 | #ifdef XDEBUG |
| 2406 | MF.verify(); |
| 2407 | #endif |
| 2408 | } |
| 2409 | |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 2410 | bool X86FrameLowering::adjustStackWithPops(MachineBasicBlock &MBB, |
| 2411 | MachineBasicBlock::iterator MBBI, DebugLoc DL, int Offset) const { |
| 2412 | |
Michael Kuperstein | d926465 | 2015-09-16 11:27:20 +0000 | [diff] [blame] | 2413 | if (Offset <= 0) |
| 2414 | return false; |
| 2415 | |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 2416 | if (Offset % SlotSize) |
| 2417 | return false; |
| 2418 | |
| 2419 | int NumPops = Offset / SlotSize; |
| 2420 | // This is only worth it if we have at most 2 pops. |
| 2421 | if (NumPops != 1 && NumPops != 2) |
| 2422 | return false; |
| 2423 | |
| 2424 | // Handle only the trivial case where the adjustment directly follows |
| 2425 | // a call. This is the most common one, anyway. |
| 2426 | if (MBBI == MBB.begin()) |
| 2427 | return false; |
| 2428 | MachineBasicBlock::iterator Prev = std::prev(MBBI); |
| 2429 | if (!Prev->isCall() || !Prev->getOperand(1).isRegMask()) |
| 2430 | return false; |
| 2431 | |
| 2432 | unsigned Regs[2]; |
| 2433 | unsigned FoundRegs = 0; |
| 2434 | |
| 2435 | auto RegMask = Prev->getOperand(1); |
Michael Kuperstein | d926465 | 2015-09-16 11:27:20 +0000 | [diff] [blame] | 2436 | |
| 2437 | auto &RegClass = |
| 2438 | Is64Bit ? X86::GR64_NOREX_NOSPRegClass : X86::GR32_NOREX_NOSPRegClass; |
| 2439 | // Try to find up to NumPops free registers. |
| 2440 | for (auto Candidate : RegClass) { |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 2441 | |
| 2442 | // Poor man's liveness: |
| 2443 | // Since we're immediately after a call, any register that is clobbered |
| 2444 | // by the call and not defined by it can be considered dead. |
| 2445 | if (!RegMask.clobbersPhysReg(Candidate)) |
| 2446 | continue; |
| 2447 | |
| 2448 | bool IsDef = false; |
| 2449 | for (const MachineOperand &MO : Prev->implicit_operands()) { |
Michael Kuperstein | 8be8de6 | 2016-03-10 18:43:21 +0000 | [diff] [blame^] | 2450 | if (MO.isReg() && MO.isDef() && |
| 2451 | TRI->isSuperOrSubRegisterEq(MO.getReg(), Candidate)) { |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 2452 | IsDef = true; |
| 2453 | break; |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | if (IsDef) |
| 2458 | continue; |
| 2459 | |
| 2460 | Regs[FoundRegs++] = Candidate; |
| 2461 | if (FoundRegs == (unsigned)NumPops) |
| 2462 | break; |
| 2463 | } |
| 2464 | |
| 2465 | if (FoundRegs == 0) |
| 2466 | return false; |
| 2467 | |
| 2468 | // If we found only one free register, but need two, reuse the same one twice. |
| 2469 | while (FoundRegs < (unsigned)NumPops) |
| 2470 | Regs[FoundRegs++] = Regs[0]; |
| 2471 | |
| 2472 | for (int i = 0; i < NumPops; ++i) |
| 2473 | BuildMI(MBB, MBBI, DL, |
| 2474 | TII.get(STI.is64Bit() ? X86::POP64r : X86::POP32r), Regs[i]); |
| 2475 | |
| 2476 | return true; |
| 2477 | } |
| 2478 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2479 | void X86FrameLowering:: |
| 2480 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 2481 | MachineBasicBlock::iterator I) const { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2482 | bool reserveCallFrame = hasReservedCallFrame(MF); |
Matthias Braun | fa3872e | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 2483 | unsigned Opcode = I->getOpcode(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2484 | bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode(); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2485 | DebugLoc DL = I->getDebugLoc(); |
| 2486 | uint64_t Amount = !reserveCallFrame ? I->getOperand(0).getImm() : 0; |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 2487 | uint64_t InternalAmt = (isDestroy || Amount) ? I->getOperand(1).getImm() : 0; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2488 | I = MBB.erase(I); |
| 2489 | |
| 2490 | if (!reserveCallFrame) { |
| 2491 | // If the stack pointer can be changed after prologue, turn the |
| 2492 | // adjcallstackup instruction into a 'sub ESP, <amt>' and the |
| 2493 | // adjcallstackdown instruction into 'add ESP, <amt>' |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2494 | |
| 2495 | // We need to keep the stack aligned properly. To do this, we round the |
| 2496 | // amount of space needed for the outgoing arguments up to the next |
| 2497 | // alignment boundary. |
David Majnemer | 93c22a4 | 2015-02-10 00:57:42 +0000 | [diff] [blame] | 2498 | unsigned StackAlign = getStackAlignment(); |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 2499 | Amount = alignTo(Amount, StackAlign); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2500 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2501 | MachineModuleInfo &MMI = MF.getMMI(); |
| 2502 | const Function *Fn = MF.getFunction(); |
| 2503 | bool WindowsCFI = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); |
| 2504 | bool DwarfCFI = !WindowsCFI && |
| 2505 | (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); |
| 2506 | |
Michael Kuperstein | 259f150 | 2015-10-07 07:01:31 +0000 | [diff] [blame] | 2507 | // If we have any exception handlers in this function, and we adjust |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2508 | // the SP before calls, we may need to indicate this to the unwinder |
| 2509 | // using GNU_ARGS_SIZE. Note that this may be necessary even when |
| 2510 | // Amount == 0, because the preceding function may have set a non-0 |
| 2511 | // GNU_ARGS_SIZE. |
Michael Kuperstein | 259f150 | 2015-10-07 07:01:31 +0000 | [diff] [blame] | 2512 | // TODO: We don't need to reset this between subsequent functions, |
| 2513 | // if it didn't change. |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2514 | bool HasDwarfEHHandlers = !WindowsCFI && |
| 2515 | !MF.getMMI().getLandingPads().empty(); |
Michael Kuperstein | 259f150 | 2015-10-07 07:01:31 +0000 | [diff] [blame] | 2516 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2517 | if (HasDwarfEHHandlers && !isDestroy && |
Michael Kuperstein | 259f150 | 2015-10-07 07:01:31 +0000 | [diff] [blame] | 2518 | MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences()) |
Michael Kuperstein | af22daf | 2015-10-13 06:22:30 +0000 | [diff] [blame] | 2519 | BuildCFI(MBB, I, DL, |
| 2520 | MCCFIInstruction::createGnuArgsSize(nullptr, Amount)); |
Michael Kuperstein | 259f150 | 2015-10-07 07:01:31 +0000 | [diff] [blame] | 2521 | |
| 2522 | if (Amount == 0) |
| 2523 | return; |
| 2524 | |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 2525 | // Factor out the amount that gets handled inside the sequence |
| 2526 | // (Pushes of argument for frame setup, callee pops for frame destroy) |
| 2527 | Amount -= InternalAmt; |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2528 | |
Michael Kuperstein | 77ce9d3 | 2015-12-06 13:06:20 +0000 | [diff] [blame] | 2529 | // TODO: This is needed only if we require precise CFA. |
| 2530 | // If this is a callee-pop calling convention, emit a CFA adjust for |
| 2531 | // the amount the callee popped. |
| 2532 | if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2533 | BuildCFI(MBB, I, DL, |
| 2534 | MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt)); |
| 2535 | |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 2536 | if (Amount) { |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 2537 | // Add Amount to SP to destroy a frame, and subtract to setup. |
| 2538 | int Offset = isDestroy ? Amount : -Amount; |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 2539 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2540 | if (!(Fn->optForMinSize() && |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 2541 | adjustStackWithPops(MBB, I, DL, Offset))) |
| 2542 | BuildStackAdjustment(MBB, I, DL, Offset, /*InEpilogue=*/false); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2543 | } |
Michael Kuperstein | 7337ee2 | 2015-08-11 08:48:48 +0000 | [diff] [blame] | 2544 | |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2545 | if (DwarfCFI && !hasFP(MF)) { |
| 2546 | // If we don't have FP, but need to generate unwind information, |
| 2547 | // we need to set the correct CFA offset after the stack adjustment. |
| 2548 | // How much we adjust the CFA offset depends on whether we're emitting |
| 2549 | // CFI only for EH purposes or for debugging. EH only requires the CFA |
| 2550 | // offset to be correct at each call site, while for debugging we want |
| 2551 | // it to be more precise. |
| 2552 | int CFAOffset = Amount; |
Michael Kuperstein | 77ce9d3 | 2015-12-06 13:06:20 +0000 | [diff] [blame] | 2553 | // TODO: When not using precise CFA, we also need to adjust for the |
| 2554 | // InternalAmt here. |
| 2555 | |
| 2556 | if (CFAOffset) { |
| 2557 | CFAOffset = isDestroy ? -CFAOffset : CFAOffset; |
| 2558 | BuildCFI(MBB, I, DL, |
| 2559 | MCCFIInstruction::createAdjustCfaOffset(nullptr, CFAOffset)); |
| 2560 | } |
Michael Kuperstein | 73dc852 | 2015-11-03 08:17:25 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2563 | return; |
| 2564 | } |
| 2565 | |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 2566 | if (isDestroy && InternalAmt) { |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2567 | // If we are performing frame pointer elimination and if the callee pops |
| 2568 | // something off the stack pointer, add it back. We do this until we have |
| 2569 | // more advanced stack pointer tracking ability. |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2570 | // We are not tracking the stack pointer adjustment by the callee, so make |
| 2571 | // sure we restore the stack pointer immediately after the call, there may |
| 2572 | // be spill code inserted between the CALL and ADJCALLSTACKUP instructions. |
| 2573 | MachineBasicBlock::iterator B = MBB.begin(); |
| 2574 | while (I != B && !std::prev(I)->isCall()) |
| 2575 | --I; |
Reid Kleckner | 98d7803 | 2015-06-18 20:22:12 +0000 | [diff] [blame] | 2576 | BuildStackAdjustment(MBB, I, DL, -InternalAmt, /*InEpilogue=*/false); |
Michael Kuperstein | e86aa9a | 2015-02-01 16:15:07 +0000 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 2580 | bool X86FrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const { |
| 2581 | assert(MBB.getParent() && "Block is not attached to a function!"); |
| 2582 | |
Quentin Colombet | 421723c | 2015-11-04 22:37:28 +0000 | [diff] [blame] | 2583 | // Win64 has strict requirements in terms of epilogue and we are |
| 2584 | // not taking a chance at messing with them. |
| 2585 | // I.e., unless this block is already an exit block, we can't use |
| 2586 | // it as an epilogue. |
Reid Kleckner | 4255b04e | 2015-11-16 18:47:12 +0000 | [diff] [blame] | 2587 | if (STI.isTargetWin64() && !MBB.succ_empty() && !MBB.isReturnBlock()) |
Quentin Colombet | 421723c | 2015-11-04 22:37:28 +0000 | [diff] [blame] | 2588 | return false; |
| 2589 | |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 2590 | if (canUseLEAForSPInEpilogue(*MBB.getParent())) |
| 2591 | return true; |
| 2592 | |
| 2593 | // If we cannot use LEA to adjust SP, we may need to use ADD, which |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 2594 | // clobbers the EFLAGS. Check that we do not need to preserve it, |
| 2595 | // otherwise, conservatively assume this is not |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 2596 | // safe to insert the epilogue here. |
Quentin Colombet | f1e91c8 | 2015-12-02 01:22:54 +0000 | [diff] [blame] | 2597 | return !flagsNeedToBePreservedBeforeTheTerminators(MBB); |
Quentin Colombet | aa802075 | 2015-05-27 06:28:41 +0000 | [diff] [blame] | 2598 | } |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2599 | |
Quentin Colombet | 5d2f7cf | 2015-12-09 23:08:18 +0000 | [diff] [blame] | 2600 | bool X86FrameLowering::enableShrinkWrapping(const MachineFunction &MF) const { |
| 2601 | // If we may need to emit frameless compact unwind information, give |
| 2602 | // up as this is currently broken: PR25614. |
Quentin Colombet | 4cf5691 | 2016-01-19 23:29:03 +0000 | [diff] [blame] | 2603 | return (MF.getFunction()->hasFnAttribute(Attribute::NoUnwind) || hasFP(MF)) && |
| 2604 | // The lowering of segmented stack and HiPE only support entry blocks |
| 2605 | // as prologue blocks: PR26107. |
| 2606 | // This limitation may be lifted if we fix: |
| 2607 | // - adjustForSegmentedStacks |
| 2608 | // - adjustForHiPEPrologue |
| 2609 | MF.getFunction()->getCallingConv() != CallingConv::HiPE && |
| 2610 | !MF.shouldSplitStack(); |
Quentin Colombet | 5d2f7cf | 2015-12-09 23:08:18 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 2613 | MachineBasicBlock::iterator X86FrameLowering::restoreWin32EHStackPointers( |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2614 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 2615 | DebugLoc DL, bool RestoreSP) const { |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2616 | assert(STI.isTargetWindowsMSVC() && "funclets only supported in MSVC env"); |
| 2617 | assert(STI.isTargetWin32() && "EBP/ESI restoration only required on win32"); |
| 2618 | assert(STI.is32Bit() && !Uses64BitFramePtr && |
| 2619 | "restoring EBP/ESI on non-32-bit target"); |
| 2620 | |
| 2621 | MachineFunction &MF = *MBB.getParent(); |
| 2622 | unsigned FramePtr = TRI->getFrameRegister(MF); |
| 2623 | unsigned BasePtr = TRI->getBaseRegister(); |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 2624 | WinEHFuncInfo &FuncInfo = *MF.getWinEHFuncInfo(); |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2625 | X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
| 2626 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 2627 | |
| 2628 | // FIXME: Don't set FrameSetup flag in catchret case. |
| 2629 | |
| 2630 | int FI = FuncInfo.EHRegNodeFrameIndex; |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 2631 | int EHRegSize = MFI->getObjectSize(FI); |
| 2632 | |
| 2633 | if (RestoreSP) { |
| 2634 | // MOV32rm -EHRegSize(%ebp), %esp |
| 2635 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32rm), X86::ESP), |
| 2636 | X86::EBP, true, -EHRegSize) |
| 2637 | .setMIFlag(MachineInstr::FrameSetup); |
| 2638 | } |
| 2639 | |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2640 | unsigned UsedReg; |
| 2641 | int EHRegOffset = getFrameIndexReference(MF, FI, UsedReg); |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2642 | int EndOffset = -EHRegOffset - EHRegSize; |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 2643 | FuncInfo.EHRegNodeEndOffset = EndOffset; |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 2644 | |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2645 | if (UsedReg == FramePtr) { |
| 2646 | // ADD $offset, %ebp |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2647 | unsigned ADDri = getADDriOpcode(false, EndOffset); |
| 2648 | BuildMI(MBB, MBBI, DL, TII.get(ADDri), FramePtr) |
| 2649 | .addReg(FramePtr) |
| 2650 | .addImm(EndOffset) |
| 2651 | .setMIFlag(MachineInstr::FrameSetup) |
| 2652 | ->getOperand(3) |
| 2653 | .setIsDead(); |
Reid Kleckner | b2244cb | 2015-10-08 18:41:52 +0000 | [diff] [blame] | 2654 | assert(EndOffset >= 0 && |
| 2655 | "end of registration object above normal EBP position!"); |
| 2656 | } else if (UsedReg == BasePtr) { |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2657 | // LEA offset(%ebp), %esi |
| 2658 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::LEA32r), BasePtr), |
| 2659 | FramePtr, false, EndOffset) |
| 2660 | .setMIFlag(MachineInstr::FrameSetup); |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 2661 | // MOV32rm SavedEBPOffset(%esi), %ebp |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2662 | assert(X86FI->getHasSEHFramePtrSave()); |
| 2663 | int Offset = |
| 2664 | getFrameIndexReference(MF, X86FI->getSEHFramePtrSaveIndex(), UsedReg); |
| 2665 | assert(UsedReg == BasePtr); |
Reid Kleckner | 5b8a46e | 2015-09-17 20:43:47 +0000 | [diff] [blame] | 2666 | addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32rm), FramePtr), |
| 2667 | UsedReg, true, Offset) |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2668 | .setMIFlag(MachineInstr::FrameSetup); |
Reid Kleckner | b2244cb | 2015-10-08 18:41:52 +0000 | [diff] [blame] | 2669 | } else { |
| 2670 | llvm_unreachable("32-bit frames with WinEH must use FramePtr or BasePtr"); |
Reid Kleckner | df12951 | 2015-09-08 22:44:41 +0000 | [diff] [blame] | 2671 | } |
| 2672 | return MBBI; |
| 2673 | } |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 2674 | |
Zia Ansari | 30a0238 | 2016-02-15 23:44:13 +0000 | [diff] [blame] | 2675 | namespace { |
| 2676 | // Struct used by orderFrameObjects to help sort the stack objects. |
| 2677 | struct X86FrameSortingObject { |
| 2678 | bool IsValid = false; // true if we care about this Object. |
| 2679 | unsigned ObjectIndex = 0; // Index of Object into MFI list. |
| 2680 | unsigned ObjectSize = 0; // Size of Object in bytes. |
| 2681 | unsigned ObjectAlignment = 1; // Alignment of Object in bytes. |
| 2682 | unsigned ObjectNumUses = 0; // Object static number of uses. |
| 2683 | }; |
| 2684 | |
| 2685 | // The comparison function we use for std::sort to order our local |
| 2686 | // stack symbols. The current algorithm is to use an estimated |
| 2687 | // "density". This takes into consideration the size and number of |
| 2688 | // uses each object has in order to roughly minimize code size. |
| 2689 | // So, for example, an object of size 16B that is referenced 5 times |
| 2690 | // will get higher priority than 4 4B objects referenced 1 time each. |
| 2691 | // It's not perfect and we may be able to squeeze a few more bytes out of |
| 2692 | // it (for example : 0(esp) requires fewer bytes, symbols allocated at the |
| 2693 | // fringe end can have special consideration, given their size is less |
| 2694 | // important, etc.), but the algorithmic complexity grows too much to be |
| 2695 | // worth the extra gains we get. This gets us pretty close. |
| 2696 | // The final order leaves us with objects with highest priority going |
| 2697 | // at the end of our list. |
| 2698 | struct X86FrameSortingComparator { |
| 2699 | inline bool operator()(const X86FrameSortingObject &A, |
| 2700 | const X86FrameSortingObject &B) { |
| 2701 | uint64_t DensityAScaled, DensityBScaled; |
| 2702 | |
| 2703 | // For consistency in our comparison, all invalid objects are placed |
| 2704 | // at the end. This also allows us to stop walking when we hit the |
| 2705 | // first invalid item after it's all sorted. |
| 2706 | if (!A.IsValid) |
| 2707 | return false; |
| 2708 | if (!B.IsValid) |
| 2709 | return true; |
| 2710 | |
| 2711 | // The density is calculated by doing : |
| 2712 | // (double)DensityA = A.ObjectNumUses / A.ObjectSize |
| 2713 | // (double)DensityB = B.ObjectNumUses / B.ObjectSize |
| 2714 | // Since this approach may cause inconsistencies in |
| 2715 | // the floating point <, >, == comparisons, depending on the floating |
| 2716 | // point model with which the compiler was built, we're going |
| 2717 | // to scale both sides by multiplying with |
| 2718 | // A.ObjectSize * B.ObjectSize. This ends up factoring away |
| 2719 | // the division and, with it, the need for any floating point |
| 2720 | // arithmetic. |
| 2721 | DensityAScaled = static_cast<uint64_t>(A.ObjectNumUses) * |
| 2722 | static_cast<uint64_t>(B.ObjectSize); |
| 2723 | DensityBScaled = static_cast<uint64_t>(B.ObjectNumUses) * |
| 2724 | static_cast<uint64_t>(A.ObjectSize); |
| 2725 | |
| 2726 | // If the two densities are equal, prioritize highest alignment |
| 2727 | // objects. This allows for similar alignment objects |
| 2728 | // to be packed together (given the same density). |
| 2729 | // There's room for improvement here, also, since we can pack |
| 2730 | // similar alignment (different density) objects next to each |
| 2731 | // other to save padding. This will also require further |
| 2732 | // complexity/iterations, and the overall gain isn't worth it, |
| 2733 | // in general. Something to keep in mind, though. |
| 2734 | if (DensityAScaled == DensityBScaled) |
| 2735 | return A.ObjectAlignment < B.ObjectAlignment; |
| 2736 | |
| 2737 | return DensityAScaled < DensityBScaled; |
| 2738 | } |
| 2739 | }; |
| 2740 | } // namespace |
| 2741 | |
| 2742 | // Order the symbols in the local stack. |
| 2743 | // We want to place the local stack objects in some sort of sensible order. |
| 2744 | // The heuristic we use is to try and pack them according to static number |
| 2745 | // of uses and size of object in order to minimize code size. |
| 2746 | void X86FrameLowering::orderFrameObjects( |
| 2747 | const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const { |
| 2748 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 2749 | |
| 2750 | // Don't waste time if there's nothing to do. |
| 2751 | if (ObjectsToAllocate.empty()) |
| 2752 | return; |
| 2753 | |
| 2754 | // Create an array of all MFI objects. We won't need all of these |
| 2755 | // objects, but we're going to create a full array of them to make |
| 2756 | // it easier to index into when we're counting "uses" down below. |
| 2757 | // We want to be able to easily/cheaply access an object by simply |
| 2758 | // indexing into it, instead of having to search for it every time. |
| 2759 | std::vector<X86FrameSortingObject> SortingObjects(MFI->getObjectIndexEnd()); |
| 2760 | |
| 2761 | // Walk the objects we care about and mark them as such in our working |
| 2762 | // struct. |
| 2763 | for (auto &Obj : ObjectsToAllocate) { |
| 2764 | SortingObjects[Obj].IsValid = true; |
| 2765 | SortingObjects[Obj].ObjectIndex = Obj; |
| 2766 | SortingObjects[Obj].ObjectAlignment = MFI->getObjectAlignment(Obj); |
| 2767 | // Set the size. |
| 2768 | int ObjectSize = MFI->getObjectSize(Obj); |
| 2769 | if (ObjectSize == 0) |
| 2770 | // Variable size. Just use 4. |
| 2771 | SortingObjects[Obj].ObjectSize = 4; |
| 2772 | else |
| 2773 | SortingObjects[Obj].ObjectSize = ObjectSize; |
| 2774 | } |
| 2775 | |
| 2776 | // Count the number of uses for each object. |
| 2777 | for (auto &MBB : MF) { |
| 2778 | for (auto &MI : MBB) { |
| 2779 | for (const MachineOperand &MO : MI.operands()) { |
| 2780 | // Check to see if it's a local stack symbol. |
| 2781 | if (!MO.isFI()) |
| 2782 | continue; |
| 2783 | int Index = MO.getIndex(); |
| 2784 | // Check to see if it falls within our range, and is tagged |
| 2785 | // to require ordering. |
| 2786 | if (Index >= 0 && Index < MFI->getObjectIndexEnd() && |
| 2787 | SortingObjects[Index].IsValid) |
| 2788 | SortingObjects[Index].ObjectNumUses++; |
| 2789 | } |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | // Sort the objects using X86FrameSortingAlgorithm (see its comment for |
| 2794 | // info). |
| 2795 | std::stable_sort(SortingObjects.begin(), SortingObjects.end(), |
| 2796 | X86FrameSortingComparator()); |
| 2797 | |
| 2798 | // Now modify the original list to represent the final order that |
| 2799 | // we want. The order will depend on whether we're going to access them |
| 2800 | // from the stack pointer or the frame pointer. For SP, the list should |
| 2801 | // end up with the END containing objects that we want with smaller offsets. |
| 2802 | // For FP, it should be flipped. |
| 2803 | int i = 0; |
| 2804 | for (auto &Obj : SortingObjects) { |
| 2805 | // All invalid items are sorted at the end, so it's safe to stop. |
| 2806 | if (!Obj.IsValid) |
| 2807 | break; |
| 2808 | ObjectsToAllocate[i++] = Obj.ObjectIndex; |
| 2809 | } |
| 2810 | |
| 2811 | // Flip it if we're accessing off of the FP. |
| 2812 | if (!TRI->needsStackRealignment(MF) && hasFP(MF)) |
| 2813 | std::reverse(ObjectsToAllocate.begin(), ObjectsToAllocate.end()); |
| 2814 | } |
| 2815 | |
| 2816 | |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 2817 | unsigned X86FrameLowering::getWinEHParentFrameOffset(const MachineFunction &MF) const { |
| 2818 | // RDX, the parent frame pointer, is homed into 16(%rsp) in the prologue. |
| 2819 | unsigned Offset = 16; |
| 2820 | // RBP is immediately pushed. |
| 2821 | Offset += SlotSize; |
| 2822 | // All callee-saved registers are then pushed. |
| 2823 | Offset += MF.getInfo<X86MachineFunctionInfo>()->getCalleeSavedFrameSize(); |
| 2824 | // Every funclet allocates enough stack space for the largest outgoing call. |
| 2825 | Offset += getWinEHFuncletFrameSize(MF); |
| 2826 | return Offset; |
| 2827 | } |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 2828 | |
| 2829 | void X86FrameLowering::processFunctionBeforeFrameFinalized( |
| 2830 | MachineFunction &MF, RegScavenger *RS) const { |
| 2831 | // If this function isn't doing Win64-style C++ EH, we don't need to do |
| 2832 | // anything. |
| 2833 | const Function *Fn = MF.getFunction(); |
Reid Kleckner | c397b26 | 2015-11-16 18:47:25 +0000 | [diff] [blame] | 2834 | if (!STI.is64Bit() || !MF.getMMI().hasEHFunclets() || |
| 2835 | classifyEHPersonality(Fn->getPersonalityFn()) != EHPersonality::MSVC_CXX) |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 2836 | return; |
| 2837 | |
| 2838 | // Win64 C++ EH needs to allocate the UnwindHelp object at some fixed offset |
| 2839 | // relative to RSP after the prologue. Find the offset of the last fixed |
Reid Kleckner | c397b26 | 2015-11-16 18:47:25 +0000 | [diff] [blame] | 2840 | // object, so that we can allocate a slot immediately following it. If there |
| 2841 | // were no fixed objects, use offset -SlotSize, which is immediately after the |
| 2842 | // return address. Fixed objects have negative frame indices. |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 2843 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 2844 | WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo(); |
Reid Kleckner | c397b26 | 2015-11-16 18:47:25 +0000 | [diff] [blame] | 2845 | int64_t MinFixedObjOffset = -SlotSize; |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 2846 | for (int I = MFI->getObjectIndexBegin(); I < 0; ++I) |
| 2847 | MinFixedObjOffset = std::min(MinFixedObjOffset, MFI->getObjectOffset(I)); |
| 2848 | |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 2849 | for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { |
| 2850 | for (WinEHHandlerType &H : TBME.HandlerArray) { |
| 2851 | int FrameIndex = H.CatchObj.FrameIndex; |
| 2852 | if (FrameIndex != INT_MAX) { |
| 2853 | // Ensure alignment. |
| 2854 | unsigned Align = MFI->getObjectAlignment(FrameIndex); |
| 2855 | MinFixedObjOffset -= std::abs(MinFixedObjOffset) % Align; |
| 2856 | MinFixedObjOffset -= MFI->getObjectSize(FrameIndex); |
| 2857 | MFI->setObjectOffset(FrameIndex, MinFixedObjOffset); |
| 2858 | } |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | // Ensure alignment. |
| 2863 | MinFixedObjOffset -= std::abs(MinFixedObjOffset) % 8; |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 2864 | int64_t UnwindHelpOffset = MinFixedObjOffset - SlotSize; |
| 2865 | int UnwindHelpFI = |
| 2866 | MFI->CreateFixedObject(SlotSize, UnwindHelpOffset, /*Immutable=*/false); |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 2867 | EHInfo.UnwindHelpFrameIdx = UnwindHelpFI; |
Reid Kleckner | 94b5706 | 2015-11-13 19:06:01 +0000 | [diff] [blame] | 2868 | |
| 2869 | // Store -2 into UnwindHelp on function entry. We have to scan forwards past |
| 2870 | // other frame setup instructions. |
| 2871 | MachineBasicBlock &MBB = MF.front(); |
| 2872 | auto MBBI = MBB.begin(); |
| 2873 | while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup)) |
| 2874 | ++MBBI; |
| 2875 | |
| 2876 | DebugLoc DL = MBB.findDebugLoc(MBBI); |
| 2877 | addFrameReference(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mi32)), |
| 2878 | UnwindHelpFI) |
| 2879 | .addImm(-2); |
| 2880 | } |