Mercurial > hg > Members > tobaru > cbc > CbC_llvm
comparison lib/Target/X86/X86FrameLowering.cpp @ 121:803732b1fca8
LLVM 5.0
author | kono |
---|---|
date | Fri, 27 Oct 2017 17:07:41 +0900 |
parents | 1172e4bd9c6f |
children |
comparison
equal
deleted
inserted
replaced
120:1172e4bd9c6f | 121:803732b1fca8 |
---|---|
27 #include "llvm/CodeGen/WinEHFuncInfo.h" | 27 #include "llvm/CodeGen/WinEHFuncInfo.h" |
28 #include "llvm/IR/DataLayout.h" | 28 #include "llvm/IR/DataLayout.h" |
29 #include "llvm/IR/Function.h" | 29 #include "llvm/IR/Function.h" |
30 #include "llvm/MC/MCAsmInfo.h" | 30 #include "llvm/MC/MCAsmInfo.h" |
31 #include "llvm/MC/MCSymbol.h" | 31 #include "llvm/MC/MCSymbol.h" |
32 #include "llvm/Support/Debug.h" | |
32 #include "llvm/Target/TargetOptions.h" | 33 #include "llvm/Target/TargetOptions.h" |
33 #include "llvm/Support/Debug.h" | |
34 #include <cstdlib> | 34 #include <cstdlib> |
35 | 35 |
36 using namespace llvm; | 36 using namespace llvm; |
37 | 37 |
38 X86FrameLowering::X86FrameLowering(const X86Subtarget &STI, | 38 X86FrameLowering::X86FrameLowering(const X86Subtarget &STI, |
81 /// hasFP - Return true if the specified function should have a dedicated frame | 81 /// hasFP - Return true if the specified function should have a dedicated frame |
82 /// pointer register. This is true if the function has variable sized allocas | 82 /// pointer register. This is true if the function has variable sized allocas |
83 /// or if frame pointer elimination is disabled. | 83 /// or if frame pointer elimination is disabled. |
84 bool X86FrameLowering::hasFP(const MachineFunction &MF) const { | 84 bool X86FrameLowering::hasFP(const MachineFunction &MF) const { |
85 const MachineFrameInfo &MFI = MF.getFrameInfo(); | 85 const MachineFrameInfo &MFI = MF.getFrameInfo(); |
86 const MachineModuleInfo &MMI = MF.getMMI(); | |
87 | |
88 return (MF.getTarget().Options.DisableFramePointerElim(MF) || | 86 return (MF.getTarget().Options.DisableFramePointerElim(MF) || |
89 TRI->needsStackRealignment(MF) || | 87 TRI->needsStackRealignment(MF) || |
90 MFI.hasVarSizedObjects() || | 88 MFI.hasVarSizedObjects() || |
91 MFI.isFrameAddressTaken() || MFI.hasOpaqueSPAdjustment() || | 89 MFI.isFrameAddressTaken() || MFI.hasOpaqueSPAdjustment() || |
92 MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() || | 90 MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() || |
93 MMI.callsUnwindInit() || MMI.hasEHFunclets() || MMI.callsEHReturn() || | 91 MF.callsUnwindInit() || MF.hasEHFunclets() || MF.callsEHReturn() || |
94 MFI.hasStackMap() || MFI.hasPatchPoint() || | 92 MFI.hasStackMap() || MFI.hasPatchPoint() || |
95 MFI.hasCopyImplyingStackAdjustment()); | 93 MFI.hasCopyImplyingStackAdjustment()); |
96 } | 94 } |
97 | 95 |
98 static unsigned getSUBriOpcode(unsigned IsLP64, int64_t Imm) { | 96 static unsigned getSUBriOpcode(unsigned IsLP64, int64_t Imm) { |
149 MachineBasicBlock::iterator &MBBI, | 147 MachineBasicBlock::iterator &MBBI, |
150 const X86RegisterInfo *TRI, | 148 const X86RegisterInfo *TRI, |
151 bool Is64Bit) { | 149 bool Is64Bit) { |
152 const MachineFunction *MF = MBB.getParent(); | 150 const MachineFunction *MF = MBB.getParent(); |
153 const Function *F = MF->getFunction(); | 151 const Function *F = MF->getFunction(); |
154 if (!F || MF->getMMI().callsEHReturn()) | 152 if (!F || MF->callsEHReturn()) |
155 return 0; | 153 return 0; |
156 | 154 |
157 const TargetRegisterClass &AvailableRegs = *TRI->getGPRsForTailCall(*MF); | 155 const TargetRegisterClass &AvailableRegs = *TRI->getGPRsForTailCall(*MF); |
158 | 156 |
159 if (MBBI == MBB.end()) | 157 if (MBBI == MBB.end()) |
252 void X86FrameLowering::emitSPUpdate(MachineBasicBlock &MBB, | 250 void X86FrameLowering::emitSPUpdate(MachineBasicBlock &MBB, |
253 MachineBasicBlock::iterator &MBBI, | 251 MachineBasicBlock::iterator &MBBI, |
254 int64_t NumBytes, bool InEpilogue) const { | 252 int64_t NumBytes, bool InEpilogue) const { |
255 bool isSub = NumBytes < 0; | 253 bool isSub = NumBytes < 0; |
256 uint64_t Offset = isSub ? -NumBytes : NumBytes; | 254 uint64_t Offset = isSub ? -NumBytes : NumBytes; |
255 MachineInstr::MIFlag Flag = | |
256 isSub ? MachineInstr::FrameSetup : MachineInstr::FrameDestroy; | |
257 | 257 |
258 uint64_t Chunk = (1LL << 31) - 1; | 258 uint64_t Chunk = (1LL << 31) - 1; |
259 DebugLoc DL = MBB.findDebugLoc(MBBI); | 259 DebugLoc DL = MBB.findDebugLoc(MBBI); |
260 | 260 |
261 if (Offset > Chunk) { | |
262 // Rather than emit a long series of instructions for large offsets, | |
263 // load the offset into a register and do one sub/add | |
264 unsigned Reg = 0; | |
265 unsigned Rax = (unsigned)(Is64Bit ? X86::RAX : X86::EAX); | |
266 | |
267 if (isSub && !isEAXLiveIn(MBB)) | |
268 Reg = Rax; | |
269 else | |
270 Reg = findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); | |
271 | |
272 unsigned MovRIOpc = Is64Bit ? X86::MOV64ri : X86::MOV32ri; | |
273 unsigned AddSubRROpc = | |
274 isSub ? getSUBrrOpcode(Is64Bit) : getADDrrOpcode(Is64Bit); | |
275 if (Reg) { | |
276 BuildMI(MBB, MBBI, DL, TII.get(MovRIOpc), Reg) | |
277 .addImm(Offset) | |
278 .setMIFlag(Flag); | |
279 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AddSubRROpc), StackPtr) | |
280 .addReg(StackPtr) | |
281 .addReg(Reg); | |
282 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. | |
283 return; | |
284 } else if (Offset > 8 * Chunk) { | |
285 // If we would need more than 8 add or sub instructions (a >16GB stack | |
286 // frame), it's worth spilling RAX to materialize this immediate. | |
287 // pushq %rax | |
288 // movabsq +-$Offset+-SlotSize, %rax | |
289 // addq %rsp, %rax | |
290 // xchg %rax, (%rsp) | |
291 // movq (%rsp), %rsp | |
292 assert(Is64Bit && "can't have 32-bit 16GB stack frame"); | |
293 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64r)) | |
294 .addReg(Rax, RegState::Kill) | |
295 .setMIFlag(Flag); | |
296 // Subtract is not commutative, so negate the offset and always use add. | |
297 // Subtract 8 less and add 8 more to account for the PUSH we just did. | |
298 if (isSub) | |
299 Offset = -(Offset - SlotSize); | |
300 else | |
301 Offset = Offset + SlotSize; | |
302 BuildMI(MBB, MBBI, DL, TII.get(MovRIOpc), Rax) | |
303 .addImm(Offset) | |
304 .setMIFlag(Flag); | |
305 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(X86::ADD64rr), Rax) | |
306 .addReg(Rax) | |
307 .addReg(StackPtr); | |
308 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. | |
309 // Exchange the new SP in RAX with the top of the stack. | |
310 addRegOffset( | |
311 BuildMI(MBB, MBBI, DL, TII.get(X86::XCHG64rm), Rax).addReg(Rax), | |
312 StackPtr, false, 0); | |
313 // Load new SP from the top of the stack into RSP. | |
314 addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rm), StackPtr), | |
315 StackPtr, false, 0); | |
316 return; | |
317 } | |
318 } | |
319 | |
261 while (Offset) { | 320 while (Offset) { |
262 if (Offset > Chunk) { | |
263 // Rather than emit a long series of instructions for large offsets, | |
264 // load the offset into a register and do one sub/add | |
265 unsigned Reg = 0; | |
266 | |
267 if (isSub && !isEAXLiveIn(MBB)) | |
268 Reg = (unsigned)(Is64Bit ? X86::RAX : X86::EAX); | |
269 else | |
270 Reg = findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); | |
271 | |
272 if (Reg) { | |
273 unsigned Opc = Is64Bit ? X86::MOV64ri : X86::MOV32ri; | |
274 BuildMI(MBB, MBBI, DL, TII.get(Opc), Reg) | |
275 .addImm(Offset); | |
276 Opc = isSub | |
277 ? getSUBrrOpcode(Is64Bit) | |
278 : getADDrrOpcode(Is64Bit); | |
279 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) | |
280 .addReg(StackPtr) | |
281 .addReg(Reg); | |
282 MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. | |
283 Offset = 0; | |
284 continue; | |
285 } | |
286 } | |
287 | |
288 uint64_t ThisVal = std::min(Offset, Chunk); | 321 uint64_t ThisVal = std::min(Offset, Chunk); |
289 if (ThisVal == (Is64Bit ? 8 : 4)) { | 322 if (ThisVal == SlotSize) { |
290 // Use push / pop instead. | 323 // Use push / pop for slot sized adjustments as a size optimization. We |
324 // need to find a dead register when using pop. | |
291 unsigned Reg = isSub | 325 unsigned Reg = isSub |
292 ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX) | 326 ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX) |
293 : findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); | 327 : findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); |
294 if (Reg) { | 328 if (Reg) { |
295 unsigned Opc = isSub | 329 unsigned Opc = isSub |
296 ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r) | 330 ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r) |
297 : (Is64Bit ? X86::POP64r : X86::POP32r); | 331 : (Is64Bit ? X86::POP64r : X86::POP32r); |
298 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opc)) | 332 BuildMI(MBB, MBBI, DL, TII.get(Opc)) |
299 .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub)); | 333 .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub)) |
300 if (isSub) | 334 .setMIFlag(Flag); |
301 MI->setFlag(MachineInstr::FrameSetup); | |
302 else | |
303 MI->setFlag(MachineInstr::FrameDestroy); | |
304 Offset -= ThisVal; | 335 Offset -= ThisVal; |
305 continue; | 336 continue; |
306 } | 337 } |
307 } | 338 } |
308 | 339 |
309 MachineInstrBuilder MI = BuildStackAdjustment( | 340 BuildStackAdjustment(MBB, MBBI, DL, isSub ? -ThisVal : ThisVal, InEpilogue) |
310 MBB, MBBI, DL, isSub ? -ThisVal : ThisVal, InEpilogue); | 341 .setMIFlag(Flag); |
311 if (isSub) | |
312 MI.setMIFlag(MachineInstr::FrameSetup); | |
313 else | |
314 MI.setMIFlag(MachineInstr::FrameDestroy); | |
315 | 342 |
316 Offset -= ThisVal; | 343 Offset -= ThisVal; |
317 } | 344 } |
318 } | 345 } |
319 | 346 |
373 return 0; | 400 return 0; |
374 | 401 |
375 MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI; | 402 MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI; |
376 MachineBasicBlock::iterator NI = doMergeWithPrevious ? nullptr | 403 MachineBasicBlock::iterator NI = doMergeWithPrevious ? nullptr |
377 : std::next(MBBI); | 404 : std::next(MBBI); |
405 PI = skipDebugInstructionsBackward(PI, MBB.begin()); | |
406 if (NI != nullptr) | |
407 NI = skipDebugInstructionsForward(NI, MBB.end()); | |
408 | |
378 unsigned Opc = PI->getOpcode(); | 409 unsigned Opc = PI->getOpcode(); |
379 int Offset = 0; | 410 int Offset = 0; |
380 | 411 |
381 if (!doMergeWithPrevious && NI != MBB.end() && | 412 if (!doMergeWithPrevious && NI != MBB.end() && |
382 NI->getOpcode() == TargetOpcode::CFI_INSTRUCTION) { | 413 NI->getOpcode() == TargetOpcode::CFI_INSTRUCTION) { |
416 void X86FrameLowering::BuildCFI(MachineBasicBlock &MBB, | 447 void X86FrameLowering::BuildCFI(MachineBasicBlock &MBB, |
417 MachineBasicBlock::iterator MBBI, | 448 MachineBasicBlock::iterator MBBI, |
418 const DebugLoc &DL, | 449 const DebugLoc &DL, |
419 const MCCFIInstruction &CFIInst) const { | 450 const MCCFIInstruction &CFIInst) const { |
420 MachineFunction &MF = *MBB.getParent(); | 451 MachineFunction &MF = *MBB.getParent(); |
421 unsigned CFIIndex = MF.getMMI().addFrameInst(CFIInst); | 452 unsigned CFIIndex = MF.addFrameInst(CFIInst); |
422 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) | 453 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
423 .addCFIIndex(CFIIndex); | 454 .addCFIIndex(CFIIndex); |
424 } | 455 } |
425 | 456 |
426 void X86FrameLowering::emitCalleeSavedFrameMoves( | 457 void X86FrameLowering::emitCalleeSavedFrameMoves( |
715 if (Is64Bit) | 746 if (Is64Bit) |
716 CallOp = IsLargeCodeModel ? X86::CALL64r : X86::CALL64pcrel32; | 747 CallOp = IsLargeCodeModel ? X86::CALL64r : X86::CALL64pcrel32; |
717 else | 748 else |
718 CallOp = X86::CALLpcrel32; | 749 CallOp = X86::CALLpcrel32; |
719 | 750 |
720 const char *Symbol; | 751 StringRef Symbol = STI.getTargetLowering()->getStackProbeSymbolName(MF); |
721 if (Is64Bit) { | |
722 if (STI.isTargetCygMing()) { | |
723 Symbol = "___chkstk_ms"; | |
724 } else { | |
725 Symbol = "__chkstk"; | |
726 } | |
727 } else if (STI.isTargetCygMing()) | |
728 Symbol = "_alloca"; | |
729 else | |
730 Symbol = "_chkstk"; | |
731 | 752 |
732 MachineInstrBuilder CI; | 753 MachineInstrBuilder CI; |
733 MachineBasicBlock::iterator ExpansionMBBI = std::prev(MBBI); | 754 MachineBasicBlock::iterator ExpansionMBBI = std::prev(MBBI); |
734 | 755 |
735 // All current stack probes take AX and SP as input, clobber flags, and | 756 // All current stack probes take AX and SP as input, clobber flags, and |
736 // preserve all registers. x86_64 probes leave RSP unmodified. | 757 // preserve all registers. x86_64 probes leave RSP unmodified. |
737 if (Is64Bit && MF.getTarget().getCodeModel() == CodeModel::Large) { | 758 if (Is64Bit && MF.getTarget().getCodeModel() == CodeModel::Large) { |
738 // For the large code model, we have to call through a register. Use R11, | 759 // For the large code model, we have to call through a register. Use R11, |
739 // as it is scratch in all supported calling conventions. | 760 // as it is scratch in all supported calling conventions. |
740 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::R11) | 761 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::R11) |
741 .addExternalSymbol(Symbol); | 762 .addExternalSymbol(MF.createExternalSymbolName(Symbol)); |
742 CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)).addReg(X86::R11); | 763 CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)).addReg(X86::R11); |
743 } else { | 764 } else { |
744 CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)).addExternalSymbol(Symbol); | 765 CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)) |
766 .addExternalSymbol(MF.createExternalSymbolName(Symbol)); | |
745 } | 767 } |
746 | 768 |
747 unsigned AX = Is64Bit ? X86::RAX : X86::EAX; | 769 unsigned AX = Is64Bit ? X86::RAX : X86::EAX; |
748 unsigned SP = Is64Bit ? X86::RSP : X86::ESP; | 770 unsigned SP = Is64Bit ? X86::RSP : X86::ESP; |
749 CI.addReg(AX, RegState::Implicit) | 771 CI.addReg(AX, RegState::Implicit) |
750 .addReg(SP, RegState::Implicit) | 772 .addReg(SP, RegState::Implicit) |
751 .addReg(AX, RegState::Define | RegState::Implicit) | 773 .addReg(AX, RegState::Define | RegState::Implicit) |
752 .addReg(SP, RegState::Define | RegState::Implicit) | 774 .addReg(SP, RegState::Define | RegState::Implicit) |
753 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); | 775 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); |
754 | 776 |
755 if (Is64Bit) { | 777 if (STI.isTargetWin64() || !STI.isOSWindows()) { |
778 // MSVC x32's _chkstk and cygwin/mingw's _alloca adjust %esp themselves. | |
756 // MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp | 779 // MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp |
757 // themselves. It also does not clobber %rax so we can reuse it when | 780 // themselves. They also does not clobber %rax so we can reuse it when |
758 // adjusting %rsp. | 781 // adjusting %rsp. |
759 BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64rr), X86::RSP) | 782 // All other platforms do not specify a particular ABI for the stack probe |
760 .addReg(X86::RSP) | 783 // function, so we arbitrarily define it to not adjust %esp/%rsp itself. |
761 .addReg(X86::RAX); | 784 BuildMI(MBB, MBBI, DL, TII.get(getSUBrrOpcode(Is64Bit)), SP) |
785 .addReg(SP) | |
786 .addReg(AX); | |
762 } | 787 } |
763 | 788 |
764 if (InProlog) { | 789 if (InProlog) { |
765 // Apply the frame setup flag to all inserted instrs. | 790 // Apply the frame setup flag to all inserted instrs. |
766 for (++ExpansionMBBI; ExpansionMBBI != MBBI; ++ExpansionMBBI) | 791 for (++ExpansionMBBI; ExpansionMBBI != MBBI; ++ExpansionMBBI) |
897 [for all callee-saved registers] | 922 [for all callee-saved registers] |
898 .cfi_offset %<reg>, (offset from %rsp) | 923 .cfi_offset %<reg>, (offset from %rsp) |
899 | 924 |
900 Notes: | 925 Notes: |
901 - .seh directives are emitted only for Windows 64 ABI | 926 - .seh directives are emitted only for Windows 64 ABI |
927 - .cv_fpo directives are emitted on win32 when emitting CodeView | |
902 - .cfi directives are emitted for all other ABIs | 928 - .cfi directives are emitted for all other ABIs |
903 - for 32-bit code, substitute %e?? registers for %r?? | 929 - for 32-bit code, substitute %e?? registers for %r?? |
904 */ | 930 */ |
905 | 931 |
906 void X86FrameLowering::emitPrologue(MachineFunction &MF, | 932 void X86FrameLowering::emitPrologue(MachineFunction &MF, |
917 bool IsFunclet = MBB.isEHFuncletEntry(); | 943 bool IsFunclet = MBB.isEHFuncletEntry(); |
918 EHPersonality Personality = EHPersonality::Unknown; | 944 EHPersonality Personality = EHPersonality::Unknown; |
919 if (Fn->hasPersonalityFn()) | 945 if (Fn->hasPersonalityFn()) |
920 Personality = classifyEHPersonality(Fn->getPersonalityFn()); | 946 Personality = classifyEHPersonality(Fn->getPersonalityFn()); |
921 bool FnHasClrFunclet = | 947 bool FnHasClrFunclet = |
922 MMI.hasEHFunclets() && Personality == EHPersonality::CoreCLR; | 948 MF.hasEHFunclets() && Personality == EHPersonality::CoreCLR; |
923 bool IsClrFunclet = IsFunclet && FnHasClrFunclet; | 949 bool IsClrFunclet = IsFunclet && FnHasClrFunclet; |
924 bool HasFP = hasFP(MF); | 950 bool HasFP = hasFP(MF); |
925 bool IsWin64CC = STI.isCallingConvWin64(Fn->getCallingConv()); | 951 bool IsWin64CC = STI.isCallingConvWin64(Fn->getCallingConv()); |
926 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); | 952 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); |
927 bool NeedsWinCFI = IsWin64Prologue && Fn->needsUnwindTableEntry(); | 953 bool NeedsWin64CFI = IsWin64Prologue && Fn->needsUnwindTableEntry(); |
954 // FIXME: Emit FPO data for EH funclets. | |
955 bool NeedsWinFPO = | |
956 !IsFunclet && STI.isTargetWin32() && MMI.getModule()->getCodeViewFlag(); | |
957 bool NeedsWinCFI = NeedsWin64CFI || NeedsWinFPO; | |
928 bool NeedsDwarfCFI = | 958 bool NeedsDwarfCFI = |
929 !IsWin64Prologue && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); | 959 !IsWin64Prologue && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); |
930 unsigned FramePtr = TRI->getFrameRegister(MF); | 960 unsigned FramePtr = TRI->getFrameRegister(MF); |
931 const unsigned MachineFramePtr = | 961 const unsigned MachineFramePtr = |
932 STI.isTarget64BitILP32() | 962 STI.isTarget64BitILP32() |
933 ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; | 963 ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; |
934 unsigned BasePtr = TRI->getBaseRegister(); | 964 unsigned BasePtr = TRI->getBaseRegister(); |
935 bool HasWinCFI = false; | 965 bool HasWinCFI = false; |
936 | 966 |
937 // Debug location must be unknown since the first debug location is used | 967 // Debug location must be unknown since the first debug location is used |
938 // to determine the end of the prologue. | 968 // to determine the end of the prologue. |
939 DebugLoc DL; | 969 DebugLoc DL; |
940 | 970 |
941 // Add RETADDR move area to callee saved frame size. | 971 // Add RETADDR move area to callee saved frame size. |
945 | 975 |
946 if (TailCallReturnAddrDelta < 0) | 976 if (TailCallReturnAddrDelta < 0) |
947 X86FI->setCalleeSavedFrameSize( | 977 X86FI->setCalleeSavedFrameSize( |
948 X86FI->getCalleeSavedFrameSize() - TailCallReturnAddrDelta); | 978 X86FI->getCalleeSavedFrameSize() - TailCallReturnAddrDelta); |
949 | 979 |
950 bool UseStackProbe = (STI.isOSWindows() && !STI.isTargetMachO()); | 980 bool UseStackProbe = !STI.getTargetLowering()->getStackProbeSymbolName(MF).empty(); |
951 | 981 |
952 // The default stack probe size is 4096 if the function has no stackprobesize | 982 // The default stack probe size is 4096 if the function has no stackprobesize |
953 // attribute. | 983 // attribute. |
954 unsigned StackProbeSize = 4096; | 984 unsigned StackProbeSize = 4096; |
955 if (Fn->hasFnAttribute("stack-probe-size")) | 985 if (Fn->hasFnAttribute("stack-probe-size")) |
956 Fn->getFnAttribute("stack-probe-size") | 986 Fn->getFnAttribute("stack-probe-size") |
957 .getValueAsString() | 987 .getValueAsString() |
958 .getAsInteger(0, StackProbeSize); | 988 .getAsInteger(0, StackProbeSize); |
989 | |
990 // Re-align the stack on 64-bit if the x86-interrupt calling convention is | |
991 // used and an error code was pushed, since the x86-64 ABI requires a 16-byte | |
992 // stack alignment. | |
993 if (Fn->getCallingConv() == CallingConv::X86_INTR && Is64Bit && | |
994 Fn->arg_size() == 2) { | |
995 StackSize += 8; | |
996 MFI.setStackSize(StackSize); | |
997 emitSPUpdate(MBB, MBBI, -8, /*InEpilogue=*/false); | |
998 } | |
959 | 999 |
960 // If this is x86-64 and the Red Zone is not disabled, if we are a leaf | 1000 // If this is x86-64 and the Red Zone is not disabled, if we are a leaf |
961 // function, and use up to 128 bytes of stack space, don't have a frame | 1001 // function, and use up to 128 bytes of stack space, don't have a frame |
962 // pointer, calls, or dynamic alloca then we do not need to adjust the | 1002 // pointer, calls, or dynamic alloca then we do not need to adjust the |
963 // stack pointer (we fit in the Red Zone). We also check that we don't | 1003 // stack pointer (we fit in the Red Zone). We also check that we don't |
964 // push and pop from the stack. | 1004 // push and pop from the stack. |
965 if (Is64Bit && !Fn->hasFnAttribute(Attribute::NoRedZone) && | 1005 if (Is64Bit && !Fn->hasFnAttribute(Attribute::NoRedZone) && |
966 !TRI->needsStackRealignment(MF) && | 1006 !TRI->needsStackRealignment(MF) && |
967 !MFI.hasVarSizedObjects() && // No dynamic alloca. | 1007 !MFI.hasVarSizedObjects() && // No dynamic alloca. |
968 !MFI.adjustsStack() && // No calls. | 1008 !MFI.adjustsStack() && // No calls. |
1009 !UseStackProbe && // No stack probes. | |
969 !IsWin64CC && // Win64 has no Red Zone | 1010 !IsWin64CC && // Win64 has no Red Zone |
970 !MFI.hasCopyImplyingStackAdjustment() && // Don't push and pop. | 1011 !MFI.hasCopyImplyingStackAdjustment() && // Don't push and pop. |
971 !MF.shouldSplitStack()) { // Regular stack | 1012 !MF.shouldSplitStack()) { // Regular stack |
972 uint64_t MinSize = X86FI->getCalleeSavedFrameSize(); | 1013 uint64_t MinSize = X86FI->getCalleeSavedFrameSize(); |
973 if (HasFP) MinSize += SlotSize; | 1014 if (HasFP) MinSize += SlotSize; |
1019 .setMIFlag(MachineInstr::FrameSetup); | 1060 .setMIFlag(MachineInstr::FrameSetup); |
1020 MBB.addLiveIn(Establisher); | 1061 MBB.addLiveIn(Establisher); |
1021 } | 1062 } |
1022 | 1063 |
1023 if (HasFP) { | 1064 if (HasFP) { |
1065 assert(MF.getRegInfo().isReserved(MachineFramePtr) && "FP reserved"); | |
1066 | |
1024 // Calculate required stack adjustment. | 1067 // Calculate required stack adjustment. |
1025 uint64_t FrameSize = StackSize - SlotSize; | 1068 uint64_t FrameSize = StackSize - SlotSize; |
1026 // If required, include space for extra hidden slot for stashing base pointer. | 1069 // If required, include space for extra hidden slot for stashing base pointer. |
1027 if (X86FI->getRestoreBasePointer()) | 1070 if (X86FI->getRestoreBasePointer()) |
1028 FrameSize += SlotSize; | 1071 FrameSize += SlotSize; |
1080 // Define the current CFA to use the EBP/RBP register. | 1123 // Define the current CFA to use the EBP/RBP register. |
1081 unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); | 1124 unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); |
1082 BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaRegister( | 1125 BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaRegister( |
1083 nullptr, DwarfFramePtr)); | 1126 nullptr, DwarfFramePtr)); |
1084 } | 1127 } |
1085 } | 1128 |
1086 | 1129 if (NeedsWinFPO) { |
1087 // Mark the FramePtr as live-in in every block. Don't do this again for | 1130 // .cv_fpo_setframe $FramePtr |
1088 // funclet prologues. | 1131 HasWinCFI = true; |
1089 if (!IsFunclet) { | 1132 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame)) |
1090 for (MachineBasicBlock &EveryMBB : MF) | 1133 .addImm(FramePtr) |
1091 EveryMBB.addLiveIn(MachineFramePtr); | 1134 .addImm(0) |
1135 .setMIFlag(MachineInstr::FrameSetup); | |
1136 } | |
1092 } | 1137 } |
1093 } else { | 1138 } else { |
1094 assert(!IsFunclet && "funclets without FPs not yet implemented"); | 1139 assert(!IsFunclet && "funclets without FPs not yet implemented"); |
1095 NumBytes = StackSize - X86FI->getCalleeSavedFrameSize(); | 1140 NumBytes = StackSize - X86FI->getCalleeSavedFrameSize(); |
1096 } | 1141 } |
1122 StackOffset += stackGrowth; | 1167 StackOffset += stackGrowth; |
1123 } | 1168 } |
1124 | 1169 |
1125 if (NeedsWinCFI) { | 1170 if (NeedsWinCFI) { |
1126 HasWinCFI = true; | 1171 HasWinCFI = true; |
1127 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)).addImm(Reg).setMIFlag( | 1172 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)) |
1128 MachineInstr::FrameSetup); | 1173 .addImm(Reg) |
1174 .setMIFlag(MachineInstr::FrameSetup); | |
1129 } | 1175 } |
1130 } | 1176 } |
1131 | 1177 |
1132 // Realign stack after we pushed callee-saved registers (so that we'll be | 1178 // Realign stack after we pushed callee-saved registers (so that we'll be |
1133 // able to calculate their offsets from the frame pointer). | 1179 // able to calculate their offsets from the frame pointer). |
1154 // virtual memory manager are allocated in correct sequence. | 1200 // virtual memory manager are allocated in correct sequence. |
1155 uint64_t AlignedNumBytes = NumBytes; | 1201 uint64_t AlignedNumBytes = NumBytes; |
1156 if (IsWin64Prologue && !IsFunclet && TRI->needsStackRealignment(MF)) | 1202 if (IsWin64Prologue && !IsFunclet && TRI->needsStackRealignment(MF)) |
1157 AlignedNumBytes = alignTo(AlignedNumBytes, MaxAlign); | 1203 AlignedNumBytes = alignTo(AlignedNumBytes, MaxAlign); |
1158 if (AlignedNumBytes >= StackProbeSize && UseStackProbe) { | 1204 if (AlignedNumBytes >= StackProbeSize && UseStackProbe) { |
1205 assert(!X86FI->getUsesRedZone() && | |
1206 "The Red Zone is not accounted for in stack probes"); | |
1207 | |
1159 // Check whether EAX is livein for this block. | 1208 // Check whether EAX is livein for this block. |
1160 bool isEAXAlive = isEAXLiveIn(MBB); | 1209 bool isEAXAlive = isEAXLiveIn(MBB); |
1161 | 1210 |
1162 if (isEAXAlive) { | 1211 if (isEAXAlive) { |
1163 // Sanity check that EAX is not livein for this function. | 1212 // Sanity check that EAX is not livein for this function. |
1259 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rr), FramePtr) | 1308 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rr), FramePtr) |
1260 .addReg(SPOrEstablisher); | 1309 .addReg(SPOrEstablisher); |
1261 | 1310 |
1262 // If this is not a funclet, emit the CFI describing our frame pointer. | 1311 // If this is not a funclet, emit the CFI describing our frame pointer. |
1263 if (NeedsWinCFI && !IsFunclet) { | 1312 if (NeedsWinCFI && !IsFunclet) { |
1313 assert(!NeedsWinFPO && "this setframe incompatible with FPO data"); | |
1264 HasWinCFI = true; | 1314 HasWinCFI = true; |
1265 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame)) | 1315 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame)) |
1266 .addImm(FramePtr) | 1316 .addImm(FramePtr) |
1267 .addImm(SEHFrameOffset) | 1317 .addImm(SEHFrameOffset) |
1268 .setMIFlag(MachineInstr::FrameSetup); | 1318 .setMIFlag(MachineInstr::FrameSetup); |
1297 unsigned IgnoredFrameReg; | 1347 unsigned IgnoredFrameReg; |
1298 int Offset = getFrameIndexReference(MF, FI, IgnoredFrameReg); | 1348 int Offset = getFrameIndexReference(MF, FI, IgnoredFrameReg); |
1299 Offset += SEHFrameOffset; | 1349 Offset += SEHFrameOffset; |
1300 | 1350 |
1301 HasWinCFI = true; | 1351 HasWinCFI = true; |
1352 assert(!NeedsWinFPO && "SEH_SaveXMM incompatible with FPO data"); | |
1302 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SaveXMM)) | 1353 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SaveXMM)) |
1303 .addImm(Reg) | 1354 .addImm(Reg) |
1304 .addImm(Offset) | 1355 .addImm(Offset) |
1305 .setMIFlag(MachineInstr::FrameSetup); | 1356 .setMIFlag(MachineInstr::FrameSetup); |
1306 } | 1357 } |
1486 | 1537 |
1487 void X86FrameLowering::emitEpilogue(MachineFunction &MF, | 1538 void X86FrameLowering::emitEpilogue(MachineFunction &MF, |
1488 MachineBasicBlock &MBB) const { | 1539 MachineBasicBlock &MBB) const { |
1489 const MachineFrameInfo &MFI = MF.getFrameInfo(); | 1540 const MachineFrameInfo &MFI = MF.getFrameInfo(); |
1490 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); | 1541 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); |
1491 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); | 1542 MachineBasicBlock::iterator Terminator = MBB.getFirstTerminator(); |
1492 Optional<unsigned> RetOpcode; | 1543 MachineBasicBlock::iterator MBBI = Terminator; |
1493 if (MBBI != MBB.end()) | |
1494 RetOpcode = MBBI->getOpcode(); | |
1495 DebugLoc DL; | 1544 DebugLoc DL; |
1496 if (MBBI != MBB.end()) | 1545 if (MBBI != MBB.end()) |
1497 DL = MBBI->getDebugLoc(); | 1546 DL = MBBI->getDebugLoc(); |
1498 // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit. | 1547 // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit. |
1499 const bool Is64BitILP32 = STI.isTarget64BitILP32(); | 1548 const bool Is64BitILP32 = STI.isTarget64BitILP32(); |
1500 unsigned FramePtr = TRI->getFrameRegister(MF); | 1549 unsigned FramePtr = TRI->getFrameRegister(MF); |
1501 unsigned MachineFramePtr = | 1550 unsigned MachineFramePtr = |
1502 Is64BitILP32 ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; | 1551 Is64BitILP32 ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; |
1503 | 1552 |
1504 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); | 1553 bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); |
1505 bool NeedsWinCFI = | 1554 bool NeedsWin64CFI = |
1506 IsWin64Prologue && MF.getFunction()->needsUnwindTableEntry(); | 1555 IsWin64Prologue && MF.getFunction()->needsUnwindTableEntry(); |
1507 bool IsFunclet = MBBI == MBB.end() ? false : isFuncletReturnInstr(*MBBI); | 1556 bool IsFunclet = MBBI == MBB.end() ? false : isFuncletReturnInstr(*MBBI); |
1508 MachineBasicBlock *TargetMBB = nullptr; | |
1509 | 1557 |
1510 // Get the number of bytes to allocate from the FrameInfo. | 1558 // Get the number of bytes to allocate from the FrameInfo. |
1511 uint64_t StackSize = MFI.getStackSize(); | 1559 uint64_t StackSize = MFI.getStackSize(); |
1512 uint64_t MaxAlign = calculateMaxStackAlign(MF); | 1560 uint64_t MaxAlign = calculateMaxStackAlign(MF); |
1513 unsigned CSSize = X86FI->getCalleeSavedFrameSize(); | 1561 unsigned CSSize = X86FI->getCalleeSavedFrameSize(); |
1562 bool HasFP = hasFP(MF); | |
1514 uint64_t NumBytes = 0; | 1563 uint64_t NumBytes = 0; |
1515 | 1564 |
1516 if (RetOpcode && *RetOpcode == X86::CATCHRET) { | 1565 if (IsFunclet) { |
1517 // SEH shouldn't use catchret. | 1566 assert(HasFP && "EH funclets without FP not yet implemented"); |
1518 assert(!isAsynchronousEHPersonality( | |
1519 classifyEHPersonality(MF.getFunction()->getPersonalityFn())) && | |
1520 "SEH should not use CATCHRET"); | |
1521 | |
1522 NumBytes = getWinEHFuncletFrameSize(MF); | 1567 NumBytes = getWinEHFuncletFrameSize(MF); |
1523 assert(hasFP(MF) && "EH funclets without FP not yet implemented"); | 1568 } else if (HasFP) { |
1524 TargetMBB = MBBI->getOperand(0).getMBB(); | 1569 // Calculate required stack adjustment. |
1525 | 1570 uint64_t FrameSize = StackSize - SlotSize; |
1571 NumBytes = FrameSize - CSSize; | |
1572 | |
1573 // Callee-saved registers were pushed on stack before the stack was | |
1574 // realigned. | |
1575 if (TRI->needsStackRealignment(MF) && !IsWin64Prologue) | |
1576 NumBytes = alignTo(FrameSize, MaxAlign); | |
1577 } else { | |
1578 NumBytes = StackSize - CSSize; | |
1579 } | |
1580 uint64_t SEHStackAllocAmt = NumBytes; | |
1581 | |
1582 if (HasFP) { | |
1526 // Pop EBP. | 1583 // Pop EBP. |
1527 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), | 1584 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), |
1528 MachineFramePtr) | 1585 MachineFramePtr) |
1529 .setMIFlag(MachineInstr::FrameDestroy); | 1586 .setMIFlag(MachineInstr::FrameDestroy); |
1530 } else if (RetOpcode && *RetOpcode == X86::CLEANUPRET) { | 1587 } |
1531 NumBytes = getWinEHFuncletFrameSize(MF); | 1588 |
1532 assert(hasFP(MF) && "EH funclets without FP not yet implemented"); | 1589 MachineBasicBlock::iterator FirstCSPop = MBBI; |
1533 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), | |
1534 MachineFramePtr) | |
1535 .setMIFlag(MachineInstr::FrameDestroy); | |
1536 } else if (hasFP(MF)) { | |
1537 // Calculate required stack adjustment. | |
1538 uint64_t FrameSize = StackSize - SlotSize; | |
1539 NumBytes = FrameSize - CSSize; | |
1540 | |
1541 // Callee-saved registers were pushed on stack before the stack was | |
1542 // realigned. | |
1543 if (TRI->needsStackRealignment(MF) && !IsWin64Prologue) | |
1544 NumBytes = alignTo(FrameSize, MaxAlign); | |
1545 | |
1546 // Pop EBP. | |
1547 BuildMI(MBB, MBBI, DL, | |
1548 TII.get(Is64Bit ? X86::POP64r : X86::POP32r), MachineFramePtr) | |
1549 .setMIFlag(MachineInstr::FrameDestroy); | |
1550 } else { | |
1551 NumBytes = StackSize - CSSize; | |
1552 } | |
1553 uint64_t SEHStackAllocAmt = NumBytes; | |
1554 | |
1555 // Skip the callee-saved pop instructions. | 1590 // Skip the callee-saved pop instructions. |
1556 while (MBBI != MBB.begin()) { | 1591 while (MBBI != MBB.begin()) { |
1557 MachineBasicBlock::iterator PI = std::prev(MBBI); | 1592 MachineBasicBlock::iterator PI = std::prev(MBBI); |
1558 unsigned Opc = PI->getOpcode(); | 1593 unsigned Opc = PI->getOpcode(); |
1559 | 1594 |
1560 if ((Opc != X86::POP32r || !PI->getFlag(MachineInstr::FrameDestroy)) && | 1595 if (Opc != X86::DBG_VALUE && !PI->isTerminator()) { |
1561 (Opc != X86::POP64r || !PI->getFlag(MachineInstr::FrameDestroy)) && | 1596 if ((Opc != X86::POP32r || !PI->getFlag(MachineInstr::FrameDestroy)) && |
1562 Opc != X86::DBG_VALUE && !PI->isTerminator()) | 1597 (Opc != X86::POP64r || !PI->getFlag(MachineInstr::FrameDestroy))) |
1563 break; | 1598 break; |
1599 FirstCSPop = PI; | |
1600 } | |
1564 | 1601 |
1565 --MBBI; | 1602 --MBBI; |
1566 } | 1603 } |
1567 MachineBasicBlock::iterator FirstCSPop = MBBI; | 1604 MBBI = FirstCSPop; |
1568 | 1605 |
1569 if (TargetMBB) { | 1606 if (IsFunclet && Terminator->getOpcode() == X86::CATCHRET) |
1570 // Fill EAX/RAX with the address of the target block. | 1607 emitCatchRetReturnValue(MBB, FirstCSPop, &*Terminator); |
1571 unsigned ReturnReg = STI.is64Bit() ? X86::RAX : X86::EAX; | |
1572 if (STI.is64Bit()) { | |
1573 // LEA64r TargetMBB(%rip), %rax | |
1574 BuildMI(MBB, FirstCSPop, DL, TII.get(X86::LEA64r), ReturnReg) | |
1575 .addReg(X86::RIP) | |
1576 .addImm(0) | |
1577 .addReg(0) | |
1578 .addMBB(TargetMBB) | |
1579 .addReg(0); | |
1580 } else { | |
1581 // MOV32ri $TargetMBB, %eax | |
1582 BuildMI(MBB, FirstCSPop, DL, TII.get(X86::MOV32ri), ReturnReg) | |
1583 .addMBB(TargetMBB); | |
1584 } | |
1585 // Record that we've taken the address of TargetMBB and no longer just | |
1586 // reference it in a terminator. | |
1587 TargetMBB->setHasAddressTaken(); | |
1588 } | |
1589 | 1608 |
1590 if (MBBI != MBB.end()) | 1609 if (MBBI != MBB.end()) |
1591 DL = MBBI->getDebugLoc(); | 1610 DL = MBBI->getDebugLoc(); |
1592 | 1611 |
1593 // If there is an ADD32ri or SUB32ri of ESP immediately before this | 1612 // If there is an ADD32ri or SUB32ri of ESP immediately before this |
1635 // either in prologue or in epilogue. This behavior causes a problem when a | 1654 // either in prologue or in epilogue. This behavior causes a problem when a |
1636 // call immediately precedes an epilogue, because the return address points | 1655 // call immediately precedes an epilogue, because the return address points |
1637 // into the epilogue. To cope with that, we insert an epilogue marker here, | 1656 // into the epilogue. To cope with that, we insert an epilogue marker here, |
1638 // then replace it with a 'nop' if it ends up immediately after a CALL in the | 1657 // then replace it with a 'nop' if it ends up immediately after a CALL in the |
1639 // final emitted code. | 1658 // final emitted code. |
1640 if (NeedsWinCFI && MF.hasWinCFI()) | 1659 if (NeedsWin64CFI && MF.hasWinCFI()) |
1641 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue)); | 1660 BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue)); |
1642 | 1661 |
1643 if (!RetOpcode || !isTailCallOpcode(*RetOpcode)) { | 1662 if (Terminator == MBB.end() || !isTailCallOpcode(Terminator->getOpcode())) { |
1644 // Add the return addr area delta back since we are not tail calling. | 1663 // Add the return addr area delta back since we are not tail calling. |
1645 int Offset = -1 * X86FI->getTCReturnAddrDelta(); | 1664 int Offset = -1 * X86FI->getTCReturnAddrDelta(); |
1646 assert(Offset >= 0 && "TCDelta should never be positive"); | 1665 assert(Offset >= 0 && "TCDelta should never be positive"); |
1647 if (Offset) { | 1666 if (Offset) { |
1648 MBBI = MBB.getFirstTerminator(); | |
1649 | |
1650 // Check for possible merge with preceding ADD instruction. | 1667 // Check for possible merge with preceding ADD instruction. |
1651 Offset += mergeSPUpdates(MBB, MBBI, true); | 1668 Offset += mergeSPUpdates(MBB, Terminator, true); |
1652 emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true); | 1669 emitSPUpdate(MBB, Terminator, Offset, /*InEpilogue=*/true); |
1653 } | 1670 } |
1654 } | 1671 } |
1655 } | 1672 } |
1656 | 1673 |
1657 // NOTE: this only has a subset of the full frame index logic. In | |
1658 // particular, the FI < 0 and AfterFPPop logic is handled in | |
1659 // X86RegisterInfo::eliminateFrameIndex, but not here. Possibly | |
1660 // (probably?) it should be moved into here. | |
1661 int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, | 1674 int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
1662 unsigned &FrameReg) const { | 1675 unsigned &FrameReg) const { |
1663 const MachineFrameInfo &MFI = MF.getFrameInfo(); | 1676 const MachineFrameInfo &MFI = MF.getFrameInfo(); |
1664 | 1677 |
1678 bool IsFixed = MFI.isFixedObjectIndex(FI); | |
1665 // We can't calculate offset from frame pointer if the stack is realigned, | 1679 // We can't calculate offset from frame pointer if the stack is realigned, |
1666 // so enforce usage of stack/base pointer. The base pointer is used when we | 1680 // so enforce usage of stack/base pointer. The base pointer is used when we |
1667 // have dynamic allocas in addition to dynamic realignment. | 1681 // have dynamic allocas in addition to dynamic realignment. |
1668 if (TRI->hasBasePointer(MF)) | 1682 if (TRI->hasBasePointer(MF)) |
1669 FrameReg = TRI->getBaseRegister(); | 1683 FrameReg = IsFixed ? TRI->getFramePtr() : TRI->getBaseRegister(); |
1670 else if (TRI->needsStackRealignment(MF)) | 1684 else if (TRI->needsStackRealignment(MF)) |
1671 FrameReg = TRI->getStackRegister(); | 1685 FrameReg = IsFixed ? TRI->getFramePtr() : TRI->getStackRegister(); |
1672 else | 1686 else |
1673 FrameReg = TRI->getFrameRegister(MF); | 1687 FrameReg = TRI->getFrameRegister(MF); |
1674 | 1688 |
1675 // Offset will hold the offset from the stack pointer at function entry to the | 1689 // Offset will hold the offset from the stack pointer at function entry to the |
1676 // object. | 1690 // object. |
1738 if (TailCallReturnAddrDelta < 0) | 1752 if (TailCallReturnAddrDelta < 0) |
1739 Offset -= TailCallReturnAddrDelta; | 1753 Offset -= TailCallReturnAddrDelta; |
1740 } | 1754 } |
1741 | 1755 |
1742 return Offset + FPDelta; | 1756 return Offset + FPDelta; |
1757 } | |
1758 | |
1759 int X86FrameLowering::getFrameIndexReferenceSP(const MachineFunction &MF, | |
1760 int FI, unsigned &FrameReg, | |
1761 int Adjustment) const { | |
1762 const MachineFrameInfo &MFI = MF.getFrameInfo(); | |
1763 FrameReg = TRI->getStackRegister(); | |
1764 return MFI.getObjectOffset(FI) - getOffsetOfLocalArea() + Adjustment; | |
1743 } | 1765 } |
1744 | 1766 |
1745 int | 1767 int |
1746 X86FrameLowering::getFrameIndexReferencePreferSP(const MachineFunction &MF, | 1768 X86FrameLowering::getFrameIndexReferencePreferSP(const MachineFunction &MF, |
1747 int FI, unsigned &FrameReg, | 1769 int FI, unsigned &FrameReg, |
1796 | 1818 |
1797 // We don't handle tail calls, and shouldn't be seeing them either. | 1819 // We don't handle tail calls, and shouldn't be seeing them either. |
1798 assert(MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta() >= 0 && | 1820 assert(MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta() >= 0 && |
1799 "we don't handle this case!"); | 1821 "we don't handle this case!"); |
1800 | 1822 |
1801 // Fill in FrameReg output argument. | |
1802 FrameReg = TRI->getStackRegister(); | |
1803 | |
1804 // This is how the math works out: | 1823 // This is how the math works out: |
1805 // | 1824 // |
1806 // %rsp grows (i.e. gets lower) left to right. Each box below is | 1825 // %rsp grows (i.e. gets lower) left to right. Each box below is |
1807 // one word (eight bytes). Obj0 is the stack slot we're trying to | 1826 // one word (eight bytes). Obj0 is the stack slot we're trying to |
1808 // get to. | 1827 // get to. |
1823 // E is also the value of %rsp after stack has been set up, and we | 1842 // E is also the value of %rsp after stack has been set up, and we |
1824 // want (C - E) -- the value we can add to %rsp to get to Obj0. Now | 1843 // want (C - E) -- the value we can add to %rsp to get to Obj0. Now |
1825 // (C - E) == (C - A) - (B - A) + (B - E) | 1844 // (C - E) == (C - A) - (B - A) + (B - E) |
1826 // { Using [1], [2] and [3] above } | 1845 // { Using [1], [2] and [3] above } |
1827 // == getObjectOffset - LocalAreaOffset + StackSize | 1846 // == getObjectOffset - LocalAreaOffset + StackSize |
1828 // | 1847 |
1829 | 1848 return getFrameIndexReferenceSP(MF, FI, FrameReg, StackSize); |
1830 // Get the Offset from the StackPointer | |
1831 int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea(); | |
1832 | |
1833 return Offset + StackSize; | |
1834 } | 1849 } |
1835 | 1850 |
1836 bool X86FrameLowering::assignCalleeSavedSpillSlots( | 1851 bool X86FrameLowering::assignCalleeSavedSpillSlots( |
1837 MachineFunction &MF, const TargetRegisterInfo *TRI, | 1852 MachineFunction &MF, const TargetRegisterInfo *TRI, |
1838 std::vector<CalleeSavedInfo> &CSI) const { | 1853 std::vector<CalleeSavedInfo> &CSI) const { |
1880 unsigned Reg = CSI[i - 1].getReg(); | 1895 unsigned Reg = CSI[i - 1].getReg(); |
1881 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg)) | 1896 if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg)) |
1882 continue; | 1897 continue; |
1883 | 1898 |
1884 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); | 1899 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); |
1900 unsigned Size = TRI->getSpillSize(*RC); | |
1901 unsigned Align = TRI->getSpillAlignment(*RC); | |
1885 // ensure alignment | 1902 // ensure alignment |
1886 SpillSlotOffset -= std::abs(SpillSlotOffset) % RC->getAlignment(); | 1903 SpillSlotOffset -= std::abs(SpillSlotOffset) % Align; |
1887 // spill into slot | 1904 // spill into slot |
1888 SpillSlotOffset -= RC->getSize(); | 1905 SpillSlotOffset -= Size; |
1889 int SlotIndex = | 1906 int SlotIndex = MFI.CreateFixedSpillStackObject(Size, SpillSlotOffset); |
1890 MFI.CreateFixedSpillStackObject(RC->getSize(), SpillSlotOffset); | |
1891 CSI[i - 1].setFrameIdx(SlotIndex); | 1907 CSI[i - 1].setFrameIdx(SlotIndex); |
1892 MFI.ensureMaxAlignment(RC->getAlignment()); | 1908 MFI.ensureMaxAlignment(Align); |
1893 } | 1909 } |
1894 | 1910 |
1895 return true; | 1911 return true; |
1896 } | 1912 } |
1897 | 1913 |
1959 } | 1975 } |
1960 | 1976 |
1961 return true; | 1977 return true; |
1962 } | 1978 } |
1963 | 1979 |
1980 void X86FrameLowering::emitCatchRetReturnValue(MachineBasicBlock &MBB, | |
1981 MachineBasicBlock::iterator MBBI, | |
1982 MachineInstr *CatchRet) const { | |
1983 // SEH shouldn't use catchret. | |
1984 assert(!isAsynchronousEHPersonality(classifyEHPersonality( | |
1985 MBB.getParent()->getFunction()->getPersonalityFn())) && | |
1986 "SEH should not use CATCHRET"); | |
1987 DebugLoc DL = CatchRet->getDebugLoc(); | |
1988 MachineBasicBlock *CatchRetTarget = CatchRet->getOperand(0).getMBB(); | |
1989 | |
1990 // Fill EAX/RAX with the address of the target block. | |
1991 if (STI.is64Bit()) { | |
1992 // LEA64r CatchRetTarget(%rip), %rax | |
1993 BuildMI(MBB, MBBI, DL, TII.get(X86::LEA64r), X86::RAX) | |
1994 .addReg(X86::RIP) | |
1995 .addImm(0) | |
1996 .addReg(0) | |
1997 .addMBB(CatchRetTarget) | |
1998 .addReg(0); | |
1999 } else { | |
2000 // MOV32ri $CatchRetTarget, %eax | |
2001 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) | |
2002 .addMBB(CatchRetTarget); | |
2003 } | |
2004 | |
2005 // Record that we've taken the address of CatchRetTarget and no longer just | |
2006 // reference it in a terminator. | |
2007 CatchRetTarget->setHasAddressTaken(); | |
2008 } | |
2009 | |
1964 bool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, | 2010 bool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
1965 MachineBasicBlock::iterator MI, | 2011 MachineBasicBlock::iterator MI, |
1966 const std::vector<CalleeSavedInfo> &CSI, | 2012 std::vector<CalleeSavedInfo> &CSI, |
1967 const TargetRegisterInfo *TRI) const { | 2013 const TargetRegisterInfo *TRI) const { |
1968 if (CSI.empty()) | 2014 if (CSI.empty()) |
1969 return false; | 2015 return false; |
1970 | 2016 |
1971 if (MI != MBB.end() && isFuncletReturnInstr(*MI) && STI.isOSWindows()) { | 2017 if (MI != MBB.end() && isFuncletReturnInstr(*MI) && STI.isOSWindows()) { |
2038 // Spill the BasePtr if it's used. | 2084 // Spill the BasePtr if it's used. |
2039 if (TRI->hasBasePointer(MF)) { | 2085 if (TRI->hasBasePointer(MF)) { |
2040 SavedRegs.set(TRI->getBaseRegister()); | 2086 SavedRegs.set(TRI->getBaseRegister()); |
2041 | 2087 |
2042 // Allocate a spill slot for EBP if we have a base pointer and EH funclets. | 2088 // Allocate a spill slot for EBP if we have a base pointer and EH funclets. |
2043 if (MF.getMMI().hasEHFunclets()) { | 2089 if (MF.hasEHFunclets()) { |
2044 int FI = MFI.CreateSpillStackObject(SlotSize, SlotSize); | 2090 int FI = MFI.CreateSpillStackObject(SlotSize, SlotSize); |
2045 X86FI->setHasSEHFramePtrSave(true); | 2091 X86FI->setHasSEHFramePtrSave(true); |
2046 X86FI->setSEHFramePtrSaveIndex(FI); | 2092 X86FI->setSEHFramePtrSaveIndex(FI); |
2047 } | 2093 } |
2048 } | 2094 } |
2580 MachineBasicBlock::iterator I) const { | 2626 MachineBasicBlock::iterator I) const { |
2581 bool reserveCallFrame = hasReservedCallFrame(MF); | 2627 bool reserveCallFrame = hasReservedCallFrame(MF); |
2582 unsigned Opcode = I->getOpcode(); | 2628 unsigned Opcode = I->getOpcode(); |
2583 bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode(); | 2629 bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode(); |
2584 DebugLoc DL = I->getDebugLoc(); | 2630 DebugLoc DL = I->getDebugLoc(); |
2585 uint64_t Amount = !reserveCallFrame ? I->getOperand(0).getImm() : 0; | 2631 uint64_t Amount = !reserveCallFrame ? TII.getFrameSize(*I) : 0; |
2586 uint64_t InternalAmt = (isDestroy || Amount) ? I->getOperand(1).getImm() : 0; | 2632 uint64_t InternalAmt = (isDestroy || Amount) ? TII.getFrameAdjustment(*I) : 0; |
2587 I = MBB.erase(I); | 2633 I = MBB.erase(I); |
2634 auto InsertPos = skipDebugInstructionsForward(I, MBB.end()); | |
2588 | 2635 |
2589 if (!reserveCallFrame) { | 2636 if (!reserveCallFrame) { |
2590 // If the stack pointer can be changed after prologue, turn the | 2637 // If the stack pointer can be changed after prologue, turn the |
2591 // adjcallstackup instruction into a 'sub ESP, <amt>' and the | 2638 // adjcallstackup instruction into a 'sub ESP, <amt>' and the |
2592 // adjcallstackdown instruction into 'add ESP, <amt>' | 2639 // adjcallstackdown instruction into 'add ESP, <amt>' |
2608 // using GNU_ARGS_SIZE. Note that this may be necessary even when | 2655 // using GNU_ARGS_SIZE. Note that this may be necessary even when |
2609 // Amount == 0, because the preceding function may have set a non-0 | 2656 // Amount == 0, because the preceding function may have set a non-0 |
2610 // GNU_ARGS_SIZE. | 2657 // GNU_ARGS_SIZE. |
2611 // TODO: We don't need to reset this between subsequent functions, | 2658 // TODO: We don't need to reset this between subsequent functions, |
2612 // if it didn't change. | 2659 // if it didn't change. |
2613 bool HasDwarfEHHandlers = !WindowsCFI && | 2660 bool HasDwarfEHHandlers = !WindowsCFI && !MF.getLandingPads().empty(); |
2614 !MF.getMMI().getLandingPads().empty(); | |
2615 | 2661 |
2616 if (HasDwarfEHHandlers && !isDestroy && | 2662 if (HasDwarfEHHandlers && !isDestroy && |
2617 MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences()) | 2663 MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences()) |
2618 BuildCFI(MBB, I, DL, | 2664 BuildCFI(MBB, InsertPos, DL, |
2619 MCCFIInstruction::createGnuArgsSize(nullptr, Amount)); | 2665 MCCFIInstruction::createGnuArgsSize(nullptr, Amount)); |
2620 | 2666 |
2621 if (Amount == 0) | 2667 if (Amount == 0) |
2622 return I; | 2668 return I; |
2623 | 2669 |
2627 | 2673 |
2628 // TODO: This is needed only if we require precise CFA. | 2674 // TODO: This is needed only if we require precise CFA. |
2629 // If this is a callee-pop calling convention, emit a CFA adjust for | 2675 // If this is a callee-pop calling convention, emit a CFA adjust for |
2630 // the amount the callee popped. | 2676 // the amount the callee popped. |
2631 if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) | 2677 if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) |
2632 BuildCFI(MBB, I, DL, | 2678 BuildCFI(MBB, InsertPos, DL, |
2633 MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt)); | 2679 MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt)); |
2634 | 2680 |
2635 // Add Amount to SP to destroy a frame, or subtract to setup. | 2681 // Add Amount to SP to destroy a frame, or subtract to setup. |
2636 int64_t StackAdjustment = isDestroy ? Amount : -Amount; | 2682 int64_t StackAdjustment = isDestroy ? Amount : -Amount; |
2637 int64_t CfaAdjustment = -StackAdjustment; | 2683 int64_t CfaAdjustment = -StackAdjustment; |
2638 | 2684 |
2639 if (StackAdjustment) { | 2685 if (StackAdjustment) { |
2640 // Merge with any previous or following adjustment instruction. Note: the | 2686 // Merge with any previous or following adjustment instruction. Note: the |
2641 // instructions merged with here do not have CFI, so their stack | 2687 // instructions merged with here do not have CFI, so their stack |
2642 // adjustments do not feed into CfaAdjustment. | 2688 // adjustments do not feed into CfaAdjustment. |
2643 StackAdjustment += mergeSPUpdates(MBB, I, true); | 2689 StackAdjustment += mergeSPUpdates(MBB, InsertPos, true); |
2644 StackAdjustment += mergeSPUpdates(MBB, I, false); | 2690 StackAdjustment += mergeSPUpdates(MBB, InsertPos, false); |
2645 | 2691 |
2646 if (StackAdjustment) { | 2692 if (StackAdjustment) { |
2647 if (!(Fn->optForMinSize() && | 2693 if (!(Fn->optForMinSize() && |
2648 adjustStackWithPops(MBB, I, DL, StackAdjustment))) | 2694 adjustStackWithPops(MBB, InsertPos, DL, StackAdjustment))) |
2649 BuildStackAdjustment(MBB, I, DL, StackAdjustment, | 2695 BuildStackAdjustment(MBB, InsertPos, DL, StackAdjustment, |
2650 /*InEpilogue=*/false); | 2696 /*InEpilogue=*/false); |
2651 } | 2697 } |
2652 } | 2698 } |
2653 | 2699 |
2654 if (DwarfCFI && !hasFP(MF)) { | 2700 if (DwarfCFI && !hasFP(MF)) { |
2660 // it to be more precise. | 2706 // it to be more precise. |
2661 | 2707 |
2662 // TODO: When not using precise CFA, we also need to adjust for the | 2708 // TODO: When not using precise CFA, we also need to adjust for the |
2663 // InternalAmt here. | 2709 // InternalAmt here. |
2664 if (CfaAdjustment) { | 2710 if (CfaAdjustment) { |
2665 BuildCFI(MBB, I, DL, MCCFIInstruction::createAdjustCfaOffset( | 2711 BuildCFI(MBB, InsertPos, DL, |
2666 nullptr, CfaAdjustment)); | 2712 MCCFIInstruction::createAdjustCfaOffset(nullptr, |
2713 CfaAdjustment)); | |
2667 } | 2714 } |
2668 } | 2715 } |
2669 | 2716 |
2670 return I; | 2717 return I; |
2671 } | 2718 } |
2944 return Offset; | 2991 return Offset; |
2945 } | 2992 } |
2946 | 2993 |
2947 void X86FrameLowering::processFunctionBeforeFrameFinalized( | 2994 void X86FrameLowering::processFunctionBeforeFrameFinalized( |
2948 MachineFunction &MF, RegScavenger *RS) const { | 2995 MachineFunction &MF, RegScavenger *RS) const { |
2996 // Mark the function as not having WinCFI. We will set it back to true in | |
2997 // emitPrologue if it gets called and emits CFI. | |
2998 MF.setHasWinCFI(false); | |
2999 | |
2949 // If this function isn't doing Win64-style C++ EH, we don't need to do | 3000 // If this function isn't doing Win64-style C++ EH, we don't need to do |
2950 // anything. | 3001 // anything. |
2951 const Function *Fn = MF.getFunction(); | 3002 const Function *Fn = MF.getFunction(); |
2952 if (!STI.is64Bit() || !MF.getMMI().hasEHFunclets() || | 3003 if (!STI.is64Bit() || !MF.hasEHFunclets() || |
2953 classifyEHPersonality(Fn->getPersonalityFn()) != EHPersonality::MSVC_CXX) | 3004 classifyEHPersonality(Fn->getPersonalityFn()) != EHPersonality::MSVC_CXX) |
2954 return; | 3005 return; |
2955 | 3006 |
2956 // Win64 C++ EH needs to allocate the UnwindHelp object at some fixed offset | 3007 // Win64 C++ EH needs to allocate the UnwindHelp object at some fixed offset |
2957 // relative to RSP after the prologue. Find the offset of the last fixed | 3008 // relative to RSP after the prologue. Find the offset of the last fixed |