diff --git a/llvm/lib/Target/X86/X86FrameLowering.h b/llvm/lib/Target/X86/X86FrameLowering.h --- a/llvm/lib/Target/X86/X86FrameLowering.h +++ b/llvm/lib/Target/X86/X86FrameLowering.h @@ -192,6 +192,10 @@ bool has128ByteRedZone(const MachineFunction& MF) const; private: + bool isWin64Prologue(const MachineFunction &MF) const; + + bool needsDwarfCFI(const MachineFunction &MF) const; + uint64_t calculateMaxStackAlign(const MachineFunction &MF) const; /// Emit target stack probe as a call to a helper function diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp --- a/llvm/lib/Target/X86/X86FrameLowering.cpp +++ b/llvm/lib/Target/X86/X86FrameLowering.cpp @@ -616,6 +616,8 @@ uint64_t AlignOffset) const { assert(Offset && "null offset"); + const bool NeedsDwarfCFI = needsDwarfCFI(MF); + const bool HasFP = hasFP(MF); const X86Subtarget &STI = MF.getSubtarget(); const X86TargetLowering &TLI = *STI.getTargetLowering(); const unsigned MovMIOpc = Is64Bit ? X86::MOV64mi32 : X86::MOV32mi; @@ -659,11 +661,23 @@ // save loop bound { + const unsigned BoundOffset = Offset / StackProbeSize * StackProbeSize; const unsigned SUBOpc = getSUBriOpcode(Uses64BitFramePtr, Offset); BuildMI(MBB, MBBI, DL, TII.get(SUBOpc), FinalStackProbed) .addReg(FinalStackProbed) - .addImm(Offset / StackProbeSize * StackProbeSize) + .addImm(BoundOffset) .setMIFlag(MachineInstr::FrameSetup); + + // TODO: generate DWARF information for 32bit architecture + // didn't generate dwarf information on 32bit, because R11D is not usable + // and decide a safe register could be complicate or with extra cost + if (!HasFP && NeedsDwarfCFI && Uses64BitFramePtr) { + BuildCFI(MBB, MBBI, DL, + MCCFIInstruction::createDefCfaRegister( + nullptr, TRI->getDwarfRegNum(FinalStackProbed, true))); + BuildCFI(MBB, MBBI, DL, + MCCFIInstruction::createAdjustCfaOffset(nullptr, BoundOffset)); + } } // allocate a page @@ -703,14 +717,23 @@ // handle tail unsigned TailOffset = Offset % StackProbeSize; + MachineBasicBlock::iterator TailMBBIter = tailMBB->begin(); if (TailOffset) { const unsigned Opc = getSUBriOpcode(Uses64BitFramePtr, TailOffset); - BuildMI(*tailMBB, tailMBB->begin(), DL, TII.get(Opc), StackPtr) + BuildMI(*tailMBB, TailMBBIter, DL, TII.get(Opc), StackPtr) .addReg(StackPtr) .addImm(TailOffset) .setMIFlag(MachineInstr::FrameSetup); } + // TODO: generate DWARF information for 32bit architecture + // didn't generate dwarf information on 32bit, because R11D is not usable + // and decide a safe register could be complicate or with extra cost + if (!HasFP && NeedsDwarfCFI && Uses64BitFramePtr) { + BuildCFI(*tailMBB, TailMBBIter, DL, + MCCFIInstruction::createDefCfaRegister( + nullptr, TRI->getDwarfRegNum(StackPtr, true))); + } // Update Live In information recomputeLiveIns(*testMBB); recomputeLiveIns(*tailMBB); @@ -1200,6 +1223,13 @@ return Is64Bit && !IsWin64CC && !Fn.hasFnAttribute(Attribute::NoRedZone); } +bool X86FrameLowering::isWin64Prologue(const MachineFunction &MF) const { + return MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); +} + +bool X86FrameLowering::needsDwarfCFI(const MachineFunction &MF) const { + return !isWin64Prologue(MF) && MF.needsFrameMoves(); +} /// emitPrologue - Push callee-saved registers onto the stack, which /// automatically adjust the stack pointer. Adjust the stack pointer to allocate @@ -1305,13 +1335,13 @@ MF.hasEHFunclets() && Personality == EHPersonality::CoreCLR; bool IsClrFunclet = IsFunclet && FnHasClrFunclet; bool HasFP = hasFP(MF); - bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); + bool IsWin64Prologue = isWin64Prologue(MF); bool NeedsWin64CFI = IsWin64Prologue && Fn.needsUnwindTableEntry(); // FIXME: Emit FPO data for EH funclets. bool NeedsWinFPO = !IsFunclet && STI.isTargetWin32() && MMI.getModule()->getCodeViewFlag(); bool NeedsWinCFI = NeedsWin64CFI || NeedsWinFPO; - bool NeedsDwarfCFI = !IsWin64Prologue && MF.needsFrameMoves(); + bool NeedsDwarfCFI = needsDwarfCFI(MF); Register FramePtr = TRI->getFrameRegister(MF); const Register MachineFramePtr = STI.isTarget64BitILP32() diff --git a/llvm/test/CodeGen/X86/stack-clash-large.ll b/llvm/test/CodeGen/X86/stack-clash-large.ll --- a/llvm/test/CodeGen/X86/stack-clash-large.ll +++ b/llvm/test/CodeGen/X86/stack-clash-large.ll @@ -7,6 +7,8 @@ ; CHECK-X86-64: # %bb.0: ; CHECK-X86-64-NEXT: movq %rsp, %r11 ; CHECK-X86-64-NEXT: subq $69632, %r11 # imm = 0x11000 +; CHECK-X86-64-NEXT: .cfi_def_cfa_register %r11 +; CHECK-X86-64-NEXT: .cfi_adjust_cfa_offset 69632 ; CHECK-X86-64-NEXT: .LBB0_1: # =>This Inner Loop Header: Depth=1 ; CHECK-X86-64-NEXT: subq $4096, %rsp # imm = 0x1000 ; CHECK-X86-64-NEXT: movq $0, (%rsp) @@ -14,6 +16,7 @@ ; CHECK-X86-64-NEXT: jne .LBB0_1 ; CHECK-X86-64-NEXT: # %bb.2: ; CHECK-X86-64-NEXT: subq $2248, %rsp # imm = 0x8C8 +; CHECK-X86-64-NEXT: .cfi_def_cfa_register %rsp ; CHECK-X86-64-NEXT: .cfi_def_cfa_offset 71888 ; CHECK-X86-64-NEXT: movl $1, 264(%rsp) ; CHECK-X86-64-NEXT: movl $1, 28664(%rsp)