Index: lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp +++ lib/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp @@ -287,6 +287,7 @@ const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); + unsigned FrameReg = TRI->getFrameRegister(*MF); RegDescribedVarsMap RegVars; DbgValueEntriesMap LiveEntries; for (const auto &MBB : *MF) { @@ -371,7 +372,8 @@ // later though, to keep LiveEntries iteration stable. if (DbgValue->getOperand(0).isReg()) { unsigned RegNo = DbgValue->getOperand(0).getReg(); - if (TRI->isVirtualRegister(RegNo) || ChangingRegs.test(RegNo)) + if (TRI->isVirtualRegister(RegNo) || ChangingRegs.test(RegNo) || + FrameReg == RegNo) RegsToClobber.insert(RegNo); } else { // This is a constant, terminate it at end of the block. Store Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -1330,9 +1330,10 @@ // Check if there is a single DBG_VALUE, valid throughout the var's scope. // If the history map contains a single debug value, there may be an // additional entry which clobbers the debug value. + size_t HistSize = HistoryMapEntries.size(); bool SingleValueWithClobber = - HistoryMapEntries.size() == 2 && HistoryMapEntries[1].isClobber(); - if (HistoryMapEntries.size() == 1 || SingleValueWithClobber) { + HistSize == 2 && HistoryMapEntries[1].isClobber(); + if (HistSize == 1 || SingleValueWithClobber) { const auto *End = SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr; if (validThroughout(LScopes, MInsn, End)) { @@ -1352,6 +1353,25 @@ SmallVector Entries; buildLocationList(Entries, HistoryMapEntries); + // Check whether buildLocationList managed to merge all locations to one + // that is valid throughout the variable's scope. If so, produce single + // value location. + // TODO: Add support for single value fragment locations. + if (Entries.size() == 1 && !MInsn->getDebugExpression()->isFragment()) { + const auto *End = HistoryMapEntries.back().isClobber() + ? HistoryMapEntries.back().getInstr() + : nullptr; + const MachineInstr *DbgMI = + HistoryMapEntries.back().isDbgValue() + ? HistoryMapEntries.back().getInstr() + : HistoryMapEntries[HistSize - 2].getInstr(); + // Use first DBG_VALUE instruction for check. + if (validThroughout(LScopes, MInsn, End)) { + RegVar->initializeDbgValue(DbgMI); + continue; + } + } + // If the variable has a DIBasicType, extract it. Basic types cannot have // unique identifiers, so don't bother resolving the type with the // identifier map. Index: test/DebugInfo/MIR/X86/dbg-stack-value-range.mir =================================================================== --- /dev/null +++ test/DebugInfo/MIR/X86/dbg-stack-value-range.mir @@ -0,0 +1,185 @@ +# RUN: llc -start-before=livedebugvalues %s -filetype=asm -o -| FileCheck %s +# +# DbgEntityHistoryCalculator should close variable's range at the end of +# the basic block when variable is referenced through non-changing frame +# pointer register. Having BB1 that branches to BB2 and BB3, where +# BB2 falls through to BB3, variable can have different locations at BB1 and BB2. +# Since input locations at BB3 for same variable are different LiveDebugValues +# wont generate DBG_VALUE for BB3. If last variable location at BB2 is +# non-changing register, DbgEntityHistoryCalculator will extend range of +# DBG_VALUE from BB2 to whole BB3 and thus produce incorrect range for +# case when we took branch BB3 from BB1. +# +# Verifies that "local1" stack location is ended at the end of the block (.Ltmp6). +# +# CHECK: .Ltmp4: +# CHECK-NEXT: #DEBUG_VALUE: foo:local1 <- [DW_OP_constu 4, DW_OP_minus, DW_OP_deref] $rbp +# CHECK: jmp .LBB0_2 +# CHECK-NEXT: .Ltmp6: +# CHECK: .Lfunc_end0: +# +# CHECK: .Linfo_string7: +# CHECK-NEXT: .asciz "local1" +# +# CHECK: .Ldebug_loc1: +# CHECK-NEXT: .quad .Ltmp1-.Lfunc_begin0 +# CHECK-NEXT: .quad .Ltmp4-.Lfunc_begin0 +# CHECK-NEXT: .short 1 # Loc expr size +# CHECK-NEXT: .byte 94 # super-register DW_OP_reg14 +# CHECK-NEXT: .quad .Ltmp4-.Lfunc_begin0 +# CHECK-NEXT: .quad .Ltmp6-.Lfunc_begin0 +# CHECK-NEXT: .short 2 # Loc expr size +# CHECK-NEXT: .byte 118 # DW_OP_breg6 +# CHECK-NEXT: .byte 124 # -4 +# +# CHECK: .byte 5 # Abbrev [5] 0x5d:0xf DW_TAG_variable +# CHECK-NEXT: .long .Ldebug_loc1 # DW_AT_location +# CHECK-NEXT: .long .Linfo_string7 # DW_AT_name + + +--- | + ; ModuleID = 'dbg-stack-value-range.ll' + source_filename = "dbg-stack-value-range.c" + target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + target triple = "x86_64-unknown-linux-gnu" + + ; Function Attrs: nounwind uwtable + define dso_local i32 @foo(i32 %X) local_unnamed_addr #0 !dbg !7 { + entry: + %local1 = alloca i32, align 4 + call void @llvm.dbg.value(metadata i32 %X, metadata !12, metadata !DIExpression()), !dbg !15 + %0 = bitcast i32* %local1 to i8*, !dbg !15 + call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %0), !dbg !15 + call void @llvm.dbg.value(metadata i32 5, metadata !14, metadata !DIExpression()), !dbg !15 + %call = tail call i32 (...) @check(), !dbg !15 + %tobool = icmp eq i32 %call, 0, !dbg !15 + br i1 %tobool, label %if.else, label %if.then, !dbg !15 + + if.then: ; preds = %entry + call void @llvm.dbg.value(metadata i32 4, metadata !13, metadata !DIExpression()), !dbg !15 + store i32 4, i32* %local1, align 4, !dbg !15, !tbaa !16 + call void @llvm.dbg.value(metadata i32* %local1, metadata !13, metadata !DIExpression(DW_OP_deref)), !dbg !15 + %call1 = call i32 @init(i32* nonnull %local1), !dbg !15 + call void @llvm.dbg.value(metadata i32 undef, metadata !14, metadata !DIExpression()), !dbg !15 + br label %if.end, !dbg !15 + + if.else: ; preds = %entry + call void @llvm.dbg.value(metadata i32 5, metadata !13, metadata !DIExpression()), !dbg !15 + store i32 5, i32* %local1, align 4, !dbg !15, !tbaa !16 + br label %if.end + + if.end: ; preds = %if.else, %if.then + %1 = bitcast i32* %local1 to i8*, !dbg !15 + call void @llvm.dbg.value(metadata i32 undef, metadata !14, metadata !DIExpression()), !dbg !15 + %call2 = call i32 (...) @init2(), !dbg !15 + call void @llvm.dbg.value(metadata i32 undef, metadata !14, metadata !DIExpression()), !dbg !15 + %2 = load i32, i32* %local1, align 4, !dbg !15, !tbaa !16 + call void @llvm.dbg.value(metadata i32 %2, metadata !13, metadata !DIExpression()), !dbg !15 + call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %1), !dbg !15 + ret i32 %2, !dbg !15 + } + + ; Function Attrs: argmemonly nounwind + declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) + + declare dso_local i32 @check(...) local_unnamed_addr + + declare dso_local i32 @init(i32*) local_unnamed_addr + + declare dso_local i32 @init2(...) local_unnamed_addr + + ; Function Attrs: argmemonly nounwind + declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) + + ; Function Attrs: nounwind readnone speculatable + declare void @llvm.dbg.value(metadata, metadata, metadata) + + ; Function Attrs: nounwind + declare void @llvm.stackprotector(i8*, i8**) + + attributes #0 = { nounwind uwtable "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf" } + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!3, !4, !5} + !llvm.ident = !{!6} + + !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) + !1 = !DIFile(filename: "dbg-stack-value-range.c", 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 9.0.0"} + !7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 11, type: !8, scopeLine: 11, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11) + !8 = !DISubroutineType(types: !9) + !9 = !{!10, !10} + !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !11 = !{!12, !13, !14} + !12 = !DILocalVariable(name: "X", arg: 1, scope: !7, file: !1, line: 11, type: !10) + !13 = !DILocalVariable(name: "local1", scope: !7, file: !1, line: 12, type: !10) + !14 = !DILocalVariable(name: "local2", scope: !7, file: !1, line: 12, type: !10) + !15 = !DILocation(line: 11, column: 13, scope: !7) + !16 = !{!17, !17, i64 0} + !17 = !{!"int", !18, i64 0} + !18 = !{!"omnipotent char", !19, i64 0} + !19 = !{!"Simple C/C++ TBAA"} + +... +--- +name: foo +alignment: 4 +frameInfo: + stackSize: 24 + offsetAdjustment: -24 + maxAlignment: 4 + adjustsStack: true + hasCalls: true +fixedStack: + - { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 16, stack-id: 0, + callee-saved-register: '$r14d', callee-saved-restored: true, debug-info-variable: '', + debug-info-expression: '', debug-info-location: '' } +stack: + - { id: 0, name: local1, type: default, offset: -20, size: 4, alignment: 4, + stack-id: 0, callee-saved-register: '', callee-saved-restored: true, + debug-info-variable: '', debug-info-expression: '', debug-info-location: '' } +body: | + bb.0.entry: + successors: %bb.3(0x30000000), %bb.1(0x50000000) + + DBG_VALUE $edi, $noreg, !12, !DIExpression(), debug-location !15 + frame-setup PUSH64r killed $rbp, implicit-def $rsp, implicit $rsp + CFI_INSTRUCTION def_cfa_offset 16 + CFI_INSTRUCTION offset $rbp, -16 + $rbp = frame-setup MOV64rr $rsp + CFI_INSTRUCTION def_cfa_register $rbp + $rsp = frame-setup SUB64ri8 $rsp, 16, implicit-def dead $eflags + DBG_VALUE 5, $noreg, !14, !DIExpression(), debug-location !15 + $r14d = MOV32ri 4, implicit-def $r14 + DBG_VALUE $r14d, $noreg, !13, !DIExpression(), debug-location !15 + dead $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $al, debug-location !15 + CALL64pcrel32 @check, csr_64, implicit $rsp, implicit $ssp, implicit $al, implicit-def $rsp, implicit-def $ssp, implicit-def $eax, debug-location !15 + TEST32rr killed renamable $eax, renamable $eax, implicit-def $eflags, debug-location !15 + JCC_1 %bb.3, 4, implicit killed $eflags, debug-location !15 + + bb.1.if.then: + successors: %bb.3(0x80000000) + + MOV32mr $rbp, 1, $noreg, -4, $noreg, $r14d, debug-location !15 :: (store 4 into %ir.local1, !tbaa !16) + DBG_VALUE $rbp, $noreg, !13, !DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref), debug-location !15 + renamable $rdi = LEA64r $rbp, 1, $noreg, -4, $noreg + CALL64pcrel32 @init, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, implicit-def dead $eax, debug-location !15 + DBG_VALUE $noreg, $noreg, !14, !DIExpression(), debug-location !15 + JMP_1 %bb.3 + + bb.3.if.end: + DBG_VALUE $noreg, $noreg, !14, !DIExpression(), debug-location !15 + dead $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $al, debug-location !15 + CALL64pcrel32 @init2, csr_64, implicit $rsp, implicit $ssp, implicit $al, implicit-def $rsp, implicit-def $ssp, implicit-def dead $eax, debug-location !15 + DBG_VALUE $noreg, $noreg, !14, !DIExpression(), debug-location !15 + renamable $eax = MOV32rm $rbp, 1, $noreg, -4, $noreg, debug-location !15 :: (dereferenceable load 4 from %ir.local1, !tbaa !16) + $rsp = frame-destroy ADD64ri8 $rsp, 16, implicit-def dead $eflags, debug-location !15 + $rbp = frame-destroy POP64r implicit-def $rsp, implicit $rsp, debug-location !15 + CFI_INSTRUCTION def_cfa $rsp, 8, debug-location !15 + RETQ $eax, debug-location !15 + +... Index: test/DebugInfo/X86/fission-ranges.ll =================================================================== --- test/DebugInfo/X86/fission-ranges.ll +++ test/DebugInfo/X86/fission-ranges.ll @@ -15,7 +15,8 @@ ; CHECK-NEXT: DW_AT_GNU_addr_base [DW_FORM_sec_offset] (0x00000000) ; CHECK: .debug_info.dwo contents: -; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[P:0x[0-9a-z]*]] +; CHECK: DW_AT_const_value [DW_FORM_sdata] (1) +; CHECK-NEXT: DW_AT_name {{.*}} "p" ; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[A:0x[0-9a-z]*]] ; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[E:0x[0-9a-z]*]] ; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[B:0x[0-9a-z]*]] @@ -28,8 +29,6 @@ ; Don't assume these locations are entirely correct - feel free to update them ; if they've changed due to a bugfix, change in register allocation, etc. -; CHECK: [[P]]: -; CHECK-NEXT: Addr idx 1 (w/ length 204): DW_OP_consts +1, DW_OP_stack_value ; CHECK: [[A]]: ; CHECK-NEXT: Addr idx 2 (w/ length 169): DW_OP_consts +0, DW_OP_stack_value ; CHECK-NEXT: Addr idx 3 (w/ length 15): DW_OP_reg0 RAX @@ -37,13 +36,13 @@ ; CHECK-NEXT: Addr idx 5 (w/ length 4): DW_OP_reg0 RAX ; CHECK: [[E]]: ; CHECK-NEXT: Addr idx 6 (w/ length 9): DW_OP_reg0 RAX -; CHECK-NEXT: Addr idx 7 (w/ length 98): DW_OP_breg7 RSP-44 +; CHECK-NEXT: Addr idx 7 (w/ length 10): DW_OP_breg7 RSP-44 ; CHECK: [[B]]: ; CHECK-NEXT: Addr idx 8 (w/ length 15): DW_OP_reg0 RAX -; CHECK-NEXT: Addr idx 9 (w/ length 66): DW_OP_breg7 RSP-32 +; CHECK-NEXT: Addr idx 9 (w/ length 2): DW_OP_breg7 RSP-32 ; CHECK: [[D]]: ; CHECK-NEXT: Addr idx 10 (w/ length 15): DW_OP_reg0 RAX -; CHECK-NEXT: Addr idx 11 (w/ length 42): DW_OP_breg7 RSP-20 +; CHECK-NEXT: Addr idx 11 (w/ length 2): DW_OP_breg7 RSP-20 ; Make sure we don't produce any relocations in any .dwo section (though in particular, debug_info.dwo) ; HDR-NOT: .rela.{{.*}}.dwo