LLVM 24.0.0git
X86FrameLowering.cpp
Go to the documentation of this file.
1//===-- X86FrameLowering.cpp - X86 Frame Information ----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the X86 implementation of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "X86FrameLowering.h"
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86InstrInfo.h"
19#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
21#include "llvm/ADT/Statistic.h"
30#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/Module.h"
34#include "llvm/MC/MCAsmInfo.h"
36#include "llvm/MC/MCSymbol.h"
37#include "llvm/Support/LEB128.h"
39#include <cstdlib>
40
41#define DEBUG_TYPE "x86-fl"
42
43STATISTIC(NumFrameLoopProbe, "Number of loop stack probes used in prologue");
44STATISTIC(NumFrameExtraProbe,
45 "Number of extra stack probes generated in prologue");
46STATISTIC(NumFunctionUsingPush2Pop2, "Number of functions using push2/pop2");
47
48using namespace llvm;
49
51 const Function &Fn = MF.getFunction();
52
53 // Whole module is in V3 mode.
55 return true;
56
57 // Otherwise promote a function that may use EGPR (R16-R31), which V1/V2
58 // unwind codes cannot encode. The per-function "+egpr" feature is the signal,
59 // so an auto-dispatch APX clone gets V3 while the baseline clone stays on the
60 // module default. We conservatively promote any egpr function rather than
61 // checking for an actual EGPR save, keeping this a cheap query. (PUSH2/POP2
62 // does not need V3: V1/V2 describe a PUSH2 as two SEH_PushReg codes.)
63 return Fn.needsUnwindTableEntry() &&
64 MF.getSubtarget<X86Subtarget>().hasEGPR();
65}
66
67static const TargetRegisterClass *
69 const TargetRegisterInfo &TRI) {
70 if (X86::VK16RegClass.contains(Reg))
71 return STI.hasBWI() ? &X86::VK64RegClass : &X86::VK16RegClass;
72 return TRI.getMinimalPhysRegClass(Reg);
73}
74
76 MaybeAlign StackAlignOverride)
77 : TargetFrameLowering(StackGrowsDown, StackAlignOverride.valueOrOne(),
78 STI.is64Bit() ? -8 : -4),
79 STI(STI), TII(*STI.getInstrInfo()), TRI(STI.getRegisterInfo()) {
80 // Cache a bunch of frame-related predicates for this subtarget.
81 SlotSize = TRI->getSlotSize();
82 assert(SlotSize == 4 || SlotSize == 8);
83 Is64Bit = STI.is64Bit();
84 IsLP64 = STI.isTarget64BitLP64();
85 // standard x86_64 uses 64-bit frame/stack pointers, x32 - 32-bit.
86 Uses64BitFramePtr = STI.isTarget64BitLP64();
87 StackPtr = TRI->getStackRegister();
88}
89
91 return !MF.getFrameInfo().hasVarSizedObjects() &&
92 !MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences() &&
93 !MF.getInfo<X86MachineFunctionInfo>()->hasPreallocatedCall();
94}
95
96/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
97/// call frame pseudos can be simplified. Having a FP, as in the default
98/// implementation, is not sufficient here since we can't always use it.
99/// Use a more nuanced condition.
101 const MachineFunction &MF) const {
102 return hasReservedCallFrame(MF) ||
103 MF.getInfo<X86MachineFunctionInfo>()->hasPreallocatedCall() ||
104 (hasFP(MF) && !TRI->hasStackRealignment(MF)) ||
105 TRI->hasBasePointer(MF);
106}
107
108// needsFrameIndexResolution - Do we need to perform FI resolution for
109// this function. Normally, this is required only when the function
110// has any stack objects. However, FI resolution actually has another job,
111// not apparent from the title - it resolves callframesetup/destroy
112// that were not simplified earlier.
113// So, this is required for x86 functions that have push sequences even
114// when there are no stack objects.
116 const MachineFunction &MF) const {
117 return MF.getFrameInfo().hasStackObjects() ||
118 MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences();
119}
120
121/// hasFPImpl - Return true if the specified function should have a dedicated
122/// frame pointer register. This is true if the function has variable sized
123/// allocas or if frame pointer elimination is disabled.
125 const MachineFrameInfo &MFI = MF.getFrameInfo();
126 return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
127 TRI->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
131 MF.callsUnwindInit() || MF.hasEHFunclets() || MF.callsEHReturn() ||
132 MFI.hasStackMap() || MFI.hasPatchPoint() ||
133 (isWin64Prologue(MF) && MFI.hasCopyImplyingStackAdjustment()));
134}
135
136static unsigned getSUBriOpcode(bool IsLP64) {
137 return IsLP64 ? X86::SUB64ri32 : X86::SUB32ri;
138}
139
140static unsigned getADDriOpcode(bool IsLP64) {
141 return IsLP64 ? X86::ADD64ri32 : X86::ADD32ri;
142}
143
144static unsigned getSUBrrOpcode(bool IsLP64) {
145 return IsLP64 ? X86::SUB64rr : X86::SUB32rr;
146}
147
148static unsigned getADDrrOpcode(bool IsLP64) {
149 return IsLP64 ? X86::ADD64rr : X86::ADD32rr;
150}
151
152static unsigned getANDriOpcode(bool IsLP64, int64_t Imm) {
153 return IsLP64 ? X86::AND64ri32 : X86::AND32ri;
154}
155
156static unsigned getLEArOpcode(bool IsLP64) {
157 return IsLP64 ? X86::LEA64r : X86::LEA32r;
158}
159
160// Push-Pop Acceleration (PPX) hint is used to indicate that the POP reads the
161// value written by the PUSH from the stack. The processor tracks these marked
162// instructions internally and fast-forwards register data between matching PUSH
163// and POP instructions, without going through memory or through the training
164// loop of the Fast Store Forwarding Predictor (FSFP). Instead, a more efficient
165// memory-renaming optimization can be used.
166//
167// The PPX hint is purely a performance hint. Instructions with this hint have
168// the same functional semantics as those without. PPX hints set by the
169// compiler that violate the balancing rule may turn off the PPX optimization,
170// but they will not affect program semantics.
171//
172// Hence, PPX is used for balanced spill/reloads (Exceptions and setjmp/longjmp
173// are not considered).
174//
175// PUSH2 and POP2 are instructions for (respectively) pushing/popping 2
176// GPRs at a time to/from the stack.
177static unsigned getPUSHOpcode(const X86Subtarget &ST) {
178 return ST.is64Bit() ? (ST.hasPPX() ? X86::PUSHP64r : X86::PUSH64r)
179 : X86::PUSH32r;
180}
181static unsigned getPOPOpcode(const X86Subtarget &ST) {
182 return ST.is64Bit() ? (ST.hasPPX() ? X86::POPP64r : X86::POP64r)
183 : X86::POP32r;
184}
185static unsigned getPUSH2Opcode(const X86Subtarget &ST) {
186 return ST.hasPPX() ? X86::PUSH2P : X86::PUSH2;
187}
188static unsigned getPOP2Opcode(const X86Subtarget &ST) {
189 return ST.hasPPX() ? X86::POP2P : X86::POP2;
190}
191
193 for (MachineBasicBlock::RegisterMaskPair RegMask : MBB.liveins()) {
194 MCRegister Reg = RegMask.PhysReg;
195
196 if (Reg == X86::RAX || Reg == X86::EAX || Reg == X86::AX ||
197 Reg == X86::AH || Reg == X86::AL)
198 return true;
199 }
200
201 return false;
202}
203
204/// Check if the flags need to be preserved before the terminators.
205/// This would be the case, if the eflags is live-in of the region
206/// composed by the terminators or live-out of that region, without
207/// being defined by a terminator.
208static bool
210 for (const MachineInstr &MI : MBB.terminators()) {
211 bool BreakNext = false;
212 for (const MachineOperand &MO : MI.operands()) {
213 if (!MO.isReg())
214 continue;
215 Register Reg = MO.getReg();
216 if (Reg != X86::EFLAGS)
217 continue;
218
219 // This terminator needs an eflags that is not defined
220 // by a previous another terminator:
221 // EFLAGS is live-in of the region composed by the terminators.
222 if (!MO.isDef())
223 return true;
224 // This terminator defines the eflags, i.e., we don't need to preserve it.
225 // However, we still need to check this specific terminator does not
226 // read a live-in value.
227 BreakNext = true;
228 }
229 // We found a definition of the eflags, no need to preserve them.
230 if (BreakNext)
231 return false;
232 }
233
234 // None of the terminators use or define the eflags.
235 // Check if they are live-out, that would imply we need to preserve them.
236 for (const MachineBasicBlock *Succ : MBB.successors())
237 if (Succ->isLiveIn(X86::EFLAGS))
238 return true;
239
240 return false;
241}
242
243constexpr uint64_t MaxSPChunk = (1ULL << 31) - 1;
244
245/// emitSPUpdate - Emit a series of instructions to increment / decrement the
246/// stack pointer by a constant value.
249 const DebugLoc &DL, int64_t NumBytes,
250 bool InEpilogue) const {
251 bool isSub = NumBytes < 0;
252 uint64_t Offset = isSub ? -NumBytes : NumBytes;
255
257 // We're being asked to adjust a 32-bit stack pointer by 4 GiB or more.
258 // This might be unreachable code, so don't complain now; just trap if
259 // it's reached at runtime.
260 BuildMI(MBB, MBBI, DL, TII.get(X86::TRAP));
261 return;
262 }
263
264 MachineFunction &MF = *MBB.getParent();
266 const X86TargetLowering &TLI = *STI.getTargetLowering();
267 const bool EmitInlineStackProbe = TLI.hasInlineStackProbe(MF);
268
269 // It's ok to not take into account large chunks when probing, as the
270 // allocation is split in smaller chunks anyway.
271 if (EmitInlineStackProbe && !InEpilogue) {
272
273 // This pseudo-instruction is going to be expanded, potentially using a
274 // loop, by inlineStackProbe().
275 BuildMI(MBB, MBBI, DL, TII.get(X86::STACKALLOC_W_PROBING)).addImm(Offset);
276 return;
277 } else if (Offset > MaxSPChunk) {
278 // Rather than emit a long series of instructions for large offsets,
279 // load the offset into a register and do one sub/add
280 unsigned Reg = 0;
281 unsigned Rax = (unsigned)(Uses64BitFramePtr ? X86::RAX : X86::EAX);
282
283 if (isSub && !isEAXLiveIn(MBB))
284 Reg = Rax;
285 else
286 Reg = getX86SubSuperRegister(TRI->findDeadCallerSavedReg(MBB, MBBI),
287 Uses64BitFramePtr ? 64 : 32);
288
289 unsigned AddSubRROpc = isSub ? getSUBrrOpcode(Uses64BitFramePtr)
291 if (Reg) {
292 BuildMI(MBB, MBBI, DL,
294 .addImm(Offset)
295 .setMIFlag(Flag);
296 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AddSubRROpc), StackPtr)
298 .addReg(Reg);
299 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead.
300 return;
301 } else if (Offset > 8 * MaxSPChunk) {
302 // If we would need more than 8 add or sub instructions (a >16GB stack
303 // frame), it's worth spilling RAX to materialize this immediate.
304 // pushq %rax
305 // movabsq +-$Offset+-SlotSize, %rax
306 // addq %rsp, %rax
307 // xchg %rax, (%rsp)
308 // movq (%rsp), %rsp
309 assert(Uses64BitFramePtr && "can't have 32-bit 16GB stack frame");
310 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64r))
312 .setMIFlag(Flag);
313 // Subtract is not commutative, so negate the offset and always use add.
314 // Subtract 8 less and add 8 more to account for the PUSH we just did.
315 if (isSub)
316 Offset = -(Offset - SlotSize);
317 else
319 BuildMI(MBB, MBBI, DL,
321 .addImm(Offset)
322 .setMIFlag(Flag);
323 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(X86::ADD64rr), Rax)
324 .addReg(Rax)
326 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead.
327 // Exchange the new SP in RAX with the top of the stack.
329 BuildMI(MBB, MBBI, DL, TII.get(X86::XCHG64rm), Rax).addReg(Rax),
330 StackPtr, false, 0);
331 // Load new SP from the top of the stack into RSP.
332 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rm), StackPtr),
333 StackPtr, false, 0);
334 return;
335 }
336 }
337
338 while (Offset) {
339 if (Offset == SlotSize) {
340 // Use push / pop for slot sized adjustments as a size optimization. We
341 // need to find a dead register when using pop.
342 unsigned Reg = isSub ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX)
343 : TRI->findDeadCallerSavedReg(MBB, MBBI);
344 if (Reg) {
345 unsigned Opc = isSub ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r)
346 : (Is64Bit ? X86::POP64r : X86::POP32r);
347 BuildMI(MBB, MBBI, DL, TII.get(Opc))
348 .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub))
349 .setMIFlag(Flag);
350 return;
351 }
352 }
353
354 uint64_t ThisVal = std::min(Offset, MaxSPChunk);
355
356 BuildStackAdjustment(MBB, MBBI, DL, isSub ? -ThisVal : ThisVal, InEpilogue)
357 .setMIFlag(Flag);
358
359 Offset -= ThisVal;
360 }
361}
362
363MachineInstrBuilder X86FrameLowering::BuildStackAdjustment(
365 const DebugLoc &DL, int64_t Offset, bool InEpilogue) const {
366 assert(Offset != 0 && "zero offset stack adjustment requested");
367
368 // On Atom, using LEA to adjust SP is preferred, but using it in the epilogue
369 // is tricky.
370 bool UseLEA;
371 if (!InEpilogue) {
372 // Check if inserting the prologue at the beginning
373 // of MBB would require to use LEA operations.
374 // We need to use LEA operations if EFLAGS is live in, because
375 // it means an instruction will read it before it gets defined.
376 UseLEA = STI.useLeaForSP() || MBB.isLiveIn(X86::EFLAGS);
377 } else {
378 // If we can use LEA for SP but we shouldn't, check that none
379 // of the terminators uses the eflags. Otherwise we will insert
380 // a ADD that will redefine the eflags and break the condition.
381 // Alternatively, we could move the ADD, but this may not be possible
382 // and is an optimization anyway.
383 UseLEA = canUseLEAForSPInEpilogue(*MBB.getParent());
384 if (UseLEA && !STI.useLeaForSP())
386 // If that assert breaks, that means we do not do the right thing
387 // in canUseAsEpilogue.
389 "We shouldn't have allowed this insertion point");
390 }
391
392 MachineInstrBuilder MI;
393 // Use an NF (no-flags) variant as a smaller replacement for LEA when EFLAGS
394 // must be preserved (i.e. only when we would otherwise emit LEA). If EFLAGS
395 // is dead we prefer the plain SUB/ADD, which is shorter than the EVEX-encoded
396 // NF form. The NF stack-adjust opcodes below are 64-bit (SUB64ri32_NF/
397 // ADD64ri32_NF), so don't use them for the x32 ABI where the stack pointer is
398 // 32-bit. NF cannot reach a Win64 epilogue (which never uses LEA for the SP
399 // adjustment unless it has a frame pointer, and that path doesn't go through
400 // here), so the Windows epilogue unwinder never sees an undisassemblable NF
401 // add/sub.
402 bool UseNF = UseLEA && STI.hasNF() && Uses64BitFramePtr;
403 bool IsSub = Offset < 0;
404 uint64_t AbsOffset = IsSub ? -Offset : Offset;
405 if (UseNF) {
406 const unsigned Opc = IsSub ? X86::SUB64ri32_NF : X86::ADD64ri32_NF;
407 MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
409 .addImm(AbsOffset);
410 // NF instructions define no EFLAGS, so there is nothing to mark dead.
411 } else if (UseLEA) {
414 StackPtr),
415 StackPtr, false, Offset);
416 } else {
417 const unsigned Opc = IsSub ? getSUBriOpcode(Uses64BitFramePtr)
419 MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
421 .addImm(AbsOffset);
422 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead.
423 }
424 return MI;
425}
426
427template <typename FoundT, typename CalcT>
428int64_t X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB,
430 FoundT FoundStackAdjust,
431 CalcT CalcNewOffset,
432 bool doMergeWithPrevious) const {
433 if ((doMergeWithPrevious && MBBI == MBB.begin()) ||
434 (!doMergeWithPrevious && MBBI == MBB.end()))
435 return CalcNewOffset(0);
436
437 MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI;
438
440 // It is assumed that ADD/SUB/LEA instruction is succeded by one CFI
441 // instruction, and that there are no DBG_VALUE or other instructions between
442 // ADD/SUB/LEA and its corresponding CFI instruction.
443 /* TODO: Add support for the case where there are multiple CFI instructions
444 below the ADD/SUB/LEA, e.g.:
445 ...
446 add
447 cfi_def_cfa_offset
448 cfi_offset
449 ...
450 */
451 if (doMergeWithPrevious && PI != MBB.begin() && PI->isCFIInstruction())
452 PI = std::prev(PI);
453
454 int64_t Offset = 0;
455 for (;;) {
456 unsigned Opc = PI->getOpcode();
457
458 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD32ri ||
459 Opc == X86::ADD64ri32_NF) &&
460 PI->getOperand(0).getReg() == StackPtr) {
461 assert(PI->getOperand(1).getReg() == StackPtr);
462 Offset = PI->getOperand(2).getImm();
463 } else if ((Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
464 PI->getOperand(0).getReg() == StackPtr &&
465 PI->getOperand(1).getReg() == StackPtr &&
466 PI->getOperand(2).getImm() == 1 &&
467 PI->getOperand(3).getReg() == X86::NoRegister &&
468 PI->getOperand(5).getReg() == X86::NoRegister) {
469 // For LEAs we have: def = lea SP, FI, noreg, Offset, noreg.
470 Offset = PI->getOperand(4).getImm();
471 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB32ri ||
472 Opc == X86::SUB64ri32_NF) &&
473 PI->getOperand(0).getReg() == StackPtr) {
474 assert(PI->getOperand(1).getReg() == StackPtr);
475 Offset = -PI->getOperand(2).getImm();
476 } else
477 return CalcNewOffset(0);
478
479 FoundStackAdjust(PI, Offset);
480 if ((uint64_t)std::abs((int64_t)CalcNewOffset(Offset)) < MaxSPChunk)
481 break;
482
483 if (doMergeWithPrevious ? (PI == MBB.begin()) : (PI == MBB.end()))
484 return CalcNewOffset(0);
485
486 PI = doMergeWithPrevious ? std::prev(PI) : std::next(PI);
487 }
488
489 PI = MBB.erase(PI);
490 if (PI != MBB.end() && PI->isCFIInstruction()) {
491 auto CIs = MBB.getParent()->getFrameInstructions();
492 MCCFIInstruction CI = CIs[PI->getOperand(0).getCFIIndex()];
495 PI = MBB.erase(PI);
496 }
497 if (!doMergeWithPrevious)
499
500 return CalcNewOffset(Offset);
501}
502
505 int64_t AddOffset,
506 bool doMergeWithPrevious) const {
507 return mergeSPUpdates(
508 MBB, MBBI, [AddOffset](int64_t Offset) { return AddOffset + Offset; },
509 doMergeWithPrevious);
510}
511
514 const DebugLoc &DL,
515 const MCCFIInstruction &CFIInst,
516 MachineInstr::MIFlag Flag) const {
517 MachineFunction &MF = *MBB.getParent();
518 unsigned CFIIndex = MF.addFrameInst(CFIInst);
519
521 MF.getInfo<X86MachineFunctionInfo>()->setHasCFIAdjustCfa(true);
522
523 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
524 .addCFIIndex(CFIIndex)
525 .setMIFlag(Flag);
526}
527
528/// Emits Dwarf Info specifying offsets of callee saved registers and
529/// frame pointer. This is called only when basic block sections are enabled.
532 MachineFunction &MF = *MBB.getParent();
533 if (!hasFP(MF)) {
535 return;
536 }
537 const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
538 const Register FramePtr = TRI->getFrameRegister(MF);
539 const Register MachineFramePtr =
540 STI.isTarget64BitILP32() ? Register(getX86SubSuperRegister(FramePtr, 64))
541 : FramePtr;
542 unsigned DwarfReg = MRI->getDwarfRegNum(MachineFramePtr, true);
543 // Offset = space for return address + size of the frame pointer itself.
544 int64_t Offset = (Is64Bit ? 8 : 4) + (Uses64BitFramePtr ? 8 : 4);
546 MCCFIInstruction::createOffset(nullptr, DwarfReg, -Offset));
548}
549
552 const DebugLoc &DL, bool IsPrologue) const {
553 MachineFunction &MF = *MBB.getParent();
554 MachineFrameInfo &MFI = MF.getFrameInfo();
555 const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
557
558 // Add callee saved registers to move list.
559 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
560
561 // Calculate offsets.
562 for (const CalleeSavedInfo &I : CSI) {
563 int64_t Offset = MFI.getObjectOffset(I.getFrameIdx());
564 MCRegister Reg = I.getReg();
565 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
566
567 if (IsPrologue) {
568 if (X86FI->getStackPtrSaveMI()) {
569 // +2*SlotSize because there is return address and ebp at the bottom
570 // of the stack.
571 // | retaddr |
572 // | ebp |
573 // | |<--ebp
574 Offset += 2 * SlotSize;
575 SmallString<64> CfaExpr;
576 CfaExpr.push_back(dwarf::DW_CFA_expression);
577 uint8_t buffer[16];
578 CfaExpr.append(buffer, buffer + encodeULEB128(DwarfReg, buffer));
579 CfaExpr.push_back(2);
580 Register FramePtr = TRI->getFrameRegister(MF);
581 const Register MachineFramePtr =
582 STI.isTarget64BitILP32()
584 : FramePtr;
585 unsigned DwarfFramePtr = MRI->getDwarfRegNum(MachineFramePtr, true);
586 CfaExpr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfFramePtr));
587 CfaExpr.append(buffer, buffer + encodeSLEB128(Offset, buffer));
589 MCCFIInstruction::createEscape(nullptr, CfaExpr.str()),
591 } else {
593 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
594 }
595 } else {
597 MCCFIInstruction::createRestore(nullptr, DwarfReg));
598 }
599 }
600 if (auto *MI = X86FI->getStackPtrSaveMI()) {
601 int FI = MI->getOperand(1).getIndex();
602 int64_t Offset = MFI.getObjectOffset(FI) + 2 * SlotSize;
603 SmallString<64> CfaExpr;
604 Register FramePtr = TRI->getFrameRegister(MF);
605 const Register MachineFramePtr =
606 STI.isTarget64BitILP32()
608 : FramePtr;
609 unsigned DwarfFramePtr = MRI->getDwarfRegNum(MachineFramePtr, true);
610 CfaExpr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfFramePtr));
611 uint8_t buffer[16];
612 CfaExpr.append(buffer, buffer + encodeSLEB128(Offset, buffer));
613 CfaExpr.push_back(dwarf::DW_OP_deref);
614
615 SmallString<64> DefCfaExpr;
616 DefCfaExpr.push_back(dwarf::DW_CFA_def_cfa_expression);
617 DefCfaExpr.append(buffer, buffer + encodeSLEB128(CfaExpr.size(), buffer));
618 DefCfaExpr.append(CfaExpr.str());
619 // DW_CFA_def_cfa_expression: DW_OP_breg5 offset, DW_OP_deref
621 MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str()),
623 }
624}
625
626void X86FrameLowering::emitZeroCallUsedRegs(BitVector RegsToZero,
628 RegScavenger *) const {
629 const MachineFunction &MF = *MBB.getParent();
630
631 // Insertion point.
632 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
633
634 // Fake a debug loc.
635 DebugLoc DL;
636 if (MBBI != MBB.end())
637 DL = MBBI->getDebugLoc();
638
639 // Zero out FP stack if referenced. Do this outside of the loop below so that
640 // it's done only once.
641 for (MCRegister Reg : RegsToZero.set_bits()) {
642 if (!X86::RFP80RegClass.contains(Reg))
643 continue;
644
645 // Do not push zeros over x87 return values. X86FloatingPoint records
646 // returned values as implicit ST0/ST1 uses on the return instruction.
647 unsigned NumFPRegs = 8;
648 if (MBBI->hasRegisterImplicitUseOperand(X86::ST0))
649 --NumFPRegs;
650 if (MBBI->hasRegisterImplicitUseOperand(X86::ST1))
651 --NumFPRegs;
652
653 for (unsigned i = 0; i != NumFPRegs; ++i)
654 BuildMI(MBB, MBBI, DL, TII.get(X86::LD_F0));
655
656 for (unsigned i = 0; i != NumFPRegs; ++i)
657 BuildMI(MBB, MBBI, DL, TII.get(X86::ST_FPrr)).addReg(X86::ST0);
658 break;
659 }
660
661 // For GPRs, we only care to clear out the 32-bit register.
662 BitVector GPRsToZero(TRI->getNumRegs());
663 for (MCRegister Reg : RegsToZero.set_bits())
664 if (TRI->isGeneralPurposeRegister(MF, Reg)) {
665 GPRsToZero.set(getX86SubSuperRegister(Reg, 32));
666 RegsToZero.reset(Reg);
667 }
668
669 // Zero out the GPRs first.
670 for (MCRegister Reg : GPRsToZero.set_bits())
671 TII.buildClearRegister(Reg, MBB, MBBI, DL);
672
673 // Zero out the remaining registers.
674 for (MCRegister Reg : RegsToZero.set_bits())
675 TII.buildClearRegister(Reg, MBB, MBBI, DL);
676}
677
680 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog,
681 std::optional<MachineFunction::DebugInstrOperandPair> InstrNum) const {
683 if (STI.isTargetWindowsCoreCLR()) {
684 if (InProlog) {
685 BuildMI(MBB, MBBI, DL, TII.get(X86::STACKALLOC_W_PROBING))
686 .addImm(0 /* no explicit stack size */);
687 } else {
688 emitStackProbeInline(MF, MBB, MBBI, DL, false);
689 }
690 } else {
691 emitStackProbeCall(MF, MBB, MBBI, DL, InProlog, InstrNum);
692 }
693}
694
696 return STI.isOSWindows() && !STI.isTargetWin64();
697}
698
700 MachineBasicBlock &PrologMBB) const {
701 auto Where = llvm::find_if(PrologMBB, [](MachineInstr &MI) {
702 return MI.getOpcode() == X86::STACKALLOC_W_PROBING;
703 });
704 if (Where != PrologMBB.end()) {
705 DebugLoc DL = PrologMBB.findDebugLoc(Where);
706 emitStackProbeInline(MF, PrologMBB, Where, DL, true);
707 Where->eraseFromParent();
708 }
709}
710
711void X86FrameLowering::emitStackProbeInline(MachineFunction &MF,
714 const DebugLoc &DL,
715 bool InProlog) const {
717 if (STI.isTargetWindowsCoreCLR() && STI.is64Bit())
718 emitStackProbeInlineWindowsCoreCLR64(MF, MBB, MBBI, DL, InProlog);
719 else
720 emitStackProbeInlineGeneric(MF, MBB, MBBI, DL, InProlog);
721}
722
723void X86FrameLowering::emitStackProbeInlineGeneric(
725 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog) const {
726 MachineInstr &AllocWithProbe = *MBBI;
727 uint64_t Offset = AllocWithProbe.getOperand(0).getImm();
728
731 assert(!(STI.is64Bit() && STI.isTargetWindowsCoreCLR()) &&
732 "different expansion expected for CoreCLR 64 bit");
733
734 const uint64_t StackProbeSize = TLI.getStackProbeSize(MF);
735 uint64_t ProbeChunk = StackProbeSize * 8;
736
737 uint64_t MaxAlign =
738 TRI->hasStackRealignment(MF) ? calculateMaxStackAlign(MF) : 0;
739
740 // Synthesize a loop or unroll it, depending on the number of iterations.
741 // BuildStackAlignAND ensures that only MaxAlign % StackProbeSize bits left
742 // between the unaligned rsp and current rsp.
743 if (Offset > ProbeChunk) {
744 emitStackProbeInlineGenericLoop(MF, MBB, MBBI, DL, Offset,
745 MaxAlign % StackProbeSize);
746 } else {
747 emitStackProbeInlineGenericBlock(MF, MBB, MBBI, DL, Offset,
748 MaxAlign % StackProbeSize);
749 }
750}
751
752void X86FrameLowering::emitStackProbeInlineGenericBlock(
755 uint64_t AlignOffset) const {
756
757 const bool NeedsDwarfCFI = needsDwarfCFI(MF);
758 const bool HasFP = hasFP(MF);
759 const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
760 const X86TargetLowering &TLI = *STI.getTargetLowering();
761 const unsigned MovMIOpc = Is64Bit ? X86::MOV64mi32 : X86::MOV32mi;
762 const uint64_t StackProbeSize = TLI.getStackProbeSize(MF);
763
764 uint64_t CurrentOffset = 0;
765
766 assert(AlignOffset < StackProbeSize);
767
768 // If the offset is so small it fits within a page, there's nothing to do.
769 if (StackProbeSize < Offset + AlignOffset) {
770
771 uint64_t StackAdjustment = StackProbeSize - AlignOffset;
772 BuildStackAdjustment(MBB, MBBI, DL, -StackAdjustment, /*InEpilogue=*/false)
773 .setMIFlag(MachineInstr::FrameSetup);
774 if (!HasFP && NeedsDwarfCFI) {
775 BuildCFI(
776 MBB, MBBI, DL,
777 MCCFIInstruction::createAdjustCfaOffset(nullptr, StackAdjustment));
778 }
779
780 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(MovMIOpc))
782 StackPtr, false, 0)
783 .addImm(0)
785 NumFrameExtraProbe++;
786 CurrentOffset = StackProbeSize - AlignOffset;
787 }
788
789 // For the next N - 1 pages, just probe. I tried to take advantage of
790 // natural probes but it implies much more logic and there was very few
791 // interesting natural probes to interleave.
792 while (CurrentOffset + StackProbeSize < Offset) {
793 BuildStackAdjustment(MBB, MBBI, DL, -StackProbeSize, /*InEpilogue=*/false)
794 .setMIFlag(MachineInstr::FrameSetup);
795
796 if (!HasFP && NeedsDwarfCFI) {
797 BuildCFI(
798 MBB, MBBI, DL,
799 MCCFIInstruction::createAdjustCfaOffset(nullptr, StackProbeSize));
800 }
801 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(MovMIOpc))
803 StackPtr, false, 0)
804 .addImm(0)
806 NumFrameExtraProbe++;
807 CurrentOffset += StackProbeSize;
808 }
809
810 // No need to probe the tail, it is smaller than a Page.
811 uint64_t ChunkSize = Offset - CurrentOffset;
812 if (ChunkSize == SlotSize) {
813 // Use push for slot sized adjustments as a size optimization,
814 // like emitSPUpdate does when not probing.
815 unsigned Reg = Is64Bit ? X86::RAX : X86::EAX;
816 unsigned Opc = Is64Bit ? X86::PUSH64r : X86::PUSH32r;
817 BuildMI(MBB, MBBI, DL, TII.get(Opc))
820 } else {
821 BuildStackAdjustment(MBB, MBBI, DL, -ChunkSize, /*InEpilogue=*/false)
822 .setMIFlag(MachineInstr::FrameSetup);
823 }
824 // No need to adjust Dwarf CFA offset here, the last position of the stack has
825 // been defined
826}
827
828void X86FrameLowering::emitStackProbeInlineGenericLoop(
831 uint64_t AlignOffset) const {
832 assert(Offset && "null offset");
833
834 assert(MBB.computeRegisterLiveness(TRI, X86::EFLAGS, MBBI) !=
836 "Inline stack probe loop will clobber live EFLAGS.");
837
838 const bool NeedsDwarfCFI = needsDwarfCFI(MF);
839 const bool HasFP = hasFP(MF);
840 const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
841 const X86TargetLowering &TLI = *STI.getTargetLowering();
842 const unsigned MovMIOpc = Is64Bit ? X86::MOV64mi32 : X86::MOV32mi;
843 const uint64_t StackProbeSize = TLI.getStackProbeSize(MF);
844
845 if (AlignOffset) {
846 if (AlignOffset < StackProbeSize) {
847 // Perform a first smaller allocation followed by a probe.
848 BuildStackAdjustment(MBB, MBBI, DL, -AlignOffset, /*InEpilogue=*/false)
849 .setMIFlag(MachineInstr::FrameSetup);
850
851 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(MovMIOpc))
853 StackPtr, false, 0)
854 .addImm(0)
856 NumFrameExtraProbe++;
857 Offset -= AlignOffset;
858 }
859 }
860
861 // Synthesize a loop
862 NumFrameLoopProbe++;
863 const BasicBlock *LLVM_BB = MBB.getBasicBlock();
864
865 MachineBasicBlock *testMBB = MF.CreateMachineBasicBlock(LLVM_BB);
866 MachineBasicBlock *tailMBB = MF.CreateMachineBasicBlock(LLVM_BB);
867
869 MF.insert(MBBIter, testMBB);
870 MF.insert(MBBIter, tailMBB);
871
872 Register FinalStackProbed = Uses64BitFramePtr ? X86::R11
873 : Is64Bit ? X86::R11D
874 : X86::EAX;
875
876 // save loop bound
877 {
878 const uint64_t BoundOffset = alignDown(Offset, StackProbeSize);
879
880 // Can we calculate the loop bound using SUB with a 32-bit immediate?
881 // Note that the immediate gets sign-extended when used with a 64-bit
882 // register, so in that case we only have 31 bits to work with.
883 bool canUseSub =
884 Uses64BitFramePtr ? isUInt<31>(BoundOffset) : isUInt<32>(BoundOffset);
885
886 if (canUseSub) {
887 const unsigned SUBOpc = getSUBriOpcode(Uses64BitFramePtr);
888
889 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::COPY), FinalStackProbed)
892 BuildMI(MBB, MBBI, DL, TII.get(SUBOpc), FinalStackProbed)
893 .addReg(FinalStackProbed)
894 .addImm(BoundOffset)
896 } else if (Uses64BitFramePtr) {
897 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), FinalStackProbed)
898 .addImm(-BoundOffset)
900 BuildMI(MBB, MBBI, DL, TII.get(X86::ADD64rr), FinalStackProbed)
901 .addReg(FinalStackProbed)
904 } else {
905 llvm_unreachable("Offset too large for 32-bit stack pointer");
906 }
907
908 // while in the loop, use loop-invariant reg for CFI,
909 // instead of the stack pointer, which changes during the loop
910 if (!HasFP && NeedsDwarfCFI) {
911 // x32 uses the same DWARF register numbers as x86-64,
912 // so there isn't a register number for r11d, we must use r11 instead
913 const Register DwarfFinalStackProbed =
914 STI.isTarget64BitILP32()
915 ? Register(getX86SubSuperRegister(FinalStackProbed, 64))
916 : FinalStackProbed;
917
920 nullptr, TRI->getDwarfRegNum(DwarfFinalStackProbed, true)));
922 MCCFIInstruction::createAdjustCfaOffset(nullptr, BoundOffset));
923 }
924 }
925
926 // allocate a page
927 BuildStackAdjustment(*testMBB, testMBB->end(), DL, -StackProbeSize,
928 /*InEpilogue=*/false)
929 .setMIFlag(MachineInstr::FrameSetup);
930
931 // touch the page
932 addRegOffset(BuildMI(testMBB, DL, TII.get(MovMIOpc))
934 StackPtr, false, 0)
935 .addImm(0)
937
938 // cmp with stack pointer bound
939 BuildMI(testMBB, DL, TII.get(Uses64BitFramePtr ? X86::CMP64rr : X86::CMP32rr))
941 .addReg(FinalStackProbed)
943
944 // jump
945 BuildMI(testMBB, DL, TII.get(X86::JCC_1))
946 .addMBB(testMBB)
949 testMBB->addSuccessor(testMBB);
950 testMBB->addSuccessor(tailMBB);
951
952 // BB management
953 tailMBB->splice(tailMBB->end(), &MBB, MBBI, MBB.end());
955 MBB.addSuccessor(testMBB);
956
957 // handle tail
958 const uint64_t TailOffset = Offset % StackProbeSize;
959 MachineBasicBlock::iterator TailMBBIter = tailMBB->begin();
960 if (TailOffset) {
961 BuildStackAdjustment(*tailMBB, TailMBBIter, DL, -TailOffset,
962 /*InEpilogue=*/false)
963 .setMIFlag(MachineInstr::FrameSetup);
964 }
965
966 // after the loop, switch back to stack pointer for CFI
967 if (!HasFP && NeedsDwarfCFI) {
968 // x32 uses the same DWARF register numbers as x86-64,
969 // so there isn't a register number for esp, we must use rsp instead
970 const Register DwarfStackPtr =
971 STI.isTarget64BitILP32()
974
975 BuildCFI(*tailMBB, TailMBBIter, DL,
977 nullptr, TRI->getDwarfRegNum(DwarfStackPtr, true)));
978 }
979
980 // Update Live In information
981 fullyRecomputeLiveIns({tailMBB, testMBB});
982}
983
984void X86FrameLowering::emitStackProbeInlineWindowsCoreCLR64(
986 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog) const {
987 const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
988 assert(STI.is64Bit() && "different expansion needed for 32 bit");
989 assert(STI.isTargetWindowsCoreCLR() && "custom expansion expects CoreCLR");
990 const TargetInstrInfo &TII = *STI.getInstrInfo();
991 const BasicBlock *LLVM_BB = MBB.getBasicBlock();
992
993 assert(MBB.computeRegisterLiveness(TRI, X86::EFLAGS, MBBI) !=
995 "Inline stack probe loop will clobber live EFLAGS.");
996
997 // RAX contains the number of bytes of desired stack adjustment.
998 // The handling here assumes this value has already been updated so as to
999 // maintain stack alignment.
1000 //
1001 // We need to exit with RSP modified by this amount and execute suitable
1002 // page touches to notify the OS that we're growing the stack responsibly.
1003 // All stack probing must be done without modifying RSP.
1004 //
1005 // MBB:
1006 // SizeReg = RAX;
1007 // ZeroReg = 0
1008 // CopyReg = RSP
1009 // Flags, TestReg = CopyReg - SizeReg
1010 // FinalReg = !Flags.Ovf ? TestReg : ZeroReg
1011 // LimitReg = gs magic thread env access
1012 // if FinalReg >= LimitReg goto ContinueMBB
1013 // RoundBB:
1014 // RoundReg = page address of FinalReg
1015 // LoopMBB:
1016 // LoopReg = PHI(LimitReg,ProbeReg)
1017 // ProbeReg = LoopReg - PageSize
1018 // [ProbeReg] = 0
1019 // if (ProbeReg > RoundReg) goto LoopMBB
1020 // ContinueMBB:
1021 // RSP = RSP - RAX
1022 // [rest of original MBB]
1023
1024 // Set up the new basic blocks
1025 MachineBasicBlock *RoundMBB = MF.CreateMachineBasicBlock(LLVM_BB);
1026 MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB);
1027 MachineBasicBlock *ContinueMBB = MF.CreateMachineBasicBlock(LLVM_BB);
1028
1029 MachineFunction::iterator MBBIter = std::next(MBB.getIterator());
1030 MF.insert(MBBIter, RoundMBB);
1031 MF.insert(MBBIter, LoopMBB);
1032 MF.insert(MBBIter, ContinueMBB);
1033
1034 // Split MBB and move the tail portion down to ContinueMBB.
1035 MachineBasicBlock::iterator BeforeMBBI = std::prev(MBBI);
1036 ContinueMBB->splice(ContinueMBB->begin(), &MBB, MBBI, MBB.end());
1037 ContinueMBB->transferSuccessorsAndUpdatePHIs(&MBB);
1038
1039 // Some useful constants
1040 const int64_t ThreadEnvironmentStackLimit = 0x10;
1041 const int64_t PageSize = 0x1000;
1042 const int64_t PageMask = ~(PageSize - 1);
1043
1044 // Registers we need. For the normal case we use virtual
1045 // registers. For the prolog expansion we use RAX, RCX and RDX.
1046 MachineRegisterInfo &MRI = MF.getRegInfo();
1047 const TargetRegisterClass *RegClass = &X86::GR64RegClass;
1048 const Register
1049 SizeReg = InProlog ? X86::RAX : MRI.createVirtualRegister(RegClass),
1050 ZeroReg = InProlog ? X86::RCX : MRI.createVirtualRegister(RegClass),
1051 CopyReg = InProlog ? X86::RDX : MRI.createVirtualRegister(RegClass),
1052 TestReg = InProlog ? X86::RDX : MRI.createVirtualRegister(RegClass),
1053 FinalReg = InProlog ? X86::RDX : MRI.createVirtualRegister(RegClass),
1054 RoundedReg = InProlog ? X86::RDX : MRI.createVirtualRegister(RegClass),
1055 LimitReg = InProlog ? X86::RCX : MRI.createVirtualRegister(RegClass),
1056 JoinReg = InProlog ? X86::RCX : MRI.createVirtualRegister(RegClass),
1057 ProbeReg = InProlog ? X86::RCX : MRI.createVirtualRegister(RegClass);
1058
1059 // SP-relative offsets where we can save RCX and RDX.
1060 int64_t RCXShadowSlot = 0;
1061 int64_t RDXShadowSlot = 0;
1062
1063 // If inlining in the prolog, save RCX and RDX.
1064 if (InProlog) {
1065 // Compute the offsets. We need to account for things already
1066 // pushed onto the stack at this point: return address, frame
1067 // pointer (if used), and callee saves.
1068 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1069 const int64_t CalleeSaveSize = X86FI->getCalleeSavedFrameSize();
1070 const bool HasFP = hasFP(MF);
1071
1072 // Check if we need to spill RCX and/or RDX.
1073 // Here we assume that no earlier prologue instruction changes RCX and/or
1074 // RDX, so checking the block live-ins is enough.
1075 const bool IsRCXLiveIn = MBB.isLiveIn(X86::RCX);
1076 const bool IsRDXLiveIn = MBB.isLiveIn(X86::RDX);
1077 int64_t InitSlot = 8 + CalleeSaveSize + (HasFP ? 8 : 0);
1078 // Assign the initial slot to both registers, then change RDX's slot if both
1079 // need to be spilled.
1080 if (IsRCXLiveIn)
1081 RCXShadowSlot = InitSlot;
1082 if (IsRDXLiveIn)
1083 RDXShadowSlot = InitSlot;
1084 if (IsRDXLiveIn && IsRCXLiveIn)
1085 RDXShadowSlot += 8;
1086 // Emit the saves if needed.
1087 if (IsRCXLiveIn)
1088 addRegOffset(BuildMI(&MBB, DL, TII.get(X86::MOV64mr)), X86::RSP, false,
1089 RCXShadowSlot)
1090 .addReg(X86::RCX);
1091 if (IsRDXLiveIn)
1092 addRegOffset(BuildMI(&MBB, DL, TII.get(X86::MOV64mr)), X86::RSP, false,
1093 RDXShadowSlot)
1094 .addReg(X86::RDX);
1095 } else {
1096 // Not in the prolog. Copy RAX to a virtual reg.
1097 BuildMI(&MBB, DL, TII.get(X86::MOV64rr), SizeReg).addReg(X86::RAX);
1098 }
1099
1100 // Add code to MBB to check for overflow and set the new target stack pointer
1101 // to zero if so.
1102 BuildMI(&MBB, DL, TII.get(X86::XOR64rr), ZeroReg)
1103 .addReg(ZeroReg, RegState::Undef)
1104 .addReg(ZeroReg, RegState::Undef);
1105 BuildMI(&MBB, DL, TII.get(X86::MOV64rr), CopyReg).addReg(X86::RSP);
1106 BuildMI(&MBB, DL, TII.get(X86::SUB64rr), TestReg)
1107 .addReg(CopyReg)
1108 .addReg(SizeReg);
1109 BuildMI(&MBB, DL, TII.get(X86::CMOV64rr), FinalReg)
1110 .addReg(TestReg)
1111 .addReg(ZeroReg)
1113
1114 // FinalReg now holds final stack pointer value, or zero if
1115 // allocation would overflow. Compare against the current stack
1116 // limit from the thread environment block. Note this limit is the
1117 // lowest touched page on the stack, not the point at which the OS
1118 // will cause an overflow exception, so this is just an optimization
1119 // to avoid unnecessarily touching pages that are below the current
1120 // SP but already committed to the stack by the OS.
1121 BuildMI(&MBB, DL, TII.get(X86::MOV64rm), LimitReg)
1122 .addReg(0)
1123 .addImm(1)
1124 .addReg(0)
1125 .addImm(ThreadEnvironmentStackLimit)
1126 .addReg(X86::GS);
1127 BuildMI(&MBB, DL, TII.get(X86::CMP64rr)).addReg(FinalReg).addReg(LimitReg);
1128 // Jump if the desired stack pointer is at or above the stack limit.
1129 BuildMI(&MBB, DL, TII.get(X86::JCC_1))
1130 .addMBB(ContinueMBB)
1132
1133 // Add code to roundMBB to round the final stack pointer to a page boundary.
1134 if (InProlog)
1135 RoundMBB->addLiveIn(FinalReg);
1136 BuildMI(RoundMBB, DL, TII.get(X86::AND64ri32), RoundedReg)
1137 .addReg(FinalReg)
1138 .addImm(PageMask);
1139 BuildMI(RoundMBB, DL, TII.get(X86::JMP_1)).addMBB(LoopMBB);
1140
1141 // LimitReg now holds the current stack limit, RoundedReg page-rounded
1142 // final RSP value. Add code to loopMBB to decrement LimitReg page-by-page
1143 // and probe until we reach RoundedReg.
1144 if (!InProlog) {
1145 BuildMI(LoopMBB, DL, TII.get(X86::PHI), JoinReg)
1146 .addReg(LimitReg)
1147 .addMBB(RoundMBB)
1148 .addReg(ProbeReg)
1149 .addMBB(LoopMBB);
1150 }
1151
1152 if (InProlog)
1153 LoopMBB->addLiveIn(JoinReg);
1154 addRegOffset(BuildMI(LoopMBB, DL, TII.get(X86::LEA64r), ProbeReg), JoinReg,
1155 false, -PageSize);
1156
1157 // Probe by storing a byte onto the stack.
1158 BuildMI(LoopMBB, DL, TII.get(X86::MOV8mi))
1159 .addReg(ProbeReg)
1160 .addImm(1)
1161 .addReg(0)
1162 .addImm(0)
1163 .addReg(0)
1164 .addImm(0);
1165
1166 if (InProlog)
1167 LoopMBB->addLiveIn(RoundedReg);
1168 BuildMI(LoopMBB, DL, TII.get(X86::CMP64rr))
1169 .addReg(RoundedReg)
1170 .addReg(ProbeReg);
1171 BuildMI(LoopMBB, DL, TII.get(X86::JCC_1))
1172 .addMBB(LoopMBB)
1174
1175 MachineBasicBlock::iterator ContinueMBBI = ContinueMBB->getFirstNonPHI();
1176
1177 // If in prolog, restore RDX and RCX.
1178 if (InProlog) {
1179 if (RCXShadowSlot) // It means we spilled RCX in the prologue.
1180 addRegOffset(BuildMI(*ContinueMBB, ContinueMBBI, DL,
1181 TII.get(X86::MOV64rm), X86::RCX),
1182 X86::RSP, false, RCXShadowSlot);
1183 if (RDXShadowSlot) // It means we spilled RDX in the prologue.
1184 addRegOffset(BuildMI(*ContinueMBB, ContinueMBBI, DL,
1185 TII.get(X86::MOV64rm), X86::RDX),
1186 X86::RSP, false, RDXShadowSlot);
1187 }
1188
1189 // Now that the probing is done, add code to continueMBB to update
1190 // the stack pointer for real.
1191 BuildMI(*ContinueMBB, ContinueMBBI, DL, TII.get(X86::SUB64rr), X86::RSP)
1192 .addReg(X86::RSP)
1193 .addReg(SizeReg);
1194
1195 // Add the control flow edges we need.
1196 MBB.addSuccessor(ContinueMBB);
1197 MBB.addSuccessor(RoundMBB);
1198 RoundMBB->addSuccessor(LoopMBB);
1199 LoopMBB->addSuccessor(ContinueMBB);
1200 LoopMBB->addSuccessor(LoopMBB);
1201
1202 if (InProlog) {
1203 LivePhysRegs LiveRegs;
1204 computeAndAddLiveIns(LiveRegs, *ContinueMBB);
1205 }
1206
1207 // Mark all the instructions added to the prolog as frame setup.
1208 if (InProlog) {
1209 for (++BeforeMBBI; BeforeMBBI != MBB.end(); ++BeforeMBBI) {
1210 BeforeMBBI->setFlag(MachineInstr::FrameSetup);
1211 }
1212 for (MachineInstr &MI : *RoundMBB) {
1214 }
1215 for (MachineInstr &MI : *LoopMBB) {
1217 }
1218 for (MachineInstr &MI :
1219 llvm::make_range(ContinueMBB->begin(), ContinueMBBI)) {
1221 }
1222 }
1223}
1224
1225void X86FrameLowering::emitStackProbeCall(
1227 MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog,
1228 std::optional<MachineFunction::DebugInstrOperandPair> InstrNum) const {
1229 bool IsLargeCodeModel = MF.getTarget().getCodeModel() == CodeModel::Large;
1230
1231 // FIXME: Add indirect thunk support and remove this.
1232 if (Is64Bit && IsLargeCodeModel && STI.useIndirectThunkCalls())
1233 report_fatal_error("Emitting stack probe calls on 64-bit with the large "
1234 "code model and indirect thunks not yet implemented.");
1235
1236 assert(MBB.computeRegisterLiveness(TRI, X86::EFLAGS, MBBI) !=
1238 "Stack probe calls will clobber live EFLAGS.");
1239
1240 unsigned CallOp;
1241 if (Is64Bit)
1242 CallOp = IsLargeCodeModel ? X86::CALL64r : X86::CALL64pcrel32;
1243 else
1244 CallOp = X86::CALLpcrel32;
1245
1246 StringRef Symbol = STI.getTargetLowering()->getStackProbeSymbolName(MF);
1247
1248 MachineInstrBuilder CI;
1249 MachineBasicBlock::iterator ExpansionMBBI = std::prev(MBBI);
1250
1251 // All current stack probes take AX and SP as input, clobber flags, and
1252 // preserve all registers. x86_64 probes leave RSP unmodified.
1254 // For the large code model, we have to call through a register. Use R11,
1255 // as it is scratch in all supported calling conventions.
1256 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::R11)
1258 CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)).addReg(X86::R11);
1259 } else {
1260 CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp))
1262 }
1263
1264 unsigned AX = Uses64BitFramePtr ? X86::RAX : X86::EAX;
1265 unsigned SP = Uses64BitFramePtr ? X86::RSP : X86::ESP;
1271
1272 MachineInstr *ModInst = CI;
1273 if (STI.isTargetWin64() || !STI.isOSWindows()) {
1274 // MSVC x32's _chkstk and cygwin/mingw's _alloca adjust %esp themselves.
1275 // MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp
1276 // themselves. They also does not clobber %rax so we can reuse it when
1277 // adjusting %rsp.
1278 // All other platforms do not specify a particular ABI for the stack probe
1279 // function, so we arbitrarily define it to not adjust %esp/%rsp itself.
1280 ModInst =
1282 .addReg(SP)
1283 .addReg(AX);
1284 }
1285
1286 // DebugInfo variable locations -- if there's an instruction number for the
1287 // allocation (i.e., DYN_ALLOC_*), substitute it for the instruction that
1288 // modifies SP.
1289 if (InstrNum) {
1290 if (STI.isTargetWin64() || !STI.isOSWindows()) {
1291 // Label destination operand of the subtract.
1292 MF.makeDebugValueSubstitution(*InstrNum,
1293 {ModInst->getDebugInstrNum(), 0});
1294 } else {
1295 // Label the call. The operand number is the penultimate operand, zero
1296 // based.
1297 unsigned SPDefOperand = ModInst->getNumOperands() - 2;
1299 *InstrNum, {ModInst->getDebugInstrNum(), SPDefOperand});
1300 }
1301 }
1302
1303 if (InProlog) {
1304 // Apply the frame setup flag to all inserted instrs.
1305 for (++ExpansionMBBI; ExpansionMBBI != MBBI; ++ExpansionMBBI)
1306 ExpansionMBBI->setFlag(MachineInstr::FrameSetup);
1307 }
1308}
1309
1310static unsigned calculateSetFPREG(uint64_t SPAdjust) {
1311 // Win64 ABI has a less restrictive limitation of 240; 128 works equally well
1312 // and might require smaller successive adjustments.
1313 const uint64_t Win64MaxSEHOffset = 128;
1314 uint64_t SEHFrameOffset = std::min(SPAdjust, Win64MaxSEHOffset);
1315 // Win64 ABI requires 16-byte alignment for the UWOP_SET_FPREG opcode.
1316 return SEHFrameOffset & -16;
1317}
1318
1319// If we're forcing a stack realignment we can't rely on just the frame
1320// info, we need to know the ABI stack alignment as well in case we
1321// have a call out. Otherwise just make sure we have some alignment - we'll
1322// go with the minimum SlotSize.
1323uint64_t
1324X86FrameLowering::calculateMaxStackAlign(const MachineFunction &MF) const {
1325 const MachineFrameInfo &MFI = MF.getFrameInfo();
1326 Align MaxAlign = MFI.getMaxAlign(); // Desired stack alignment.
1327 Align StackAlign = getStackAlign();
1328 bool HasRealign = MF.getFunction().hasFnAttribute("stackrealign");
1329 if (HasRealign) {
1330 if (MFI.hasCalls())
1331 MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign;
1332 else if (MaxAlign < SlotSize)
1333 MaxAlign = Align(SlotSize);
1334 }
1335
1337 if (HasRealign)
1338 MaxAlign = (MaxAlign > 16) ? MaxAlign : Align(16);
1339 else
1340 MaxAlign = Align(16);
1341 }
1342 return MaxAlign.value();
1343}
1344
1345void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB,
1347 const DebugLoc &DL, Register Reg,
1348 uint64_t MaxAlign) const {
1349 uint64_t Val = -MaxAlign;
1350 unsigned AndOp = getANDriOpcode(Uses64BitFramePtr, Val);
1351
1352 MachineFunction &MF = *MBB.getParent();
1353 const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
1354 const X86TargetLowering &TLI = *STI.getTargetLowering();
1355 const uint64_t StackProbeSize = TLI.getStackProbeSize(MF);
1356 const bool EmitInlineStackProbe = TLI.hasInlineStackProbe(MF);
1357
1358 // We want to make sure that (in worst case) less than StackProbeSize bytes
1359 // are not probed after the AND. This assumption is used in
1360 // emitStackProbeInlineGeneric.
1361 if (Reg == StackPtr && EmitInlineStackProbe && MaxAlign >= StackProbeSize) {
1362 {
1363 NumFrameLoopProbe++;
1364 MachineBasicBlock *entryMBB =
1366 MachineBasicBlock *headMBB =
1368 MachineBasicBlock *bodyMBB =
1370 MachineBasicBlock *footMBB =
1372
1374 MF.insert(MBBIter, entryMBB);
1375 MF.insert(MBBIter, headMBB);
1376 MF.insert(MBBIter, bodyMBB);
1377 MF.insert(MBBIter, footMBB);
1378 const unsigned MovMIOpc = Is64Bit ? X86::MOV64mi32 : X86::MOV32mi;
1379 Register FinalStackProbed = Uses64BitFramePtr ? X86::R11
1380 : Is64Bit ? X86::R11D
1381 : X86::EAX;
1382
1383 // Setup entry block
1384 {
1385
1386 entryMBB->splice(entryMBB->end(), &MBB, MBB.begin(), MBBI);
1387 BuildMI(entryMBB, DL, TII.get(TargetOpcode::COPY), FinalStackProbed)
1390 MachineInstr *MI =
1391 BuildMI(entryMBB, DL, TII.get(AndOp), FinalStackProbed)
1392 .addReg(FinalStackProbed)
1393 .addImm(Val)
1395
1396 // The EFLAGS implicit def is dead.
1397 MI->getOperand(3).setIsDead();
1398
1399 BuildMI(entryMBB, DL,
1400 TII.get(Uses64BitFramePtr ? X86::CMP64rr : X86::CMP32rr))
1401 .addReg(FinalStackProbed)
1404 BuildMI(entryMBB, DL, TII.get(X86::JCC_1))
1405 .addMBB(&MBB)
1408 entryMBB->addSuccessor(headMBB);
1409 entryMBB->addSuccessor(&MBB);
1410 }
1411
1412 // Loop entry block
1413
1414 {
1415 const unsigned SUBOpc = getSUBriOpcode(Uses64BitFramePtr);
1416 BuildMI(headMBB, DL, TII.get(SUBOpc), StackPtr)
1418 .addImm(StackProbeSize)
1420
1421 BuildMI(headMBB, DL,
1422 TII.get(Uses64BitFramePtr ? X86::CMP64rr : X86::CMP32rr))
1424 .addReg(FinalStackProbed)
1426
1427 // jump to the footer if StackPtr < FinalStackProbed
1428 BuildMI(headMBB, DL, TII.get(X86::JCC_1))
1429 .addMBB(footMBB)
1432
1433 headMBB->addSuccessor(bodyMBB);
1434 headMBB->addSuccessor(footMBB);
1435 }
1436
1437 // setup loop body
1438 {
1439 addRegOffset(BuildMI(bodyMBB, DL, TII.get(MovMIOpc))
1441 StackPtr, false, 0)
1442 .addImm(0)
1444
1445 const unsigned SUBOpc = getSUBriOpcode(Uses64BitFramePtr);
1446 BuildMI(bodyMBB, DL, TII.get(SUBOpc), StackPtr)
1448 .addImm(StackProbeSize)
1450
1451 // cmp with stack pointer bound
1452 BuildMI(bodyMBB, DL,
1453 TII.get(Uses64BitFramePtr ? X86::CMP64rr : X86::CMP32rr))
1454 .addReg(FinalStackProbed)
1457
1458 // jump back while FinalStackProbed < StackPtr
1459 BuildMI(bodyMBB, DL, TII.get(X86::JCC_1))
1460 .addMBB(bodyMBB)
1463 bodyMBB->addSuccessor(bodyMBB);
1464 bodyMBB->addSuccessor(footMBB);
1465 }
1466
1467 // setup loop footer
1468 {
1469 BuildMI(footMBB, DL, TII.get(TargetOpcode::COPY), StackPtr)
1470 .addReg(FinalStackProbed)
1472 addRegOffset(BuildMI(footMBB, DL, TII.get(MovMIOpc))
1474 StackPtr, false, 0)
1475 .addImm(0)
1477 footMBB->addSuccessor(&MBB);
1478 }
1479
1480 fullyRecomputeLiveIns({footMBB, bodyMBB, headMBB, &MBB});
1481 }
1482 } else {
1483 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AndOp), Reg)
1484 .addReg(Reg)
1485 .addImm(Val)
1487
1488 // The EFLAGS implicit def is dead.
1489 MI->getOperand(3).setIsDead();
1490 }
1491}
1492
1494 // x86-64 (non Win64) has a 128 byte red zone which is guaranteed not to be
1495 // clobbered by any interrupt handler.
1496 assert(&STI == &MF.getSubtarget<X86Subtarget>() &&
1497 "MF used frame lowering for wrong subtarget");
1498 const Function &Fn = MF.getFunction();
1499 const bool IsWin64CC = STI.isCallingConvWin64(Fn.getCallingConv());
1500 return Is64Bit && !IsWin64CC && !Fn.hasFnAttribute(Attribute::NoRedZone);
1501}
1502
1503/// Return true if we need to use the restricted Windows x64 prologue and
1504/// epilogue code patterns that can be described with WinCFI (.seh_*
1505/// directives).
1506bool X86FrameLowering::isWin64Prologue(const MachineFunction &MF) const {
1507 return MF.getTarget().getMCAsmInfo().usesWindowsCFI();
1508}
1509
1510bool X86FrameLowering::needsDwarfCFI(const MachineFunction &MF) const {
1511 return !isWin64Prologue(MF) && MF.needsFrameMoves();
1512}
1513
1514/// Return true if an opcode is part of the REP group of instructions
1515static bool isOpcodeRep(unsigned Opcode) {
1516 switch (Opcode) {
1517 case X86::REPNE_PREFIX:
1518 case X86::REP_MOVSB_32:
1519 case X86::REP_MOVSB_64:
1520 case X86::REP_MOVSD_32:
1521 case X86::REP_MOVSD_64:
1522 case X86::REP_MOVSQ_32:
1523 case X86::REP_MOVSQ_64:
1524 case X86::REP_MOVSW_32:
1525 case X86::REP_MOVSW_64:
1526 case X86::REP_PREFIX:
1527 case X86::REP_STOSB_32:
1528 case X86::REP_STOSB_64:
1529 case X86::REP_STOSD_32:
1530 case X86::REP_STOSD_64:
1531 case X86::REP_STOSQ_32:
1532 case X86::REP_STOSQ_64:
1533 case X86::REP_STOSW_32:
1534 case X86::REP_STOSW_64:
1535 return true;
1536 default:
1537 break;
1538 }
1539 return false;
1540}
1541
1542/// emitPrologue - Push callee-saved registers onto the stack, which
1543/// automatically adjust the stack pointer. Adjust the stack pointer to allocate
1544/// space for local variables. Also emit labels used by the exception handler to
1545/// generate the exception handling frames.
1546
1547/*
1548 Here's a gist of what gets emitted:
1549
1550 ; Establish frame pointer, if needed
1551 [if needs FP]
1552 push %rbp
1553 .cfi_def_cfa_offset 16
1554 .cfi_offset %rbp, -16
1555 .seh_pushreg %rpb
1556 mov %rsp, %rbp
1557 .cfi_def_cfa_register %rbp
1558
1559 ; Spill general-purpose registers
1560 [for all callee-saved GPRs]
1561 pushq %<reg>
1562 [if not needs FP]
1563 .cfi_def_cfa_offset (offset from RETADDR)
1564 .seh_pushreg %<reg>
1565
1566 ; If the required stack alignment > default stack alignment
1567 ; rsp needs to be re-aligned. This creates a "re-alignment gap"
1568 ; of unknown size in the stack frame.
1569 [if stack needs re-alignment]
1570 and $MASK, %rsp
1571
1572 ; Allocate space for locals
1573 [if target is Windows and allocated space > 4096 bytes]
1574 ; Windows needs special care for allocations larger
1575 ; than one page.
1576 mov $NNN, %rax
1577 call ___chkstk_ms/___chkstk
1578 sub %rax, %rsp
1579 [else]
1580 sub $NNN, %rsp
1581
1582 [if needs FP]
1583 .seh_stackalloc (size of XMM spill slots)
1584 .seh_setframe %rbp, SEHFrameOffset ; = size of all spill slots
1585 [else]
1586 .seh_stackalloc NNN
1587
1588 ; Spill XMMs
1589 ; Note, that while only Windows 64 ABI specifies XMMs as callee-preserved,
1590 ; they may get spilled on any platform, if the current function
1591 ; calls @llvm.eh.unwind.init
1592 [if needs FP]
1593 [for all callee-saved XMM registers]
1594 movaps %<xmm reg>, -MMM(%rbp)
1595 [for all callee-saved XMM registers]
1596 .seh_savexmm %<xmm reg>, (-MMM + SEHFrameOffset)
1597 ; i.e. the offset relative to (%rbp - SEHFrameOffset)
1598 [else]
1599 [for all callee-saved XMM registers]
1600 movaps %<xmm reg>, KKK(%rsp)
1601 [for all callee-saved XMM registers]
1602 .seh_savexmm %<xmm reg>, KKK
1603
1604 .seh_endprologue
1605
1606 [if needs base pointer]
1607 mov %rsp, %rbx
1608 [if needs to restore base pointer]
1609 mov %rsp, -MMM(%rbp)
1610
1611 ; Emit CFI info
1612 [if needs FP]
1613 [for all callee-saved registers]
1614 .cfi_offset %<reg>, (offset from %rbp)
1615 [else]
1616 .cfi_def_cfa_offset (offset from RETADDR)
1617 [for all callee-saved registers]
1618 .cfi_offset %<reg>, (offset from %rsp)
1619
1620 Notes:
1621 - .seh directives are emitted only for Windows 64 ABI
1622 - .cv_fpo directives are emitted on win32 when emitting CodeView
1623 - .cfi directives are emitted for all other ABIs
1624 - for 32-bit code, substitute %e?? registers for %r??
1625*/
1626
1628 MachineBasicBlock &MBB) const {
1629 assert(&STI == &MF.getSubtarget<X86Subtarget>() &&
1630 "MF used frame lowering for wrong subtarget");
1632 MachineFrameInfo &MFI = MF.getFrameInfo();
1633 const Function &Fn = MF.getFunction();
1635 uint64_t MaxAlign = calculateMaxStackAlign(MF); // Desired stack alignment.
1636 uint64_t StackSize = MFI.getStackSize(); // Number of bytes to allocate.
1637 bool IsFunclet = MBB.isEHFuncletEntry();
1639 if (Fn.hasPersonalityFn())
1640 Personality = classifyEHPersonality(Fn.getPersonalityFn());
1641 bool FnHasClrFunclet =
1642 MF.hasEHFunclets() && Personality == EHPersonality::CoreCLR;
1643 bool IsClrFunclet = IsFunclet && FnHasClrFunclet;
1644 bool HasFP = hasFP(MF);
1645 bool IsWin64Prologue = isWin64Prologue(MF);
1646 bool NeedsWin64CFI = IsWin64Prologue && Fn.needsUnwindTableEntry();
1647 // FIXME: Emit FPO data for EH funclets.
1648 bool NeedsWinFPO = !IsFunclet && STI.isTargetWin32() &&
1650 bool NeedsWinCFI = NeedsWin64CFI || NeedsWinFPO;
1651 bool NeedsDwarfCFI = needsDwarfCFI(MF);
1652 bool IsWin64UnwindV3 = NeedsWin64CFI && requireWinX64UnwindV3(MF);
1653 Register FramePtr = TRI->getFrameRegister(MF);
1654 const Register MachineFramePtr =
1655 STI.isTarget64BitILP32() ? Register(getX86SubSuperRegister(FramePtr, 64))
1656 : FramePtr;
1657 Register BasePtr = TRI->getBaseRegister();
1658 bool HasWinCFI = false;
1659
1660 // Helpers to emit Windows x64 unwind SEH pseudos with the correct placement.
1661 // V1/V2: pseudo goes after the real instruction.
1662 // V3: pseudo goes before the real instruction.
1663 // Usage:
1664 // EmitSEHBefore([&]{ BuildMI(...SEH_PushReg...); });
1665 // BuildMI(... real instruction ...);
1666 // EmitSEHAfter([&]{ BuildMI(...SEH_PushReg...); });
1667 auto EmitSEHBefore = [&](auto EmitFn) {
1668 if (NeedsWinCFI && IsWin64UnwindV3) {
1669 HasWinCFI = true;
1670 EmitFn();
1671 }
1672 };
1673 auto EmitSEHAfter = [&](auto EmitFn) {
1674 if (NeedsWinCFI && !IsWin64UnwindV3) {
1675 HasWinCFI = true;
1676 EmitFn();
1677 }
1678 };
1679
1680 // Debug location must be unknown since the first debug location is used
1681 // to determine the end of the prologue.
1682 DebugLoc DL;
1683 Register ArgBaseReg;
1684
1685 // Emit extra prolog for argument stack slot reference.
1686 if (auto *MI = X86FI->getStackPtrSaveMI()) {
1687 // MI is lea instruction that created in X86ArgumentStackSlotPass.
1688 // Creat extra prolog for stack realignment.
1689 ArgBaseReg = MI->getOperand(0).getReg();
1690 // leal 4(%esp), %basereg
1691 // .cfi_def_cfa %basereg, 0
1692 // andl $-128, %esp
1693 // pushl -4(%basereg)
1694 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::LEA64r : X86::LEA32r),
1695 ArgBaseReg)
1697 .addImm(1)
1698 .addUse(X86::NoRegister)
1700 .addUse(X86::NoRegister)
1702 if (NeedsDwarfCFI) {
1703 // .cfi_def_cfa %basereg, 0
1704 unsigned DwarfStackPtr = TRI->getDwarfRegNum(ArgBaseReg, true);
1705 BuildCFI(MBB, MBBI, DL,
1706 MCCFIInstruction::cfiDefCfa(nullptr, DwarfStackPtr, 0),
1708 }
1709 BuildStackAlignAND(MBB, MBBI, DL, StackPtr, MaxAlign);
1710 int64_t Offset = -(int64_t)SlotSize;
1711 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::PUSH64rmm : X86::PUSH32rmm))
1712 .addReg(ArgBaseReg)
1713 .addImm(1)
1714 .addReg(X86::NoRegister)
1715 .addImm(Offset)
1716 .addReg(X86::NoRegister)
1718 }
1719
1720 // Space reserved for stack-based arguments when making a (ABI-guaranteed)
1721 // tail call.
1722 unsigned TailCallArgReserveSize = -X86FI->getTCReturnAddrDelta();
1723 if (TailCallArgReserveSize && IsWin64Prologue)
1724 report_fatal_error("Can't handle guaranteed tail call under win64 yet");
1725
1726 const bool EmitStackProbeCall =
1727 STI.getTargetLowering()->hasStackProbeSymbol(MF);
1728 unsigned StackProbeSize = STI.getTargetLowering()->getStackProbeSize(MF);
1729
1730 if (HasFP && X86FI->hasSwiftAsyncContext()) {
1733 if (STI.swiftAsyncContextIsDynamicallySet()) {
1734 // The special symbol below is absolute and has a *value* suitable to be
1735 // combined with the frame pointer directly.
1736 BuildMI(MBB, MBBI, DL, TII.get(X86::OR64rm), MachineFramePtr)
1737 .addUse(MachineFramePtr)
1738 .addUse(X86::RIP)
1739 .addImm(1)
1740 .addUse(X86::NoRegister)
1741 .addExternalSymbol("swift_async_extendedFramePointerFlags",
1743 .addUse(X86::NoRegister);
1744 break;
1745 }
1746 [[fallthrough]];
1747
1749 assert(
1750 !IsWin64Prologue &&
1751 "win64 prologue does not set the bit 60 in the saved frame pointer");
1752 BuildMI(MBB, MBBI, DL, TII.get(X86::BTS64ri8), MachineFramePtr)
1753 .addUse(MachineFramePtr)
1754 .addImm(60)
1756 break;
1757
1759 break;
1760 }
1761 }
1762
1763 // Re-align the stack on 64-bit if the x86-interrupt calling convention is
1764 // used and an error code was pushed, since the x86-64 ABI requires a 16-byte
1765 // stack alignment.
1767 Fn.arg_size() == 2) {
1768 StackSize += 8;
1769 MFI.setStackSize(StackSize);
1770
1771 // Update the stack pointer by pushing a register. This is the instruction
1772 // emitted that would be end up being emitted by a call to `emitSPUpdate`.
1773 // Hard-coding the update to a push avoids emitting a second
1774 // `STACKALLOC_W_PROBING` instruction in the save block: We know that stack
1775 // probing isn't needed anyways for an 8-byte update.
1776 // Pushing a register leaves us in a similar situation to a regular
1777 // function call where we know that the address at (rsp-8) is writeable.
1778 // That way we avoid any off-by-ones with stack probing for additional
1779 // stack pointer updates later on.
1780 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64r))
1781 .addReg(X86::RAX, RegState::Undef)
1783 }
1784
1785 // If this is x86-64 and the Red Zone is not disabled, if we are a leaf
1786 // function, and use up to 128 bytes of stack space, don't have a frame
1787 // pointer, calls, or dynamic alloca then we do not need to adjust the
1788 // stack pointer (we fit in the Red Zone). We also check that we don't
1789 // push and pop from the stack.
1790 if (has128ByteRedZone(MF) && !TRI->hasStackRealignment(MF) &&
1791 !MFI.hasVarSizedObjects() && // No dynamic alloca.
1792 !MFI.adjustsStack() && // No calls.
1793 !EmitStackProbeCall && // No stack probes.
1794 !MFI.hasCopyImplyingStackAdjustment() && // Don't push and pop.
1795 !MF.shouldSplitStack()) { // Regular stack
1796 uint64_t MinSize =
1798 if (HasFP)
1799 MinSize += SlotSize;
1800 X86FI->setUsesRedZone(MinSize > 0 || StackSize > 0);
1801 StackSize = std::max(MinSize, StackSize > 128 ? StackSize - 128 : 0);
1802 MFI.setStackSize(StackSize);
1803 }
1804
1805 // Insert stack pointer adjustment for later moving of return addr. Only
1806 // applies to tail call optimized functions where the callee argument stack
1807 // size is bigger than the callers.
1808 if (TailCallArgReserveSize != 0) {
1809 BuildStackAdjustment(MBB, MBBI, DL, -(int)TailCallArgReserveSize,
1810 /*InEpilogue=*/false)
1811 .setMIFlag(MachineInstr::FrameSetup);
1812 }
1813
1814 // Mapping for machine moves:
1815 //
1816 // DST: VirtualFP AND
1817 // SRC: VirtualFP => DW_CFA_def_cfa_offset
1818 // ELSE => DW_CFA_def_cfa
1819 //
1820 // SRC: VirtualFP AND
1821 // DST: Register => DW_CFA_def_cfa_register
1822 //
1823 // ELSE
1824 // OFFSET < 0 => DW_CFA_offset_extended_sf
1825 // REG < 64 => DW_CFA_offset + Reg
1826 // ELSE => DW_CFA_offset_extended
1827
1828 uint64_t NumBytes = 0;
1829 int stackGrowth = -SlotSize;
1830
1831 // Find the funclet establisher parameter
1832 MCRegister Establisher;
1833 if (IsClrFunclet)
1834 Establisher = Uses64BitFramePtr ? X86::RCX : X86::ECX;
1835 else if (IsFunclet)
1836 Establisher = Uses64BitFramePtr ? X86::RDX : X86::EDX;
1837
1838 if (IsWin64Prologue && IsFunclet && !IsClrFunclet) {
1839 // Immediately spill establisher into the home slot.
1840 // The runtime cares about this.
1841 // MOV64mr %rdx, 16(%rsp)
1842 unsigned MOVmr = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr;
1843 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(MOVmr)), StackPtr, true, 16)
1844 .addReg(Establisher)
1846 MBB.addLiveIn(Establisher);
1847 }
1848
1849 if (HasFP) {
1850 assert(MF.getRegInfo().isReserved(MachineFramePtr) && "FP reserved");
1851
1852 // Calculate required stack adjustment.
1853 uint64_t FrameSize = StackSize - SlotSize;
1854 NumBytes =
1855 FrameSize - (X86FI->getCalleeSavedFrameSize() + TailCallArgReserveSize);
1856
1857 // Callee-saved registers are pushed on stack before the stack is realigned.
1858 if (TRI->hasStackRealignment(MF) && !IsWin64Prologue)
1859 NumBytes = alignTo(NumBytes, MaxAlign);
1860
1861 // Save EBP/RBP into the appropriate stack slot.
1862 auto EmitSEHPushFramePtr = [&]() {
1863 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
1866 };
1867 EmitSEHBefore(EmitSEHPushFramePtr);
1868 BuildMI(MBB, MBBI, DL,
1870 .addReg(MachineFramePtr, RegState::Kill)
1872 EmitSEHAfter(EmitSEHPushFramePtr);
1873
1874 if (NeedsDwarfCFI && !ArgBaseReg.isValid()) {
1875 // Mark the place where EBP/RBP was saved.
1876 // Define the current CFA rule to use the provided offset.
1877 assert(StackSize);
1878 BuildCFI(MBB, MBBI, DL,
1880 nullptr, -2 * stackGrowth + (int)TailCallArgReserveSize),
1882
1883 // Change the rule for the FramePtr to be an "offset" rule.
1884 unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
1885 BuildCFI(MBB, MBBI, DL,
1886 MCCFIInstruction::createOffset(nullptr, DwarfFramePtr,
1887 2 * stackGrowth -
1888 (int)TailCallArgReserveSize),
1890 }
1891
1892 if (!IsFunclet) {
1893 if (X86FI->hasSwiftAsyncContext()) {
1894 assert(!IsWin64Prologue &&
1895 "win64 prologue does not store async context right below rbp");
1896 const auto &Attrs = MF.getFunction().getAttributes();
1897
1898 // Before we update the live frame pointer we have to ensure there's a
1899 // valid (or null) asynchronous context in its slot just before FP in
1900 // the frame record, so store it now.
1901 auto EmitSEHPushR14 = [&]() {
1902 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
1903 .addImm(X86::R14)
1905 };
1906 EmitSEHBefore(EmitSEHPushR14);
1907 if (Attrs.hasAttrSomewhere(Attribute::SwiftAsync)) {
1908 // We have an initial context in r14, store it just before the frame
1909 // pointer.
1910 MBB.addLiveIn(X86::R14);
1911 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64r))
1912 .addReg(X86::R14)
1914 } else {
1915 // No initial context, store null so that there's no pointer that
1916 // could be misused.
1917 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64i32))
1918 .addImm(0)
1920 }
1921
1922 // Update CFA offset for the async-context push.
1923 if (NeedsDwarfCFI && !ArgBaseReg.isValid()) {
1924 BuildCFI(
1925 MBB, MBBI, DL,
1926 MCCFIInstruction::createAdjustCfaOffset(nullptr, -stackGrowth),
1928 }
1929
1930 EmitSEHAfter(EmitSEHPushR14);
1931
1932 BuildMI(MBB, MBBI, DL, TII.get(X86::LEA64r), FramePtr)
1933 .addUse(X86::RSP)
1934 .addImm(1)
1935 .addUse(X86::NoRegister)
1936 .addImm(8)
1937 .addUse(X86::NoRegister)
1939
1940 // Switch to an FP-relative CFA before adjusting RSP below.
1941 if (NeedsDwarfCFI && !ArgBaseReg.isValid()) {
1942 unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
1943 BuildCFI(MBB, MBBI, DL,
1944 MCCFIInstruction::cfiDefCfa(nullptr, DwarfFramePtr,
1945 -2 * stackGrowth +
1946 (int)TailCallArgReserveSize),
1948 }
1949
1950 BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64ri32), X86::RSP)
1951 .addUse(X86::RSP)
1952 .addImm(8)
1954 }
1955
1956 if (!IsWin64Prologue && !IsFunclet) {
1957 // Update EBP with the new base value.
1958 if (!X86FI->hasSwiftAsyncContext())
1959 BuildMI(MBB, MBBI, DL,
1960 TII.get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr),
1961 FramePtr)
1964
1965 if (NeedsDwarfCFI) {
1966 if (ArgBaseReg.isValid()) {
1967 SmallString<64> CfaExpr;
1968 CfaExpr.push_back(dwarf::DW_CFA_expression);
1969 uint8_t buffer[16];
1970 unsigned DwarfReg = TRI->getDwarfRegNum(MachineFramePtr, true);
1971 CfaExpr.append(buffer, buffer + encodeULEB128(DwarfReg, buffer));
1972 CfaExpr.push_back(2);
1973 CfaExpr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfReg));
1974 CfaExpr.push_back(0);
1975 // DW_CFA_expression: reg5 DW_OP_breg5 +0
1976 BuildCFI(MBB, MBBI, DL,
1977 MCCFIInstruction::createEscape(nullptr, CfaExpr.str()),
1979 } else if (!X86FI->hasSwiftAsyncContext()) {
1980 // Mark effective beginning of when frame pointer becomes valid.
1981 // Define the current CFA to use the EBP/RBP register.
1982 unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
1983 BuildCFI(
1984 MBB, MBBI, DL,
1985 MCCFIInstruction::createDefCfaRegister(nullptr, DwarfFramePtr),
1987 }
1988 }
1989
1990 if (NeedsWinFPO) {
1991 // .cv_fpo_setframe $FramePtr
1992 // NeedsWinFPO is Win32 only, so we're never using Unwind v3, hence it
1993 // is always inserted afterwards.
1994 assert(!IsWin64UnwindV3);
1995 HasWinCFI = true;
1996 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame))
1998 .addImm(0)
2000 }
2001 }
2002 }
2003 } else {
2004 assert(!IsFunclet && "funclets without FPs not yet implemented");
2005 NumBytes =
2006 StackSize - (X86FI->getCalleeSavedFrameSize() + TailCallArgReserveSize);
2007 }
2008
2009 // Update the offset adjustment, which is mainly used by codeview to translate
2010 // from ESP to VFRAME relative local variable offsets.
2011 if (!IsFunclet) {
2012 if (HasFP && TRI->hasStackRealignment(MF))
2013 MFI.setOffsetAdjustment(-NumBytes);
2014 else
2015 MFI.setOffsetAdjustment(-StackSize);
2016 }
2017
2018 // For EH funclets, only allocate enough space for outgoing calls. Save the
2019 // NumBytes value that we would've used for the parent frame.
2020 unsigned ParentFrameNumBytes = NumBytes;
2021 if (IsFunclet)
2022 NumBytes = getWinEHFuncletFrameSize(MF);
2023
2024 // Skip the callee-saved push instructions.
2025 bool PushedRegs = false;
2026 int StackOffset = 2 * stackGrowth;
2028 auto IsCSPush = [&](const MachineBasicBlock::iterator &MBBI) {
2029 if (MBBI == MBB.end() || !MBBI->getFlag(MachineInstr::FrameSetup))
2030 return false;
2031 unsigned Opc = MBBI->getOpcode();
2032 return Opc == X86::PUSH32r || Opc == X86::PUSH64r || Opc == X86::PUSHP64r ||
2033 Opc == X86::PUSH2 || Opc == X86::PUSH2P;
2034 };
2035
2036 while (IsCSPush(MBBI)) {
2037 PushedRegs = true;
2038 Register Reg = MBBI->getOperand(0).getReg();
2039 LastCSPush = MBBI;
2040 unsigned Opc = LastCSPush->getOpcode();
2041 bool IsPush2 = Opc == X86::PUSH2 || Opc == X86::PUSH2P;
2042
2043 // V3: emit SEH pseudo before the real instruction.
2044 EmitSEHBefore([&]() {
2045 if (IsPush2) {
2046 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Push2Regs))
2047 .addImm(Reg)
2048 .addImm(LastCSPush->getOperand(1).getReg())
2050 } else {
2051 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
2052 .addImm(Reg)
2054 }
2055 });
2056 ++MBBI;
2057
2058 if (!HasFP && NeedsDwarfCFI) {
2059 // Mark callee-saved push instruction.
2060 // Define the current CFA rule to use the provided offset.
2061 assert(StackSize);
2062 // Compared to push, push2 introduces more stack offset (one more
2063 // register).
2064 if (IsPush2)
2065 StackOffset += stackGrowth;
2066 BuildCFI(MBB, MBBI, DL,
2069 StackOffset += stackGrowth;
2070 }
2071
2072 // V1/V2: emit SEH pseudo after the real instruction.
2073 EmitSEHAfter([&]() {
2074 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
2075 .addImm(Reg)
2077 if (IsPush2)
2078 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
2079 .addImm(LastCSPush->getOperand(1).getReg())
2081 });
2082 }
2083
2084 // Realign stack after we pushed callee-saved registers (so that we'll be
2085 // able to calculate their offsets from the frame pointer).
2086 // Don't do this for Win64, it needs to realign the stack after the prologue.
2087 if (!IsWin64Prologue && !IsFunclet && TRI->hasStackRealignment(MF) &&
2088 !ArgBaseReg.isValid()) {
2089 assert(HasFP && "There should be a frame pointer if stack is realigned.");
2090 auto EmitSEHStackAlign = [&]() {
2091 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlign))
2092 .addImm(MaxAlign)
2094 };
2095 EmitSEHBefore(EmitSEHStackAlign);
2096 BuildStackAlignAND(MBB, MBBI, DL, StackPtr, MaxAlign);
2097 EmitSEHAfter(EmitSEHStackAlign);
2098 }
2099
2100 // If there is an SUB32ri of ESP immediately before this instruction, merge
2101 // the two. This can be the case when tail call elimination is enabled and
2102 // the callee has more arguments than the caller.
2103 NumBytes = mergeSPUpdates(
2104 MBB, MBBI, [NumBytes](int64_t Offset) { return NumBytes - Offset; },
2105 true);
2106
2107 // Adjust stack pointer: ESP -= numbytes.
2108
2109 // Windows and cygwin/mingw require a prologue helper routine when allocating
2110 // more than 4K bytes on the stack. Windows uses __chkstk and cygwin/mingw
2111 // uses __alloca. __alloca and the 32-bit version of __chkstk will probe the
2112 // stack and adjust the stack pointer in one go. The 64-bit version of
2113 // __chkstk is only responsible for probing the stack. The 64-bit prologue is
2114 // responsible for adjusting the stack pointer. Touching the stack at 4K
2115 // increments is necessary to ensure that the guard pages used by the OS
2116 // virtual memory manager are allocated in correct sequence.
2117 uint64_t AlignedNumBytes = NumBytes;
2118 if (IsWin64Prologue && !IsFunclet && TRI->hasStackRealignment(MF))
2119 AlignedNumBytes = alignTo(AlignedNumBytes, MaxAlign);
2120
2121 auto EmitSEHStackAlloc = [&]() {
2122 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc))
2123 .addImm(NumBytes)
2125 };
2126 if (NumBytes)
2127 EmitSEHBefore(EmitSEHStackAlloc);
2128
2129 if (AlignedNumBytes >= StackProbeSize && EmitStackProbeCall) {
2130 assert(!X86FI->getUsesRedZone() &&
2131 "The Red Zone is not accounted for in stack probes");
2132
2133 // Check whether EAX is livein for this block.
2134 bool isEAXAlive = isEAXLiveIn(MBB);
2135
2136 if (isEAXAlive) {
2137 if (Is64Bit) {
2138 // Save RAX
2139 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64r))
2140 .addReg(X86::RAX, RegState::Kill)
2142 } else {
2143 // Save EAX
2144 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH32r))
2145 .addReg(X86::EAX, RegState::Kill)
2147 }
2148 }
2149
2150 if (Is64Bit) {
2151 // Handle the 64-bit Windows ABI case where we need to call __chkstk.
2152 // Function prologue is responsible for adjusting the stack pointer.
2153 int64_t Alloc = isEAXAlive ? NumBytes - 8 : NumBytes;
2155 X86::RAX)
2156 .addImm(Alloc)
2158 } else {
2159 // Allocate NumBytes-4 bytes on stack in case of isEAXAlive.
2160 // We'll also use 4 already allocated bytes for EAX.
2161 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX)
2162 .addImm(isEAXAlive ? NumBytes - 4 : NumBytes)
2164 }
2165
2166 // Call __chkstk, __chkstk_ms, or __alloca.
2167 emitStackProbe(MF, MBB, MBBI, DL, true);
2168
2169 if (isEAXAlive) {
2170 // Restore RAX/EAX
2172 if (Is64Bit)
2173 MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV64rm), X86::RAX),
2174 StackPtr, false, NumBytes - 8);
2175 else
2176 MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV32rm), X86::EAX),
2177 StackPtr, false, NumBytes - 4);
2178 MI->setFlag(MachineInstr::FrameSetup);
2179 MBB.insert(MBBI, MI);
2180 }
2181 } else if (NumBytes) {
2182 emitSPUpdate(MBB, MBBI, DL, -(int64_t)NumBytes, /*InEpilogue=*/false);
2183 }
2184
2185 if (NumBytes)
2186 EmitSEHAfter(EmitSEHStackAlloc);
2187
2188 int SEHFrameOffset = 0;
2189 Register SPOrEstablisher;
2190 if (IsFunclet) {
2191 if (IsClrFunclet) {
2192 // The establisher parameter passed to a CLR funclet is actually a pointer
2193 // to the (mostly empty) frame of its nearest enclosing funclet; we have
2194 // to find the root function establisher frame by loading the PSPSym from
2195 // the intermediate frame.
2196 unsigned PSPSlotOffset = getPSPSlotOffsetFromSP(MF);
2197 MachinePointerInfo NoInfo;
2198 MBB.addLiveIn(Establisher);
2199 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rm), Establisher),
2200 Establisher, false, PSPSlotOffset)
2203 ;
2204 // Save the root establisher back into the current funclet's (mostly
2205 // empty) frame, in case a sub-funclet or the GC needs it.
2206 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mr)), StackPtr,
2207 false, PSPSlotOffset)
2208 .addReg(Establisher)
2210 NoInfo,
2213 }
2214 SPOrEstablisher = Establisher;
2215 } else {
2216 SPOrEstablisher = StackPtr;
2217 }
2218
2219 if (IsWin64Prologue && HasFP) {
2220 // Set RBP to a small fixed offset from RSP. In the funclet case, we base
2221 // this calculation on the incoming establisher, which holds the value of
2222 // RSP from the parent frame at the end of the prologue.
2223 SEHFrameOffset = calculateSetFPREG(ParentFrameNumBytes);
2224
2225 // If this is not a funclet, emit the CFI describing our frame pointer.
2226 if (NeedsWinCFI && !IsFunclet) {
2227 assert(!NeedsWinFPO && "this setframe incompatible with FPO data");
2228 HasWinCFI = true;
2229 if (isAsynchronousEHPersonality(Personality) || MF.hasEHFunclets()) {
2230 if (TRI->hasBasePointer(MF))
2233 else
2234 MF.getWinEHFuncInfo()->SEHSetFrameOffset = SEHFrameOffset;
2235 }
2236 }
2237
2238 auto EmitSEHSetFrame = [&]() {
2239 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame))
2241 .addImm(SEHFrameOffset)
2243 };
2244
2245 if (!IsFunclet)
2246 EmitSEHBefore(EmitSEHSetFrame);
2247
2248 if (SEHFrameOffset)
2249 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::LEA64r), FramePtr),
2250 SPOrEstablisher, false, SEHFrameOffset);
2251 else
2252 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rr), FramePtr)
2253 .addReg(SPOrEstablisher);
2254
2255 if (!IsFunclet)
2256 EmitSEHAfter(EmitSEHSetFrame);
2257 } else if (IsFunclet && STI.is32Bit()) {
2258 // Reset EBP / ESI to something good for funclets.
2260 // If we're a catch funclet, we can be returned to via catchret. Save ESP
2261 // into the registration node so that the runtime will restore it for us.
2262 if (!MBB.isCleanupFuncletEntry()) {
2263 assert(Personality == EHPersonality::MSVC_CXX);
2264 Register FrameReg;
2266 int64_t EHRegOffset = getFrameIndexReference(MF, FI, FrameReg).getFixed();
2267 // ESP is the first field, so no extra displacement is needed.
2268 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32mr)), FrameReg,
2269 false, EHRegOffset)
2270 .addReg(X86::ESP);
2271 }
2272 }
2273
2274 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup)) {
2275 const MachineInstr &FrameInstr = *MBBI;
2276
2277 if (NeedsWinCFI) {
2278 int FI;
2279 if (Register Reg = TII.isStoreToStackSlot(FrameInstr, FI)) {
2280 if (X86::FR64RegClass.contains(Reg)) {
2281 int Offset;
2282 Register IgnoredFrameReg;
2283 if (IsWin64Prologue && IsFunclet)
2284 Offset = getWin64EHFrameIndexRef(MF, FI, IgnoredFrameReg);
2285 else
2286 Offset =
2287 getFrameIndexReference(MF, FI, IgnoredFrameReg).getFixed() +
2288 SEHFrameOffset;
2289
2290 assert(!NeedsWinFPO && "SEH_SaveXMM incompatible with FPO data");
2291 auto EmitSEHSaveXMM = [&]() {
2292 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SaveXMM))
2293 .addImm(Reg)
2294 .addImm(Offset)
2296 };
2297 EmitSEHBefore(EmitSEHSaveXMM);
2298 ++MBBI;
2299 EmitSEHAfter(EmitSEHSaveXMM);
2300 continue;
2301 }
2302 }
2303 }
2304 ++MBBI;
2305 }
2306
2307 if (NeedsWinCFI && HasWinCFI) {
2308 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_EndPrologue))
2310 }
2311
2312 if (FnHasClrFunclet && !IsFunclet) {
2313 // Save the so-called Initial-SP (i.e. the value of the stack pointer
2314 // immediately after the prolog) into the PSPSlot so that funclets
2315 // and the GC can recover it.
2316 unsigned PSPSlotOffset = getPSPSlotOffsetFromSP(MF);
2317 auto PSPInfo = MachinePointerInfo::getFixedStack(
2319 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mr)), StackPtr, false,
2320 PSPSlotOffset)
2325 }
2326
2327 // Realign stack after we spilled callee-saved registers (so that we'll be
2328 // able to calculate their offsets from the frame pointer).
2329 // Win64 requires aligning the stack after the prologue.
2330 if (IsWin64Prologue && TRI->hasStackRealignment(MF)) {
2331 assert(HasFP && "There should be a frame pointer if stack is realigned.");
2332 BuildStackAlignAND(MBB, MBBI, DL, SPOrEstablisher, MaxAlign);
2333 }
2334
2335 // We already dealt with stack realignment and funclets above.
2336 if (IsFunclet && STI.is32Bit())
2337 return;
2338
2339 // If we need a base pointer, set it up here. It's whatever the value
2340 // of the stack pointer is at this point. Any variable size objects
2341 // will be allocated after this, so we can still use the base pointer
2342 // to reference locals.
2343 if (TRI->hasBasePointer(MF)) {
2344 // Update the base pointer with the current stack pointer.
2345 unsigned Opc = Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr;
2346 BuildMI(MBB, MBBI, DL, TII.get(Opc), BasePtr)
2347 .addReg(SPOrEstablisher)
2349 if (X86FI->getRestoreBasePointer()) {
2350 // Stash value of base pointer. Saving RSP instead of EBP shortens
2351 // dependence chain. Used by SjLj EH.
2352 unsigned Opm = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr;
2353 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opm)), FramePtr, true,
2355 .addReg(SPOrEstablisher)
2357 }
2358
2359 if (X86FI->getHasSEHFramePtrSave() && !IsFunclet) {
2360 // Stash the value of the frame pointer relative to the base pointer for
2361 // Win32 EH. This supports Win32 EH, which does the inverse of the above:
2362 // it recovers the frame pointer from the base pointer rather than the
2363 // other way around.
2364 unsigned Opm = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr;
2365 Register UsedReg;
2366 int Offset =
2367 getFrameIndexReference(MF, X86FI->getSEHFramePtrSaveIndex(), UsedReg)
2368 .getFixed();
2369 assert(UsedReg == BasePtr);
2370 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opm)), UsedReg, true, Offset)
2373 }
2374 }
2375 if (ArgBaseReg.isValid()) {
2376 // Save argument base pointer.
2377 auto *MI = X86FI->getStackPtrSaveMI();
2378 int FI = MI->getOperand(1).getIndex();
2379 unsigned MOVmr = Is64Bit ? X86::MOV64mr : X86::MOV32mr;
2380 // movl %basereg, offset(%ebp)
2381 addFrameReference(BuildMI(MBB, MBBI, DL, TII.get(MOVmr)), FI)
2382 .addReg(ArgBaseReg)
2384 }
2385
2386 if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI) {
2387 // Mark end of stack pointer adjustment.
2388 if (!HasFP && NumBytes) {
2389 // Define the current CFA rule to use the provided offset.
2390 assert(StackSize);
2391 BuildCFI(
2392 MBB, MBBI, DL,
2393 MCCFIInstruction::cfiDefCfaOffset(nullptr, StackSize - stackGrowth),
2395 }
2396
2397 // Emit DWARF info specifying the offsets of the callee-saved registers.
2399 }
2400
2401 // X86 Interrupt handling function cannot assume anything about the direction
2402 // flag (DF in EFLAGS register). Clear this flag by creating "cld" instruction
2403 // in each prologue of interrupt handler function.
2404 //
2405 // Create "cld" instruction only in these cases:
2406 // 1. The interrupt handling function uses any of the "rep" instructions.
2407 // 2. Interrupt handling function calls another function.
2408 // 3. If there are any inline asm blocks, as we do not know what they do
2409 //
2410 // TODO: We should also emit cld if we detect the use of std, but as of now,
2411 // the compiler does not even emit that instruction or even define it, so in
2412 // practice, this would only happen with inline asm, which we cover anyway.
2414 bool NeedsCLD = false;
2415
2416 for (const MachineBasicBlock &B : MF) {
2417 for (const MachineInstr &MI : B) {
2418 if (MI.isCall()) {
2419 NeedsCLD = true;
2420 break;
2421 }
2422
2423 if (isOpcodeRep(MI.getOpcode())) {
2424 NeedsCLD = true;
2425 break;
2426 }
2427
2428 if (MI.isInlineAsm()) {
2429 // TODO: Parse asm for rep instructions or call sites?
2430 // For now, let's play it safe and emit a cld instruction
2431 // just in case.
2432 NeedsCLD = true;
2433 break;
2434 }
2435 }
2436 }
2437
2438 if (NeedsCLD) {
2439 BuildMI(MBB, MBBI, DL, TII.get(X86::CLD))
2441 }
2442 }
2443
2444 // At this point we know if the function has WinCFI or not.
2445 MF.setHasWinCFI(HasWinCFI);
2446}
2447
2449 const MachineFunction &MF) const {
2450 // We can't use LEA instructions for adjusting the stack pointer if we don't
2451 // have a frame pointer in the Win64 ABI. Only ADD instructions may be used
2452 // to deallocate the stack.
2453 // This means that we can use LEA for SP in two situations:
2454 // 1. We *aren't* using the Win64 ABI which means we are free to use LEA.
2455 // 2. We *have* a frame pointer which means we are permitted to use LEA.
2456 return !MF.getTarget().getMCAsmInfo().usesWindowsCFI() || hasFP(MF);
2457}
2458
2460 switch (MI.getOpcode()) {
2461 case X86::CATCHRET:
2462 case X86::CLEANUPRET:
2463 return true;
2464 default:
2465 return false;
2466 }
2467 llvm_unreachable("impossible");
2468}
2469
2470// CLR funclets use a special "Previous Stack Pointer Symbol" slot on the
2471// stack. It holds a pointer to the bottom of the root function frame. The
2472// establisher frame pointer passed to a nested funclet may point to the
2473// (mostly empty) frame of its parent funclet, but it will need to find
2474// the frame of the root function to access locals. To facilitate this,
2475// every funclet copies the pointer to the bottom of the root function
2476// frame into a PSPSym slot in its own (mostly empty) stack frame. Using the
2477// same offset for the PSPSym in the root function frame that's used in the
2478// funclets' frames allows each funclet to dynamically accept any ancestor
2479// frame as its establisher argument (the runtime doesn't guarantee the
2480// immediate parent for some reason lost to history), and also allows the GC,
2481// which uses the PSPSym for some bookkeeping, to find it in any funclet's
2482// frame with only a single offset reported for the entire method.
2483unsigned
2484X86FrameLowering::getPSPSlotOffsetFromSP(const MachineFunction &MF) const {
2485 const WinEHFuncInfo &Info = *MF.getWinEHFuncInfo();
2487 int Offset = getFrameIndexReferencePreferSP(MF, Info.PSPSymFrameIdx, SPReg,
2488 /*IgnoreSPUpdates*/ true)
2489 .getFixed();
2490 assert(Offset >= 0 && SPReg == TRI->getStackRegister());
2491 return static_cast<unsigned>(Offset);
2492}
2493
2494unsigned
2495X86FrameLowering::getWinEHFuncletFrameSize(const MachineFunction &MF) const {
2496 const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
2497 // This is the size of the pushed CSRs.
2498 unsigned CSSize = X86FI->getCalleeSavedFrameSize();
2499 // This is the size of callee saved XMMs.
2500 const auto &WinEHXMMSlotInfo = X86FI->getWinEHXMMSlotInfo();
2501 unsigned XMMSize =
2502 WinEHXMMSlotInfo.size() * TRI->getSpillSize(X86::VR128RegClass);
2503 // This is the amount of stack a funclet needs to allocate.
2504 unsigned UsedSize;
2505 EHPersonality Personality =
2507 if (Personality == EHPersonality::CoreCLR) {
2508 // CLR funclets need to hold enough space to include the PSPSym, at the
2509 // same offset from the stack pointer (immediately after the prolog) as it
2510 // resides at in the main function.
2511 UsedSize = getPSPSlotOffsetFromSP(MF) + SlotSize;
2512 } else {
2513 // Other funclets just need enough stack for outgoing call arguments.
2514 UsedSize = MF.getFrameInfo().getMaxCallFrameSize();
2515 }
2516 // RBP is not included in the callee saved register block. After pushing RBP,
2517 // everything is 16 byte aligned. Everything we allocate before an outgoing
2518 // call must also be 16 byte aligned.
2519 unsigned FrameSizeMinusRBP = alignTo(CSSize + UsedSize, getStackAlign());
2520 // Subtract out the size of the callee saved registers. This is how much stack
2521 // each funclet will allocate.
2522 return FrameSizeMinusRBP + XMMSize - CSSize;
2523}
2524
2525static bool isTailCallOpcode(unsigned Opc) {
2526 return Opc == X86::TCRETURNri || Opc == X86::TCRETURN_WIN64ri ||
2527 Opc == X86::TCRETURN_HIPE32ri || Opc == X86::TCRETURNdi ||
2528 Opc == X86::TCRETURNmi || Opc == X86::TCRETURNri64 ||
2529 Opc == X86::TCRETURNri64_ImpCall || Opc == X86::TCRETURNdi64 ||
2530 Opc == X86::TCRETURNmi64 || Opc == X86::TCRETURN_WINmi64;
2531}
2532
2534 MachineBasicBlock &MBB) const {
2535 const MachineFrameInfo &MFI = MF.getFrameInfo();
2537 MachineBasicBlock::iterator Terminator = MBB.getFirstTerminator();
2538 MachineBasicBlock::iterator MBBI = Terminator;
2539 DebugLoc DL;
2540 if (MBBI != MBB.end())
2541 DL = MBBI->getDebugLoc();
2542 // standard x86_64 uses 64-bit frame/stack pointers, x32 - 32-bit.
2543 const bool Is64BitILP32 = STI.isTarget64BitILP32();
2544 Register FramePtr = TRI->getFrameRegister(MF);
2545 Register MachineFramePtr =
2546 Is64BitILP32 ? Register(getX86SubSuperRegister(FramePtr, 64)) : FramePtr;
2547
2548 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo().usesWindowsCFI();
2549 bool NeedsWin64CFI =
2550 IsWin64Prologue && MF.getFunction().needsUnwindTableEntry();
2551 // For V3 unwind, epilog SEH pseudos are emitted inline before each
2552 // unwind-effecting instruction.
2553 bool IsWin64UnwindV3 =
2554 NeedsWin64CFI && MF.hasWinCFI() && requireWinX64UnwindV3(MF);
2555 bool IsFunclet = MBBI == MBB.end() ? false : isFuncletReturnInstr(*MBBI);
2556
2557 // Get the number of bytes to allocate from the FrameInfo.
2558 uint64_t StackSize = MFI.getStackSize();
2559 uint64_t MaxAlign = calculateMaxStackAlign(MF);
2560 unsigned CSSize = X86FI->getCalleeSavedFrameSize();
2561 unsigned TailCallArgReserveSize = -X86FI->getTCReturnAddrDelta();
2562 bool HasFP = hasFP(MF);
2563 uint64_t NumBytes = 0;
2564
2565 bool NeedsDwarfCFI = (!MF.getTarget().getTargetTriple().isOSDarwin() &&
2567 !MF.getTarget().getTargetTriple().isUEFI()) &&
2568 MF.needsFrameMoves();
2569
2570 Register ArgBaseReg;
2571 if (auto *MI = X86FI->getStackPtrSaveMI()) {
2572 unsigned Opc = X86::LEA32r;
2573 Register StackReg = X86::ESP;
2574 ArgBaseReg = MI->getOperand(0).getReg();
2575 if (STI.is64Bit()) {
2576 Opc = X86::LEA64r;
2577 StackReg = X86::RSP;
2578 }
2579 // leal -4(%basereg), %esp
2580 // .cfi_def_cfa %esp, 4
2581 BuildMI(MBB, MBBI, DL, TII.get(Opc), StackReg)
2582 .addUse(ArgBaseReg)
2583 .addImm(1)
2584 .addUse(X86::NoRegister)
2585 .addImm(-(int64_t)SlotSize)
2586 .addUse(X86::NoRegister)
2588 if (NeedsDwarfCFI) {
2589 unsigned DwarfStackPtr = TRI->getDwarfRegNum(StackReg, true);
2590 BuildCFI(MBB, MBBI, DL,
2591 MCCFIInstruction::cfiDefCfa(nullptr, DwarfStackPtr, SlotSize),
2593 --MBBI;
2594 }
2595 --MBBI;
2596 }
2597
2598 if (IsFunclet) {
2599 assert(HasFP && "EH funclets without FP not yet implemented");
2600 NumBytes = getWinEHFuncletFrameSize(MF);
2601 } else if (HasFP) {
2602 // Calculate required stack adjustment.
2603 uint64_t FrameSize = StackSize - SlotSize;
2604 NumBytes = FrameSize - CSSize - TailCallArgReserveSize;
2605
2606 // Callee-saved registers were pushed on stack before the stack was
2607 // realigned.
2608 if (TRI->hasStackRealignment(MF) && !IsWin64Prologue)
2609 NumBytes = alignTo(FrameSize, MaxAlign);
2610 } else {
2611 NumBytes = StackSize - CSSize - TailCallArgReserveSize;
2612 }
2613 uint64_t SEHStackAllocAmt = NumBytes;
2614
2615 unsigned SEHFrameOffset = 0;
2616 if (IsWin64Prologue && HasFP)
2617 SEHFrameOffset = calculateSetFPREG(SEHStackAllocAmt);
2618
2619 // AfterPop is the position to insert .cfi_restore.
2621 if (HasFP) {
2622 if (X86FI->hasSwiftAsyncContext()) {
2623 // Discard the context.
2624 int64_t Offset = mergeSPAdd(MBB, MBBI, 16, true);
2625 emitSPUpdate(MBB, MBBI, DL, Offset, /*InEpilogue*/ true);
2626 }
2627 // Pop EBP.
2628 if (IsWin64UnwindV3)
2629 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg))
2632 BuildMI(MBB, MBBI, DL,
2634 MachineFramePtr)
2636
2637 // We need to reset FP to its untagged state on return. Bit 60 is currently
2638 // used to show the presence of an extended frame.
2639 if (X86FI->hasSwiftAsyncContext()) {
2640 BuildMI(MBB, MBBI, DL, TII.get(X86::BTR64ri8), MachineFramePtr)
2641 .addUse(MachineFramePtr)
2642 .addImm(60)
2644 }
2645
2646 if (NeedsDwarfCFI) {
2647 if (!ArgBaseReg.isValid()) {
2648 unsigned DwarfStackPtr =
2649 TRI->getDwarfRegNum(Is64Bit ? X86::RSP : X86::ESP, true);
2650 BuildCFI(MBB, MBBI, DL,
2651 MCCFIInstruction::cfiDefCfa(nullptr, DwarfStackPtr, SlotSize),
2653 }
2654 if (!MBB.succ_empty() && !MBB.isReturnBlock()) {
2655 unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true);
2656 BuildCFI(MBB, AfterPop, DL,
2657 MCCFIInstruction::createRestore(nullptr, DwarfFramePtr),
2659 --MBBI;
2660 --AfterPop;
2661 }
2662 --MBBI;
2663 }
2664 }
2665
2666 MachineBasicBlock::iterator FirstCSPop = MBBI;
2667 // Skip the callee-saved pop instructions.
2668 while (MBBI != MBB.begin()) {
2669 MachineBasicBlock::iterator PI = std::prev(MBBI);
2670 unsigned Opc = PI->getOpcode();
2671
2672 if (Opc != X86::DBG_VALUE && !PI->isTerminator()) {
2673 if (!PI->getFlag(MachineInstr::FrameDestroy) ||
2674 (Opc != X86::POP32r && Opc != X86::POP64r && Opc != X86::BTR64ri8 &&
2675 Opc != X86::ADD64ri32 && Opc != X86::POPP64r && Opc != X86::POP2 &&
2676 Opc != X86::POP2P && Opc != X86::LEA64r && Opc != X86::SEH_PushReg &&
2677 Opc != X86::SEH_Push2Regs && Opc != X86::SEH_StackAlloc &&
2678 Opc != X86::ADD64ri32_NF))
2679 break;
2680 FirstCSPop = PI;
2681 }
2682
2683 --MBBI;
2684 }
2685 if (ArgBaseReg.isValid()) {
2686 // Restore argument base pointer.
2687 auto *MI = X86FI->getStackPtrSaveMI();
2688 int FI = MI->getOperand(1).getIndex();
2689 unsigned MOVrm = Is64Bit ? X86::MOV64rm : X86::MOV32rm;
2690 // movl offset(%ebp), %basereg
2691 addFrameReference(BuildMI(MBB, MBBI, DL, TII.get(MOVrm), ArgBaseReg), FI)
2693 }
2694 MBBI = FirstCSPop;
2695
2696 if (IsFunclet && Terminator->getOpcode() == X86::CATCHRET)
2697 emitCatchRetReturnValue(MBB, FirstCSPop, &*Terminator);
2698
2699 if (MBBI != MBB.end())
2700 DL = MBBI->getDebugLoc();
2701 // If there is an ADD32ri or SUB32ri of ESP immediately before this
2702 // instruction, merge the two instructions.
2703 if (NumBytes || MFI.hasVarSizedObjects())
2704 NumBytes = mergeSPAdd(MBB, MBBI, NumBytes, true);
2705
2706 if (IsWin64UnwindV3 && NeedsWin64CFI && MF.hasWinCFI()) {
2707 // Find the XMM restores that were tagged with FrameDestroy, now that we
2708 // know the offset we can emit the SEH pseudos for them.
2709 auto EpilogStart = MBBI;
2710 {
2711 auto ScanIt = MBBI;
2712 while (ScanIt != MBB.begin()) {
2713 auto PI = std::prev(ScanIt);
2714 int FI;
2715 if (PI->getFlag(MachineInstr::FrameDestroy) &&
2716 TII.isLoadFromStackSlot(*PI, FI)) {
2717 Register Reg = PI->getOperand(0).getReg();
2718 if (X86::FR64RegClass.contains(Reg)) {
2719 Register IgnoredFrameReg;
2720 int Offset =
2721 getFrameIndexReference(MF, FI, IgnoredFrameReg).getFixed() +
2722 SEHFrameOffset;
2723 BuildMI(MBB, PI, DL, TII.get(X86::SEH_SaveXMM))
2724 .addImm(Reg)
2725 .addImm(Offset)
2727 // std::prev(PI) is the SEH_SaveXMM we just inserted (before PI).
2728 // We start ScanIt from that point so that the next
2729 // std::prev(ScanIt) will examine the instruction before the pseudo,
2730 // i.e. the next potential XMM restore further up the block.
2731 EpilogStart = std::prev(PI);
2732 ScanIt = EpilogStart;
2733 continue;
2734 }
2735 }
2736 break;
2737 }
2738 }
2739
2740 // For V3, SEH_BeginEpilogue must be emitted before any epilog SEH pseudos.
2741 BuildMI(MBB, EpilogStart, DL, TII.get(X86::SEH_BeginEpilogue));
2742 }
2743
2744 // If dynamic alloca is used, then reset esp to point to the last callee-saved
2745 // slot before popping them off! Same applies for the case, when stack was
2746 // realigned. Don't do this if this was a funclet epilogue, since the funclets
2747 // will not do realignment or dynamic stack allocation.
2748 if (((TRI->hasStackRealignment(MF)) || MFI.hasVarSizedObjects()) &&
2749 !IsFunclet) {
2750 if (TRI->hasStackRealignment(MF))
2751 MBBI = FirstCSPop;
2752 uint64_t LEAAmount =
2753 IsWin64Prologue ? SEHStackAllocAmt - SEHFrameOffset : -CSSize;
2754
2755 if (X86FI->hasSwiftAsyncContext())
2756 LEAAmount -= 16;
2757
2758 // There are only two legal forms of epilogue:
2759 // - add SEHAllocationSize, %rsp
2760 // - lea SEHAllocationSize(%FramePtr), %rsp
2761 //
2762 // 'mov %FramePtr, %rsp' will not be recognized as an epilogue sequence.
2763 // However, we may use this sequence if we have a frame pointer because the
2764 // effects of the prologue can safely be undone.
2765 if (IsWin64UnwindV3) {
2766 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame))
2768 .addImm(SEHFrameOffset)
2770 if (SEHStackAllocAmt)
2771 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc))
2772 .addImm(SEHStackAllocAmt)
2774 }
2775 if (LEAAmount != 0) {
2778 false, LEAAmount);
2779 --MBBI;
2780 } else {
2781 unsigned Opc = (Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr);
2783 --MBBI;
2784 }
2785 } else if (NumBytes) {
2786 // Adjust stack pointer back: ESP += numbytes.
2787 if (IsWin64UnwindV3)
2788 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc))
2789 .addImm(NumBytes)
2791 emitSPUpdate(MBB, MBBI, DL, NumBytes, /*InEpilogue=*/true);
2792 if (!HasFP && NeedsDwarfCFI) {
2793 // Define the current CFA rule to use the provided offset.
2794 BuildCFI(MBB, MBBI, DL,
2796 nullptr, CSSize + TailCallArgReserveSize + SlotSize),
2798 }
2799 --MBBI;
2800 }
2801
2802 // For V1/V2, emit SEH_BeginEpilogue after stack restore code.
2803 if (!IsWin64UnwindV3 && NeedsWin64CFI && MF.hasWinCFI())
2804 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_BeginEpilogue));
2805
2806 if (!HasFP && NeedsDwarfCFI) {
2807 MBBI = FirstCSPop;
2808 int64_t Offset = -(int64_t)CSSize - SlotSize;
2809 // Mark callee-saved pop instruction.
2810 // Define the current CFA rule to use the provided offset.
2811 while (MBBI != MBB.end()) {
2813 unsigned Opc = PI->getOpcode();
2814 ++MBBI;
2815 if (Opc == X86::POP32r || Opc == X86::POP64r || Opc == X86::POPP64r ||
2816 Opc == X86::POP2 || Opc == X86::POP2P) {
2817 Offset += SlotSize;
2818 // Compared to pop, pop2 introduces more stack offset (one more
2819 // register).
2820 if (Opc == X86::POP2 || Opc == X86::POP2P)
2821 Offset += SlotSize;
2822 BuildCFI(MBB, MBBI, DL,
2825 }
2826 }
2827 }
2828
2829 // Emit DWARF info specifying the restores of the callee-saved registers.
2830 // For epilogue with return inside or being other block without successor,
2831 // no need to generate .cfi_restore for callee-saved registers.
2832 if (NeedsDwarfCFI && !MBB.succ_empty())
2833 emitCalleeSavedFrameMoves(MBB, AfterPop, DL, false);
2834
2835 if (Terminator == MBB.end() || !isTailCallOpcode(Terminator->getOpcode())) {
2836 // Add the return addr area delta back since we are not tail calling.
2837 int64_t Delta = X86FI->getTCReturnAddrDelta();
2838 assert(Delta <= 0 && "TCDelta should never be positive");
2839 if (Delta) {
2840 // Check for possible merge with preceding ADD instruction.
2841 int64_t Offset = mergeSPAdd(MBB, Terminator, -Delta, true);
2842 emitSPUpdate(MBB, Terminator, DL, Offset, /*InEpilogue=*/true);
2843 }
2844 }
2845
2846 // Emit tilerelease for AMX kernel.
2848 BuildMI(MBB, Terminator, DL, TII.get(X86::TILERELEASE));
2849
2850 if (NeedsWin64CFI && MF.hasWinCFI())
2851 BuildMI(MBB, Terminator, DL, TII.get(X86::SEH_EndEpilogue));
2852}
2853
2855 int FI,
2856 Register &FrameReg) const {
2857 const MachineFrameInfo &MFI = MF.getFrameInfo();
2858
2859 bool IsFixed = MFI.isFixedObjectIndex(FI);
2860 // We can't calculate offset from frame pointer if the stack is realigned,
2861 // so enforce usage of stack/base pointer. The base pointer is used when we
2862 // have dynamic allocas in addition to dynamic realignment.
2863 if (TRI->hasBasePointer(MF))
2864 FrameReg = IsFixed ? TRI->getFramePtr() : TRI->getBaseRegister();
2865 else if (TRI->hasStackRealignment(MF))
2866 FrameReg = IsFixed ? TRI->getFramePtr() : TRI->getStackRegister();
2867 else
2868 FrameReg = TRI->getFrameRegister(MF);
2869
2870 // Offset will hold the offset from the stack pointer at function entry to the
2871 // object.
2872 // We need to factor in additional offsets applied during the prologue to the
2873 // frame, base, and stack pointer depending on which is used.
2874 int64_t Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea();
2876 unsigned CSSize = X86FI->getCalleeSavedFrameSize();
2877 uint64_t StackSize = MFI.getStackSize();
2878 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo().usesWindowsCFI();
2879 int64_t FPDelta = 0;
2880
2881 // In an x86 interrupt, remove the offset we added to account for the return
2882 // address from any stack object allocated in the caller's frame. Interrupts
2883 // do not have a standard return address. Fixed objects in the current frame,
2884 // such as SSE register spills, should not get this treatment.
2886 Offset >= 0) {
2888 }
2889
2890 if (IsWin64Prologue) {
2891 assert(!MFI.hasCalls() || (StackSize % 16) == 8);
2892
2893 // Calculate required stack adjustment.
2894 uint64_t FrameSize = StackSize - SlotSize;
2895 // If required, include space for extra hidden slot for stashing base
2896 // pointer.
2897 if (X86FI->getRestoreBasePointer())
2898 FrameSize += SlotSize;
2899 uint64_t NumBytes = FrameSize - CSSize;
2900
2901 uint64_t SEHFrameOffset = calculateSetFPREG(NumBytes);
2902 if (FI && FI == X86FI->getFAIndex())
2903 return StackOffset::getFixed(-SEHFrameOffset);
2904
2905 // FPDelta is the offset from the "traditional" FP location of the old base
2906 // pointer followed by return address and the location required by the
2907 // restricted Win64 prologue.
2908 // Add FPDelta to all offsets below that go through the frame pointer.
2909 FPDelta = FrameSize - SEHFrameOffset;
2910 assert((!MFI.hasCalls() || (FPDelta % 16) == 0) &&
2911 "FPDelta isn't aligned per the Win64 ABI!");
2912 }
2913
2914 if (FrameReg == TRI->getFramePtr()) {
2915 // Skip saved EBP/RBP
2916 Offset += SlotSize;
2917
2918 // Account for restricted Windows prologue.
2919 Offset += FPDelta;
2920
2921 // Skip the RETADDR move area
2922 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
2923 if (TailCallReturnAddrDelta < 0)
2924 Offset -= TailCallReturnAddrDelta;
2925
2927 }
2928
2929 // FrameReg is either the stack pointer or a base pointer. But the base is
2930 // located at the end of the statically known StackSize so the distinction
2931 // doesn't really matter.
2932 if (TRI->hasStackRealignment(MF) || TRI->hasBasePointer(MF))
2933 assert(isAligned(MFI.getObjectAlign(FI), -(Offset + StackSize)));
2934 return StackOffset::getFixed(Offset + StackSize);
2935}
2936
2938 Register &FrameReg) const {
2939 const MachineFrameInfo &MFI = MF.getFrameInfo();
2941 const auto &WinEHXMMSlotInfo = X86FI->getWinEHXMMSlotInfo();
2942 const auto it = WinEHXMMSlotInfo.find(FI);
2943
2944 if (it == WinEHXMMSlotInfo.end())
2945 return getFrameIndexReference(MF, FI, FrameReg).getFixed();
2946
2947 FrameReg = TRI->getStackRegister();
2948 return alignDown(MFI.getMaxCallFrameSize(), getStackAlign().value()) +
2949 it->second;
2950}
2951
2954 Register &FrameReg,
2955 int Adjustment) const {
2956 const MachineFrameInfo &MFI = MF.getFrameInfo();
2957 FrameReg = TRI->getStackRegister();
2958 return StackOffset::getFixed(MFI.getObjectOffset(FI) -
2959 getOffsetOfLocalArea() + Adjustment);
2960}
2961
2964 int FI, Register &FrameReg,
2965 bool IgnoreSPUpdates) const {
2966
2967 const MachineFrameInfo &MFI = MF.getFrameInfo();
2968 // Does not include any dynamic realign.
2969 const uint64_t StackSize = MFI.getStackSize();
2970 // LLVM arranges the stack as follows:
2971 // ...
2972 // ARG2
2973 // ARG1
2974 // RETADDR
2975 // PUSH RBP <-- RBP points here
2976 // PUSH CSRs
2977 // ~~~~~~~ <-- possible stack realignment (non-win64)
2978 // ...
2979 // STACK OBJECTS
2980 // ... <-- RSP after prologue points here
2981 // ~~~~~~~ <-- possible stack realignment (win64)
2982 //
2983 // if (hasVarSizedObjects()):
2984 // ... <-- "base pointer" (ESI/RBX) points here
2985 // DYNAMIC ALLOCAS
2986 // ... <-- RSP points here
2987 //
2988 // Case 1: In the simple case of no stack realignment and no dynamic
2989 // allocas, both "fixed" stack objects (arguments and CSRs) are addressable
2990 // with fixed offsets from RSP.
2991 //
2992 // Case 2: In the case of stack realignment with no dynamic allocas, fixed
2993 // stack objects are addressed with RBP and regular stack objects with RSP.
2994 //
2995 // Case 3: In the case of dynamic allocas and stack realignment, RSP is used
2996 // to address stack arguments for outgoing calls and nothing else. The "base
2997 // pointer" points to local variables, and RBP points to fixed objects.
2998 //
2999 // In cases 2 and 3, we can only answer for non-fixed stack objects, and the
3000 // answer we give is relative to the SP after the prologue, and not the
3001 // SP in the middle of the function.
3002
3003 if (MFI.isFixedObjectIndex(FI) && TRI->hasStackRealignment(MF) &&
3004 !STI.isTargetWin64())
3005 return getFrameIndexReference(MF, FI, FrameReg);
3006
3007 // If !hasReservedCallFrame the function might have SP adjustement in the
3008 // body. So, even though the offset is statically known, it depends on where
3009 // we are in the function.
3010 if (!IgnoreSPUpdates && !hasReservedCallFrame(MF))
3011 return getFrameIndexReference(MF, FI, FrameReg);
3012
3013 // We don't handle tail calls, and shouldn't be seeing them either.
3015 "we don't handle this case!");
3016
3017 // This is how the math works out:
3018 //
3019 // %rsp grows (i.e. gets lower) left to right. Each box below is
3020 // one word (eight bytes). Obj0 is the stack slot we're trying to
3021 // get to.
3022 //
3023 // ----------------------------------
3024 // | BP | Obj0 | Obj1 | ... | ObjN |
3025 // ----------------------------------
3026 // ^ ^ ^ ^
3027 // A B C E
3028 //
3029 // A is the incoming stack pointer.
3030 // (B - A) is the local area offset (-8 for x86-64) [1]
3031 // (C - A) is the Offset returned by MFI.getObjectOffset for Obj0 [2]
3032 //
3033 // |(E - B)| is the StackSize (absolute value, positive). For a
3034 // stack that grown down, this works out to be (B - E). [3]
3035 //
3036 // E is also the value of %rsp after stack has been set up, and we
3037 // want (C - E) -- the value we can add to %rsp to get to Obj0. Now
3038 // (C - E) == (C - A) - (B - A) + (B - E)
3039 // { Using [1], [2] and [3] above }
3040 // == getObjectOffset - LocalAreaOffset + StackSize
3041
3042 return getFrameIndexReferenceSP(MF, FI, FrameReg, StackSize);
3043}
3044
3047 std::vector<CalleeSavedInfo> &CSI) const {
3048 MachineFrameInfo &MFI = MF.getFrameInfo();
3050
3051 unsigned CalleeSavedFrameSize = 0;
3052 unsigned XMMCalleeSavedFrameSize = 0;
3053 auto &WinEHXMMSlotInfo = X86FI->getWinEHXMMSlotInfo();
3054 int SpillSlotOffset = getOffsetOfLocalArea() + X86FI->getTCReturnAddrDelta();
3055
3056 int64_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
3057
3058 if (TailCallReturnAddrDelta < 0) {
3059 // create RETURNADDR area
3060 // arg
3061 // arg
3062 // RETADDR
3063 // { ...
3064 // RETADDR area
3065 // ...
3066 // }
3067 // [EBP]
3068 MFI.CreateFixedObject(-TailCallReturnAddrDelta,
3069 TailCallReturnAddrDelta - SlotSize, true);
3070 }
3071
3072 // Spill the BasePtr if it's used.
3073 if (this->TRI->hasBasePointer(MF)) {
3074 // Allocate a spill slot for EBP if we have a base pointer and EH funclets.
3075 if (MF.hasEHFunclets()) {
3077 X86FI->setHasSEHFramePtrSave(true);
3078 X86FI->setSEHFramePtrSaveIndex(FI);
3079 }
3080 }
3081
3082 bool IsFPRemovedFromCSI = false;
3083 if (hasFP(MF)) {
3084 // emitPrologue always spills frame register the first thing.
3085 SpillSlotOffset -= SlotSize;
3086 MFI.CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
3087
3088 // The async context lives directly before the frame pointer, and we
3089 // allocate a second slot to preserve stack alignment.
3090 if (X86FI->hasSwiftAsyncContext()) {
3091 SpillSlotOffset -= SlotSize;
3092 MFI.CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
3093 SpillSlotOffset -= SlotSize;
3094 }
3095
3096 // Since emitPrologue and emitEpilogue will handle spilling and restoring of
3097 // the frame register, we can delete it from CSI list and not have to worry
3098 // about avoiding it later.
3099 Register FPReg = TRI->getFrameRegister(MF);
3100 for (unsigned i = 0; i < CSI.size(); ++i) {
3101 if (TRI->regsOverlap(CSI[i].getReg(), FPReg)) {
3102 CSI.erase(CSI.begin() + i);
3103 IsFPRemovedFromCSI = true;
3104 break;
3105 }
3106 }
3107 }
3108
3109 // Strategy:
3110 // 1. Use push2 when
3111 // a) number of CSR > 1 if no need padding
3112 // b) number of CSR > 2 if need padding
3113 // c) stack alignment >= 16 bytes
3114 // 2. When the number of CSR push is odd
3115 // a. Start to use push2 from the 1st push if stack is 16B aligned.
3116 // b. Start to use push2 from the 2nd push if stack is not 16B aligned.
3117 // 3. When the number of CSR push is even, start to use push2 from the 1st
3118 // push and make the stack 16B aligned before the push
3119 unsigned NumRegsForPush2 = 0;
3120 if (STI.hasPush2Pop2() && getStackAlignment() >= 16) {
3121 unsigned NumCSGPR = llvm::count_if(CSI, [](const CalleeSavedInfo &I) {
3122 return X86::GR64RegClass.contains(I.getReg());
3123 });
3124 bool UsePush2Pop2 = !IsFPRemovedFromCSI ? NumCSGPR > 2 : NumCSGPR > 1;
3125 NumRegsForPush2 =
3126 UsePush2Pop2
3127 ? alignDown(IsFPRemovedFromCSI ? NumCSGPR : NumCSGPR - 1, 2)
3128 : 0;
3129 }
3130
3131 // Assign slots for GPRs. It increases frame size.
3132 for (CalleeSavedInfo &I : llvm::reverse(CSI)) {
3133 MCRegister Reg = I.getReg();
3134
3135 if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg))
3136 continue;
3137
3138 // A CSR is a candidate for push2/pop2 when it's slot offset is 16B aligned
3139 // or only an odd number of registers in the candidates.
3140 if (X86FI->getNumCandidatesForPush2Pop2() < NumRegsForPush2 &&
3141 (SpillSlotOffset % 16 == 0 ||
3142 X86FI->getNumCandidatesForPush2Pop2() % 2))
3143 X86FI->addCandidateForPush2Pop2(Reg);
3144
3145 SpillSlotOffset -= SlotSize;
3146 CalleeSavedFrameSize += SlotSize;
3147
3148 int SlotIndex = MFI.CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
3149 I.setFrameIdx(SlotIndex);
3150 }
3151
3152 // Adjust the offset of spill slot as we know the accurate callee saved frame
3153 // size.
3154 if (X86FI->getRestoreBasePointer()) {
3155 SpillSlotOffset -= SlotSize;
3156 CalleeSavedFrameSize += SlotSize;
3157
3158 MFI.CreateFixedSpillStackObject(SlotSize, SpillSlotOffset);
3159 // TODO: saving the slot index is better?
3160 X86FI->setRestoreBasePointer(CalleeSavedFrameSize);
3161 }
3162 assert(X86FI->getNumCandidatesForPush2Pop2() % 2 == 0 &&
3163 "Expect even candidates for push2/pop2");
3164 if (X86FI->getNumCandidatesForPush2Pop2())
3165 ++NumFunctionUsingPush2Pop2;
3166 X86FI->setCalleeSavedFrameSize(CalleeSavedFrameSize);
3167 MFI.setCVBytesOfCalleeSavedRegisters(CalleeSavedFrameSize);
3168
3169 // Assign slots for XMMs.
3170 for (CalleeSavedInfo &I : llvm::reverse(CSI)) {
3171 MCRegister Reg = I.getReg();
3172 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg))
3173 continue;
3174
3176 unsigned Size = TRI->getSpillSize(*RC);
3177 Align Alignment = TRI->getSpillAlign(*RC);
3178 // ensure alignment
3179 assert(SpillSlotOffset < 0 && "SpillSlotOffset should always < 0 on X86");
3180 SpillSlotOffset = -alignTo(-SpillSlotOffset, Alignment);
3181
3182 // spill into slot
3183 SpillSlotOffset -= Size;
3184 int SlotIndex = MFI.CreateFixedSpillStackObject(Size, SpillSlotOffset);
3185 I.setFrameIdx(SlotIndex);
3186 MFI.ensureMaxAlignment(Alignment);
3187
3188 // Save the start offset and size of XMM in stack frame for funclets.
3189 if (X86::VR128RegClass.contains(Reg)) {
3190 WinEHXMMSlotInfo[SlotIndex] = XMMCalleeSavedFrameSize;
3191 XMMCalleeSavedFrameSize += Size;
3192 }
3193 }
3194
3195 return true;
3196}
3197
3201 DebugLoc DL = MBB.findDebugLoc(MI);
3202
3203 // Don't save CSRs in 32-bit EH funclets. The caller saves EBX, EBP, ESI, EDI
3204 // for us, and there are no XMM CSRs on Win32.
3205 if (MBB.isEHFuncletEntry() && STI.is32Bit() && STI.isOSWindows())
3206 return true;
3207
3208 // Push GPRs. It increases frame size.
3209 const MachineFunction &MF = *MBB.getParent();
3211
3212 // Update LiveIn of the basic block and decide whether we can add a kill flag
3213 // to the use.
3214 auto UpdateLiveInCheckCanKill = [&](Register Reg) {
3215 const MachineRegisterInfo &MRI = MF.getRegInfo();
3216 // Do not set a kill flag on values that are also marked as live-in. This
3217 // happens with the @llvm-returnaddress intrinsic and with arguments
3218 // passed in callee saved registers.
3219 // Omitting the kill flags is conservatively correct even if the live-in
3220 // is not used after all.
3221 if (MRI.isLiveIn(Reg))
3222 return false;
3223 MBB.addLiveIn(Reg);
3224 // Check if any subregister is live-in
3225 for (MCRegAliasIterator AReg(Reg, TRI, false); AReg.isValid(); ++AReg)
3226 if (MRI.isLiveIn(*AReg))
3227 return false;
3228 return true;
3229 };
3230 auto UpdateLiveInGetKillRegState = [&](Register Reg) {
3231 return getKillRegState(UpdateLiveInCheckCanKill(Reg));
3232 };
3233
3234 for (auto RI = CSI.rbegin(), RE = CSI.rend(); RI != RE; ++RI) {
3235 MCRegister Reg = RI->getReg();
3236 if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg))
3237 continue;
3238
3239 if (X86FI->isCandidateForPush2Pop2(Reg)) {
3240 MCRegister Reg2 = (++RI)->getReg();
3242 .addReg(Reg, UpdateLiveInGetKillRegState(Reg))
3243 .addReg(Reg2, UpdateLiveInGetKillRegState(Reg2))
3245 } else {
3246 BuildMI(MBB, MI, DL, TII.get(getPUSHOpcode(STI)))
3247 .addReg(Reg, UpdateLiveInGetKillRegState(Reg))
3249 }
3250 }
3251
3252 if (X86FI->getRestoreBasePointer()) {
3253 unsigned Opc = STI.is64Bit() ? X86::PUSH64r : X86::PUSH32r;
3254 Register BaseReg = this->TRI->getBaseRegister();
3255 BuildMI(MBB, MI, DL, TII.get(Opc))
3256 .addReg(BaseReg, getKillRegState(true))
3258 }
3259
3260 // Make XMM regs spilled. X86 does not have ability of push/pop XMM.
3261 // It can be done by spilling XMMs to stack frame.
3262 for (const CalleeSavedInfo &I : llvm::reverse(CSI)) {
3263 MCRegister Reg = I.getReg();
3264 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg))
3265 continue;
3266
3267 // Add the callee-saved register as live-in. It's killed at the spill.
3268 MBB.addLiveIn(Reg);
3270
3271 TII.storeRegToStackSlot(MBB, MI, Reg, true, I.getFrameIdx(), RC, Register(),
3273 }
3274
3275 return true;
3276}
3277
3278void X86FrameLowering::emitCatchRetReturnValue(MachineBasicBlock &MBB,
3280 MachineInstr *CatchRet) const {
3281 // SEH shouldn't use catchret.
3283 MBB.getParent()->getFunction().getPersonalityFn())) &&
3284 "SEH should not use CATCHRET");
3285 const DebugLoc &DL = CatchRet->getDebugLoc();
3286 MachineBasicBlock *CatchRetTarget = CatchRet->getOperand(0).getMBB();
3287
3288 // Fill EAX/RAX with the address of the target block.
3289 if (STI.is64Bit()) {
3290 // LEA64r CatchRetTarget(%rip), %rax
3291 BuildMI(MBB, MBBI, DL, TII.get(X86::LEA64r), X86::RAX)
3292 .addReg(X86::RIP)
3293 .addImm(0)
3294 .addReg(0)
3295 .addMBB(CatchRetTarget)
3296 .addReg(0);
3297 } else {
3298 // MOV32ri $CatchRetTarget, %eax
3299 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX)
3300 .addMBB(CatchRetTarget);
3301 }
3302
3303 // Record that we've taken the address of CatchRetTarget and no longer just
3304 // reference it in a terminator.
3305 CatchRetTarget->setMachineBlockAddressTaken();
3306}
3307
3311 if (CSI.empty())
3312 return false;
3313
3314 if (MI != MBB.end() && isFuncletReturnInstr(*MI) && STI.isOSWindows()) {
3315 // Don't restore CSRs in 32-bit EH funclets. Matches
3316 // spillCalleeSavedRegisters.
3317 if (STI.is32Bit())
3318 return true;
3319 // Don't restore CSRs before an SEH catchret. SEH except blocks do not form
3320 // funclets. emitEpilogue transforms these to normal jumps.
3321 if (MI->getOpcode() == X86::CATCHRET) {
3322 const Function &F = MBB.getParent()->getFunction();
3323 bool IsSEH = isAsynchronousEHPersonality(
3324 classifyEHPersonality(F.getPersonalityFn()));
3325 if (IsSEH)
3326 return true;
3327 }
3328 }
3329
3330 DebugLoc DL = MBB.findDebugLoc(MI);
3331 MachineFunction &MF = *MBB.getParent();
3333
3334 bool NeedsWin64CFI =
3335 isWin64Prologue(MF) && MF.getFunction().needsUnwindTableEntry();
3336 bool IsWin64UnwindV3 = NeedsWin64CFI && requireWinX64UnwindV3(MF);
3337
3338 // Reload XMMs from stack frame.
3339 for (const CalleeSavedInfo &I : CSI) {
3340 MCRegister Reg = I.getReg();
3341 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg))
3342 continue;
3343
3345 TII.loadRegFromStackSlot(MBB, MI, Reg, I.getFrameIdx(), RC, Register(), 0,
3347 }
3348
3349 // Clear the stack slot for spill base pointer register.
3350 if (X86FI->getRestoreBasePointer()) {
3351 if (IsWin64UnwindV3)
3352 BuildMI(MBB, MI, DL, TII.get(X86::SEH_PushReg))
3353 .addImm(this->TRI->getBaseRegister())
3355 unsigned Opc = STI.is64Bit() ? X86::POP64r : X86::POP32r;
3356 Register BaseReg = this->TRI->getBaseRegister();
3357 BuildMI(MBB, MI, DL, TII.get(Opc), BaseReg)
3359 }
3360
3361 // POP GPRs.
3362 for (auto I = CSI.begin(), E = CSI.end(); I != E; ++I) {
3363 MCRegister Reg = I->getReg();
3364 if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg))
3365 continue;
3366
3367 if (X86FI->isCandidateForPush2Pop2(Reg)) {
3368 MCRegister Reg2 = (++I)->getReg();
3369 if (IsWin64UnwindV3) {
3370 BuildMI(MBB, MI, DL, TII.get(X86::SEH_Push2Regs))
3371 .addImm(Reg)
3372 .addImm(Reg2)
3374 }
3375 BuildMI(MBB, MI, DL, TII.get(getPOP2Opcode(STI)), Reg)
3376 .addReg(Reg2, RegState::Define)
3378 } else {
3379 if (IsWin64UnwindV3)
3380 BuildMI(MBB, MI, DL, TII.get(X86::SEH_PushReg))
3381 .addImm(Reg)
3383 BuildMI(MBB, MI, DL, TII.get(getPOPOpcode(STI)), Reg)
3385 }
3386 }
3387
3388 return true;
3389}
3390
3392 BitVector &SavedRegs,
3393 RegScavenger *RS) const {
3395
3396 // Spill the BasePtr if it's used.
3397 if (TRI->hasBasePointer(MF)) {
3398 Register BasePtr = TRI->getBaseRegister();
3399 if (STI.isTarget64BitILP32())
3400 BasePtr = getX86SubSuperRegister(BasePtr, 64);
3401 SavedRegs.set(BasePtr);
3402 }
3403 if (STI.hasUserReservedRegisters()) {
3404 for (int Reg = SavedRegs.find_first(); Reg != -1;
3405 Reg = SavedRegs.find_next(Reg)) {
3406 if (STI.isRegisterReservedByUser(Reg)) {
3407 SavedRegs.reset(Reg);
3408 }
3409 }
3410 }
3411}
3412
3413static bool HasNestArgument(const MachineFunction *MF) {
3414 const Function &F = MF->getFunction();
3415 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
3416 I++) {
3417 if (I->hasNestAttr() && !I->use_empty())
3418 return true;
3419 }
3420 return false;
3421}
3422
3423/// GetScratchRegister - Get a temp register for performing work in the
3424/// segmented stack and the Erlang/HiPE stack prologue. Depending on platform
3425/// and the properties of the function either one or two registers will be
3426/// needed. Set primary to true for the first register, false for the second.
3427static unsigned GetScratchRegister(bool Is64Bit, bool IsLP64,
3428 const MachineFunction &MF, bool Primary) {
3429 CallingConv::ID CallingConvention = MF.getFunction().getCallingConv();
3430
3431 // Erlang stuff.
3432 if (CallingConvention == CallingConv::HiPE) {
3433 if (Is64Bit)
3434 return Primary ? X86::R14 : X86::R13;
3435 else
3436 return Primary ? X86::EBX : X86::EDI;
3437 }
3438
3439 if (Is64Bit) {
3440 if (IsLP64)
3441 return Primary ? X86::R11 : X86::R12;
3442 else
3443 return Primary ? X86::R11D : X86::R12D;
3444 }
3445
3446 bool IsNested = HasNestArgument(&MF);
3447
3448 if (CallingConvention == CallingConv::X86_FastCall ||
3449 CallingConvention == CallingConv::Fast ||
3450 CallingConvention == CallingConv::Tail) {
3451 if (IsNested)
3452 report_fatal_error("Segmented stacks does not support fastcall with "
3453 "nested function.");
3454 return Primary ? X86::EAX : X86::ECX;
3455 }
3456 if (IsNested)
3457 return Primary ? X86::EDX : X86::EAX;
3458 return Primary ? X86::ECX : X86::EAX;
3459}
3460
3461// The stack limit in the TCB is set to this many bytes above the actual stack
3462// limit.
3464
3466 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
3467 MachineFrameInfo &MFI = MF.getFrameInfo();
3468 uint64_t StackSize;
3469 unsigned TlsReg, TlsOffset;
3470 DebugLoc DL;
3471
3472 // To support shrink-wrapping we would need to insert the new blocks
3473 // at the right place and update the branches to PrologueMBB.
3474 assert(&(*MF.begin()) == &PrologueMBB && "Shrink-wrapping not supported yet");
3475
3476 unsigned ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true);
3477 assert(!MF.getRegInfo().isLiveIn(ScratchReg) &&
3478 "Scratch register is live-in");
3479
3480 if (MF.getFunction().isVarArg())
3481 report_fatal_error("Segmented stacks do not support vararg functions.");
3482 if (!STI.isTargetLinux() && !STI.isTargetDarwin() && !STI.isTargetWin32() &&
3483 !STI.isTargetWin64() && !STI.isTargetFreeBSD() &&
3484 !STI.isTargetDragonFly())
3485 report_fatal_error("Segmented stacks not supported on this platform.");
3486
3487 // Eventually StackSize will be calculated by a link-time pass; which will
3488 // also decide whether checking code needs to be injected into this particular
3489 // prologue.
3490 StackSize = MFI.getStackSize();
3491
3492 if (!MFI.needsSplitStackProlog())
3493 return;
3494
3498 bool IsNested = false;
3499
3500 // We need to know if the function has a nest argument only in 64 bit mode.
3501 if (Is64Bit)
3502 IsNested = HasNestArgument(&MF);
3503
3504 // The MOV R10, RAX needs to be in a different block, since the RET we emit in
3505 // allocMBB needs to be last (terminating) instruction.
3506
3507 for (const auto &LI : PrologueMBB.liveins()) {
3508 allocMBB->addLiveIn(LI);
3509 checkMBB->addLiveIn(LI);
3510 }
3511
3512 if (IsNested)
3513 allocMBB->addLiveIn(IsLP64 ? X86::R10 : X86::R10D);
3514
3515 MF.push_front(allocMBB);
3516 MF.push_front(checkMBB);
3517
3518 // When the frame size is less than 256 we just compare the stack
3519 // boundary directly to the value of the stack pointer, per gcc.
3520 bool CompareStackPointer = StackSize < kSplitStackAvailable;
3521
3522 // Read the limit off the current stacklet off the stack_guard location.
3523 if (Is64Bit) {
3524 if (STI.isTargetLinux()) {
3525 TlsReg = X86::FS;
3526 TlsOffset = IsLP64 ? 0x70 : 0x40;
3527 } else if (STI.isTargetDarwin()) {
3528 TlsReg = X86::GS;
3529 TlsOffset = 0x60 + 90 * 8; // See pthread_machdep.h. Steal TLS slot 90.
3530 } else if (STI.isTargetWin64()) {
3531 TlsReg = X86::GS;
3532 TlsOffset = 0x28; // pvArbitrary, reserved for application use
3533 } else if (STI.isTargetFreeBSD()) {
3534 TlsReg = X86::FS;
3535 TlsOffset = 0x18;
3536 } else if (STI.isTargetDragonFly()) {
3537 TlsReg = X86::FS;
3538 TlsOffset = 0x20; // use tls_tcb.tcb_segstack
3539 } else {
3540 report_fatal_error("Segmented stacks not supported on this platform.");
3541 }
3542
3543 if (CompareStackPointer)
3544 ScratchReg = IsLP64 ? X86::RSP : X86::ESP;
3545 else
3546 BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::LEA64r : X86::LEA64_32r),
3547 ScratchReg)
3548 .addReg(X86::RSP)
3549 .addImm(1)
3550 .addReg(0)
3551 .addImm(-StackSize)
3552 .addReg(0);
3553
3554 BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::CMP64rm : X86::CMP32rm))
3555 .addReg(ScratchReg)
3556 .addReg(0)
3557 .addImm(1)
3558 .addReg(0)
3559 .addImm(TlsOffset)
3560 .addReg(TlsReg);
3561 } else {
3562 if (STI.isTargetLinux()) {
3563 TlsReg = X86::GS;
3564 TlsOffset = 0x30;
3565 } else if (STI.isTargetDarwin()) {
3566 TlsReg = X86::GS;
3567 TlsOffset = 0x48 + 90 * 4;
3568 } else if (STI.isTargetWin32()) {
3569 TlsReg = X86::FS;
3570 TlsOffset = 0x14; // pvArbitrary, reserved for application use
3571 } else if (STI.isTargetDragonFly()) {
3572 TlsReg = X86::FS;
3573 TlsOffset = 0x10; // use tls_tcb.tcb_segstack
3574 } else if (STI.isTargetFreeBSD()) {
3575 report_fatal_error("Segmented stacks not supported on FreeBSD i386.");
3576 } else {
3577 report_fatal_error("Segmented stacks not supported on this platform.");
3578 }
3579
3580 if (CompareStackPointer)
3581 ScratchReg = X86::ESP;
3582 else
3583 BuildMI(checkMBB, DL, TII.get(X86::LEA32r), ScratchReg)
3584 .addReg(X86::ESP)
3585 .addImm(1)
3586 .addReg(0)
3587 .addImm(-StackSize)
3588 .addReg(0);
3589
3590 if (STI.isTargetLinux() || STI.isTargetWin32() || STI.isTargetWin64() ||
3591 STI.isTargetDragonFly()) {
3592 BuildMI(checkMBB, DL, TII.get(X86::CMP32rm))
3593 .addReg(ScratchReg)
3594 .addReg(0)
3595 .addImm(0)
3596 .addReg(0)
3597 .addImm(TlsOffset)
3598 .addReg(TlsReg);
3599 } else if (STI.isTargetDarwin()) {
3600
3601 // TlsOffset doesn't fit into a mod r/m byte so we need an extra register.
3602 unsigned ScratchReg2;
3603 bool SaveScratch2;
3604 if (CompareStackPointer) {
3605 // The primary scratch register is available for holding the TLS offset.
3606 ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, true);
3607 SaveScratch2 = false;
3608 } else {
3609 // Need to use a second register to hold the TLS offset
3610 ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, false);
3611
3612 // Unfortunately, with fastcc the second scratch register may hold an
3613 // argument.
3614 SaveScratch2 = MF.getRegInfo().isLiveIn(ScratchReg2);
3615 }
3616
3617 // If Scratch2 is live-in then it needs to be saved.
3618 assert((!MF.getRegInfo().isLiveIn(ScratchReg2) || SaveScratch2) &&
3619 "Scratch register is live-in and not saved");
3620
3621 if (SaveScratch2)
3622 BuildMI(checkMBB, DL, TII.get(X86::PUSH32r))
3623 .addReg(ScratchReg2, RegState::Kill);
3624
3625 BuildMI(checkMBB, DL, TII.get(X86::MOV32ri), ScratchReg2)
3626 .addImm(TlsOffset);
3627 BuildMI(checkMBB, DL, TII.get(X86::CMP32rm))
3628 .addReg(ScratchReg)
3629 .addReg(ScratchReg2)
3630 .addImm(1)
3631 .addReg(0)
3632 .addImm(0)
3633 .addReg(TlsReg);
3634
3635 if (SaveScratch2)
3636 BuildMI(checkMBB, DL, TII.get(X86::POP32r), ScratchReg2);
3637 }
3638 }
3639
3640 // This jump is taken if SP >= (Stacklet Limit + Stack Space required).
3641 // It jumps to normal execution of the function body.
3642 BuildMI(checkMBB, DL, TII.get(X86::JCC_1))
3643 .addMBB(&PrologueMBB)
3645
3646 // On 32 bit we first push the arguments size and then the frame size. On 64
3647 // bit, we pass the stack frame size in r10 and the argument size in r11.
3648 if (Is64Bit) {
3649 // Functions with nested arguments use R10, so it needs to be saved across
3650 // the call to _morestack
3651
3652 const unsigned RegAX = IsLP64 ? X86::RAX : X86::EAX;
3653 const unsigned Reg10 = IsLP64 ? X86::R10 : X86::R10D;
3654 const unsigned Reg11 = IsLP64 ? X86::R11 : X86::R11D;
3655 const unsigned MOVrr = IsLP64 ? X86::MOV64rr : X86::MOV32rr;
3656
3657 if (IsNested)
3658 BuildMI(allocMBB, DL, TII.get(MOVrr), RegAX).addReg(Reg10);
3659
3660 BuildMI(allocMBB, DL, TII.get(X86::getMOVriOpcode(IsLP64, StackSize)),
3661 Reg10)
3662 .addImm(StackSize);
3663 BuildMI(allocMBB, DL,
3665 Reg11)
3666 .addImm(X86FI->getArgumentStackSize());
3667 } else {
3668 BuildMI(allocMBB, DL, TII.get(X86::PUSH32i))
3669 .addImm(X86FI->getArgumentStackSize());
3670 BuildMI(allocMBB, DL, TII.get(X86::PUSH32i)).addImm(StackSize);
3671 }
3672
3673 // __morestack is in libgcc
3675 // Under the large code model, we cannot assume that __morestack lives
3676 // within 2^31 bytes of the call site, so we cannot use pc-relative
3677 // addressing. We cannot perform the call via a temporary register,
3678 // as the rax register may be used to store the static chain, and all
3679 // other suitable registers may be either callee-save or used for
3680 // parameter passing. We cannot use the stack at this point either
3681 // because __morestack manipulates the stack directly.
3682 //
3683 // To avoid these issues, perform an indirect call via a read-only memory
3684 // location containing the address.
3685 //
3686 // This solution is not perfect, as it assumes that the .rodata section
3687 // is laid out within 2^31 bytes of each function body, but this seems
3688 // to be sufficient for JIT.
3689 // FIXME: Add retpoline support and remove the error here..
3690 if (STI.useIndirectThunkCalls())
3691 report_fatal_error("Emitting morestack calls on 64-bit with the large "
3692 "code model and thunks not yet implemented.");
3693 BuildMI(allocMBB, DL, TII.get(X86::CALL64m))
3694 .addReg(X86::RIP)
3695 .addImm(0)
3696 .addReg(0)
3697 .addExternalSymbol("__morestack_addr")
3698 .addReg(0);
3699 } else {
3700 if (Is64Bit)
3701 BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32))
3702 .addExternalSymbol("__morestack");
3703 else
3704 BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32))
3705 .addExternalSymbol("__morestack");
3706 }
3707
3708 if (IsNested)
3709 BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET_RESTORE_R10));
3710 else
3711 BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET));
3712
3713 allocMBB->addSuccessor(&PrologueMBB);
3714
3715 checkMBB->addSuccessor(allocMBB, BranchProbability::getZero());
3716 checkMBB->addSuccessor(&PrologueMBB, BranchProbability::getOne());
3717
3718#ifdef EXPENSIVE_CHECKS
3719 MF.verify();
3720#endif
3721}
3722
3723/// Lookup an ERTS parameter in the !hipe.literals named metadata node.
3724/// HiPE provides Erlang Runtime System-internal parameters, such as PCB offsets
3725/// to fields it needs, through a named metadata node "hipe.literals" containing
3726/// name-value pairs.
3727static unsigned getHiPELiteral(NamedMDNode *HiPELiteralsMD,
3728 const StringRef LiteralName) {
3729 for (int i = 0, e = HiPELiteralsMD->getNumOperands(); i != e; ++i) {
3730 MDNode *Node = HiPELiteralsMD->getOperand(i);
3731 if (Node->getNumOperands() != 2)
3732 continue;
3733 MDString *NodeName = dyn_cast<MDString>(Node->getOperand(0));
3734 ValueAsMetadata *NodeVal = dyn_cast<ValueAsMetadata>(Node->getOperand(1));
3735 if (!NodeName || !NodeVal)
3736 continue;
3737 ConstantInt *ValConst = dyn_cast_or_null<ConstantInt>(NodeVal->getValue());
3738 if (ValConst && NodeName->getString() == LiteralName) {
3739 return ValConst->getZExtValue();
3740 }
3741 }
3742
3743 report_fatal_error("HiPE literal " + LiteralName +
3744 " required but not provided");
3745}
3746
3747// Return true if there are no non-ehpad successors to MBB and there are no
3748// non-meta instructions between MBBI and MBB.end().
3751 return llvm::all_of(
3752 MBB.successors(),
3753 [](const MachineBasicBlock *Succ) { return Succ->isEHPad(); }) &&
3754 std::all_of(MBBI, MBB.end(), [](const MachineInstr &MI) {
3755 return MI.isMetaInstruction();
3756 });
3757}
3758
3759/// Erlang programs may need a special prologue to handle the stack size they
3760/// might need at runtime. That is because Erlang/OTP does not implement a C
3761/// stack but uses a custom implementation of hybrid stack/heap architecture.
3762/// (for more information see Eric Stenman's Ph.D. thesis:
3763/// http://publications.uu.se/uu/fulltext/nbn_se_uu_diva-2688.pdf)
3764///
3765/// CheckStack:
3766/// temp0 = sp - MaxStack
3767/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart
3768/// OldStart:
3769/// ...
3770/// IncStack:
3771/// call inc_stack # doubles the stack space
3772/// temp0 = sp - MaxStack
3773/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart
3775 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
3776 MachineFrameInfo &MFI = MF.getFrameInfo();
3777 DebugLoc DL;
3778
3779 // To support shrink-wrapping we would need to insert the new blocks
3780 // at the right place and update the branches to PrologueMBB.
3781 assert(&(*MF.begin()) == &PrologueMBB && "Shrink-wrapping not supported yet");
3782
3783 // HiPE-specific values
3784 NamedMDNode *HiPELiteralsMD =
3785 MF.getFunction().getParent()->getNamedMetadata("hipe.literals");
3786 if (!HiPELiteralsMD)
3788 "Can't generate HiPE prologue without runtime parameters");
3789 const unsigned HipeLeafWords = getHiPELiteral(
3790 HiPELiteralsMD, Is64Bit ? "AMD64_LEAF_WORDS" : "X86_LEAF_WORDS");
3791 const unsigned CCRegisteredArgs = Is64Bit ? 6 : 5;
3792 const unsigned Guaranteed = HipeLeafWords * SlotSize;
3793 unsigned CallerStkArity = MF.getFunction().arg_size() > CCRegisteredArgs
3794 ? MF.getFunction().arg_size() - CCRegisteredArgs
3795 : 0;
3796 unsigned MaxStack = MFI.getStackSize() + CallerStkArity * SlotSize + SlotSize;
3797
3798 assert(STI.isTargetLinux() &&
3799 "HiPE prologue is only supported on Linux operating systems.");
3800
3801 // Compute the largest caller's frame that is needed to fit the callees'
3802 // frames. This 'MaxStack' is computed from:
3803 //
3804 // a) the fixed frame size, which is the space needed for all spilled temps,
3805 // b) outgoing on-stack parameter areas, and
3806 // c) the minimum stack space this function needs to make available for the
3807 // functions it calls (a tunable ABI property).
3808 if (MFI.hasCalls()) {
3809 unsigned MoreStackForCalls = 0;
3810
3811 for (auto &MBB : MF) {
3812 for (auto &MI : MBB) {
3813 if (!MI.isCall())
3814 continue;
3815
3816 // Get callee operand.
3817 const MachineOperand &MO = MI.getOperand(0);
3818
3819 // Only take account of global function calls (no closures etc.).
3820 if (!MO.isGlobal())
3821 continue;
3822
3823 const Function *F = dyn_cast<Function>(MO.getGlobal());
3824 if (!F)
3825 continue;
3826
3827 // Do not update 'MaxStack' for primitive and built-in functions
3828 // (encoded with names either starting with "erlang."/"bif_" or not
3829 // having a ".", such as a simple <Module>.<Function>.<Arity>, or an
3830 // "_", such as the BIF "suspend_0") as they are executed on another
3831 // stack.
3832 if (F->getName().contains("erlang.") || F->getName().contains("bif_") ||
3833 F->getName().find_first_of("._") == StringRef::npos)
3834 continue;
3835
3836 unsigned CalleeStkArity = F->arg_size() > CCRegisteredArgs
3837 ? F->arg_size() - CCRegisteredArgs
3838 : 0;
3839 if (HipeLeafWords - 1 > CalleeStkArity)
3840 MoreStackForCalls =
3841 std::max(MoreStackForCalls,
3842 (HipeLeafWords - 1 - CalleeStkArity) * SlotSize);
3843 }
3844 }
3845 MaxStack += MoreStackForCalls;
3846 }
3847
3848 // If the stack frame needed is larger than the guaranteed then runtime checks
3849 // and calls to "inc_stack_0" BIF should be inserted in the assembly prologue.
3850 if (MaxStack > Guaranteed) {
3851 MachineBasicBlock *stackCheckMBB = MF.CreateMachineBasicBlock();
3852 MachineBasicBlock *incStackMBB = MF.CreateMachineBasicBlock();
3853
3854 for (const auto &LI : PrologueMBB.liveins()) {
3855 stackCheckMBB->addLiveIn(LI);
3856 incStackMBB->addLiveIn(LI);
3857 }
3858
3859 MF.push_front(incStackMBB);
3860 MF.push_front(stackCheckMBB);
3861
3862 unsigned ScratchReg, SPReg, PReg, SPLimitOffset;
3863 unsigned LEAop, CMPop, CALLop;
3864 SPLimitOffset = getHiPELiteral(HiPELiteralsMD, "P_NSP_LIMIT");
3865 if (Is64Bit) {
3866 SPReg = X86::RSP;
3867 PReg = X86::RBP;
3868 LEAop = X86::LEA64r;
3869 CMPop = X86::CMP64rm;
3870 CALLop = X86::CALL64pcrel32;
3871 } else {
3872 SPReg = X86::ESP;
3873 PReg = X86::EBP;
3874 LEAop = X86::LEA32r;
3875 CMPop = X86::CMP32rm;
3876 CALLop = X86::CALLpcrel32;
3877 }
3878
3879 ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true);
3880 assert(!MF.getRegInfo().isLiveIn(ScratchReg) &&
3881 "HiPE prologue scratch register is live-in");
3882
3883 // Create new MBB for StackCheck:
3884 addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(LEAop), ScratchReg), SPReg,
3885 false, -MaxStack);
3886 // SPLimitOffset is in a fixed heap location (pointed by BP).
3887 addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(CMPop)).addReg(ScratchReg),
3888 PReg, false, SPLimitOffset);
3889 BuildMI(stackCheckMBB, DL, TII.get(X86::JCC_1))
3890 .addMBB(&PrologueMBB)
3892
3893 // Create new MBB for IncStack:
3894 BuildMI(incStackMBB, DL, TII.get(CALLop)).addExternalSymbol("inc_stack_0");
3895 addRegOffset(BuildMI(incStackMBB, DL, TII.get(LEAop), ScratchReg), SPReg,
3896 false, -MaxStack);
3897 addRegOffset(BuildMI(incStackMBB, DL, TII.get(CMPop)).addReg(ScratchReg),
3898 PReg, false, SPLimitOffset);
3899 BuildMI(incStackMBB, DL, TII.get(X86::JCC_1))
3900 .addMBB(incStackMBB)
3902
3903 stackCheckMBB->addSuccessor(&PrologueMBB, {99, 100});
3904 stackCheckMBB->addSuccessor(incStackMBB, {1, 100});
3905 incStackMBB->addSuccessor(&PrologueMBB, {99, 100});
3906 incStackMBB->addSuccessor(incStackMBB, {1, 100});
3907 }
3908#ifdef EXPENSIVE_CHECKS
3909 MF.verify();
3910#endif
3911}
3912
3913bool X86FrameLowering::adjustStackWithPops(MachineBasicBlock &MBB,
3915 const DebugLoc &DL,
3916 int Offset) const {
3917 if (Offset <= 0)
3918 return false;
3919
3920 if (Offset % SlotSize)
3921 return false;
3922
3923 int NumPops = Offset / SlotSize;
3924 // This is only worth it if we have at most 2 pops.
3925 if (NumPops != 1 && NumPops != 2)
3926 return false;
3927
3928 // Handle only the trivial case where the adjustment directly follows
3929 // a call. This is the most common one, anyway.
3930 if (MBBI == MBB.begin())
3931 return false;
3932 MachineBasicBlock::iterator Prev = std::prev(MBBI);
3933 if (!Prev->isCall() || !Prev->getOperand(1).isRegMask())
3934 return false;
3935
3936 unsigned Regs[2];
3937 unsigned FoundRegs = 0;
3938
3939 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
3940 const MachineOperand &RegMask = Prev->getOperand(1);
3941
3942 auto &RegClass =
3943 Is64Bit ? X86::GR64_NOREX_NOSPRegClass : X86::GR32_NOREX_NOSPRegClass;
3944 // Try to find up to NumPops free registers.
3945 for (auto Candidate : RegClass) {
3946 // Poor man's liveness:
3947 // Since we're immediately after a call, any register that is clobbered
3948 // by the call and not defined by it can be considered dead.
3949 if (!RegMask.clobbersPhysReg(Candidate))
3950 continue;
3951
3952 // Don't clobber reserved registers
3953 if (MRI.isReserved(Candidate))
3954 continue;
3955
3956 bool IsDef = false;
3957 for (const MachineOperand &MO : Prev->implicit_operands()) {
3958 if (MO.isReg() && MO.isDef() &&
3959 TRI->isSuperOrSubRegisterEq(MO.getReg(), Candidate)) {
3960 IsDef = true;
3961 break;
3962 }
3963 }
3964
3965 if (IsDef)
3966 continue;
3967
3968 Regs[FoundRegs++] = Candidate;
3969 if (FoundRegs == (unsigned)NumPops)
3970 break;
3971 }
3972
3973 if (FoundRegs == 0)
3974 return false;
3975
3976 // If we found only one free register, but need two, reuse the same one twice.
3977 while (FoundRegs < (unsigned)NumPops)
3978 Regs[FoundRegs++] = Regs[0];
3979
3980 for (int i = 0; i < NumPops; ++i)
3981 BuildMI(MBB, MBBI, DL, TII.get(STI.is64Bit() ? X86::POP64r : X86::POP32r),
3982 Regs[i]);
3983
3984 return true;
3985}
3986
3990 bool reserveCallFrame = hasReservedCallFrame(MF);
3991 unsigned Opcode = I->getOpcode();
3992 bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode();
3993 DebugLoc DL = I->getDebugLoc(); // copy DebugLoc as I will be erased.
3994 uint64_t Amount = TII.getFrameSize(*I);
3995 uint64_t InternalAmt = (isDestroy || Amount) ? TII.getFrameAdjustment(*I) : 0;
3996 I = MBB.erase(I);
3997 auto InsertPos = skipDebugInstructionsForward(I, MBB.end());
3998
3999 // Try to avoid emitting dead SP adjustments if the block end is unreachable,
4000 // typically because the function is marked noreturn (abort, throw,
4001 // assert_fail, etc).
4002 if (isDestroy && blockEndIsUnreachable(MBB, I))
4003 return I;
4004
4005 if (!reserveCallFrame) {
4006 // If the stack pointer can be changed after prologue, turn the
4007 // adjcallstackup instruction into a 'sub ESP, <amt>' and the
4008 // adjcallstackdown instruction into 'add ESP, <amt>'
4009
4010 // We need to keep the stack aligned properly. To do this, we round the
4011 // amount of space needed for the outgoing arguments up to the next
4012 // alignment boundary.
4013 Amount = alignTo(Amount, getStackAlign());
4014
4015 const Function &F = MF.getFunction();
4016 bool WindowsCFI = MF.getTarget().getMCAsmInfo().usesWindowsCFI();
4017 bool DwarfCFI = !WindowsCFI && MF.needsFrameMoves();
4018
4019 // If we have any exception handlers in this function, and we adjust
4020 // the SP before calls, we may need to indicate this to the unwinder
4021 // using GNU_ARGS_SIZE. Note that this may be necessary even when
4022 // Amount == 0, because the preceding function may have set a non-0
4023 // GNU_ARGS_SIZE.
4024 // TODO: We don't need to reset this between subsequent functions,
4025 // if it didn't change.
4026 bool HasDwarfEHHandlers = !WindowsCFI && !MF.getLandingPads().empty();
4027
4028 if (HasDwarfEHHandlers && !isDestroy &&
4030 BuildCFI(MBB, InsertPos, DL,
4031 MCCFIInstruction::createGnuArgsSize(nullptr, Amount));
4032
4033 if (Amount == 0)
4034 return I;
4035
4036 // Factor out the amount that gets handled inside the sequence
4037 // (Pushes of argument for frame setup, callee pops for frame destroy)
4038 Amount -= InternalAmt;
4039
4040 // TODO: This is needed only if we require precise CFA.
4041 // If this is a callee-pop calling convention, emit a CFA adjust for
4042 // the amount the callee popped.
4043 if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF))
4044 BuildCFI(MBB, InsertPos, DL,
4045 MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt));
4046
4047 // Add Amount to SP to destroy a frame, or subtract to setup.
4048 int64_t StackAdjustment = isDestroy ? Amount : -Amount;
4049 int64_t CfaAdjustment = StackAdjustment;
4050
4051 if (StackAdjustment) {
4052 // Merge with any previous or following adjustment instruction. Note: the
4053 // instructions merged with here do not have CFI, so their stack
4054 // adjustments do not feed into CfaAdjustment
4055
4056 auto CalcCfaAdjust = [&CfaAdjustment](MachineBasicBlock::iterator PI,
4057 int64_t Offset) {
4058 CfaAdjustment += Offset;
4059 };
4060 auto CalcNewOffset = [&StackAdjustment](int64_t Offset) {
4061 return StackAdjustment + Offset;
4062 };
4063 StackAdjustment =
4064 mergeSPUpdates(MBB, InsertPos, CalcCfaAdjust, CalcNewOffset, true);
4065 StackAdjustment =
4066 mergeSPUpdates(MBB, InsertPos, CalcCfaAdjust, CalcNewOffset, false);
4067
4068 if (StackAdjustment) {
4069 if (!(F.hasMinSize() &&
4070 adjustStackWithPops(MBB, InsertPos, DL, StackAdjustment)))
4071 BuildStackAdjustment(MBB, InsertPos, DL, StackAdjustment,
4072 /*InEpilogue=*/false);
4073 }
4074 }
4075
4076 if (DwarfCFI && !hasFP(MF) && CfaAdjustment) {
4077 // If we don't have FP, but need to generate unwind information,
4078 // we need to set the correct CFA offset after the stack adjustment.
4079 // How much we adjust the CFA offset depends on whether we're emitting
4080 // CFI only for EH purposes or for debugging. EH only requires the CFA
4081 // offset to be correct at each call site, while for debugging we want
4082 // it to be more precise.
4083
4084 // TODO: When not using precise CFA, we also need to adjust for the
4085 // InternalAmt here.
4086 BuildCFI(
4087 MBB, InsertPos, DL,
4088 MCCFIInstruction::createAdjustCfaOffset(nullptr, -CfaAdjustment));
4089 }
4090
4091 return I;
4092 }
4093
4094 if (InternalAmt) {
4097 while (CI != B && !std::prev(CI)->isCall())
4098 --CI;
4099 BuildStackAdjustment(MBB, CI, DL, -InternalAmt, /*InEpilogue=*/false);
4100 }
4101
4102 return I;
4103}
4104
4106 assert(MBB.getParent() && "Block is not attached to a function!");
4107 const MachineFunction &MF = *MBB.getParent();
4108 if (!MBB.isLiveIn(X86::EFLAGS))
4109 return true;
4110
4111 // If stack probes have to loop inline or call, that will clobber EFLAGS.
4112 // FIXME: we could allow cases that will use emitStackProbeInlineGenericBlock.
4114 const X86TargetLowering &TLI = *STI.getTargetLowering();
4115 if (TLI.hasInlineStackProbe(MF) || TLI.hasStackProbeSymbol(MF))
4116 return false;
4117
4119 return !TRI->hasStackRealignment(MF) && !X86FI->hasSwiftAsyncContext();
4120}
4121
4123 assert(MBB.getParent() && "Block is not attached to a function!");
4124
4125 // Win64 has strict requirements in terms of epilogue and we are
4126 // not taking a chance at messing with them.
4127 // I.e., unless this block is already an exit block, we can't use
4128 // it as an epilogue.
4129 if (STI.isTargetWin64() && !MBB.succ_empty() && !MBB.isReturnBlock())
4130 return false;
4131
4132 // Swift async context epilogue has a BTR instruction that clobbers parts of
4133 // EFLAGS.
4134 const MachineFunction &MF = *MBB.getParent();
4137
4138 if (canUseLEAForSPInEpilogue(*MBB.getParent()))
4139 return true;
4140
4141 // If we cannot use LEA to adjust SP, we may need to use ADD, which
4142 // clobbers the EFLAGS. Check that we do not need to preserve it,
4143 // otherwise, conservatively assume this is not
4144 // safe to insert the epilogue here.
4146}
4147
4149 // If we may need to emit frameless compact unwind information, give
4150 // up as this is currently broken: PR25614.
4151 bool CompactUnwind =
4153 return (MF.getFunction().hasFnAttribute(Attribute::NoUnwind) || hasFP(MF) ||
4154 !CompactUnwind) &&
4155 // The lowering of segmented stack and HiPE only support entry
4156 // blocks as prologue blocks: PR26107. This limitation may be
4157 // lifted if we fix:
4158 // - adjustForSegmentedStacks
4159 // - adjustForHiPEPrologue
4161 !MF.shouldSplitStack();
4162}
4163
4166 const DebugLoc &DL, bool RestoreSP) const {
4167 assert(STI.isTargetWindowsMSVC() && "funclets only supported in MSVC env");
4168 assert(STI.isTargetWin32() && "EBP/ESI restoration only required on win32");
4169 assert(STI.is32Bit() && !Uses64BitFramePtr &&
4170 "restoring EBP/ESI on non-32-bit target");
4171
4172 MachineFunction &MF = *MBB.getParent();
4173 Register FramePtr = TRI->getFrameRegister(MF);
4174 Register BasePtr = TRI->getBaseRegister();
4175 WinEHFuncInfo &FuncInfo = *MF.getWinEHFuncInfo();
4177 MachineFrameInfo &MFI = MF.getFrameInfo();
4178
4179 // FIXME: Don't set FrameSetup flag in catchret case.
4180
4181 int FI = FuncInfo.EHRegNodeFrameIndex;
4182 int EHRegSize = MFI.getObjectSize(FI);
4183
4184 if (RestoreSP) {
4185 // MOV32rm -EHRegSize(%ebp), %esp
4186 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32rm), X86::ESP),
4187 X86::EBP, true, -EHRegSize)
4189 }
4190
4191 Register UsedReg;
4192 int EHRegOffset = getFrameIndexReference(MF, FI, UsedReg).getFixed();
4193 int EndOffset = -EHRegOffset - EHRegSize;
4194 FuncInfo.EHRegNodeEndOffset = EndOffset;
4195
4196 if (UsedReg == FramePtr) {
4197 // ADD $offset, %ebp
4198 unsigned ADDri = getADDriOpcode(false);
4199 BuildMI(MBB, MBBI, DL, TII.get(ADDri), FramePtr)
4201 .addImm(EndOffset)
4203 ->getOperand(3)
4204 .setIsDead();
4205 assert(EndOffset >= 0 &&
4206 "end of registration object above normal EBP position!");
4207 } else if (UsedReg == BasePtr) {
4208 // LEA offset(%ebp), %esi
4209 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::LEA32r), BasePtr),
4210 FramePtr, false, EndOffset)
4212 // MOV32rm SavedEBPOffset(%esi), %ebp
4213 assert(X86FI->getHasSEHFramePtrSave());
4214 int Offset =
4215 getFrameIndexReference(MF, X86FI->getSEHFramePtrSaveIndex(), UsedReg)
4216 .getFixed();
4217 assert(UsedReg == BasePtr);
4218 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32rm), FramePtr),
4219 UsedReg, true, Offset)
4221 } else {
4222 llvm_unreachable("32-bit frames with WinEH must use FramePtr or BasePtr");
4223 }
4224 return MBBI;
4225}
4226
4228 return TRI->getSlotSize();
4229}
4230
4235
4239 Register FrameRegister = RI->getFrameRegister(MF);
4240 if (getInitialCFARegister(MF) == FrameRegister &&
4242 DwarfFrameBase FrameBase;
4243 FrameBase.Kind = DwarfFrameBase::CFA;
4244 FrameBase.Location.Offset =
4246 return FrameBase;
4247 }
4248
4249 return DwarfFrameBase{DwarfFrameBase::Register, {FrameRegister}};
4250}
4251
4252namespace {
4253// Struct used by orderFrameObjects to help sort the stack objects.
4254struct X86FrameSortingObject {
4255 bool IsValid = false; // true if we care about this Object.
4256 unsigned ObjectIndex = 0; // Index of Object into MFI list.
4257 unsigned ObjectSize = 0; // Size of Object in bytes.
4258 Align ObjectAlignment = Align(1); // Alignment of Object in bytes.
4259 unsigned ObjectNumUses = 0; // Object static number of uses.
4260};
4261
4262// The comparison function we use for std::sort to order our local
4263// stack symbols. The current algorithm is to use an estimated
4264// "density". This takes into consideration the size and number of
4265// uses each object has in order to roughly minimize code size.
4266// So, for example, an object of size 16B that is referenced 5 times
4267// will get higher priority than 4 4B objects referenced 1 time each.
4268// It's not perfect and we may be able to squeeze a few more bytes out of
4269// it (for example : 0(esp) requires fewer bytes, symbols allocated at the
4270// fringe end can have special consideration, given their size is less
4271// important, etc.), but the algorithmic complexity grows too much to be
4272// worth the extra gains we get. This gets us pretty close.
4273// The final order leaves us with objects with highest priority going
4274// at the end of our list.
4275struct X86FrameSortingComparator {
4276 inline bool operator()(const X86FrameSortingObject &A,
4277 const X86FrameSortingObject &B) const {
4278 uint64_t DensityAScaled, DensityBScaled;
4279
4280 // For consistency in our comparison, all invalid objects are placed
4281 // at the end. This also allows us to stop walking when we hit the
4282 // first invalid item after it's all sorted.
4283 if (!A.IsValid)
4284 return false;
4285 if (!B.IsValid)
4286 return true;
4287
4288 // The density is calculated by doing :
4289 // (double)DensityA = A.ObjectNumUses / A.ObjectSize
4290 // (double)DensityB = B.ObjectNumUses / B.ObjectSize
4291 // Since this approach may cause inconsistencies in
4292 // the floating point <, >, == comparisons, depending on the floating
4293 // point model with which the compiler was built, we're going
4294 // to scale both sides by multiplying with
4295 // A.ObjectSize * B.ObjectSize. This ends up factoring away
4296 // the division and, with it, the need for any floating point
4297 // arithmetic.
4298 DensityAScaled = static_cast<uint64_t>(A.ObjectNumUses) *
4299 static_cast<uint64_t>(B.ObjectSize);
4300 DensityBScaled = static_cast<uint64_t>(B.ObjectNumUses) *
4301 static_cast<uint64_t>(A.ObjectSize);
4302
4303 // If the two densities are equal, prioritize highest alignment
4304 // objects. This allows for similar alignment objects
4305 // to be packed together (given the same density).
4306 // There's room for improvement here, also, since we can pack
4307 // similar alignment (different density) objects next to each
4308 // other to save padding. This will also require further
4309 // complexity/iterations, and the overall gain isn't worth it,
4310 // in general. Something to keep in mind, though.
4311 if (DensityAScaled == DensityBScaled)
4312 return A.ObjectAlignment < B.ObjectAlignment;
4313
4314 return DensityAScaled < DensityBScaled;
4315 }
4316};
4317} // namespace
4318
4319// Order the symbols in the local stack.
4320// We want to place the local stack objects in some sort of sensible order.
4321// The heuristic we use is to try and pack them according to static number
4322// of uses and size of object in order to minimize code size.
4324 const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const {
4325 const MachineFrameInfo &MFI = MF.getFrameInfo();
4326
4327 // Don't waste time if there's nothing to do.
4328 if (ObjectsToAllocate.empty())
4329 return;
4330
4331 // Create an array of all MFI objects. We won't need all of these
4332 // objects, but we're going to create a full array of them to make
4333 // it easier to index into when we're counting "uses" down below.
4334 // We want to be able to easily/cheaply access an object by simply
4335 // indexing into it, instead of having to search for it every time.
4336 std::vector<X86FrameSortingObject> SortingObjects(MFI.getObjectIndexEnd());
4337
4338 // Walk the objects we care about and mark them as such in our working
4339 // struct.
4340 for (auto &Obj : ObjectsToAllocate) {
4341 SortingObjects[Obj].IsValid = true;
4342 SortingObjects[Obj].ObjectIndex = Obj;
4343 SortingObjects[Obj].ObjectAlignment = MFI.getObjectAlign(Obj);
4344 // Set the size.
4345 int ObjectSize = MFI.getObjectSize(Obj);
4346 if (ObjectSize == 0)
4347 // Variable size. Just use 4.
4348 SortingObjects[Obj].ObjectSize = 4;
4349 else
4350 SortingObjects[Obj].ObjectSize = ObjectSize;
4351 }
4352
4353 // Count the number of uses for each object.
4354 for (auto &MBB : MF) {
4355 for (auto &MI : MBB) {
4356 if (MI.isDebugInstr())
4357 continue;
4358 for (const MachineOperand &MO : MI.operands()) {
4359 // Check to see if it's a local stack symbol.
4360 if (!MO.isFI())
4361 continue;
4362 int Index = MO.getIndex();
4363 // Check to see if it falls within our range, and is tagged
4364 // to require ordering.
4365 if (Index >= 0 && Index < MFI.getObjectIndexEnd() &&
4366 SortingObjects[Index].IsValid)
4367 SortingObjects[Index].ObjectNumUses++;
4368 }
4369 }
4370 }
4371
4372 // Sort the objects using X86FrameSortingAlgorithm (see its comment for
4373 // info).
4374 llvm::stable_sort(SortingObjects, X86FrameSortingComparator());
4375
4376 // Now modify the original list to represent the final order that
4377 // we want. The order will depend on whether we're going to access them
4378 // from the stack pointer or the frame pointer. For SP, the list should
4379 // end up with the END containing objects that we want with smaller offsets.
4380 // For FP, it should be flipped.
4381 int i = 0;
4382 for (auto &Obj : SortingObjects) {
4383 // All invalid items are sorted at the end, so it's safe to stop.
4384 if (!Obj.IsValid)
4385 break;
4386 ObjectsToAllocate[i++] = Obj.ObjectIndex;
4387 }
4388
4389 // Flip it if we're accessing off of the FP.
4390 if (!TRI->hasStackRealignment(MF) && hasFP(MF))
4391 std::reverse(ObjectsToAllocate.begin(), ObjectsToAllocate.end());
4392}
4393
4394unsigned
4396 // RDX, the parent frame pointer, is homed into 16(%rsp) in the prologue.
4397 unsigned Offset = 16;
4398 // RBP is immediately pushed.
4399 Offset += SlotSize;
4400 // All callee-saved registers are then pushed.
4401 Offset += MF.getInfo<X86MachineFunctionInfo>()->getCalleeSavedFrameSize();
4402 // Every funclet allocates enough stack space for the largest outgoing call.
4403 Offset += getWinEHFuncletFrameSize(MF);
4404 return Offset;
4405}
4406
4408 MachineFunction &MF, RegScavenger *RS) const {
4409 // Mark the function as not having WinCFI. We will set it back to true in
4410 // emitPrologue if it gets called and emits CFI.
4411 MF.setHasWinCFI(false);
4412
4413 MachineFrameInfo &MFI = MF.getFrameInfo();
4414 // If the frame is big enough that we might need to scavenge a register to
4415 // handle huge offsets, reserve a stack slot for that now.
4416 if (!isInt<32>(MFI.estimateStackSize(MF))) {
4417 int FI = MFI.CreateStackObject(SlotSize, Align(SlotSize), false);
4418 RS->addScavengingFrameIndex(FI);
4419 }
4420
4421 // If we are using Windows x64 CFI, ensure that the stack is always 8 byte
4422 // aligned. The format doesn't support misaligned stack adjustments.
4425
4426 // If this function isn't doing Win64-style C++ EH, we don't need to do
4427 // anything.
4428 if (STI.is64Bit() && MF.hasEHFunclets() &&
4431 adjustFrameForMsvcCxxEh(MF);
4432 }
4433}
4434
4435void X86FrameLowering::adjustFrameForMsvcCxxEh(MachineFunction &MF) const {
4436 // Win64 C++ EH needs to allocate the UnwindHelp object at some fixed offset
4437 // relative to RSP after the prologue. Find the offset of the last fixed
4438 // object, so that we can allocate a slot immediately following it. If there
4439 // were no fixed objects, use offset -SlotSize, which is immediately after the
4440 // return address. Fixed objects have negative frame indices.
4441 MachineFrameInfo &MFI = MF.getFrameInfo();
4442 WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo();
4443 int64_t MinFixedObjOffset = -SlotSize;
4444 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I)
4445 MinFixedObjOffset = std::min(MinFixedObjOffset, MFI.getObjectOffset(I));
4446
4447 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
4448 for (WinEHHandlerType &H : TBME.HandlerArray) {
4449 int FrameIndex = H.CatchObj.FrameIndex;
4450 if ((FrameIndex != INT_MAX) && MFI.getObjectOffset(FrameIndex) == 0) {
4451 // Ensure alignment.
4452 unsigned Align = MFI.getObjectAlign(FrameIndex).value();
4453 MinFixedObjOffset -= std::abs(MinFixedObjOffset) % Align;
4454 MinFixedObjOffset -= MFI.getObjectSize(FrameIndex);
4455 MFI.setObjectOffset(FrameIndex, MinFixedObjOffset);
4456 }
4457 }
4458 }
4459
4460 // Ensure alignment.
4461 MinFixedObjOffset -= std::abs(MinFixedObjOffset) % 8;
4462 int64_t UnwindHelpOffset = MinFixedObjOffset - SlotSize;
4463 int UnwindHelpFI =
4464 MFI.CreateFixedObject(SlotSize, UnwindHelpOffset, /*IsImmutable=*/false);
4465 EHInfo.UnwindHelpFrameIdx = UnwindHelpFI;
4466
4467 // Store -2 into UnwindHelp on function entry. We have to scan forwards past
4468 // other frame setup instructions.
4469 MachineBasicBlock &MBB = MF.front();
4470 auto MBBI = MBB.begin();
4471 while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
4472 ++MBBI;
4473
4475 addFrameReference(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mi32)),
4476 UnwindHelpFI)
4477 .addImm(-2);
4478}
4479
4481 MachineFunction &MF, RegScavenger *RS) const {
4482 auto *X86FI = MF.getInfo<X86MachineFunctionInfo>();
4483
4484 if (STI.is32Bit() && MF.hasEHFunclets())
4486 // We have emitted prolog and epilog. Don't need stack pointer saving
4487 // instruction any more.
4488 if (MachineInstr *MI = X86FI->getStackPtrSaveMI()) {
4489 MI->eraseFromParent();
4490 X86FI->setStackPtrSaveMI(nullptr);
4491 }
4492}
4493
4495 MachineFunction &MF) const {
4496 // 32-bit functions have to restore stack pointers when control is transferred
4497 // back to the parent function. These blocks are identified as eh pads that
4498 // are not funclet entries.
4499 bool IsSEH = isAsynchronousEHPersonality(
4501 for (MachineBasicBlock &MBB : MF) {
4502 bool NeedsRestore = MBB.isEHPad() && !MBB.isEHFuncletEntry();
4503 if (NeedsRestore)
4505 /*RestoreSP=*/IsSEH);
4506 }
4507}
4508
4509// Compute the alignment gap between current SP after spilling FP/BP and the
4510// next properly aligned stack offset.
4512 const TargetRegisterClass *RC,
4513 unsigned NumSpilledRegs) {
4515 unsigned AllocSize = TRI->getSpillSize(*RC) * NumSpilledRegs;
4516 Align StackAlign = MF.getSubtarget().getFrameLowering()->getStackAlign();
4517 unsigned AlignedSize = alignTo(AllocSize, StackAlign);
4518 return AlignedSize - AllocSize;
4519}
4520
4521void X86FrameLowering::spillFPBPUsingSP(MachineFunction &MF,
4523 Register FP, Register BP,
4524 int SPAdjust) const {
4525 assert(FP.isValid() || BP.isValid());
4526
4527 MachineBasicBlock *MBB = BeforeMI->getParent();
4528 DebugLoc DL = BeforeMI->getDebugLoc();
4529
4530 // Spill FP.
4531 if (FP.isValid()) {
4532 BuildMI(*MBB, BeforeMI, DL,
4533 TII.get(getPUSHOpcode(MF.getSubtarget<X86Subtarget>())))
4534 .addReg(FP);
4535 }
4536
4537 // Spill BP.
4538 if (BP.isValid()) {
4539 BuildMI(*MBB, BeforeMI, DL,
4540 TII.get(getPUSHOpcode(MF.getSubtarget<X86Subtarget>())))
4541 .addReg(BP);
4542 }
4543
4544 // Make sure SP is aligned.
4545 if (SPAdjust)
4546 emitSPUpdate(*MBB, BeforeMI, DL, -SPAdjust, false);
4547
4548 // Emit unwinding information.
4549 if (FP.isValid() && needsDwarfCFI(MF)) {
4550 // Emit .cfi_remember_state to remember old frame.
4551 unsigned CFIIndex =
4553 BuildMI(*MBB, BeforeMI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
4554 .addCFIIndex(CFIIndex);
4555
4556 // Setup new CFA value with DW_CFA_def_cfa_expression:
4557 // DW_OP_breg7+offset, DW_OP_deref, DW_OP_consts 16, DW_OP_plus
4558 SmallString<64> CfaExpr;
4559 uint8_t buffer[16];
4560 int Offset = SPAdjust;
4561 if (BP.isValid())
4562 Offset += TRI->getSpillSize(*TRI->getMinimalPhysRegClass(BP));
4563 // If BeforeMI is a frame setup instruction, we need to adjust the position
4564 // and offset of the new cfi instruction.
4565 if (TII.isFrameSetup(*BeforeMI)) {
4566 Offset += alignTo(TII.getFrameSize(*BeforeMI), getStackAlign());
4567 BeforeMI = std::next(BeforeMI);
4568 }
4569 Register StackPtr = TRI->getStackRegister();
4570 if (STI.isTarget64BitILP32())
4572 unsigned DwarfStackPtr = TRI->getDwarfRegNum(StackPtr, true);
4573 CfaExpr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfStackPtr));
4574 CfaExpr.append(buffer, buffer + encodeSLEB128(Offset, buffer));
4575 CfaExpr.push_back(dwarf::DW_OP_deref);
4576 CfaExpr.push_back(dwarf::DW_OP_consts);
4577 CfaExpr.append(buffer, buffer + encodeSLEB128(SlotSize * 2, buffer));
4578 CfaExpr.push_back((uint8_t)dwarf::DW_OP_plus);
4579
4580 SmallString<64> DefCfaExpr;
4581 DefCfaExpr.push_back(dwarf::DW_CFA_def_cfa_expression);
4582 DefCfaExpr.append(buffer, buffer + encodeSLEB128(CfaExpr.size(), buffer));
4583 DefCfaExpr.append(CfaExpr.str());
4584 BuildCFI(*MBB, BeforeMI, DL,
4585 MCCFIInstruction::createEscape(nullptr, DefCfaExpr.str()),
4587 }
4588}
4589
4590void X86FrameLowering::restoreFPBPUsingSP(MachineFunction &MF,
4592 Register FP, Register BP,
4593 int SPAdjust) const {
4594 assert(FP.isValid() || BP.isValid());
4595
4596 // Adjust SP so it points to spilled FP or BP.
4597 MachineBasicBlock *MBB = AfterMI->getParent();
4598 MachineBasicBlock::iterator Pos = std::next(AfterMI);
4599 DebugLoc DL = AfterMI->getDebugLoc();
4600 if (SPAdjust)
4601 emitSPUpdate(*MBB, Pos, DL, SPAdjust, false);
4602
4603 // Restore BP.
4604 if (BP.isValid()) {
4605 BuildMI(*MBB, Pos, DL,
4606 TII.get(getPOPOpcode(MF.getSubtarget<X86Subtarget>())), BP);
4607 }
4608
4609 // Restore FP.
4610 if (FP.isValid()) {
4611 BuildMI(*MBB, Pos, DL,
4612 TII.get(getPOPOpcode(MF.getSubtarget<X86Subtarget>())), FP);
4613
4614 // Emit unwinding information.
4615 if (needsDwarfCFI(MF)) {
4616 // Restore original frame with .cfi_restore_state.
4617 unsigned CFIIndex =
4619 BuildMI(*MBB, Pos, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
4620 .addCFIIndex(CFIIndex);
4621 }
4622 }
4623}
4624
4625void X86FrameLowering::saveAndRestoreFPBPUsingSP(
4627 MachineBasicBlock::iterator AfterMI, bool SpillFP, bool SpillBP) const {
4628 assert(SpillFP || SpillBP);
4629
4630 Register FP, BP;
4631 const TargetRegisterClass *RC;
4632 unsigned NumRegs = 0;
4633
4634 if (SpillFP) {
4635 FP = TRI->getFrameRegister(MF);
4636 if (STI.isTarget64BitILP32())
4638 RC = TRI->getMinimalPhysRegClass(FP);
4639 ++NumRegs;
4640 }
4641 if (SpillBP) {
4642 BP = TRI->getBaseRegister();
4643 if (STI.isTarget64BitILP32())
4644 BP = Register(getX86SubSuperRegister(BP, 64));
4645 RC = TRI->getMinimalPhysRegClass(BP);
4646 ++NumRegs;
4647 }
4648 int SPAdjust = computeFPBPAlignmentGap(MF, RC, NumRegs);
4649
4650 spillFPBPUsingSP(MF, BeforeMI, FP, BP, SPAdjust);
4651 restoreFPBPUsingSP(MF, AfterMI, FP, BP, SPAdjust);
4652}
4653
4654bool X86FrameLowering::skipSpillFPBP(
4656 if (MI->getOpcode() == X86::LCMPXCHG16B_SAVE_RBX) {
4657 // The pseudo instruction LCMPXCHG16B_SAVE_RBX is generated in the form
4658 // SaveRbx = COPY RBX
4659 // SaveRbx = LCMPXCHG16B_SAVE_RBX ..., SaveRbx, implicit-def rbx
4660 // And later LCMPXCHG16B_SAVE_RBX is expanded to restore RBX from SaveRbx.
4661 // We should skip this instruction sequence.
4662 int FI;
4663 Register Reg;
4664 while (!(MI->getOpcode() == TargetOpcode::COPY &&
4665 MI->getOperand(1).getReg() == X86::RBX) &&
4666 !((Reg = TII.isStoreToStackSlot(*MI, FI)) && Reg == X86::RBX))
4667 ++MI;
4668 return true;
4669 }
4670 return false;
4671}
4672
4674 const TargetRegisterInfo *TRI, bool &AccessFP,
4675 bool &AccessBP) {
4676 AccessFP = AccessBP = false;
4677 if (FP) {
4678 if (MI.findRegisterUseOperandIdx(FP, TRI, false) != -1 ||
4679 MI.findRegisterDefOperandIdx(FP, TRI, false, true) != -1)
4680 AccessFP = true;
4681 }
4682 if (BP) {
4683 if (MI.findRegisterUseOperandIdx(BP, TRI, false) != -1 ||
4684 MI.findRegisterDefOperandIdx(BP, TRI, false, true) != -1)
4685 AccessBP = true;
4686 }
4687 return AccessFP || AccessBP;
4688}
4689
4690// Invoke instruction has been lowered to normal function call. We try to figure
4691// out if MI comes from Invoke.
4692// Do we have any better method?
4693static bool isInvoke(const MachineInstr &MI, bool InsideEHLabels) {
4694 if (!MI.isCall())
4695 return false;
4696 if (InsideEHLabels)
4697 return true;
4698
4699 const MachineBasicBlock *MBB = MI.getParent();
4700 if (!MBB->hasEHPadSuccessor())
4701 return false;
4702
4703 // Check if there is another call instruction from MI to the end of MBB.
4705 for (++MBBI; MBBI != ME; ++MBBI)
4706 if (MBBI->isCall())
4707 return false;
4708 return true;
4709}
4710
4711/// Given the live range of FP or BP (DefMI, KillMI), check if there is any
4712/// interfered stack access in the range, usually generated by register spill.
4713void X86FrameLowering::checkInterferedAccess(
4715 MachineBasicBlock::reverse_iterator KillMI, bool SpillFP,
4716 bool SpillBP) const {
4717 if (DefMI == KillMI)
4718 return;
4719 if (TRI->hasBasePointer(MF)) {
4720 if (!SpillBP)
4721 return;
4722 } else {
4723 if (!SpillFP)
4724 return;
4725 }
4726
4727 auto MI = KillMI;
4728 while (MI != DefMI) {
4729 if (any_of(MI->operands(),
4730 [](const MachineOperand &MO) { return MO.isFI(); }))
4731 MF.getContext().reportError(SMLoc(),
4732 "Interference usage of base pointer/frame "
4733 "pointer.");
4734 MI++;
4735 }
4736}
4737
4738/// If a function uses base pointer and the base pointer is clobbered by inline
4739/// asm, RA doesn't detect this case, and after the inline asm, the base pointer
4740/// contains garbage value.
4741/// For example if a 32b x86 function uses base pointer esi, and esi is
4742/// clobbered by following inline asm
4743/// asm("rep movsb" : "+D"(ptr), "+S"(x), "+c"(c)::"memory");
4744/// We need to save esi before the asm and restore it after the asm.
4745///
4746/// The problem can also occur to frame pointer if there is a function call, and
4747/// the callee uses a different calling convention and clobbers the fp.
4748///
4749/// Because normal frame objects (spill slots) are accessed through fp/bp
4750/// register, so we can't spill fp/bp to normal spill slots.
4751///
4752/// FIXME: There are 2 possible enhancements:
4753/// 1. In many cases there are different physical registers not clobbered by
4754/// inline asm, we can use one of them as base pointer. Or use a virtual
4755/// register as base pointer and let RA allocate a physical register to it.
4756/// 2. If there is no other instructions access stack with fp/bp from the
4757/// inline asm to the epilog, and no cfi requirement for a correct fp, we can
4758/// skip the save and restore operations.
4760 Register FP, BP;
4762 if (TFI.hasFP(MF))
4763 FP = TRI->getFrameRegister(MF);
4764 if (TRI->hasBasePointer(MF))
4765 BP = TRI->getBaseRegister();
4766
4767 // Currently only inline asm and function call can clobbers fp/bp. So we can
4768 // do some quick test and return early.
4769 if (!MF.hasInlineAsm()) {
4771 if (!X86FI->getFPClobberedByCall())
4772 FP = 0;
4773 if (!X86FI->getBPClobberedByCall())
4774 BP = 0;
4775 }
4776 if (!FP && !BP)
4777 return;
4778
4779 for (MachineBasicBlock &MBB : MF) {
4780 bool InsideEHLabels = false;
4781 auto MI = MBB.rbegin(), ME = MBB.rend();
4782 auto TermMI = MBB.getFirstTerminator();
4783 if (TermMI == MBB.begin())
4784 continue;
4785 MI = *(std::prev(TermMI));
4786
4787 while (MI != ME) {
4788 // Skip frame setup/destroy instructions.
4789 // Skip Invoke (call inside try block) instructions.
4790 // Skip instructions handled by target.
4791 if (MI->getFlag(MachineInstr::MIFlag::FrameSetup) ||
4793 isInvoke(*MI, InsideEHLabels) || skipSpillFPBP(MF, MI)) {
4794 ++MI;
4795 continue;
4796 }
4797
4798 if (MI->getOpcode() == TargetOpcode::EH_LABEL) {
4799 InsideEHLabels = !InsideEHLabels;
4800 ++MI;
4801 continue;
4802 }
4803
4804 bool AccessFP, AccessBP;
4805 // Check if fp or bp is used in MI.
4806 if (!isFPBPAccess(*MI, FP, BP, TRI, AccessFP, AccessBP)) {
4807 ++MI;
4808 continue;
4809 }
4810
4811 // Look for the range [DefMI, KillMI] in which fp or bp is defined and
4812 // used.
4813 bool FPLive = false, BPLive = false;
4814 bool SpillFP = false, SpillBP = false;
4815 auto DefMI = MI, KillMI = MI;
4816 do {
4817 SpillFP |= AccessFP;
4818 SpillBP |= AccessBP;
4819
4820 // Maintain FPLive and BPLive.
4821 if (FPLive && MI->findRegisterDefOperandIdx(FP, TRI, false, true) != -1)
4822 FPLive = false;
4823 if (FP && MI->findRegisterUseOperandIdx(FP, TRI, false) != -1)
4824 FPLive = true;
4825 if (BPLive && MI->findRegisterDefOperandIdx(BP, TRI, false, true) != -1)
4826 BPLive = false;
4827 if (BP && MI->findRegisterUseOperandIdx(BP, TRI, false) != -1)
4828 BPLive = true;
4829
4830 DefMI = MI++;
4831 } while ((MI != ME) &&
4832 (FPLive || BPLive ||
4833 isFPBPAccess(*MI, FP, BP, TRI, AccessFP, AccessBP)));
4834
4835 // Don't need to save/restore if FP is accessed through llvm.frameaddress.
4836 if (FPLive && !SpillBP)
4837 continue;
4838
4839 // If the bp is clobbered by a call, we should save and restore outside of
4840 // the frame setup instructions.
4841 if (KillMI->isCall() && DefMI != ME) {
4842 auto FrameSetup = std::next(DefMI);
4843 // Look for frame setup instruction toward the start of the BB.
4844 // If we reach another call instruction, it means no frame setup
4845 // instruction for the current call instruction.
4846 while (FrameSetup != ME && !TII.isFrameSetup(*FrameSetup) &&
4847 !FrameSetup->isCall())
4848 ++FrameSetup;
4849 // If a frame setup instruction is found, we need to find out the
4850 // corresponding frame destroy instruction.
4851 if (FrameSetup != ME && TII.isFrameSetup(*FrameSetup) &&
4852 (TII.getFrameSize(*FrameSetup) ||
4853 TII.getFrameAdjustment(*FrameSetup))) {
4854 while (!TII.isFrameInstr(*KillMI))
4855 --KillMI;
4856 DefMI = FrameSetup;
4857 MI = DefMI;
4858 ++MI;
4859 }
4860 }
4861
4862 checkInterferedAccess(MF, DefMI, KillMI, SpillFP, SpillBP);
4863
4864 // Call target function to spill and restore FP and BP registers.
4865 saveAndRestoreFPBPUsingSP(MF, &(*DefMI), &(*KillMI), SpillFP, SpillBP);
4866 }
4867 }
4868}
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const uint64_t kSplitStackAvailable
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static bool isTailCallOpcode(unsigned Opc)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static constexpr MCPhysReg FPReg
static constexpr MCPhysReg SPReg
This file declares the machine register scavenger class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
static bool is64Bit(const char *name)
static unsigned calculateSetFPREG(uint64_t SPAdjust)
static unsigned GetScratchRegister(bool Is64Bit, bool IsLP64, const MachineFunction &MF, bool Primary)
GetScratchRegister - Get a temp register for performing work in the segmented stack and the Erlang/Hi...
static unsigned getADDriOpcode(bool IsLP64)
static unsigned getPUSH2Opcode(const X86Subtarget &ST)
static unsigned getLEArOpcode(bool IsLP64)
static unsigned getSUBriOpcode(bool IsLP64)
static bool flagsNeedToBePreservedBeforeTheTerminators(const MachineBasicBlock &MBB)
Check if the flags need to be preserved before the terminators.
static bool isFPBPAccess(const MachineInstr &MI, Register FP, Register BP, const TargetRegisterInfo *TRI, bool &AccessFP, bool &AccessBP)
static const TargetRegisterClass * getCalleeSavedSpillRC(MCRegister Reg, const X86Subtarget &STI, const TargetRegisterInfo &TRI)
static bool isOpcodeRep(unsigned Opcode)
Return true if an opcode is part of the REP group of instructions.
static unsigned getANDriOpcode(bool IsLP64, int64_t Imm)
static bool isEAXLiveIn(MachineBasicBlock &MBB)
static int computeFPBPAlignmentGap(MachineFunction &MF, const TargetRegisterClass *RC, unsigned NumSpilledRegs)
static unsigned getADDrrOpcode(bool IsLP64)
static bool HasNestArgument(const MachineFunction *MF)
static unsigned getPOPOpcode(const X86Subtarget &ST)
static bool isInvoke(const MachineInstr &MI, bool InsideEHLabels)
static unsigned getPOP2Opcode(const X86Subtarget &ST)
static unsigned getHiPELiteral(NamedMDNode *HiPELiteralsMD, const StringRef LiteralName)
Lookup an ERTS parameter in the !hipe.literals named metadata node.
static bool blockEndIsUnreachable(const MachineBasicBlock &MBB, MachineBasicBlock::const_iterator MBBI)
static unsigned getSUBrrOpcode(bool IsLP64)
static unsigned getPUSHOpcode(const X86Subtarget &ST)
constexpr uint64_t MaxSPChunk
static const unsigned FramePtr
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
reverse_iterator rend() const
Definition ArrayRef.h:133
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
reverse_iterator rbegin() const
Definition ArrayRef.h:132
BitVector & reset()
Reset all bits in the bitvector.
Definition BitVector.h:409
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
Definition BitVector.h:317
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
int find_next(unsigned Prev) const
Returns the index of the next set bit following the "Prev" bit.
Definition BitVector.h:324
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
static constexpr BranchProbability getOne()
static constexpr BranchProbability getZero()
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
This is the shared class of boolean and integer constants.
Definition Constants.h:87
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
A debug info location.
Definition DebugLoc.h:126
unsigned size() const
Definition DenseMap.h:172
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition Function.h:882
Constant * getPersonalityFn() const
Get the personality function associated with this function.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:328
size_t arg_size() const
Definition Function.h:878
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition Function.h:666
const Argument * const_arg_iterator
Definition Function.h:74
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition Function.h:229
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
Module * getParent()
Get the module that this global value is contained inside of...
bool usesWindowsCFI() const
Definition MCAsmInfo.h:675
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition MCDwarf.h:635
static MCCFIInstruction createGnuArgsSize(MCSymbol *L, int64_t Size, SMLoc Loc={})
A special wrapper for .cfi_escape that indicates GNU_ARGS_SIZE.
Definition MCDwarf.h:765
static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_restore says that the rule for Register is now the same as it was at the beginning of the functi...
Definition MCDwarf.h:725
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition MCDwarf.h:628
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition MCDwarf.h:670
static MCCFIInstruction createRememberState(MCSymbol *L, SMLoc Loc={})
.cfi_remember_state Save all current rules for all registers.
Definition MCDwarf.h:745
OpType getOperation() const
Definition MCDwarf.h:833
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition MCDwarf.h:643
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals, SMLoc Loc={}, StringRef Comment="")
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
Definition MCDwarf.h:756
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int64_t Adjustment, SMLoc Loc={})
.cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but Offset is a relative value that is added/subt...
Definition MCDwarf.h:651
static MCCFIInstruction createRestoreState(MCSymbol *L, SMLoc Loc={})
.cfi_restore_state Restore the previously saved state.
Definition MCDwarf.h:750
const MCObjectFileInfo * getObjectFileInfo() const
Definition MCContext.h:413
const MCRegisterInfo * getRegisterInfo() const
Definition MCContext.h:411
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
MCSection * getCompactUnwindSection() const
MCRegAliasIterator enumerates all registers aliasing Reg.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
virtual int64_t getDwarfRegNum(MCRegister Reg, bool isEH) const
Map a target register to an equivalent dwarf register number.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Metadata node.
Definition Metadata.h:1069
A single uniqued string.
Definition Metadata.h:722
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:633
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
MachineInstrBundleIterator< const MachineInstr > const_iterator
iterator_range< livein_iterator > liveins() const
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI, MCRegister Reg, const_iterator Before, unsigned Neighborhood=10) const
Return whether (physical) register Reg has been defined and not killed as of just before Before.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
@ LQR_Live
Register is known to be (at least partially) live.
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool needsSplitStackProlog() const
Return true if this function requires a split stack prolog, even if it uses no stack space.
LLVM_ABI int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
LLVM_ABI void ensureMaxAlignment(Align Alignment)
Make sure the function is at least Align bytes aligned.
bool hasCalls() const
Return true if the current function has any function calls.
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
Align getMaxAlign() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
bool hasPatchPoint() const
This method may be called any time after instruction selection is complete to determine if there is a...
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
void setCVBytesOfCalleeSavedRegisters(unsigned S)
LLVM_ABI uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool hasStackMap() const
This method may be called any time after instruction selection is complete to determine if there is a...
LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment, TargetStackID::Value StackID=TargetStackID::Default)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
bool hasCopyImplyingStackAdjustment() const
Returns true if the function contains operations which will lower down to instructions which manipula...
bool hasStackObjects() const
Return true if there are any stack objects in this function.
LLVM_ABI int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
int getObjectIndexBegin() const
Return the minimum frame object index.
void setOffsetAdjustment(int64_t Adj)
Set the correction for frame offsets.
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
unsigned addFrameInst(const MCCFIInstruction &Inst)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const std::vector< MCCFIInstruction > & getFrameInstructions() const
Returns a reference to a list of cfi instructions in the function's prologue.
bool hasInlineAsm() const
Returns true if the function contains any inline assembly.
void makeDebugValueSubstitution(DebugInstrOperandPair, DebugInstrOperandPair, unsigned SubReg=0)
Create a substitution between one <instr,operand> value to a different, new value.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
void push_front(MachineBasicBlock *MBB)
const char * createExternalSymbolName(StringRef Name)
Allocate a string and populate it with the given external symbol name.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool verify(Pass *p=nullptr, const char *Banner=nullptr, raw_ostream *OS=nullptr, bool AbortOnError=true) const
Run the current MachineFunction through the machine code verifier, useful for debugger use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
BasicBlockListType::iterator iterator
bool shouldSplitStack() const
Should we be emitting segmented stack stuff for the function.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
unsigned getNumOperands() const
Retuns the total number of operands.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI unsigned getDebugInstrNum()
Fetch the instruction number of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
@ MOVolatile
The memory access is volatile.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
MachineBasicBlock * getMBB() const
void setIsDead(bool Val=true)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI bool isLiveIn(Register Reg) const
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
Definition Module.cpp:301
WinX64EHUnwindMode getWinX64EHUnwindMode() const
Get how unwind information should be generated for x64 Windows.
Definition Module.cpp:976
unsigned getCodeViewFlag() const
Returns the CodeView Version by checking module flags.
Definition Module.cpp:617
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
iterator end() const
Definition ArrayRef.h:339
iterator begin() const
Definition ArrayRef.h:338
A tuple of MDNodes.
Definition Metadata.h:1755
LLVM_ABI MDNode * getOperand(unsigned i) const
LLVM_ABI unsigned getNumOperands() const
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:68
StringRef str() const
Explicit conversion to StringRef.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:46
static StackOffset getFixed(int64_t Fixed)
Definition TypeSize.h:39
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static constexpr size_t npos
Definition StringRef.h:58
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
TargetFrameLowering(StackDirection D, Align StackAl, int LAO, Align TransAl=Align(1), bool StackReal=true)
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
const Triple & getTargetTriple() const
const MCAsmInfo & getMCAsmInfo() const
Return target specific asm information.
TargetOptions Options
CodeModel::Model getCodeModel() const
Returns the code model.
SwiftAsyncFramePointerMode SwiftAsyncFramePointer
Control when and how the Swift async frame pointer bit should be set.
LLVM_ABI bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual Register getFrameRegister(const MachineFunction &MF) const =0
Debug information queries.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
bool isUEFI() const
Tests whether the OS is UEFI.
Definition Triple.h:772
bool isOSWindows() const
Tests whether the OS is Windows.
Definition Triple.h:775
Value wrapper in the Metadata hierarchy.
Definition Metadata.h:459
Value * getValue() const
Definition Metadata.h:499
bool has128ByteRedZone(const MachineFunction &MF) const
Return true if the function has a redzone (accessible bytes past the frame of the top of stack functi...
void spillFPBP(MachineFunction &MF) const override
If a function uses base pointer and the base pointer is clobbered by inline asm, RA doesn't detect th...
bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override
canSimplifyCallFramePseudos - If there is a reserved call frame, the call frame pseudos can be simpli...
bool needsFrameIndexResolution(const MachineFunction &MF) const override
X86FrameLowering(const X86Subtarget &STI, MaybeAlign StackAlignOverride)
const X86RegisterInfo * TRI
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
bool hasFPImpl(const MachineFunction &MF) const override
hasFPImpl - Return true if the specified function should have a dedicated frame pointer register.
MachineBasicBlock::iterator restoreWin32EHStackPointers(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool RestoreSP=false) const
Sets up EBP and optionally ESI based on the incoming EBP value.
int getInitialCFAOffset(const MachineFunction &MF) const override
Return initial CFA offset value i.e.
bool canUseAsPrologue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a prologue for the target.
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog, std::optional< MachineFunction::DebugInstrOperandPair > InstrNum=std::nullopt) const
Emit target stack probe code.
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool IsPrologue) const
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int64_t mergeSPAdd(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, int64_t AddOffset, bool doMergeWithPrevious) const
Equivalent to: mergeSPUpdates(MBB, MBBI, [AddOffset](int64_t Offset) { return AddOffset + Offset; }...
StackOffset getFrameIndexReferenceSP(const MachineFunction &MF, int FI, Register &SPReg, int Adjustment) const
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
bool enableShrinkWrapping(const MachineFunction &MF) const override
Returns true if the target will correctly handle shrink wrapping.
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
void inlineStackProbe(MachineFunction &MF, MachineBasicBlock &PrologMBB) const override
Replace a StackProbe inline-stub with the actual probe code inline.
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
const X86InstrInfo & TII
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const DebugLoc &DL, int64_t NumBytes, bool InEpilogue) const
Emit a series of instructions to increment / decrement the stack pointer by a constant value.
bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override
Check whether or not the given MBB can be used as a epilogue for the target.
bool Is64Bit
Is64Bit implies that x86_64 instructions are available.
Register getInitialCFARegister(const MachineFunction &MF) const override
Return initial CFA register value i.e.
bool Uses64BitFramePtr
True if the 64-bit frame or stack pointer should be used.
unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override
void adjustForSegmentedStacks(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Adjust the prologue to have the function use segmented stacks.
DwarfFrameBase getDwarfFrameBase(const MachineFunction &MF) const override
Return the frame base information to be encoded in the DWARF subprogram debug info.
void emitCalleeSavedFrameMovesFullCFA(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const override
Emits Dwarf Info specifying offsets of callee saved registers and frame pointer.
int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI, Register &SPReg) const
bool canUseLEAForSPInEpilogue(const MachineFunction &MF) const
Check that LEA can be used on SP in an epilogue sequence for MF.
bool stackProbeFunctionModifiesSP() const override
Does the stack probe function call return with a modified stack pointer?
void orderFrameObjects(const MachineFunction &MF, SmallVectorImpl< int > &ObjectsToAllocate) const override
Order the symbols in the local stack.
void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, const MCCFIInstruction &CFIInst, MachineInstr::MIFlag Flag=MachineInstr::NoFlags) const
Wraps up getting a CFI index and building a MachineInstr for it.
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
void processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameIndicesReplaced - This method is called immediately before MO_FrameIndex op...
StackOffset getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI, Register &FrameReg, bool IgnoreSPUpdates) const override
Same as getFrameIndexReference, except that the stack pointer (as opposed to the frame pointer) will ...
void restoreWinEHStackPointersInParent(MachineFunction &MF) const
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
void adjustForHiPEPrologue(MachineFunction &MF, MachineBasicBlock &PrologueMBB) const override
Erlang programs may need a special prologue to handle the stack size they might need at runtime.
const X86Subtarget & STI
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
bool isCandidateForPush2Pop2(Register Reg) const
void setRestoreBasePointer(const MachineFunction *MF)
DenseMap< int, unsigned > & getWinEHXMMSlotInfo()
MachineInstr * getStackPtrSaveMI() const
AMXProgModelEnum getAMXProgModel() const
void setStackPtrSaveMI(MachineInstr *MI)
void setCalleeSavedFrameSize(unsigned bytes)
const X86TargetLowering * getTargetLowering() const override
bool isTargetWindowsCoreCLR() const
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
uint16_t StackAdjustment(const RuntimeFunction &RF)
StackAdjustment - calculated stack adjustment in words.
Definition ARMWinEH.h:200
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ HiPE
Used by the High-Performance Erlang Compiler (HiPE).
Definition CallingConv.h:53
@ X86_INTR
x86 hardware interrupt context.
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition CallingConv.h:76
@ X86_FastCall
'fast' analog of X86_StdCall.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ MO_GOTPCREL
MO_GOTPCREL - On a symbol operand this indicates that the immediate is offset to the GOT entry for th...
unsigned getMOVriOpcode(bool Use64BitReg, int64_t Imm)
Return a MOVri opcode for materializing Imm into a 32- or 64-bit GPR.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
void stable_sort(R &&Range)
Definition STLExtras.h:2116
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
@ Define
Register definition.
constexpr RegState getKillRegState(bool B)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:134
MCRegister getX86SubSuperRegister(MCRegister Reg, unsigned Size, bool High=false)
@ DwarfCFI
DWARF-like instruction based exceptions.
Definition CodeGen.h:56
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:547
IterT skipDebugInstructionsForward(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
static bool isFuncletReturnInstr(const MachineInstr &MI)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
@ DeploymentBased
Determine whether to set the bit statically or dynamically based on the deployment target.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
constexpr RegState getDefRegState(bool B)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
bool requireWinX64UnwindV3(const MachineFunction &MF)
Returns true when MF must use Windows x64 Unwind V3: the module is in V3 mode, or the function needs ...
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
IterT skipDebugInstructionsBackward(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition LEB128.h:24
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2019
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
static const MachineInstrBuilder & addRegOffset(const MachineInstrBuilder &MIB, Register Reg, bool isKill, int Offset)
addRegOffset - This function is used to add a memory reference of the form [Reg + Offset],...
constexpr RegState getUndefRegState(bool B)
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Pair of physical register and lane mask.
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
union llvm::TargetFrameLowering::DwarfFrameBase::@004076321055032247336074224075335064105264310375 Location
enum llvm::TargetFrameLowering::DwarfFrameBase::FrameBaseKind Kind
SmallVector< WinEHTryBlockMapEntry, 4 > TryBlockMap
SmallVector< WinEHHandlerType, 1 > HandlerArray