Index: lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp @@ -222,6 +222,9 @@ case MCCFIInstruction::OpDefCfaOffset: OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset()); break; + case MCCFIInstruction::OpAdjustCfaOffset: + OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset()); + break; case MCCFIInstruction::OpDefCfa: OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset()); break; Index: lib/Target/X86/X86CallFrameOptimization.cpp =================================================================== --- lib/Target/X86/X86CallFrameOptimization.cpp +++ lib/Target/X86/X86CallFrameOptimization.cpp @@ -27,8 +27,10 @@ #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstrBuilder.h" #include "llvm/CodeGen/MachineRegisterInfo.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/Passes.h" #include "llvm/IR/Function.h" +#include "llvm/MC/MCDwarf.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetInstrInfo.h" @@ -424,6 +426,18 @@ } } + const Function *Fn = MF.getFunction(); + MachineModuleInfo &MMI = MF.getMMI(); + bool IsWinEH = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); + bool NeedsDwarfCFI = + !IsWinEH && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); + if (NeedsDwarfCFI) { + unsigned CFIIndex = MMI.addFrameInst( + MCCFIInstruction::createAdjustCfaOffset(nullptr, 4)); + BuildMI(MBB, Context.Call, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) + .addCFIIndex(CFIIndex); + } + MBB.erase(MOV); } Index: lib/Target/X86/X86FrameLowering.cpp =================================================================== --- lib/Target/X86/X86FrameLowering.cpp +++ lib/Target/X86/X86FrameLowering.cpp @@ -223,6 +223,13 @@ uint64_t Chunk = (1LL << 31) - 1; DebugLoc DL = MBB.findDebugLoc(MBBI); + MachineFunction &MF = *MBB.getParent(); + const Function *Fn = MF.getFunction(); + MachineModuleInfo &MMI = MF.getMMI(); + bool IsWinEH = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); + bool NeedsDwarfCFI = + !IsWinEH && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); + while (Offset) { if (Offset > Chunk) { // Rather than emit a long series of instructions for large offsets, @@ -246,6 +253,14 @@ .addReg(Reg); MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. Offset = 0; + + if (NeedsDwarfCFI) { + unsigned CFIIndex = MMI.addFrameInst( + MCCFIInstruction::createAdjustCfaOffset(nullptr, -Offset)); + BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) + .addCFIIndex(CFIIndex); + } + continue; } } @@ -265,6 +280,14 @@ if (isSub) MI->setFlag(MachineInstr::FrameSetup); Offset -= ThisVal; + + if (NeedsDwarfCFI) { + unsigned CFIIndex = MMI.addFrameInst( + MCCFIInstruction::createAdjustCfaOffset(nullptr, -ThisVal)); + BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) + .addCFIIndex(CFIIndex); + } + continue; } } @@ -279,6 +302,13 @@ .addReg(StackPtr) .addImm(ThisVal); MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. + + if (NeedsDwarfCFI) { + unsigned CFIIndex = MMI.addFrameInst( + MCCFIInstruction::createAdjustCfaOffset(nullptr, -ThisVal)); + BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) + .addCFIIndex(CFIIndex); + } } if (isSub) @@ -1055,7 +1085,7 @@ unsigned Opc = PI->getOpcode(); if (Opc != X86::POP32r && Opc != X86::POP64r && Opc != X86::DBG_VALUE && - !PI->isTerminator()) + Opc != X86::CFI_INSTRUCTION && !PI->isTerminator()) break; --MBBI; @@ -1498,6 +1528,7 @@ // POP GPRs. unsigned Opc = STI.is64Bit() ? X86::POP64r : X86::POP32r; + unsigned CFIAdjustment = STI.is64Bit() ? -8 : -4; for (unsigned i = 0, e = CSI.size(); i != e; ++i) { unsigned Reg = CSI[i].getReg(); if (!X86::GR64RegClass.contains(Reg) && @@ -1505,6 +1536,18 @@ continue; BuildMI(MBB, MI, DL, TII.get(Opc), Reg); + + const Function *Fn = MF.getFunction(); + MachineModuleInfo &MMI = MF.getMMI(); + bool IsWinEH = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); + bool NeedsDwarfCFI = + !IsWinEH && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); + if (NeedsDwarfCFI) { + unsigned CFIIndex = MF.getMMI().addFrameInst( + MCCFIInstruction::createAdjustCfaOffset(nullptr, CFIAdjustment)); + BuildMI(MBB, MI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) + .addCFIIndex(CFIIndex); + } } return true; } @@ -1994,6 +2037,13 @@ uint64_t InternalAmt = (isDestroy || Amount) ? I->getOperand(1).getImm() : 0; I = MBB.erase(I); + const Function *Fn = MF.getFunction(); + MachineModuleInfo &MMI = MF.getMMI(); + bool IsWinEH = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); + bool NeedsDwarfCFI = + !IsWinEH && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); + uint64_t CFIAdjustment = 0; + if (!reserveCallFrame) { // If the stack pointer can be changed after prologue, turn the // adjcallstackup instruction into a 'sub ESP, ' and the @@ -2017,12 +2067,14 @@ if (Opcode == TII.getCallFrameSetupOpcode()) { New = BuildMI(MF, DL, TII.get(getSUBriOpcode(IsLP64, Amount)), StackPtr) .addReg(StackPtr).addImm(Amount); + CFIAdjustment = Amount; } else { assert(Opcode == TII.getCallFrameDestroyOpcode()); unsigned Opc = getADDriOpcode(IsLP64, Amount); New = BuildMI(MF, DL, TII.get(Opc), StackPtr) .addReg(StackPtr).addImm(Amount); + CFIAdjustment = -Amount; } } @@ -2032,6 +2084,13 @@ // Replace the pseudo instruction with a new instruction. MBB.insert(I, New); + + if (NeedsDwarfCFI) { + unsigned CFIIndex = MMI.addFrameInst( + MCCFIInstruction::createAdjustCfaOffset(nullptr, CFIAdjustment)); + BuildMI(MBB, I, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) + .addCFIIndex(CFIIndex); + } } return;