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