blob: 70487a11b39083b4afc5377dafc7da03db47f5b7 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- X86FrameLowering.cpp - X86 Frame Information ----------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
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//
Anton Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the X86 implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "X86FrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "X86InstrBuilder.h"
16#include "X86InstrInfo.h"
17#include "X86MachineFunctionInfo.h"
Rafael Espindolac2174212011-08-30 19:39:58 +000018#include "X86Subtarget.h"
Anton Korobeynikov14ee3442010-11-18 23:25:52 +000019#include "X86TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/SmallSet.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/Function.h"
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000028#include "llvm/MC/MCAsmInfo.h"
Bill Wendlingb6adf462011-07-07 00:54:13 +000029#include "llvm/MC/MCSymbol.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000030#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetOptions.h"
NAKAMURA Takumi1db59952014-06-25 12:41:52 +000032#include "llvm/Support/Debug.h"
Chandler Carruth82cc9642014-10-10 08:27:19 +000033#include <cstdlib>
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000034
35using namespace llvm;
36
37// FIXME: completely move here.
38extern cl::opt<bool> ForceStackAlign;
39
Anton Korobeynikov2f931282011-01-10 12:39:04 +000040bool X86FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000041 return !MF.getFrameInfo()->hasVarSizedObjects();
42}
43
44/// hasFP - Return true if the specified function should have a dedicated frame
45/// pointer register. This is true if the function has variable sized allocas
46/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000047bool X86FrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000048 const MachineFrameInfo *MFI = MF.getFrameInfo();
49 const MachineModuleInfo &MMI = MF.getMMI();
Eric Christopherfc6de422014-08-05 02:39:49 +000050 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000051
Nick Lewycky50f02cb2011-12-02 22:16:29 +000052 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
Chad Rosier20b79dc2012-05-23 23:45:10 +000053 RegInfo->needsStackRealignment(MF) ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000054 MFI->hasVarSizedObjects() ||
Reid Kleckneree088972013-12-10 18:27:32 +000055 MFI->isFrameAddressTaken() || MFI->hasInlineAsmWithSPAdjust() ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000056 MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() ||
Juergen Ributzka99bd3cb2014-10-02 22:21:49 +000057 MMI.callsUnwindInit() || MMI.callsEHReturn() ||
58 MFI->hasStackMap() || MFI->hasPatchPoint());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000059}
60
Eli Bendersky8da87162013-02-21 20:05:00 +000061static unsigned getSUBriOpcode(unsigned IsLP64, int64_t Imm) {
62 if (IsLP64) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000063 if (isInt<8>(Imm))
64 return X86::SUB64ri8;
65 return X86::SUB64ri32;
66 } else {
67 if (isInt<8>(Imm))
68 return X86::SUB32ri8;
69 return X86::SUB32ri;
70 }
71}
72
Eli Benderskyef4558a2013-02-06 20:43:57 +000073static unsigned getADDriOpcode(unsigned IsLP64, int64_t Imm) {
74 if (IsLP64) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000075 if (isInt<8>(Imm))
76 return X86::ADD64ri8;
77 return X86::ADD64ri32;
78 } else {
79 if (isInt<8>(Imm))
80 return X86::ADD32ri8;
81 return X86::ADD32ri;
82 }
83}
84
Eli Benderskyef4558a2013-02-06 20:43:57 +000085static unsigned getLEArOpcode(unsigned IsLP64) {
86 return IsLP64 ? X86::LEA64r : X86::LEA32r;
Evan Cheng1b81fdd2012-02-07 22:50:41 +000087}
88
Evan Cheng65089fc2011-01-03 22:53:22 +000089/// findDeadCallerSavedReg - Return a caller-saved register that isn't live
90/// when it reaches the "return" instruction. We can then pop a stack object
91/// to this register without worry about clobbering it.
92static unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB,
93 MachineBasicBlock::iterator &MBBI,
94 const TargetRegisterInfo &TRI,
95 bool Is64Bit) {
96 const MachineFunction *MF = MBB.getParent();
97 const Function *F = MF->getFunction();
98 if (!F || MF->getMMI().callsEHReturn())
99 return 0;
100
Craig Topper1d326582012-03-04 10:43:23 +0000101 static const uint16_t CallerSavedRegs32Bit[] = {
Andrew Trick210bf832011-08-12 00:49:19 +0000102 X86::EAX, X86::EDX, X86::ECX, 0
Evan Cheng65089fc2011-01-03 22:53:22 +0000103 };
104
Craig Topper1d326582012-03-04 10:43:23 +0000105 static const uint16_t CallerSavedRegs64Bit[] = {
Evan Cheng65089fc2011-01-03 22:53:22 +0000106 X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
Andrew Trick210bf832011-08-12 00:49:19 +0000107 X86::R8, X86::R9, X86::R10, X86::R11, 0
Evan Cheng65089fc2011-01-03 22:53:22 +0000108 };
109
110 unsigned Opc = MBBI->getOpcode();
111 switch (Opc) {
112 default: return 0;
David Woodhouse79dd5052014-01-08 12:58:07 +0000113 case X86::RETL:
114 case X86::RETQ:
David Woodhouse4e033b02014-01-13 14:05:59 +0000115 case X86::RETIL:
116 case X86::RETIQ:
Evan Cheng65089fc2011-01-03 22:53:22 +0000117 case X86::TCRETURNdi:
118 case X86::TCRETURNri:
119 case X86::TCRETURNmi:
120 case X86::TCRETURNdi64:
121 case X86::TCRETURNri64:
122 case X86::TCRETURNmi64:
123 case X86::EH_RETURN:
124 case X86::EH_RETURN64: {
Craig Topper1d326582012-03-04 10:43:23 +0000125 SmallSet<uint16_t, 8> Uses;
Evan Cheng65089fc2011-01-03 22:53:22 +0000126 for (unsigned i = 0, e = MBBI->getNumOperands(); i != e; ++i) {
127 MachineOperand &MO = MBBI->getOperand(i);
128 if (!MO.isReg() || MO.isDef())
129 continue;
130 unsigned Reg = MO.getReg();
131 if (!Reg)
132 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000133 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
134 Uses.insert(*AI);
Evan Cheng65089fc2011-01-03 22:53:22 +0000135 }
136
Craig Topper1d326582012-03-04 10:43:23 +0000137 const uint16_t *CS = Is64Bit ? CallerSavedRegs64Bit : CallerSavedRegs32Bit;
Evan Cheng65089fc2011-01-03 22:53:22 +0000138 for (; *CS; ++CS)
139 if (!Uses.count(*CS))
140 return *CS;
141 }
142 }
143
144 return 0;
145}
146
147
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000148/// emitSPUpdate - Emit a series of instructions to increment / decrement the
149/// stack pointer by a constant value.
150static
151void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Evan Cheng65089fc2011-01-03 22:53:22 +0000152 unsigned StackPtr, int64_t NumBytes,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000153 bool Is64BitTarget, bool Is64BitStackPtr, bool UseLEA,
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000154 const TargetInstrInfo &TII, const TargetRegisterInfo &TRI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000155 bool isSub = NumBytes < 0;
156 uint64_t Offset = isSub ? -NumBytes : NumBytes;
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000157 unsigned Opc;
158 if (UseLEA)
Pavel Chupinf55eb452014-08-07 09:41:19 +0000159 Opc = getLEArOpcode(Is64BitStackPtr);
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000160 else
161 Opc = isSub
Pavel Chupinf55eb452014-08-07 09:41:19 +0000162 ? getSUBriOpcode(Is64BitStackPtr, Offset)
163 : getADDriOpcode(Is64BitStackPtr, Offset);
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000164
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000165 uint64_t Chunk = (1LL << 31) - 1;
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000166 DebugLoc DL = MBB.findDebugLoc(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000167
168 while (Offset) {
169 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
Pavel Chupinf55eb452014-08-07 09:41:19 +0000170 if (ThisVal == (Is64BitTarget ? 8 : 4)) {
Evan Cheng65089fc2011-01-03 22:53:22 +0000171 // Use push / pop instead.
172 unsigned Reg = isSub
Pavel Chupinf55eb452014-08-07 09:41:19 +0000173 ? (unsigned)(Is64BitTarget ? X86::RAX : X86::EAX)
174 : findDeadCallerSavedReg(MBB, MBBI, TRI, Is64BitTarget);
Evan Cheng65089fc2011-01-03 22:53:22 +0000175 if (Reg) {
176 Opc = isSub
Pavel Chupinf55eb452014-08-07 09:41:19 +0000177 ? (Is64BitTarget ? X86::PUSH64r : X86::PUSH32r)
178 : (Is64BitTarget ? X86::POP64r : X86::POP32r);
Charles Davis7ed40cb2011-06-12 01:45:54 +0000179 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opc))
Evan Cheng65089fc2011-01-03 22:53:22 +0000180 .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub));
Charles Davis7ed40cb2011-06-12 01:45:54 +0000181 if (isSub)
182 MI->setFlag(MachineInstr::FrameSetup);
Evan Cheng65089fc2011-01-03 22:53:22 +0000183 Offset -= ThisVal;
184 continue;
185 }
186 }
187
Craig Topper062a2ba2014-04-25 05:30:21 +0000188 MachineInstr *MI = nullptr;
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000189
190 if (UseLEA) {
191 MI = addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr),
192 StackPtr, false, isSub ? -ThisVal : ThisVal);
193 } else {
194 MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
195 .addReg(StackPtr)
196 .addImm(ThisVal);
197 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead.
198 }
199
Charles Davis7ed40cb2011-06-12 01:45:54 +0000200 if (isSub)
201 MI->setFlag(MachineInstr::FrameSetup);
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000202
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000203 Offset -= ThisVal;
204 }
205}
206
207/// mergeSPUpdatesUp - Merge two stack-manipulating instructions upper iterator.
208static
209void mergeSPUpdatesUp(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Craig Topper062a2ba2014-04-25 05:30:21 +0000210 unsigned StackPtr, uint64_t *NumBytes = nullptr) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000211 if (MBBI == MBB.begin()) return;
212
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000213 MachineBasicBlock::iterator PI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000214 unsigned Opc = PI->getOpcode();
215 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000216 Opc == X86::ADD32ri || Opc == X86::ADD32ri8 ||
217 Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000218 PI->getOperand(0).getReg() == StackPtr) {
219 if (NumBytes)
220 *NumBytes += PI->getOperand(2).getImm();
221 MBB.erase(PI);
222 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
223 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
224 PI->getOperand(0).getReg() == StackPtr) {
225 if (NumBytes)
226 *NumBytes -= PI->getOperand(2).getImm();
227 MBB.erase(PI);
228 }
229}
230
Eric Christopher4237bf12014-04-29 00:16:33 +0000231/// mergeSPUpdatesDown - Merge two stack-manipulating instructions lower
232/// iterator.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000233static
234void mergeSPUpdatesDown(MachineBasicBlock &MBB,
235 MachineBasicBlock::iterator &MBBI,
Craig Topper062a2ba2014-04-25 05:30:21 +0000236 unsigned StackPtr, uint64_t *NumBytes = nullptr) {
Sanjoy Dasf60485c2011-12-01 19:15:08 +0000237 // FIXME: THIS ISN'T RUN!!!
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000238 return;
239
240 if (MBBI == MBB.end()) return;
241
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000242 MachineBasicBlock::iterator NI = std::next(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000243 if (NI == MBB.end()) return;
244
245 unsigned Opc = NI->getOpcode();
246 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
247 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
248 NI->getOperand(0).getReg() == StackPtr) {
249 if (NumBytes)
250 *NumBytes -= NI->getOperand(2).getImm();
251 MBB.erase(NI);
252 MBBI = NI;
253 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
254 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
255 NI->getOperand(0).getReg() == StackPtr) {
256 if (NumBytes)
257 *NumBytes += NI->getOperand(2).getImm();
258 MBB.erase(NI);
259 MBBI = NI;
260 }
261}
262
263/// mergeSPUpdates - Checks the instruction before/after the passed
Eric Christopher4237bf12014-04-29 00:16:33 +0000264/// instruction. If it is an ADD/SUB/LEA instruction it is deleted argument and
265/// the stack adjustment is returned as a positive value for ADD/LEA and a
266/// negative for SUB.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000267static int mergeSPUpdates(MachineBasicBlock &MBB,
Eric Christopher4237bf12014-04-29 00:16:33 +0000268 MachineBasicBlock::iterator &MBBI, unsigned StackPtr,
269 bool doMergeWithPrevious) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000270 if ((doMergeWithPrevious && MBBI == MBB.begin()) ||
271 (!doMergeWithPrevious && MBBI == MBB.end()))
272 return 0;
273
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000274 MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI;
Craig Topper062a2ba2014-04-25 05:30:21 +0000275 MachineBasicBlock::iterator NI = doMergeWithPrevious ? nullptr
276 : std::next(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000277 unsigned Opc = PI->getOpcode();
278 int Offset = 0;
279
280 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000281 Opc == X86::ADD32ri || Opc == X86::ADD32ri8 ||
282 Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000283 PI->getOperand(0).getReg() == StackPtr){
284 Offset += PI->getOperand(2).getImm();
285 MBB.erase(PI);
286 if (!doMergeWithPrevious) MBBI = NI;
287 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
288 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
289 PI->getOperand(0).getReg() == StackPtr) {
290 Offset -= PI->getOperand(2).getImm();
291 MBB.erase(PI);
292 if (!doMergeWithPrevious) MBBI = NI;
293 }
294
295 return Offset;
296}
297
298static bool isEAXLiveIn(MachineFunction &MF) {
299 for (MachineRegisterInfo::livein_iterator II = MF.getRegInfo().livein_begin(),
300 EE = MF.getRegInfo().livein_end(); II != EE; ++II) {
301 unsigned Reg = II->first;
302
303 if (Reg == X86::EAX || Reg == X86::AX ||
304 Reg == X86::AH || Reg == X86::AL)
305 return true;
306 }
307
308 return false;
309}
310
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000311void
312X86FrameLowering::emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
313 MachineBasicBlock::iterator MBBI,
314 DebugLoc DL) const {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000315 MachineFunction &MF = *MBB.getParent();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000316 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000317 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000318 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000319 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000320
321 // Add callee saved registers to move list.
322 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
323 if (CSI.empty()) return;
324
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000325 // Calculate offsets.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000326 for (std::vector<CalleeSavedInfo>::const_iterator
327 I = CSI.begin(), E = CSI.end(); I != E; ++I) {
328 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
329 unsigned Reg = I->getReg();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000330
Bill Wendlingbc07a892013-06-18 07:20:20 +0000331 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000332 unsigned CFIIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +0000333 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, DwarfReg,
334 Offset));
Eric Christopher612bb692014-04-29 00:16:46 +0000335 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
336 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000337 }
338}
339
Nadav Rotem1bef5a02012-12-23 07:30:09 +0000340/// usesTheStack - This function checks if any of the users of EFLAGS
Nadav Rotemd5aae982012-12-21 23:48:49 +0000341/// copies the EFLAGS. We know that the code that lowers COPY of EFLAGS has
342/// to use the stack, and if we don't adjust the stack we clobber the first
343/// frame index.
Nadav Rotem1bef5a02012-12-23 07:30:09 +0000344/// See X86InstrInfo::copyPhysReg.
Bill Wendling28519072013-08-15 18:46:14 +0000345static bool usesTheStack(const MachineFunction &MF) {
346 const MachineRegisterInfo &MRI = MF.getRegInfo();
Nadav Rotemd5aae982012-12-21 23:48:49 +0000347
Owen Anderson16c6bf42014-03-13 23:12:04 +0000348 for (MachineRegisterInfo::reg_instr_iterator
349 ri = MRI.reg_instr_begin(X86::EFLAGS), re = MRI.reg_instr_end();
350 ri != re; ++ri)
Nadav Rotemd5aae982012-12-21 23:48:49 +0000351 if (ri->isCopy())
352 return true;
353
354 return false;
355}
356
Philip Reames34fcca72014-08-21 22:15:20 +0000357void X86FrameLowering::getStackProbeFunction(const X86Subtarget &STI,
358 unsigned &CallOp,
359 const char *&Symbol) {
360 CallOp = STI.is64Bit() ? X86::W64ALLOCA : X86::CALLpcrel32;
361
362 if (STI.is64Bit()) {
363 if (STI.isTargetCygMing()) {
364 Symbol = "___chkstk_ms";
365 } else {
366 Symbol = "__chkstk";
367 }
368 } else if (STI.isTargetCygMing())
369 Symbol = "_alloca";
370 else
371 Symbol = "_chkstk";
372}
373
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000374/// emitPrologue - Push callee-saved registers onto the stack, which
375/// automatically adjust the stack pointer. Adjust the stack pointer to allocate
376/// space for local variables. Also emit labels used by the exception handler to
377/// generate the exception handling frames.
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000378
379/*
380 Here's a gist of what gets emitted:
381
382 ; Establish frame pointer, if needed
383 [if needs FP]
384 push %rbp
385 .cfi_def_cfa_offset 16
386 .cfi_offset %rbp, -16
387 .seh_pushreg %rpb
388 mov %rsp, %rbp
389 .cfi_def_cfa_register %rbp
390
391 ; Spill general-purpose registers
392 [for all callee-saved GPRs]
393 pushq %<reg>
394 [if not needs FP]
395 .cfi_def_cfa_offset (offset from RETADDR)
396 .seh_pushreg %<reg>
397
398 ; If the required stack alignment > default stack alignment
399 ; rsp needs to be re-aligned. This creates a "re-alignment gap"
400 ; of unknown size in the stack frame.
401 [if stack needs re-alignment]
402 and $MASK, %rsp
403
404 ; Allocate space for locals
405 [if target is Windows and allocated space > 4096 bytes]
406 ; Windows needs special care for allocations larger
407 ; than one page.
408 mov $NNN, %rax
409 call ___chkstk_ms/___chkstk
410 sub %rax, %rsp
411 [else]
412 sub $NNN, %rsp
413
414 [if needs FP]
415 .seh_stackalloc (size of XMM spill slots)
416 .seh_setframe %rbp, SEHFrameOffset ; = size of all spill slots
417 [else]
418 .seh_stackalloc NNN
419
420 ; Spill XMMs
421 ; Note, that while only Windows 64 ABI specifies XMMs as callee-preserved,
422 ; they may get spilled on any platform, if the current function
423 ; calls @llvm.eh.unwind.init
424 [if needs FP]
425 [for all callee-saved XMM registers]
426 movaps %<xmm reg>, -MMM(%rbp)
427 [for all callee-saved XMM registers]
428 .seh_savexmm %<xmm reg>, (-MMM + SEHFrameOffset)
429 ; i.e. the offset relative to (%rbp - SEHFrameOffset)
430 [else]
431 [for all callee-saved XMM registers]
432 movaps %<xmm reg>, KKK(%rsp)
433 [for all callee-saved XMM registers]
434 .seh_savexmm %<xmm reg>, KKK
435
436 .seh_endprologue
437
438 [if needs base pointer]
439 mov %rsp, %rbx
440
441 ; Emit CFI info
442 [if needs FP]
443 [for all callee-saved registers]
444 .cfi_offset %<reg>, (offset from %rbp)
445 [else]
446 .cfi_def_cfa_offset (offset from RETADDR)
447 [for all callee-saved registers]
448 .cfi_offset %<reg>, (offset from %rsp)
449
450 Notes:
451 - .seh directives are emitted only for Windows 64 ABI
452 - .cfi directives are emitted for all other ABIs
453 - for 32-bit code, substitute %e?? registers for %r??
454*/
455
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000456void X86FrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000457 MachineBasicBlock &MBB = MF.front(); // Prologue goes in entry BB.
458 MachineBasicBlock::iterator MBBI = MBB.begin();
459 MachineFrameInfo *MFI = MF.getFrameInfo();
460 const Function *Fn = MF.getFunction();
Eric Christopherfc6de422014-08-05 02:39:49 +0000461 const X86RegisterInfo *RegInfo =
462 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
463 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000464 MachineModuleInfo &MMI = MF.getMMI();
465 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000466 uint64_t MaxAlign = MFI->getMaxAlignment(); // Desired stack alignment.
467 uint64_t StackSize = MFI->getStackSize(); // Number of bytes to allocate.
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000468 bool HasFP = hasFP(MF);
Eric Christopherf4381642014-06-05 22:00:31 +0000469 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000470 bool Is64Bit = STI.is64Bit();
Pavel Chupinf55eb452014-08-07 09:41:19 +0000471 // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit.
472 const bool Uses64BitFramePtr = STI.isTarget64BitLP64() || STI.isTargetNaCl64();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000473 bool IsWin64 = STI.isTargetWin64();
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000474 bool IsWinEH =
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000475 MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() ==
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000476 ExceptionHandling::WinEH; // Not necessarily synonymous with IsWin64.
477 bool NeedsWinEH = IsWinEH && Fn->needsUnwindTableEntry();
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000478 bool NeedsDwarfCFI =
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000479 !IsWinEH && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry());
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000480 bool UseLEA = STI.useLeaForSP();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000481 unsigned StackAlign = getStackAlignment();
482 unsigned SlotSize = RegInfo->getSlotSize();
483 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Pavel Chupinf55eb452014-08-07 09:41:19 +0000484 const unsigned MachineFramePtr = STI.isTarget64BitILP32() ?
485 getX86SubSuperRegister(FramePtr, MVT::i64, false) : FramePtr;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000486 unsigned StackPtr = RegInfo->getStackRegister();
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000487 unsigned BasePtr = RegInfo->getBaseRegister();
Bill Wendlingf27e3312013-09-10 00:20:27 +0000488 DebugLoc DL;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000489
490 // If we're forcing a stack realignment we can't rely on just the frame
491 // info, we need to know the ABI stack alignment as well in case we
492 // have a call out. Otherwise just make sure we have some alignment - we'll
493 // go with the minimum SlotSize.
494 if (ForceStackAlign) {
495 if (MFI->hasCalls())
496 MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign;
497 else if (MaxAlign < SlotSize)
498 MaxAlign = SlotSize;
499 }
500
501 // Add RETADDR move area to callee saved frame size.
502 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
503 if (TailCallReturnAddrDelta < 0)
504 X86FI->setCalleeSavedFrameSize(
505 X86FI->getCalleeSavedFrameSize() - TailCallReturnAddrDelta);
506
Philip Reames2c52c662014-08-21 22:53:49 +0000507 bool UseStackProbe = (STI.isOSWindows() && !STI.isTargetMacho());
508
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000509 // If this is x86-64 and the Red Zone is not disabled, if we are a leaf
510 // function, and use up to 128 bytes of stack space, don't have a frame
511 // pointer, calls, or dynamic alloca then we do not need to adjust the
Nadav Rotemd5aae982012-12-21 23:48:49 +0000512 // stack pointer (we fit in the Red Zone). We also check that we don't
513 // push and pop from the stack.
Bill Wendling698e84f2012-12-30 10:32:01 +0000514 if (Is64Bit && !Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
515 Attribute::NoRedZone) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000516 !RegInfo->needsStackRealignment(MF) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000517 !MFI->hasVarSizedObjects() && // No dynamic alloca.
518 !MFI->adjustsStack() && // No calls.
519 !IsWin64 && // Win64 has no Red Zone
Nadav Rotem1bef5a02012-12-23 07:30:09 +0000520 !usesTheStack(MF) && // Don't push and pop.
Reid Kleckner9c658212014-04-10 22:58:43 +0000521 !MF.shouldSplitStack()) { // Regular stack
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000522 uint64_t MinSize = X86FI->getCalleeSavedFrameSize();
523 if (HasFP) MinSize += SlotSize;
524 StackSize = std::max(MinSize, StackSize > 128 ? StackSize - 128 : 0);
525 MFI->setStackSize(StackSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000526 }
527
528 // Insert stack pointer adjustment for later moving of return addr. Only
529 // applies to tail call optimized functions where the callee argument stack
530 // size is bigger than the callers.
531 if (TailCallReturnAddrDelta < 0) {
532 MachineInstr *MI =
533 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000534 TII.get(getSUBriOpcode(Uses64BitFramePtr, -TailCallReturnAddrDelta)),
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000535 StackPtr)
536 .addReg(StackPtr)
Charles Davis7ed40cb2011-06-12 01:45:54 +0000537 .addImm(-TailCallReturnAddrDelta)
538 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000539 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead.
540 }
541
542 // Mapping for machine moves:
543 //
544 // DST: VirtualFP AND
545 // SRC: VirtualFP => DW_CFA_def_cfa_offset
546 // ELSE => DW_CFA_def_cfa
547 //
548 // SRC: VirtualFP AND
549 // DST: Register => DW_CFA_def_cfa_register
550 //
551 // ELSE
552 // OFFSET < 0 => DW_CFA_offset_extended_sf
553 // REG < 64 => DW_CFA_offset + Reg
554 // ELSE => DW_CFA_offset_extended
555
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000556 uint64_t NumBytes = 0;
Michael Liao6d810bd2012-10-25 06:29:14 +0000557 int stackGrowth = -SlotSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000558
559 if (HasFP) {
560 // Calculate required stack adjustment.
561 uint64_t FrameSize = StackSize - SlotSize;
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000562 if (RegInfo->needsStackRealignment(MF)) {
563 // Callee-saved registers are pushed on stack before the stack
564 // is realigned.
565 FrameSize -= X86FI->getCalleeSavedFrameSize();
566 NumBytes = (FrameSize + MaxAlign - 1) / MaxAlign * MaxAlign;
567 } else {
568 NumBytes = FrameSize - X86FI->getCalleeSavedFrameSize();
569 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000570
571 // Get the offset of the stack slot for the EBP register, which is
572 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
573 // Update the frame offset adjustment.
574 MFI->setOffsetAdjustment(-NumBytes);
575
576 // Save EBP/RBP into the appropriate stack slot.
577 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
Pavel Chupinf55eb452014-08-07 09:41:19 +0000578 .addReg(MachineFramePtr, RegState::Kill)
Charles Davis7ed40cb2011-06-12 01:45:54 +0000579 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000580
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000581 if (NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000582 // Mark the place where EBP/RBP was saved.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000583 // Define the current CFA rule to use the provided offset.
Rafael Espindola84ee6c42013-05-15 22:27:35 +0000584 assert(StackSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000585 unsigned CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000586 MCCFIInstruction::createDefCfaOffset(nullptr, 2 * stackGrowth));
Eric Christopher612bb692014-04-29 00:16:46 +0000587 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000588 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000589
590 // Change the rule for the FramePtr to be an "offset" rule.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000591 unsigned DwarfFramePtr = RegInfo->getDwarfRegNum(MachineFramePtr, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000592 CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000593 MCCFIInstruction::createOffset(nullptr,
594 DwarfFramePtr, 2 * stackGrowth));
Eric Christopher612bb692014-04-29 00:16:46 +0000595 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000596 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000597 }
598
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000599 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000600 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
601 .addImm(FramePtr)
602 .setMIFlag(MachineInstr::FrameSetup);
603 }
604
Bill Wendlingb97270d2011-07-25 18:00:28 +0000605 // Update EBP with the new base value.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000606 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000607 TII.get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), FramePtr)
Charles Davis7ed40cb2011-06-12 01:45:54 +0000608 .addReg(StackPtr)
609 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000610
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000611 if (NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000612 // Mark effective beginning of when frame pointer becomes valid.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000613 // Define the current CFA to use the EBP/RBP register.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000614 unsigned DwarfFramePtr = RegInfo->getDwarfRegNum(MachineFramePtr, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000615 unsigned CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000616 MCCFIInstruction::createDefCfaRegister(nullptr, DwarfFramePtr));
Eric Christopher612bb692014-04-29 00:16:46 +0000617 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000618 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000619 }
620
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000621 // Mark the FramePtr as live-in in every block.
622 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Pavel Chupinf55eb452014-08-07 09:41:19 +0000623 I->addLiveIn(MachineFramePtr);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000624 } else {
625 NumBytes = StackSize - X86FI->getCalleeSavedFrameSize();
626 }
627
628 // Skip the callee-saved push instructions.
629 bool PushedRegs = false;
630 int StackOffset = 2 * stackGrowth;
631
632 while (MBBI != MBB.end() &&
633 (MBBI->getOpcode() == X86::PUSH32r ||
634 MBBI->getOpcode() == X86::PUSH64r)) {
635 PushedRegs = true;
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000636 unsigned Reg = MBBI->getOperand(0).getReg();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000637 ++MBBI;
638
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000639 if (!HasFP && NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000640 // Mark callee-saved push instruction.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000641 // Define the current CFA rule to use the provided offset.
Rafael Espindola72421862013-05-16 04:59:17 +0000642 assert(StackSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000643 unsigned CFIIndex = MMI.addFrameInst(
644 MCCFIInstruction::createDefCfaOffset(nullptr, StackOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000645 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000646 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000647 StackOffset += stackGrowth;
648 }
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000649
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000650 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000651 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)).addImm(Reg).setMIFlag(
652 MachineInstr::FrameSetup);
653 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000654 }
655
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000656 // Realign stack after we pushed callee-saved registers (so that we'll be
657 // able to calculate their offsets from the frame pointer).
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000658 if (RegInfo->needsStackRealignment(MF)) {
659 assert(HasFP && "There should be a frame pointer if stack is realigned.");
660 MachineInstr *MI =
661 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000662 TII.get(Uses64BitFramePtr ? X86::AND64ri32 : X86::AND32ri), StackPtr)
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000663 .addReg(StackPtr)
664 .addImm(-MaxAlign)
665 .setMIFlag(MachineInstr::FrameSetup);
666
667 // The EFLAGS implicit def is dead.
668 MI->getOperand(3).setIsDead();
669 }
670
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000671 // If there is an SUB32ri of ESP immediately before this instruction, merge
672 // the two. This can be the case when tail call elimination is enabled and
673 // the callee has more arguments then the caller.
674 NumBytes -= mergeSPUpdates(MBB, MBBI, StackPtr, true);
675
676 // If there is an ADD32ri or SUB32ri of ESP immediately after this
677 // instruction, merge the two instructions.
678 mergeSPUpdatesDown(MBB, MBBI, StackPtr, &NumBytes);
679
680 // Adjust stack pointer: ESP -= numbytes.
681
Philip Reames2c52c662014-08-21 22:53:49 +0000682 static const size_t PageSize = 4096;
683
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000684 // Windows and cygwin/mingw require a prologue helper routine when allocating
685 // more than 4K bytes on the stack. Windows uses __chkstk and cygwin/mingw
686 // uses __alloca. __alloca and the 32-bit version of __chkstk will probe the
687 // stack and adjust the stack pointer in one go. The 64-bit version of
688 // __chkstk is only responsible for probing the stack. The 64-bit prologue is
689 // responsible for adjusting the stack pointer. Touching the stack at 4K
690 // increments is necessary to ensure that the guard pages used by the OS
691 // virtual memory manager are allocated in correct sequence.
Philip Reames2c52c662014-08-21 22:53:49 +0000692 if (NumBytes >= PageSize && UseStackProbe) {
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000693 const char *StackProbeSymbol;
Philip Reames34fcca72014-08-21 22:15:20 +0000694 unsigned CallOp;
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000695
Philip Reames34fcca72014-08-21 22:15:20 +0000696 getStackProbeFunction(STI, CallOp, StackProbeSymbol);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000697
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000698 // Check whether EAX is livein for this function.
699 bool isEAXAlive = isEAXLiveIn(MF);
700
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000701 if (isEAXAlive) {
702 // Sanity check that EAX is not livein for this function.
703 // It should not be, so throw an assert.
704 assert(!Is64Bit && "EAX is livein in x64 case!");
705
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000706 // Save EAX
707 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH32r))
Bill Wendling28b6e122011-07-21 00:44:56 +0000708 .addReg(X86::EAX, RegState::Kill)
709 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000710 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000711
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000712 if (Is64Bit) {
713 // Handle the 64-bit Windows ABI case where we need to call __chkstk.
714 // Function prologue is responsible for adjusting the stack pointer.
715 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::RAX)
Bill Wendling28b6e122011-07-21 00:44:56 +0000716 .addImm(NumBytes)
717 .setMIFlag(MachineInstr::FrameSetup);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000718 } else {
719 // Allocate NumBytes-4 bytes on stack in case of isEAXAlive.
720 // We'll also use 4 already allocated bytes for EAX.
721 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX)
Bill Wendling28b6e122011-07-21 00:44:56 +0000722 .addImm(isEAXAlive ? NumBytes - 4 : NumBytes)
723 .setMIFlag(MachineInstr::FrameSetup);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000724 }
725
726 BuildMI(MBB, MBBI, DL,
Philip Reames34fcca72014-08-21 22:15:20 +0000727 TII.get(CallOp))
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000728 .addExternalSymbol(StackProbeSymbol)
729 .addReg(StackPtr, RegState::Define | RegState::Implicit)
Bill Wendling28b6e122011-07-21 00:44:56 +0000730 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit)
731 .setMIFlag(MachineInstr::FrameSetup);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000732
Kai Nacke87b23ae2013-12-13 05:37:05 +0000733 if (Is64Bit) {
734 // MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp
735 // themself. It also does not clobber %rax so we can reuse it when
736 // adjusting %rsp.
Nico Rieck51969be2013-07-08 11:20:11 +0000737 BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64rr), StackPtr)
738 .addReg(StackPtr)
739 .addReg(X86::RAX)
740 .setMIFlag(MachineInstr::FrameSetup);
741 }
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000742 if (isEAXAlive) {
Philip Reames4e8cb792014-08-21 22:19:16 +0000743 // Restore EAX
744 MachineInstr *MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV32rm),
745 X86::EAX),
746 StackPtr, false, NumBytes - 4);
747 MI->setFlag(MachineInstr::FrameSetup);
748 MBB.insert(MBBI, MI);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000749 }
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000750 } else if (NumBytes) {
Pavel Chupinf55eb452014-08-07 09:41:19 +0000751 emitSPUpdate(MBB, MBBI, StackPtr, -(int64_t)NumBytes, Is64Bit, Uses64BitFramePtr,
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000752 UseLEA, TII, *RegInfo);
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000753 }
754
755 int SEHFrameOffset = 0;
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000756 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000757 if (HasFP) {
758 // We need to set frame base offset low enough such that all saved
759 // register offsets would be positive relative to it, but we can't
760 // just use NumBytes, because .seh_setframe offset must be <=240.
761 // So we pretend to have only allocated enough space to spill the
762 // non-volatile registers.
763 // We don't care about the rest of stack allocation, because unwinder
764 // will restore SP to (BP - SEHFrameOffset)
765 for (const CalleeSavedInfo &Info : MFI->getCalleeSavedInfo()) {
766 int offset = MFI->getObjectOffset(Info.getFrameIdx());
Chandler Carruth82cc9642014-10-10 08:27:19 +0000767 SEHFrameOffset = std::max(SEHFrameOffset, std::abs(offset));
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000768 }
769 SEHFrameOffset += SEHFrameOffset % 16; // ensure alignmant
770
771 // This only needs to account for XMM spill slots, GPR slots
Reid Klecknerb5dd9452014-07-01 00:42:47 +0000772 // are covered by the .seh_pushreg's emitted above.
773 unsigned Size = SEHFrameOffset - X86FI->getCalleeSavedFrameSize();
774 if (Size) {
775 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc))
776 .addImm(Size)
777 .setMIFlag(MachineInstr::FrameSetup);
778 }
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000779
780 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame))
781 .addImm(FramePtr)
782 .addImm(SEHFrameOffset)
783 .setMIFlag(MachineInstr::FrameSetup);
784 } else {
785 // SP will be the base register for restoring XMMs
786 if (NumBytes) {
787 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc))
788 .addImm(NumBytes)
789 .setMIFlag(MachineInstr::FrameSetup);
790 }
791 }
792 }
793
794 // Skip the rest of register spilling code
795 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
796 ++MBBI;
797
798 // Emit SEH info for non-GPRs
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000799 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000800 for (const CalleeSavedInfo &Info : MFI->getCalleeSavedInfo()) {
801 unsigned Reg = Info.getReg();
802 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg))
803 continue;
804 assert(X86::FR64RegClass.contains(Reg) && "Unexpected register class");
805
806 int Offset = getFrameIndexOffset(MF, Info.getFrameIdx());
807 Offset += SEHFrameOffset;
808
809 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SaveXMM))
810 .addImm(Reg)
811 .addImm(Offset)
812 .setMIFlag(MachineInstr::FrameSetup);
813 }
814
815 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_EndPrologue))
816 .setMIFlag(MachineInstr::FrameSetup);
817 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000818
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000819 // If we need a base pointer, set it up here. It's whatever the value
820 // of the stack pointer is at this point. Any variable size objects
821 // will be allocated after this, so we can still use the base pointer
822 // to reference locals.
823 if (RegInfo->hasBasePointer(MF)) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000824 // Update the base pointer with the current stack pointer.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000825 unsigned Opc = Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr;
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000826 BuildMI(MBB, MBBI, DL, TII.get(Opc), BasePtr)
827 .addReg(StackPtr)
828 .setMIFlag(MachineInstr::FrameSetup);
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000829 }
830
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000831 if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000832 // Mark end of stack pointer adjustment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000833 if (!HasFP && NumBytes) {
834 // Define the current CFA rule to use the provided offset.
Rafael Espindola84ee6c42013-05-15 22:27:35 +0000835 assert(StackSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000836 unsigned CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000837 MCCFIInstruction::createDefCfaOffset(nullptr,
838 -StackSize + stackGrowth));
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000839
Eric Christopher612bb692014-04-29 00:16:46 +0000840 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000841 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000842 }
843
844 // Emit DWARF info specifying the offsets of the callee-saved registers.
845 if (PushedRegs)
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000846 emitCalleeSavedFrameMoves(MBB, MBBI, DL);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000847 }
848}
849
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000850void X86FrameLowering::emitEpilogue(MachineFunction &MF,
Nick Lewycky34a425b2011-06-14 03:23:52 +0000851 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000852 const MachineFrameInfo *MFI = MF.getFrameInfo();
853 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000854 const X86RegisterInfo *RegInfo =
855 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
856 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000857 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
858 assert(MBBI != MBB.end() && "Returning block has no instructions");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000859 unsigned RetOpcode = MBBI->getOpcode();
860 DebugLoc DL = MBBI->getDebugLoc();
Eric Christopherf4381642014-06-05 22:00:31 +0000861 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000862 bool Is64Bit = STI.is64Bit();
Pavel Chupinf55eb452014-08-07 09:41:19 +0000863 // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit.
864 const bool Uses64BitFramePtr = STI.isTarget64BitLP64() || STI.isTargetNaCl64();
865 const bool Is64BitILP32 = STI.isTarget64BitILP32();
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000866 bool UseLEA = STI.useLeaForSP();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000867 unsigned StackAlign = getStackAlignment();
868 unsigned SlotSize = RegInfo->getSlotSize();
869 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Pavel Chupin12488922014-08-07 11:09:59 +0000870 unsigned MachineFramePtr = Is64BitILP32 ?
871 getX86SubSuperRegister(FramePtr, MVT::i64, false) : FramePtr;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000872 unsigned StackPtr = RegInfo->getStackRegister();
873
Reid Klecknere7040102014-08-04 21:05:27 +0000874 bool IsWinEH =
875 MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() ==
876 ExceptionHandling::WinEH;
877 bool NeedsWinEH = IsWinEH && MF.getFunction()->needsUnwindTableEntry();
878
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000879 switch (RetOpcode) {
880 default:
881 llvm_unreachable("Can only insert epilog into returning blocks");
David Woodhouse79dd5052014-01-08 12:58:07 +0000882 case X86::RETQ:
883 case X86::RETL:
David Woodhouse4e033b02014-01-13 14:05:59 +0000884 case X86::RETIL:
885 case X86::RETIQ:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000886 case X86::TCRETURNdi:
887 case X86::TCRETURNri:
888 case X86::TCRETURNmi:
889 case X86::TCRETURNdi64:
890 case X86::TCRETURNri64:
891 case X86::TCRETURNmi64:
892 case X86::EH_RETURN:
893 case X86::EH_RETURN64:
894 break; // These are ok
895 }
896
897 // Get the number of bytes to allocate from the FrameInfo.
898 uint64_t StackSize = MFI->getStackSize();
899 uint64_t MaxAlign = MFI->getMaxAlignment();
900 unsigned CSSize = X86FI->getCalleeSavedFrameSize();
901 uint64_t NumBytes = 0;
902
903 // If we're forcing a stack realignment we can't rely on just the frame
904 // info, we need to know the ABI stack alignment as well in case we
905 // have a call out. Otherwise just make sure we have some alignment - we'll
906 // go with the minimum.
907 if (ForceStackAlign) {
908 if (MFI->hasCalls())
909 MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign;
910 else
911 MaxAlign = MaxAlign ? MaxAlign : 4;
912 }
913
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000914 if (hasFP(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000915 // Calculate required stack adjustment.
916 uint64_t FrameSize = StackSize - SlotSize;
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000917 if (RegInfo->needsStackRealignment(MF)) {
918 // Callee-saved registers were pushed on stack before the stack
919 // was realigned.
920 FrameSize -= CSSize;
921 NumBytes = (FrameSize + MaxAlign - 1) / MaxAlign * MaxAlign;
922 } else {
923 NumBytes = FrameSize - CSSize;
924 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000925
926 // Pop EBP.
927 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000928 TII.get(Is64Bit ? X86::POP64r : X86::POP32r), MachineFramePtr);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000929 } else {
930 NumBytes = StackSize - CSSize;
931 }
932
933 // Skip the callee-saved pop instructions.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000934 while (MBBI != MBB.begin()) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000935 MachineBasicBlock::iterator PI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000936 unsigned Opc = PI->getOpcode();
937
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000938 if (Opc != X86::POP32r && Opc != X86::POP64r && Opc != X86::DBG_VALUE &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000939 !PI->isTerminator())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000940 break;
941
942 --MBBI;
943 }
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000944 MachineBasicBlock::iterator FirstCSPop = MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000945
946 DL = MBBI->getDebugLoc();
947
948 // If there is an ADD32ri or SUB32ri of ESP immediately before this
949 // instruction, merge the two instructions.
950 if (NumBytes || MFI->hasVarSizedObjects())
951 mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
952
953 // If dynamic alloca is used, then reset esp to point to the last callee-saved
954 // slot before popping them off! Same applies for the case, when stack was
955 // realigned.
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000956 if (RegInfo->needsStackRealignment(MF) || MFI->hasVarSizedObjects()) {
957 if (RegInfo->needsStackRealignment(MF))
958 MBBI = FirstCSPop;
959 if (CSSize != 0) {
Pavel Chupinf55eb452014-08-07 09:41:19 +0000960 unsigned Opc = getLEArOpcode(Uses64BitFramePtr);
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000961 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr),
962 FramePtr, false, -CSSize);
Reid Klecknere7040102014-08-04 21:05:27 +0000963 --MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000964 } else {
Pavel Chupinf55eb452014-08-07 09:41:19 +0000965 unsigned Opc = (Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr);
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000966 BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000967 .addReg(FramePtr);
Reid Klecknere7040102014-08-04 21:05:27 +0000968 --MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000969 }
970 } else if (NumBytes) {
971 // Adjust stack pointer back: ESP += numbytes.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000972 emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, Uses64BitFramePtr, UseLEA,
Eli Bendersky44a40ca2013-02-05 21:53:29 +0000973 TII, *RegInfo);
Reid Klecknere7040102014-08-04 21:05:27 +0000974 --MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000975 }
976
Reid Klecknere7040102014-08-04 21:05:27 +0000977 // Windows unwinder will not invoke function's exception handler if IP is
978 // either in prologue or in epilogue. This behavior causes a problem when a
979 // call immediately precedes an epilogue, because the return address points
980 // into the epilogue. To cope with that, we insert an epilogue marker here,
981 // then replace it with a 'nop' if it ends up immediately after a CALL in the
982 // final emitted code.
983 if (NeedsWinEH)
984 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue));
985
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000986 // We're returning from function via eh_return.
987 if (RetOpcode == X86::EH_RETURN || RetOpcode == X86::EH_RETURN64) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000988 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000989 MachineOperand &DestAddr = MBBI->getOperand(0);
990 assert(DestAddr.isReg() && "Offset should be in register!");
991 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000992 TII.get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr),
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000993 StackPtr).addReg(DestAddr.getReg());
994 } else if (RetOpcode == X86::TCRETURNri || RetOpcode == X86::TCRETURNdi ||
995 RetOpcode == X86::TCRETURNmi ||
996 RetOpcode == X86::TCRETURNri64 || RetOpcode == X86::TCRETURNdi64 ||
997 RetOpcode == X86::TCRETURNmi64) {
998 bool isMem = RetOpcode == X86::TCRETURNmi || RetOpcode == X86::TCRETURNmi64;
999 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesenbbb1a542011-01-13 22:47:43 +00001000 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001001 MachineOperand &JumpTarget = MBBI->getOperand(0);
1002 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
1003 assert(StackAdjust.isImm() && "Expecting immediate value.");
1004
1005 // Adjust stack pointer.
1006 int StackAdj = StackAdjust.getImm();
1007 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
1008 int Offset = 0;
1009 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
1010
1011 // Incoporate the retaddr area.
1012 Offset = StackAdj-MaxTCDelta;
1013 assert(Offset >= 0 && "Offset should never be negative");
1014
1015 if (Offset) {
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001016 // Check for possible merge with preceding ADD instruction.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001017 Offset += mergeSPUpdates(MBB, MBBI, StackPtr, true);
Pavel Chupinf55eb452014-08-07 09:41:19 +00001018 emitSPUpdate(MBB, MBBI, StackPtr, Offset, Is64Bit, Uses64BitFramePtr,
Eli Bendersky44a40ca2013-02-05 21:53:29 +00001019 UseLEA, TII, *RegInfo);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001020 }
1021
1022 // Jump to label or value in register.
1023 if (RetOpcode == X86::TCRETURNdi || RetOpcode == X86::TCRETURNdi64) {
Evan Chengd4b08732010-11-30 23:55:39 +00001024 MachineInstrBuilder MIB =
1025 BuildMI(MBB, MBBI, DL, TII.get((RetOpcode == X86::TCRETURNdi)
1026 ? X86::TAILJMPd : X86::TAILJMPd64));
1027 if (JumpTarget.isGlobal())
1028 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
1029 JumpTarget.getTargetFlags());
1030 else {
1031 assert(JumpTarget.isSymbol());
1032 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
1033 JumpTarget.getTargetFlags());
1034 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001035 } else if (RetOpcode == X86::TCRETURNmi || RetOpcode == X86::TCRETURNmi64) {
1036 MachineInstrBuilder MIB =
1037 BuildMI(MBB, MBBI, DL, TII.get((RetOpcode == X86::TCRETURNmi)
1038 ? X86::TAILJMPm : X86::TAILJMPm64));
1039 for (unsigned i = 0; i != 5; ++i)
1040 MIB.addOperand(MBBI->getOperand(i));
1041 } else if (RetOpcode == X86::TCRETURNri64) {
1042 BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr64)).
1043 addReg(JumpTarget.getReg(), RegState::Kill);
1044 } else {
1045 BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr)).
1046 addReg(JumpTarget.getReg(), RegState::Kill);
1047 }
1048
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001049 MachineInstr *NewMI = std::prev(MBBI);
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001050 NewMI->copyImplicitOps(MF, MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001051
1052 // Delete the pseudo instruction TCRETURN.
1053 MBB.erase(MBBI);
David Woodhouse4e033b02014-01-13 14:05:59 +00001054 } else if ((RetOpcode == X86::RETQ || RetOpcode == X86::RETL ||
1055 RetOpcode == X86::RETIQ || RetOpcode == X86::RETIL) &&
1056 (X86FI->getTCReturnAddrDelta() < 0)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001057 // Add the return addr area delta back since we are not tail calling.
1058 int delta = -1*X86FI->getTCReturnAddrDelta();
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001059 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001060
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001061 // Check for possible merge with preceding ADD instruction.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001062 delta += mergeSPUpdates(MBB, MBBI, StackPtr, true);
Pavel Chupinf55eb452014-08-07 09:41:19 +00001063 emitSPUpdate(MBB, MBBI, StackPtr, delta, Is64Bit, Uses64BitFramePtr, UseLEA, TII,
Eli Bendersky44a40ca2013-02-05 21:53:29 +00001064 *RegInfo);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001065 }
1066}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001067
Eric Christopher4237bf12014-04-29 00:16:33 +00001068int X86FrameLowering::getFrameIndexOffset(const MachineFunction &MF,
1069 int FI) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001070 const X86RegisterInfo *RegInfo =
1071 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +00001072 const MachineFrameInfo *MFI = MF.getFrameInfo();
1073 int Offset = MFI->getObjectOffset(FI) - getOffsetOfLocalArea();
1074 uint64_t StackSize = MFI->getStackSize();
1075
Chad Rosierbdb08ac2012-07-10 17:45:53 +00001076 if (RegInfo->hasBasePointer(MF)) {
1077 assert (hasFP(MF) && "VLAs and dynamic stack realign, but no FP?!");
1078 if (FI < 0) {
1079 // Skip the saved EBP.
1080 return Offset + RegInfo->getSlotSize();
1081 } else {
1082 assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0);
1083 return Offset + StackSize;
1084 }
1085 } else if (RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikov46877782010-11-20 15:59:32 +00001086 if (FI < 0) {
1087 // Skip the saved EBP.
Chad Rosier20b79dc2012-05-23 23:45:10 +00001088 return Offset + RegInfo->getSlotSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +00001089 } else {
Duncan Sandsd278d352011-10-18 12:44:00 +00001090 assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0);
Anton Korobeynikov46877782010-11-20 15:59:32 +00001091 return Offset + StackSize;
1092 }
1093 // FIXME: Support tail calls
1094 } else {
1095 if (!hasFP(MF))
1096 return Offset + StackSize;
1097
1098 // Skip the saved EBP.
Chad Rosier20b79dc2012-05-23 23:45:10 +00001099 Offset += RegInfo->getSlotSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +00001100
1101 // Skip the RETADDR move area
1102 const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1103 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1104 if (TailCallReturnAddrDelta < 0)
1105 Offset -= TailCallReturnAddrDelta;
1106 }
1107
1108 return Offset;
1109}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001110
Alexey Samsonovc4b3ad82012-05-01 15:16:06 +00001111int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
1112 unsigned &FrameReg) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001113 const X86RegisterInfo *RegInfo =
1114 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Alexey Samsonovc4b3ad82012-05-01 15:16:06 +00001115 // We can't calculate offset from frame pointer if the stack is realigned,
Chad Rosierbdb08ac2012-07-10 17:45:53 +00001116 // so enforce usage of stack/base pointer. The base pointer is used when we
1117 // have dynamic allocas in addition to dynamic realignment.
1118 if (RegInfo->hasBasePointer(MF))
1119 FrameReg = RegInfo->getBaseRegister();
1120 else if (RegInfo->needsStackRealignment(MF))
1121 FrameReg = RegInfo->getStackRegister();
1122 else
1123 FrameReg = RegInfo->getFrameRegister(MF);
Alexey Samsonovc4b3ad82012-05-01 15:16:06 +00001124 return getFrameIndexOffset(MF, FI);
1125}
1126
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001127bool X86FrameLowering::assignCalleeSavedSpillSlots(
1128 MachineFunction &MF, const TargetRegisterInfo *TRI,
1129 std::vector<CalleeSavedInfo> &CSI) const {
1130 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00001131 const X86RegisterInfo *RegInfo =
1132 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001133 unsigned SlotSize = RegInfo->getSlotSize();
1134 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001135
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001136 unsigned CalleeSavedFrameSize = 0;
1137 int SpillSlotOffset = getOffsetOfLocalArea() + X86FI->getTCReturnAddrDelta();
1138
1139 if (hasFP(MF)) {
1140 // emitPrologue always spills frame register the first thing.
1141 SpillSlotOffset -= SlotSize;
1142 MFI->CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
1143
1144 // Since emitPrologue and emitEpilogue will handle spilling and restoring of
1145 // the frame register, we can delete it from CSI list and not have to worry
1146 // about avoiding it later.
1147 unsigned FPReg = RegInfo->getFrameRegister(MF);
1148 for (unsigned i = 0; i < CSI.size(); ++i) {
Pavel Chupinf55eb452014-08-07 09:41:19 +00001149 if (TRI->regsOverlap(CSI[i].getReg(),FPReg)) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001150 CSI.erase(CSI.begin() + i);
1151 break;
1152 }
1153 }
1154 }
1155
1156 // Assign slots for GPRs. It increases frame size.
1157 for (unsigned i = CSI.size(); i != 0; --i) {
1158 unsigned Reg = CSI[i - 1].getReg();
1159
1160 if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg))
1161 continue;
1162
1163 SpillSlotOffset -= SlotSize;
1164 CalleeSavedFrameSize += SlotSize;
1165
1166 int SlotIndex = MFI->CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
1167 CSI[i - 1].setFrameIdx(SlotIndex);
1168 }
1169
1170 X86FI->setCalleeSavedFrameSize(CalleeSavedFrameSize);
1171
1172 // Assign slots for XMMs.
1173 for (unsigned i = CSI.size(); i != 0; --i) {
1174 unsigned Reg = CSI[i - 1].getReg();
1175 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg))
1176 continue;
1177
1178 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
1179 // ensure alignment
Chandler Carruth82cc9642014-10-10 08:27:19 +00001180 SpillSlotOffset -= std::abs(SpillSlotOffset) % RC->getAlignment();
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001181 // spill into slot
1182 SpillSlotOffset -= RC->getSize();
1183 int SlotIndex =
1184 MFI->CreateFixedSpillStackObject(RC->getSize(), SpillSlotOffset);
1185 CSI[i - 1].setFrameIdx(SlotIndex);
1186 MFI->ensureMaxAlignment(RC->getAlignment());
1187 }
1188
1189 return true;
1190}
1191
1192bool X86FrameLowering::spillCalleeSavedRegisters(
1193 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1194 const std::vector<CalleeSavedInfo> &CSI,
1195 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001196 DebugLoc DL = MBB.findDebugLoc(MI);
1197
1198 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001199 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Eric Christopherf4381642014-06-05 22:00:31 +00001200 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001201
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001202 // Push GPRs. It increases frame size.
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001203 unsigned Opc = STI.is64Bit() ? X86::PUSH64r : X86::PUSH32r;
1204 for (unsigned i = CSI.size(); i != 0; --i) {
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001205 unsigned Reg = CSI[i - 1].getReg();
1206
1207 if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg))
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001208 continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001209 // Add the callee-saved register as live-in. It's killed at the spill.
1210 MBB.addLiveIn(Reg);
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001211
Charles Davis7ed40cb2011-06-12 01:45:54 +00001212 BuildMI(MBB, MI, DL, TII.get(Opc)).addReg(Reg, RegState::Kill)
1213 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001214 }
1215
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001216 // Make XMM regs spilled. X86 does not have ability of push/pop XMM.
1217 // It can be done by spilling XMMs to stack frame.
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001218 for (unsigned i = CSI.size(); i != 0; --i) {
1219 unsigned Reg = CSI[i-1].getReg();
1220 if (X86::GR64RegClass.contains(Reg) ||
1221 X86::GR32RegClass.contains(Reg))
1222 continue;
1223 // Add the callee-saved register as live-in. It's killed at the spill.
1224 MBB.addLiveIn(Reg);
1225 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001226
1227 TII.storeRegToStackSlot(MBB, MI, Reg, true, CSI[i - 1].getFrameIdx(), RC,
1228 TRI);
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001229 --MI;
1230 MI->setFlag(MachineInstr::FrameSetup);
1231 ++MI;
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001232 }
1233
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001234 return true;
1235}
1236
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001237bool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001238 MachineBasicBlock::iterator MI,
1239 const std::vector<CalleeSavedInfo> &CSI,
1240 const TargetRegisterInfo *TRI) const {
1241 if (CSI.empty())
1242 return false;
1243
1244 DebugLoc DL = MBB.findDebugLoc(MI);
1245
1246 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001247 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Eric Christopherf4381642014-06-05 22:00:31 +00001248 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001249
1250 // Reload XMMs from stack frame.
1251 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1252 unsigned Reg = CSI[i].getReg();
1253 if (X86::GR64RegClass.contains(Reg) ||
1254 X86::GR32RegClass.contains(Reg))
1255 continue;
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001256
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001257 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001258 TII.loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RC, TRI);
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001259 }
1260
1261 // POP GPRs.
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001262 unsigned Opc = STI.is64Bit() ? X86::POP64r : X86::POP32r;
1263 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1264 unsigned Reg = CSI[i].getReg();
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001265 if (!X86::GR64RegClass.contains(Reg) &&
1266 !X86::GR32RegClass.contains(Reg))
1267 continue;
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001268
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001269 BuildMI(MBB, MI, DL, TII.get(Opc), Reg);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001270 }
1271 return true;
1272}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001273
1274void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001275X86FrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Eric Christopher11b05cc2014-06-05 00:09:05 +00001276 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001277 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00001278 const X86RegisterInfo *RegInfo =
1279 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001280 unsigned SlotSize = RegInfo->getSlotSize();
1281
1282 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Tim Northoverecc018c2013-08-04 09:35:57 +00001283 int64_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001284
1285 if (TailCallReturnAddrDelta < 0) {
1286 // create RETURNADDR area
1287 // arg
1288 // arg
1289 // RETADDR
1290 // { ...
1291 // RETADDR area
1292 // ...
1293 // }
1294 // [EBP]
1295 MFI->CreateFixedObject(-TailCallReturnAddrDelta,
Tim Northoverecc018c2013-08-04 09:35:57 +00001296 TailCallReturnAddrDelta - SlotSize, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001297 }
1298
Chad Rosierbdb08ac2012-07-10 17:45:53 +00001299 // Spill the BasePtr if it's used.
1300 if (RegInfo->hasBasePointer(MF))
1301 MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001302}
Rafael Espindolac2174212011-08-30 19:39:58 +00001303
1304static bool
1305HasNestArgument(const MachineFunction *MF) {
1306 const Function *F = MF->getFunction();
1307 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1308 I != E; I++) {
1309 if (I->hasNestAttr())
1310 return true;
1311 }
1312 return false;
1313}
1314
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001315/// GetScratchRegister - Get a temp register for performing work in the
1316/// segmented stack and the Erlang/HiPE stack prologue. Depending on platform
1317/// and the properties of the function either one or two registers will be
1318/// needed. Set primary to true for the first register, false for the second.
Rafael Espindolac2174212011-08-30 19:39:58 +00001319static unsigned
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001320GetScratchRegister(bool Is64Bit, bool IsLP64, const MachineFunction &MF, bool Primary) {
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001321 CallingConv::ID CallingConvention = MF.getFunction()->getCallingConv();
1322
1323 // Erlang stuff.
1324 if (CallingConvention == CallingConv::HiPE) {
1325 if (Is64Bit)
1326 return Primary ? X86::R14 : X86::R13;
1327 else
1328 return Primary ? X86::EBX : X86::EDI;
1329 }
1330
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001331 if (Is64Bit) {
1332 if (IsLP64)
1333 return Primary ? X86::R11 : X86::R12;
1334 else
1335 return Primary ? X86::R11D : X86::R12D;
1336 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001337
David Blaikie46a9f012012-01-20 21:51:11 +00001338 bool IsNested = HasNestArgument(&MF);
1339
1340 if (CallingConvention == CallingConv::X86_FastCall ||
1341 CallingConvention == CallingConv::Fast) {
1342 if (IsNested)
1343 report_fatal_error("Segmented stacks does not support fastcall with "
1344 "nested function.");
1345 return Primary ? X86::EAX : X86::ECX;
Rafael Espindolac2174212011-08-30 19:39:58 +00001346 }
David Blaikie46a9f012012-01-20 21:51:11 +00001347 if (IsNested)
1348 return Primary ? X86::EDX : X86::EAX;
1349 return Primary ? X86::ECX : X86::EAX;
Rafael Espindolac2174212011-08-30 19:39:58 +00001350}
1351
Sanjoy Das006e43b2011-12-03 09:32:07 +00001352// The stack limit in the TCB is set to this many bytes above the actual stack
1353// limit.
1354static const uint64_t kSplitStackAvailable = 256;
1355
Rafael Espindolac2174212011-08-30 19:39:58 +00001356void
1357X86FrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const {
1358 MachineBasicBlock &prologueMBB = MF.front();
1359 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00001360 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Rafael Espindolac2174212011-08-30 19:39:58 +00001361 uint64_t StackSize;
Eric Christopherf4381642014-06-05 22:00:31 +00001362 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Rafael Espindolac2174212011-08-30 19:39:58 +00001363 bool Is64Bit = STI.is64Bit();
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001364 const bool IsLP64 = STI.isTarget64BitLP64();
Rafael Espindolac2174212011-08-30 19:39:58 +00001365 unsigned TlsReg, TlsOffset;
1366 DebugLoc DL;
Rafael Espindolac2174212011-08-30 19:39:58 +00001367
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001368 unsigned ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true);
Rafael Espindolac2174212011-08-30 19:39:58 +00001369 assert(!MF.getRegInfo().isLiveIn(ScratchReg) &&
1370 "Scratch register is live-in");
1371
1372 if (MF.getFunction()->isVarArg())
1373 report_fatal_error("Segmented stacks do not support vararg functions.");
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001374 if (!STI.isTargetLinux() && !STI.isTargetDarwin() &&
Reid Kleckner10110272014-04-01 18:34:21 +00001375 !STI.isTargetWin32() && !STI.isTargetWin64() && !STI.isTargetFreeBSD())
Rafael Espindola00e861e2012-01-12 20:24:30 +00001376 report_fatal_error("Segmented stacks not supported on this platform.");
Rafael Espindolac2174212011-08-30 19:39:58 +00001377
Tim Northoverf9e798b2014-05-22 13:03:43 +00001378 // Eventually StackSize will be calculated by a link-time pass; which will
1379 // also decide whether checking code needs to be injected into this particular
1380 // prologue.
1381 StackSize = MFI->getStackSize();
1382
1383 // Do not generate a prologue for functions with a stack of size zero
1384 if (StackSize == 0)
1385 return;
1386
Rafael Espindolac2174212011-08-30 19:39:58 +00001387 MachineBasicBlock *allocMBB = MF.CreateMachineBasicBlock();
1388 MachineBasicBlock *checkMBB = MF.CreateMachineBasicBlock();
1389 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1390 bool IsNested = false;
1391
1392 // We need to know if the function has a nest argument only in 64 bit mode.
1393 if (Is64Bit)
1394 IsNested = HasNestArgument(&MF);
1395
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001396 // The MOV R10, RAX needs to be in a different block, since the RET we emit in
1397 // allocMBB needs to be last (terminating) instruction.
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001398
Rafael Espindolac2174212011-08-30 19:39:58 +00001399 for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(),
1400 e = prologueMBB.livein_end(); i != e; i++) {
1401 allocMBB->addLiveIn(*i);
1402 checkMBB->addLiveIn(*i);
1403 }
1404
1405 if (IsNested)
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001406 allocMBB->addLiveIn(IsLP64 ? X86::R10 : X86::R10D);
Rafael Espindola66393c12011-10-26 21:12:27 +00001407
Rafael Espindolac2174212011-08-30 19:39:58 +00001408 MF.push_front(allocMBB);
1409 MF.push_front(checkMBB);
1410
Rafael Espindolad90466b2012-01-11 19:00:37 +00001411 // When the frame size is less than 256 we just compare the stack
1412 // boundary directly to the value of the stack pointer, per gcc.
1413 bool CompareStackPointer = StackSize < kSplitStackAvailable;
1414
Rafael Espindolac2174212011-08-30 19:39:58 +00001415 // Read the limit off the current stacklet off the stack_guard location.
1416 if (Is64Bit) {
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001417 if (STI.isTargetLinux()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001418 TlsReg = X86::FS;
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001419 TlsOffset = IsLP64 ? 0x70 : 0x40;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001420 } else if (STI.isTargetDarwin()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001421 TlsReg = X86::GS;
1422 TlsOffset = 0x60 + 90*8; // See pthread_machdep.h. Steal TLS slot 90.
Reid Kleckner10110272014-04-01 18:34:21 +00001423 } else if (STI.isTargetWin64()) {
1424 TlsReg = X86::GS;
1425 TlsOffset = 0x28; // pvArbitrary, reserved for application use
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001426 } else if (STI.isTargetFreeBSD()) {
Rafael Espindola00e861e2012-01-12 20:24:30 +00001427 TlsReg = X86::FS;
1428 TlsOffset = 0x18;
Rafael Espindola10745d32012-01-12 20:22:08 +00001429 } else {
1430 report_fatal_error("Segmented stacks not supported on this platform.");
Rafael Espindolad90466b2012-01-11 19:00:37 +00001431 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001432
Rafael Espindolad90466b2012-01-11 19:00:37 +00001433 if (CompareStackPointer)
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001434 ScratchReg = IsLP64 ? X86::RSP : X86::ESP;
Sanjoy Das006e43b2011-12-03 09:32:07 +00001435 else
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001436 BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::LEA64r : X86::LEA64_32r), ScratchReg).addReg(X86::RSP)
Rafael Espindola6635ae12012-01-11 18:14:03 +00001437 .addImm(1).addReg(0).addImm(-StackSize).addReg(0);
Sanjoy Das006e43b2011-12-03 09:32:07 +00001438
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001439 BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::CMP64rm : X86::CMP32rm)).addReg(ScratchReg)
Rafael Espindola6635ae12012-01-11 18:14:03 +00001440 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg);
Rafael Espindolac2174212011-08-30 19:39:58 +00001441 } else {
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001442 if (STI.isTargetLinux()) {
Rafael Espindola10745d32012-01-12 20:22:08 +00001443 TlsReg = X86::GS;
1444 TlsOffset = 0x30;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001445 } else if (STI.isTargetDarwin()) {
Rafael Espindola10745d32012-01-12 20:22:08 +00001446 TlsReg = X86::GS;
1447 TlsOffset = 0x48 + 90*4;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001448 } else if (STI.isTargetWin32()) {
Rafael Espindola10745d32012-01-12 20:22:08 +00001449 TlsReg = X86::FS;
1450 TlsOffset = 0x14; // pvArbitrary, reserved for application use
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001451 } else if (STI.isTargetFreeBSD()) {
Rafael Espindola00e861e2012-01-12 20:24:30 +00001452 report_fatal_error("Segmented stacks not supported on FreeBSD i386.");
Rafael Espindola10745d32012-01-12 20:22:08 +00001453 } else {
1454 report_fatal_error("Segmented stacks not supported on this platform.");
1455 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001456
Rafael Espindolad90466b2012-01-11 19:00:37 +00001457 if (CompareStackPointer)
Sanjoy Das006e43b2011-12-03 09:32:07 +00001458 ScratchReg = X86::ESP;
1459 else
1460 BuildMI(checkMBB, DL, TII.get(X86::LEA32r), ScratchReg).addReg(X86::ESP)
Rafael Espindola6635ae12012-01-11 18:14:03 +00001461 .addImm(1).addReg(0).addImm(-StackSize).addReg(0);
Sanjoy Das006e43b2011-12-03 09:32:07 +00001462
Reid Kleckner10110272014-04-01 18:34:21 +00001463 if (STI.isTargetLinux() || STI.isTargetWin32() || STI.isTargetWin64()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001464 BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)).addReg(ScratchReg)
1465 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg);
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001466 } else if (STI.isTargetDarwin()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001467
Eric Christopher4237bf12014-04-29 00:16:33 +00001468 // TlsOffset doesn't fit into a mod r/m byte so we need an extra register.
Rafael Espindolad90466b2012-01-11 19:00:37 +00001469 unsigned ScratchReg2;
1470 bool SaveScratch2;
1471 if (CompareStackPointer) {
Eric Christopher4237bf12014-04-29 00:16:33 +00001472 // The primary scratch register is available for holding the TLS offset.
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001473 ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, true);
Rafael Espindolad90466b2012-01-11 19:00:37 +00001474 SaveScratch2 = false;
1475 } else {
1476 // Need to use a second register to hold the TLS offset
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001477 ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, false);
Rafael Espindolad90466b2012-01-11 19:00:37 +00001478
Eric Christopher4237bf12014-04-29 00:16:33 +00001479 // Unfortunately, with fastcc the second scratch register may hold an
1480 // argument.
Rafael Espindolad90466b2012-01-11 19:00:37 +00001481 SaveScratch2 = MF.getRegInfo().isLiveIn(ScratchReg2);
1482 }
1483
Eric Christopher4237bf12014-04-29 00:16:33 +00001484 // If Scratch2 is live-in then it needs to be saved.
Rafael Espindolad90466b2012-01-11 19:00:37 +00001485 assert((!MF.getRegInfo().isLiveIn(ScratchReg2) || SaveScratch2) &&
1486 "Scratch register is live-in and not saved");
1487
1488 if (SaveScratch2)
1489 BuildMI(checkMBB, DL, TII.get(X86::PUSH32r))
1490 .addReg(ScratchReg2, RegState::Kill);
1491
1492 BuildMI(checkMBB, DL, TII.get(X86::MOV32ri), ScratchReg2)
1493 .addImm(TlsOffset);
1494 BuildMI(checkMBB, DL, TII.get(X86::CMP32rm))
1495 .addReg(ScratchReg)
1496 .addReg(ScratchReg2).addImm(1).addReg(0)
1497 .addImm(0)
1498 .addReg(TlsReg);
1499
1500 if (SaveScratch2)
1501 BuildMI(checkMBB, DL, TII.get(X86::POP32r), ScratchReg2);
1502 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001503 }
1504
1505 // This jump is taken if SP >= (Stacklet Limit + Stack Space required).
1506 // It jumps to normal execution of the function body.
Rafael Espindola2b894482012-01-11 18:23:35 +00001507 BuildMI(checkMBB, DL, TII.get(X86::JA_4)).addMBB(&prologueMBB);
Rafael Espindolac2174212011-08-30 19:39:58 +00001508
1509 // On 32 bit we first push the arguments size and then the frame size. On 64
1510 // bit, we pass the stack frame size in r10 and the argument size in r11.
1511 if (Is64Bit) {
1512 // Functions with nested arguments use R10, so it needs to be saved across
1513 // the call to _morestack
1514
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001515 const unsigned RegAX = IsLP64 ? X86::RAX : X86::EAX;
1516 const unsigned Reg10 = IsLP64 ? X86::R10 : X86::R10D;
1517 const unsigned Reg11 = IsLP64 ? X86::R11 : X86::R11D;
1518 const unsigned MOVrr = IsLP64 ? X86::MOV64rr : X86::MOV32rr;
1519 const unsigned MOVri = IsLP64 ? X86::MOV64ri : X86::MOV32ri;
Rafael Espindolac2174212011-08-30 19:39:58 +00001520
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001521 if (IsNested)
1522 BuildMI(allocMBB, DL, TII.get(MOVrr), RegAX).addReg(Reg10);
1523
1524 BuildMI(allocMBB, DL, TII.get(MOVri), Reg10)
Rafael Espindolac2174212011-08-30 19:39:58 +00001525 .addImm(StackSize);
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001526 BuildMI(allocMBB, DL, TII.get(MOVri), Reg11)
Rafael Espindolac2174212011-08-30 19:39:58 +00001527 .addImm(X86FI->getArgumentStackSize());
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001528 MF.getRegInfo().setPhysRegUsed(Reg10);
1529 MF.getRegInfo().setPhysRegUsed(Reg11);
Rafael Espindolac2174212011-08-30 19:39:58 +00001530 } else {
Rafael Espindolac2174212011-08-30 19:39:58 +00001531 BuildMI(allocMBB, DL, TII.get(X86::PUSHi32))
1532 .addImm(X86FI->getArgumentStackSize());
1533 BuildMI(allocMBB, DL, TII.get(X86::PUSHi32))
1534 .addImm(StackSize);
1535 }
1536
1537 // __morestack is in libgcc
1538 if (Is64Bit)
1539 BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32))
1540 .addExternalSymbol("__morestack");
1541 else
1542 BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32))
1543 .addExternalSymbol("__morestack");
1544
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001545 if (IsNested)
Rafael Espindola66393c12011-10-26 21:12:27 +00001546 BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET_RESTORE_R10));
1547 else
1548 BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET));
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001549
Rafael Espindola66393c12011-10-26 21:12:27 +00001550 allocMBB->addSuccessor(&prologueMBB);
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001551
Rafael Espindolac2174212011-08-30 19:39:58 +00001552 checkMBB->addSuccessor(allocMBB);
1553 checkMBB->addSuccessor(&prologueMBB);
1554
Jakob Stoklund Olesen55cf2ed2011-09-24 01:11:19 +00001555#ifdef XDEBUG
Rafael Espindolac2174212011-08-30 19:39:58 +00001556 MF.verify();
1557#endif
1558}
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001559
Yiannis Tsiourisd4842e52013-02-28 16:59:10 +00001560/// Erlang programs may need a special prologue to handle the stack size they
1561/// might need at runtime. That is because Erlang/OTP does not implement a C
1562/// stack but uses a custom implementation of hybrid stack/heap architecture.
1563/// (for more information see Eric Stenman's Ph.D. thesis:
1564/// http://publications.uu.se/uu/fulltext/nbn_se_uu_diva-2688.pdf)
1565///
1566/// CheckStack:
Eric Christopher4237bf12014-04-29 00:16:33 +00001567/// temp0 = sp - MaxStack
1568/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart
Yiannis Tsiourisd4842e52013-02-28 16:59:10 +00001569/// OldStart:
Eric Christopher4237bf12014-04-29 00:16:33 +00001570/// ...
Yiannis Tsiourisd4842e52013-02-28 16:59:10 +00001571/// IncStack:
Eric Christopher4237bf12014-04-29 00:16:33 +00001572/// call inc_stack # doubles the stack space
1573/// temp0 = sp - MaxStack
1574/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001575void X86FrameLowering::adjustForHiPEPrologue(MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001576 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001577 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher11b05cc2014-06-05 00:09:05 +00001578 const unsigned SlotSize =
Eric Christopherfc6de422014-08-05 02:39:49 +00001579 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo())
1580 ->getSlotSize();
Eric Christopherf4381642014-06-05 22:00:31 +00001581 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001582 const bool Is64Bit = STI.is64Bit();
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001583 const bool IsLP64 = STI.isTarget64BitLP64();
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001584 DebugLoc DL;
1585 // HiPE-specific values
1586 const unsigned HipeLeafWords = 24;
1587 const unsigned CCRegisteredArgs = Is64Bit ? 6 : 5;
1588 const unsigned Guaranteed = HipeLeafWords * SlotSize;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001589 unsigned CallerStkArity = MF.getFunction()->arg_size() > CCRegisteredArgs ?
1590 MF.getFunction()->arg_size() - CCRegisteredArgs : 0;
1591 unsigned MaxStack = MFI->getStackSize() + CallerStkArity*SlotSize + SlotSize;
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001592
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001593 assert(STI.isTargetLinux() &&
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001594 "HiPE prologue is only supported on Linux operating systems.");
1595
1596 // Compute the largest caller's frame that is needed to fit the callees'
1597 // frames. This 'MaxStack' is computed from:
1598 //
1599 // a) the fixed frame size, which is the space needed for all spilled temps,
1600 // b) outgoing on-stack parameter areas, and
1601 // c) the minimum stack space this function needs to make available for the
1602 // functions it calls (a tunable ABI property).
1603 if (MFI->hasCalls()) {
1604 unsigned MoreStackForCalls = 0;
1605
1606 for (MachineFunction::iterator MBBI = MF.begin(), MBBE = MF.end();
1607 MBBI != MBBE; ++MBBI)
1608 for (MachineBasicBlock::iterator MI = MBBI->begin(), ME = MBBI->end();
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001609 MI != ME; ++MI) {
1610 if (!MI->isCall())
1611 continue;
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001612
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001613 // Get callee operand.
1614 const MachineOperand &MO = MI->getOperand(0);
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001615
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001616 // Only take account of global function calls (no closures etc.).
1617 if (!MO.isGlobal())
1618 continue;
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001619
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001620 const Function *F = dyn_cast<Function>(MO.getGlobal());
1621 if (!F)
1622 continue;
1623
1624 // Do not update 'MaxStack' for primitive and built-in functions
1625 // (encoded with names either starting with "erlang."/"bif_" or not
1626 // having a ".", such as a simple <Module>.<Function>.<Arity>, or an
1627 // "_", such as the BIF "suspend_0") as they are executed on another
1628 // stack.
1629 if (F->getName().find("erlang.") != StringRef::npos ||
1630 F->getName().find("bif_") != StringRef::npos ||
1631 F->getName().find_first_of("._") == StringRef::npos)
1632 continue;
1633
1634 unsigned CalleeStkArity =
1635 F->arg_size() > CCRegisteredArgs ? F->arg_size()-CCRegisteredArgs : 0;
1636 if (HipeLeafWords - 1 > CalleeStkArity)
1637 MoreStackForCalls = std::max(MoreStackForCalls,
1638 (HipeLeafWords - 1 - CalleeStkArity) * SlotSize);
1639 }
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001640 MaxStack += MoreStackForCalls;
1641 }
1642
1643 // If the stack frame needed is larger than the guaranteed then runtime checks
1644 // and calls to "inc_stack_0" BIF should be inserted in the assembly prologue.
1645 if (MaxStack > Guaranteed) {
1646 MachineBasicBlock &prologueMBB = MF.front();
1647 MachineBasicBlock *stackCheckMBB = MF.CreateMachineBasicBlock();
1648 MachineBasicBlock *incStackMBB = MF.CreateMachineBasicBlock();
1649
1650 for (MachineBasicBlock::livein_iterator I = prologueMBB.livein_begin(),
1651 E = prologueMBB.livein_end(); I != E; I++) {
1652 stackCheckMBB->addLiveIn(*I);
1653 incStackMBB->addLiveIn(*I);
1654 }
1655
1656 MF.push_front(incStackMBB);
1657 MF.push_front(stackCheckMBB);
1658
1659 unsigned ScratchReg, SPReg, PReg, SPLimitOffset;
1660 unsigned LEAop, CMPop, CALLop;
1661 if (Is64Bit) {
1662 SPReg = X86::RSP;
1663 PReg = X86::RBP;
1664 LEAop = X86::LEA64r;
1665 CMPop = X86::CMP64rm;
1666 CALLop = X86::CALL64pcrel32;
1667 SPLimitOffset = 0x90;
1668 } else {
1669 SPReg = X86::ESP;
1670 PReg = X86::EBP;
1671 LEAop = X86::LEA32r;
1672 CMPop = X86::CMP32rm;
1673 CALLop = X86::CALLpcrel32;
1674 SPLimitOffset = 0x4c;
1675 }
1676
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001677 ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true);
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001678 assert(!MF.getRegInfo().isLiveIn(ScratchReg) &&
1679 "HiPE prologue scratch register is live-in");
1680
1681 // Create new MBB for StackCheck:
1682 addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(LEAop), ScratchReg),
1683 SPReg, false, -MaxStack);
1684 // SPLimitOffset is in a fixed heap location (pointed by BP).
1685 addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(CMPop))
1686 .addReg(ScratchReg), PReg, false, SPLimitOffset);
1687 BuildMI(stackCheckMBB, DL, TII.get(X86::JAE_4)).addMBB(&prologueMBB);
1688
1689 // Create new MBB for IncStack:
1690 BuildMI(incStackMBB, DL, TII.get(CALLop)).
1691 addExternalSymbol("inc_stack_0");
1692 addRegOffset(BuildMI(incStackMBB, DL, TII.get(LEAop), ScratchReg),
1693 SPReg, false, -MaxStack);
1694 addRegOffset(BuildMI(incStackMBB, DL, TII.get(CMPop))
1695 .addReg(ScratchReg), PReg, false, SPLimitOffset);
1696 BuildMI(incStackMBB, DL, TII.get(X86::JLE_4)).addMBB(incStackMBB);
1697
1698 stackCheckMBB->addSuccessor(&prologueMBB, 99);
1699 stackCheckMBB->addSuccessor(incStackMBB, 1);
1700 incStackMBB->addSuccessor(&prologueMBB, 99);
1701 incStackMBB->addSuccessor(incStackMBB, 1);
1702 }
1703#ifdef XDEBUG
1704 MF.verify();
1705#endif
1706}
Eli Bendersky8da87162013-02-21 20:05:00 +00001707
1708void X86FrameLowering::
1709eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1710 MachineBasicBlock::iterator I) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001711 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1712 const X86RegisterInfo &RegInfo = *static_cast<const X86RegisterInfo *>(
1713 MF.getSubtarget().getRegisterInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001714 unsigned StackPtr = RegInfo.getStackRegister();
1715 bool reseveCallFrame = hasReservedCallFrame(MF);
1716 int Opcode = I->getOpcode();
1717 bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode();
Eric Christopherf4381642014-06-05 22:00:31 +00001718 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Eli Bendersky8da87162013-02-21 20:05:00 +00001719 bool IsLP64 = STI.isTarget64BitLP64();
1720 DebugLoc DL = I->getDebugLoc();
1721 uint64_t Amount = !reseveCallFrame ? I->getOperand(0).getImm() : 0;
1722 uint64_t CalleeAmt = isDestroy ? I->getOperand(1).getImm() : 0;
1723 I = MBB.erase(I);
1724
1725 if (!reseveCallFrame) {
1726 // If the stack pointer can be changed after prologue, turn the
1727 // adjcallstackup instruction into a 'sub ESP, <amt>' and the
1728 // adjcallstackdown instruction into 'add ESP, <amt>'
1729 // TODO: consider using push / pop instead of sub + store / add
1730 if (Amount == 0)
1731 return;
1732
1733 // We need to keep the stack aligned properly. To do this, we round the
1734 // amount of space needed for the outgoing arguments up to the next
1735 // alignment boundary.
Eric Christopherd9134482014-08-04 21:25:23 +00001736 unsigned StackAlign = MF.getTarget()
1737 .getSubtargetImpl()
1738 ->getFrameLowering()
1739 ->getStackAlignment();
Eli Bendersky8da87162013-02-21 20:05:00 +00001740 Amount = (Amount + StackAlign - 1) / StackAlign * StackAlign;
1741
Craig Topper062a2ba2014-04-25 05:30:21 +00001742 MachineInstr *New = nullptr;
Eli Bendersky8da87162013-02-21 20:05:00 +00001743 if (Opcode == TII.getCallFrameSetupOpcode()) {
1744 New = BuildMI(MF, DL, TII.get(getSUBriOpcode(IsLP64, Amount)),
1745 StackPtr)
1746 .addReg(StackPtr)
1747 .addImm(Amount);
1748 } else {
1749 assert(Opcode == TII.getCallFrameDestroyOpcode());
1750
1751 // Factor out the amount the callee already popped.
1752 Amount -= CalleeAmt;
1753
1754 if (Amount) {
1755 unsigned Opc = getADDriOpcode(IsLP64, Amount);
1756 New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
1757 .addReg(StackPtr).addImm(Amount);
1758 }
1759 }
1760
1761 if (New) {
1762 // The EFLAGS implicit def is dead.
1763 New->getOperand(3).setIsDead();
1764
1765 // Replace the pseudo instruction with a new instruction.
1766 MBB.insert(I, New);
1767 }
1768
1769 return;
1770 }
1771
1772 if (Opcode == TII.getCallFrameDestroyOpcode() && CalleeAmt) {
1773 // If we are performing frame pointer elimination and if the callee pops
1774 // something off the stack pointer, add it back. We do this until we have
1775 // more advanced stack pointer tracking ability.
1776 unsigned Opc = getSUBriOpcode(IsLP64, CalleeAmt);
1777 MachineInstr *New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
1778 .addReg(StackPtr).addImm(CalleeAmt);
1779
1780 // The EFLAGS implicit def is dead.
1781 New->getOperand(3).setIsDead();
1782
1783 // We are not tracking the stack pointer adjustment by the callee, so make
1784 // sure we restore the stack pointer immediately after the call, there may
1785 // be spill code inserted between the CALL and ADJCALLSTACKUP instructions.
1786 MachineBasicBlock::iterator B = MBB.begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001787 while (I != B && !std::prev(I)->isCall())
Eli Bendersky8da87162013-02-21 20:05:00 +00001788 --I;
1789 MBB.insert(I, New);
1790 }
1791}
1792