Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BDCE] does not salvage/invalidate debuginfo when rewriting sext as zext #46640

Open
bjope opened this issue Aug 24, 2020 · 0 comments
Open

[BDCE] does not salvage/invalidate debuginfo when rewriting sext as zext #46640

bjope opened this issue Aug 24, 2020 · 0 comments
Labels
bugzilla Issues migrated from bugzilla llvm:instcombine wrong-debug

Comments

@bjope
Copy link
Collaborator

bjope commented Aug 24, 2020

Bugzilla Link 47296
Version trunk
OS Linux
Blocks #38116
CC @DMG862,@jmorse,@jdm

Extended Description

The patch from https://reviews.llvm.org/D60413 (commit 7bf1683)
teached bit-tracking dead code elimination to rewrite:

%6 = sext i32 %5 to i64, !dbg !​26
call void @​llvm.dbg.value(metadata i64 %6, metadata !​18, metadata !DIExpression()), !dbg !​27

as

%6 = zext i32 %5 to i64, !dbg !​26
call void @​llvm.dbg.value(metadata i64 %6, metadata !​18, metadata !DIExpression()), !dbg !​27

when no uses of %6 depended on the most significant bits.

However, since the metadata references in the dbg.value intrinsics aren't dealt with in any special way, a debugger would print the wrong value for variable !​18 (showing a zero extended value instead of a sign extended value).

I figure that it would be more correct to invalidate the debug-uses (presenting an undef value), or somehow magically "salvage" the correct value by some fancy DIExpression based on %5.

Here is an example with full IR (compile with "opt -bdce"):

;-----------------------------------------------------------------------------
; ModuleID = './example.ll'
source_filename = "./example.ll"

; Function Attrs: nounwind uwtable
define i32 @​foo(i32 %0, i32 %1, i32* %2, i32* %3) #​0 !dbg !​7 {
%5 = add nsw i32 %0, 2, !dbg !​25
%6 = sext i32 %5 to i64, !dbg !​26
call void @​llvm.dbg.value(metadata i64 %6, metadata !​18, metadata !DIExpression()), !dbg !​27
%7 = icmp sgt i32 %1, 0, !dbg !​28
br i1 %7, label %8, label %11, !dbg !​29

8: ; preds = %4
%9 = or i64 %6, 5, !dbg !​30
call void @​llvm.dbg.value(metadata i64 %9, metadata !​20, metadata !DIExpression()), !dbg !​31
%10 = trunc i64 %9 to i32, !dbg !​32
store i32 %10, i32* %2, align 4, !dbg !​33, !tbaa !​34
br label %14, !dbg !​38

11: ; preds = %4
%12 = add nsw i64 %6, 3, !dbg !​39
call void @​llvm.dbg.value(metadata i64 %12, metadata !​23, metadata !DIExpression()), !dbg !​40
%13 = trunc i64 %12 to i32, !dbg !​41
store i32 %13, i32* %3, align 4, !dbg !​42, !tbaa !​34
br label %14

14: ; preds = %11, %8
ret i32 undef, !dbg !​43
}

; Function Attrs: nounwind readnone speculatable willreturn
declare void @​llvm.dbg.value(metadata, metadata, metadata) #​1

attributes #​0 = { nounwind uwtable }
attributes #​1 = { nounwind readnone speculatable willreturn }

!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 12.0.0 (https://github.com/llvm/llvm-project.git 2b37174)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !​2, splitDebugInlining: false, nameTableKind: None)
!​1 = !DIFile(filename: "example.c", directory: "/home/ce")
!​2 = !{}
!​3 = !{i32 7, !"Dwarf Version", i32 4}
!​4 = !{i32 2, !"Debug Info Version", i32 3}
!​5 = !{i32 1, !"wchar_size", i32 4}
!​6 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git 2b37174)"}
!​7 = distinct !DISubprogram(name: "foo", scope: !​8, file: !​8, line: 2, type: !​9, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !​0, retainedNodes: !​13)
!​8 = !DIFile(filename: "", directory: "/home/ce")
!​9 = !DISubroutineType(types: !​10)
!​10 = !{#11, !​11, !​11, !​12, !​12}
!​11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!​12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !​11, size: 64)
!​13 = !{#14, !​15, !​16, !​17, !​18, !​20, !​23}
!​14 = !DILocalVariable(name: "X", arg: 1, scope: !​7, file: !​8, line: 2, type: !​11)
!​15 = !DILocalVariable(name: "Y", arg: 2, scope: !​7, file: !​8, line: 2, type: !​11)
!​16 = !DILocalVariable(name: "Z", arg: 3, scope: !​7, file: !​8, line: 2, type: !​12)
!​17 = !DILocalVariable(name: "W", arg: 4, scope: !​7, file: !​8, line: 2, type: !​12)
!​18 = !DILocalVariable(name: "LX", scope: !​7, file: !​8, line: 3, type: !​19)
!​19 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
!​20 = !DILocalVariable(name: "T", scope: !​21, file: !​8, line: 5, type: !​19)
!​21 = distinct !DILexicalBlock(scope: !​22, file: !​8, line: 4, column: 16)
!​22 = distinct !DILexicalBlock(scope: !​7, file: !​8, line: 4, column: 9)
!​23 = !DILocalVariable(name: "T", scope: !​24, file: !​8, line: 8, type: !​19)
!​24 = distinct !DILexicalBlock(scope: !​22, file: !​8, line: 7, column: 12)
!​25 = !DILocation(line: 3, column: 17, scope: !​7)
!​26 = !DILocation(line: 3, column: 15, scope: !​7)
!​27 = !DILocation(line: 0, scope: !​7)
!​28 = !DILocation(line: 4, column: 11, scope: !​22)
!​29 = !DILocation(line: 4, column: 9, scope: !​7)
!​30 = !DILocation(line: 5, column: 21, scope: !​21)
!​31 = !DILocation(line: 0, scope: !​21)
!​32 = !DILocation(line: 6, column: 13, scope: !​21)
!​33 = !DILocation(line: 6, column: 11, scope: !​21)
!​34 = !{#35, !​35, i64 0}
!​35 = !{!"int", !​36, i64 0}
!​36 = !{!"omnipotent char", !​37, i64 0}
!​37 = !{!"Simple C/C++ TBAA"}
!​38 = !DILocation(line: 7, column: 5, scope: !​21)
!​39 = !DILocation(line: 8, column: 21, scope: !​24)
!​40 = !DILocation(line: 0, scope: !​24)
!​41 = !DILocation(line: 9, column: 13, scope: !​24)
!​42 = !DILocation(line: 9, column: 11, scope: !​24)
!​43 = !DILocation(line: 11, column: 1, scope: !​7)
;-----------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla llvm:instcombine wrong-debug
Projects
None yet
Development

No branches or pull requests

2 participants