Index: llvm/lib/CodeGen/LiveDebugVariables.cpp =================================================================== --- llvm/lib/CodeGen/LiveDebugVariables.cpp +++ llvm/lib/CodeGen/LiveDebugVariables.cpp @@ -96,22 +96,24 @@ enum : unsigned { UndefLocNo = ~0U }; -/// Describes a debug variable location by number. +/// Describes a debug variable value by location number and expression. class DbgValueLocation { public: DbgValueLocation() = default; - DbgValueLocation(unsigned LocNo) : LocNo(LocNo) {} + DbgValueLocation(unsigned LocNo, const DIExpression &Expression) + : LocNo(LocNo), Expression(&Expression) {} - unsigned locNo() const { return LocNo; } - bool isUndef() const { return locNo() == UndefLocNo; } + unsigned getLocNo() const { return LocNo; } + const DIExpression *getExpression() const { return Expression; } + bool isUndef() const { return getLocNo() == UndefLocNo; } DbgValueLocation changeLocNo(unsigned NewLocNo) const { - return DbgValueLocation(NewLocNo); + return DbgValueLocation(NewLocNo, *Expression); } friend inline bool operator==(const DbgValueLocation &LHS, const DbgValueLocation &RHS) { - return LHS.LocNo == RHS.LocNo; + return LHS.LocNo == RHS.LocNo && LHS.Expression == RHS.Expression; } friend inline bool operator!=(const DbgValueLocation &LHS, @@ -193,7 +195,9 @@ const DILocation *IA) const { // FIXME: The fragment should be part of the equivalence class, but not // other things in the expression like stack values. - return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA; + return Var == Variable && + Expr->getFragmentInfo() == Expression->getFragmentInfo() && + dl->getInlinedAt() == IA; } /// Merge equivalence classes. @@ -271,8 +275,9 @@ void mapVirtRegs(LDVImpl *LDV); /// Add a definition point to this value. - void addDef(SlotIndex Idx, const MachineOperand &LocMO) { - DbgValueLocation Loc(getLocationNo(LocMO)); + void addDef(SlotIndex Idx, const MachineOperand &LocMO, + const DIExpression &Expr) { + DbgValueLocation Loc(getLocationNo(LocMO), Expr); // Add a singular (Idx,Idx) -> Loc mapping. LocMap::iterator I = locInts.find(Idx); if (!I.valid() || I.start() != Idx) @@ -306,11 +311,11 @@ /// possible. /// /// \param LI Scan for copies of the value in LI->reg. - /// \param LocNo Location number of LI->reg. + /// \param Loc Location number of LI->reg. /// \param Kills Points where the range of LocNo could be extended. /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here. void addDefsFromCopies( - LiveInterval *LI, unsigned LocNo, + LiveInterval *LI, DbgValueLocation Loc, const SmallVectorImpl &Kills, SmallVectorImpl> &NewDefs, MachineRegisterInfo &MRI, LiveIntervals &LIS); @@ -645,11 +650,11 @@ UserValue *UV = getUserValue(Var, Expr, MI.getDebugLoc()); if (!Discard) - UV->addDef(Idx, MI.getOperand(0)); + UV->addDef(Idx, MI.getOperand(0), *Expr); else { MachineOperand MO = MachineOperand::CreateReg(0U, false); MO.setIsDebug(); - UV->addDef(Idx, MO); + UV->addDef(Idx, MO, *Expr); } return true; } @@ -757,7 +762,7 @@ } void UserValue::addDefsFromCopies( - LiveInterval *LI, unsigned LocNo, + LiveInterval *LI, DbgValueLocation Loc, const SmallVectorImpl &Kills, SmallVectorImpl> &NewDefs, MachineRegisterInfo &MRI, LiveIntervals &LIS) { @@ -787,7 +792,7 @@ // it, or we are looking at a wrong value of LI. SlotIndex Idx = LIS.getInstructionIndex(*MI); LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); - if (!I.valid() || I.value().locNo() != LocNo) + if (!I.valid() || I.value() != Loc) continue; if (!LIS.hasInterval(DstReg)) @@ -821,7 +826,7 @@ MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); - DbgValueLocation NewLoc(LocNo); + DbgValueLocation NewLoc = Loc.changeLocNo(LocNo); I.insert(Idx, Idx.getNextSlot(), NewLoc); NewDefs.push_back(std::make_pair(Idx, NewLoc)); break; @@ -869,7 +874,7 @@ // sub-register in that regclass). For now, simply skip handling copies if // a sub-register is involved. if (LI && !LocMO.getSubReg()) - addDefsFromCopies(LI, Loc.locNo(), Kills, Defs, MRI, LIS); + addDefsFromCopies(LI, Loc, Kills, Defs, MRI, LIS); continue; } @@ -1297,7 +1302,7 @@ // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate // that the original virtual register was a pointer. Also, add the stack slot // offset for the spilled register to the expression. - const DIExpression *Expr = Expression; + const DIExpression *Expr = Loc.getExpression(); if (Spilled) Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, SpillOffset); Index: llvm/test/DebugInfo/X86/dbg-addr-dse.ll =================================================================== --- llvm/test/DebugInfo/X86/dbg-addr-dse.ll +++ llvm/test/DebugInfo/X86/dbg-addr-dse.ll @@ -3,10 +3,6 @@ ; RUN: FileCheck %s < %t.s --check-prefix=ASM ; RUN: llvm-dwarfdump %t.o | FileCheck %s --check-prefix=DWARF -; XFAIL: * -; See PR41992, the third and final dbg.value disappears after -; LiveDebugVariables. - ; In this example, the variable lives mostly in memory, but at the point of the ; assignment to global, it lives nowhere, and is described as the constant ; value 1. Index: llvm/test/DebugInfo/X86/live-debug-vars-dse.mir =================================================================== --- llvm/test/DebugInfo/X86/live-debug-vars-dse.mir +++ llvm/test/DebugInfo/X86/live-debug-vars-dse.mir @@ -1,9 +1,5 @@ # RUN: llc -start-after=machine-scheduler %s -o - | FileCheck %s -# XFAIL: * -# See PR41992, the third and final dbg.value disappears after -# LiveDebugVariables. - # C source: # void escape(int *); # extern int global; Index: llvm/test/DebugInfo/X86/live-debug-vars-intervals.mir =================================================================== --- /dev/null +++ llvm/test/DebugInfo/X86/live-debug-vars-intervals.mir @@ -0,0 +1,154 @@ +# These tests check that DBG_VALUE intervals are correctly coalesced (or not) in +# LiveDebugVariables. +# NOTE: We do not currently handle partially overlapping fragments in LDV. +# +# The IR in this file has been modified by hand, generated from this source: +# void escape(int *); +# extern int global; +# void f(int x) { +# escape(&x); +# x = 1; +# global = x; +# x = 2; +# escape(&x); +# } + +# RUN: llc %s -start-before=machine-scheduler -stop-after=virtregrewriter -o - \ +# RUN: | FileCheck %s --implicit-check-not=DBG_VALUE + +# Verify that DBG_VALUEs with same { Variable, Fragment } but different DIExpressions +# are not coalesced. +# +# DV1 %0 "x" (deref) --+-- Do not coalesce these because DV2 refers to the same bits. +# DV2 %0 "x" () | +# DV3 %0 "x" (deref) --+ +# +# CHECK: name: f1 +# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR:[0-9]+]], !DIExpression(DW_OP_deref) +# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR]], !DIExpression() +# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR]], !DIExpression(DW_OP_deref) + +# Verify that DBG_VALUEs with same { Variable, Fragment } and { Location, DIExpression } are +# coalesced if there are no intervening DBG_VALUEs with the same { Variable, Fragment }. +# +# DV1 %0 "x" (fragment 00 16) --+-- Coalesce these because DV2 refers to different bits. +# DV2 %0 "x" (fragment 16 16) | +# DV3 %0 "x" (fragment 00 16) --+ +# +# CHECK: name: f2 +# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16) +# CHECK: DBG_VALUE %stack.0.x.addr, $noreg, ![[VAR]], !DIExpression(DW_OP_LLVM_fragment, 16, 16) + +--- | + target triple = "x86_64-unknown-linux-gnu" + + @global = external global i32, align 4 + declare void @llvm.dbg.value(metadata, metadata, metadata) + declare void @escape(i32*) + + define void @f1(i32 %x) !dbg !6 { + entry: + %x.addr = alloca i32, align 4 + store i32 %x, i32* %x.addr, align 4 + call void @llvm.dbg.value(metadata i32* %x.addr, metadata !11, metadata !DIExpression(DW_OP_deref)), !dbg !12 + call void @escape(i32* %x.addr), !dbg !12 + call void @llvm.dbg.value(metadata i32* %x.addr, metadata !11, metadata !DIExpression()), !dbg !12 + store i32 1, i32* @global, align 4, !dbg !12 + call void @llvm.dbg.value(metadata i32* %x.addr, metadata !11, metadata !DIExpression(DW_OP_deref)), !dbg !12 + store i32 2, i32* %x.addr, align 4, !dbg !12 + call void @escape(i32* %x.addr), !dbg !12 + ret void, !dbg !12 + } + + define void @f2(i32 %x) !dbg !13 { + entry: + %x.addr = alloca i32, align 4 + store i32 %x, i32* %x.addr, align 4 + call void @llvm.dbg.value(metadata i32* %x.addr, metadata !14, metadata !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16)), !dbg !15 + call void @escape(i32* %x.addr) + call void @llvm.dbg.value(metadata i32* %x.addr, metadata !14, metadata !DIExpression(DW_OP_LLVM_fragment, 16, 16)), !dbg !15 + store i32 1, i32* @global, align 4 + call void @llvm.dbg.value(metadata i32* %x.addr, metadata !14, metadata !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16)), !dbg !15 + store i32 2, i32* %x.addr, align 4 + call void @escape(i32* %x.addr) + ret void + } + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!3, !4} + !llvm.ident = !{!5} + + !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) + !1 = !DIFile(filename: "test.c", directory: "") + !2 = !{} + !3 = !{i32 2, !"Dwarf Version", i32 4} + !4 = !{i32 2, !"Debug Info Version", i32 3} + !5 = !{!"clang version 11.0.0 "} + !6 = distinct !DISubprogram(name: "f1", scope: !1, file: !1, line: 3, type: !7, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !10) + !7 = !DISubroutineType(types: !8) + !8 = !{null, !9} + !9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !10 = !{!11} + !11 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !1, line: 3, type: !9) + !12 = !DILocation(line: 3, column: 12, scope: !6) + !13 = distinct !DISubprogram(name: "f2", scope: !1, file: !1, line: 20, type: !7, scopeLine: 20, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !10) + !14 = !DILocalVariable(name: "x", arg: 1, scope: !13, file: !1, line: 21, type: !9) + !15 = !DILocation(line: 23, column: 12, scope: !13) + +... +--- +name: f1 +tracksRegLiveness: true +stack: + - { id: 0, name: x.addr, type: default, offset: 0, size: 4, alignment: 4, + stack-id: default, callee-saved-register: '', callee-saved-restored: true, + debug-info-variable: '', debug-info-expression: '', debug-info-location: '' } +body: | + bb.0.entry: + liveins: $edi + %0:gr32 = COPY $edi + MOV32mr %stack.0.x.addr, 1, $noreg, 0, $noreg, %0 :: (store 4 into %ir.x.addr) + DBG_VALUE %stack.0.x.addr, $noreg, !11, !DIExpression(DW_OP_deref), debug-location !12 + ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12 + %1:gr64 = LEA64r %stack.0.x.addr, 1, $noreg, 0, $noreg + $rdi = COPY %1, debug-location !12 + CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, debug-location !12 + ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12 + DBG_VALUE %stack.0.x.addr, $noreg, !11, !DIExpression(), debug-location !12 + MOV32mi $rip, 1, $noreg, @global, $noreg, 1, debug-location !12 :: (store 4 into @global) + DBG_VALUE %stack.0.x.addr, $noreg, !11, !DIExpression(DW_OP_deref), debug-location !12 + MOV32mi %stack.0.x.addr, 1, $noreg, 0, $noreg, 2, debug-location !12 :: (store 4 into %ir.x.addr) + ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12 + $rdi = COPY %1, debug-location !12 + CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp, debug-location !12 + ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp, debug-location !12 + RET 0, debug-location !12 +... +--- +name: f2 +tracksRegLiveness: true +stack: + - { id: 0, name: x.addr, type: default, offset: 0, size: 4, alignment: 4, + stack-id: default, callee-saved-register: '', callee-saved-restored: true, + debug-info-variable: '', debug-info-expression: '', debug-info-location: '' } +body: | + bb.0.entry: + liveins: $edi + %0:gr32 = COPY $edi + MOV32mr %stack.0.x.addr, 1, $noreg, 0, $noreg, %0 :: (store 4 into %ir.x.addr) + DBG_VALUE %stack.0.x.addr, $noreg, !14, !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16), debug-location !15 + ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp + %1:gr64 = LEA64r %stack.0.x.addr, 1, $noreg, 0, $noreg + $rdi = COPY %1 + CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp + ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp + DBG_VALUE %stack.0.x.addr, $noreg, !14, !DIExpression(DW_OP_LLVM_fragment, 16, 16), debug-location !15 + MOV32mi $rip, 1, $noreg, @global, $noreg, 1 :: (store 4 into @global) + DBG_VALUE %stack.0.x.addr, $noreg, !14, !DIExpression(DW_OP_deref, DW_OP_LLVM_fragment, 0, 16), debug-location !15 + MOV32mi %stack.0.x.addr, 1, $noreg, 0, $noreg, 2 :: (store 4 into %ir.x.addr) + ADJCALLSTACKDOWN64 0, 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp + $rdi = COPY %1 + CALL64pcrel32 @escape, csr_64, implicit $rsp, implicit $ssp, implicit $rdi, implicit-def $rsp, implicit-def $ssp + ADJCALLSTACKUP64 0, 0, implicit-def dead $rsp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $rsp, implicit $ssp + RET 0 +... Index: llvm/test/tools/llvm-locstats/locstats.ll =================================================================== --- llvm/test/tools/llvm-locstats/locstats.ll +++ llvm/test/tools/llvm-locstats/locstats.ll @@ -11,9 +11,9 @@ ; LOCSTATS: [30%,40%) 0 0% ; LOCSTATS: [40%,50%) 1 11% ; LOCSTATS: [50%,60%) 1 11% -; LOCSTATS: [60%,70%) 0 0% +; LOCSTATS: [60%,70%) 1 11% ; LOCSTATS: [70%,80%) 0 0% -; LOCSTATS: [80%,90%) 3 33% +; LOCSTATS: [80%,90%) 2 22% ; LOCSTATS: [90%,100%) 1 11% ; LOCSTATS: 100% 2 22% ;