<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [BDCE] does not salvage/invalidate debuginfo when rewriting sext as zext"
   href="https://bugs.llvm.org/show_bug.cgi?id=47296">47296</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[BDCE] does not salvage/invalidate debuginfo when rewriting sext as zext
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bjorn.a.pettersson@ericsson.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The patch from <a href="https://reviews.llvm.org/D60413">https://reviews.llvm.org/D60413</a> (commit 7bf168390fd05460b1c0df)
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 (<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a>
2b37174b9a5db235e493cb72e4454cc08a1b1791)", 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 (<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a>
2b37174b9a5db235e493cb72e4454cc08a1b1791)"}
!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: "<source>", 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)
;-----------------------------------------------------------------------------</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>