blob: cfd68a5c1b418a01392caa65fbab6385b5f7f7ed [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"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000033
34using namespace llvm;
35
36// FIXME: completely move here.
37extern cl::opt<bool> ForceStackAlign;
38
Anton Korobeynikov2f931282011-01-10 12:39:04 +000039bool X86FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000040 return !MF.getFrameInfo()->hasVarSizedObjects();
41}
42
43/// hasFP - Return true if the specified function should have a dedicated frame
44/// pointer register. This is true if the function has variable sized allocas
45/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000046bool X86FrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000047 const MachineFrameInfo *MFI = MF.getFrameInfo();
48 const MachineModuleInfo &MMI = MF.getMMI();
Eric Christopherfc6de422014-08-05 02:39:49 +000049 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000050
Nick Lewycky50f02cb2011-12-02 22:16:29 +000051 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
Chad Rosier20b79dc2012-05-23 23:45:10 +000052 RegInfo->needsStackRealignment(MF) ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000053 MFI->hasVarSizedObjects() ||
Reid Kleckneree088972013-12-10 18:27:32 +000054 MFI->isFrameAddressTaken() || MFI->hasInlineAsmWithSPAdjust() ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000055 MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() ||
Juergen Ributzka99bd3cb2014-10-02 22:21:49 +000056 MMI.callsUnwindInit() || MMI.callsEHReturn() ||
57 MFI->hasStackMap() || MFI->hasPatchPoint());
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000058}
59
Eli Bendersky8da87162013-02-21 20:05:00 +000060static unsigned getSUBriOpcode(unsigned IsLP64, int64_t Imm) {
61 if (IsLP64) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000062 if (isInt<8>(Imm))
63 return X86::SUB64ri8;
64 return X86::SUB64ri32;
65 } else {
66 if (isInt<8>(Imm))
67 return X86::SUB32ri8;
68 return X86::SUB32ri;
69 }
70}
71
Eli Benderskyef4558a2013-02-06 20:43:57 +000072static unsigned getADDriOpcode(unsigned IsLP64, int64_t Imm) {
73 if (IsLP64) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000074 if (isInt<8>(Imm))
75 return X86::ADD64ri8;
76 return X86::ADD64ri32;
77 } else {
78 if (isInt<8>(Imm))
79 return X86::ADD32ri8;
80 return X86::ADD32ri;
81 }
82}
83
Eli Benderskyef4558a2013-02-06 20:43:57 +000084static unsigned getLEArOpcode(unsigned IsLP64) {
85 return IsLP64 ? X86::LEA64r : X86::LEA32r;
Evan Cheng1b81fdd2012-02-07 22:50:41 +000086}
87
Evan Cheng65089fc2011-01-03 22:53:22 +000088/// findDeadCallerSavedReg - Return a caller-saved register that isn't live
89/// when it reaches the "return" instruction. We can then pop a stack object
90/// to this register without worry about clobbering it.
91static unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator &MBBI,
93 const TargetRegisterInfo &TRI,
94 bool Is64Bit) {
95 const MachineFunction *MF = MBB.getParent();
96 const Function *F = MF->getFunction();
97 if (!F || MF->getMMI().callsEHReturn())
98 return 0;
99
Craig Topper1d326582012-03-04 10:43:23 +0000100 static const uint16_t CallerSavedRegs32Bit[] = {
Andrew Trick210bf832011-08-12 00:49:19 +0000101 X86::EAX, X86::EDX, X86::ECX, 0
Evan Cheng65089fc2011-01-03 22:53:22 +0000102 };
103
Craig Topper1d326582012-03-04 10:43:23 +0000104 static const uint16_t CallerSavedRegs64Bit[] = {
Evan Cheng65089fc2011-01-03 22:53:22 +0000105 X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
Andrew Trick210bf832011-08-12 00:49:19 +0000106 X86::R8, X86::R9, X86::R10, X86::R11, 0
Evan Cheng65089fc2011-01-03 22:53:22 +0000107 };
108
109 unsigned Opc = MBBI->getOpcode();
110 switch (Opc) {
111 default: return 0;
David Woodhouse79dd5052014-01-08 12:58:07 +0000112 case X86::RETL:
113 case X86::RETQ:
David Woodhouse4e033b02014-01-13 14:05:59 +0000114 case X86::RETIL:
115 case X86::RETIQ:
Evan Cheng65089fc2011-01-03 22:53:22 +0000116 case X86::TCRETURNdi:
117 case X86::TCRETURNri:
118 case X86::TCRETURNmi:
119 case X86::TCRETURNdi64:
120 case X86::TCRETURNri64:
121 case X86::TCRETURNmi64:
122 case X86::EH_RETURN:
123 case X86::EH_RETURN64: {
Craig Topper1d326582012-03-04 10:43:23 +0000124 SmallSet<uint16_t, 8> Uses;
Evan Cheng65089fc2011-01-03 22:53:22 +0000125 for (unsigned i = 0, e = MBBI->getNumOperands(); i != e; ++i) {
126 MachineOperand &MO = MBBI->getOperand(i);
127 if (!MO.isReg() || MO.isDef())
128 continue;
129 unsigned Reg = MO.getReg();
130 if (!Reg)
131 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000132 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
133 Uses.insert(*AI);
Evan Cheng65089fc2011-01-03 22:53:22 +0000134 }
135
Craig Topper1d326582012-03-04 10:43:23 +0000136 const uint16_t *CS = Is64Bit ? CallerSavedRegs64Bit : CallerSavedRegs32Bit;
Evan Cheng65089fc2011-01-03 22:53:22 +0000137 for (; *CS; ++CS)
138 if (!Uses.count(*CS))
139 return *CS;
140 }
141 }
142
143 return 0;
144}
145
146
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000147/// emitSPUpdate - Emit a series of instructions to increment / decrement the
148/// stack pointer by a constant value.
149static
150void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Evan Cheng65089fc2011-01-03 22:53:22 +0000151 unsigned StackPtr, int64_t NumBytes,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000152 bool Is64BitTarget, bool Is64BitStackPtr, bool UseLEA,
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000153 const TargetInstrInfo &TII, const TargetRegisterInfo &TRI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000154 bool isSub = NumBytes < 0;
155 uint64_t Offset = isSub ? -NumBytes : NumBytes;
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000156 unsigned Opc;
157 if (UseLEA)
Pavel Chupinf55eb452014-08-07 09:41:19 +0000158 Opc = getLEArOpcode(Is64BitStackPtr);
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000159 else
160 Opc = isSub
Pavel Chupinf55eb452014-08-07 09:41:19 +0000161 ? getSUBriOpcode(Is64BitStackPtr, Offset)
162 : getADDriOpcode(Is64BitStackPtr, Offset);
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000163
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000164 uint64_t Chunk = (1LL << 31) - 1;
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000165 DebugLoc DL = MBB.findDebugLoc(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000166
167 while (Offset) {
168 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
Pavel Chupinf55eb452014-08-07 09:41:19 +0000169 if (ThisVal == (Is64BitTarget ? 8 : 4)) {
Evan Cheng65089fc2011-01-03 22:53:22 +0000170 // Use push / pop instead.
171 unsigned Reg = isSub
Pavel Chupinf55eb452014-08-07 09:41:19 +0000172 ? (unsigned)(Is64BitTarget ? X86::RAX : X86::EAX)
173 : findDeadCallerSavedReg(MBB, MBBI, TRI, Is64BitTarget);
Evan Cheng65089fc2011-01-03 22:53:22 +0000174 if (Reg) {
175 Opc = isSub
Pavel Chupinf55eb452014-08-07 09:41:19 +0000176 ? (Is64BitTarget ? X86::PUSH64r : X86::PUSH32r)
177 : (Is64BitTarget ? X86::POP64r : X86::POP32r);
Charles Davis7ed40cb2011-06-12 01:45:54 +0000178 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opc))
Evan Cheng65089fc2011-01-03 22:53:22 +0000179 .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub));
Charles Davis7ed40cb2011-06-12 01:45:54 +0000180 if (isSub)
181 MI->setFlag(MachineInstr::FrameSetup);
Evan Cheng65089fc2011-01-03 22:53:22 +0000182 Offset -= ThisVal;
183 continue;
184 }
185 }
186
Craig Topper062a2ba2014-04-25 05:30:21 +0000187 MachineInstr *MI = nullptr;
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000188
189 if (UseLEA) {
190 MI = addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr),
191 StackPtr, false, isSub ? -ThisVal : ThisVal);
192 } else {
193 MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
194 .addReg(StackPtr)
195 .addImm(ThisVal);
196 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead.
197 }
198
Charles Davis7ed40cb2011-06-12 01:45:54 +0000199 if (isSub)
200 MI->setFlag(MachineInstr::FrameSetup);
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000201
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000202 Offset -= ThisVal;
203 }
204}
205
206/// mergeSPUpdatesUp - Merge two stack-manipulating instructions upper iterator.
207static
208void mergeSPUpdatesUp(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
Craig Topper062a2ba2014-04-25 05:30:21 +0000209 unsigned StackPtr, uint64_t *NumBytes = nullptr) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000210 if (MBBI == MBB.begin()) return;
211
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000212 MachineBasicBlock::iterator PI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000213 unsigned Opc = PI->getOpcode();
214 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000215 Opc == X86::ADD32ri || Opc == X86::ADD32ri8 ||
216 Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000217 PI->getOperand(0).getReg() == StackPtr) {
218 if (NumBytes)
219 *NumBytes += PI->getOperand(2).getImm();
220 MBB.erase(PI);
221 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
222 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
223 PI->getOperand(0).getReg() == StackPtr) {
224 if (NumBytes)
225 *NumBytes -= PI->getOperand(2).getImm();
226 MBB.erase(PI);
227 }
228}
229
Eric Christopher4237bf12014-04-29 00:16:33 +0000230/// mergeSPUpdatesDown - Merge two stack-manipulating instructions lower
231/// iterator.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000232static
233void mergeSPUpdatesDown(MachineBasicBlock &MBB,
234 MachineBasicBlock::iterator &MBBI,
Craig Topper062a2ba2014-04-25 05:30:21 +0000235 unsigned StackPtr, uint64_t *NumBytes = nullptr) {
Sanjoy Dasf60485c2011-12-01 19:15:08 +0000236 // FIXME: THIS ISN'T RUN!!!
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000237 return;
238
239 if (MBBI == MBB.end()) return;
240
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000241 MachineBasicBlock::iterator NI = std::next(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000242 if (NI == MBB.end()) return;
243
244 unsigned Opc = NI->getOpcode();
245 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
246 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
247 NI->getOperand(0).getReg() == StackPtr) {
248 if (NumBytes)
249 *NumBytes -= NI->getOperand(2).getImm();
250 MBB.erase(NI);
251 MBBI = NI;
252 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
253 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
254 NI->getOperand(0).getReg() == StackPtr) {
255 if (NumBytes)
256 *NumBytes += NI->getOperand(2).getImm();
257 MBB.erase(NI);
258 MBBI = NI;
259 }
260}
261
262/// mergeSPUpdates - Checks the instruction before/after the passed
Eric Christopher4237bf12014-04-29 00:16:33 +0000263/// instruction. If it is an ADD/SUB/LEA instruction it is deleted argument and
264/// the stack adjustment is returned as a positive value for ADD/LEA and a
265/// negative for SUB.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000266static int mergeSPUpdates(MachineBasicBlock &MBB,
Eric Christopher4237bf12014-04-29 00:16:33 +0000267 MachineBasicBlock::iterator &MBBI, unsigned StackPtr,
268 bool doMergeWithPrevious) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000269 if ((doMergeWithPrevious && MBBI == MBB.begin()) ||
270 (!doMergeWithPrevious && MBBI == MBB.end()))
271 return 0;
272
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000273 MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI;
Craig Topper062a2ba2014-04-25 05:30:21 +0000274 MachineBasicBlock::iterator NI = doMergeWithPrevious ? nullptr
275 : std::next(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000276 unsigned Opc = PI->getOpcode();
277 int Offset = 0;
278
279 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000280 Opc == X86::ADD32ri || Opc == X86::ADD32ri8 ||
281 Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000282 PI->getOperand(0).getReg() == StackPtr){
283 Offset += PI->getOperand(2).getImm();
284 MBB.erase(PI);
285 if (!doMergeWithPrevious) MBBI = NI;
286 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
287 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
288 PI->getOperand(0).getReg() == StackPtr) {
289 Offset -= PI->getOperand(2).getImm();
290 MBB.erase(PI);
291 if (!doMergeWithPrevious) MBBI = NI;
292 }
293
294 return Offset;
295}
296
297static bool isEAXLiveIn(MachineFunction &MF) {
298 for (MachineRegisterInfo::livein_iterator II = MF.getRegInfo().livein_begin(),
299 EE = MF.getRegInfo().livein_end(); II != EE; ++II) {
300 unsigned Reg = II->first;
301
302 if (Reg == X86::EAX || Reg == X86::AX ||
303 Reg == X86::AH || Reg == X86::AL)
304 return true;
305 }
306
307 return false;
308}
309
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000310void
311X86FrameLowering::emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
312 MachineBasicBlock::iterator MBBI,
313 DebugLoc DL) const {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000314 MachineFunction &MF = *MBB.getParent();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000315 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000316 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000317 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +0000318 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000319
320 // Add callee saved registers to move list.
321 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
322 if (CSI.empty()) return;
323
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000324 // Calculate offsets.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000325 for (std::vector<CalleeSavedInfo>::const_iterator
326 I = CSI.begin(), E = CSI.end(); I != E; ++I) {
327 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
328 unsigned Reg = I->getReg();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000329
Bill Wendlingbc07a892013-06-18 07:20:20 +0000330 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000331 unsigned CFIIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +0000332 MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, DwarfReg,
333 Offset));
Eric Christopher612bb692014-04-29 00:16:46 +0000334 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
335 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000336 }
337}
338
Nadav Rotem1bef5a02012-12-23 07:30:09 +0000339/// usesTheStack - This function checks if any of the users of EFLAGS
Nadav Rotemd5aae982012-12-21 23:48:49 +0000340/// copies the EFLAGS. We know that the code that lowers COPY of EFLAGS has
341/// to use the stack, and if we don't adjust the stack we clobber the first
342/// frame index.
Nadav Rotem1bef5a02012-12-23 07:30:09 +0000343/// See X86InstrInfo::copyPhysReg.
Bill Wendling28519072013-08-15 18:46:14 +0000344static bool usesTheStack(const MachineFunction &MF) {
345 const MachineRegisterInfo &MRI = MF.getRegInfo();
Nadav Rotemd5aae982012-12-21 23:48:49 +0000346
Owen Anderson16c6bf42014-03-13 23:12:04 +0000347 for (MachineRegisterInfo::reg_instr_iterator
348 ri = MRI.reg_instr_begin(X86::EFLAGS), re = MRI.reg_instr_end();
349 ri != re; ++ri)
Nadav Rotemd5aae982012-12-21 23:48:49 +0000350 if (ri->isCopy())
351 return true;
352
353 return false;
354}
355
Philip Reames34fcca72014-08-21 22:15:20 +0000356void X86FrameLowering::getStackProbeFunction(const X86Subtarget &STI,
357 unsigned &CallOp,
358 const char *&Symbol) {
359 CallOp = STI.is64Bit() ? X86::W64ALLOCA : X86::CALLpcrel32;
360
361 if (STI.is64Bit()) {
362 if (STI.isTargetCygMing()) {
363 Symbol = "___chkstk_ms";
364 } else {
365 Symbol = "__chkstk";
366 }
367 } else if (STI.isTargetCygMing())
368 Symbol = "_alloca";
369 else
370 Symbol = "_chkstk";
371}
372
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000373/// emitPrologue - Push callee-saved registers onto the stack, which
374/// automatically adjust the stack pointer. Adjust the stack pointer to allocate
375/// space for local variables. Also emit labels used by the exception handler to
376/// generate the exception handling frames.
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000377
378/*
379 Here's a gist of what gets emitted:
380
381 ; Establish frame pointer, if needed
382 [if needs FP]
383 push %rbp
384 .cfi_def_cfa_offset 16
385 .cfi_offset %rbp, -16
386 .seh_pushreg %rpb
387 mov %rsp, %rbp
388 .cfi_def_cfa_register %rbp
389
390 ; Spill general-purpose registers
391 [for all callee-saved GPRs]
392 pushq %<reg>
393 [if not needs FP]
394 .cfi_def_cfa_offset (offset from RETADDR)
395 .seh_pushreg %<reg>
396
397 ; If the required stack alignment > default stack alignment
398 ; rsp needs to be re-aligned. This creates a "re-alignment gap"
399 ; of unknown size in the stack frame.
400 [if stack needs re-alignment]
401 and $MASK, %rsp
402
403 ; Allocate space for locals
404 [if target is Windows and allocated space > 4096 bytes]
405 ; Windows needs special care for allocations larger
406 ; than one page.
407 mov $NNN, %rax
408 call ___chkstk_ms/___chkstk
409 sub %rax, %rsp
410 [else]
411 sub $NNN, %rsp
412
413 [if needs FP]
414 .seh_stackalloc (size of XMM spill slots)
415 .seh_setframe %rbp, SEHFrameOffset ; = size of all spill slots
416 [else]
417 .seh_stackalloc NNN
418
419 ; Spill XMMs
420 ; Note, that while only Windows 64 ABI specifies XMMs as callee-preserved,
421 ; they may get spilled on any platform, if the current function
422 ; calls @llvm.eh.unwind.init
423 [if needs FP]
424 [for all callee-saved XMM registers]
425 movaps %<xmm reg>, -MMM(%rbp)
426 [for all callee-saved XMM registers]
427 .seh_savexmm %<xmm reg>, (-MMM + SEHFrameOffset)
428 ; i.e. the offset relative to (%rbp - SEHFrameOffset)
429 [else]
430 [for all callee-saved XMM registers]
431 movaps %<xmm reg>, KKK(%rsp)
432 [for all callee-saved XMM registers]
433 .seh_savexmm %<xmm reg>, KKK
434
435 .seh_endprologue
436
437 [if needs base pointer]
438 mov %rsp, %rbx
439
440 ; Emit CFI info
441 [if needs FP]
442 [for all callee-saved registers]
443 .cfi_offset %<reg>, (offset from %rbp)
444 [else]
445 .cfi_def_cfa_offset (offset from RETADDR)
446 [for all callee-saved registers]
447 .cfi_offset %<reg>, (offset from %rsp)
448
449 Notes:
450 - .seh directives are emitted only for Windows 64 ABI
451 - .cfi directives are emitted for all other ABIs
452 - for 32-bit code, substitute %e?? registers for %r??
453*/
454
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000455void X86FrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000456 MachineBasicBlock &MBB = MF.front(); // Prologue goes in entry BB.
457 MachineBasicBlock::iterator MBBI = MBB.begin();
458 MachineFrameInfo *MFI = MF.getFrameInfo();
459 const Function *Fn = MF.getFunction();
Eric Christopherfc6de422014-08-05 02:39:49 +0000460 const X86RegisterInfo *RegInfo =
461 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
462 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000463 MachineModuleInfo &MMI = MF.getMMI();
464 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000465 uint64_t MaxAlign = MFI->getMaxAlignment(); // Desired stack alignment.
466 uint64_t StackSize = MFI->getStackSize(); // Number of bytes to allocate.
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000467 bool HasFP = hasFP(MF);
Eric Christopherf4381642014-06-05 22:00:31 +0000468 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000469 bool Is64Bit = STI.is64Bit();
Pavel Chupinf55eb452014-08-07 09:41:19 +0000470 // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit.
471 const bool Uses64BitFramePtr = STI.isTarget64BitLP64() || STI.isTargetNaCl64();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000472 bool IsWin64 = STI.isTargetWin64();
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000473 bool IsWinEH =
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000474 MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() ==
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000475 ExceptionHandling::WinEH; // Not necessarily synonymous with IsWin64.
476 bool NeedsWinEH = IsWinEH && Fn->needsUnwindTableEntry();
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000477 bool NeedsDwarfCFI =
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000478 !IsWinEH && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry());
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000479 bool UseLEA = STI.useLeaForSP();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000480 unsigned StackAlign = getStackAlignment();
481 unsigned SlotSize = RegInfo->getSlotSize();
482 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Pavel Chupinf55eb452014-08-07 09:41:19 +0000483 const unsigned MachineFramePtr = STI.isTarget64BitILP32() ?
484 getX86SubSuperRegister(FramePtr, MVT::i64, false) : FramePtr;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000485 unsigned StackPtr = RegInfo->getStackRegister();
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000486 unsigned BasePtr = RegInfo->getBaseRegister();
Bill Wendlingf27e3312013-09-10 00:20:27 +0000487 DebugLoc DL;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000488
489 // If we're forcing a stack realignment we can't rely on just the frame
490 // info, we need to know the ABI stack alignment as well in case we
491 // have a call out. Otherwise just make sure we have some alignment - we'll
492 // go with the minimum SlotSize.
493 if (ForceStackAlign) {
494 if (MFI->hasCalls())
495 MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign;
496 else if (MaxAlign < SlotSize)
497 MaxAlign = SlotSize;
498 }
499
500 // Add RETADDR move area to callee saved frame size.
501 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
502 if (TailCallReturnAddrDelta < 0)
503 X86FI->setCalleeSavedFrameSize(
504 X86FI->getCalleeSavedFrameSize() - TailCallReturnAddrDelta);
505
Philip Reames2c52c662014-08-21 22:53:49 +0000506 bool UseStackProbe = (STI.isOSWindows() && !STI.isTargetMacho());
507
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000508 // If this is x86-64 and the Red Zone is not disabled, if we are a leaf
509 // function, and use up to 128 bytes of stack space, don't have a frame
510 // pointer, calls, or dynamic alloca then we do not need to adjust the
Nadav Rotemd5aae982012-12-21 23:48:49 +0000511 // stack pointer (we fit in the Red Zone). We also check that we don't
512 // push and pop from the stack.
Bill Wendling698e84f2012-12-30 10:32:01 +0000513 if (Is64Bit && !Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
514 Attribute::NoRedZone) &&
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000515 !RegInfo->needsStackRealignment(MF) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000516 !MFI->hasVarSizedObjects() && // No dynamic alloca.
517 !MFI->adjustsStack() && // No calls.
518 !IsWin64 && // Win64 has no Red Zone
Nadav Rotem1bef5a02012-12-23 07:30:09 +0000519 !usesTheStack(MF) && // Don't push and pop.
Reid Kleckner9c658212014-04-10 22:58:43 +0000520 !MF.shouldSplitStack()) { // Regular stack
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000521 uint64_t MinSize = X86FI->getCalleeSavedFrameSize();
522 if (HasFP) MinSize += SlotSize;
523 StackSize = std::max(MinSize, StackSize > 128 ? StackSize - 128 : 0);
524 MFI->setStackSize(StackSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000525 }
526
527 // Insert stack pointer adjustment for later moving of return addr. Only
528 // applies to tail call optimized functions where the callee argument stack
529 // size is bigger than the callers.
530 if (TailCallReturnAddrDelta < 0) {
531 MachineInstr *MI =
532 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000533 TII.get(getSUBriOpcode(Uses64BitFramePtr, -TailCallReturnAddrDelta)),
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000534 StackPtr)
535 .addReg(StackPtr)
Charles Davis7ed40cb2011-06-12 01:45:54 +0000536 .addImm(-TailCallReturnAddrDelta)
537 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000538 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead.
539 }
540
541 // Mapping for machine moves:
542 //
543 // DST: VirtualFP AND
544 // SRC: VirtualFP => DW_CFA_def_cfa_offset
545 // ELSE => DW_CFA_def_cfa
546 //
547 // SRC: VirtualFP AND
548 // DST: Register => DW_CFA_def_cfa_register
549 //
550 // ELSE
551 // OFFSET < 0 => DW_CFA_offset_extended_sf
552 // REG < 64 => DW_CFA_offset + Reg
553 // ELSE => DW_CFA_offset_extended
554
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000555 uint64_t NumBytes = 0;
Michael Liao6d810bd2012-10-25 06:29:14 +0000556 int stackGrowth = -SlotSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000557
558 if (HasFP) {
559 // Calculate required stack adjustment.
560 uint64_t FrameSize = StackSize - SlotSize;
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000561 if (RegInfo->needsStackRealignment(MF)) {
562 // Callee-saved registers are pushed on stack before the stack
563 // is realigned.
564 FrameSize -= X86FI->getCalleeSavedFrameSize();
565 NumBytes = (FrameSize + MaxAlign - 1) / MaxAlign * MaxAlign;
566 } else {
567 NumBytes = FrameSize - X86FI->getCalleeSavedFrameSize();
568 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000569
570 // Get the offset of the stack slot for the EBP register, which is
571 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
572 // Update the frame offset adjustment.
573 MFI->setOffsetAdjustment(-NumBytes);
574
575 // Save EBP/RBP into the appropriate stack slot.
576 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
Pavel Chupinf55eb452014-08-07 09:41:19 +0000577 .addReg(MachineFramePtr, RegState::Kill)
Charles Davis7ed40cb2011-06-12 01:45:54 +0000578 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000579
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000580 if (NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000581 // Mark the place where EBP/RBP was saved.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000582 // Define the current CFA rule to use the provided offset.
Rafael Espindola84ee6c42013-05-15 22:27:35 +0000583 assert(StackSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000584 unsigned CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000585 MCCFIInstruction::createDefCfaOffset(nullptr, 2 * stackGrowth));
Eric Christopher612bb692014-04-29 00:16:46 +0000586 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000587 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000588
589 // Change the rule for the FramePtr to be an "offset" rule.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000590 unsigned DwarfFramePtr = RegInfo->getDwarfRegNum(MachineFramePtr, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000591 CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000592 MCCFIInstruction::createOffset(nullptr,
593 DwarfFramePtr, 2 * stackGrowth));
Eric Christopher612bb692014-04-29 00:16:46 +0000594 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000595 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000596 }
597
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000598 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000599 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
600 .addImm(FramePtr)
601 .setMIFlag(MachineInstr::FrameSetup);
602 }
603
Bill Wendlingb97270d2011-07-25 18:00:28 +0000604 // Update EBP with the new base value.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000605 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000606 TII.get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), FramePtr)
Charles Davis7ed40cb2011-06-12 01:45:54 +0000607 .addReg(StackPtr)
608 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000609
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000610 if (NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000611 // Mark effective beginning of when frame pointer becomes valid.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000612 // Define the current CFA to use the EBP/RBP register.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000613 unsigned DwarfFramePtr = RegInfo->getDwarfRegNum(MachineFramePtr, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000614 unsigned CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000615 MCCFIInstruction::createDefCfaRegister(nullptr, DwarfFramePtr));
Eric Christopher612bb692014-04-29 00:16:46 +0000616 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000617 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000618 }
619
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000620 // Mark the FramePtr as live-in in every block.
621 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Pavel Chupinf55eb452014-08-07 09:41:19 +0000622 I->addLiveIn(MachineFramePtr);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000623 } else {
624 NumBytes = StackSize - X86FI->getCalleeSavedFrameSize();
625 }
626
627 // Skip the callee-saved push instructions.
628 bool PushedRegs = false;
629 int StackOffset = 2 * stackGrowth;
630
631 while (MBBI != MBB.end() &&
632 (MBBI->getOpcode() == X86::PUSH32r ||
633 MBBI->getOpcode() == X86::PUSH64r)) {
634 PushedRegs = true;
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000635 unsigned Reg = MBBI->getOperand(0).getReg();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000636 ++MBBI;
637
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000638 if (!HasFP && NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000639 // Mark callee-saved push instruction.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000640 // Define the current CFA rule to use the provided offset.
Rafael Espindola72421862013-05-16 04:59:17 +0000641 assert(StackSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000642 unsigned CFIIndex = MMI.addFrameInst(
643 MCCFIInstruction::createDefCfaOffset(nullptr, StackOffset));
Eric Christopher612bb692014-04-29 00:16:46 +0000644 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000645 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000646 StackOffset += stackGrowth;
647 }
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000648
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000649 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000650 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)).addImm(Reg).setMIFlag(
651 MachineInstr::FrameSetup);
652 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000653 }
654
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000655 // Realign stack after we pushed callee-saved registers (so that we'll be
656 // able to calculate their offsets from the frame pointer).
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000657 if (RegInfo->needsStackRealignment(MF)) {
658 assert(HasFP && "There should be a frame pointer if stack is realigned.");
659 MachineInstr *MI =
660 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000661 TII.get(Uses64BitFramePtr ? X86::AND64ri32 : X86::AND32ri), StackPtr)
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000662 .addReg(StackPtr)
663 .addImm(-MaxAlign)
664 .setMIFlag(MachineInstr::FrameSetup);
665
666 // The EFLAGS implicit def is dead.
667 MI->getOperand(3).setIsDead();
668 }
669
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000670 // If there is an SUB32ri of ESP immediately before this instruction, merge
671 // the two. This can be the case when tail call elimination is enabled and
672 // the callee has more arguments then the caller.
673 NumBytes -= mergeSPUpdates(MBB, MBBI, StackPtr, true);
674
675 // If there is an ADD32ri or SUB32ri of ESP immediately after this
676 // instruction, merge the two instructions.
677 mergeSPUpdatesDown(MBB, MBBI, StackPtr, &NumBytes);
678
679 // Adjust stack pointer: ESP -= numbytes.
680
Philip Reames2c52c662014-08-21 22:53:49 +0000681 static const size_t PageSize = 4096;
682
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000683 // Windows and cygwin/mingw require a prologue helper routine when allocating
684 // more than 4K bytes on the stack. Windows uses __chkstk and cygwin/mingw
685 // uses __alloca. __alloca and the 32-bit version of __chkstk will probe the
686 // stack and adjust the stack pointer in one go. The 64-bit version of
687 // __chkstk is only responsible for probing the stack. The 64-bit prologue is
688 // responsible for adjusting the stack pointer. Touching the stack at 4K
689 // increments is necessary to ensure that the guard pages used by the OS
690 // virtual memory manager are allocated in correct sequence.
Philip Reames2c52c662014-08-21 22:53:49 +0000691 if (NumBytes >= PageSize && UseStackProbe) {
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000692 const char *StackProbeSymbol;
Philip Reames34fcca72014-08-21 22:15:20 +0000693 unsigned CallOp;
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000694
Philip Reames34fcca72014-08-21 22:15:20 +0000695 getStackProbeFunction(STI, CallOp, StackProbeSymbol);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000696
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000697 // Check whether EAX is livein for this function.
698 bool isEAXAlive = isEAXLiveIn(MF);
699
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000700 if (isEAXAlive) {
701 // Sanity check that EAX is not livein for this function.
702 // It should not be, so throw an assert.
703 assert(!Is64Bit && "EAX is livein in x64 case!");
704
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000705 // Save EAX
706 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH32r))
Bill Wendling28b6e122011-07-21 00:44:56 +0000707 .addReg(X86::EAX, RegState::Kill)
708 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000709 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000710
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000711 if (Is64Bit) {
712 // Handle the 64-bit Windows ABI case where we need to call __chkstk.
713 // Function prologue is responsible for adjusting the stack pointer.
714 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::RAX)
Bill Wendling28b6e122011-07-21 00:44:56 +0000715 .addImm(NumBytes)
716 .setMIFlag(MachineInstr::FrameSetup);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000717 } else {
718 // Allocate NumBytes-4 bytes on stack in case of isEAXAlive.
719 // We'll also use 4 already allocated bytes for EAX.
720 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX)
Bill Wendling28b6e122011-07-21 00:44:56 +0000721 .addImm(isEAXAlive ? NumBytes - 4 : NumBytes)
722 .setMIFlag(MachineInstr::FrameSetup);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000723 }
724
725 BuildMI(MBB, MBBI, DL,
Philip Reames34fcca72014-08-21 22:15:20 +0000726 TII.get(CallOp))
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000727 .addExternalSymbol(StackProbeSymbol)
728 .addReg(StackPtr, RegState::Define | RegState::Implicit)
Bill Wendling28b6e122011-07-21 00:44:56 +0000729 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit)
730 .setMIFlag(MachineInstr::FrameSetup);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000731
Kai Nacke87b23ae2013-12-13 05:37:05 +0000732 if (Is64Bit) {
733 // MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp
734 // themself. It also does not clobber %rax so we can reuse it when
735 // adjusting %rsp.
Nico Rieck51969be2013-07-08 11:20:11 +0000736 BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64rr), StackPtr)
737 .addReg(StackPtr)
738 .addReg(X86::RAX)
739 .setMIFlag(MachineInstr::FrameSetup);
740 }
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000741 if (isEAXAlive) {
Philip Reames4e8cb792014-08-21 22:19:16 +0000742 // Restore EAX
743 MachineInstr *MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV32rm),
744 X86::EAX),
745 StackPtr, false, NumBytes - 4);
746 MI->setFlag(MachineInstr::FrameSetup);
747 MBB.insert(MBBI, MI);
NAKAMURA Takumi521eb7c2011-03-24 07:07:00 +0000748 }
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000749 } else if (NumBytes) {
Pavel Chupinf55eb452014-08-07 09:41:19 +0000750 emitSPUpdate(MBB, MBBI, StackPtr, -(int64_t)NumBytes, Is64Bit, Uses64BitFramePtr,
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000751 UseLEA, TII, *RegInfo);
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000752 }
753
754 int SEHFrameOffset = 0;
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000755 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000756 if (HasFP) {
757 // We need to set frame base offset low enough such that all saved
758 // register offsets would be positive relative to it, but we can't
759 // just use NumBytes, because .seh_setframe offset must be <=240.
760 // So we pretend to have only allocated enough space to spill the
761 // non-volatile registers.
762 // We don't care about the rest of stack allocation, because unwinder
763 // will restore SP to (BP - SEHFrameOffset)
764 for (const CalleeSavedInfo &Info : MFI->getCalleeSavedInfo()) {
765 int offset = MFI->getObjectOffset(Info.getFrameIdx());
766 SEHFrameOffset = std::max(SEHFrameOffset, abs(offset));
767 }
768 SEHFrameOffset += SEHFrameOffset % 16; // ensure alignmant
769
770 // This only needs to account for XMM spill slots, GPR slots
Reid Klecknerb5dd9452014-07-01 00:42:47 +0000771 // are covered by the .seh_pushreg's emitted above.
772 unsigned Size = SEHFrameOffset - X86FI->getCalleeSavedFrameSize();
773 if (Size) {
774 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc))
775 .addImm(Size)
776 .setMIFlag(MachineInstr::FrameSetup);
777 }
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000778
779 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame))
780 .addImm(FramePtr)
781 .addImm(SEHFrameOffset)
782 .setMIFlag(MachineInstr::FrameSetup);
783 } else {
784 // SP will be the base register for restoring XMMs
785 if (NumBytes) {
786 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc))
787 .addImm(NumBytes)
788 .setMIFlag(MachineInstr::FrameSetup);
789 }
790 }
791 }
792
793 // Skip the rest of register spilling code
794 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
795 ++MBBI;
796
797 // Emit SEH info for non-GPRs
Saleem Abdulrasool67b54812014-06-29 21:43:47 +0000798 if (NeedsWinEH) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000799 for (const CalleeSavedInfo &Info : MFI->getCalleeSavedInfo()) {
800 unsigned Reg = Info.getReg();
801 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg))
802 continue;
803 assert(X86::FR64RegClass.contains(Reg) && "Unexpected register class");
804
805 int Offset = getFrameIndexOffset(MF, Info.getFrameIdx());
806 Offset += SEHFrameOffset;
807
808 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SaveXMM))
809 .addImm(Reg)
810 .addImm(Offset)
811 .setMIFlag(MachineInstr::FrameSetup);
812 }
813
814 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_EndPrologue))
815 .setMIFlag(MachineInstr::FrameSetup);
816 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000817
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000818 // If we need a base pointer, set it up here. It's whatever the value
819 // of the stack pointer is at this point. Any variable size objects
820 // will be allocated after this, so we can still use the base pointer
821 // to reference locals.
822 if (RegInfo->hasBasePointer(MF)) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000823 // Update the base pointer with the current stack pointer.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000824 unsigned Opc = Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr;
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000825 BuildMI(MBB, MBBI, DL, TII.get(Opc), BasePtr)
826 .addReg(StackPtr)
827 .setMIFlag(MachineInstr::FrameSetup);
Chad Rosierbdb08ac2012-07-10 17:45:53 +0000828 }
829
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000830 if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000831 // Mark end of stack pointer adjustment.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000832 if (!HasFP && NumBytes) {
833 // Define the current CFA rule to use the provided offset.
Rafael Espindola84ee6c42013-05-15 22:27:35 +0000834 assert(StackSize);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000835 unsigned CFIIndex = MMI.addFrameInst(
Craig Topper062a2ba2014-04-25 05:30:21 +0000836 MCCFIInstruction::createDefCfaOffset(nullptr,
837 -StackSize + stackGrowth));
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000838
Eric Christopher612bb692014-04-29 00:16:46 +0000839 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000840 .addCFIIndex(CFIIndex);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000841 }
842
843 // Emit DWARF info specifying the offsets of the callee-saved registers.
844 if (PushedRegs)
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000845 emitCalleeSavedFrameMoves(MBB, MBBI, DL);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000846 }
847}
848
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000849void X86FrameLowering::emitEpilogue(MachineFunction &MF,
Nick Lewycky34a425b2011-06-14 03:23:52 +0000850 MachineBasicBlock &MBB) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000851 const MachineFrameInfo *MFI = MF.getFrameInfo();
852 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000853 const X86RegisterInfo *RegInfo =
854 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
855 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000856 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
857 assert(MBBI != MBB.end() && "Returning block has no instructions");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000858 unsigned RetOpcode = MBBI->getOpcode();
859 DebugLoc DL = MBBI->getDebugLoc();
Eric Christopherf4381642014-06-05 22:00:31 +0000860 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000861 bool Is64Bit = STI.is64Bit();
Pavel Chupinf55eb452014-08-07 09:41:19 +0000862 // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit.
863 const bool Uses64BitFramePtr = STI.isTarget64BitLP64() || STI.isTargetNaCl64();
864 const bool Is64BitILP32 = STI.isTarget64BitILP32();
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000865 bool UseLEA = STI.useLeaForSP();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000866 unsigned StackAlign = getStackAlignment();
867 unsigned SlotSize = RegInfo->getSlotSize();
868 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Pavel Chupin12488922014-08-07 11:09:59 +0000869 unsigned MachineFramePtr = Is64BitILP32 ?
870 getX86SubSuperRegister(FramePtr, MVT::i64, false) : FramePtr;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000871 unsigned StackPtr = RegInfo->getStackRegister();
872
Reid Klecknere7040102014-08-04 21:05:27 +0000873 bool IsWinEH =
874 MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() ==
875 ExceptionHandling::WinEH;
876 bool NeedsWinEH = IsWinEH && MF.getFunction()->needsUnwindTableEntry();
877
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000878 switch (RetOpcode) {
879 default:
880 llvm_unreachable("Can only insert epilog into returning blocks");
David Woodhouse79dd5052014-01-08 12:58:07 +0000881 case X86::RETQ:
882 case X86::RETL:
David Woodhouse4e033b02014-01-13 14:05:59 +0000883 case X86::RETIL:
884 case X86::RETIQ:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000885 case X86::TCRETURNdi:
886 case X86::TCRETURNri:
887 case X86::TCRETURNmi:
888 case X86::TCRETURNdi64:
889 case X86::TCRETURNri64:
890 case X86::TCRETURNmi64:
891 case X86::EH_RETURN:
892 case X86::EH_RETURN64:
893 break; // These are ok
894 }
895
896 // Get the number of bytes to allocate from the FrameInfo.
897 uint64_t StackSize = MFI->getStackSize();
898 uint64_t MaxAlign = MFI->getMaxAlignment();
899 unsigned CSSize = X86FI->getCalleeSavedFrameSize();
900 uint64_t NumBytes = 0;
901
902 // If we're forcing a stack realignment we can't rely on just the frame
903 // info, we need to know the ABI stack alignment as well in case we
904 // have a call out. Otherwise just make sure we have some alignment - we'll
905 // go with the minimum.
906 if (ForceStackAlign) {
907 if (MFI->hasCalls())
908 MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign;
909 else
910 MaxAlign = MaxAlign ? MaxAlign : 4;
911 }
912
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000913 if (hasFP(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000914 // Calculate required stack adjustment.
915 uint64_t FrameSize = StackSize - SlotSize;
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000916 if (RegInfo->needsStackRealignment(MF)) {
917 // Callee-saved registers were pushed on stack before the stack
918 // was realigned.
919 FrameSize -= CSSize;
920 NumBytes = (FrameSize + MaxAlign - 1) / MaxAlign * MaxAlign;
921 } else {
922 NumBytes = FrameSize - CSSize;
923 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000924
925 // Pop EBP.
926 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000927 TII.get(Is64Bit ? X86::POP64r : X86::POP32r), MachineFramePtr);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000928 } else {
929 NumBytes = StackSize - CSSize;
930 }
931
932 // Skip the callee-saved pop instructions.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000933 while (MBBI != MBB.begin()) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000934 MachineBasicBlock::iterator PI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000935 unsigned Opc = PI->getOpcode();
936
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000937 if (Opc != X86::POP32r && Opc != X86::POP64r && Opc != X86::DBG_VALUE &&
Evan Cheng7f8e5632011-12-07 07:15:52 +0000938 !PI->isTerminator())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000939 break;
940
941 --MBBI;
942 }
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000943 MachineBasicBlock::iterator FirstCSPop = MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000944
945 DL = MBBI->getDebugLoc();
946
947 // If there is an ADD32ri or SUB32ri of ESP immediately before this
948 // instruction, merge the two instructions.
949 if (NumBytes || MFI->hasVarSizedObjects())
950 mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
951
952 // If dynamic alloca is used, then reset esp to point to the last callee-saved
953 // slot before popping them off! Same applies for the case, when stack was
954 // realigned.
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000955 if (RegInfo->needsStackRealignment(MF) || MFI->hasVarSizedObjects()) {
956 if (RegInfo->needsStackRealignment(MF))
957 MBBI = FirstCSPop;
958 if (CSSize != 0) {
Pavel Chupinf55eb452014-08-07 09:41:19 +0000959 unsigned Opc = getLEArOpcode(Uses64BitFramePtr);
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000960 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr),
961 FramePtr, false, -CSSize);
Reid Klecknere7040102014-08-04 21:05:27 +0000962 --MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000963 } else {
Pavel Chupinf55eb452014-08-07 09:41:19 +0000964 unsigned Opc = (Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr);
Alexey Samsonovdcc12912012-07-16 06:54:09 +0000965 BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000966 .addReg(FramePtr);
Reid Klecknere7040102014-08-04 21:05:27 +0000967 --MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000968 }
969 } else if (NumBytes) {
970 // Adjust stack pointer back: ESP += numbytes.
Pavel Chupinf55eb452014-08-07 09:41:19 +0000971 emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, Uses64BitFramePtr, UseLEA,
Eli Bendersky44a40ca2013-02-05 21:53:29 +0000972 TII, *RegInfo);
Reid Klecknere7040102014-08-04 21:05:27 +0000973 --MBBI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000974 }
975
Reid Klecknere7040102014-08-04 21:05:27 +0000976 // Windows unwinder will not invoke function's exception handler if IP is
977 // either in prologue or in epilogue. This behavior causes a problem when a
978 // call immediately precedes an epilogue, because the return address points
979 // into the epilogue. To cope with that, we insert an epilogue marker here,
980 // then replace it with a 'nop' if it ends up immediately after a CALL in the
981 // final emitted code.
982 if (NeedsWinEH)
983 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue));
984
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000985 // We're returning from function via eh_return.
986 if (RetOpcode == X86::EH_RETURN || RetOpcode == X86::EH_RETURN64) {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000987 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000988 MachineOperand &DestAddr = MBBI->getOperand(0);
989 assert(DestAddr.isReg() && "Offset should be in register!");
990 BuildMI(MBB, MBBI, DL,
Pavel Chupinf55eb452014-08-07 09:41:19 +0000991 TII.get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr),
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000992 StackPtr).addReg(DestAddr.getReg());
993 } else if (RetOpcode == X86::TCRETURNri || RetOpcode == X86::TCRETURNdi ||
994 RetOpcode == X86::TCRETURNmi ||
995 RetOpcode == X86::TCRETURNri64 || RetOpcode == X86::TCRETURNdi64 ||
996 RetOpcode == X86::TCRETURNmi64) {
997 bool isMem = RetOpcode == X86::TCRETURNmi || RetOpcode == X86::TCRETURNmi64;
998 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesenbbb1a542011-01-13 22:47:43 +0000999 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001000 MachineOperand &JumpTarget = MBBI->getOperand(0);
1001 MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1);
1002 assert(StackAdjust.isImm() && "Expecting immediate value.");
1003
1004 // Adjust stack pointer.
1005 int StackAdj = StackAdjust.getImm();
1006 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
1007 int Offset = 0;
1008 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
1009
1010 // Incoporate the retaddr area.
1011 Offset = StackAdj-MaxTCDelta;
1012 assert(Offset >= 0 && "Offset should never be negative");
1013
1014 if (Offset) {
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001015 // Check for possible merge with preceding ADD instruction.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001016 Offset += mergeSPUpdates(MBB, MBBI, StackPtr, true);
Pavel Chupinf55eb452014-08-07 09:41:19 +00001017 emitSPUpdate(MBB, MBBI, StackPtr, Offset, Is64Bit, Uses64BitFramePtr,
Eli Bendersky44a40ca2013-02-05 21:53:29 +00001018 UseLEA, TII, *RegInfo);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001019 }
1020
1021 // Jump to label or value in register.
1022 if (RetOpcode == X86::TCRETURNdi || RetOpcode == X86::TCRETURNdi64) {
Evan Chengd4b08732010-11-30 23:55:39 +00001023 MachineInstrBuilder MIB =
1024 BuildMI(MBB, MBBI, DL, TII.get((RetOpcode == X86::TCRETURNdi)
1025 ? X86::TAILJMPd : X86::TAILJMPd64));
1026 if (JumpTarget.isGlobal())
1027 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
1028 JumpTarget.getTargetFlags());
1029 else {
1030 assert(JumpTarget.isSymbol());
1031 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
1032 JumpTarget.getTargetFlags());
1033 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001034 } else if (RetOpcode == X86::TCRETURNmi || RetOpcode == X86::TCRETURNmi64) {
1035 MachineInstrBuilder MIB =
1036 BuildMI(MBB, MBBI, DL, TII.get((RetOpcode == X86::TCRETURNmi)
1037 ? X86::TAILJMPm : X86::TAILJMPm64));
1038 for (unsigned i = 0; i != 5; ++i)
1039 MIB.addOperand(MBBI->getOperand(i));
1040 } else if (RetOpcode == X86::TCRETURNri64) {
1041 BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr64)).
1042 addReg(JumpTarget.getReg(), RegState::Kill);
1043 } else {
1044 BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr)).
1045 addReg(JumpTarget.getReg(), RegState::Kill);
1046 }
1047
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001048 MachineInstr *NewMI = std::prev(MBBI);
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001049 NewMI->copyImplicitOps(MF, MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001050
1051 // Delete the pseudo instruction TCRETURN.
1052 MBB.erase(MBBI);
David Woodhouse4e033b02014-01-13 14:05:59 +00001053 } else if ((RetOpcode == X86::RETQ || RetOpcode == X86::RETL ||
1054 RetOpcode == X86::RETIQ || RetOpcode == X86::RETIL) &&
1055 (X86FI->getTCReturnAddrDelta() < 0)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001056 // Add the return addr area delta back since we are not tail calling.
1057 int delta = -1*X86FI->getTCReturnAddrDelta();
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +00001058 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001059
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001060 // Check for possible merge with preceding ADD instruction.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001061 delta += mergeSPUpdates(MBB, MBBI, StackPtr, true);
Pavel Chupinf55eb452014-08-07 09:41:19 +00001062 emitSPUpdate(MBB, MBBI, StackPtr, delta, Is64Bit, Uses64BitFramePtr, UseLEA, TII,
Eli Bendersky44a40ca2013-02-05 21:53:29 +00001063 *RegInfo);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00001064 }
1065}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +00001066
Eric Christopher4237bf12014-04-29 00:16:33 +00001067int X86FrameLowering::getFrameIndexOffset(const MachineFunction &MF,
1068 int FI) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001069 const X86RegisterInfo *RegInfo =
1070 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +00001071 const MachineFrameInfo *MFI = MF.getFrameInfo();
1072 int Offset = MFI->getObjectOffset(FI) - getOffsetOfLocalArea();
1073 uint64_t StackSize = MFI->getStackSize();
1074
Chad Rosierbdb08ac2012-07-10 17:45:53 +00001075 if (RegInfo->hasBasePointer(MF)) {
1076 assert (hasFP(MF) && "VLAs and dynamic stack realign, but no FP?!");
1077 if (FI < 0) {
1078 // Skip the saved EBP.
1079 return Offset + RegInfo->getSlotSize();
1080 } else {
1081 assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0);
1082 return Offset + StackSize;
1083 }
1084 } else if (RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikov46877782010-11-20 15:59:32 +00001085 if (FI < 0) {
1086 // Skip the saved EBP.
Chad Rosier20b79dc2012-05-23 23:45:10 +00001087 return Offset + RegInfo->getSlotSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +00001088 } else {
Duncan Sandsd278d352011-10-18 12:44:00 +00001089 assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0);
Anton Korobeynikov46877782010-11-20 15:59:32 +00001090 return Offset + StackSize;
1091 }
1092 // FIXME: Support tail calls
1093 } else {
1094 if (!hasFP(MF))
1095 return Offset + StackSize;
1096
1097 // Skip the saved EBP.
Chad Rosier20b79dc2012-05-23 23:45:10 +00001098 Offset += RegInfo->getSlotSize();
Anton Korobeynikov46877782010-11-20 15:59:32 +00001099
1100 // Skip the RETADDR move area
1101 const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1102 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
1103 if (TailCallReturnAddrDelta < 0)
1104 Offset -= TailCallReturnAddrDelta;
1105 }
1106
1107 return Offset;
1108}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001109
Alexey Samsonovc4b3ad82012-05-01 15:16:06 +00001110int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
1111 unsigned &FrameReg) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001112 const X86RegisterInfo *RegInfo =
1113 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Alexey Samsonovc4b3ad82012-05-01 15:16:06 +00001114 // We can't calculate offset from frame pointer if the stack is realigned,
Chad Rosierbdb08ac2012-07-10 17:45:53 +00001115 // so enforce usage of stack/base pointer. The base pointer is used when we
1116 // have dynamic allocas in addition to dynamic realignment.
1117 if (RegInfo->hasBasePointer(MF))
1118 FrameReg = RegInfo->getBaseRegister();
1119 else if (RegInfo->needsStackRealignment(MF))
1120 FrameReg = RegInfo->getStackRegister();
1121 else
1122 FrameReg = RegInfo->getFrameRegister(MF);
Alexey Samsonovc4b3ad82012-05-01 15:16:06 +00001123 return getFrameIndexOffset(MF, FI);
1124}
1125
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001126bool X86FrameLowering::assignCalleeSavedSpillSlots(
1127 MachineFunction &MF, const TargetRegisterInfo *TRI,
1128 std::vector<CalleeSavedInfo> &CSI) const {
1129 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00001130 const X86RegisterInfo *RegInfo =
1131 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001132 unsigned SlotSize = RegInfo->getSlotSize();
1133 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001134
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001135 unsigned CalleeSavedFrameSize = 0;
1136 int SpillSlotOffset = getOffsetOfLocalArea() + X86FI->getTCReturnAddrDelta();
1137
1138 if (hasFP(MF)) {
1139 // emitPrologue always spills frame register the first thing.
1140 SpillSlotOffset -= SlotSize;
1141 MFI->CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
1142
1143 // Since emitPrologue and emitEpilogue will handle spilling and restoring of
1144 // the frame register, we can delete it from CSI list and not have to worry
1145 // about avoiding it later.
1146 unsigned FPReg = RegInfo->getFrameRegister(MF);
1147 for (unsigned i = 0; i < CSI.size(); ++i) {
Pavel Chupinf55eb452014-08-07 09:41:19 +00001148 if (TRI->regsOverlap(CSI[i].getReg(),FPReg)) {
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001149 CSI.erase(CSI.begin() + i);
1150 break;
1151 }
1152 }
1153 }
1154
1155 // Assign slots for GPRs. It increases frame size.
1156 for (unsigned i = CSI.size(); i != 0; --i) {
1157 unsigned Reg = CSI[i - 1].getReg();
1158
1159 if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg))
1160 continue;
1161
1162 SpillSlotOffset -= SlotSize;
1163 CalleeSavedFrameSize += SlotSize;
1164
1165 int SlotIndex = MFI->CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
1166 CSI[i - 1].setFrameIdx(SlotIndex);
1167 }
1168
1169 X86FI->setCalleeSavedFrameSize(CalleeSavedFrameSize);
1170
1171 // Assign slots for XMMs.
1172 for (unsigned i = CSI.size(); i != 0; --i) {
1173 unsigned Reg = CSI[i - 1].getReg();
1174 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg))
1175 continue;
1176
1177 const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg);
1178 // ensure alignment
1179 SpillSlotOffset -= abs(SpillSlotOffset) % RC->getAlignment();
1180 // spill into slot
1181 SpillSlotOffset -= RC->getSize();
1182 int SlotIndex =
1183 MFI->CreateFixedSpillStackObject(RC->getSize(), SpillSlotOffset);
1184 CSI[i - 1].setFrameIdx(SlotIndex);
1185 MFI->ensureMaxAlignment(RC->getAlignment());
1186 }
1187
1188 return true;
1189}
1190
1191bool X86FrameLowering::spillCalleeSavedRegisters(
1192 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
1193 const std::vector<CalleeSavedInfo> &CSI,
1194 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001195 DebugLoc DL = MBB.findDebugLoc(MI);
1196
1197 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001198 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Eric Christopherf4381642014-06-05 22:00:31 +00001199 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001200
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001201 // Push GPRs. It increases frame size.
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001202 unsigned Opc = STI.is64Bit() ? X86::PUSH64r : X86::PUSH32r;
1203 for (unsigned i = CSI.size(); i != 0; --i) {
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001204 unsigned Reg = CSI[i - 1].getReg();
1205
1206 if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg))
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001207 continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001208 // Add the callee-saved register as live-in. It's killed at the spill.
1209 MBB.addLiveIn(Reg);
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001210
Charles Davis7ed40cb2011-06-12 01:45:54 +00001211 BuildMI(MBB, MI, DL, TII.get(Opc)).addReg(Reg, RegState::Kill)
1212 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001213 }
1214
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001215 // Make XMM regs spilled. X86 does not have ability of push/pop XMM.
1216 // It can be done by spilling XMMs to stack frame.
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001217 for (unsigned i = CSI.size(); i != 0; --i) {
1218 unsigned Reg = CSI[i-1].getReg();
1219 if (X86::GR64RegClass.contains(Reg) ||
1220 X86::GR32RegClass.contains(Reg))
1221 continue;
1222 // Add the callee-saved register as live-in. It's killed at the spill.
1223 MBB.addLiveIn(Reg);
1224 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001225
1226 TII.storeRegToStackSlot(MBB, MI, Reg, true, CSI[i - 1].getFrameIdx(), RC,
1227 TRI);
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001228 --MI;
1229 MI->setFlag(MachineInstr::FrameSetup);
1230 ++MI;
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001231 }
1232
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001233 return true;
1234}
1235
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001236bool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001237 MachineBasicBlock::iterator MI,
1238 const std::vector<CalleeSavedInfo> &CSI,
1239 const TargetRegisterInfo *TRI) const {
1240 if (CSI.empty())
1241 return false;
1242
1243 DebugLoc DL = MBB.findDebugLoc(MI);
1244
1245 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00001246 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Eric Christopherf4381642014-06-05 22:00:31 +00001247 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001248
1249 // Reload XMMs from stack frame.
1250 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1251 unsigned Reg = CSI[i].getReg();
1252 if (X86::GR64RegClass.contains(Reg) ||
1253 X86::GR32RegClass.contains(Reg))
1254 continue;
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001255
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001256 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
NAKAMURA Takumic403be12014-06-25 12:40:56 +00001257 TII.loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RC, TRI);
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001258 }
1259
1260 // POP GPRs.
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001261 unsigned Opc = STI.is64Bit() ? X86::POP64r : X86::POP32r;
1262 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1263 unsigned Reg = CSI[i].getReg();
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001264 if (!X86::GR64RegClass.contains(Reg) &&
1265 !X86::GR32RegClass.contains(Reg))
1266 continue;
NAKAMURA Takumi1db59952014-06-25 12:41:52 +00001267
NAKAMURA Takumid4e50032011-02-27 08:47:19 +00001268 BuildMI(MBB, MI, DL, TII.get(Opc), Reg);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001269 }
1270 return true;
1271}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001272
1273void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001274X86FrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Eric Christopher11b05cc2014-06-05 00:09:05 +00001275 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001276 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00001277 const X86RegisterInfo *RegInfo =
1278 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001279 unsigned SlotSize = RegInfo->getSlotSize();
1280
1281 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Tim Northoverecc018c2013-08-04 09:35:57 +00001282 int64_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001283
1284 if (TailCallReturnAddrDelta < 0) {
1285 // create RETURNADDR area
1286 // arg
1287 // arg
1288 // RETADDR
1289 // { ...
1290 // RETADDR area
1291 // ...
1292 // }
1293 // [EBP]
1294 MFI->CreateFixedObject(-TailCallReturnAddrDelta,
Tim Northoverecc018c2013-08-04 09:35:57 +00001295 TailCallReturnAddrDelta - SlotSize, true);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001296 }
1297
Chad Rosierbdb08ac2012-07-10 17:45:53 +00001298 // Spill the BasePtr if it's used.
1299 if (RegInfo->hasBasePointer(MF))
1300 MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001301}
Rafael Espindolac2174212011-08-30 19:39:58 +00001302
1303static bool
1304HasNestArgument(const MachineFunction *MF) {
1305 const Function *F = MF->getFunction();
1306 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1307 I != E; I++) {
1308 if (I->hasNestAttr())
1309 return true;
1310 }
1311 return false;
1312}
1313
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001314/// GetScratchRegister - Get a temp register for performing work in the
1315/// segmented stack and the Erlang/HiPE stack prologue. Depending on platform
1316/// and the properties of the function either one or two registers will be
1317/// needed. Set primary to true for the first register, false for the second.
Rafael Espindolac2174212011-08-30 19:39:58 +00001318static unsigned
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001319GetScratchRegister(bool Is64Bit, bool IsLP64, const MachineFunction &MF, bool Primary) {
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001320 CallingConv::ID CallingConvention = MF.getFunction()->getCallingConv();
1321
1322 // Erlang stuff.
1323 if (CallingConvention == CallingConv::HiPE) {
1324 if (Is64Bit)
1325 return Primary ? X86::R14 : X86::R13;
1326 else
1327 return Primary ? X86::EBX : X86::EDI;
1328 }
1329
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001330 if (Is64Bit) {
1331 if (IsLP64)
1332 return Primary ? X86::R11 : X86::R12;
1333 else
1334 return Primary ? X86::R11D : X86::R12D;
1335 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001336
David Blaikie46a9f012012-01-20 21:51:11 +00001337 bool IsNested = HasNestArgument(&MF);
1338
1339 if (CallingConvention == CallingConv::X86_FastCall ||
1340 CallingConvention == CallingConv::Fast) {
1341 if (IsNested)
1342 report_fatal_error("Segmented stacks does not support fastcall with "
1343 "nested function.");
1344 return Primary ? X86::EAX : X86::ECX;
Rafael Espindolac2174212011-08-30 19:39:58 +00001345 }
David Blaikie46a9f012012-01-20 21:51:11 +00001346 if (IsNested)
1347 return Primary ? X86::EDX : X86::EAX;
1348 return Primary ? X86::ECX : X86::EAX;
Rafael Espindolac2174212011-08-30 19:39:58 +00001349}
1350
Sanjoy Das006e43b2011-12-03 09:32:07 +00001351// The stack limit in the TCB is set to this many bytes above the actual stack
1352// limit.
1353static const uint64_t kSplitStackAvailable = 256;
1354
Rafael Espindolac2174212011-08-30 19:39:58 +00001355void
1356X86FrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const {
1357 MachineBasicBlock &prologueMBB = MF.front();
1358 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00001359 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Rafael Espindolac2174212011-08-30 19:39:58 +00001360 uint64_t StackSize;
Eric Christopherf4381642014-06-05 22:00:31 +00001361 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Rafael Espindolac2174212011-08-30 19:39:58 +00001362 bool Is64Bit = STI.is64Bit();
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001363 const bool IsLP64 = STI.isTarget64BitLP64();
Rafael Espindolac2174212011-08-30 19:39:58 +00001364 unsigned TlsReg, TlsOffset;
1365 DebugLoc DL;
Rafael Espindolac2174212011-08-30 19:39:58 +00001366
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001367 unsigned ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true);
Rafael Espindolac2174212011-08-30 19:39:58 +00001368 assert(!MF.getRegInfo().isLiveIn(ScratchReg) &&
1369 "Scratch register is live-in");
1370
1371 if (MF.getFunction()->isVarArg())
1372 report_fatal_error("Segmented stacks do not support vararg functions.");
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001373 if (!STI.isTargetLinux() && !STI.isTargetDarwin() &&
Reid Kleckner10110272014-04-01 18:34:21 +00001374 !STI.isTargetWin32() && !STI.isTargetWin64() && !STI.isTargetFreeBSD())
Rafael Espindola00e861e2012-01-12 20:24:30 +00001375 report_fatal_error("Segmented stacks not supported on this platform.");
Rafael Espindolac2174212011-08-30 19:39:58 +00001376
Tim Northoverf9e798b2014-05-22 13:03:43 +00001377 // Eventually StackSize will be calculated by a link-time pass; which will
1378 // also decide whether checking code needs to be injected into this particular
1379 // prologue.
1380 StackSize = MFI->getStackSize();
1381
1382 // Do not generate a prologue for functions with a stack of size zero
1383 if (StackSize == 0)
1384 return;
1385
Rafael Espindolac2174212011-08-30 19:39:58 +00001386 MachineBasicBlock *allocMBB = MF.CreateMachineBasicBlock();
1387 MachineBasicBlock *checkMBB = MF.CreateMachineBasicBlock();
1388 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1389 bool IsNested = false;
1390
1391 // We need to know if the function has a nest argument only in 64 bit mode.
1392 if (Is64Bit)
1393 IsNested = HasNestArgument(&MF);
1394
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001395 // The MOV R10, RAX needs to be in a different block, since the RET we emit in
1396 // allocMBB needs to be last (terminating) instruction.
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001397
Rafael Espindolac2174212011-08-30 19:39:58 +00001398 for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(),
1399 e = prologueMBB.livein_end(); i != e; i++) {
1400 allocMBB->addLiveIn(*i);
1401 checkMBB->addLiveIn(*i);
1402 }
1403
1404 if (IsNested)
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001405 allocMBB->addLiveIn(IsLP64 ? X86::R10 : X86::R10D);
Rafael Espindola66393c12011-10-26 21:12:27 +00001406
Rafael Espindolac2174212011-08-30 19:39:58 +00001407 MF.push_front(allocMBB);
1408 MF.push_front(checkMBB);
1409
Rafael Espindolad90466b2012-01-11 19:00:37 +00001410 // When the frame size is less than 256 we just compare the stack
1411 // boundary directly to the value of the stack pointer, per gcc.
1412 bool CompareStackPointer = StackSize < kSplitStackAvailable;
1413
Rafael Espindolac2174212011-08-30 19:39:58 +00001414 // Read the limit off the current stacklet off the stack_guard location.
1415 if (Is64Bit) {
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001416 if (STI.isTargetLinux()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001417 TlsReg = X86::FS;
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001418 TlsOffset = IsLP64 ? 0x70 : 0x40;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001419 } else if (STI.isTargetDarwin()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001420 TlsReg = X86::GS;
1421 TlsOffset = 0x60 + 90*8; // See pthread_machdep.h. Steal TLS slot 90.
Reid Kleckner10110272014-04-01 18:34:21 +00001422 } else if (STI.isTargetWin64()) {
1423 TlsReg = X86::GS;
1424 TlsOffset = 0x28; // pvArbitrary, reserved for application use
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001425 } else if (STI.isTargetFreeBSD()) {
Rafael Espindola00e861e2012-01-12 20:24:30 +00001426 TlsReg = X86::FS;
1427 TlsOffset = 0x18;
Rafael Espindola10745d32012-01-12 20:22:08 +00001428 } else {
1429 report_fatal_error("Segmented stacks not supported on this platform.");
Rafael Espindolad90466b2012-01-11 19:00:37 +00001430 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001431
Rafael Espindolad90466b2012-01-11 19:00:37 +00001432 if (CompareStackPointer)
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001433 ScratchReg = IsLP64 ? X86::RSP : X86::ESP;
Sanjoy Das006e43b2011-12-03 09:32:07 +00001434 else
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001435 BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::LEA64r : X86::LEA64_32r), ScratchReg).addReg(X86::RSP)
Rafael Espindola6635ae12012-01-11 18:14:03 +00001436 .addImm(1).addReg(0).addImm(-StackSize).addReg(0);
Sanjoy Das006e43b2011-12-03 09:32:07 +00001437
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001438 BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::CMP64rm : X86::CMP32rm)).addReg(ScratchReg)
Rafael Espindola6635ae12012-01-11 18:14:03 +00001439 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg);
Rafael Espindolac2174212011-08-30 19:39:58 +00001440 } else {
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001441 if (STI.isTargetLinux()) {
Rafael Espindola10745d32012-01-12 20:22:08 +00001442 TlsReg = X86::GS;
1443 TlsOffset = 0x30;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001444 } else if (STI.isTargetDarwin()) {
Rafael Espindola10745d32012-01-12 20:22:08 +00001445 TlsReg = X86::GS;
1446 TlsOffset = 0x48 + 90*4;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001447 } else if (STI.isTargetWin32()) {
Rafael Espindola10745d32012-01-12 20:22:08 +00001448 TlsReg = X86::FS;
1449 TlsOffset = 0x14; // pvArbitrary, reserved for application use
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001450 } else if (STI.isTargetFreeBSD()) {
Rafael Espindola00e861e2012-01-12 20:24:30 +00001451 report_fatal_error("Segmented stacks not supported on FreeBSD i386.");
Rafael Espindola10745d32012-01-12 20:22:08 +00001452 } else {
1453 report_fatal_error("Segmented stacks not supported on this platform.");
1454 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001455
Rafael Espindolad90466b2012-01-11 19:00:37 +00001456 if (CompareStackPointer)
Sanjoy Das006e43b2011-12-03 09:32:07 +00001457 ScratchReg = X86::ESP;
1458 else
1459 BuildMI(checkMBB, DL, TII.get(X86::LEA32r), ScratchReg).addReg(X86::ESP)
Rafael Espindola6635ae12012-01-11 18:14:03 +00001460 .addImm(1).addReg(0).addImm(-StackSize).addReg(0);
Sanjoy Das006e43b2011-12-03 09:32:07 +00001461
Reid Kleckner10110272014-04-01 18:34:21 +00001462 if (STI.isTargetLinux() || STI.isTargetWin32() || STI.isTargetWin64()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001463 BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)).addReg(ScratchReg)
1464 .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg);
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001465 } else if (STI.isTargetDarwin()) {
Rafael Espindolad90466b2012-01-11 19:00:37 +00001466
Eric Christopher4237bf12014-04-29 00:16:33 +00001467 // TlsOffset doesn't fit into a mod r/m byte so we need an extra register.
Rafael Espindolad90466b2012-01-11 19:00:37 +00001468 unsigned ScratchReg2;
1469 bool SaveScratch2;
1470 if (CompareStackPointer) {
Eric Christopher4237bf12014-04-29 00:16:33 +00001471 // The primary scratch register is available for holding the TLS offset.
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001472 ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, true);
Rafael Espindolad90466b2012-01-11 19:00:37 +00001473 SaveScratch2 = false;
1474 } else {
1475 // Need to use a second register to hold the TLS offset
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001476 ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, false);
Rafael Espindolad90466b2012-01-11 19:00:37 +00001477
Eric Christopher4237bf12014-04-29 00:16:33 +00001478 // Unfortunately, with fastcc the second scratch register may hold an
1479 // argument.
Rafael Espindolad90466b2012-01-11 19:00:37 +00001480 SaveScratch2 = MF.getRegInfo().isLiveIn(ScratchReg2);
1481 }
1482
Eric Christopher4237bf12014-04-29 00:16:33 +00001483 // If Scratch2 is live-in then it needs to be saved.
Rafael Espindolad90466b2012-01-11 19:00:37 +00001484 assert((!MF.getRegInfo().isLiveIn(ScratchReg2) || SaveScratch2) &&
1485 "Scratch register is live-in and not saved");
1486
1487 if (SaveScratch2)
1488 BuildMI(checkMBB, DL, TII.get(X86::PUSH32r))
1489 .addReg(ScratchReg2, RegState::Kill);
1490
1491 BuildMI(checkMBB, DL, TII.get(X86::MOV32ri), ScratchReg2)
1492 .addImm(TlsOffset);
1493 BuildMI(checkMBB, DL, TII.get(X86::CMP32rm))
1494 .addReg(ScratchReg)
1495 .addReg(ScratchReg2).addImm(1).addReg(0)
1496 .addImm(0)
1497 .addReg(TlsReg);
1498
1499 if (SaveScratch2)
1500 BuildMI(checkMBB, DL, TII.get(X86::POP32r), ScratchReg2);
1501 }
Rafael Espindolac2174212011-08-30 19:39:58 +00001502 }
1503
1504 // This jump is taken if SP >= (Stacklet Limit + Stack Space required).
1505 // It jumps to normal execution of the function body.
Rafael Espindola2b894482012-01-11 18:23:35 +00001506 BuildMI(checkMBB, DL, TII.get(X86::JA_4)).addMBB(&prologueMBB);
Rafael Espindolac2174212011-08-30 19:39:58 +00001507
1508 // On 32 bit we first push the arguments size and then the frame size. On 64
1509 // bit, we pass the stack frame size in r10 and the argument size in r11.
1510 if (Is64Bit) {
1511 // Functions with nested arguments use R10, so it needs to be saved across
1512 // the call to _morestack
1513
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001514 const unsigned RegAX = IsLP64 ? X86::RAX : X86::EAX;
1515 const unsigned Reg10 = IsLP64 ? X86::R10 : X86::R10D;
1516 const unsigned Reg11 = IsLP64 ? X86::R11 : X86::R11D;
1517 const unsigned MOVrr = IsLP64 ? X86::MOV64rr : X86::MOV32rr;
1518 const unsigned MOVri = IsLP64 ? X86::MOV64ri : X86::MOV32ri;
Rafael Espindolac2174212011-08-30 19:39:58 +00001519
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001520 if (IsNested)
1521 BuildMI(allocMBB, DL, TII.get(MOVrr), RegAX).addReg(Reg10);
1522
1523 BuildMI(allocMBB, DL, TII.get(MOVri), Reg10)
Rafael Espindolac2174212011-08-30 19:39:58 +00001524 .addImm(StackSize);
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001525 BuildMI(allocMBB, DL, TII.get(MOVri), Reg11)
Rafael Espindolac2174212011-08-30 19:39:58 +00001526 .addImm(X86FI->getArgumentStackSize());
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001527 MF.getRegInfo().setPhysRegUsed(Reg10);
1528 MF.getRegInfo().setPhysRegUsed(Reg11);
Rafael Espindolac2174212011-08-30 19:39:58 +00001529 } else {
Rafael Espindolac2174212011-08-30 19:39:58 +00001530 BuildMI(allocMBB, DL, TII.get(X86::PUSHi32))
1531 .addImm(X86FI->getArgumentStackSize());
1532 BuildMI(allocMBB, DL, TII.get(X86::PUSHi32))
1533 .addImm(StackSize);
1534 }
1535
1536 // __morestack is in libgcc
1537 if (Is64Bit)
1538 BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32))
1539 .addExternalSymbol("__morestack");
1540 else
1541 BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32))
1542 .addExternalSymbol("__morestack");
1543
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001544 if (IsNested)
Rafael Espindola66393c12011-10-26 21:12:27 +00001545 BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET_RESTORE_R10));
1546 else
1547 BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET));
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001548
Rafael Espindola66393c12011-10-26 21:12:27 +00001549 allocMBB->addSuccessor(&prologueMBB);
Bill Wendling25f6d3e2011-10-13 08:24:19 +00001550
Rafael Espindolac2174212011-08-30 19:39:58 +00001551 checkMBB->addSuccessor(allocMBB);
1552 checkMBB->addSuccessor(&prologueMBB);
1553
Jakob Stoklund Olesen55cf2ed2011-09-24 01:11:19 +00001554#ifdef XDEBUG
Rafael Espindolac2174212011-08-30 19:39:58 +00001555 MF.verify();
1556#endif
1557}
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001558
Yiannis Tsiourisd4842e52013-02-28 16:59:10 +00001559/// Erlang programs may need a special prologue to handle the stack size they
1560/// might need at runtime. That is because Erlang/OTP does not implement a C
1561/// stack but uses a custom implementation of hybrid stack/heap architecture.
1562/// (for more information see Eric Stenman's Ph.D. thesis:
1563/// http://publications.uu.se/uu/fulltext/nbn_se_uu_diva-2688.pdf)
1564///
1565/// CheckStack:
Eric Christopher4237bf12014-04-29 00:16:33 +00001566/// temp0 = sp - MaxStack
1567/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart
Yiannis Tsiourisd4842e52013-02-28 16:59:10 +00001568/// OldStart:
Eric Christopher4237bf12014-04-29 00:16:33 +00001569/// ...
Yiannis Tsiourisd4842e52013-02-28 16:59:10 +00001570/// IncStack:
Eric Christopher4237bf12014-04-29 00:16:33 +00001571/// call inc_stack # doubles the stack space
1572/// temp0 = sp - MaxStack
1573/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001574void X86FrameLowering::adjustForHiPEPrologue(MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001575 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001576 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher11b05cc2014-06-05 00:09:05 +00001577 const unsigned SlotSize =
Eric Christopherfc6de422014-08-05 02:39:49 +00001578 static_cast<const X86RegisterInfo *>(MF.getSubtarget().getRegisterInfo())
1579 ->getSlotSize();
Eric Christopherf4381642014-06-05 22:00:31 +00001580 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001581 const bool Is64Bit = STI.is64Bit();
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001582 const bool IsLP64 = STI.isTarget64BitLP64();
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001583 DebugLoc DL;
1584 // HiPE-specific values
1585 const unsigned HipeLeafWords = 24;
1586 const unsigned CCRegisteredArgs = Is64Bit ? 6 : 5;
1587 const unsigned Guaranteed = HipeLeafWords * SlotSize;
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001588 unsigned CallerStkArity = MF.getFunction()->arg_size() > CCRegisteredArgs ?
1589 MF.getFunction()->arg_size() - CCRegisteredArgs : 0;
1590 unsigned MaxStack = MFI->getStackSize() + CallerStkArity*SlotSize + SlotSize;
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001591
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001592 assert(STI.isTargetLinux() &&
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001593 "HiPE prologue is only supported on Linux operating systems.");
1594
1595 // Compute the largest caller's frame that is needed to fit the callees'
1596 // frames. This 'MaxStack' is computed from:
1597 //
1598 // a) the fixed frame size, which is the space needed for all spilled temps,
1599 // b) outgoing on-stack parameter areas, and
1600 // c) the minimum stack space this function needs to make available for the
1601 // functions it calls (a tunable ABI property).
1602 if (MFI->hasCalls()) {
1603 unsigned MoreStackForCalls = 0;
1604
1605 for (MachineFunction::iterator MBBI = MF.begin(), MBBE = MF.end();
1606 MBBI != MBBE; ++MBBI)
1607 for (MachineBasicBlock::iterator MI = MBBI->begin(), ME = MBBI->end();
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001608 MI != ME; ++MI) {
1609 if (!MI->isCall())
1610 continue;
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001611
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001612 // Get callee operand.
1613 const MachineOperand &MO = MI->getOperand(0);
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001614
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001615 // Only take account of global function calls (no closures etc.).
1616 if (!MO.isGlobal())
1617 continue;
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001618
Benjamin Kramer1cb826b2013-02-19 17:32:57 +00001619 const Function *F = dyn_cast<Function>(MO.getGlobal());
1620 if (!F)
1621 continue;
1622
1623 // Do not update 'MaxStack' for primitive and built-in functions
1624 // (encoded with names either starting with "erlang."/"bif_" or not
1625 // having a ".", such as a simple <Module>.<Function>.<Arity>, or an
1626 // "_", such as the BIF "suspend_0") as they are executed on another
1627 // stack.
1628 if (F->getName().find("erlang.") != StringRef::npos ||
1629 F->getName().find("bif_") != StringRef::npos ||
1630 F->getName().find_first_of("._") == StringRef::npos)
1631 continue;
1632
1633 unsigned CalleeStkArity =
1634 F->arg_size() > CCRegisteredArgs ? F->arg_size()-CCRegisteredArgs : 0;
1635 if (HipeLeafWords - 1 > CalleeStkArity)
1636 MoreStackForCalls = std::max(MoreStackForCalls,
1637 (HipeLeafWords - 1 - CalleeStkArity) * SlotSize);
1638 }
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001639 MaxStack += MoreStackForCalls;
1640 }
1641
1642 // If the stack frame needed is larger than the guaranteed then runtime checks
1643 // and calls to "inc_stack_0" BIF should be inserted in the assembly prologue.
1644 if (MaxStack > Guaranteed) {
1645 MachineBasicBlock &prologueMBB = MF.front();
1646 MachineBasicBlock *stackCheckMBB = MF.CreateMachineBasicBlock();
1647 MachineBasicBlock *incStackMBB = MF.CreateMachineBasicBlock();
1648
1649 for (MachineBasicBlock::livein_iterator I = prologueMBB.livein_begin(),
1650 E = prologueMBB.livein_end(); I != E; I++) {
1651 stackCheckMBB->addLiveIn(*I);
1652 incStackMBB->addLiveIn(*I);
1653 }
1654
1655 MF.push_front(incStackMBB);
1656 MF.push_front(stackCheckMBB);
1657
1658 unsigned ScratchReg, SPReg, PReg, SPLimitOffset;
1659 unsigned LEAop, CMPop, CALLop;
1660 if (Is64Bit) {
1661 SPReg = X86::RSP;
1662 PReg = X86::RBP;
1663 LEAop = X86::LEA64r;
1664 CMPop = X86::CMP64rm;
1665 CALLop = X86::CALL64pcrel32;
1666 SPLimitOffset = 0x90;
1667 } else {
1668 SPReg = X86::ESP;
1669 PReg = X86::EBP;
1670 LEAop = X86::LEA32r;
1671 CMPop = X86::CMP32rm;
1672 CALLop = X86::CALLpcrel32;
1673 SPLimitOffset = 0x4c;
1674 }
1675
Pavel Chupinbe9f1212014-09-22 13:11:35 +00001676 ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true);
Benjamin Kramer53bc37c2013-02-18 20:55:12 +00001677 assert(!MF.getRegInfo().isLiveIn(ScratchReg) &&
1678 "HiPE prologue scratch register is live-in");
1679
1680 // Create new MBB for StackCheck:
1681 addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(LEAop), ScratchReg),
1682 SPReg, false, -MaxStack);
1683 // SPLimitOffset is in a fixed heap location (pointed by BP).
1684 addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(CMPop))
1685 .addReg(ScratchReg), PReg, false, SPLimitOffset);
1686 BuildMI(stackCheckMBB, DL, TII.get(X86::JAE_4)).addMBB(&prologueMBB);
1687
1688 // Create new MBB for IncStack:
1689 BuildMI(incStackMBB, DL, TII.get(CALLop)).
1690 addExternalSymbol("inc_stack_0");
1691 addRegOffset(BuildMI(incStackMBB, DL, TII.get(LEAop), ScratchReg),
1692 SPReg, false, -MaxStack);
1693 addRegOffset(BuildMI(incStackMBB, DL, TII.get(CMPop))
1694 .addReg(ScratchReg), PReg, false, SPLimitOffset);
1695 BuildMI(incStackMBB, DL, TII.get(X86::JLE_4)).addMBB(incStackMBB);
1696
1697 stackCheckMBB->addSuccessor(&prologueMBB, 99);
1698 stackCheckMBB->addSuccessor(incStackMBB, 1);
1699 incStackMBB->addSuccessor(&prologueMBB, 99);
1700 incStackMBB->addSuccessor(incStackMBB, 1);
1701 }
1702#ifdef XDEBUG
1703 MF.verify();
1704#endif
1705}
Eli Bendersky8da87162013-02-21 20:05:00 +00001706
1707void X86FrameLowering::
1708eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1709 MachineBasicBlock::iterator I) const {
Eric Christopherfc6de422014-08-05 02:39:49 +00001710 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1711 const X86RegisterInfo &RegInfo = *static_cast<const X86RegisterInfo *>(
1712 MF.getSubtarget().getRegisterInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001713 unsigned StackPtr = RegInfo.getStackRegister();
1714 bool reseveCallFrame = hasReservedCallFrame(MF);
1715 int Opcode = I->getOpcode();
1716 bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode();
Eric Christopherf4381642014-06-05 22:00:31 +00001717 const X86Subtarget &STI = MF.getTarget().getSubtarget<X86Subtarget>();
Eli Bendersky8da87162013-02-21 20:05:00 +00001718 bool IsLP64 = STI.isTarget64BitLP64();
1719 DebugLoc DL = I->getDebugLoc();
1720 uint64_t Amount = !reseveCallFrame ? I->getOperand(0).getImm() : 0;
1721 uint64_t CalleeAmt = isDestroy ? I->getOperand(1).getImm() : 0;
1722 I = MBB.erase(I);
1723
1724 if (!reseveCallFrame) {
1725 // If the stack pointer can be changed after prologue, turn the
1726 // adjcallstackup instruction into a 'sub ESP, <amt>' and the
1727 // adjcallstackdown instruction into 'add ESP, <amt>'
1728 // TODO: consider using push / pop instead of sub + store / add
1729 if (Amount == 0)
1730 return;
1731
1732 // We need to keep the stack aligned properly. To do this, we round the
1733 // amount of space needed for the outgoing arguments up to the next
1734 // alignment boundary.
Eric Christopherd9134482014-08-04 21:25:23 +00001735 unsigned StackAlign = MF.getTarget()
1736 .getSubtargetImpl()
1737 ->getFrameLowering()
1738 ->getStackAlignment();
Eli Bendersky8da87162013-02-21 20:05:00 +00001739 Amount = (Amount + StackAlign - 1) / StackAlign * StackAlign;
1740
Craig Topper062a2ba2014-04-25 05:30:21 +00001741 MachineInstr *New = nullptr;
Eli Bendersky8da87162013-02-21 20:05:00 +00001742 if (Opcode == TII.getCallFrameSetupOpcode()) {
1743 New = BuildMI(MF, DL, TII.get(getSUBriOpcode(IsLP64, Amount)),
1744 StackPtr)
1745 .addReg(StackPtr)
1746 .addImm(Amount);
1747 } else {
1748 assert(Opcode == TII.getCallFrameDestroyOpcode());
1749
1750 // Factor out the amount the callee already popped.
1751 Amount -= CalleeAmt;
1752
1753 if (Amount) {
1754 unsigned Opc = getADDriOpcode(IsLP64, Amount);
1755 New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
1756 .addReg(StackPtr).addImm(Amount);
1757 }
1758 }
1759
1760 if (New) {
1761 // The EFLAGS implicit def is dead.
1762 New->getOperand(3).setIsDead();
1763
1764 // Replace the pseudo instruction with a new instruction.
1765 MBB.insert(I, New);
1766 }
1767
1768 return;
1769 }
1770
1771 if (Opcode == TII.getCallFrameDestroyOpcode() && CalleeAmt) {
1772 // If we are performing frame pointer elimination and if the callee pops
1773 // something off the stack pointer, add it back. We do this until we have
1774 // more advanced stack pointer tracking ability.
1775 unsigned Opc = getSUBriOpcode(IsLP64, CalleeAmt);
1776 MachineInstr *New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
1777 .addReg(StackPtr).addImm(CalleeAmt);
1778
1779 // The EFLAGS implicit def is dead.
1780 New->getOperand(3).setIsDead();
1781
1782 // We are not tracking the stack pointer adjustment by the callee, so make
1783 // sure we restore the stack pointer immediately after the call, there may
1784 // be spill code inserted between the CALL and ADJCALLSTACKUP instructions.
1785 MachineBasicBlock::iterator B = MBB.begin();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001786 while (I != B && !std::prev(I)->isCall())
Eli Bendersky8da87162013-02-21 20:05:00 +00001787 --I;
1788 MBB.insert(I, New);
1789 }
1790}
1791