Index: llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -1325,15 +1325,27 @@ PrevInstLoc = DL; } -static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { +// Return true if MI has prologue instruction state. +static bool isPrologueInstruction(const MachineInstr &MI) { + return MI.getFlag(MachineInstr::FrameSetup) || !MI.getDebugLoc(); +} + +static std::pair +checkPrologueExistsAndFindEndLoc(const MachineFunction *MF) { + bool hasPrologue = false; // First known non-DBG_VALUE and non-frame setup location marks - // the beginning of the function body. + // the beginning of the function body. Set hasPrologue to true + // if prologue instructions are found. for (const auto &MBB : *MF) - for (const auto &MI : MBB) - if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && - MI.getDebugLoc()) - return MI.getDebugLoc(); - return DebugLoc(); + for (const auto &MI : MBB) { + if (MI.isMetaInstruction()) + continue; + if (isPrologueInstruction(MI)) + hasPrologue = true; + else + return std::make_pair(hasPrologue, MI.getDebugLoc()); + } + return std::make_pair(hasPrologue, DebugLoc()); } // Gather pre-function debug information. Assumes being called immediately @@ -1357,9 +1369,10 @@ else Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID()); - // Record beginning of function. - PrologEndLoc = findPrologueEndLoc(MF); - if (PrologEndLoc) { + bool hasPrologue; + // Record beginning of function and check for prologue instructions. + std::tie(hasPrologue, PrologEndLoc) = checkPrologueExistsAndFindEndLoc(MF); + if (hasPrologue && PrologEndLoc) { // We'd like to list the prologue as "not statements" but GDB behaves // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); Index: llvm/test/DebugInfo/no-debug-line-for-removed-prologue.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/no-debug-line-for-removed-prologue.ll @@ -0,0 +1,56 @@ +; RUN: llc < %s -filetype=asm | FileCheck %s +; +; Test checks that .loc directives for stripped function prologues +; are not emmitted. To try and force stripping of prologues a simple +; leaf function call example is used. +; +; Compiled from the following with clang -g -gmlt -O2: +; +; int leafCall() +; { +; return 2 + 2; +; } +; +; int main() +; { +; return leafCall(); +; } + +; ModuleID = 'leafCall.cpp' +source_filename = "leafCall.cpp" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +; Function Attrs: norecurse nounwind readnone uwtable +define dso_local i32 @_Z8leafCallv() local_unnamed_addr #0 !dbg !7 { +entry: + ; CHECK-LABEL: _Z8leafCallv: + ; CHECK-NOT: .loc + ; CHECK: .loc 1 3 2 prologue_end + ret i32 4, !dbg !9 +} + +; Function Attrs: norecurse nounwind readnone uwtable +define dso_local i32 @main(i32 %argc, i8** nocapture readnone) local_unnamed_addr #0 !dbg !10 { +entry: + ; CHECK-LABEL: main: + ; CHECK-NOT: .loc + ; CHECK: .loc 1 8 2 prologue_end + ret i32 4, !dbg !11 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 7.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2) +!1 = !DIFile(filename: "leafCall.cpp", directory: "./") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 7.0.0 "} +!7 = distinct !DISubprogram(name: "leafCall", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !2) +!8 = !DISubroutineType(types: !2) +!9 = !DILocation(line: 3, column: 2, scope: !7) +!10 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 6, type: !8, isLocal: false, isDefinition: true, scopeLine: 7, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !2) +!11 = !DILocation(line: 8, column: 2, scope: !10)